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