2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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
36 #include <sys/param.h>
38 #include <sys/mount.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/prctl.h>
43 #include <sys/types.h>
46 #include <sys/syscall.h>
48 #if HAVE_SYS_CAPABILITY_H
49 #include <sys/capability.h>
52 #if !HAVE_DECL_PR_CAPBSET_DROP
53 #define PR_CAPBSET_DROP 24
69 #include "namespace.h"
70 #include "lxcseccomp.h"
75 lxc_log_define(lxc_start
, lxc
);
77 const struct ns_info ns_info
[LXC_NS_MAX
] = {
78 [LXC_NS_MNT
] = {"mnt", CLONE_NEWNS
},
79 [LXC_NS_PID
] = {"pid", CLONE_NEWPID
},
80 [LXC_NS_UTS
] = {"uts", CLONE_NEWUTS
},
81 [LXC_NS_IPC
] = {"ipc", CLONE_NEWIPC
},
82 [LXC_NS_USER
] = {"user", CLONE_NEWUSER
},
83 [LXC_NS_NET
] = {"net", CLONE_NEWNET
}
86 static void print_top_failing_dir(const char *path
)
88 size_t len
= strlen(path
);
89 char *copy
= alloca(len
+1), *p
, *e
, saved
;
95 while (p
< e
&& *p
== '/') p
++;
96 while (p
< e
&& *p
!= '/') p
++;
99 if (access(copy
, X_OK
)) {
100 SYSERROR("could not access %s. Please grant it 'x' " \
101 "access, or add an ACL for the container root.",
109 static void close_ns(int ns_fd
[LXC_NS_MAX
]) {
112 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
120 static int preserve_ns(int ns_fd
[LXC_NS_MAX
], int clone_flags
) {
122 char path
[MAXPATHLEN
];
124 for (i
= 0; i
< LXC_NS_MAX
; i
++)
127 if (access("/proc/self/ns", X_OK
)) {
128 WARN("Kernel does not support attach; preserve_ns ignored");
132 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
133 if ((clone_flags
& ns_info
[i
].clone_flag
) == 0)
135 snprintf(path
, MAXPATHLEN
, "/proc/self/ns/%s", ns_info
[i
].proc_name
);
136 ns_fd
[i
] = open(path
, O_RDONLY
| O_CLOEXEC
);
147 SYSERROR("failed to open '%s'", path
);
151 static int attach_ns(const int ns_fd
[LXC_NS_MAX
]) {
154 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
158 if (setns(ns_fd
[i
], 0) != 0)
164 SYSERROR("failed to set namespace '%s'", ns_info
[i
].proc_name
);
168 static int match_fd(int fd
)
170 return (fd
== 0 || fd
== 1 || fd
== 2);
174 * Check for any fds we need to close
175 * * if fd_to_ignore != -1, then if we find that fd open we will ignore it.
176 * * By default we warn about open fds we find.
177 * * If closeall is true, we will close open fds.
178 * * If lxc-start was passed "-C", then conf->close_all_fds will be true,
179 * in which case we also close all open fds.
180 * * A daemonized container will always pass closeall=true.
182 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
, int fd_to_ignore
)
184 struct dirent dirent
, *direntp
;
188 if (conf
&& conf
->close_all_fds
)
192 dir
= opendir("/proc/self/fd");
194 WARN("failed to open directory: %m");
200 while (!readdir_r(dir
, &dirent
, &direntp
)) {
204 if (!strcmp(direntp
->d_name
, "."))
207 if (!strcmp(direntp
->d_name
, ".."))
210 fd
= atoi(direntp
->d_name
);
212 if (fd
== fddir
|| fd
== lxc_log_fd
|| fd
== fd_to_ignore
)
221 INFO("closed inherited fd %d", fd
);
224 WARN("inherited fd %d", fd
);
227 closedir(dir
); /* cannot fail */
231 static int setup_signal_fd(sigset_t
*oldmask
)
236 /* Block everything except serious error signals */
237 if (sigfillset(&mask
) ||
238 sigdelset(&mask
, SIGILL
) ||
239 sigdelset(&mask
, SIGSEGV
) ||
240 sigdelset(&mask
, SIGBUS
) ||
241 sigdelset(&mask
, SIGWINCH
) ||
242 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
243 SYSERROR("failed to set signal mask");
247 fd
= signalfd(-1, &mask
, 0);
249 SYSERROR("failed to create the signal fd");
253 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
254 SYSERROR("failed to set sigfd to close-on-exec");
259 DEBUG("sigchild handler set");
264 static int signal_handler(int fd
, uint32_t events
, void *data
,
265 struct lxc_epoll_descr
*descr
)
267 struct signalfd_siginfo siginfo
;
271 bool init_died
= false;
273 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
275 ERROR("failed to read signal info");
279 if (ret
!= sizeof(siginfo
)) {
280 ERROR("unexpected siginfo size");
284 // check whether init is running
286 ret
= waitid(P_PID
, *pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
287 if (ret
== 0 && info
.si_pid
== *pid
) {
291 if (siginfo
.ssi_signo
!= SIGCHLD
) {
292 kill(*pid
, siginfo
.ssi_signo
);
293 INFO("forwarded signal %d to pid %d", siginfo
.ssi_signo
, *pid
);
294 return init_died
? 1 : 0;
297 if (siginfo
.ssi_code
== CLD_STOPPED
||
298 siginfo
.ssi_code
== CLD_CONTINUED
) {
299 INFO("container init process was stopped/continued");
300 return init_died
? 1 : 0;
303 /* more robustness, protect ourself from a SIGCHLD sent
304 * by a process different from the container init
306 if (siginfo
.ssi_pid
!= *pid
) {
307 WARN("invalid pid for SIGCHLD");
308 return init_died
? 1 : 0;
311 DEBUG("container init process exited");
315 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
317 handler
->state
= state
;
318 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
322 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
324 int sigfd
= handler
->sigfd
;
325 int pid
= handler
->pid
;
326 struct lxc_epoll_descr descr
;
328 if (lxc_mainloop_open(&descr
)) {
329 ERROR("failed to create mainloop");
333 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
334 ERROR("failed to add handler for the signal");
335 goto out_mainloop_open
;
338 if (lxc_console_mainloop_add(&descr
, handler
)) {
339 ERROR("failed to add console handler to mainloop");
340 goto out_mainloop_open
;
343 if (lxc_cmd_mainloop_add(name
, &descr
, handler
)) {
344 ERROR("failed to add command handler to mainloop");
345 goto out_mainloop_open
;
348 if (handler
->conf
->need_utmp_watch
) {
349 #if HAVE_SYS_CAPABILITY_H
350 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
351 ERROR("failed to add utmp handler to mainloop");
352 goto out_mainloop_open
;
355 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support");
359 return lxc_mainloop(&descr
, -1);
362 lxc_mainloop_close(&descr
);
368 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
, const char *lxcpath
)
370 struct lxc_handler
*handler
;
372 handler
= malloc(sizeof(*handler
));
376 memset(handler
, 0, sizeof(*handler
));
378 handler
->conf
= conf
;
379 handler
->lxcpath
= lxcpath
;
384 handler
->name
= strdup(name
);
385 if (!handler
->name
) {
386 ERROR("failed to allocate memory");
390 if (lxc_cmd_init(name
, handler
, lxcpath
))
393 if (lxc_read_seccomp_config(conf
) != 0) {
394 ERROR("failed loading seccomp policy");
395 goto out_close_maincmd_fd
;
398 /* Begin by setting the state to STARTING */
399 if (lxc_set_state(name
, handler
, STARTING
)) {
400 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
401 goto out_close_maincmd_fd
;
404 /* Start of environment variable setup for hooks */
405 if (setenv("LXC_NAME", name
, 1)) {
406 SYSERROR("failed to set environment variable for container name");
408 if (setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1)) {
409 SYSERROR("failed to set environment variable for config path");
411 if (setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1)) {
412 SYSERROR("failed to set environment variable for rootfs mount");
414 if (setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1)) {
415 SYSERROR("failed to set environment variable for rootfs mount");
417 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1)) {
418 SYSERROR("failed to set environment variable for console path");
420 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1)) {
421 SYSERROR("failed to set environment variable for console log");
423 /* End of environment variable setup for hooks */
425 if (run_lxc_hooks(name
, "pre-start", conf
, handler
->lxcpath
, NULL
)) {
426 ERROR("failed to run pre-start hooks for container '%s'.", name
);
430 if (lxc_create_tty(name
, conf
)) {
431 ERROR("failed to create the ttys");
435 /* the signal fd has to be created before forking otherwise
436 * if the child process exits before we setup the signal fd,
437 * the event will be lost and the command will be stuck */
438 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
439 if (handler
->sigfd
< 0) {
440 ERROR("failed to set sigchild fd handler");
444 /* do this after setting up signals since it might unblock SIGWINCH */
445 if (lxc_console_create(conf
)) {
446 ERROR("failed to create console");
447 goto out_restore_sigmask
;
450 if (ttys_shift_ids(conf
) < 0) {
451 ERROR("Failed to shift tty into container");
452 goto out_restore_sigmask
;
455 INFO("'%s' is initialized", name
);
459 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
461 lxc_delete_tty(&conf
->tty_info
);
463 lxc_set_state(name
, handler
, ABORTING
);
464 out_close_maincmd_fd
:
465 close(conf
->maincmd_fd
);
466 conf
->maincmd_fd
= -1;
469 handler
->name
= NULL
;
475 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
477 /* The STOPPING state is there for future cleanup code
478 * which can take awhile
480 lxc_set_state(name
, handler
, STOPPING
);
481 lxc_set_state(name
, handler
, STOPPED
);
483 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, handler
->lxcpath
, NULL
))
484 ERROR("failed to run post-stop hooks for container '%s'.", name
);
486 /* reset mask set by setup_signal_fd */
487 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
488 WARN("failed to restore sigprocmask");
490 lxc_console_delete(&handler
->conf
->console
);
491 lxc_delete_tty(&handler
->conf
->tty_info
);
492 lxc_delete_autodev(handler
);
493 close(handler
->conf
->maincmd_fd
);
494 handler
->conf
->maincmd_fd
= -1;
496 cgroup_destroy(handler
);
500 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
504 lxc_set_state(name
, handler
, ABORTING
);
505 if (handler
->pid
> 0)
506 kill(handler
->pid
, SIGKILL
);
507 while ((ret
= waitpid(-1, &status
, 0)) > 0) ;
510 #include <sys/reboot.h>
511 #include <linux/reboot.h>
514 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
515 * in a child pid namespace if container reboot support exists.
516 * Otherwise, it will either succeed or return -EPERM.
518 static int container_reboot_supported(void *arg
)
524 if (ret
== -1 && errno
== EINVAL
)
529 static int must_drop_cap_sys_boot(struct lxc_conf
*conf
)
532 int ret
, cmd
, v
, flags
;
533 long stack_size
= 4096;
534 void *stack
= alloca(stack_size
);
538 f
= fopen("/proc/sys/kernel/ctrl-alt-del", "r");
540 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
544 ret
= fscanf(f
, "%d", &v
);
547 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
550 cmd
= v
? LINUX_REBOOT_CMD_CAD_ON
: LINUX_REBOOT_CMD_CAD_OFF
;
552 flags
= CLONE_NEWPID
| SIGCHLD
;
553 if (!lxc_list_empty(&conf
->id_map
))
554 flags
|= CLONE_NEWUSER
;
557 pid
= __clone2(container_reboot_supported
, stack
, stack_size
, flags
, &cmd
);
560 pid
= clone(container_reboot_supported
, stack
, flags
, &cmd
);
563 if (flags
& CLONE_NEWUSER
)
564 ERROR("failed to clone (%#x): %s (includes CLONE_NEWUSER)", flags
, strerror(errno
));
566 ERROR("failed to clone (%#x): %s", flags
, strerror(errno
));
569 if (wait(&status
) < 0) {
570 SYSERROR("unexpected wait error: %m");
574 if (WEXITSTATUS(status
) != 1)
581 * netpipe is used in the unprivileged case to transfer the ifindexes
582 * from parent to child
584 static int netpipe
= -1;
586 static inline int count_veths(struct lxc_list
*network
)
588 struct lxc_list
*iterator
;
589 struct lxc_netdev
*netdev
;
592 lxc_list_for_each(iterator
, network
) {
593 netdev
= iterator
->elem
;
594 if (netdev
->type
!= LXC_NET_VETH
)
601 static int read_unpriv_netifindex(struct lxc_list
*network
)
603 struct lxc_list
*iterator
;
604 struct lxc_netdev
*netdev
;
608 lxc_list_for_each(iterator
, network
) {
609 netdev
= iterator
->elem
;
610 if (netdev
->type
!= LXC_NET_VETH
)
612 if (!(netdev
->name
= malloc(IFNAMSIZ
))) {
613 ERROR("Out of memory");
617 if (read(netpipe
, netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
626 static int do_start(void *data
)
628 struct lxc_list
*iterator
;
629 struct lxc_handler
*handler
= data
;
631 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
632 SYSERROR("failed to set sigprocmask");
636 /* This prctl must be before the synchro, so if the parent
637 * dies before we set the parent death signal, we will detect
638 * its death with the synchro right after, otherwise we have
639 * a window where the parent can exit before we set the pdeath
640 * signal leading to a unsupervized container.
642 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
643 SYSERROR("failed to set pdeath signal");
647 lxc_sync_fini_parent(handler
);
649 /* don't leak the pinfd to the container */
650 if (handler
->pinfd
>= 0) {
651 close(handler
->pinfd
);
654 /* Tell the parent task it can begin to configure the
655 * container and wait for it to finish
657 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
660 if (read_unpriv_netifindex(&handler
->conf
->network
) < 0)
661 goto out_warn_father
;
664 * if we are in a new user namespace, become root there to have
665 * privilege over our namespace
667 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
668 NOTICE("switching to gid/uid 0 in new user namespace");
671 goto out_warn_father
;
675 goto out_warn_father
;
677 if (setgroups(0, NULL
)) {
678 SYSERROR("setgroups");
679 goto out_warn_father
;
683 if (access(handler
->lxcpath
, X_OK
)) {
684 print_top_failing_dir(handler
->lxcpath
);
685 goto out_warn_father
;
688 #if HAVE_SYS_CAPABILITY_H
689 if (handler
->conf
->need_utmp_watch
) {
690 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
691 SYSERROR("failed to remove CAP_SYS_BOOT capability");
692 goto out_warn_father
;
694 DEBUG("Dropped cap_sys_boot");
698 /* Setup the container, ip, names, utsname, ... */
699 if (lxc_setup(handler
)) {
700 ERROR("failed to setup the container");
701 goto out_warn_father
;
704 /* ask father to setup cgroups and wait for him to finish */
705 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
708 /* Set the label to change to when we exec(2) the container's init */
709 if (lsm_process_label_set(NULL
, handler
->conf
, 1, 1) < 0)
710 goto out_warn_father
;
712 /* Some init's such as busybox will set sane tty settings on stdin,
713 * stdout, stderr which it thinks is the console. We already set them
714 * the way we wanted on the real terminal, and we want init to do its
715 * setup on its console ie. the pty allocated in lxc_console_create()
716 * so make sure that that pty is stdin,stdout,stderr.
718 if (lxc_console_set_stdfds(handler
) < 0)
719 goto out_warn_father
;
721 /* If we mounted a temporary proc, then unmount it now */
722 tmp_proc_unmount(handler
->conf
);
724 if (lxc_seccomp_load(handler
->conf
) != 0)
725 goto out_warn_father
;
727 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, handler
->lxcpath
, NULL
)) {
728 ERROR("failed to run start hooks for container '%s'.", handler
->name
);
729 goto out_warn_father
;
732 /* The clearenv() and putenv() calls have been moved here
733 * to allow us to use environment variables passed to the various
734 * hooks, such as the start hook above. Not all of the
735 * variables like CONFIG_PATH or ROOTFS are valid in this
736 * context but others are. */
738 SYSERROR("failed to clear environment");
739 /* don't error out though */
742 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
743 if (putenv((char *)iterator
->elem
)) {
744 SYSERROR("failed to set environment variable '%s'", (char *)iterator
->elem
);
745 goto out_warn_father
;
749 if (putenv("container=lxc")) {
750 SYSERROR("failed to set environment variable 'container=lxc'");
751 goto out_warn_father
;
754 close(handler
->sigfd
);
756 /* after this call, we are in error because this
757 * ops should not return as it execs */
758 handler
->ops
->start(handler
, handler
->data
);
761 /* we want the parent to know something went wrong, so any
762 * value other than what it expects is ok. */
763 lxc_sync_wake_parent(handler
, LXC_SYNC_POST_CONFIGURE
);
767 static int save_phys_nics(struct lxc_conf
*conf
)
769 struct lxc_list
*iterator
;
771 lxc_list_for_each(iterator
, &conf
->network
) {
772 struct lxc_netdev
*netdev
= iterator
->elem
;
774 if (netdev
->type
!= LXC_NET_PHYS
)
776 conf
->saved_nics
= realloc(conf
->saved_nics
,
777 (conf
->num_savednics
+1)*sizeof(struct saved_nic
));
778 if (!conf
->saved_nics
) {
779 SYSERROR("failed to allocate memory");
782 conf
->saved_nics
[conf
->num_savednics
].ifindex
= netdev
->ifindex
;
783 conf
->saved_nics
[conf
->num_savednics
].orig_name
= strdup(netdev
->link
);
784 if (!conf
->saved_nics
[conf
->num_savednics
].orig_name
) {
785 SYSERROR("failed to allocate memory");
788 INFO("stored saved_nic #%d idx %d name %s", conf
->num_savednics
,
789 conf
->saved_nics
[conf
->num_savednics
].ifindex
,
790 conf
->saved_nics
[conf
->num_savednics
].orig_name
);
791 conf
->num_savednics
++;
797 static int lxc_spawn(struct lxc_handler
*handler
)
799 int failed_before_rename
= 0;
800 const char *name
= handler
->name
;
801 bool cgroups_connected
= false;
802 int saved_ns_fd
[LXC_NS_MAX
];
803 int preserve_mask
= 0, i
;
804 int netpipepair
[2], nveths
;
808 for (i
= 0; i
< LXC_NS_MAX
; i
++)
809 if (handler
->conf
->inherit_ns_fd
[i
] != -1)
810 preserve_mask
|= ns_info
[i
].clone_flag
;
812 if (lxc_sync_init(handler
))
815 handler
->clone_flags
= CLONE_NEWPID
|CLONE_NEWNS
;
816 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
817 INFO("Cloning a new user namespace");
818 handler
->clone_flags
|= CLONE_NEWUSER
;
821 if (handler
->conf
->inherit_ns_fd
[LXC_NS_NET
] == -1) {
822 if (!lxc_requests_empty_network(handler
))
823 handler
->clone_flags
|= CLONE_NEWNET
;
825 if (!lxc_list_empty(&handler
->conf
->network
)) {
827 /* Find gateway addresses from the link device, which is
828 * no longer accessible inside the container. Do this
829 * before creating network interfaces, since goto
830 * out_delete_net does not work before lxc_clone. */
831 if (lxc_find_gateway_addresses(handler
)) {
832 ERROR("failed to find gateway addresses");
833 lxc_sync_fini(handler
);
837 /* that should be done before the clone because we will
838 * fill the netdev index and use them in the child
840 if (lxc_create_network(handler
)) {
841 ERROR("failed to create the network");
842 lxc_sync_fini(handler
);
847 if (save_phys_nics(handler
->conf
)) {
848 ERROR("failed to save physical nic info");
852 INFO("Inheriting a net namespace");
855 if (handler
->conf
->inherit_ns_fd
[LXC_NS_IPC
] == -1) {
856 handler
->clone_flags
|= CLONE_NEWIPC
;
858 INFO("Inheriting an IPC namespace");
861 if (handler
->conf
->inherit_ns_fd
[LXC_NS_UTS
] == -1) {
862 handler
->clone_flags
|= CLONE_NEWUTS
;
864 INFO("Inheriting a UTS namespace");
868 if (!cgroup_init(handler
)) {
869 ERROR("failed initializing cgroup support");
873 cgroups_connected
= true;
875 if (!cgroup_create(handler
)) {
876 ERROR("failed creating cgroups");
881 * if the rootfs is not a blockdev, prevent the container from
882 * marking it readonly.
884 * if the container is unprivileged then skip rootfs pinning
886 if (lxc_list_empty(&handler
->conf
->id_map
)) {
887 handler
->pinfd
= pin_rootfs(handler
->conf
->rootfs
.path
);
888 if (handler
->pinfd
== -1)
889 INFO("failed to pin the container's rootfs");
892 if (preserve_ns(saved_ns_fd
, preserve_mask
) < 0)
894 if (attach_ns(handler
->conf
->inherit_ns_fd
) < 0)
897 if (am_unpriv() && (nveths
= count_veths(&handler
->conf
->network
))) {
898 if (pipe(netpipepair
) < 0) {
899 SYSERROR("Error creating pipe");
902 /* store netpipe in the global var for do_start's use */
903 netpipe
= netpipepair
[0];
906 /* Create a process in a new set of namespaces */
907 handler
->pid
= lxc_clone(do_start
, handler
, handler
->clone_flags
);
908 if (handler
->pid
< 0) {
909 SYSERROR("failed to fork into a new namespace");
913 if (attach_ns(saved_ns_fd
))
914 WARN("failed to restore saved namespaces");
916 lxc_sync_fini_child(handler
);
918 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
919 failed_before_rename
= 1;
921 if (!cgroup_create_legacy(handler
)) {
922 ERROR("failed to setup the legacy cgroups for %s", name
);
925 if (!cgroup_setup_limits(handler
, false)) {
926 ERROR("failed to setup the cgroup limits for '%s'", name
);
930 if (!cgroup_enter(handler
))
933 if (!cgroup_chown(handler
))
936 if (failed_before_rename
)
939 /* Create the network configuration */
940 if (handler
->clone_flags
& CLONE_NEWNET
) {
941 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
942 ERROR("failed to create the configured network");
948 struct lxc_list
*iterator
;
949 struct lxc_netdev
*netdev
;
952 lxc_list_for_each(iterator
, &handler
->conf
->network
) {
953 netdev
= iterator
->elem
;
954 if (netdev
->type
!= LXC_NET_VETH
)
956 if (write(netpipepair
[1], netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
957 ERROR("Error writing veth name to container");
961 close(netpipepair
[1]);
964 /* map the container uids - the container became an invalid
965 * userid the moment it was cloned with CLONE_NEWUSER - this
966 * call doesn't change anything immediately, but allows the
967 * container to setuid(0) (0 being mapped to something else on
968 * the host) later to become a valid uid again */
969 if (lxc_map_ids(&handler
->conf
->id_map
, handler
->pid
)) {
970 ERROR("failed to set up id mapping");
974 /* Tell the child to continue its initialization. we'll get
975 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
977 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
980 if (!cgroup_setup_limits(handler
, true)) {
981 ERROR("failed to setup the devices cgroup for '%s'", name
);
986 cgroups_connected
= false;
988 /* Tell the child to complete its initialization and wait for
989 * it to exec or return an error. (the child will never
990 * return LXC_SYNC_POST_CGROUP+1. It will either close the
991 * sync pipe, causing lxc_sync_barrier_child to return
992 * success, or return a different value, causing us to error
995 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CGROUP
))
998 if (detect_shared_rootfs())
999 umount2(handler
->conf
->rootfs
.mount
, MNT_DETACH
);
1001 if (handler
->ops
->post_start(handler
, handler
->data
))
1004 if (lxc_set_state(name
, handler
, RUNNING
)) {
1005 ERROR("failed to set state to %s",
1006 lxc_state2str(RUNNING
));
1010 lxc_sync_fini(handler
);
1015 if (cgroups_connected
)
1016 cgroup_disconnect();
1017 if (handler
->clone_flags
& CLONE_NEWNET
)
1018 lxc_delete_network(handler
);
1020 lxc_abort(name
, handler
);
1021 lxc_sync_fini(handler
);
1022 if (handler
->pinfd
>= 0) {
1023 close(handler
->pinfd
);
1024 handler
->pinfd
= -1;
1030 int get_netns_fd(int pid
)
1032 char path
[MAXPATHLEN
];
1035 ret
= snprintf(path
, MAXPATHLEN
, "/proc/%d/ns/net", pid
);
1036 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1037 WARN("Failed to pin netns file for pid %d", pid
);
1041 fd
= open(path
, O_RDONLY
);
1043 WARN("Failed to pin netns file %s for pid %d: %s",
1044 path
, pid
, strerror(errno
));
1050 int __lxc_start(const char *name
, struct lxc_conf
*conf
,
1051 struct lxc_operations
* ops
, void *data
, const char *lxcpath
)
1053 struct lxc_handler
*handler
;
1058 handler
= lxc_init(name
, conf
, lxcpath
);
1060 ERROR("failed to initialize the container");
1064 handler
->data
= data
;
1066 if (must_drop_cap_sys_boot(handler
->conf
)) {
1067 #if HAVE_SYS_CAPABILITY_H
1068 DEBUG("Dropping cap_sys_boot");
1070 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported");
1073 DEBUG("Not dropping cap_sys_boot or watching utmp");
1074 handler
->conf
->need_utmp_watch
= 0;
1077 if (!attach_block_device(handler
->conf
)) {
1078 ERROR("Failure attaching block device");
1079 goto out_fini_nonet
;
1082 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1083 /* if the backing store is a device, mount it here and now */
1084 if (rootfs_is_blockdev(conf
)) {
1085 if (unshare(CLONE_NEWNS
) < 0) {
1086 ERROR("Error unsharing mounts");
1087 goto out_fini_nonet
;
1089 remount_all_slave();
1090 if (do_rootfs_setup(conf
, name
, lxcpath
) < 0) {
1091 ERROR("Error setting up rootfs mount as root before spawn");
1092 goto out_fini_nonet
;
1094 INFO("Set up container rootfs as host root");
1098 err
= lxc_spawn(handler
);
1100 ERROR("failed to spawn '%s'", name
);
1101 goto out_detach_blockdev
;
1104 netnsfd
= get_netns_fd(handler
->pid
);
1106 err
= lxc_poll(name
, handler
);
1108 ERROR("mainloop exited with an error");
1114 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
1118 * If the child process exited but was not signaled,
1119 * it didn't call reboot. This should mean it was an
1120 * lxc-execute which simply exited. In any case, treat
1123 if (WIFSIGNALED(status
)) {
1124 switch(WTERMSIG(status
)) {
1125 case SIGINT
: /* halt */
1126 DEBUG("Container halting");
1128 case SIGHUP
: /* reboot */
1129 DEBUG("Container rebooting");
1130 handler
->conf
->reboot
= 1;
1132 case SIGSYS
: /* seccomp */
1133 DEBUG("Container violated its seccomp policy");
1136 DEBUG("unknown exit status for init: %d", WTERMSIG(status
));
1141 lxc_rename_phys_nics_on_shutdown(netnsfd
, handler
->conf
);
1145 if (handler
->pinfd
>= 0) {
1146 close(handler
->pinfd
);
1147 handler
->pinfd
= -1;
1150 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1151 err
= lxc_error_set_and_log(handler
->pid
, status
);
1153 lxc_delete_network(handler
);
1155 out_detach_blockdev
:
1156 detach_block_device(handler
->conf
);
1159 lxc_fini(name
, handler
);
1163 lxc_abort(name
, handler
);
1171 static int start(struct lxc_handler
*handler
, void* data
)
1173 struct start_args
*arg
= data
;
1175 NOTICE("exec'ing '%s'", arg
->argv
[0]);
1177 execvp(arg
->argv
[0], arg
->argv
);
1178 SYSERROR("failed to exec %s", arg
->argv
[0]);
1182 static int post_start(struct lxc_handler
*handler
, void* data
)
1184 struct start_args
*arg
= data
;
1186 NOTICE("'%s' started with pid '%d'", arg
->argv
[0], handler
->pid
);
1190 static struct lxc_operations start_ops
= {
1192 .post_start
= post_start
1195 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
,
1196 const char *lxcpath
)
1198 struct start_args start_arg
= {
1202 conf
->need_utmp_watch
= 1;
1203 return __lxc_start(name
, conf
, &start_ops
, &start_arg
, lxcpath
);