]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
start: use lxc_safe_int()
[mirror_lxc.git] / src / lxc / start.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _GNU_SOURCE
25 #include "config.h"
26
27 #include <alloca.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <grp.h>
32 #include <poll.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/file.h>
39 #include <sys/mount.h>
40 #include <sys/param.h>
41 #include <sys/prctl.h>
42 #include <sys/socket.h>
43 #include <sys/stat.h>
44 #include <sys/syscall.h>
45 #include <sys/types.h>
46 #include <sys/un.h>
47 #include <sys/wait.h>
48
49 #if HAVE_SYS_CAPABILITY_H
50 #include <sys/capability.h>
51 #endif
52
53 #ifndef HAVE_DECL_PR_CAPBSET_DROP
54 #define PR_CAPBSET_DROP 24
55 #endif
56
57 #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS
58 #define PR_SET_NO_NEW_PRIVS 38
59 #endif
60
61 #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS
62 #define PR_GET_NO_NEW_PRIVS 39
63 #endif
64
65 #include "af_unix.h"
66 #include "bdev.h"
67 #include "caps.h"
68 #include "cgroup.h"
69 #include "commands.h"
70 #include "conf.h"
71 #include "console.h"
72 #include "error.h"
73 #include "log.h"
74 #include "lxclock.h"
75 #include "lxcseccomp.h"
76 #include "lxcutmp.h"
77 #include "mainloop.h"
78 #include "monitor.h"
79 #include "namespace.h"
80 #include "start.h"
81 #include "sync.h"
82 #include "utils.h"
83 #include "lsm/lsm.h"
84
85 lxc_log_define(lxc_start, lxc);
86
87 extern void mod_all_rdeps(struct lxc_container *c, bool inc);
88 static bool do_destroy_container(struct lxc_conf *conf);
89 static int lxc_rmdir_onedev_wrapper(void *data);
90 static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
91 const char *name);
92
93 static void print_top_failing_dir(const char *path)
94 {
95 size_t len = strlen(path);
96 char *copy = alloca(len + 1), *p, *e, saved;
97 strcpy(copy, path);
98
99 p = copy;
100 e = copy + len;
101 while (p < e) {
102 while (p < e && *p == '/')
103 p++;
104 while (p < e && *p != '/')
105 p++;
106 saved = *p;
107 *p = '\0';
108 if (access(copy, X_OK)) {
109 SYSERROR("Could not access %s. Please grant it x "
110 "access, or add an ACL for the container "
111 "root.", copy);
112 return;
113 }
114 *p = saved;
115 }
116 }
117
118 static void close_ns(int ns_fd[LXC_NS_MAX])
119 {
120 int i;
121
122 for (i = 0; i < LXC_NS_MAX; i++) {
123 if (ns_fd[i] > -1) {
124 close(ns_fd[i]);
125 ns_fd[i] = -1;
126 }
127 }
128 }
129
130 /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified
131 * in clone_flags.
132 * Return true on success, false on failure.
133 */
134 static bool preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid)
135 {
136 int i, ret;
137
138 for (i = 0; i < LXC_NS_MAX; i++)
139 ns_fd[i] = -1;
140
141 ret = lxc_preserve_ns(pid, "");
142 if (ret < 0) {
143 SYSERROR("Kernel does not support attaching to namespaces.");
144 return false;
145 } else {
146 close(ret);
147 }
148
149 for (i = 0; i < LXC_NS_MAX; i++) {
150 if ((clone_flags & ns_info[i].clone_flag) == 0)
151 continue;
152 ns_fd[i] = lxc_preserve_ns(pid, ns_info[i].proc_name);
153 if (ns_fd[i] < 0)
154 goto error;
155 }
156
157 return true;
158
159 error:
160 if (errno == ENOENT)
161 SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info[i].proc_name);
162 else
163 SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info[i].proc_name, strerror(errno));
164 close_ns(ns_fd);
165 return false;
166 }
167
168 static int attach_ns(const int ns_fd[LXC_NS_MAX]) {
169 int i;
170
171 for (i = 0; i < LXC_NS_MAX; i++) {
172 if (ns_fd[i] < 0)
173 continue;
174
175 if (setns(ns_fd[i], 0) != 0)
176 goto error;
177 }
178 return 0;
179
180 error:
181 SYSERROR("Failed to attach %s namespace.", ns_info[i].proc_name);
182 return -1;
183 }
184
185 static int match_fd(int fd)
186 {
187 return (fd == 0 || fd == 1 || fd == 2);
188 }
189
190 /* Check for any fds we need to close.
191 * - If fd_to_ignore != -1, then if we find that fd open we will ignore it.
192 * - By default we warn about open fds we find.
193 * - If closeall is true, we will close open fds.
194 * - If lxc-start was passed "-C", then conf->close_all_fds will be true, in
195 * which case we also close all open fds.
196 * - A daemonized container will always pass closeall=true.
197 */
198 int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore)
199 {
200 struct dirent *direntp;
201 int fd, fddir;
202 DIR *dir;
203
204 if (conf && conf->close_all_fds)
205 closeall = true;
206
207 restart:
208 dir = opendir("/proc/self/fd");
209 if (!dir) {
210 WARN("Failed to open directory: %m.");
211 return -1;
212 }
213
214 fddir = dirfd(dir);
215
216 while ((direntp = readdir(dir))) {
217 if (!direntp)
218 break;
219
220 if (!strcmp(direntp->d_name, "."))
221 continue;
222
223 if (!strcmp(direntp->d_name, ".."))
224 continue;
225
226 if (lxc_safe_int(direntp->d_name, &fd) < 0) {
227 INFO("Could not parse file descriptor for: %s", direntp->d_name);
228 continue;
229 }
230
231 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
232 continue;
233
234 if (current_config && fd == current_config->logfd)
235 continue;
236
237 if (match_fd(fd))
238 continue;
239
240 if (closeall) {
241 close(fd);
242 closedir(dir);
243 INFO("Closed inherited fd: %d.", fd);
244 goto restart;
245 }
246 WARN("Inherited fd: %d.", fd);
247 }
248
249 /* Only enable syslog at this point to avoid the above logging function
250 * to open a new fd and make the check_inherited function enter an
251 * infinite loop.
252 */
253 lxc_log_enable_syslog();
254
255 closedir(dir); /* cannot fail */
256 return 0;
257 }
258
259 static int setup_signal_fd(sigset_t *oldmask)
260 {
261 sigset_t mask;
262 int fd;
263
264 /* Block everything except serious error signals. */
265 if (sigfillset(&mask) ||
266 sigdelset(&mask, SIGILL) ||
267 sigdelset(&mask, SIGSEGV) ||
268 sigdelset(&mask, SIGBUS) ||
269 sigdelset(&mask, SIGWINCH) ||
270 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
271 SYSERROR("Failed to set signal mask.");
272 return -1;
273 }
274
275 fd = signalfd(-1, &mask, 0);
276 if (fd < 0) {
277 SYSERROR("Failed to create signal file descriptor.");
278 return -1;
279 }
280
281 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
282 SYSERROR("Failed to set FD_CLOEXEC on the signal file descriptor: %d.", fd);
283 close(fd);
284 return -1;
285 }
286
287 DEBUG("Set SIGCHLD handler with file descriptor: %d.", fd);
288
289 return fd;
290 }
291
292 static int signal_handler(int fd, uint32_t events, void *data,
293 struct lxc_epoll_descr *descr)
294 {
295 struct signalfd_siginfo siginfo;
296 siginfo_t info;
297 int ret;
298 pid_t *pid = data;
299 bool init_died = false;
300
301 ret = read(fd, &siginfo, sizeof(siginfo));
302 if (ret < 0) {
303 ERROR("Failed to read signal info from signal file descriptor: %d.", fd);
304 return -1;
305 }
306
307 if (ret != sizeof(siginfo)) {
308 ERROR("Unexpected size for siginfo struct.");
309 return -1;
310 }
311
312 /* Check whether init is running. */
313 info.si_pid = 0;
314 ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG);
315 if (ret == 0 && info.si_pid == *pid)
316 init_died = true;
317
318 if (siginfo.ssi_signo != SIGCHLD) {
319 kill(*pid, siginfo.ssi_signo);
320 INFO("Forwarded signal %d to pid %d.", siginfo.ssi_signo, *pid);
321 return init_died ? 1 : 0;
322 }
323
324 if (siginfo.ssi_code == CLD_STOPPED) {
325 INFO("Container init process was stopped.");
326 return init_died ? 1 : 0;
327 } else if (siginfo.ssi_code == CLD_CONTINUED) {
328 INFO("Container init process was continued.");
329 return init_died ? 1 : 0;
330 }
331
332 /* More robustness, protect ourself from a SIGCHLD sent
333 * by a process different from the container init.
334 */
335 if (siginfo.ssi_pid != *pid) {
336 WARN("Invalid pid for SIGCHLD. Received pid %d, expected pid %d.", siginfo.ssi_pid, *pid);
337 return init_died ? 1 : 0;
338 }
339
340 DEBUG("Container init process %d exited.", *pid);
341 return 1;
342 }
343
344 int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
345 {
346 handler->state = state;
347 lxc_monitor_send_state(name, state, handler->lxcpath);
348 return 0;
349 }
350
351 int lxc_poll(const char *name, struct lxc_handler *handler)
352 {
353 int sigfd = handler->sigfd;
354 int pid = handler->pid;
355 struct lxc_epoll_descr descr;
356
357 if (lxc_mainloop_open(&descr)) {
358 ERROR("Failed to create LXC mainloop.");
359 goto out_sigfd;
360 }
361
362 if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
363 ERROR("Failed to add signal handler with file descriptor %d to LXC mainloop.", sigfd);
364 goto out_mainloop_open;
365 }
366
367 if (lxc_console_mainloop_add(&descr, handler->conf)) {
368 ERROR("Failed to add console handler to LXC mainloop.");
369 goto out_mainloop_open;
370 }
371
372 if (lxc_cmd_mainloop_add(name, &descr, handler)) {
373 ERROR("Failed to add command handler to LXC mainloop.");
374 goto out_mainloop_open;
375 }
376
377 if (handler->conf->need_utmp_watch) {
378 #if HAVE_SYS_CAPABILITY_H
379 if (lxc_utmp_mainloop_add(&descr, handler)) {
380 ERROR("Failed to add utmp handler to LXC mainloop.");
381 goto out_mainloop_open;
382 }
383 #else
384 DEBUG("Not starting utmp handler as CAP_SYS_BOOT cannot be dropped without capabilities support.");
385 #endif
386 }
387
388 return lxc_mainloop(&descr, -1);
389
390 out_mainloop_open:
391 lxc_mainloop_close(&descr);
392
393 out_sigfd:
394 close(sigfd);
395
396 return -1;
397 }
398
399 struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath)
400 {
401 int i;
402 struct lxc_handler *handler;
403
404 handler = malloc(sizeof(*handler));
405 if (!handler)
406 return NULL;
407
408 memset(handler, 0, sizeof(*handler));
409
410 handler->ttysock[0] = handler->ttysock[1] = -1;
411 handler->conf = conf;
412 handler->lxcpath = lxcpath;
413 handler->pinfd = -1;
414
415 for (i = 0; i < LXC_NS_MAX; i++)
416 handler->nsfd[i] = -1;
417
418 lsm_init();
419
420 handler->name = strdup(name);
421 if (!handler->name) {
422 ERROR("Failed to allocate memory.");
423 goto out_free;
424 }
425
426 if (lxc_cmd_init(name, handler, lxcpath))
427 goto out_free_name;
428
429 if (lxc_read_seccomp_config(conf) != 0) {
430 ERROR("Failed loading seccomp policy.");
431 goto out_close_maincmd_fd;
432 }
433
434 /* Begin by setting the state to STARTING. */
435 if (lxc_set_state(name, handler, STARTING)) {
436 ERROR("Failed to set state for container \"%s\" to \"%s\".", name, lxc_state2str(STARTING));
437 goto out_close_maincmd_fd;
438 }
439
440 /* Start of environment variable setup for hooks. */
441 if (name && setenv("LXC_NAME", name, 1))
442 SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name);
443
444 if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1))
445 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf->rcfile);
446
447 if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1))
448 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf->rootfs.mount);
449
450 if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1))
451 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf->rootfs.path);
452
453 if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1))
454 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf->console.path);
455
456 if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1))
457 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf->console.log_path);
458
459 if (setenv("LXC_CGNS_AWARE", "1", 1))
460 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1.");
461 /* End of environment variable setup for hooks. */
462
463 if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) {
464 ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name);
465 goto out_aborting;
466 }
467
468 /* The signal fd has to be created before forking otherwise if the child
469 * process exits before we setup the signal fd, the event will be lost
470 * and the command will be stuck.
471 */
472 handler->sigfd = setup_signal_fd(&handler->oldmask);
473 if (handler->sigfd < 0) {
474 ERROR("Failed to setup SIGCHLD fd handler.");
475 goto out_delete_tty;
476 }
477
478 /* Do this after setting up signals since it might unblock SIGWINCH. */
479 if (lxc_console_create(conf)) {
480 ERROR("Failed to create console for container \"%s\".", name);
481 goto out_restore_sigmask;
482 }
483
484 if (ttys_shift_ids(conf) < 0) {
485 ERROR("Failed to shift tty into container.");
486 goto out_restore_sigmask;
487 }
488
489 INFO("Container \"%s\" is initialized.", name);
490 return handler;
491
492 out_restore_sigmask:
493 sigprocmask(SIG_SETMASK, &handler->oldmask, NULL);
494 out_delete_tty:
495 lxc_delete_tty(&conf->tty_info);
496 out_aborting:
497 lxc_set_state(name, handler, ABORTING);
498 out_close_maincmd_fd:
499 close(conf->maincmd_fd);
500 conf->maincmd_fd = -1;
501 out_free_name:
502 free(handler->name);
503 handler->name = NULL;
504 out_free:
505 free(handler);
506 return NULL;
507 }
508
509 void lxc_fini(const char *name, struct lxc_handler *handler)
510 {
511 int i, rc;
512 pid_t self = getpid();
513 char *namespaces[LXC_NS_MAX+1];
514 size_t namespace_count = 0;
515
516 /* The STOPPING state is there for future cleanup code which can take
517 * awhile.
518 */
519 lxc_set_state(name, handler, STOPPING);
520
521 for (i = 0; i < LXC_NS_MAX; i++) {
522 if (handler->nsfd[i] != -1) {
523 rc = asprintf(&namespaces[namespace_count], "%s:/proc/%d/fd/%d",
524 ns_info[i].proc_name, self, handler->nsfd[i]);
525 if (rc == -1) {
526 SYSERROR("Failed to allocate memory.");
527 break;
528 }
529 ++namespace_count;
530 }
531 }
532 namespaces[namespace_count] = NULL;
533
534 if (handler->conf->reboot && setenv("LXC_TARGET", "reboot", 1))
535 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot.");
536
537 if (!handler->conf->reboot && setenv("LXC_TARGET", "stop", 1))
538 SYSERROR("Failed to set environment variable: LXC_TARGET=stop.");
539
540 if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath, namespaces))
541 ERROR("Failed to run lxc.hook.stop for container \"%s\".", name);
542
543 while (namespace_count--)
544 free(namespaces[namespace_count]);
545 for (i = 0; i < LXC_NS_MAX; i++) {
546 if (handler->nsfd[i] != -1) {
547 close(handler->nsfd[i]);
548 handler->nsfd[i] = -1;
549 }
550 }
551
552 if (handler->netnsfd >= 0) {
553 close(handler->netnsfd);
554 handler->netnsfd = -1;
555 }
556
557 lxc_set_state(name, handler, STOPPED);
558
559 if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) {
560 ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name);
561 if (handler->conf->reboot) {
562 WARN("Container will be stopped instead of rebooted.");
563 handler->conf->reboot = 0;
564 if (setenv("LXC_TARGET", "stop", 1))
565 WARN("Failed to set environment variable: LXC_TARGET=stop.");
566 }
567 }
568
569 /* Reset mask set by setup_signal_fd. */
570 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
571 WARN("Failed to restore signal mask.");
572
573 lxc_console_delete(&handler->conf->console);
574 lxc_delete_tty(&handler->conf->tty_info);
575 close(handler->conf->maincmd_fd);
576 handler->conf->maincmd_fd = -1;
577 free(handler->name);
578 if (handler->ttysock[0] != -1) {
579 close(handler->ttysock[0]);
580 close(handler->ttysock[1]);
581 }
582
583 if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1)
584 lxc_destroy_container_on_signal(handler, name);
585
586 cgroup_destroy(handler);
587 free(handler);
588 }
589
590 void lxc_abort(const char *name, struct lxc_handler *handler)
591 {
592 int ret, status;
593
594 lxc_set_state(name, handler, ABORTING);
595 if (handler->pid > 0)
596 kill(handler->pid, SIGKILL);
597 while ((ret = waitpid(-1, &status, 0)) > 0) {
598 ;
599 }
600 }
601
602 #include <sys/reboot.h>
603 #include <linux/reboot.h>
604
605 /* reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL in a child pid namespace
606 * if container reboot support exists. Otherwise, it will either succeed or
607 * return -EPERM.
608 */
609 static int container_reboot_supported(void *arg)
610 {
611 int *cmd = arg;
612 int ret;
613
614 ret = reboot(*cmd);
615 if (ret == -1 && errno == EINVAL)
616 return 1;
617 return 0;
618 }
619
620 static int must_drop_cap_sys_boot(struct lxc_conf *conf)
621 {
622 FILE *f;
623 int ret, cmd, v, flags;
624 long stack_size = 4096;
625 void *stack = alloca(stack_size);
626 int status;
627 pid_t pid;
628
629 f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
630 if (!f) {
631 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
632 return 1;
633 }
634
635 ret = fscanf(f, "%d", &v);
636 fclose(f);
637 if (ret != 1) {
638 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del.");
639 return 1;
640 }
641 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
642
643 flags = CLONE_NEWPID | SIGCHLD;
644 if (!lxc_list_empty(&conf->id_map))
645 flags |= CLONE_NEWUSER;
646
647 #ifdef __ia64__
648 pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd);
649 #else
650 stack += stack_size;
651 pid = clone(container_reboot_supported, stack, flags, &cmd);
652 #endif
653 if (pid < 0) {
654 if (flags & CLONE_NEWUSER)
655 ERROR("Failed to clone (%#x): %s (includes CLONE_NEWUSER).", flags, strerror(errno));
656 else
657 ERROR("Failed to clone (%#x): %s.", flags, strerror(errno));
658 return -1;
659 }
660 if (wait(&status) < 0) {
661 SYSERROR("Unexpected wait error: %m.");
662 return -1;
663 }
664
665 if (WEXITSTATUS(status) != 1)
666 return 1;
667
668 return 0;
669 }
670
671 /* netpipe is used in the unprivileged case to transfer the ifindexes from
672 * parent to child
673 */
674 static int netpipe = -1;
675
676 static inline int count_veths(struct lxc_list *network)
677 {
678 struct lxc_list *iterator;
679 struct lxc_netdev *netdev;
680 int count = 0;
681
682 lxc_list_for_each(iterator, network) {
683 netdev = iterator->elem;
684 if (netdev->type != LXC_NET_VETH)
685 continue;
686 count++;
687 }
688 return count;
689 }
690
691 static int read_unpriv_netifindex(struct lxc_list *network)
692 {
693 struct lxc_list *iterator;
694 struct lxc_netdev *netdev;
695
696 if (netpipe == -1)
697 return 0;
698 lxc_list_for_each(iterator, network) {
699 netdev = iterator->elem;
700 if (netdev->type != LXC_NET_VETH)
701 continue;
702 if (!(netdev->name = malloc(IFNAMSIZ))) {
703 ERROR("Out of memory.");
704 close(netpipe);
705 return -1;
706 }
707 if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) {
708 close(netpipe);
709 return -1;
710 }
711 }
712 close(netpipe);
713 return 0;
714 }
715
716 static int do_start(void *data)
717 {
718 struct lxc_list *iterator;
719 struct lxc_handler *handler = data;
720 int devnull_fd = -1, ret;
721 char path[PATH_MAX];
722
723 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
724 SYSERROR("Failed to set signal mask.");
725 return -1;
726 }
727
728 /* This prctl must be before the synchro, so if the parent dies before
729 * we set the parent death signal, we will detect its death with the
730 * synchro right after, otherwise we have a window where the parent can
731 * exit before we set the pdeath signal leading to a unsupervized
732 * container.
733 */
734 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
735 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL.");
736 return -1;
737 }
738
739 lxc_sync_fini_parent(handler);
740
741 /* Don't leak the pinfd to the container. */
742 if (handler->pinfd >= 0) {
743 close(handler->pinfd);
744 }
745
746 if (lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP))
747 return -1;
748
749 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
750 * https://github.com/lxc/lxd/issues/1978.
751 */
752 if ((handler->clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) ==
753 (CLONE_NEWNET | CLONE_NEWUSER)) {
754 ret = unshare(CLONE_NEWNET);
755 if (ret < 0) {
756 SYSERROR("Failed to unshare CLONE_NEWNET.");
757 goto out_warn_father;
758 }
759 INFO("Unshared CLONE_NEWNET.");
760 }
761
762 /* Tell the parent task it can begin to configure the container and wait
763 * for it to finish.
764 */
765 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
766 return -1;
767
768 if (read_unpriv_netifindex(&handler->conf->network) < 0)
769 goto out_warn_father;
770
771 /* If we are in a new user namespace, become root there to have
772 * privilege over our namespace. When using lxc-execute we default to
773 * root, but this can be overriden using the lxc.init_uid and
774 * lxc.init_gid configuration options.
775 */
776 if (!lxc_list_empty(&handler->conf->id_map)) {
777 gid_t new_gid = 0;
778 if (handler->conf->is_execute && handler->conf->init_gid)
779 new_gid = handler->conf->init_gid;
780
781 uid_t new_uid = 0;
782 if (handler->conf->is_execute && handler->conf->init_uid)
783 new_uid = handler->conf->init_uid;
784
785 NOTICE("Switching to uid=%d and gid=%d in new user namespace.", new_uid, new_gid);
786 if (setgid(new_gid)) {
787 SYSERROR("Failed to setgid().");
788 goto out_warn_father;
789 }
790 if (setuid(new_uid)) {
791 SYSERROR("Failed to setuid().");
792 goto out_warn_father;
793 }
794 if (setgroups(0, NULL)) {
795 SYSERROR("Failed to setgroups().");
796 goto out_warn_father;
797 }
798 }
799
800 if (access(handler->lxcpath, X_OK)) {
801 print_top_failing_dir(handler->lxcpath);
802 goto out_warn_father;
803 }
804
805 #if HAVE_SYS_CAPABILITY_H
806 if (handler->conf->need_utmp_watch) {
807 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
808 SYSERROR("Failed to remove the CAP_SYS_BOOT capability.");
809 goto out_warn_father;
810 }
811 DEBUG("Dropped the CAP_SYS_BOOT capability.");
812 }
813 #endif
814
815 ret = snprintf(path, sizeof(path), "%s/dev/null", handler->conf->rootfs.mount);
816 if (ret < 0 || ret >= sizeof(path))
817 goto out_warn_father;
818
819 /* In order to checkpoint restore, we need to have everything in the
820 * same mount namespace. However, some containers may not have a
821 * reasonable /dev (in particular, they may not have /dev/null), so we
822 * can't set init's std fds to /dev/null by opening it from inside the
823 * container.
824 *
825 * If that's the case, fall back to using the host's /dev/null. This
826 * means that migration won't work, but at least we won't spew output
827 * where it isn't wanted.
828 */
829 if (handler->backgrounded && !handler->conf->autodev && access(path, F_OK) < 0) {
830 devnull_fd = open_devnull();
831
832 if (devnull_fd < 0)
833 goto out_warn_father;
834 WARN("Using /dev/null from the host for container init's "
835 "standard file descriptors. Migration will not work.");
836 }
837
838 /* Setup the container, ip, names, utsname, ... */
839 if (lxc_setup(handler)) {
840 ERROR("Failed to setup container \"%s\".", handler->name);
841 goto out_warn_father;
842 }
843
844 /* Ask father to setup cgroups and wait for him to finish. */
845 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP))
846 goto out_error;
847
848 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
849 * earlier we end up with a wrong view of /proc/self/cgroup. For
850 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
851 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
852 * /proc/self/cgroup would show us:
853 *
854 * 8:cpuset:/lxc/c
855 *
856 * whereas it should actually show
857 *
858 * 8:cpuset:/
859 */
860 if (cgns_supported()) {
861 if (unshare(CLONE_NEWCGROUP) < 0) {
862 INFO("Failed to unshare CLONE_NEWCGROUP.");
863 goto out_warn_father;
864 }
865 INFO("Unshared CLONE_NEWCGROUP.");
866 }
867
868 /* Set the label to change to when we exec(2) the container's init. */
869 if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0)
870 goto out_warn_father;
871
872 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
873 * before we aren't allowed anymore.
874 */
875 if (handler->conf->no_new_privs) {
876 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
877 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
878 goto out_warn_father;
879 }
880 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
881 }
882
883 /* Some init's such as busybox will set sane tty settings on stdin,
884 * stdout, stderr which it thinks is the console. We already set them
885 * the way we wanted on the real terminal, and we want init to do its
886 * setup on its console ie. the pty allocated in lxc_console_create() so
887 * make sure that that pty is stdin,stdout,stderr.
888 */
889 if (lxc_console_set_stdfds(handler->conf->console.slave) < 0)
890 goto out_warn_father;
891
892 /* If we mounted a temporary proc, then unmount it now. */
893 tmp_proc_unmount(handler->conf);
894
895 if (lxc_seccomp_load(handler->conf) != 0)
896 goto out_warn_father;
897
898 if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) {
899 ERROR("Failed to run lxc.hook.start for container \"%s\".", handler->name);
900 goto out_warn_father;
901 }
902
903 /* The clearenv() and putenv() calls have been moved here to allow us to
904 * use environment variables passed to the various hooks, such as the
905 * start hook above. Not all of the variables like CONFIG_PATH or ROOTFS
906 * are valid in this context but others are.
907 */
908 if (clearenv()) {
909 SYSERROR("Failed to clear environment.");
910 /* Don't error out though. */
911 }
912
913 lxc_list_for_each(iterator, &handler->conf->environment) {
914 if (putenv((char *)iterator->elem)) {
915 SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem);
916 goto out_warn_father;
917 }
918 }
919
920 if (putenv("container=lxc")) {
921 SYSERROR("Failed to set environment variable: container=lxc.");
922 goto out_warn_father;
923 }
924
925 if (handler->conf->pty_names) {
926 if (putenv(handler->conf->pty_names)) {
927 SYSERROR("Failed to set environment variable for container ptys.");
928 goto out_warn_father;
929 }
930 }
931
932 close(handler->sigfd);
933
934 if (devnull_fd < 0) {
935 devnull_fd = open_devnull();
936
937 if (devnull_fd < 0)
938 goto out_warn_father;
939 }
940
941 if (handler->backgrounded && set_stdfds(devnull_fd))
942 goto out_warn_father;
943
944 if (devnull_fd >= 0) {
945 close(devnull_fd);
946 devnull_fd = -1;
947 }
948
949 setsid();
950
951 /* After this call, we are in error because this ops should not return
952 * as it execs.
953 */
954 handler->ops->start(handler, handler->data);
955
956 out_warn_father:
957 /* We want the parent to know something went wrong, so we return a
958 * special error code.
959 */
960 lxc_sync_wake_parent(handler, LXC_SYNC_ERROR);
961
962 out_error:
963 if (devnull_fd >= 0)
964 close(devnull_fd);
965
966 return -1;
967 }
968
969 static int save_phys_nics(struct lxc_conf *conf)
970 {
971 struct lxc_list *iterator;
972 int am_root = (getuid() == 0);
973
974 if (!am_root)
975 return 0;
976
977 lxc_list_for_each(iterator, &conf->network) {
978 struct lxc_netdev *netdev = iterator->elem;
979
980 if (netdev->type != LXC_NET_PHYS)
981 continue;
982 conf->saved_nics = realloc(conf->saved_nics,
983 (conf->num_savednics+1)*sizeof(struct saved_nic));
984 if (!conf->saved_nics)
985 return -1;
986 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
987 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
988 if (!conf->saved_nics[conf->num_savednics].orig_name)
989 return -1;
990 INFO("Stored saved_nic #%d idx %d name %s.", conf->num_savednics,
991 conf->saved_nics[conf->num_savednics].ifindex,
992 conf->saved_nics[conf->num_savednics].orig_name);
993 conf->num_savednics++;
994 }
995
996 return 0;
997 }
998
999 static int recv_fd(int sock, int *fd)
1000 {
1001 if (lxc_abstract_unix_recv_fd(sock, fd, NULL, 0) < 0) {
1002 SYSERROR("Error receiving tty file descriptor from child process.");
1003 return -1;
1004 }
1005 if (*fd == -1)
1006 return -1;
1007 return 0;
1008 }
1009
1010 static int recv_ttys_from_child(struct lxc_handler *handler)
1011 {
1012 struct lxc_conf *conf = handler->conf;
1013 int i, sock = handler->ttysock[1];
1014 struct lxc_tty_info *tty_info = &conf->tty_info;
1015
1016 if (!conf->tty)
1017 return 0;
1018
1019 tty_info->pty_info = malloc(sizeof(*tty_info->pty_info) * conf->tty);
1020 if (!tty_info->pty_info)
1021 return -1;
1022
1023 for (i = 0; i < conf->tty; i++) {
1024 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
1025 pty_info->busy = 0;
1026 if (recv_fd(sock, &pty_info->slave) < 0 ||
1027 recv_fd(sock, &pty_info->master) < 0) {
1028 ERROR("Error receiving tty info from child process.");
1029 return -1;
1030 }
1031 }
1032 tty_info->nbtty = conf->tty;
1033
1034 return 0;
1035 }
1036
1037 void resolve_clone_flags(struct lxc_handler *handler)
1038 {
1039 handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS;
1040
1041 if (!lxc_list_empty(&handler->conf->id_map))
1042 handler->clone_flags |= CLONE_NEWUSER;
1043
1044 if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) {
1045 if (!lxc_requests_empty_network(handler))
1046 handler->clone_flags |= CLONE_NEWNET;
1047 } else {
1048 INFO("Inheriting a NET namespace.");
1049 }
1050
1051 if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1)
1052 handler->clone_flags |= CLONE_NEWIPC;
1053 else
1054 INFO("Inheriting an IPC namespace.");
1055
1056 if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1)
1057 handler->clone_flags |= CLONE_NEWUTS;
1058 else
1059 INFO("Inheriting a UTS namespace.");
1060 }
1061
1062 static int lxc_spawn(struct lxc_handler *handler)
1063 {
1064 int failed_before_rename = 0;
1065 const char *name = handler->name;
1066 bool cgroups_connected = false;
1067 int saved_ns_fd[LXC_NS_MAX];
1068 int preserve_mask = 0, i, flags;
1069 int netpipepair[2], nveths;
1070
1071 netpipe = -1;
1072
1073 for (i = 0; i < LXC_NS_MAX; i++)
1074 if (handler->conf->inherit_ns_fd[i] != -1)
1075 preserve_mask |= ns_info[i].clone_flag;
1076
1077 if (lxc_sync_init(handler))
1078 return -1;
1079
1080 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handler->ttysock) < 0) {
1081 lxc_sync_fini(handler);
1082 return -1;
1083 }
1084
1085 resolve_clone_flags(handler);
1086
1087 if (handler->clone_flags & CLONE_NEWNET) {
1088 if (!lxc_list_empty(&handler->conf->network)) {
1089
1090 /* Find gateway addresses from the link device, which is
1091 * no longer accessible inside the container. Do this
1092 * before creating network interfaces, since goto
1093 * out_delete_net does not work before lxc_clone.
1094 */
1095 if (lxc_find_gateway_addresses(handler)) {
1096 ERROR("Failed to find gateway addresses.");
1097 lxc_sync_fini(handler);
1098 return -1;
1099 }
1100
1101 /* That should be done before the clone because we will
1102 * fill the netdev index and use them in the child.
1103 */
1104 if (lxc_create_network(handler)) {
1105 ERROR("Failed to create the network.");
1106 lxc_sync_fini(handler);
1107 return -1;
1108 }
1109 }
1110
1111 if (save_phys_nics(handler->conf)) {
1112 ERROR("Failed to save physical nic info.");
1113 goto out_abort;
1114 }
1115 }
1116
1117 if (!cgroup_init(handler)) {
1118 ERROR("Failed initializing cgroup support.");
1119 goto out_delete_net;
1120 }
1121
1122 cgroups_connected = true;
1123
1124 if (!cgroup_create(handler)) {
1125 ERROR("Failed creating cgroups.");
1126 goto out_delete_net;
1127 }
1128
1129 /* If the rootfs is not a blockdev, prevent the container from marking
1130 * it readonly.
1131 * If the container is unprivileged then skip rootfs pinning.
1132 */
1133 if (lxc_list_empty(&handler->conf->id_map)) {
1134 handler->pinfd = pin_rootfs(handler->conf->rootfs.path);
1135 if (handler->pinfd == -1)
1136 INFO("Failed to pin the rootfs for container \"%s\".", handler->name);
1137 }
1138
1139 if (!preserve_ns(saved_ns_fd, preserve_mask, getpid()))
1140 goto out_delete_net;
1141
1142 if (attach_ns(handler->conf->inherit_ns_fd) < 0)
1143 goto out_delete_net;
1144
1145 if (am_unpriv() && (nveths = count_veths(&handler->conf->network))) {
1146 if (pipe(netpipepair) < 0) {
1147 SYSERROR("Failed to create pipe.");
1148 goto out_delete_net;
1149 }
1150 /* Store netpipe in the global var for do_start's use. */
1151 netpipe = netpipepair[0];
1152 }
1153
1154 /* Create a process in a new set of namespaces. */
1155 flags = handler->clone_flags;
1156 if (handler->clone_flags & CLONE_NEWUSER) {
1157 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1158 * clone a new user namespace first and only later unshare our
1159 * network namespace to ensure that network devices ownership is
1160 * set up correctly.
1161 */
1162 flags &= ~CLONE_NEWNET;
1163 }
1164 handler->pid = lxc_clone(do_start, handler, flags);
1165 if (handler->pid < 0) {
1166 SYSERROR("Failed to clone a new set of namespaces.");
1167 goto out_delete_net;
1168 }
1169 for (i = 0; i < LXC_NS_MAX; i++)
1170 if (flags & ns_info[i].clone_flag)
1171 INFO("Cloned %s.", ns_info[i].flag_name);
1172
1173 if (!preserve_ns(handler->nsfd, handler->clone_flags | preserve_mask, handler->pid))
1174 INFO("Failed to preserve namespace for lxc.hook.stop.");
1175
1176 if (attach_ns(saved_ns_fd))
1177 WARN("Failed to restore saved namespaces.");
1178
1179 lxc_sync_fini_child(handler);
1180
1181 /* Map the container uids. The container became an invalid userid the
1182 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1183 * anything immediately, but allows the container to setuid(0) (0 being
1184 * mapped to something else on the host.) later to become a valid uid
1185 * again.
1186 */
1187 if (lxc_map_ids(&handler->conf->id_map, handler->pid)) {
1188 ERROR("Failed to set up id mapping.");
1189 goto out_delete_net;
1190 }
1191
1192 if (lxc_sync_wake_child(handler, LXC_SYNC_STARTUP)) {
1193 failed_before_rename = 1;
1194 goto out_delete_net;
1195 }
1196
1197 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) {
1198 failed_before_rename = 1;
1199 goto out_delete_net;
1200 }
1201
1202 if (!cgroup_create_legacy(handler)) {
1203 ERROR("Failed to setup legacy cgroups for container \"%s\".", name);
1204 goto out_delete_net;
1205 }
1206 if (!cgroup_setup_limits(handler, false)) {
1207 ERROR("Failed to setup cgroup limits for container \"%s\".", name);
1208 goto out_delete_net;
1209 }
1210
1211 if (!cgroup_enter(handler))
1212 goto out_delete_net;
1213
1214 if (!cgroup_chown(handler))
1215 goto out_delete_net;
1216
1217 if (failed_before_rename)
1218 goto out_delete_net;
1219
1220 /* Create the network configuration. */
1221 if (handler->clone_flags & CLONE_NEWNET) {
1222 if (lxc_assign_network(handler->lxcpath, handler->name,
1223 &handler->conf->network, handler->pid)) {
1224 ERROR("Failed to create the configured network.");
1225 goto out_delete_net;
1226 }
1227 }
1228
1229 if (netpipe != -1) {
1230 struct lxc_list *iterator;
1231 struct lxc_netdev *netdev;
1232
1233 close(netpipe);
1234 lxc_list_for_each(iterator, &handler->conf->network) {
1235 netdev = iterator->elem;
1236 if (netdev->type != LXC_NET_VETH)
1237 continue;
1238 if (write(netpipepair[1], netdev->name, IFNAMSIZ) != IFNAMSIZ) {
1239 ERROR("Error writing veth name to container.");
1240 goto out_delete_net;
1241 }
1242 }
1243 close(netpipepair[1]);
1244 }
1245
1246 /* Tell the child to continue its initialization. We'll get
1247 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1248 */
1249 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
1250 goto out_delete_net;
1251
1252 if (!cgroup_setup_limits(handler, true)) {
1253 ERROR("Failed to setup the devices cgroup for container \"%s\".", name);
1254 goto out_delete_net;
1255 }
1256
1257 cgroup_disconnect();
1258 cgroups_connected = false;
1259
1260 /* Read tty fds allocated by child. */
1261 if (recv_ttys_from_child(handler) < 0) {
1262 ERROR("Failed to receive tty info from child process.");
1263 goto out_delete_net;
1264 }
1265
1266 /* Tell the child to complete its initialization and wait for it to exec
1267 * or return an error. (The child will never return
1268 * LXC_SYNC_POST_CGROUP+1. It will either close the sync pipe, causing
1269 * lxc_sync_barrier_child to return success, or return a different
1270 * value, causing us to error out).
1271 */
1272 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP))
1273 return -1;
1274
1275 if (detect_shared_rootfs())
1276 umount2(handler->conf->rootfs.mount, MNT_DETACH);
1277
1278 if (handler->ops->post_start(handler, handler->data))
1279 goto out_abort;
1280
1281 if (lxc_set_state(name, handler, RUNNING)) {
1282 ERROR("Failed to set state for container \"%s\" to \"%s\".", name,
1283 lxc_state2str(RUNNING));
1284 goto out_abort;
1285 }
1286
1287 lxc_sync_fini(handler);
1288 handler->netnsfd = lxc_preserve_ns(handler->pid, "net");
1289
1290 return 0;
1291
1292 out_delete_net:
1293 if (cgroups_connected)
1294 cgroup_disconnect();
1295 if (handler->clone_flags & CLONE_NEWNET)
1296 lxc_delete_network(handler);
1297 out_abort:
1298 lxc_abort(name, handler);
1299 lxc_sync_fini(handler);
1300 if (handler->pinfd >= 0) {
1301 close(handler->pinfd);
1302 handler->pinfd = -1;
1303 }
1304
1305 return -1;
1306 }
1307
1308 int __lxc_start(const char *name, struct lxc_conf *conf,
1309 struct lxc_operations* ops, void *data, const char *lxcpath,
1310 bool backgrounded)
1311 {
1312 struct lxc_handler *handler;
1313 int err = -1;
1314 int status;
1315 bool removed_all_netdevs = true;
1316
1317 handler = lxc_init(name, conf, lxcpath);
1318 if (!handler) {
1319 ERROR("Failed to initialize container \"%s\".", name);
1320 return -1;
1321 }
1322 handler->ops = ops;
1323 handler->data = data;
1324 handler->backgrounded = backgrounded;
1325 handler->netnsfd = -1;
1326
1327 if (must_drop_cap_sys_boot(handler->conf)) {
1328 #if HAVE_SYS_CAPABILITY_H
1329 DEBUG("Dropping CAP_SYS_BOOT capability.");
1330 #else
1331 DEBUG("Not dropping CAP_SYS_BOOT capability as capabilities aren't supported.");
1332 #endif
1333 } else {
1334 DEBUG("Not dropping CAP_SYS_BOOT or watching utmp.");
1335 handler->conf->need_utmp_watch = 0;
1336 }
1337
1338 if (!attach_block_device(handler->conf)) {
1339 ERROR("Failed to attach block device.");
1340 goto out_fini_nonet;
1341 }
1342
1343 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
1344 /* If the backing store is a device, mount it here and now. */
1345 if (rootfs_is_blockdev(conf)) {
1346 if (unshare(CLONE_NEWNS) < 0) {
1347 ERROR("Failed to unshare CLONE_NEWNS.");
1348 goto out_fini_nonet;
1349 }
1350 INFO("Unshared CLONE_NEWNS.");
1351
1352 remount_all_slave();
1353 if (do_rootfs_setup(conf, name, lxcpath) < 0) {
1354 ERROR("Error setting up rootfs mount as root before spawn.");
1355 goto out_fini_nonet;
1356 }
1357 INFO("Set up container rootfs as host root.");
1358 }
1359 }
1360
1361 err = lxc_spawn(handler);
1362 if (err) {
1363 ERROR("Failed to spawn container \"%s\".", name);
1364 goto out_detach_blockdev;
1365 }
1366
1367 handler->conf->reboot = 0;
1368
1369 err = lxc_poll(name, handler);
1370 if (err) {
1371 ERROR("LXC mainloop exited with error: %d.", err);
1372 if (handler->netnsfd >= 0) {
1373 close(handler->netnsfd);
1374 handler->netnsfd = -1;
1375 }
1376 goto out_abort;
1377 }
1378
1379 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1380 continue;
1381
1382 /* If the child process exited but was not signaled, it didn't call
1383 * reboot. This should mean it was an lxc-execute which simply exited.
1384 * In any case, treat it as a 'halt'.
1385 */
1386 if (WIFSIGNALED(status)) {
1387 switch(WTERMSIG(status)) {
1388 case SIGINT: /* halt */
1389 DEBUG("Container \"%s\" is halting.", name);
1390 break;
1391 case SIGHUP: /* reboot */
1392 DEBUG("Container \"%s\" is rebooting.", name);
1393 handler->conf->reboot = 1;
1394 break;
1395 case SIGSYS: /* seccomp */
1396 DEBUG("Container \"%s\" violated its seccomp policy.", name);
1397 break;
1398 default:
1399 DEBUG("Unknown exit status for container \"%s\" init %d.", name, WTERMSIG(status));
1400 break;
1401 }
1402 }
1403
1404 DEBUG("Pushing physical nics back to host namespace");
1405 lxc_restore_phys_nics_to_netns(handler->netnsfd, handler->conf);
1406
1407 DEBUG("Tearing down virtual network devices used by container \"%s\".", name);
1408 removed_all_netdevs = lxc_delete_network(handler);
1409
1410 if (handler->pinfd >= 0) {
1411 close(handler->pinfd);
1412 handler->pinfd = -1;
1413 }
1414
1415 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
1416 err = lxc_error_set_and_log(handler->pid, status);
1417 out_fini:
1418 if (!removed_all_netdevs) {
1419 DEBUG("Failed tearing down network devices used by container. Trying again!");
1420 removed_all_netdevs = lxc_delete_network(handler);
1421 if (!removed_all_netdevs)
1422 DEBUG("Failed tearing down network devices used by container. Not trying again!");
1423 }
1424
1425 out_detach_blockdev:
1426 detach_block_device(handler->conf);
1427
1428 out_fini_nonet:
1429 lxc_fini(name, handler);
1430 return err;
1431
1432 out_abort:
1433 lxc_abort(name, handler);
1434 goto out_fini;
1435 }
1436
1437 struct start_args {
1438 char *const *argv;
1439 };
1440
1441 static int start(struct lxc_handler *handler, void* data)
1442 {
1443 struct start_args *arg = data;
1444
1445 NOTICE("Exec'ing \"%s\".", arg->argv[0]);
1446
1447 execvp(arg->argv[0], arg->argv);
1448 SYSERROR("Failed to exec \"%s\".", arg->argv[0]);
1449 return 0;
1450 }
1451
1452 static int post_start(struct lxc_handler *handler, void* data)
1453 {
1454 struct start_args *arg = data;
1455
1456 NOTICE("Started \"%s\" with pid \"%d\".", arg->argv[0], handler->pid);
1457 return 0;
1458 }
1459
1460 static struct lxc_operations start_ops = {
1461 .start = start,
1462 .post_start = post_start
1463 };
1464
1465 int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf,
1466 const char *lxcpath, bool backgrounded)
1467 {
1468 struct start_args start_arg = {
1469 .argv = argv,
1470 };
1471
1472 conf->need_utmp_watch = 1;
1473 return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath, backgrounded);
1474 }
1475
1476 static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
1477 const char *name)
1478 {
1479 char destroy[MAXPATHLEN];
1480 bool bret = true;
1481 int ret = 0;
1482 struct lxc_container *c;
1483 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
1484 bret = do_destroy_container(handler->conf);
1485 if (!bret) {
1486 ERROR("Error destroying rootfs for container \"%s\".", name);
1487 return;
1488 }
1489 }
1490 INFO("Destroyed rootfs for container \"%s\".", name);
1491
1492 ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name);
1493 if (ret < 0 || ret >= MAXPATHLEN) {
1494 ERROR("Error destroying directory for container \"%s\".", name);
1495 return;
1496 }
1497
1498 c = lxc_container_new(name, handler->lxcpath);
1499 if (c) {
1500 if (container_disk_lock(c)) {
1501 INFO("Could not update lxc_snapshots file.");
1502 lxc_container_put(c);
1503 } else {
1504 mod_all_rdeps(c, false);
1505 container_disk_unlock(c);
1506 lxc_container_put(c);
1507 }
1508 }
1509
1510 if (am_unpriv())
1511 ret = userns_exec_1(handler->conf, lxc_rmdir_onedev_wrapper, destroy);
1512 else
1513 ret = lxc_rmdir_onedev(destroy, NULL);
1514
1515 if (ret < 0) {
1516 ERROR("Error destroying directory for container \"%s\".", name);
1517 return;
1518 }
1519 INFO("Destroyed directory for container \"%s\".", name);
1520 }
1521
1522 static int lxc_rmdir_onedev_wrapper(void *data)
1523 {
1524 char *arg = (char *) data;
1525 return lxc_rmdir_onedev(arg, NULL);
1526 }
1527
1528 static bool do_destroy_container(struct lxc_conf *conf) {
1529 if (am_unpriv()) {
1530 if (userns_exec_1(conf, bdev_destroy_wrapper, conf) < 0)
1531 return false;
1532 return true;
1533 }
1534 return bdev_destroy(conf);
1535 }