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>
47 #include <sys/syscall.h>
49 #if HAVE_SYS_CAPABILITY_H
50 #include <sys/capability.h>
53 #if !HAVE_DECL_PR_CAPBSET_DROP
54 #define PR_CAPBSET_DROP 24
70 #include "namespace.h"
71 #include "lxcseccomp.h"
76 lxc_log_define(lxc_start
, lxc
);
78 const struct ns_info ns_info
[LXC_NS_MAX
] = {
79 [LXC_NS_MNT
] = {"mnt", CLONE_NEWNS
},
80 [LXC_NS_PID
] = {"pid", CLONE_NEWPID
},
81 [LXC_NS_UTS
] = {"uts", CLONE_NEWUTS
},
82 [LXC_NS_IPC
] = {"ipc", CLONE_NEWIPC
},
83 [LXC_NS_USER
] = {"user", CLONE_NEWUSER
},
84 [LXC_NS_NET
] = {"net", CLONE_NEWNET
}
87 static void close_ns(int ns_fd
[LXC_NS_MAX
]) {
91 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
100 static int preserve_ns(int ns_fd
[LXC_NS_MAX
], int clone_flags
) {
102 char path
[MAXPATHLEN
];
104 if (access("/proc/self/ns", X_OK
)) {
105 ERROR("Does this kernel version support 'attach'?");
109 for (i
= 0; i
< LXC_NS_MAX
; i
++)
112 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
113 if ((clone_flags
& ns_info
[i
].clone_flag
) == 0)
115 snprintf(path
, MAXPATHLEN
, "/proc/self/ns/%s", ns_info
[i
].proc_name
);
117 ns_fd
[i
] = open(path
, O_RDONLY
| O_CLOEXEC
);
129 SYSERROR("failed to open '%s'", path
);
133 static int attach_ns(const int ns_fd
[LXC_NS_MAX
]) {
136 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
140 if (setns(ns_fd
[i
], 0) != 0)
146 SYSERROR("failed to set namespace '%s'", ns_info
[i
].proc_name
);
150 static int match_fd(int fd
)
152 return (fd
== 0 || fd
== 1 || fd
== 2);
155 int lxc_check_inherited(struct lxc_conf
*conf
, int fd_to_ignore
)
157 struct dirent dirent
, *direntp
;
163 dir
= opendir("/proc/self/fd");
166 WARN("failed to open directory: %m");
172 while (!readdir_r(dir
, &dirent
, &direntp
)) {
176 if (!strcmp(direntp
->d_name
, "."))
179 if (!strcmp(direntp
->d_name
, ".."))
182 fd
= atoi(direntp
->d_name
);
184 if (fd
== fddir
|| fd
== lxc_log_fd
|| fd
== fd_to_ignore
)
190 if (conf
->close_all_fds
) {
195 INFO("closed inherited fd %d", fd
);
198 WARN("inherited fd %d", fd
);
202 closedir(dir
); /* cannot fail */
207 static int setup_signal_fd(sigset_t
*oldmask
)
212 /* Block everything except serious error signals */
213 if (sigfillset(&mask
) ||
214 sigdelset(&mask
, SIGILL
) ||
215 sigdelset(&mask
, SIGSEGV
) ||
216 sigdelset(&mask
, SIGBUS
) ||
217 sigdelset(&mask
, SIGWINCH
) ||
218 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
219 SYSERROR("failed to set signal mask");
223 fd
= signalfd(-1, &mask
, 0);
225 SYSERROR("failed to create the signal fd");
229 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
230 SYSERROR("failed to set sigfd to close-on-exec");
235 DEBUG("sigchild handler set");
240 static int signal_handler(int fd
, uint32_t events
, void *data
,
241 struct lxc_epoll_descr
*descr
)
243 struct signalfd_siginfo siginfo
;
247 bool init_died
= false;
249 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
251 ERROR("failed to read signal info");
255 if (ret
!= sizeof(siginfo
)) {
256 ERROR("unexpected siginfo size");
260 // check whether init is running
262 ret
= waitid(P_PID
, *pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
263 if (ret
== 0 && info
.si_pid
== *pid
) {
267 if (siginfo
.ssi_signo
!= SIGCHLD
) {
268 kill(*pid
, siginfo
.ssi_signo
);
269 INFO("forwarded signal %d to pid %d", siginfo
.ssi_signo
, *pid
);
270 return init_died
? 1 : 0;
273 if (siginfo
.ssi_code
== CLD_STOPPED
||
274 siginfo
.ssi_code
== CLD_CONTINUED
) {
275 INFO("container init process was stopped/continued");
276 return init_died
? 1 : 0;
279 /* more robustness, protect ourself from a SIGCHLD sent
280 * by a process different from the container init
282 if (siginfo
.ssi_pid
!= *pid
) {
283 WARN("invalid pid for SIGCHLD");
284 return init_died
? 1 : 0;
287 DEBUG("container init process exited");
291 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
293 handler
->state
= state
;
294 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
298 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
300 int sigfd
= handler
->sigfd
;
301 int pid
= handler
->pid
;
302 struct lxc_epoll_descr descr
;
304 if (lxc_mainloop_open(&descr
)) {
305 ERROR("failed to create mainloop");
309 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
310 ERROR("failed to add handler for the signal");
311 goto out_mainloop_open
;
314 if (lxc_console_mainloop_add(&descr
, handler
)) {
315 ERROR("failed to add console handler to mainloop");
316 goto out_mainloop_open
;
319 if (lxc_cmd_mainloop_add(name
, &descr
, handler
)) {
320 ERROR("failed to add command handler to mainloop");
321 goto out_mainloop_open
;
324 if (handler
->conf
->need_utmp_watch
) {
325 #if HAVE_SYS_CAPABILITY_H
326 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
327 ERROR("failed to add utmp handler to mainloop");
328 goto out_mainloop_open
;
331 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support\n");
335 return lxc_mainloop(&descr
, -1);
338 lxc_mainloop_close(&descr
);
346 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
, const char *lxcpath
)
348 struct lxc_handler
*handler
;
350 handler
= malloc(sizeof(*handler
));
354 memset(handler
, 0, sizeof(*handler
));
356 handler
->conf
= conf
;
357 handler
->lxcpath
= lxcpath
;
362 handler
->name
= strdup(name
);
363 if (!handler
->name
) {
364 ERROR("failed to allocate memory");
368 if (lxc_cmd_init(name
, handler
, lxcpath
))
371 if (lxc_read_seccomp_config(conf
) != 0) {
372 ERROR("failed loading seccomp policy");
373 goto out_close_maincmd_fd
;
376 /* Begin by setting the state to STARTING */
377 if (lxc_set_state(name
, handler
, STARTING
)) {
378 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
379 goto out_close_maincmd_fd
;
382 /* Start of environment variable setup for hooks */
383 if (setenv("LXC_NAME", name
, 1)) {
384 SYSERROR("failed to set environment variable for container name");
386 if (setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1)) {
387 SYSERROR("failed to set environment variable for config path");
389 if (setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1)) {
390 SYSERROR("failed to set environment variable for rootfs mount");
392 if (setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1)) {
393 SYSERROR("failed to set environment variable for rootfs mount");
395 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1)) {
396 SYSERROR("failed to set environment variable for console path");
398 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1)) {
399 SYSERROR("failed to set environment variable for console log");
401 /* End of environment variable setup for hooks */
403 if (run_lxc_hooks(name
, "pre-start", conf
, handler
->lxcpath
, NULL
)) {
404 ERROR("failed to run pre-start hooks for container '%s'.", name
);
408 if (lxc_create_tty(name
, conf
)) {
409 ERROR("failed to create the ttys");
413 /* the signal fd has to be created before forking otherwise
414 * if the child process exits before we setup the signal fd,
415 * the event will be lost and the command will be stuck */
416 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
417 if (handler
->sigfd
< 0) {
418 ERROR("failed to set sigchild fd handler");
422 /* do this after setting up signals since it might unblock SIGWINCH */
423 if (lxc_console_create(conf
)) {
424 ERROR("failed to create console");
425 goto out_restore_sigmask
;
428 if (ttys_shift_ids(conf
) < 0) {
429 ERROR("Failed to shift tty into container");
430 goto out_restore_sigmask
;
433 INFO("'%s' is initialized", name
);
437 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
439 lxc_delete_tty(&conf
->tty_info
);
441 lxc_set_state(name
, handler
, ABORTING
);
442 out_close_maincmd_fd
:
444 close(conf
->maincmd_fd
);
446 conf
->maincmd_fd
= -1;
449 handler
->name
= NULL
;
455 static void lxc_fini(const char *name
, struct lxc_handler
*handler
)
457 /* The STOPPING state is there for future cleanup code
458 * which can take awhile
460 lxc_set_state(name
, handler
, STOPPING
);
461 lxc_set_state(name
, handler
, STOPPED
);
463 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, handler
->lxcpath
, NULL
))
464 ERROR("failed to run post-stop hooks for container '%s'.", name
);
466 /* reset mask set by setup_signal_fd */
467 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
468 WARN("failed to restore sigprocmask");
470 lxc_console_delete(&handler
->conf
->console
);
471 lxc_delete_tty(&handler
->conf
->tty_info
);
473 close(handler
->conf
->maincmd_fd
);
475 handler
->conf
->maincmd_fd
= -1;
477 if (handler
->cgroup
) {
478 lxc_cgroup_process_info_free_and_remove(handler
->cgroup
);
479 handler
->cgroup
= NULL
;
484 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
488 lxc_set_state(name
, handler
, ABORTING
);
489 if (handler
->pid
> 0)
490 kill(handler
->pid
, SIGKILL
);
491 while ((ret
= waitpid(-1, &status
, 0)) > 0) ;
494 #include <sys/reboot.h>
495 #include <linux/reboot.h>
498 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
499 * in a child pid namespace if container reboot support exists.
500 * Otherwise, it will either succeed or return -EPERM.
502 static int container_reboot_supported(void *arg
)
508 if (ret
== -1 && errno
== EINVAL
)
513 static int must_drop_cap_sys_boot(struct lxc_conf
*conf
)
516 int ret
, cmd
, v
, flags
;
517 long stack_size
= 4096;
518 void *stack
= alloca(stack_size
);
523 f
= fopen("/proc/sys/kernel/ctrl-alt-del", "r");
526 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
530 ret
= fscanf(f
, "%d", &v
);
535 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
538 cmd
= v
? LINUX_REBOOT_CMD_CAD_ON
: LINUX_REBOOT_CMD_CAD_OFF
;
540 flags
= CLONE_NEWPID
| SIGCHLD
;
541 if (!lxc_list_empty(&conf
->id_map
))
542 flags
|= CLONE_NEWUSER
;
545 pid
= __clone2(container_reboot_supported
, stack
, stack_size
, flags
, &cmd
);
548 pid
= clone(container_reboot_supported
, stack
, flags
, &cmd
);
551 SYSERROR("failed to clone\n");
554 if (wait(&status
) < 0) {
555 SYSERROR("unexpected wait error: %m\n");
559 if (WEXITSTATUS(status
) != 1)
565 static int do_start(void *data
)
567 struct lxc_handler
*handler
= data
;
568 const char *lsm_label
= NULL
;
570 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
571 SYSERROR("failed to set sigprocmask");
575 /* This prctl must be before the synchro, so if the parent
576 * dies before we set the parent death signal, we will detect
577 * its death with the synchro right after, otherwise we have
578 * a window where the parent can exit before we set the pdeath
579 * signal leading to a unsupervized container.
581 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
582 SYSERROR("failed to set pdeath signal");
586 lxc_sync_fini_parent(handler
);
588 /* don't leak the pinfd to the container */
589 if (handler
->pinfd
>= 0) {
591 close(handler
->pinfd
);
595 /* Tell the parent task it can begin to configure the
596 * container and wait for it to finish
598 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
602 * if we are in a new user namespace, become root there to have
603 * privilege over our namespace
605 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
606 NOTICE("switching to gid/uid 0 in new user namespace");
609 goto out_warn_father
;
613 goto out_warn_father
;
617 #if HAVE_SYS_CAPABILITY_H
618 if (handler
->conf
->need_utmp_watch
) {
619 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
620 SYSERROR("failed to remove CAP_SYS_BOOT capability");
621 goto out_warn_father
;
623 DEBUG("Dropped cap_sys_boot\n");
627 /* Setup the container, ip, names, utsname, ... */
628 if (lxc_setup(handler
->name
, handler
->conf
, handler
->lxcpath
, handler
->cgroup
, handler
->data
) ){
629 ERROR("failed to setup the container");
630 goto out_warn_father
;
633 /* ask father to setup cgroups and wait for him to finish */
634 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
637 /* Set the label to change to when we exec(2) the container's init */
638 if (!strcmp(lsm_name(), "AppArmor"))
639 lsm_label
= handler
->conf
->lsm_aa_profile
;
640 else if (!strcmp(lsm_name(), "SELinux"))
641 lsm_label
= handler
->conf
->lsm_se_context
;
642 if (lsm_process_label_set(lsm_label
, 1, 1) < 0)
643 goto out_warn_father
;
644 lsm_proc_unmount(handler
->conf
);
646 if (lxc_seccomp_load(handler
->conf
) != 0)
647 goto out_warn_father
;
649 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, handler
->lxcpath
, NULL
)) {
650 ERROR("failed to run start hooks for container '%s'.", handler
->name
);
651 goto out_warn_father
;
654 /* The clearenv() and putenv() calls have been moved here
655 * to allow us to use enviroment variables passed to the various
656 * hooks, such as the start hook above. Not all of the
657 * variables like CONFIG_PATH or ROOTFS are valid in this
658 * context but others are. */
660 SYSERROR("failed to clear environment");
661 /* don't error out though */
664 if (putenv("container=lxc")) {
665 SYSERROR("failed to set environment variable");
666 goto out_warn_father
;
670 close(handler
->sigfd
);
673 /* after this call, we are in error because this
674 * ops should not return as it execs */
675 handler
->ops
->start(handler
, handler
->data
);
678 /* we want the parent to know something went wrong, so any
679 * value other than what it expects is ok. */
680 lxc_sync_wake_parent(handler
, LXC_SYNC_POST_CONFIGURE
);
684 int save_phys_nics(struct lxc_conf
*conf
)
686 struct lxc_list
*iterator
;
688 lxc_list_for_each(iterator
, &conf
->network
) {
689 struct lxc_netdev
*netdev
= iterator
->elem
;
691 if (netdev
->type
!= LXC_NET_PHYS
)
693 conf
->saved_nics
= realloc(conf
->saved_nics
,
694 (conf
->num_savednics
+1)*sizeof(struct saved_nic
));
695 if (!conf
->saved_nics
) {
696 SYSERROR("failed to allocate memory");
699 conf
->saved_nics
[conf
->num_savednics
].ifindex
= netdev
->ifindex
;
700 conf
->saved_nics
[conf
->num_savednics
].orig_name
= strdup(netdev
->link
);
701 if (!conf
->saved_nics
[conf
->num_savednics
].orig_name
) {
702 SYSERROR("failed to allocate memory");
705 INFO("stored saved_nic #%d idx %d name %s\n", conf
->num_savednics
,
706 conf
->saved_nics
[conf
->num_savednics
].ifindex
,
707 conf
->saved_nics
[conf
->num_savednics
].orig_name
);
708 conf
->num_savednics
++;
714 int lxc_spawn(struct lxc_handler
*handler
)
716 int failed_before_rename
= 0;
717 const char *name
= handler
->name
;
718 struct cgroup_meta_data
*cgroup_meta
= NULL
;
719 const char *cgroup_pattern
= NULL
;
720 int saved_ns_fd
[LXC_NS_MAX
];
721 int preserve_mask
= 0, i
;
723 for (i
= 0; i
< LXC_NS_MAX
; i
++)
724 if (handler
->conf
->inherit_ns_fd
[i
] != -1)
725 preserve_mask
|= ns_info
[i
].clone_flag
;
727 if (lxc_sync_init(handler
))
730 handler
->clone_flags
= CLONE_NEWPID
|CLONE_NEWNS
;
731 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
732 INFO("Cloning a new user namespace");
733 handler
->clone_flags
|= CLONE_NEWUSER
;
736 if (handler
->conf
->inherit_ns_fd
[LXC_NS_NET
] == -1) {
737 if (!lxc_list_empty(&handler
->conf
->network
)) {
739 handler
->clone_flags
|= CLONE_NEWNET
;
741 /* Find gateway addresses from the link device, which is
742 * no longer accessible inside the container. Do this
743 * before creating network interfaces, since goto
744 * out_delete_net does not work before lxc_clone. */
745 if (lxc_find_gateway_addresses(handler
)) {
746 ERROR("failed to find gateway addresses");
747 lxc_sync_fini(handler
);
751 /* that should be done before the clone because we will
752 * fill the netdev index and use them in the child
754 if (lxc_create_network(handler
)) {
755 ERROR("failed to create the network");
756 lxc_sync_fini(handler
);
761 if (save_phys_nics(handler
->conf
)) {
762 ERROR("failed to save physical nic info");
766 INFO("Inheriting a net namespace");
769 if (handler
->conf
->inherit_ns_fd
[LXC_NS_IPC
] == -1) {
770 handler
->clone_flags
|= CLONE_NEWIPC
;
772 INFO("Inheriting an IPC namespace");
775 if (handler
->conf
->inherit_ns_fd
[LXC_NS_UTS
] == -1) {
776 handler
->clone_flags
|= CLONE_NEWUTS
;
778 INFO("Inheriting a UTS namespace");
782 cgroup_meta
= lxc_cgroup_load_meta();
784 ERROR("failed to detect cgroup metadata");
788 /* if we are running as root, use system cgroup pattern, otherwise
789 * just create a cgroup under the current one. But also fall back to
790 * that if for some reason reading the configuration fails and no
791 * default value is available
794 cgroup_pattern
= default_cgroup_pattern();
796 cgroup_pattern
= "%n";
798 /* Create cgroup before doing clone(), so the child will know from
799 * handler which cgroup it is going to be put in later.
801 if ((handler
->cgroup
= lxc_cgroup_create(name
, cgroup_pattern
, cgroup_meta
, NULL
)) == NULL
) {
802 ERROR("failed to create cgroups for '%s'", name
);
807 * if the rootfs is not a blockdev, prevent the container from
808 * marking it readonly.
811 handler
->pinfd
= pin_rootfs(handler
->conf
->rootfs
.path
);
812 if (handler
->pinfd
== -1)
813 INFO("failed to pin the container's rootfs");
815 preserve_ns(saved_ns_fd
, preserve_mask
);
816 attach_ns(handler
->conf
->inherit_ns_fd
);
818 /* Create a process in a new set of namespaces */
819 handler
->pid
= lxc_clone(do_start
, handler
, handler
->clone_flags
);
820 if (handler
->pid
< 0) {
821 SYSERROR("failed to fork into a new namespace");
825 attach_ns(saved_ns_fd
);
827 lxc_sync_fini_child(handler
);
829 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
830 failed_before_rename
= 1;
832 /* In case there is still legacy ns cgroup support in the kernel.
833 * Should be removed at some later point in time.
835 if (lxc_cgroup_create_legacy(handler
->cgroup
, name
, handler
->pid
) < 0) {
836 ERROR("failed to create legacy ns cgroups for '%s'", name
);
840 if (lxc_setup_cgroup_without_devices(handler
, &handler
->conf
->cgroup
)) {
841 ERROR("failed to setup the cgroups for '%s'", name
);
845 if (lxc_cgroup_enter(handler
->cgroup
, handler
->pid
, false) < 0)
848 if (failed_before_rename
)
851 /* Create the network configuration */
852 if (handler
->clone_flags
& CLONE_NEWNET
) {
853 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
854 ERROR("failed to create the configured network");
859 /* map the container uids - the container became an invalid
860 * userid the moment it was cloned with CLONE_NEWUSER - this
861 * call doesn't change anything immediately, but allows the
862 * container to setuid(0) (0 being mapped to something else on
863 * the host) later to become a valid uid again */
864 if (lxc_map_ids(&handler
->conf
->id_map
, handler
->pid
)) {
865 ERROR("failed to set up id mapping");
869 /* Tell the child to continue its initialization. we'll get
870 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
872 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
875 if (lxc_setup_cgroup_devices(handler
, &handler
->conf
->cgroup
)) {
876 ERROR("failed to setup the devices cgroup for '%s'", name
);
880 /* Tell the child to complete its initialization and wait for
881 * it to exec or return an error. (the child will never
882 * return LXC_SYNC_POST_CGROUP+1. It will either close the
883 * sync pipe, causing lxc_sync_barrier_child to return
884 * success, or return a different value, causing us to error
887 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CGROUP
))
890 if (detect_shared_rootfs())
891 umount2(handler
->conf
->rootfs
.mount
, MNT_DETACH
);
893 if (handler
->ops
->post_start(handler
, handler
->data
))
896 if (lxc_set_state(name
, handler
, RUNNING
)) {
897 ERROR("failed to set state to %s",
898 lxc_state2str(RUNNING
));
902 lxc_cgroup_put_meta(cgroup_meta
);
903 lxc_sync_fini(handler
);
908 if (handler
->clone_flags
& CLONE_NEWNET
)
909 lxc_delete_network(handler
);
911 lxc_cgroup_put_meta(cgroup_meta
);
912 lxc_abort(name
, handler
);
913 lxc_sync_fini(handler
);
914 if (handler
->pinfd
>= 0) {
916 close(handler
->pinfd
);
924 int __lxc_start(const char *name
, struct lxc_conf
*conf
,
925 struct lxc_operations
* ops
, void *data
, const char *lxcpath
)
927 struct lxc_handler
*handler
;
931 handler
= lxc_init(name
, conf
, lxcpath
);
933 ERROR("failed to initialize the container");
937 handler
->data
= data
;
939 if (must_drop_cap_sys_boot(handler
->conf
)) {
940 #if HAVE_SYS_CAPABILITY_H
941 DEBUG("Dropping cap_sys_boot\n");
943 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported\n");
946 DEBUG("Not dropping cap_sys_boot or watching utmp\n");
947 handler
->conf
->need_utmp_watch
= 0;
950 err
= lxc_spawn(handler
);
952 ERROR("failed to spawn '%s'", name
);
956 err
= lxc_poll(name
, handler
);
958 ERROR("mainloop exited with an error");
962 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
966 * If the child process exited but was not signaled,
967 * it didn't call reboot. This should mean it was an
968 * lxc-execute which simply exited. In any case, treat
971 if (WIFSIGNALED(status
)) {
972 switch(WTERMSIG(status
)) {
973 case SIGINT
: /* halt */
974 DEBUG("Container halting");
976 case SIGHUP
: /* reboot */
977 DEBUG("Container rebooting");
978 handler
->conf
->reboot
= 1;
981 DEBUG("unknown exit status for init: %d\n", WTERMSIG(status
));
986 lxc_rename_phys_nics_on_shutdown(handler
->conf
);
988 if (handler
->pinfd
>= 0) {
990 close(handler
->pinfd
);
995 err
= lxc_error_set_and_log(handler
->pid
, status
);
997 lxc_delete_network(handler
);
1000 lxc_fini(name
, handler
);
1004 lxc_abort(name
, handler
);
1012 static int start(struct lxc_handler
*handler
, void* data
)
1014 struct start_args
*arg
= data
;
1016 NOTICE("exec'ing '%s'", arg
->argv
[0]);
1018 execvp(arg
->argv
[0], arg
->argv
);
1019 SYSERROR("failed to exec %s", arg
->argv
[0]);
1023 static int post_start(struct lxc_handler
*handler
, void* data
)
1025 struct start_args
*arg
= data
;
1027 NOTICE("'%s' started with pid '%d'", arg
->argv
[0], handler
->pid
);
1031 static struct lxc_operations start_ops
= {
1033 .post_start
= post_start
1036 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
,
1037 const char *lxcpath
)
1039 struct start_args start_arg
= {
1043 if (lxc_check_inherited(conf
, -1))
1046 conf
->need_utmp_watch
= 1;
1047 return __lxc_start(name
, conf
, &start_ops
, &start_arg
, lxcpath
);