]>
Commit | Line | Data |
---|---|---|
cc73685d | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0ad19a3f | 2 | |
d38dd64a CB |
3 | #ifndef _GNU_SOURCE |
4 | #define _GNU_SOURCE 1 | |
5 | #endif | |
0ad19a3f | 6 | #include <dirent.h> |
7 | #include <errno.h> | |
b0a33c1e | 8 | #include <fcntl.h> |
c476bdce | 9 | #include <grp.h> |
37515ebd | 10 | #include <poll.h> |
b467714b | 11 | #include <pthread.h> |
f3787121 CB |
12 | #include <signal.h> |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
0ad19a3f | 16 | #include <sys/file.h> |
f4d507d5 | 17 | #include <sys/mount.h> |
f3787121 | 18 | #include <sys/param.h> |
0ad19a3f | 19 | #include <sys/prctl.h> |
f3787121 CB |
20 | #include <sys/socket.h> |
21 | #include <sys/stat.h> | |
22 | #include <sys/syscall.h> | |
ddceb1f9 | 23 | #include <sys/types.h> |
b0a33c1e | 24 | #include <sys/un.h> |
f3787121 | 25 | #include <sys/wait.h> |
d38dd64a | 26 | #include <unistd.h> |
495d2046 | 27 | |
f3787121 CB |
28 | #include "af_unix.h" |
29 | #include "caps.h" | |
c988c8b1 CB |
30 | #include "cgroups/cgroup.h" |
31 | #include "cgroups/cgroup_utils.h" | |
f3787121 | 32 | #include "commands.h" |
bbf5cf35 | 33 | #include "commands_utils.h" |
59eac805 | 34 | #include "compiler.h" |
f3787121 | 35 | #include "conf.h" |
d38dd64a | 36 | #include "config.h" |
74c6e2b0 | 37 | #include "confile_utils.h" |
e2bcd7db | 38 | #include "error.h" |
7fbb15ec | 39 | #include "file_utils.h" |
1cc8bd4b | 40 | #include "list.h" |
f3787121 | 41 | #include "log.h" |
d38dd64a | 42 | #include "lsm/lsm.h" |
f2d5a09d | 43 | #include "lxccontainer.h" |
f3787121 CB |
44 | #include "lxclock.h" |
45 | #include "lxcseccomp.h" | |
3ef9b3d3 | 46 | #include "macro.h" |
f3787121 | 47 | #include "mainloop.h" |
4ffeaf27 | 48 | #include "memory_utils.h" |
63376d7d | 49 | #include "monitor.h" |
f549edcc | 50 | #include "namespace.h" |
811ef482 | 51 | #include "network.h" |
f40988c7 | 52 | #include "process_utils.h" |
f3787121 | 53 | #include "start.h" |
6be5397b CB |
54 | #include "storage/storage.h" |
55 | #include "storage/storage_utils.h" | |
0ed9b1bc | 56 | #include "sync.h" |
59524108 | 57 | #include "syscall_wrappers.h" |
0ed9b1bc CB |
58 | #include "terminal.h" |
59 | #include "utils.h" | |
36eb9bde | 60 | |
d38dd64a CB |
61 | #if HAVE_LIBCAP |
62 | #include <sys/capability.h> | |
63 | #endif | |
64 | ||
9de31d5a | 65 | #ifndef HAVE_STRLCPY |
58db1a61 | 66 | #include "strlcpy.h" |
9de31d5a CB |
67 | #endif |
68 | ||
ac2cecc4 | 69 | lxc_log_define(start, lxc); |
36eb9bde | 70 | |
f01f7975 | 71 | extern void mod_all_rdeps(struct lxc_container *c, bool inc); |
790255cf | 72 | static bool do_destroy_container(struct lxc_handler *handler); |
28272964 CB |
73 | static int lxc_rmdir_onedev_wrapper(void *data); |
74 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
75 | const char *name); | |
76 | ||
c8154066 SH |
77 | static void print_top_failing_dir(const char *path) |
78 | { | |
dcd9a847 | 79 | __do_free char *copy = NULL; |
9e5f5f2f | 80 | int ret; |
4ffeaf27 | 81 | char *e, *p, saved; |
cbb9c7c7 | 82 | |
4ffeaf27 | 83 | copy = must_copy_string(path); |
c8154066 | 84 | p = copy; |
4ffeaf27 | 85 | e = copy + strlen(path); |
cbb9c7c7 | 86 | |
c8154066 | 87 | while (p < e) { |
f3787121 CB |
88 | while (p < e && *p == '/') |
89 | p++; | |
9e5f5f2f | 90 | |
f3787121 CB |
91 | while (p < e && *p != '/') |
92 | p++; | |
9e5f5f2f | 93 | |
c8154066 SH |
94 | saved = *p; |
95 | *p = '\0'; | |
9e5f5f2f CB |
96 | |
97 | ret = access(copy, X_OK); | |
98 | if (ret != 0) { | |
818a57fc | 99 | SYSERROR("Could not access %s. Please grant it x access, or add an ACL for the container " "root", copy); |
c8154066 SH |
100 | return; |
101 | } | |
102 | *p = saved; | |
103 | } | |
104 | } | |
105 | ||
5032bf39 | 106 | static void lxc_put_nsfds(struct lxc_handler *handler) |
408da065 | 107 | { |
818a57fc | 108 | for (int i = 0; i < LXC_NS_MAX; i++) { |
5032bf39 | 109 | if (handler->nsfd[i] < 0) |
39cd919c CB |
110 | continue; |
111 | ||
818a57fc | 112 | close_prot_errno_disarm(handler->nsfd[i]); |
9f30a190 | 113 | } |
9f30a190 MM |
114 | } |
115 | ||
8db6be1b CB |
116 | static int lxc_try_preserve_namespace(struct lxc_handler *handler, |
117 | lxc_namespace_t idx, const char *ns) | |
4cb53844 | 118 | { |
8db6be1b CB |
119 | __do_close int fd = -EBADF; |
120 | int ret; | |
4cb53844 | 121 | |
8db6be1b | 122 | fd = lxc_preserve_ns(handler->pid, ns); |
857ba1f0 CB |
123 | if (fd < 0) |
124 | return -errno; | |
4cb53844 | 125 | |
8db6be1b CB |
126 | ret = strnprintf(handler->nsfd_paths[idx], |
127 | sizeof(handler->nsfd_paths[idx]), "%s:/proc/%d/fd/%d", | |
128 | ns_info[idx].proc_name, handler->monitor_pid, fd); | |
cb3b010c CB |
129 | if (ret < 0) |
130 | return ret_errno(EIO); | |
8db6be1b | 131 | |
cb3b010c CB |
132 | /* |
133 | * In case LXC is configured for exposing information to hooks as | |
134 | * argv-style arguments prepare an argv array we can use. | |
135 | */ | |
8db6be1b CB |
136 | handler->hook_argv[handler->hook_argc] = handler->nsfd_paths[idx]; |
137 | handler->hook_argc++; | |
8db6be1b CB |
138 | |
139 | DEBUG("Preserved %s namespace via fd %d and stashed path as %s", | |
140 | ns_info[idx].proc_name, fd, handler->nsfd_paths[idx]); | |
141 | ||
142 | handler->nsfd[idx] = move_fd(fd); | |
143 | return 0; | |
4cb53844 CB |
144 | } |
145 | ||
146 | /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace | |
147 | * specified in ns_clone_flags. | |
4d8ac866 | 148 | * Return true on success, false on failure. |
62d05d9b | 149 | */ |
4cb53844 | 150 | static bool lxc_try_preserve_namespaces(struct lxc_handler *handler, |
8db6be1b | 151 | int ns_clone_flags) |
f3787121 | 152 | { |
8db6be1b CB |
153 | for (lxc_namespace_t ns_idx = 0; ns_idx < LXC_NS_MAX; ns_idx++) |
154 | handler->nsfd[ns_idx] = -EBADF; | |
9f30a190 | 155 | |
8db6be1b CB |
156 | for (lxc_namespace_t ns_idx = 0; ns_idx < LXC_NS_MAX; ns_idx++) { |
157 | int ret; | |
857ba1f0 | 158 | const char *ns = ns_info[ns_idx].proc_name; |
4cb53844 | 159 | |
8db6be1b | 160 | if ((ns_clone_flags & ns_info[ns_idx].clone_flag) == 0) |
9f30a190 | 161 | continue; |
28d9e29e | 162 | |
8db6be1b CB |
163 | ret = lxc_try_preserve_namespace(handler, ns_idx, |
164 | ns_info[ns_idx].proc_name); | |
165 | if (ret < 0) { | |
857ba1f0 CB |
166 | if (ret == -ENOENT) { |
167 | SYSERROR("Kernel does not support preserving %s namespaces", ns); | |
168 | continue; | |
169 | } | |
170 | ||
9beaca55 CB |
171 | /* |
172 | * Handle kernels that do not support interacting with | |
173 | * namespaces through procfs. | |
4cb53844 | 174 | */ |
4cb53844 | 175 | lxc_put_nsfds(handler); |
857ba1f0 | 176 | return log_error_errno(false, errno, "Failed to preserve %s namespace", ns); |
4cb53844 | 177 | } |
9f30a190 MM |
178 | } |
179 | ||
62d05d9b | 180 | return true; |
9f30a190 MM |
181 | } |
182 | ||
ec1dc633 | 183 | static inline bool match_stdfds(int fd) |
80090207 | 184 | { |
ec1dc633 | 185 | return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); |
80090207 CLG |
186 | } |
187 | ||
4c03724e | 188 | #ifdef HAVE_DLOG |
189 | static bool match_dlog_fds(struct dirent *direntp) | |
190 | { | |
191 | char path[PATH_MAX] = {0}; | |
192 | char link[PATH_MAX] = {0}; | |
193 | ssize_t linklen; | |
194 | int ret; | |
195 | ||
fa60cd7b CB |
196 | ret = strnprintf(path, sizeof(path), "/proc/self/fd/%s", direntp->d_name); |
197 | if (ret < 0) | |
818a57fc | 198 | return log_error(false, "Failed to create file descriptor name"); |
4c03724e | 199 | |
200 | linklen = readlink(path, link, PATH_MAX); | |
818a57fc CB |
201 | if (linklen < 0) |
202 | return log_error(false, "Failed to read link path - \"%s\"", path); | |
203 | else if (linklen >= PATH_MAX) | |
204 | return log_error(false, "The name of link path is too long - \"%s\"", path); | |
205 | ||
d0269705 CB |
206 | if (strequal(link, "/dev/log_main") || |
207 | strequal(link, "/dev/log_system") || | |
208 | strequal(link, "/dev/log_radio")) | |
4c03724e | 209 | return true; |
210 | ||
211 | return false; | |
212 | } | |
213 | #endif | |
214 | ||
46abf219 RJ |
215 | /* Parses the LISTEN_FDS environment variable value. |
216 | * The returned value is the highest fd number up to which the | |
217 | * file descriptors must be passed to the container process. | |
218 | * | |
219 | * For example, if LISTEN_FDS=2 then 4 is returned and file descriptors 3 and 4 | |
220 | * MUST be passed to the container process (in addition to the standard streams) | |
221 | * to support [socket activation][systemd-listen-fds]. | |
222 | */ | |
223 | static unsigned int get_listen_fds_max(void) | |
224 | { | |
225 | int ret; | |
226 | unsigned int num_fds; | |
227 | const char *val; | |
228 | ||
229 | val = getenv("LISTEN_FDS"); | |
230 | if (!val) | |
231 | return 0; | |
232 | ||
233 | ret = lxc_safe_uint(val, &num_fds); | |
234 | if (ret < 0) | |
235 | return syserror_ret(0, "Failed to parse \"LISTEN_FDS=%s\" environment variable", val); | |
236 | ||
237 | return log_trace(num_fds, "Parsed \"LISTEN_FDS=%s\" environment variable", val); | |
238 | } | |
239 | ||
47a46cf1 CB |
240 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, |
241 | int *fds_to_ignore, size_t len_fds) | |
80090207 | 242 | { |
80090207 | 243 | int fd, fddir; |
47a46cf1 | 244 | size_t i; |
80090207 | 245 | DIR *dir; |
a5a70219 | 246 | struct dirent *direntp; |
46abf219 | 247 | unsigned int listen_fds_max; |
d2e75eba | 248 | struct lxc_state_client *client, *nclient; |
80090207 | 249 | |
d2cf4c37 SH |
250 | if (conf && conf->close_all_fds) |
251 | closeall = true; | |
252 | ||
46abf219 RJ |
253 | listen_fds_max = get_listen_fds_max(); |
254 | ||
4e2d6b9a CB |
255 | /* |
256 | * Disable syslog at this point to avoid the above logging | |
257 | * function to open a new fd and make the check_inherited function | |
258 | * enter an infinite loop. | |
259 | */ | |
260 | lxc_log_syslog_disable(); | |
261 | ||
b119f362 | 262 | restart: |
80090207 | 263 | dir = opendir("/proc/self/fd"); |
818a57fc CB |
264 | if (!dir) |
265 | return log_warn(-1, "Failed to open directory"); | |
80090207 CLG |
266 | |
267 | fddir = dirfd(dir); | |
268 | ||
74f96976 | 269 | while ((direntp = readdir(dir))) { |
a5a70219 | 270 | int ret; |
d39b10eb CB |
271 | bool matched = false; |
272 | ||
d0269705 | 273 | if (strequal(direntp->d_name, ".")) |
80090207 CLG |
274 | continue; |
275 | ||
d0269705 | 276 | if (strequal(direntp->d_name, "..")) |
80090207 CLG |
277 | continue; |
278 | ||
a5a70219 CB |
279 | ret = lxc_safe_int(direntp->d_name, &fd); |
280 | if (ret < 0) { | |
281 | INFO("Could not parse file descriptor for \"%s\"", direntp->d_name); | |
d4cff0d2 CB |
282 | continue; |
283 | } | |
80090207 | 284 | |
47a46cf1 CB |
285 | for (i = 0; i < len_fds; i++) |
286 | if (fds_to_ignore[i] == fd) | |
287 | break; | |
288 | ||
289 | if (fd == fddir || fd == lxc_log_fd || | |
290 | (i < len_fds && fd == fds_to_ignore[i])) | |
80090207 CLG |
291 | continue; |
292 | ||
d39b10eb | 293 | /* Keep state clients that wait on reboots. */ |
3ee9e4fb | 294 | if (conf) { |
d2e75eba | 295 | list_for_each_entry_safe(client, nclient, &conf->state_clients, head) { |
3ee9e4fb CB |
296 | if (client->clientfd != fd) |
297 | continue; | |
d39b10eb | 298 | |
3ee9e4fb CB |
299 | matched = true; |
300 | break; | |
301 | } | |
d39b10eb CB |
302 | } |
303 | ||
304 | if (matched) | |
305 | continue; | |
306 | ||
858377e4 SH |
307 | if (current_config && fd == current_config->logfd) |
308 | continue; | |
309 | ||
ec1dc633 | 310 | if (match_stdfds(fd)) |
80090207 | 311 | continue; |
80090207 | 312 | |
1d5e5f26 | 313 | #ifdef HAVE_DLOG |
314 | if (match_dlog_fds(direntp)) | |
315 | continue; | |
316 | ||
317 | #endif | |
46abf219 RJ |
318 | |
319 | if (fd <= listen_fds_max) { | |
320 | INFO("Inheriting fd %d (using the LISTEN_FDS environment variable)", fd); | |
321 | continue; | |
322 | } | |
323 | ||
d2cf4c37 | 324 | if (closeall) { |
4e2d6b9a CB |
325 | if (close(fd)) |
326 | SYSINFO("Closed inherited fd %d", fd); | |
327 | else | |
328 | INFO("Closed inherited fd %d", fd); | |
b119f362 | 329 | closedir(dir); |
b119f362 SH |
330 | goto restart; |
331 | } | |
28d9e29e | 332 | WARN("Inherited fd %d", fd); |
80090207 | 333 | } |
4e2d6b9a | 334 | closedir(dir); |
80090207 | 335 | |
4e2d6b9a CB |
336 | /* |
337 | * Only enable syslog at this point to avoid the above logging | |
338 | * function to open a new fd and make the check_inherited function | |
339 | * enter an infinite loop. | |
64c57ea1 | 340 | */ |
4e2d6b9a | 341 | lxc_log_syslog_enable(); |
64c57ea1 | 342 | |
92c7f629 | 343 | return 0; |
80090207 CLG |
344 | } |
345 | ||
83ee7875 | 346 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 347 | { |
7288dfb6 | 348 | int ret; |
b0a33c1e | 349 | sigset_t mask; |
7288dfb6 | 350 | const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH}; |
b0a33c1e | 351 | |
408da065 | 352 | /* Block everything except serious error signals. */ |
df0795b1 CB |
353 | ret = sigfillset(&mask); |
354 | if (ret < 0) | |
355 | return -EBADF; | |
356 | ||
9b10bef5 | 357 | for (int sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) { |
df0795b1 CB |
358 | ret = sigdelset(&mask, signals[sig]); |
359 | if (ret < 0) | |
360 | return -EBADF; | |
b0a33c1e | 361 | } |
362 | ||
b467714b | 363 | ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask); |
818a57fc CB |
364 | if (ret < 0) |
365 | return log_error_errno(-EBADF, errno, | |
366 | "Failed to set signal mask"); | |
b0a33c1e | 367 | |
df0795b1 | 368 | ret = signalfd(-1, &mask, SFD_CLOEXEC); |
818a57fc CB |
369 | if (ret < 0) |
370 | return log_error_errno(-EBADF, | |
371 | errno, "Failed to create signal file descriptor"); | |
b0a33c1e | 372 | |
df0795b1 | 373 | TRACE("Created signal file descriptor %d", ret); |
1ac470c0 | 374 | |
df0795b1 | 375 | return ret; |
b0a33c1e | 376 | } |
377 | ||
84c92abd | 378 | static int signal_handler(int fd, uint32_t events, void *data, |
3298b37d | 379 | struct lxc_async_descr *descr) |
b0a33c1e | 380 | { |
15cd25fd | 381 | int ret; |
3c319edb CB |
382 | siginfo_t info; |
383 | struct signalfd_siginfo siginfo; | |
384 | struct lxc_handler *hdlr = data; | |
15cd25fd | 385 | |
489f39be | 386 | ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo)); |
818a57fc CB |
387 | if (ret < 0) |
388 | return log_error(LXC_MAINLOOP_ERROR, "Failed to read signal info from signal file descriptor %d", fd); | |
15cd25fd | 389 | |
818a57fc | 390 | if (ret != sizeof(siginfo)) |
f7a97743 | 391 | return log_error(LXC_MAINLOOP_ERROR, "Unexpected size for struct signalfd_siginfo"); |
15cd25fd | 392 | |
408da065 | 393 | /* Check whether init is running. */ |
80507ee8 | 394 | info.si_pid = 0; |
3c319edb CB |
395 | ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG); |
396 | if (ret == 0 && info.si_pid == hdlr->pid) | |
397 | hdlr->init_died = true; | |
80507ee8 | 398 | |
543d2f83 CB |
399 | TRACE("Received signal ssi_signo(%d) for ssi_pid(%d), si_signo(%d), si_pid(%d)", |
400 | siginfo.ssi_signo, siginfo.ssi_pid, info.si_signo, info.si_pid); | |
401 | ||
cd5177e9 TA |
402 | /* Try to figure out a reasonable exit status to report. */ |
403 | if (hdlr->init_died) { | |
404 | switch (info.si_code) { | |
405 | case CLD_EXITED: | |
406 | hdlr->exit_status = info.si_status << 8; | |
407 | break; | |
408 | case CLD_KILLED: | |
409 | case CLD_DUMPED: | |
410 | case CLD_STOPPED: | |
411 | hdlr->exit_status = info.si_status << 8 | 0x7f; | |
412 | break; | |
413 | case CLD_CONTINUED: | |
414 | /* Huh? The waitid() told us it's dead *and* continued? */ | |
415 | WARN("Init %d dead and continued?", hdlr->pid); | |
416 | hdlr->exit_status = 1; | |
417 | break; | |
418 | default: | |
20993a97 | 419 | ERROR("Unknown si_code: %d", info.si_code); |
55e1311e | 420 | hdlr->exit_status = 1; |
cd5177e9 TA |
421 | } |
422 | } | |
423 | ||
d4b5d7a8 | 424 | if (siginfo.ssi_signo == SIGHUP) { |
33942046 CB |
425 | if (hdlr->pidfd >= 0) |
426 | lxc_raw_pidfd_send_signal(hdlr->pidfd, SIGTERM, NULL, 0); | |
d9bb2fba CB |
427 | else |
428 | kill(hdlr->pid, SIGTERM); | |
d4b5d7a8 | 429 | INFO("Killing %d since terminal hung up", hdlr->pid); |
9b10bef5 CB |
430 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE |
431 | : LXC_MAINLOOP_CONTINUE; | |
d4b5d7a8 TA |
432 | } |
433 | ||
9cb94384 | 434 | if (siginfo.ssi_signo != SIGCHLD) { |
33942046 CB |
435 | if (hdlr->pidfd >= 0) |
436 | lxc_raw_pidfd_send_signal(hdlr->pidfd, | |
437 | siginfo.ssi_signo, NULL, 0); | |
d9bb2fba CB |
438 | else |
439 | kill(hdlr->pid, siginfo.ssi_signo); | |
9cb94384 | 440 | INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid); |
9b10bef5 CB |
441 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE |
442 | : LXC_MAINLOOP_CONTINUE; | |
9cb94384 TA |
443 | } |
444 | ||
3a9e949f TA |
445 | /* More robustness, protect ourself from a SIGCHLD sent |
446 | * by a process different from the container init. | |
447 | */ | |
448 | if (siginfo.ssi_pid != hdlr->pid) { | |
6e94162a CB |
449 | NOTICE("Received %d from pid %d instead of container init %d", |
450 | siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid); | |
9b10bef5 CB |
451 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE |
452 | : LXC_MAINLOOP_CONTINUE; | |
3a9e949f TA |
453 | } |
454 | ||
408da065 | 455 | if (siginfo.ssi_code == CLD_STOPPED) { |
6e94162a | 456 | INFO("Container init process was stopped"); |
9b10bef5 CB |
457 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE |
458 | : LXC_MAINLOOP_CONTINUE; | |
459 | } | |
460 | ||
461 | if (siginfo.ssi_code == CLD_CONTINUED) { | |
6e94162a | 462 | INFO("Container init process was continued"); |
9b10bef5 CB |
463 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE |
464 | : LXC_MAINLOOP_CONTINUE; | |
15cd25fd | 465 | } |
1ac470c0 | 466 | |
818a57fc | 467 | return log_debug(LXC_MAINLOOP_CLOSE, "Container init process %d exited", hdlr->pid); |
b0a33c1e | 468 | } |
469 | ||
974a8aba CB |
470 | int lxc_serve_state_clients(const char *name, struct lxc_handler *handler, |
471 | lxc_state_t state) | |
66aeffc7 | 472 | { |
d2e75eba CB |
473 | struct lxc_msg msg = { |
474 | .type = lxc_msg_state, | |
475 | .value = state, | |
476 | }; | |
9de31d5a | 477 | size_t retlen; |
dbc9832d | 478 | ssize_t ret; |
d2e75eba | 479 | struct lxc_state_client *client, *nclient; |
dbc9832d | 480 | |
974a8aba CB |
481 | if (state == THAWED) |
482 | handler->state = RUNNING; | |
483 | else | |
484 | handler->state = state; | |
485 | ||
9dfa4041 | 486 | TRACE("Set container state to %s", lxc_state2str(state)); |
dbc9832d | 487 | |
d2e75eba | 488 | if (list_empty(&handler->conf->state_clients)) |
818a57fc | 489 | return log_trace(0, "No state clients registered"); |
dbc9832d | 490 | |
9de31d5a CB |
491 | retlen = strlcpy(msg.name, name, sizeof(msg.name)); |
492 | if (retlen >= sizeof(msg.name)) | |
493 | return -E2BIG; | |
dbc9832d | 494 | |
d2e75eba | 495 | list_for_each_entry_safe(client, nclient, &handler->conf->state_clients, head) { |
d39b10eb | 496 | if (client->states[state] == 0) { |
9dfa4041 | 497 | TRACE("State %s not registered for state client %d", |
dbc9832d CB |
498 | lxc_state2str(state), client->clientfd); |
499 | continue; | |
500 | } | |
501 | ||
9dfa4041 | 502 | TRACE("Sending state %s to state client %d", |
dbc9832d CB |
503 | lxc_state2str(state), client->clientfd); |
504 | ||
7fbb15ec CB |
505 | ret = lxc_send_nointr(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL); |
506 | if (ret <= 0) | |
6d1400b5 | 507 | SYSERROR("Failed to send message to client"); |
dbc9832d CB |
508 | |
509 | /* kick client from list */ | |
d2e75eba | 510 | list_del(&client->head); |
300d1cb4 | 511 | close(client->clientfd); |
d2e75eba | 512 | free(client); |
dbc9832d | 513 | } |
dbc9832d | 514 | |
5e5576a4 CB |
515 | return 0; |
516 | } | |
517 | ||
518 | static int lxc_serve_state_socket_pair(const char *name, | |
519 | struct lxc_handler *handler, | |
520 | lxc_state_t state) | |
521 | { | |
522 | ssize_t ret; | |
523 | ||
bb955810 | 524 | if (!handler->daemonize || |
5e5576a4 CB |
525 | handler->state_socket_pair[1] < 0 || |
526 | state == STARTING) | |
527 | return 0; | |
528 | ||
529 | /* Close read end of the socket pair. */ | |
a783a414 | 530 | close_prot_errno_disarm(handler->state_socket_pair[0]); |
5e5576a4 | 531 | |
ee8377bd | 532 | again: |
5e5576a4 CB |
533 | ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1], |
534 | &(int){state}, sizeof(int)); | |
9044b79e | 535 | if (ret < 0) { |
536 | SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]); | |
537 | ||
ee8377bd CB |
538 | if (errno == EINTR) |
539 | goto again; | |
9044b79e | 540 | |
541 | return -1; | |
542 | } | |
543 | ||
818a57fc CB |
544 | if (ret != sizeof(int)) |
545 | return log_error(-1, "Message too long : %d", handler->state_socket_pair[1]); | |
5e5576a4 CB |
546 | |
547 | TRACE("Sent container state \"%s\" to %d", lxc_state2str(state), | |
548 | handler->state_socket_pair[1]); | |
549 | ||
550 | /* Close write end of the socket pair. */ | |
818a57fc | 551 | close_prot_errno_disarm(handler->state_socket_pair[1]); |
5e5576a4 CB |
552 | |
553 | return 0; | |
554 | } | |
555 | ||
556 | int lxc_set_state(const char *name, struct lxc_handler *handler, | |
557 | lxc_state_t state) | |
558 | { | |
559 | int ret; | |
560 | ||
561 | ret = lxc_serve_state_socket_pair(name, handler, state); | |
818a57fc CB |
562 | if (ret < 0) |
563 | return log_error(-1, "Failed to synchronize via anonymous pair of unix sockets"); | |
5e5576a4 CB |
564 | |
565 | ret = lxc_serve_state_clients(name, handler, state); | |
566 | if (ret < 0) | |
567 | return -1; | |
568 | ||
dbc9832d CB |
569 | /* This function will try to connect to the legacy lxc-monitord state |
570 | * server and only exists for backwards compatibility. | |
571 | */ | |
9123e471 | 572 | lxc_monitor_send_state(name, state, handler->lxcpath); |
dbc9832d | 573 | |
66aeffc7 DL |
574 | return 0; |
575 | } | |
576 | ||
735f2c6e | 577 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 578 | { |
3c319edb | 579 | int ret; |
543d2f83 | 580 | struct lxc_terminal *console = &handler->conf->console; |
3298b37d | 581 | struct lxc_async_descr descr, descr_console; |
b0a33c1e | 582 | |
543d2f83 CB |
583 | if (!wants_console(console)) |
584 | console = NULL; | |
86530b0a | 585 | |
3c319edb CB |
586 | ret = lxc_mainloop_open(&descr); |
587 | if (ret < 0) { | |
588 | ERROR("Failed to create mainloop"); | |
50c8bf05 | 589 | goto out_sigfd; |
b0a33c1e | 590 | } |
591 | ||
543d2f83 | 592 | if (console) { |
30a33fbd CB |
593 | ret = lxc_mainloop_open(&descr_console); |
594 | if (ret < 0) { | |
595 | ERROR("Failed to create console mainloop"); | |
596 | goto out_mainloop; | |
597 | } | |
3c319edb CB |
598 | } |
599 | ||
543d2f83 CB |
600 | ret = lxc_mainloop_add_handler(&descr, handler->sigfd, |
601 | signal_handler, | |
602 | default_cleanup_handler, | |
603 | handler, "signal_handler"); | |
3c319edb | 604 | if (ret < 0) { |
1cc8bd4b | 605 | ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd); |
3c319edb | 606 | goto out_mainloop_console; |
b0a33c1e | 607 | } |
608 | ||
2ac0f627 CB |
609 | ret = lxc_seccomp_setup_proxy(&handler->conf->seccomp, &descr, handler); |
610 | if (ret < 0) { | |
611 | ERROR("Failed to setup seccomp proxy"); | |
c3e3c21a | 612 | goto out_mainloop_console; |
2ac0f627 | 613 | } |
cdb2a47f | 614 | |
543d2f83 | 615 | if (console) { |
093bce5f | 616 | ret = lxc_terminal_mainloop_add(&descr, console); |
30a33fbd CB |
617 | if (ret < 0) { |
618 | ERROR("Failed to add console handlers to mainloop"); | |
619 | goto out_mainloop_console; | |
620 | } | |
563f2f2c DL |
621 | } |
622 | ||
3c319edb CB |
623 | ret = lxc_cmd_mainloop_add(name, &descr, handler); |
624 | if (ret < 0) { | |
625 | ERROR("Failed to add command handler to mainloop"); | |
626 | goto out_mainloop_console; | |
627 | } | |
b0a33c1e | 628 | |
3c319edb | 629 | TRACE("Mainloop is ready"); |
b0a33c1e | 630 | |
3c319edb | 631 | ret = lxc_mainloop(&descr, -1); |
a783a414 | 632 | close_prot_errno_disarm(descr.epfd); |
1cc8bd4b | 633 | if (ret < 0 || !handler->init_died) |
42b09f94 | 634 | goto out_mainloop_console; |
1cc8bd4b | 635 | |
543d2f83 CB |
636 | if (console) { |
637 | ret = lxc_terminal_mainloop_add(&descr_console, console); | |
638 | if (ret == 0) | |
639 | ret = lxc_mainloop(&descr_console, 0); | |
640 | } | |
30a33fbd | 641 | |
3c319edb | 642 | out_mainloop_console: |
543d2f83 | 643 | if (console) { |
30a33fbd CB |
644 | lxc_mainloop_close(&descr_console); |
645 | TRACE("Closed console mainloop"); | |
646 | } | |
3c319edb | 647 | |
42b09f94 CB |
648 | out_mainloop: |
649 | lxc_mainloop_close(&descr); | |
650 | TRACE("Closed mainloop"); | |
651 | ||
b0a33c1e | 652 | out_sigfd: |
1cc8bd4b | 653 | TRACE("Closed signal file descriptor %d", handler->sigfd); |
a783a414 | 654 | close_prot_errno_disarm(handler->sigfd); |
408da065 | 655 | |
1cc8bd4b | 656 | return ret; |
b0a33c1e | 657 | } |
658 | ||
a42abcce | 659 | void lxc_put_handler(struct lxc_handler *handler) |
f2e07cb6 | 660 | { |
a783a414 CB |
661 | close_prot_errno_disarm(handler->pidfd); |
662 | close_prot_errno_disarm(handler->sigfd); | |
1cc8bd4b | 663 | lxc_put_nsfds(handler); |
80308d07 | 664 | if (handler->conf && handler->conf->reboot == REBOOT_NONE) |
a783a414 CB |
665 | close_prot_errno_disarm(handler->conf->maincmd_fd); |
666 | close_prot_errno_disarm(handler->monitor_status_fd); | |
667 | close_prot_errno_disarm(handler->state_socket_pair[0]); | |
668 | close_prot_errno_disarm(handler->state_socket_pair[1]); | |
669 | cgroup_exit(handler->cgroup_ops); | |
a42abcce CB |
670 | if (handler->conf && handler->conf->reboot == REBOOT_NONE) |
671 | free_disarm(handler); | |
672 | else | |
673 | handler->conf = NULL; | |
f2e07cb6 CB |
674 | } |
675 | ||
a42abcce CB |
676 | struct lxc_handler *lxc_init_handler(struct lxc_handler *old, |
677 | const char *name, struct lxc_conf *conf, | |
5e5576a4 | 678 | const char *lxcpath, bool daemonize) |
59eb99ba | 679 | { |
85c279bb | 680 | int nr_keep_fds = 0; |
818a57fc | 681 | int ret; |
3a0f472d DL |
682 | struct lxc_handler *handler; |
683 | ||
a42abcce CB |
684 | if (!old) |
685 | handler = zalloc(sizeof(*handler)); | |
686 | else | |
687 | handler = old; | |
fdecbc9c | 688 | if (!handler) |
3a0f472d | 689 | return NULL; |
9044b79e | 690 | |
fdecbc9c CB |
691 | /* Note that am_guest_unpriv() checks the effective uid. We |
692 | * probably don't care if we are real root only if we are running | |
693 | * as root so this should be fine. | |
790255cf | 694 | */ |
e0010464 | 695 | handler->am_root = !am_guest_unpriv(); |
fae349da | 696 | handler->conf = conf; |
9123e471 | 697 | handler->lxcpath = lxcpath; |
818a57fc CB |
698 | handler->init_died = false; |
699 | handler->data_sock[0] = -EBADF; | |
700 | handler->data_sock[1] = -EBADF; | |
4d8bdfa0 | 701 | handler->monitor_status_fd = -EBADF; |
33942046 | 702 | handler->pidfd = -EBADF; |
1cc8bd4b | 703 | handler->sigfd = -EBADF; |
818a57fc CB |
704 | handler->state_socket_pair[0] = -EBADF; |
705 | handler->state_socket_pair[1] = -EBADF; | |
80308d07 | 706 | if (handler->conf->reboot == REBOOT_NONE) |
d2e75eba | 707 | INIT_LIST_HEAD(&handler->conf->state_clients); |
fae349da | 708 | |
8db6be1b CB |
709 | for (lxc_namespace_t idx = 0; idx < LXC_NS_MAX; idx++) { |
710 | handler->nsfd[idx] = -EBADF; | |
711 | ||
712 | if (handler->conf->reboot == REBOOT_NONE) | |
713 | continue; | |
714 | ||
715 | handler->nsfd_paths[idx][0] = '\0'; | |
716 | handler->hook_argv[idx] = NULL; | |
717 | ||
718 | if (handler->hook_argc != 0) | |
719 | handler->hook_argc = 0; | |
720 | } | |
b6b2b194 | 721 | |
f0ecc19d | 722 | handler->name = name; |
c581d2a6 CB |
723 | if (daemonize) |
724 | handler->transient_pid = lxc_raw_getpid(); | |
725 | else | |
726 | handler->transient_pid = -1; | |
3bdf52d7 | 727 | |
80308d07 | 728 | if (daemonize && handler->conf->reboot == REBOOT_NONE) { |
5e5576a4 | 729 | /* Create socketpair() to synchronize on daemonized startup. |
fdecbc9c CB |
730 | * When the container reboots we don't need to synchronize |
731 | * again currently so don't open another socketpair(). | |
5e5576a4 CB |
732 | */ |
733 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
734 | handler->state_socket_pair); | |
735 | if (ret < 0) { | |
736 | ERROR("Failed to create anonymous pair of unix sockets"); | |
737 | goto on_error; | |
738 | } | |
9044b79e | 739 | |
5e5576a4 CB |
740 | TRACE("Created anonymous pair {%d,%d} of unix sockets", |
741 | handler->state_socket_pair[0], | |
742 | handler->state_socket_pair[1]); | |
85c279bb CB |
743 | handler->keep_fds[nr_keep_fds++] = handler->state_socket_pair[0]; |
744 | handler->keep_fds[nr_keep_fds++] = handler->state_socket_pair[1]; | |
5e5576a4 CB |
745 | } |
746 | ||
80308d07 | 747 | if (handler->conf->reboot == REBOOT_NONE) { |
6834acff | 748 | handler->conf->maincmd_fd = lxc_server_init(name, lxcpath, "command"); |
d39b10eb CB |
749 | if (handler->conf->maincmd_fd < 0) { |
750 | ERROR("Failed to set up command socket"); | |
751 | goto on_error; | |
752 | } | |
85c279bb | 753 | handler->keep_fds[nr_keep_fds++] = handler->conf->maincmd_fd; |
aa460476 | 754 | } |
9044b79e | 755 | |
9dfa4041 | 756 | TRACE("Unix domain socket %d for command server is ready", |
aa460476 CB |
757 | handler->conf->maincmd_fd); |
758 | ||
759 | return handler; | |
760 | ||
f2e07cb6 | 761 | on_error: |
a42abcce | 762 | lxc_put_handler(handler); |
aa460476 CB |
763 | |
764 | return NULL; | |
765 | } | |
766 | ||
767 | int lxc_init(const char *name, struct lxc_handler *handler) | |
768 | { | |
f62cf1d4 | 769 | __do_close int status_fd = -EBADF; |
a2c09be0 | 770 | int ret; |
4a03ded4 | 771 | const char *loglevel; |
aa460476 CB |
772 | struct lxc_conf *conf = handler->conf; |
773 | ||
434c8e15 | 774 | handler->monitor_pid = lxc_raw_getpid(); |
4d8bdfa0 | 775 | status_fd = open("/proc/self/status", O_RDONLY | O_CLOEXEC); |
59b1b67e CB |
776 | if (status_fd < 0) |
777 | return log_error_errno(-1, errno, "Failed to open monitor status fd"); | |
434c8e15 | 778 | |
4eb19ac0 | 779 | handler->lsm_ops = lsm_init_static(); |
2170b263 | 780 | TRACE("Initialized LSM"); |
d2e30e99 | 781 | |
408da065 | 782 | /* Begin by setting the state to STARTING. */ |
2170b263 | 783 | ret = lxc_set_state(name, handler, STARTING); |
59b1b67e CB |
784 | if (ret < 0) |
785 | return log_error(-1, "Failed to set state to \"%s\"", lxc_state2str(STARTING)); | |
46800e77 | 786 | TRACE("Set container state to \"STARTING\""); |
0ad19a3f | 787 | |
408da065 | 788 | /* Start of environment variable setup for hooks. */ |
24c2a48d CB |
789 | ret = setenv("LXC_NAME", name, 1); |
790 | if (ret < 0) | |
791 | SYSERROR("Failed to set environment variable: LXC_NAME=%s", name); | |
408da065 | 792 | |
2170b263 CB |
793 | if (conf->rcfile) { |
794 | ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1); | |
795 | if (ret < 0) | |
818a57fc | 796 | SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s", conf->rcfile); |
2170b263 | 797 | } |
408da065 | 798 | |
2170b263 CB |
799 | if (conf->rootfs.mount) { |
800 | ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1); | |
801 | if (ret < 0) | |
818a57fc | 802 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount); |
2170b263 | 803 | } |
408da065 | 804 | |
2170b263 CB |
805 | if (conf->rootfs.path) { |
806 | ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1); | |
807 | if (ret < 0) | |
818a57fc | 808 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s", conf->rootfs.path); |
2170b263 | 809 | } |
408da065 | 810 | |
2170b263 CB |
811 | if (conf->console.path) { |
812 | ret = setenv("LXC_CONSOLE", conf->console.path, 1); | |
813 | if (ret < 0) | |
818a57fc | 814 | SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s", conf->console.path); |
2170b263 | 815 | } |
408da065 | 816 | |
2170b263 CB |
817 | if (conf->console.log_path) { |
818 | ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1); | |
819 | if (ret < 0) | |
818a57fc | 820 | SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s", conf->console.log_path); |
2170b263 | 821 | } |
408da065 | 822 | |
2170b263 CB |
823 | if (cgns_supported()) { |
824 | ret = setenv("LXC_CGNS_AWARE", "1", 1); | |
825 | if (ret < 0) | |
818a57fc | 826 | SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1"); |
2170b263 | 827 | } |
b8f88d9b | 828 | |
4a03ded4 | 829 | loglevel = lxc_log_priority_to_string(lxc_log_get_level()); |
2170b263 CB |
830 | ret = setenv("LXC_LOG_LEVEL", loglevel, 1); |
831 | if (ret < 0) | |
818a57fc | 832 | SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", loglevel); |
a2c09be0 CB |
833 | |
834 | if (conf->hooks_version == 0) | |
835 | ret = setenv("LXC_HOOK_VERSION", "0", 1); | |
836 | else | |
837 | ret = setenv("LXC_HOOK_VERSION", "1", 1); | |
838 | if (ret < 0) | |
839 | SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version); | |
408da065 | 840 | /* End of environment variable setup for hooks. */ |
f7bee6c6 | 841 | |
2170b263 | 842 | TRACE("Set environment variables"); |
dbc9832d | 843 | |
2170b263 | 844 | ret = run_lxc_hooks(name, "pre-start", conf, NULL); |
65a2dc8b CB |
845 | if (ret < 0) |
846 | return log_error(-1, "Failed to run lxc.hook.pre-start for container \"%s\"", name); | |
2170b263 | 847 | TRACE("Ran pre-start hooks"); |
26ddeedd | 848 | |
9f77617b CB |
849 | ret = lxc_terminal_parent(conf); |
850 | if (ret < 0) | |
851 | return log_error(-1, "Failed to allocate terminal"); | |
852 | ||
408da065 CB |
853 | /* The signal fd has to be created before forking otherwise if the child |
854 | * process exits before we setup the signal fd, the event will be lost | |
855 | * and the command will be stuck. | |
856 | */ | |
83ee7875 | 857 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
65a2dc8b CB |
858 | if (handler->sigfd < 0) |
859 | return log_error(-1, "Failed to setup SIGCHLD fd handler."); | |
2170b263 | 860 | TRACE("Set up signal fd"); |
b5159817 | 861 | |
5a087e05 | 862 | handler->cgroup_ops = cgroup_init(handler->conf); |
2202afc9 CB |
863 | if (!handler->cgroup_ops) { |
864 | ERROR("Failed to initialize cgroup driver"); | |
9f77617b | 865 | goto out_restore_sigmask; |
2202afc9 CB |
866 | } |
867 | TRACE("Initialized cgroup driver"); | |
868 | ||
2e1361a6 | 869 | ret = lxc_read_seccomp_config(conf); |
9f77617b CB |
870 | if (ret < 0) { |
871 | ERROR("Failed to read seccomp policy"); | |
872 | goto out_restore_sigmask; | |
873 | } | |
2e1361a6 CB |
874 | TRACE("Read seccomp policy"); |
875 | ||
af04d847 | 876 | ret = handler->lsm_ops->prepare(handler->lsm_ops, conf, handler->lxcpath); |
1800f924 WB |
877 | if (ret < 0) { |
878 | ERROR("Failed to initialize LSM"); | |
9f77617b | 879 | goto out_restore_sigmask; |
1800f924 WB |
880 | } |
881 | TRACE("Initialized LSM"); | |
882 | ||
2170b263 | 883 | INFO("Container \"%s\" is initialized", name); |
4d8bdfa0 | 884 | handler->monitor_status_fd = move_fd(status_fd); |
aa460476 | 885 | return 0; |
59eb99ba | 886 | |
b5159817 | 887 | out_restore_sigmask: |
4c496daa | 888 | (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); |
ea918412 | 889 | |
aa460476 | 890 | return -1; |
59eb99ba DL |
891 | } |
892 | ||
8db6be1b CB |
893 | void lxc_expose_namespace_environment(const struct lxc_handler *handler) |
894 | { | |
895 | for (lxc_namespace_t i = 0; i < LXC_NS_MAX; i++) { | |
896 | int ret; | |
897 | const char *fd_path; | |
898 | ||
899 | if (handler->nsfd[i] < 0) | |
900 | continue; | |
901 | ||
902 | fd_path = handler->nsfd_paths[i] + strcspn(handler->nsfd_paths[i], "/"); | |
903 | ret = setenv(ns_info[i].env_name, fd_path, 1); | |
904 | if (ret < 0) | |
905 | SYSERROR("Failed to set environment variable %s=%s", | |
906 | ns_info[i].env_name, fd_path); | |
907 | else | |
908 | TRACE("Set environment variable %s=%s", | |
909 | ns_info[i].env_name, fd_path); | |
910 | } | |
911 | } | |
912 | ||
fd5be714 | 913 | void lxc_end(struct lxc_handler *handler) |
59eb99ba | 914 | { |
818a57fc | 915 | int ret; |
0c5859ff | 916 | const char *name = handler->name; |
2202afc9 | 917 | struct cgroup_ops *cgroup_ops = handler->cgroup_ops; |
d2e75eba | 918 | struct lxc_state_client *client, *nclient; |
b6b2b194 | 919 | |
408da065 CB |
920 | /* The STOPPING state is there for future cleanup code which can take |
921 | * awhile. | |
59eb99ba | 922 | */ |
25c2aca5 | 923 | lxc_set_state(name, handler, STOPPING); |
b6b2b194 | 924 | |
8db6be1b CB |
925 | /* Passing information to hooks via environment variables. */ |
926 | if (handler->conf->hooks_version > 0) | |
927 | lxc_expose_namespace_environment(handler); | |
c154af98 | 928 | |
80308d07 | 929 | if (handler->conf->reboot > REBOOT_NONE) { |
c097467f CB |
930 | ret = setenv("LXC_TARGET", "reboot", 1); |
931 | if (ret < 0) | |
818a57fc | 932 | SYSERROR("Failed to set environment variable: LXC_TARGET=reboot"); |
c097467f | 933 | } |
408da065 | 934 | |
80308d07 | 935 | if (handler->conf->reboot == REBOOT_NONE) { |
c097467f CB |
936 | ret = setenv("LXC_TARGET", "stop", 1); |
937 | if (ret < 0) | |
818a57fc | 938 | SYSERROR("Failed to set environment variable: LXC_TARGET=stop"); |
c097467f | 939 | } |
c154af98 | 940 | |
18b3b9c1 | 941 | if (handler->conf->hooks_version == 0) |
8db6be1b | 942 | ret = run_lxc_hooks(name, "stop", handler->conf, handler->hook_argv); |
18b3b9c1 | 943 | else |
c097467f CB |
944 | ret = run_lxc_hooks(name, "stop", handler->conf, NULL); |
945 | if (ret < 0) | |
946 | ERROR("Failed to run \"lxc.hook.stop\" hook"); | |
c154af98 | 947 | |
af04d847 | 948 | handler->lsm_ops->cleanup(handler->lsm_ops, handler->conf, handler->lxcpath); |
1800f924 | 949 | |
e2aed383 WB |
950 | if (cgroup_ops) { |
951 | cgroup_ops->payload_destroy(cgroup_ops, handler); | |
952 | cgroup_ops->monitor_destroy(cgroup_ops, handler); | |
953 | } | |
4288b79f | 954 | |
79ff643d CB |
955 | put_lxc_rootfs(&handler->conf->rootfs, true); |
956 | ||
80308d07 | 957 | if (handler->conf->reboot == REBOOT_NONE) { |
f893d898 CB |
958 | /* For all new state clients simply close the command socket. |
959 | * This will inform all state clients that the container is | |
960 | * STOPPED and also prevents a race between a open()/close() on | |
961 | * the command socket causing a new process to get ECONNREFUSED | |
962 | * because we haven't yet closed the command socket. | |
963 | */ | |
b87ee312 | 964 | close_prot_errno_disarm(handler->conf->maincmd_fd); |
c3184275 CB |
965 | TRACE("Closed command socket"); |
966 | ||
967 | /* This function will try to connect to the legacy lxc-monitord | |
968 | * state server and only exists for backwards compatibility. | |
969 | */ | |
970 | lxc_monitor_send_state(name, STOPPED, handler->lxcpath); | |
971 | ||
972 | /* The command socket is closed so no one can acces the command | |
973 | * socket anymore so there's no need to lock it. | |
974 | */ | |
975 | handler->state = STOPPED; | |
976 | TRACE("Set container state to \"STOPPED\""); | |
f893d898 CB |
977 | } else { |
978 | lxc_set_state(name, handler, STOPPED); | |
695d0e56 | 979 | TRACE("Set container state to \"STOPPED\""); |
d39b10eb | 980 | } |
4288b79f | 981 | |
048493a3 CB |
982 | /* Avoid lingering namespace references. */ |
983 | lxc_put_nsfds(handler); | |
984 | ||
46800e77 CB |
985 | ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL); |
986 | if (ret < 0) { | |
c097467f | 987 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name); |
80308d07 | 988 | if (handler->conf->reboot > REBOOT_NONE) { |
c097467f | 989 | WARN("Container will be stopped instead of rebooted"); |
80308d07 | 990 | handler->conf->reboot = REBOOT_NONE; |
c097467f CB |
991 | |
992 | ret = setenv("LXC_TARGET", "stop", 1); | |
993 | if (ret < 0) | |
818a57fc | 994 | WARN("Failed to set environment variable: LXC_TARGET=stop"); |
f3787121 CB |
995 | } |
996 | } | |
26ddeedd | 997 | |
408da065 | 998 | /* Reset mask set by setup_signal_fd. */ |
b467714b | 999 | ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); |
c097467f | 1000 | if (ret < 0) |
a24c5678 | 1001 | SYSWARN("Failed to restore signal mask"); |
8f64a3f6 | 1002 | |
2aac071b | 1003 | lxc_terminal_delete(&handler->conf->console); |
0e4be3cf | 1004 | lxc_delete_tty(&handler->conf->ttys); |
f797f05e | 1005 | close_prot_errno_disarm(handler->conf->devpts_fd); |
dbc9832d | 1006 | |
dbc9832d CB |
1007 | /* The command socket is now closed, no more state clients can register |
1008 | * themselves from now on. So free the list of state clients. | |
1009 | */ | |
d2e75eba | 1010 | list_for_each_entry_safe(client, nclient, &handler->conf->state_clients, head) { |
d39b10eb | 1011 | /* Keep state clients that want to be notified about reboots. */ |
80308d07 CB |
1012 | if ((handler->conf->reboot > REBOOT_NONE) && |
1013 | (client->states[RUNNING] == 2)) | |
d39b10eb CB |
1014 | continue; |
1015 | ||
dbc9832d | 1016 | /* close state client socket */ |
d2e75eba | 1017 | list_del(&client->head); |
c097467f | 1018 | close(client->clientfd); |
d2e75eba | 1019 | free(client); |
dbc9832d CB |
1020 | } |
1021 | ||
80308d07 | 1022 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ) |
28272964 | 1023 | lxc_destroy_container_on_signal(handler, name); |
2c5f2ede | 1024 | |
a42abcce | 1025 | lxc_put_handler(handler); |
59eb99ba DL |
1026 | } |
1027 | ||
0c5859ff | 1028 | void lxc_abort(struct lxc_handler *handler) |
59eb99ba | 1029 | { |
33942046 CB |
1030 | int ret = 0; |
1031 | int status; | |
73e608b2 | 1032 | |
0c5859ff | 1033 | lxc_set_state(handler->name, handler, ABORTING); |
0e4f9d51 | 1034 | |
c718fac1 | 1035 | if (handler->pidfd >= 0) { |
33942046 | 1036 | ret = lxc_raw_pidfd_send_signal(handler->pidfd, SIGKILL, NULL, 0); |
c718fac1 | 1037 | if (ret) |
11c7d349 CB |
1038 | SYSWARN("Failed to send SIGKILL via pidfd %d for process %d", |
1039 | handler->pidfd, handler->pid); | |
c718fac1 CB |
1040 | } |
1041 | ||
65a2dc8b | 1042 | if ((!ret || errno != ESRCH) && handler->pid > 0) |
11c7d349 CB |
1043 | if (kill(handler->pid, SIGKILL)) |
1044 | SYSWARN("Failed to send SIGKILL to %d", handler->pid); | |
0e4f9d51 | 1045 | |
33942046 CB |
1046 | do { |
1047 | ret = waitpid(-1, &status, 0); | |
1048 | } while (ret > 0); | |
59eb99ba DL |
1049 | } |
1050 | ||
ffe1e01a | 1051 | static int do_start(void *data) |
50e98013 | 1052 | { |
cdb2a47f | 1053 | struct lxc_handler *handler = data; |
f62cf1d4 | 1054 | __lxc_unused __do_close int data_sock0 = handler->data_sock[0], |
c294a68d | 1055 | data_sock1 = handler->data_sock[1]; |
d3103162 | 1056 | __do_close int devnull_fd = -EBADF, status_fd = -EBADF; |
8bf3abfb | 1057 | int ret; |
928b1f04 YT |
1058 | uid_t new_uid; |
1059 | gid_t new_gid; | |
964581c2 CB |
1060 | uid_t nsuid = 0; |
1061 | gid_t nsgid = 0; | |
50e98013 | 1062 | |
18225d19 | 1063 | lxc_sync_fini_parent(handler); |
50e98013 | 1064 | |
d17c815d | 1065 | if (lxc_abstract_unix_recv_one_fd(data_sock1, &status_fd, NULL, 0) < 0) { |
77093b7c | 1066 | ERROR("Failed to receive status file descriptor from parent process"); |
4d8bdfa0 CB |
1067 | goto out_warn_father; |
1068 | } | |
1069 | ||
408da065 CB |
1070 | /* This prctl must be before the synchro, so if the parent dies before |
1071 | * we set the parent death signal, we will detect its death with the | |
1072 | * synchro right after, otherwise we have a window where the parent can | |
1073 | * exit before we set the pdeath signal leading to a unsupervized | |
1074 | * container. | |
743ecd2e | 1075 | */ |
4d8bdfa0 | 1076 | ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd); |
912314fc CB |
1077 | if (ret < 0) { |
1078 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
18225d19 | 1079 | goto out_warn_father; |
743ecd2e DL |
1080 | } |
1081 | ||
611ddd34 CB |
1082 | ret = lxc_ambient_caps_up(); |
1083 | if (ret < 0) { | |
6f5e532f | 1084 | ERROR("Failed to raise ambient capabilities"); |
611ddd34 CB |
1085 | goto out_warn_father; |
1086 | } | |
1087 | ||
b467714b | 1088 | ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); |
18225d19 CB |
1089 | if (ret < 0) { |
1090 | SYSERROR("Failed to set signal mask"); | |
1091 | goto out_warn_father; | |
1092 | } | |
50e98013 | 1093 | |
6e48e7c5 | 1094 | if (!lxc_sync_wait_parent(handler, START_SYNC_STARTUP)) |
18225d19 | 1095 | goto out_warn_father; |
5b1e83cb | 1096 | |
408da065 CB |
1097 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See |
1098 | * https://github.com/lxc/lxd/issues/1978. | |
1099 | */ | |
f7176c3e | 1100 | if (handler->ns_unshare_flags & CLONE_NEWNET) { |
5b1e83cb SH |
1101 | ret = unshare(CLONE_NEWNET); |
1102 | if (ret < 0) { | |
e96a536c | 1103 | SYSERROR("Failed to unshare CLONE_NEWNET"); |
5b1e83cb SH |
1104 | goto out_warn_father; |
1105 | } | |
e96a536c | 1106 | INFO("Unshared CLONE_NEWNET"); |
5b1e83cb SH |
1107 | } |
1108 | ||
408da065 | 1109 | /* If we are in a new user namespace, become root there to have |
d08f8d2f | 1110 | * privilege over our namespace. |
f6d3e3e4 | 1111 | */ |
0589d744 | 1112 | if (!list_empty(&handler->conf->id_map)) { |
964581c2 CB |
1113 | if (!handler->conf->root_nsuid_map) |
1114 | nsuid = handler->conf->init_uid; | |
1115 | ||
1116 | if (!handler->conf->root_nsgid_map) | |
1117 | nsgid = handler->conf->init_gid; | |
4160c3a0 | 1118 | |
d08f8d2f CB |
1119 | /* Drop groups only after we switched to a valid gid in the new |
1120 | * user namespace. | |
1121 | */ | |
8917c382 | 1122 | if (!lxc_drop_groups() && |
8af07f82 | 1123 | (handler->am_root || errno != EPERM)) |
c476bdce | 1124 | goto out_warn_father; |
4b826b1f | 1125 | |
b58214ac CB |
1126 | if (!lxc_switch_uid_gid(nsuid, nsgid)) |
1127 | goto out_warn_father; | |
1128 | ||
b81689a1 CB |
1129 | ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0), |
1130 | prctl_arg(0), prctl_arg(0)); | |
d7883725 CB |
1131 | if (ret < 0) |
1132 | goto out_warn_father; | |
912314fc CB |
1133 | |
1134 | /* set{g,u}id() clears deathsignal */ | |
4d8bdfa0 | 1135 | ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd); |
912314fc CB |
1136 | if (ret < 0) { |
1137 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1138 | goto out_warn_father; | |
1139 | } | |
f6d3e3e4 SH |
1140 | } |
1141 | ||
e96a536c CB |
1142 | ret = access(handler->lxcpath, X_OK); |
1143 | if (ret != 0) { | |
c8154066 SH |
1144 | print_top_failing_dir(handler->lxcpath); |
1145 | goto out_warn_father; | |
1146 | } | |
1147 | ||
7a55c157 TA |
1148 | /* In order to checkpoint restore, we need to have everything in the |
1149 | * same mount namespace. However, some containers may not have a | |
1150 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
1151 | * can't set init's std fds to /dev/null by opening it from inside the | |
1152 | * container. | |
1153 | * | |
1154 | * If that's the case, fall back to using the host's /dev/null. This | |
1155 | * means that migration won't work, but at least we won't spew output | |
1156 | * where it isn't wanted. | |
1157 | */ | |
bb955810 | 1158 | if (handler->daemonize && !handler->conf->autodev) { |
2806a87d | 1159 | char path[PATH_MAX]; |
3c09b97c | 1160 | |
fa60cd7b CB |
1161 | ret = strnprintf(path, sizeof(path), "%s/dev/null", |
1162 | handler->conf->rootfs.mount); | |
1163 | if (ret < 0) | |
2806a87d | 1164 | goto out_warn_father; |
3c09b97c | 1165 | |
e96a536c CB |
1166 | ret = access(path, F_OK); |
1167 | if (ret != 0) { | |
1168 | devnull_fd = open_devnull(); | |
c44de748 | 1169 | |
e96a536c CB |
1170 | if (devnull_fd < 0) |
1171 | goto out_warn_father; | |
818a57fc | 1172 | WARN("Using /dev/null from the host for container init's standard file descriptors. Migration will not work"); |
e96a536c | 1173 | } |
c44de748 AM |
1174 | } |
1175 | ||
6bc4165d CB |
1176 | /* |
1177 | * Tell the parent task it can begin to configure the container and wait | |
1178 | * for it to finish. | |
1179 | */ | |
1180 | if (!lxc_sync_wake_parent(handler, START_SYNC_CONFIGURE)) | |
c44de748 | 1181 | goto out_error; |
544a48a0 | 1182 | |
deefdf8a CB |
1183 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
1184 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
1185 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
1186 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
1187 | * /proc/self/cgroup would show us: | |
1188 | * | |
1189 | * 8:cpuset:/lxc/c | |
1190 | * | |
1191 | * whereas it should actually show | |
1192 | * | |
1193 | * 8:cpuset:/ | |
1194 | */ | |
f7176c3e | 1195 | if (handler->ns_unshare_flags & CLONE_NEWCGROUP) { |
e96a536c CB |
1196 | ret = unshare(CLONE_NEWCGROUP); |
1197 | if (ret < 0) { | |
bca7c59c CB |
1198 | if (errno != EINVAL) { |
1199 | SYSERROR("Failed to unshare CLONE_NEWCGROUP"); | |
1200 | goto out_warn_father; | |
1201 | } | |
1202 | ||
1203 | handler->ns_clone_flags &= ~CLONE_NEWCGROUP; | |
1204 | SYSINFO("Kernel does not support CLONE_NEWCGROUP"); | |
1205 | } else { | |
1206 | INFO("Unshared CLONE_NEWCGROUP"); | |
deefdf8a | 1207 | } |
deefdf8a CB |
1208 | } |
1209 | ||
f7176c3e | 1210 | if (handler->ns_unshare_flags & CLONE_NEWTIME) { |
70fd7fc9 CB |
1211 | ret = unshare(CLONE_NEWTIME); |
1212 | if (ret < 0) { | |
1213 | if (errno != EINVAL) { | |
1214 | SYSERROR("Failed to unshare CLONE_NEWTIME"); | |
1215 | goto out_warn_father; | |
1216 | } | |
1217 | ||
1218 | handler->ns_clone_flags &= ~CLONE_NEWTIME; | |
1219 | SYSINFO("Kernel does not support CLONE_NEWTIME"); | |
1220 | } else { | |
1221 | __do_close int timens_fd = -EBADF; | |
1222 | ||
1223 | INFO("Unshared CLONE_NEWTIME"); | |
1224 | ||
1225 | if (handler->conf->timens.s_boot) | |
1226 | ret = timens_offset_write(CLOCK_BOOTTIME, handler->conf->timens.s_boot, 0); | |
1227 | else if (handler->conf->timens.ns_boot) | |
1228 | ret = timens_offset_write(CLOCK_BOOTTIME, 0, handler->conf->timens.ns_boot); | |
1229 | if (ret) { | |
1230 | SYSERROR("Failed to write CLONE_BOOTTIME offset"); | |
1231 | goto out_warn_father; | |
1232 | } | |
1233 | TRACE("Wrote CLOCK_BOOTTIME offset"); | |
1234 | ||
1235 | if (handler->conf->timens.s_monotonic) | |
1236 | ret = timens_offset_write(CLOCK_MONOTONIC, handler->conf->timens.s_monotonic, 0); | |
1237 | else if (handler->conf->timens.ns_monotonic) | |
1238 | ret = timens_offset_write(CLOCK_MONOTONIC, 0, handler->conf->timens.ns_monotonic); | |
1239 | if (ret) { | |
1240 | SYSERROR("Failed to write CLONE_MONOTONIC offset"); | |
1241 | goto out_warn_father; | |
1242 | } | |
1243 | TRACE("Wrote CLOCK_MONOTONIC offset"); | |
1244 | ||
1245 | timens_fd = open("/proc/self/ns/time_for_children", O_RDONLY | O_CLOEXEC); | |
1246 | if (timens_fd < 0) { | |
1247 | SYSERROR("Failed to open \"/proc/self/ns/time_for_children\""); | |
1248 | goto out_warn_father; | |
1249 | } | |
1250 | ||
1251 | ret = setns(timens_fd, CLONE_NEWTIME); | |
cfe6f435 | 1252 | if (ret) { |
70fd7fc9 CB |
1253 | SYSERROR("Failed to setns(%d(\"/proc/self/ns/time_for_children\"))", timens_fd); |
1254 | goto out_warn_father; | |
1255 | } | |
1256 | } | |
1257 | } | |
1258 | ||
c294a68d CB |
1259 | /* |
1260 | * Add the requested environment variables to the current environment | |
1261 | * to allow them to be used by the various hooks, such as the start | |
1262 | * hook below. | |
98ff08ca | 1263 | */ |
c294a68d CB |
1264 | ret = lxc_set_environment(handler->conf); |
1265 | if (ret < 0) | |
1266 | goto out_warn_father; | |
98ff08ca | 1267 | |
6bc4165d CB |
1268 | if (!lxc_sync_wait_parent(handler, START_SYNC_POST_CONFIGURE)) |
1269 | goto out_warn_father; | |
1270 | ||
f4152036 | 1271 | /* Setup the container, ip, names, utsname, ... */ |
3b988b33 | 1272 | ret = lxc_setup(handler); |
7729f8e5 | 1273 | if (ret < 0) { |
e96a536c | 1274 | ERROR("Failed to setup container \"%s\"", handler->name); |
f4152036 CB |
1275 | goto out_warn_father; |
1276 | } | |
1277 | ||
408da065 | 1278 | /* Set the label to change to when we exec(2) the container's init. */ |
af04d847 | 1279 | ret = handler->lsm_ops->process_label_set(handler->lsm_ops, NULL, handler->conf, true); |
e96a536c | 1280 | if (ret < 0) |
e075f5d9 | 1281 | goto out_warn_father; |
5112cd70 | 1282 | |
029cdff5 | 1283 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
408da065 CB |
1284 | * before we aren't allowed anymore. |
1285 | */ | |
029cdff5 | 1286 | if (handler->conf->no_new_privs) { |
b81689a1 CB |
1287 | ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0), |
1288 | prctl_arg(0), prctl_arg(0)); | |
e96a536c | 1289 | if (ret < 0) { |
818a57fc | 1290 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges"); |
029cdff5 CB |
1291 | goto out_warn_father; |
1292 | } | |
818a57fc | 1293 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges"); |
029cdff5 CB |
1294 | } |
1295 | ||
408da065 | 1296 | /* If we mounted a temporary proc, then unmount it now. */ |
5112cd70 | 1297 | tmp_proc_unmount(handler->conf); |
e075f5d9 | 1298 | |
e96a536c CB |
1299 | ret = lxc_seccomp_load(handler->conf); |
1300 | if (ret < 0) | |
8f2c3a70 SH |
1301 | goto out_warn_father; |
1302 | ||
e96a536c CB |
1303 | ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL); |
1304 | if (ret < 0) { | |
1305 | ERROR("Failed to run lxc.hook.start for container \"%s\"", | |
1306 | handler->name); | |
773fb9ca SH |
1307 | goto out_warn_father; |
1308 | } | |
fc25b815 | 1309 | |
a783a414 | 1310 | close_prot_errno_disarm(handler->sigfd); |
26ddeedd | 1311 | |
41808e20 | 1312 | if (handler->conf->console.pty < 0 && handler->daemonize) { |
f4c177c3 CB |
1313 | if (devnull_fd < 0) { |
1314 | devnull_fd = open_devnull(); | |
1315 | if (devnull_fd < 0) | |
1316 | goto out_warn_father; | |
1317 | } | |
1318 | ||
e96a536c CB |
1319 | ret = set_stdfds(devnull_fd); |
1320 | if (ret < 0) { | |
1321 | ERROR("Failed to redirect std{in,out,err} to \"/dev/null\""); | |
c5b93afb LF |
1322 | goto out_warn_father; |
1323 | } | |
e96a536c | 1324 | } |
507cee36 | 1325 | |
a783a414 | 1326 | close_prot_errno_disarm(devnull_fd); |
c44de748 | 1327 | |
8c9a7665 TA |
1328 | setsid(); |
1329 | ||
e96a536c CB |
1330 | if (handler->conf->init_cwd) { |
1331 | ret = chdir(handler->conf->init_cwd); | |
1332 | if (ret < 0) { | |
1333 | SYSERROR("Could not change directory to \"%s\"", | |
1334 | handler->conf->init_cwd); | |
1335 | goto out_warn_father; | |
1336 | } | |
3c491553 L |
1337 | } |
1338 | ||
6e48e7c5 | 1339 | if (!lxc_sync_barrier_parent(handler, START_SYNC_CGROUP_LIMITS)) |
f4152036 CB |
1340 | goto out_warn_father; |
1341 | ||
111ed96e | 1342 | ret = lxc_sync_fds_child(handler); |
1b82d721 | 1343 | if (ret < 0) { |
111ed96e | 1344 | SYSERROR("Failed to sync file descriptors with parent"); |
1b82d721 CB |
1345 | goto out_warn_father; |
1346 | } | |
1347 | ||
1b82d721 CB |
1348 | if (!lxc_sync_wait_parent(handler, START_SYNC_READY_START)) |
1349 | goto out_warn_father; | |
1350 | ||
149857af CB |
1351 | /* Reset the environment variables the user requested in a clear |
1352 | * environment. | |
1353 | */ | |
e96a536c CB |
1354 | ret = clearenv(); |
1355 | /* Don't error out though. */ | |
1356 | if (ret < 0) | |
149857af | 1357 | SYSERROR("Failed to clear environment."); |
149857af | 1358 | |
c294a68d CB |
1359 | ret = lxc_set_environment(handler->conf); |
1360 | if (ret < 0) | |
1361 | goto out_warn_father; | |
149857af | 1362 | |
e96a536c CB |
1363 | ret = putenv("container=lxc"); |
1364 | if (ret < 0) { | |
1365 | SYSERROR("Failed to set environment variable: container=lxc"); | |
98ff08ca CB |
1366 | goto out_warn_father; |
1367 | } | |
1368 | ||
885766f5 CB |
1369 | if (handler->conf->ttys.tty_names) { |
1370 | ret = putenv(handler->conf->ttys.tty_names); | |
e96a536c CB |
1371 | if (ret < 0) { |
1372 | SYSERROR("Failed to set environment variable for container ptys"); | |
98ff08ca CB |
1373 | goto out_warn_father; |
1374 | } | |
1375 | } | |
1376 | ||
76bdf299 CB |
1377 | /* The container has been setup. We can now switch to an unprivileged |
1378 | * uid/gid. | |
1379 | */ | |
928b1f04 YT |
1380 | new_uid = handler->conf->init_uid; |
1381 | new_gid = handler->conf->init_gid; | |
76bdf299 | 1382 | |
964581c2 CB |
1383 | /* Avoid unnecessary syscalls. */ |
1384 | if (new_uid == nsuid) | |
1385 | new_uid = LXC_INVALID_UID; | |
1386 | ||
1387 | if (new_gid == nsgid) | |
1388 | new_gid = LXC_INVALID_GID; | |
c353b0b9 | 1389 | |
6aff5157 | 1390 | /* Make sure that the processes STDIO is correctly owned by the user that we are switching to */ |
c353b0b9 CB |
1391 | ret = fix_stdio_permissions(new_uid); |
1392 | if (ret) | |
1393 | WARN("Failed to ajust stdio permissions"); | |
964581c2 | 1394 | |
8af07f82 CB |
1395 | /* If we are in a new user namespace we already dropped all groups when |
1396 | * we switched to root in the new user namespace further above. Only | |
1397 | * drop groups if we can, so ensure that we have necessary privilege. | |
1398 | */ | |
0589d744 | 1399 | if (list_empty(&handler->conf->id_map)) { |
8af07f82 CB |
1400 | #if HAVE_LIBCAP |
1401 | if (lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE)) | |
1402 | #endif | |
bf31b337 RJ |
1403 | { |
1404 | if (handler->conf->init_groups.size > 0) { | |
1405 | if (!lxc_setgroups(handler->conf->init_groups.list, | |
1406 | handler->conf->init_groups.size)) | |
1407 | goto out_warn_father; | |
1408 | } else { | |
1409 | if (!lxc_drop_groups()) | |
1410 | goto out_warn_father; | |
1411 | } | |
1412 | } | |
1413 | } | |
8af07f82 | 1414 | |
b58214ac CB |
1415 | if (!lxc_switch_uid_gid(new_uid, new_gid)) |
1416 | goto out_warn_father; | |
1417 | ||
611ddd34 CB |
1418 | ret = lxc_ambient_caps_down(); |
1419 | if (ret < 0) { | |
6f5e532f | 1420 | ERROR("Failed to clear ambient capabilities"); |
611ddd34 CB |
1421 | goto out_warn_father; |
1422 | } | |
1423 | ||
258f8051 | 1424 | if (handler->conf->monitor_signal_pdeath != SIGKILL) { |
4d8bdfa0 CB |
1425 | ret = lxc_set_death_signal(handler->conf->monitor_signal_pdeath, |
1426 | handler->monitor_pid, status_fd); | |
258f8051 CB |
1427 | if (ret < 0) { |
1428 | SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", | |
1429 | handler->conf->monitor_signal_pdeath); | |
1430 | goto out_warn_father; | |
1431 | } | |
1432 | } | |
1433 | ||
d3103162 CB |
1434 | /* |
1435 | * After this call, we are in error because this ops should not return | |
408da065 CB |
1436 | * as it execs. |
1437 | */ | |
c4ea60df | 1438 | handler->ops->start(handler, handler->data); |
50e98013 DL |
1439 | |
1440 | out_warn_father: | |
d3103162 CB |
1441 | /* |
1442 | * We want the parent to know something went wrong, so we return a | |
408da065 CB |
1443 | * special error code. |
1444 | */ | |
946a0c6d | 1445 | lxc_sync_wake_parent(handler, SYNC_ERROR); |
c44de748 AM |
1446 | |
1447 | out_error: | |
50e98013 DL |
1448 | return -1; |
1449 | } | |
1450 | ||
5af9369b | 1451 | int resolve_clone_flags(struct lxc_handler *handler) |
f813849c | 1452 | { |
8bc8c715 CB |
1453 | int i; |
1454 | struct lxc_conf *conf = handler->conf; | |
70fd7fc9 CB |
1455 | bool wants_timens = conf->timens.s_boot || conf->timens.ns_boot || |
1456 | conf->timens.s_monotonic || conf->timens.ns_monotonic; | |
f813849c | 1457 | |
8bc8c715 | 1458 | for (i = 0; i < LXC_NS_MAX; i++) { |
f7a0c6ee CB |
1459 | if (conf->ns_keep) { |
1460 | if (!(conf->ns_keep & ns_info[i].clone_flag)) | |
becad0ec | 1461 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
f7a0c6ee CB |
1462 | } else if (conf->ns_clone) { |
1463 | if ((conf->ns_clone & ns_info[i].clone_flag)) | |
becad0ec | 1464 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 | 1465 | } else { |
0589d744 | 1466 | if (i == LXC_NS_USER && list_empty(&handler->conf->id_map)) |
8bc8c715 CB |
1467 | continue; |
1468 | ||
1469 | if (i == LXC_NS_NET && lxc_requests_empty_network(handler)) | |
1470 | continue; | |
1471 | ||
7bd05339 CB |
1472 | if (i == LXC_NS_CGROUP && !cgns_supported()) |
1473 | continue; | |
1474 | ||
70fd7fc9 CB |
1475 | if (i == LXC_NS_TIME && !wants_timens) |
1476 | continue; | |
1477 | ||
becad0ec | 1478 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 | 1479 | } |
03df7ab5 | 1480 | |
8bc8c715 CB |
1481 | if (!conf->ns_share[i]) |
1482 | continue; | |
5af9369b | 1483 | |
becad0ec | 1484 | handler->ns_clone_flags &= ~ns_info[i].clone_flag; |
8bc8c715 | 1485 | TRACE("Sharing %s namespace", ns_info[i].proc_name); |
5af9369b CB |
1486 | } |
1487 | ||
70fd7fc9 CB |
1488 | if (wants_timens && (conf->ns_keep & ns_info[LXC_NS_TIME].clone_flag)) |
1489 | return log_trace_errno(-1, EINVAL, "Requested to keep time namespace while also specifying offsets"); | |
1490 | ||
f7176c3e CB |
1491 | /* Deal with namespaces that are unshared. */ |
1492 | if (handler->ns_clone_flags & CLONE_NEWTIME) | |
1493 | handler->ns_unshare_flags |= CLONE_NEWTIME; | |
1494 | ||
1495 | if (!pure_unified_layout(handler->cgroup_ops) && handler->ns_clone_flags & CLONE_NEWCGROUP) | |
1496 | handler->ns_unshare_flags |= CLONE_NEWCGROUP; | |
1497 | ||
1498 | if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == | |
1499 | (CLONE_NEWNET | CLONE_NEWUSER)) | |
1500 | handler->ns_unshare_flags |= CLONE_NEWNET; | |
1501 | ||
1502 | /* Deal with namespaces that are spawned. */ | |
1503 | handler->ns_on_clone_flags = handler->ns_clone_flags & ~handler->ns_unshare_flags; | |
1504 | ||
1505 | handler->clone_flags = handler->ns_on_clone_flags | CLONE_PIDFD; | |
1506 | ||
5af9369b | 1507 | return 0; |
f813849c TA |
1508 | } |
1509 | ||
8deca6c9 CB |
1510 | /* Note that this function is used with clone(CLONE_VM). Some glibc versions |
1511 | * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD. | |
1512 | * But since the memory between parent and child is shared on CLONE_VM this | |
1513 | * would invalidate the getpid() cache that glibc used to maintain and so | |
1514 | * getpid() in the child would return the parent's pid. This is all fixed in | |
1515 | * newer glibc versions where the getpid() cache is removed and the pid/tid is | |
1516 | * not reset anymore. | |
b35ddc5b | 1517 | * However, if for whatever reason you - dear committer - somehow need to get the |
8de0119d CB |
1518 | * pid of the placeholder intermediate process for do_share_ns() you need to |
1519 | * call lxc_raw_getpid(). The next lxc_raw_clone() call does not employ | |
1520 | * CLONE_VM and will be fine. | |
8deca6c9 CB |
1521 | */ |
1522 | static inline int do_share_ns(void *arg) | |
fa3a5b22 | 1523 | { |
8deca6c9 | 1524 | int i, flags, ret; |
fa3a5b22 CB |
1525 | struct lxc_handler *handler = arg; |
1526 | ||
8deca6c9 CB |
1527 | for (i = 0; i < LXC_NS_MAX; i++) { |
1528 | if (handler->nsfd[i] < 0) | |
1529 | continue; | |
fa3a5b22 | 1530 | |
8deca6c9 | 1531 | ret = setns(handler->nsfd[i], 0); |
c0b48eff CB |
1532 | if (ret < 0) { |
1533 | /* | |
1534 | * Note that joining a user and/or mount namespace | |
1535 | * requires the process is not multithreaded otherwise | |
1536 | * setns() will fail here. | |
1537 | */ | |
1538 | SYSERROR("Failed to inherit %s namespace", | |
1539 | ns_info[i].proc_name); | |
8deca6c9 | 1540 | return -1; |
c0b48eff | 1541 | } |
fa3a5b22 | 1542 | |
8deca6c9 | 1543 | DEBUG("Inherited %s namespace", ns_info[i].proc_name); |
fa3a5b22 CB |
1544 | } |
1545 | ||
becad0ec | 1546 | flags = handler->ns_on_clone_flags; |
8deca6c9 | 1547 | flags |= CLONE_PARENT; |
33942046 CB |
1548 | handler->pid = lxc_raw_clone_cb(do_start, handler, CLONE_PIDFD | flags, |
1549 | &handler->pidfd); | |
8deca6c9 | 1550 | if (handler->pid < 0) |
fa3a5b22 CB |
1551 | return -1; |
1552 | ||
8deca6c9 | 1553 | return 0; |
fa3a5b22 CB |
1554 | } |
1555 | ||
480588e6 CB |
1556 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which |
1557 | * exec()s the requested container binary. | |
1558 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1559 | * right here should be double checked if they'd pose a security risk. (For | |
1560 | * example, any {u}mount() operations performed here will be reflected on the | |
1561 | * host!) | |
1562 | */ | |
74a3920a | 1563 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 1564 | { |
f62cf1d4 | 1565 | __do_close int data_sock0 = -EBADF, data_sock1 = -EBADF; |
8deca6c9 | 1566 | int i, ret; |
08dd2805 | 1567 | char pidstr[20]; |
57927bf2 | 1568 | bool wants_to_map_ids; |
0589d744 | 1569 | struct list_head *id_map; |
6e5fc7a5 | 1570 | const char *name = handler->name; |
28d9e29e | 1571 | const char *lxcpath = handler->lxcpath; |
2202afc9 | 1572 | bool share_ns = false; |
28d9e29e | 1573 | struct lxc_conf *conf = handler->conf; |
2202afc9 | 1574 | struct cgroup_ops *cgroup_ops = handler->cgroup_ops; |
9f30a190 | 1575 | |
28d9e29e | 1576 | id_map = &conf->id_map; |
0589d744 | 1577 | wants_to_map_ids = !list_empty(id_map); |
2f2623ec | 1578 | |
28d9e29e | 1579 | for (i = 0; i < LXC_NS_MAX; i++) { |
b074bbf1 | 1580 | if (!conf->ns_share[i]) |
28d9e29e CB |
1581 | continue; |
1582 | ||
b074bbf1 | 1583 | handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name); |
28d9e29e CB |
1584 | if (handler->nsfd[i] < 0) |
1585 | return -1; | |
fa3a5b22 | 1586 | |
8deca6c9 | 1587 | share_ns = true; |
28d9e29e | 1588 | } |
50e98013 | 1589 | |
6e48e7c5 | 1590 | if (!lxc_sync_init(handler)) |
9d7f9e52 | 1591 | return -1; |
0ad19a3f | 1592 | |
b9f522c5 | 1593 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, |
790255cf | 1594 | handler->data_sock); |
95ef0d7c CB |
1595 | if (ret < 0) |
1596 | goto out_sync_fini; | |
cdb2a47f CB |
1597 | data_sock0 = handler->data_sock[0]; |
1598 | data_sock1 = handler->data_sock[1]; | |
e8bd4e43 | 1599 | |
5af9369b | 1600 | ret = resolve_clone_flags(handler); |
95ef0d7c CB |
1601 | if (ret < 0) |
1602 | goto out_sync_fini; | |
9f30a190 | 1603 | |
03ca4af8 TP |
1604 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
1605 | ret = lxc_find_gateway_addresses(handler); | |
1606 | if (ret) { | |
1607 | ERROR("Failed to find gateway addresses"); | |
1608 | goto out_sync_fini; | |
1609 | } | |
1610 | } | |
1611 | ||
e8b181f5 | 1612 | if (!cgroup_ops->payload_create(cgroup_ops, handler)) { |
cfc62c60 | 1613 | ERROR("Failed creating cgroups"); |
47d8fb3b CS |
1614 | goto out_delete_net; |
1615 | } | |
1616 | ||
408da065 | 1617 | /* Create a process in a new set of namespaces. */ |
4e232466 CB |
1618 | if (share_ns) { |
1619 | pid_t attacher_pid; | |
1620 | ||
cfc62c60 | 1621 | attacher_pid = lxc_clone(do_share_ns, handler, |
33258b95 | 1622 | CLONE_VFORK | CLONE_VM | CLONE_FILES, NULL); |
4e232466 CB |
1623 | if (attacher_pid < 0) { |
1624 | SYSERROR(LXC_CLONE_ERROR); | |
1625 | goto out_delete_net; | |
1626 | } | |
1627 | ||
1628 | ret = wait_for_pid(attacher_pid); | |
1629 | if (ret < 0) { | |
1630 | SYSERROR("Intermediate process failed"); | |
1631 | goto out_delete_net; | |
1632 | } | |
f7176c3e CB |
1633 | |
1634 | if (handler->pid < 0) { | |
1635 | SYSERROR(LXC_CLONE_ERROR); | |
1636 | goto out_delete_net; | |
1637 | } | |
4e232466 | 1638 | } else { |
84b66ced | 1639 | int cgroup_fd = -EBADF; |
f7176c3e CB |
1640 | |
1641 | struct lxc_clone_args clone_args = { | |
1642 | .flags = handler->clone_flags, | |
1643 | .pidfd = ptr_to_u64(&handler->pidfd), | |
1644 | .exit_signal = SIGCHLD, | |
1645 | }; | |
1646 | ||
1647 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { | |
1648 | cgroup_fd = cgroup_unified_fd(cgroup_ops); | |
1649 | if (cgroup_fd >= 0) { | |
1650 | handler->clone_flags |= CLONE_INTO_CGROUP; | |
1651 | clone_args.flags |= CLONE_INTO_CGROUP; | |
1652 | clone_args.cgroup = cgroup_fd; | |
1653 | } | |
1654 | } | |
1655 | ||
1656 | /* Try to spawn directly into target cgroup. */ | |
1657 | handler->pid = lxc_clone3(&clone_args, CLONE_ARGS_SIZE_VER2); | |
1658 | if (handler->pid < 0) { | |
1659 | SYSTRACE("Failed to spawn container directly into target cgroup"); | |
1660 | ||
1661 | /* Kernel might simply be too old for CLONE_INTO_CGROUP. */ | |
1662 | handler->clone_flags &= ~(CLONE_INTO_CGROUP | CLONE_NEWCGROUP); | |
1663 | handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP; | |
1664 | handler->ns_unshare_flags |= CLONE_NEWCGROUP; | |
1665 | ||
1666 | clone_args.flags = handler->clone_flags; | |
1667 | ||
1668 | handler->pid = lxc_clone3(&clone_args, CLONE_ARGS_SIZE_VER0); | |
1669 | } else if (cgroup_fd >= 0) { | |
1670 | TRACE("Spawned container directly into target cgroup via cgroup2 fd %d", cgroup_fd); | |
1671 | } | |
1672 | ||
1673 | /* Kernel might be too old for clone3(). */ | |
1674 | if (handler->pid < 0) { | |
1675 | SYSTRACE("Failed to spawn container via clone3()"); | |
abd80bde CB |
1676 | |
1677 | /* | |
1678 | * In contrast to all other architectures arm64 verifies that | |
1679 | * the argument we use to retrieve the pidfd with is | |
1680 | * initialized to 0. But we need to be able to initialize it to | |
1681 | * a negative value such as our customary -EBADF so we can | |
1682 | * detect whether this kernel supports pidfds. If the syscall | |
1683 | * returns and the pidfd variable is set to something >= 0 then | |
1684 | * we know this is a kernel supporting pidfds. But if we can't | |
1685 | * set it to -EBADF then this won't work since 0 is a valid | |
1686 | * file descriptor too. And since legacy clone silently ignores | |
1687 | * unknown flags we are left without any way to detect support | |
1688 | * for pidfds. So let's special-case arm64 to not fail starting | |
1689 | * containers. | |
1690 | */ | |
1691 | #if defined(__aarch64__) | |
1692 | handler->pid = lxc_raw_legacy_clone(handler->clone_flags & ~CLONE_PIDFD, NULL); | |
1693 | #else | |
f7176c3e | 1694 | handler->pid = lxc_raw_legacy_clone(handler->clone_flags, &handler->pidfd); |
abd80bde | 1695 | #endif |
f7176c3e CB |
1696 | } |
1697 | ||
1698 | if (handler->pid < 0) { | |
1699 | SYSERROR(LXC_CLONE_ERROR); | |
1700 | goto out_delete_net; | |
1701 | } | |
1702 | ||
1703 | if (handler->pid == 0) { | |
1704 | (void)do_start(handler); | |
1705 | _exit(EXIT_FAILURE); | |
1706 | } | |
0ad19a3f | 1707 | } |
f7176c3e CB |
1708 | if (handler->pidfd < 0) |
1709 | handler->clone_flags &= ~CLONE_PIDFD; | |
fa3a5b22 | 1710 | TRACE("Cloned child process %d", handler->pid); |
1bd8d726 | 1711 | |
39293f22 CB |
1712 | /* Verify that we can actually make use of pidfds. */ |
1713 | if (!lxc_can_use_pidfd(handler->pidfd)) | |
1714 | close_prot_errno_disarm(handler->pidfd); | |
1715 | ||
fa60cd7b CB |
1716 | ret = strnprintf(pidstr, 20, "%d", handler->pid); |
1717 | if (ret < 0) | |
1871e646 CB |
1718 | goto out_delete_net; |
1719 | ||
1720 | ret = setenv("LXC_PID", pidstr, 1); | |
1721 | if (ret < 0) | |
1722 | SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr); | |
1723 | ||
9662e444 | 1724 | for (i = 0; i < LXC_NS_MAX; i++) |
becad0ec | 1725 | if (handler->ns_on_clone_flags & ns_info[i].clone_flag) |
28d9e29e | 1726 | INFO("Cloned %s", ns_info[i].flag_name); |
0ad19a3f | 1727 | |
8db6be1b | 1728 | if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags)) { |
28d9e29e CB |
1729 | ERROR("Failed to preserve cloned namespaces for lxc.hook.stop"); |
1730 | goto out_delete_net; | |
1731 | } | |
b6b2b194 | 1732 | |
3c22086f CLG |
1733 | lxc_sync_fini_child(handler); |
1734 | ||
4d8bdfa0 CB |
1735 | if (lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->monitor_status_fd, 1, NULL, 0) < 0) { |
1736 | ERROR("Failed to send status file descriptor to child process"); | |
1737 | goto out_delete_net; | |
1738 | } | |
1739 | close_prot_errno_disarm(handler->monitor_status_fd); | |
1740 | ||
408da065 CB |
1741 | /* Map the container uids. The container became an invalid userid the |
1742 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1743 | * anything immediately, but allows the container to setuid(0) (0 being | |
1744 | * mapped to something else on the host.) later to become a valid uid | |
1745 | * again. | |
1746 | */ | |
fa3a5b22 | 1747 | if (wants_to_map_ids) { |
df3c5314 | 1748 | if (!handler->conf->ns_share[LXC_NS_USER] && |
46186acd | 1749 | (handler->conf->ns_keep & CLONE_NEWUSER) == 0) { |
fa3a5b22 CB |
1750 | ret = lxc_map_ids(id_map, handler->pid); |
1751 | if (ret < 0) { | |
1752 | ERROR("Failed to set up id mapping."); | |
1753 | goto out_delete_net; | |
1754 | } | |
1755 | } | |
5b1e83cb SH |
1756 | } |
1757 | ||
c581d2a6 | 1758 | if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) { |
cfc62c60 | 1759 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); |
6031a6e5 DE |
1760 | goto out_delete_net; |
1761 | } | |
1762 | ||
838d1556 CB |
1763 | if (!cgroup_ops->payload_delegate_controllers(cgroup_ops)) { |
1764 | ERROR("Failed to delegate controllers to payload cgroup"); | |
7fef7a06 | 1765 | goto out_delete_net; |
fe70edee | 1766 | } |
218d4250 | 1767 | |
838d1556 CB |
1768 | if (!cgroup_ops->payload_enter(cgroup_ops, handler)) { |
1769 | ERROR("Failed to enter cgroups"); | |
c581d2a6 CB |
1770 | goto out_delete_net; |
1771 | } | |
1772 | ||
1773 | if (!cgroup_ops->setup_limits(cgroup_ops, handler)) { | |
1774 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); | |
1775 | goto out_delete_net; | |
1776 | } | |
1777 | ||
2202afc9 | 1778 | if (!cgroup_ops->chown(cgroup_ops, handler->conf)) |
0996e18a SH |
1779 | goto out_delete_net; |
1780 | ||
6bc4165d CB |
1781 | if (!lxc_sync_barrier_child(handler, START_SYNC_STARTUP)) |
1782 | goto out_delete_net; | |
1783 | ||
aa0c0e7b RK |
1784 | /* If not done yet, we're now ready to preserve the network namespace */ |
1785 | if (handler->nsfd[LXC_NS_NET] < 0) { | |
8db6be1b | 1786 | ret = lxc_try_preserve_namespace(handler, LXC_NS_NET, "net"); |
aa0c0e7b | 1787 | if (ret < 0) { |
857ba1f0 | 1788 | if (ret != -ENOENT) { |
aa0c0e7b RK |
1789 | SYSERROR("Failed to preserve net namespace"); |
1790 | goto out_delete_net; | |
1791 | } | |
4cb53844 | 1792 | } |
fa3a5b22 | 1793 | } |
aa0c0e7b RK |
1794 | ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]); |
1795 | if (ret < 0) | |
1796 | SYSWARN("Failed to allocate new network namespace id"); | |
1797 | else | |
1798 | TRACE("Allocated new network namespace id"); | |
fa3a5b22 | 1799 | |
408da065 | 1800 | /* Create the network configuration. */ |
becad0ec | 1801 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
e389f2af | 1802 | ret = lxc_create_network(handler); |
cfc62c60 | 1803 | if (ret < 0) { |
e389f2af | 1804 | ERROR("Failed to create the network"); |
7fef7a06 | 1805 | goto out_delete_net; |
82d5ae15 | 1806 | } |
0ad19a3f | 1807 | } |
1808 | ||
91d04bf9 CB |
1809 | ret = setup_proc_filesystem(conf, handler->pid); |
1810 | if (ret < 0) { | |
1811 | ERROR("Failed to setup procfs limits"); | |
1812 | goto out_delete_net; | |
6bc4165d CB |
1813 | } |
1814 | ||
223797c3 CB |
1815 | ret = setup_resource_limits(conf, handler->pid); |
1816 | if (ret < 0) { | |
1817 | ERROR("Failed to setup resource limits"); | |
1818 | goto out_delete_net; | |
6bc4165d CB |
1819 | } |
1820 | ||
8945dad0 CB |
1821 | /* Tell the child to continue its initialization. */ |
1822 | if (!lxc_sync_wake_child(handler, START_SYNC_POST_CONFIGURE)) | |
1823 | goto out_delete_net; | |
1824 | ||
e4564b7e CB |
1825 | ret = lxc_rootfs_prepare_parent(handler); |
1826 | if (ret) { | |
1827 | ERROR("Failed to prepare rootfs"); | |
1828 | goto out_delete_net; | |
1829 | } | |
1830 | ||
8945dad0 CB |
1831 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
1832 | ret = lxc_network_send_to_child(handler); | |
1833 | if (ret < 0) { | |
6bc4165d | 1834 | SYSERROR("Failed to send veth names to child"); |
8945dad0 CB |
1835 | goto out_delete_net; |
1836 | } | |
1837 | } | |
1838 | ||
6bc4165d | 1839 | if (!lxc_sync_wait_child(handler, START_SYNC_IDMAPPED_MOUNTS)) |
f4152036 CB |
1840 | goto out_delete_net; |
1841 | ||
1b82d721 CB |
1842 | ret = lxc_idmapped_mounts_parent(handler); |
1843 | if (ret) { | |
1844 | ERROR("Failed to setup mount entries"); | |
1845 | goto out_delete_net; | |
1846 | } | |
1847 | ||
1848 | if (!lxc_sync_wait_child(handler, START_SYNC_CGROUP_LIMITS)) | |
1849 | goto out_delete_net; | |
1850 | ||
432faf20 | 1851 | /* |
3a89b0ab CB |
1852 | * With isolation the limiting devices cgroup was already setup, so |
1853 | * only setup devices here if we have no namespace directory. | |
432faf20 WB |
1854 | */ |
1855 | if (!handler->conf->cgroup_meta.namespace_dir && | |
1856 | !cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, true)) { | |
cfc62c60 | 1857 | ERROR("Failed to setup legacy device cgroup controller limits"); |
b98f7d6e | 1858 | goto out_delete_net; |
544a48a0 | 1859 | } |
cfc62c60 | 1860 | TRACE("Set up legacy device cgroup controller limits"); |
544a48a0 | 1861 | |
bf651989 CB |
1862 | if (!cgroup_ops->devices_activate(cgroup_ops, handler)) { |
1863 | ERROR("Failed to setup cgroup2 device controller limits"); | |
1864 | goto out_delete_net; | |
1865 | } | |
1866 | TRACE("Set up cgroup2 device controller limits"); | |
1867 | ||
6bc4165d CB |
1868 | cgroup_ops->finalize(cgroup_ops); |
1869 | TRACE("Finished setting up cgroups"); | |
1870 | ||
1871 | /* Run any host-side start hooks */ | |
1872 | ret = run_lxc_hooks(name, "start-host", conf, NULL); | |
1873 | if (ret < 0) { | |
1874 | ERROR("Failed to run lxc.hook.start-host"); | |
1875 | goto out_delete_net; | |
1876 | } | |
1877 | ||
1878 | if (!lxc_sync_wake_child(handler, START_SYNC_FDS)) | |
1879 | goto out_delete_net; | |
1880 | ||
f7176c3e | 1881 | if (handler->ns_unshare_flags & CLONE_NEWCGROUP) { |
8bf3abfb | 1882 | /* Now we're ready to preserve the cgroup namespace */ |
8db6be1b | 1883 | ret = lxc_try_preserve_namespace(handler, LXC_NS_CGROUP, "cgroup"); |
8bf3abfb | 1884 | if (ret < 0) { |
857ba1f0 | 1885 | if (ret != -ENOENT) { |
6d1400b5 | 1886 | SYSERROR("Failed to preserve cgroup namespace"); |
4cb53844 CB |
1887 | goto out_delete_net; |
1888 | } | |
8bf3abfb | 1889 | } |
8bf3abfb CB |
1890 | } |
1891 | ||
f7176c3e | 1892 | if (handler->ns_unshare_flags & CLONE_NEWTIME) { |
245066af | 1893 | /* Now we're ready to preserve the time namespace */ |
8db6be1b | 1894 | ret = lxc_try_preserve_namespace(handler, LXC_NS_TIME, "time"); |
f1c43439 | 1895 | if (ret < 0) { |
857ba1f0 | 1896 | if (ret != -ENOENT) { |
f1c43439 CB |
1897 | SYSERROR("Failed to preserve time namespace"); |
1898 | goto out_delete_net; | |
1899 | } | |
f1c43439 CB |
1900 | } |
1901 | } | |
1902 | ||
493ae3fe | 1903 | ret = lxc_sync_fds_parent(handler); |
1b82d721 | 1904 | if (ret < 0) { |
493ae3fe | 1905 | SYSERROR("Failed to sync file descriptors with child"); |
1b82d721 | 1906 | goto out_delete_net; |
790255cf CB |
1907 | } |
1908 | ||
9f77617b CB |
1909 | ret = lxc_terminal_setup(conf); |
1910 | if (ret < 0) { | |
1911 | SYSERROR("Failed to create console"); | |
1912 | goto out_delete_net; | |
1913 | } | |
1914 | ||
1b82d721 CB |
1915 | /* |
1916 | * Tell the child to complete its initialization and wait for it to | |
1917 | * exec or return an error. (The child will never return | |
1918 | * START_SYNC_READY_START+1. It will either close the sync pipe, | |
1919 | * causing lxc_sync_barrier_child to return success, or return a | |
1920 | * different value, causing us to error out). | |
1921 | */ | |
1922 | if (!lxc_sync_barrier_child(handler, START_SYNC_READY_START)) | |
1923 | goto out_delete_net; | |
1924 | ||
1925 | /* Now all networks are created, network devices are moved into place, | |
1926 | * and the correct names and ifindices in the respective namespaces have | |
1927 | * been recorded. The corresponding structs have now all been filled. So | |
1928 | * log them for debugging purposes. | |
1929 | */ | |
1930 | lxc_log_configured_netdevs(conf); | |
1931 | ||
cfc62c60 CB |
1932 | ret = handler->ops->post_start(handler, handler->data); |
1933 | if (ret < 0) | |
e6126dbe MN |
1934 | goto out_abort; |
1935 | ||
cfc62c60 CB |
1936 | ret = lxc_set_state(name, handler, RUNNING); |
1937 | if (ret < 0) { | |
1938 | ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING)); | |
59eb99ba | 1939 | goto out_abort; |
3f21c114 | 1940 | } |
22ebac19 | 1941 | |
3c22086f | 1942 | lxc_sync_fini(handler); |
0c547523 | 1943 | |
e6126dbe | 1944 | return 0; |
1ac470c0 | 1945 | |
7fef7a06 | 1946 | out_delete_net: |
becad0ec | 1947 | if (handler->ns_clone_flags & CLONE_NEWNET) |
bb84beda | 1948 | lxc_delete_network(handler); |
1bd8d726 | 1949 | |
59eb99ba | 1950 | out_abort: |
0c5859ff | 1951 | lxc_abort(handler); |
95ef0d7c CB |
1952 | |
1953 | out_sync_fini: | |
3c22086f | 1954 | lxc_sync_fini(handler); |
5c068da9 | 1955 | |
b79fcd86 | 1956 | return -1; |
59eb99ba | 1957 | } |
0ad19a3f | 1958 | |
7bdf97d2 CB |
1959 | int __lxc_start(struct lxc_handler *handler, struct lxc_operations *ops, |
1960 | void *data, const char *lxcpath, bool daemonize, int *error_num) | |
59eb99ba | 1961 | { |
c30e9b19 | 1962 | int ret, status; |
7bdf97d2 | 1963 | const char *name = handler->name; |
aa460476 | 1964 | struct lxc_conf *conf = handler->conf; |
72068e74 | 1965 | struct cgroup_ops *cgroup_ops; |
80090207 | 1966 | |
c30e9b19 CB |
1967 | ret = lxc_init(name, handler); |
1968 | if (ret < 0) { | |
1969 | ERROR("Failed to initialize container \"%s\"", name); | |
9ad66589 | 1970 | goto out_abort; |
0ad19a3f | 1971 | } |
ee70bf78 CLG |
1972 | handler->ops = ops; |
1973 | handler->data = data; | |
bb955810 | 1974 | handler->daemonize = daemonize; |
72068e74 | 1975 | cgroup_ops = handler->cgroup_ops; |
e6126dbe | 1976 | |
76a26f55 | 1977 | if (!attach_block_device(handler->conf)) { |
c30e9b19 | 1978 | ERROR("Failed to attach block device"); |
575ea467 | 1979 | ret = -1; |
9ad66589 | 1980 | goto out_abort; |
76a26f55 SH |
1981 | } |
1982 | ||
72068e74 CB |
1983 | if (!cgroup_ops->monitor_create(cgroup_ops, handler)) { |
1984 | ERROR("Failed to create monitor cgroup"); | |
575ea467 | 1985 | ret = -1; |
9ad66589 | 1986 | goto out_abort; |
eeef32bb CB |
1987 | } |
1988 | ||
838d1556 CB |
1989 | if (!cgroup_ops->monitor_delegate_controllers(cgroup_ops)) { |
1990 | ERROR("Failed to delegate controllers to monitor cgroup"); | |
575ea467 | 1991 | ret = -1; |
9ad66589 | 1992 | goto out_abort; |
72068e74 CB |
1993 | } |
1994 | ||
838d1556 CB |
1995 | if (!cgroup_ops->monitor_enter(cgroup_ops, handler)) { |
1996 | ERROR("Failed to enter monitor cgroup"); | |
c581d2a6 | 1997 | ret = -1; |
9ad66589 | 1998 | goto out_abort; |
c581d2a6 CB |
1999 | } |
2000 | ||
e26cf563 CB |
2001 | /* If the rootfs is not a blockdev, prevent the container from marking |
2002 | * it readonly. | |
2003 | * If the container is unprivileged then skip rootfs pinning. | |
2004 | */ | |
0589d744 | 2005 | ret = lxc_rootfs_init(conf, !list_empty(&conf->id_map)); |
e26cf563 CB |
2006 | if (ret) { |
2007 | ERROR("Failed to handle rootfs pinning for container \"%s\"", handler->name); | |
2008 | ret = -1; | |
2009 | goto out_abort; | |
2010 | } | |
2011 | ||
0589d744 | 2012 | if (geteuid() == 0 && !list_empty(&conf->id_map)) { |
26ad2c6f | 2013 | /* |
4b875ef9 | 2014 | * Most filesystems can't be mounted inside a userns so handle them here. |
26ad2c6f | 2015 | */ |
4b875ef9 | 2016 | if (rootfs_is_blockdev(conf)) { |
c30e9b19 CB |
2017 | ret = unshare(CLONE_NEWNS); |
2018 | if (ret < 0) { | |
2019 | ERROR("Failed to unshare CLONE_NEWNS"); | |
9ad66589 | 2020 | goto out_abort; |
35120d9c | 2021 | } |
c30e9b19 | 2022 | INFO("Unshared CLONE_NEWNS"); |
408da065 | 2023 | |
8ce1abc2 | 2024 | ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath); |
c30e9b19 CB |
2025 | if (ret < 0) { |
2026 | ERROR("Error setting up rootfs mount as root before spawn"); | |
9ad66589 | 2027 | goto out_abort; |
35120d9c | 2028 | } |
c30e9b19 | 2029 | INFO("Set up container rootfs as host root"); |
35120d9c SH |
2030 | } |
2031 | } | |
2032 | ||
c30e9b19 CB |
2033 | ret = lxc_spawn(handler); |
2034 | if (ret < 0) { | |
2035 | ERROR("Failed to spawn container \"%s\"", name); | |
76a26f55 | 2036 | goto out_detach_blockdev; |
0ad19a3f | 2037 | } |
2038 | ||
80308d07 | 2039 | handler->conf->reboot = REBOOT_NONE; |
8bee8851 | 2040 | |
c30e9b19 CB |
2041 | ret = lxc_poll(name, handler); |
2042 | if (ret) { | |
2043 | ERROR("LXC mainloop exited with error: %d", ret); | |
9ad66589 | 2044 | goto out_delete_network; |
59eb99ba | 2045 | } |
0ad19a3f | 2046 | |
321db026 DJ |
2047 | if (!handler->init_died && handler->pid > 0) { |
2048 | ERROR("Child process is not killed"); | |
575ea467 | 2049 | ret = -1; |
9ad66589 | 2050 | goto out_delete_network; |
321db026 DJ |
2051 | } |
2052 | ||
c30e9b19 CB |
2053 | status = lxc_wait_for_pid_status(handler->pid); |
2054 | if (status < 0) | |
2055 | SYSERROR("Failed to retrieve status for %d", handler->pid); | |
e043236e | 2056 | |
408da065 CB |
2057 | /* If the child process exited but was not signaled, it didn't call |
2058 | * reboot. This should mean it was an lxc-execute which simply exited. | |
2059 | * In any case, treat it as a 'halt'. | |
8b004f07 | 2060 | */ |
d028235d | 2061 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
2062 | switch(WTERMSIG(status)) { |
2063 | case SIGINT: /* halt */ | |
c30e9b19 | 2064 | DEBUG("Container \"%s\" is halting", name); |
8b004f07 SH |
2065 | break; |
2066 | case SIGHUP: /* reboot */ | |
c30e9b19 | 2067 | DEBUG("Container \"%s\" is rebooting", name); |
80308d07 | 2068 | handler->conf->reboot = REBOOT_REQ; |
8b004f07 | 2069 | break; |
c2b9bd9e | 2070 | case SIGSYS: /* seccomp */ |
c30e9b19 | 2071 | DEBUG("Container \"%s\" violated its seccomp policy", name); |
c2b9bd9e | 2072 | break; |
8b004f07 | 2073 | default: |
c30e9b19 | 2074 | DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status)); |
8b004f07 SH |
2075 | break; |
2076 | } | |
d028235d | 2077 | } |
828695d9 | 2078 | |
c30e9b19 CB |
2079 | ret = lxc_restore_phys_nics_to_netns(handler); |
2080 | if (ret < 0) | |
818a57fc | 2081 | ERROR("Failed to move physical network devices back to parent network namespace"); |
ce5782df | 2082 | |
1787abca | 2083 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
eb808539 | 2084 | lxc_error_set_and_log(handler->pid, status); |
a3b4f3d6 TA |
2085 | if (error_num) |
2086 | *error_num = handler->exit_status; | |
1bd8d726 | 2087 | |
bb84beda | 2088 | lxc_delete_network(handler); |
76a26f55 | 2089 | detach_block_device(handler->conf); |
fd5be714 | 2090 | lxc_end(handler); |
c30e9b19 | 2091 | return ret; |
0ad19a3f | 2092 | |
9ad66589 | 2093 | out_abort: |
0c5859ff | 2094 | lxc_abort(handler); |
945daa24 CB |
2095 | lxc_end(handler); |
2096 | return ret; | |
65a2dc8b CB |
2097 | |
2098 | out_detach_blockdev: | |
0c5859ff | 2099 | lxc_abort(handler); |
945daa24 CB |
2100 | detach_block_device(handler->conf); |
2101 | lxc_end(handler); | |
2102 | return ret; | |
65a2dc8b | 2103 | |
9ad66589 | 2104 | out_delete_network: |
0c5859ff | 2105 | lxc_abort(handler); |
abd953eb | 2106 | lxc_restore_phys_nics_to_netns(handler); |
945daa24 CB |
2107 | lxc_delete_network(handler); |
2108 | detach_block_device(handler->conf); | |
2109 | lxc_end(handler); | |
2110 | return ret; | |
0ad19a3f | 2111 | } |
ee70bf78 CLG |
2112 | |
2113 | struct start_args { | |
2114 | char *const *argv; | |
2115 | }; | |
2116 | ||
2117 | static int start(struct lxc_handler *handler, void* data) | |
2118 | { | |
2119 | struct start_args *arg = data; | |
2120 | ||
984984e4 | 2121 | NOTICE("Exec'ing \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
2122 | |
2123 | execvp(arg->argv[0], arg->argv); | |
984984e4 | 2124 | SYSERROR("Failed to exec \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
2125 | return 0; |
2126 | } | |
2127 | ||
2128 | static int post_start(struct lxc_handler *handler, void* data) | |
2129 | { | |
2130 | struct start_args *arg = data; | |
2131 | ||
4c8e880e | 2132 | NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid); |
ee70bf78 CLG |
2133 | return 0; |
2134 | } | |
2135 | ||
2136 | static struct lxc_operations start_ops = { | |
2137 | .start = start, | |
2138 | .post_start = post_start | |
2139 | }; | |
2140 | ||
7bdf97d2 | 2141 | int lxc_start(char *const argv[], struct lxc_handler *handler, |
bb955810 | 2142 | const char *lxcpath, bool daemonize, int *error_num) |
ee70bf78 CLG |
2143 | { |
2144 | struct start_args start_arg = { | |
2145 | .argv = argv, | |
2146 | }; | |
2147 | ||
b2efeb0b | 2148 | TRACE("Doing lxc_start"); |
7bdf97d2 | 2149 | return __lxc_start(handler, &start_ops, &start_arg, lxcpath, daemonize, error_num); |
ee70bf78 | 2150 | } |
28272964 CB |
2151 | |
2152 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
2153 | const char *name) | |
2154 | { | |
245100a0 | 2155 | char destroy[PATH_MAX]; |
f01f7975 | 2156 | struct lxc_container *c; |
ae3beac9 CB |
2157 | int ret = 0; |
2158 | bool bret = true; | |
2159 | ||
df31363a | 2160 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
790255cf | 2161 | bret = do_destroy_container(handler); |
28272964 | 2162 | if (!bret) { |
ae3beac9 | 2163 | ERROR("Error destroying rootfs for container \"%s\"", name); |
28272964 CB |
2164 | return; |
2165 | } | |
2166 | } | |
ae3beac9 | 2167 | INFO("Destroyed rootfs for container \"%s\"", name); |
28272964 | 2168 | |
fa60cd7b CB |
2169 | ret = strnprintf(destroy, sizeof(destroy), "%s/%s", handler->lxcpath, name); |
2170 | if (ret < 0) { | |
ae3beac9 | 2171 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
2172 | return; |
2173 | } | |
2174 | ||
f01f7975 CB |
2175 | c = lxc_container_new(name, handler->lxcpath); |
2176 | if (c) { | |
2177 | if (container_disk_lock(c)) { | |
ae3beac9 | 2178 | INFO("Could not update lxc_snapshots file"); |
f01f7975 CB |
2179 | lxc_container_put(c); |
2180 | } else { | |
2181 | mod_all_rdeps(c, false); | |
2182 | container_disk_unlock(c); | |
2183 | lxc_container_put(c); | |
2184 | } | |
2185 | } | |
2186 | ||
d0fbc7ba | 2187 | if (!handler->am_root) |
94b0a3ac CB |
2188 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, |
2189 | destroy, "lxc_rmdir_onedev_wrapper"); | |
28272964 CB |
2190 | else |
2191 | ret = lxc_rmdir_onedev(destroy, NULL); | |
2192 | ||
2193 | if (ret < 0) { | |
ae3beac9 | 2194 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
2195 | return; |
2196 | } | |
ae3beac9 | 2197 | INFO("Destroyed directory for container \"%s\"", name); |
28272964 CB |
2198 | } |
2199 | ||
2200 | static int lxc_rmdir_onedev_wrapper(void *data) | |
2201 | { | |
2202 | char *arg = (char *) data; | |
2203 | return lxc_rmdir_onedev(arg, NULL); | |
2204 | } | |
2205 | ||
134df645 CB |
2206 | static bool do_destroy_container(struct lxc_handler *handler) |
2207 | { | |
94b0a3ac CB |
2208 | int ret; |
2209 | ||
d0fbc7ba | 2210 | if (!handler->am_root) { |
94b0a3ac CB |
2211 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, |
2212 | handler->conf, "storage_destroy_wrapper"); | |
2213 | if (ret < 0) | |
d028235d | 2214 | return false; |
10bc1861 | 2215 | |
d028235d SG |
2216 | return true; |
2217 | } | |
10bc1861 | 2218 | |
790255cf | 2219 | return storage_destroy(handler->conf); |
28272964 | 2220 | } |