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