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