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"
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 close_ns(int ns_fd
[LXC_NS_MAX
]) {
89 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
97 static int preserve_ns(int ns_fd
[LXC_NS_MAX
], int clone_flags
) {
99 char path
[MAXPATHLEN
];
101 if (access("/proc/self/ns", X_OK
)) {
102 ERROR("Does this kernel version support 'attach'?");
106 for (i
= 0; i
< LXC_NS_MAX
; i
++)
109 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
110 if ((clone_flags
& ns_info
[i
].clone_flag
) == 0)
112 snprintf(path
, MAXPATHLEN
, "/proc/self/ns/%s", ns_info
[i
].proc_name
);
113 ns_fd
[i
] = open(path
, O_RDONLY
| O_CLOEXEC
);
124 SYSERROR("failed to open '%s'", path
);
128 static int attach_ns(const int ns_fd
[LXC_NS_MAX
]) {
131 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
135 if (setns(ns_fd
[i
], 0) != 0)
141 SYSERROR("failed to set namespace '%s'", ns_info
[i
].proc_name
);
145 static int match_fd(int fd
)
147 return (fd
== 0 || fd
== 1 || fd
== 2);
150 int lxc_check_inherited(struct lxc_conf
*conf
, int fd_to_ignore
)
152 struct dirent dirent
, *direntp
;
157 dir
= opendir("/proc/self/fd");
159 WARN("failed to open directory: %m");
165 while (!readdir_r(dir
, &dirent
, &direntp
)) {
169 if (!strcmp(direntp
->d_name
, "."))
172 if (!strcmp(direntp
->d_name
, ".."))
175 fd
= atoi(direntp
->d_name
);
177 if (fd
== fddir
|| fd
== lxc_log_fd
|| fd
== fd_to_ignore
)
183 if (conf
->close_all_fds
) {
186 INFO("closed inherited fd %d", fd
);
189 WARN("inherited fd %d", fd
);
192 closedir(dir
); /* cannot fail */
196 static int setup_signal_fd(sigset_t
*oldmask
)
201 /* Block everything except serious error signals */
202 if (sigfillset(&mask
) ||
203 sigdelset(&mask
, SIGILL
) ||
204 sigdelset(&mask
, SIGSEGV
) ||
205 sigdelset(&mask
, SIGBUS
) ||
206 sigdelset(&mask
, SIGWINCH
) ||
207 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
208 SYSERROR("failed to set signal mask");
212 fd
= signalfd(-1, &mask
, 0);
214 SYSERROR("failed to create the signal fd");
218 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
219 SYSERROR("failed to set sigfd to close-on-exec");
224 DEBUG("sigchild handler set");
229 static int signal_handler(int fd
, uint32_t events
, void *data
,
230 struct lxc_epoll_descr
*descr
)
232 struct signalfd_siginfo siginfo
;
236 bool init_died
= false;
238 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
240 ERROR("failed to read signal info");
244 if (ret
!= sizeof(siginfo
)) {
245 ERROR("unexpected siginfo size");
249 // check whether init is running
251 ret
= waitid(P_PID
, *pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
252 if (ret
== 0 && info
.si_pid
== *pid
) {
256 if (siginfo
.ssi_signo
!= SIGCHLD
) {
257 kill(*pid
, siginfo
.ssi_signo
);
258 INFO("forwarded signal %d to pid %d", siginfo
.ssi_signo
, *pid
);
259 return init_died
? 1 : 0;
262 if (siginfo
.ssi_code
== CLD_STOPPED
||
263 siginfo
.ssi_code
== CLD_CONTINUED
) {
264 INFO("container init process was stopped/continued");
265 return init_died
? 1 : 0;
268 /* more robustness, protect ourself from a SIGCHLD sent
269 * by a process different from the container init
271 if (siginfo
.ssi_pid
!= *pid
) {
272 WARN("invalid pid for SIGCHLD");
273 return init_died
? 1 : 0;
276 DEBUG("container init process exited");
280 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
282 handler
->state
= state
;
283 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
287 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
289 int sigfd
= handler
->sigfd
;
290 int pid
= handler
->pid
;
291 struct lxc_epoll_descr descr
;
293 if (lxc_mainloop_open(&descr
)) {
294 ERROR("failed to create mainloop");
298 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
299 ERROR("failed to add handler for the signal");
300 goto out_mainloop_open
;
303 if (lxc_console_mainloop_add(&descr
, handler
)) {
304 ERROR("failed to add console handler to mainloop");
305 goto out_mainloop_open
;
308 if (lxc_cmd_mainloop_add(name
, &descr
, handler
)) {
309 ERROR("failed to add command handler to mainloop");
310 goto out_mainloop_open
;
313 if (handler
->conf
->need_utmp_watch
) {
314 #if HAVE_SYS_CAPABILITY_H
315 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
316 ERROR("failed to add utmp handler to mainloop");
317 goto out_mainloop_open
;
320 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support\n");
324 return lxc_mainloop(&descr
, -1);
327 lxc_mainloop_close(&descr
);
333 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
, const char *lxcpath
)
335 struct lxc_handler
*handler
;
337 handler
= malloc(sizeof(*handler
));
341 memset(handler
, 0, sizeof(*handler
));
343 handler
->conf
= conf
;
344 handler
->lxcpath
= lxcpath
;
349 handler
->name
= strdup(name
);
350 if (!handler
->name
) {
351 ERROR("failed to allocate memory");
355 if (lxc_cmd_init(name
, handler
, lxcpath
))
358 if (lxc_read_seccomp_config(conf
) != 0) {
359 ERROR("failed loading seccomp policy");
360 goto out_close_maincmd_fd
;
363 /* Begin by setting the state to STARTING */
364 if (lxc_set_state(name
, handler
, STARTING
)) {
365 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
366 goto out_close_maincmd_fd
;
369 /* Start of environment variable setup for hooks */
370 if (setenv("LXC_NAME", name
, 1)) {
371 SYSERROR("failed to set environment variable for container name");
373 if (setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1)) {
374 SYSERROR("failed to set environment variable for config path");
376 if (setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1)) {
377 SYSERROR("failed to set environment variable for rootfs mount");
379 if (setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1)) {
380 SYSERROR("failed to set environment variable for rootfs mount");
382 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1)) {
383 SYSERROR("failed to set environment variable for console path");
385 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1)) {
386 SYSERROR("failed to set environment variable for console log");
388 /* End of environment variable setup for hooks */
390 if (run_lxc_hooks(name
, "pre-start", conf
, handler
->lxcpath
, NULL
)) {
391 ERROR("failed to run pre-start hooks for container '%s'.", name
);
395 if (lxc_create_tty(name
, conf
)) {
396 ERROR("failed to create the ttys");
400 /* the signal fd has to be created before forking otherwise
401 * if the child process exits before we setup the signal fd,
402 * the event will be lost and the command will be stuck */
403 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
404 if (handler
->sigfd
< 0) {
405 ERROR("failed to set sigchild fd handler");
409 /* do this after setting up signals since it might unblock SIGWINCH */
410 if (lxc_console_create(conf
)) {
411 ERROR("failed to create console");
412 goto out_restore_sigmask
;
415 if (ttys_shift_ids(conf
) < 0) {
416 ERROR("Failed to shift tty into container");
417 goto out_restore_sigmask
;
420 INFO("'%s' is initialized", name
);
424 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
426 lxc_delete_tty(&conf
->tty_info
);
428 lxc_set_state(name
, handler
, ABORTING
);
429 out_close_maincmd_fd
:
430 close(conf
->maincmd_fd
);
431 conf
->maincmd_fd
= -1;
434 handler
->name
= NULL
;
440 static void lxc_fini(const char *name
, struct lxc_handler
*handler
)
442 /* The STOPPING state is there for future cleanup code
443 * which can take awhile
445 lxc_set_state(name
, handler
, STOPPING
);
446 lxc_set_state(name
, handler
, STOPPED
);
448 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, handler
->lxcpath
, NULL
))
449 ERROR("failed to run post-stop hooks for container '%s'.", name
);
451 /* reset mask set by setup_signal_fd */
452 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
453 WARN("failed to restore sigprocmask");
455 lxc_console_delete(&handler
->conf
->console
);
456 lxc_delete_tty(&handler
->conf
->tty_info
);
457 close(handler
->conf
->maincmd_fd
);
458 handler
->conf
->maincmd_fd
= -1;
460 if (handler
->cgroup
) {
461 lxc_cgroup_process_info_free_and_remove(handler
->cgroup
);
462 handler
->cgroup
= NULL
;
467 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
471 lxc_set_state(name
, handler
, ABORTING
);
472 if (handler
->pid
> 0)
473 kill(handler
->pid
, SIGKILL
);
474 while ((ret
= waitpid(-1, &status
, 0)) > 0) ;
477 #include <sys/reboot.h>
478 #include <linux/reboot.h>
481 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
482 * in a child pid namespace if container reboot support exists.
483 * Otherwise, it will either succeed or return -EPERM.
485 static int container_reboot_supported(void *arg
)
491 if (ret
== -1 && errno
== EINVAL
)
496 static int must_drop_cap_sys_boot(struct lxc_conf
*conf
)
499 int ret
, cmd
, v
, flags
;
500 long stack_size
= 4096;
501 void *stack
= alloca(stack_size
);
505 f
= fopen("/proc/sys/kernel/ctrl-alt-del", "r");
507 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
511 ret
= fscanf(f
, "%d", &v
);
514 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
517 cmd
= v
? LINUX_REBOOT_CMD_CAD_ON
: LINUX_REBOOT_CMD_CAD_OFF
;
519 flags
= CLONE_NEWPID
| SIGCHLD
;
520 if (!lxc_list_empty(&conf
->id_map
))
521 flags
|= CLONE_NEWUSER
;
524 pid
= __clone2(container_reboot_supported
, stack
, stack_size
, flags
, &cmd
);
527 pid
= clone(container_reboot_supported
, stack
, flags
, &cmd
);
530 SYSERROR("failed to clone\n");
533 if (wait(&status
) < 0) {
534 SYSERROR("unexpected wait error: %m\n");
538 if (WEXITSTATUS(status
) != 1)
544 static int do_start(void *data
)
546 struct lxc_handler
*handler
= data
;
547 const char *lsm_label
= NULL
;
549 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
550 SYSERROR("failed to set sigprocmask");
554 /* This prctl must be before the synchro, so if the parent
555 * dies before we set the parent death signal, we will detect
556 * its death with the synchro right after, otherwise we have
557 * a window where the parent can exit before we set the pdeath
558 * signal leading to a unsupervized container.
560 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
561 SYSERROR("failed to set pdeath signal");
565 lxc_sync_fini_parent(handler
);
567 /* don't leak the pinfd to the container */
568 if (handler
->pinfd
>= 0) {
569 close(handler
->pinfd
);
572 /* Tell the parent task it can begin to configure the
573 * container and wait for it to finish
575 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
579 * if we are in a new user namespace, become root there to have
580 * privilege over our namespace
582 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
583 NOTICE("switching to gid/uid 0 in new user namespace");
586 goto out_warn_father
;
590 goto out_warn_father
;
594 #if HAVE_SYS_CAPABILITY_H
595 if (handler
->conf
->need_utmp_watch
) {
596 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
597 SYSERROR("failed to remove CAP_SYS_BOOT capability");
598 goto out_warn_father
;
600 DEBUG("Dropped cap_sys_boot\n");
604 /* Setup the container, ip, names, utsname, ... */
605 if (lxc_setup(handler
->name
, handler
->conf
, handler
->lxcpath
, handler
->cgroup
, handler
->data
) ){
606 ERROR("failed to setup the container");
607 goto out_warn_father
;
610 /* ask father to setup cgroups and wait for him to finish */
611 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
614 /* Set the label to change to when we exec(2) the container's init */
615 if (!strcmp(lsm_name(), "AppArmor"))
616 lsm_label
= handler
->conf
->lsm_aa_profile
;
617 else if (!strcmp(lsm_name(), "SELinux"))
618 lsm_label
= handler
->conf
->lsm_se_context
;
619 if (lsm_process_label_set(lsm_label
, 1, 1) < 0)
620 goto out_warn_father
;
621 lsm_proc_unmount(handler
->conf
);
623 if (lxc_seccomp_load(handler
->conf
) != 0)
624 goto out_warn_father
;
626 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, handler
->lxcpath
, NULL
)) {
627 ERROR("failed to run start hooks for container '%s'.", handler
->name
);
628 goto out_warn_father
;
631 /* The clearenv() and putenv() calls have been moved here
632 * to allow us to use enviroment variables passed to the various
633 * hooks, such as the start hook above. Not all of the
634 * variables like CONFIG_PATH or ROOTFS are valid in this
635 * context but others are. */
637 SYSERROR("failed to clear environment");
638 /* don't error out though */
641 if (putenv("container=lxc")) {
642 SYSERROR("failed to set environment variable");
643 goto out_warn_father
;
646 close(handler
->sigfd
);
648 /* after this call, we are in error because this
649 * ops should not return as it execs */
650 handler
->ops
->start(handler
, handler
->data
);
653 /* we want the parent to know something went wrong, so any
654 * value other than what it expects is ok. */
655 lxc_sync_wake_parent(handler
, LXC_SYNC_POST_CONFIGURE
);
659 int save_phys_nics(struct lxc_conf
*conf
)
661 struct lxc_list
*iterator
;
663 lxc_list_for_each(iterator
, &conf
->network
) {
664 struct lxc_netdev
*netdev
= iterator
->elem
;
666 if (netdev
->type
!= LXC_NET_PHYS
)
668 conf
->saved_nics
= realloc(conf
->saved_nics
,
669 (conf
->num_savednics
+1)*sizeof(struct saved_nic
));
670 if (!conf
->saved_nics
) {
671 SYSERROR("failed to allocate memory");
674 conf
->saved_nics
[conf
->num_savednics
].ifindex
= netdev
->ifindex
;
675 conf
->saved_nics
[conf
->num_savednics
].orig_name
= strdup(netdev
->link
);
676 if (!conf
->saved_nics
[conf
->num_savednics
].orig_name
) {
677 SYSERROR("failed to allocate memory");
680 INFO("stored saved_nic #%d idx %d name %s\n", conf
->num_savednics
,
681 conf
->saved_nics
[conf
->num_savednics
].ifindex
,
682 conf
->saved_nics
[conf
->num_savednics
].orig_name
);
683 conf
->num_savednics
++;
689 int lxc_spawn(struct lxc_handler
*handler
)
691 int failed_before_rename
= 0;
692 const char *name
= handler
->name
;
693 struct cgroup_meta_data
*cgroup_meta
= NULL
;
694 const char *cgroup_pattern
= NULL
;
695 int saved_ns_fd
[LXC_NS_MAX
];
696 int preserve_mask
= 0, i
;
698 for (i
= 0; i
< LXC_NS_MAX
; i
++)
699 if (handler
->conf
->inherit_ns_fd
[i
] != -1)
700 preserve_mask
|= ns_info
[i
].clone_flag
;
702 if (lxc_sync_init(handler
))
705 handler
->clone_flags
= CLONE_NEWPID
|CLONE_NEWNS
;
706 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
707 INFO("Cloning a new user namespace");
708 handler
->clone_flags
|= CLONE_NEWUSER
;
711 if (handler
->conf
->inherit_ns_fd
[LXC_NS_NET
] == -1) {
712 if (!lxc_requests_empty_network(handler
))
713 handler
->clone_flags
|= CLONE_NEWNET
;
715 if (!lxc_list_empty(&handler
->conf
->network
)) {
717 /* Find gateway addresses from the link device, which is
718 * no longer accessible inside the container. Do this
719 * before creating network interfaces, since goto
720 * out_delete_net does not work before lxc_clone. */
721 if (lxc_find_gateway_addresses(handler
)) {
722 ERROR("failed to find gateway addresses");
723 lxc_sync_fini(handler
);
727 /* that should be done before the clone because we will
728 * fill the netdev index and use them in the child
730 if (lxc_create_network(handler
)) {
731 ERROR("failed to create the network");
732 lxc_sync_fini(handler
);
737 if (save_phys_nics(handler
->conf
)) {
738 ERROR("failed to save physical nic info");
742 INFO("Inheriting a net namespace");
745 if (handler
->conf
->inherit_ns_fd
[LXC_NS_IPC
] == -1) {
746 handler
->clone_flags
|= CLONE_NEWIPC
;
748 INFO("Inheriting an IPC namespace");
751 if (handler
->conf
->inherit_ns_fd
[LXC_NS_UTS
] == -1) {
752 handler
->clone_flags
|= CLONE_NEWUTS
;
754 INFO("Inheriting a UTS namespace");
758 cgroup_meta
= lxc_cgroup_load_meta();
760 ERROR("failed to detect cgroup metadata");
764 /* if we are running as root, use system cgroup pattern, otherwise
765 * just create a cgroup under the current one. But also fall back to
766 * that if for some reason reading the configuration fails and no
767 * default value is available
770 cgroup_pattern
= default_cgroup_pattern();
772 cgroup_pattern
= "%n";
774 /* Create cgroup before doing clone(), so the child will know from
775 * handler which cgroup it is going to be put in later.
777 if ((handler
->cgroup
= lxc_cgroup_create(name
, cgroup_pattern
, cgroup_meta
, NULL
)) == NULL
) {
778 ERROR("failed to create cgroups for '%s'", name
);
783 * if the rootfs is not a blockdev, prevent the container from
784 * marking it readonly.
787 handler
->pinfd
= pin_rootfs(handler
->conf
->rootfs
.path
);
788 if (handler
->pinfd
== -1)
789 INFO("failed to pin the container's rootfs");
791 preserve_ns(saved_ns_fd
, preserve_mask
);
792 attach_ns(handler
->conf
->inherit_ns_fd
);
794 /* Create a process in a new set of namespaces */
795 handler
->pid
= lxc_clone(do_start
, handler
, handler
->clone_flags
);
796 if (handler
->pid
< 0) {
797 SYSERROR("failed to fork into a new namespace");
801 attach_ns(saved_ns_fd
);
803 lxc_sync_fini_child(handler
);
805 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
806 failed_before_rename
= 1;
808 /* In case there is still legacy ns cgroup support in the kernel.
809 * Should be removed at some later point in time.
811 if (lxc_cgroup_create_legacy(handler
->cgroup
, name
, handler
->pid
) < 0) {
812 ERROR("failed to create legacy ns cgroups for '%s'", name
);
816 if (lxc_setup_cgroup_without_devices(handler
, &handler
->conf
->cgroup
)) {
817 ERROR("failed to setup the cgroups for '%s'", name
);
821 if (lxc_cgroup_enter(handler
->cgroup
, handler
->pid
, false) < 0)
824 if (failed_before_rename
)
827 /* Create the network configuration */
828 if (handler
->clone_flags
& CLONE_NEWNET
) {
829 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
830 ERROR("failed to create the configured network");
835 /* map the container uids - the container became an invalid
836 * userid the moment it was cloned with CLONE_NEWUSER - this
837 * call doesn't change anything immediately, but allows the
838 * container to setuid(0) (0 being mapped to something else on
839 * the host) later to become a valid uid again */
840 if (lxc_map_ids(&handler
->conf
->id_map
, handler
->pid
)) {
841 ERROR("failed to set up id mapping");
845 /* Tell the child to continue its initialization. we'll get
846 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
848 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
851 if (lxc_setup_cgroup_devices(handler
, &handler
->conf
->cgroup
)) {
852 ERROR("failed to setup the devices cgroup for '%s'", name
);
856 /* Tell the child to complete its initialization and wait for
857 * it to exec or return an error. (the child will never
858 * return LXC_SYNC_POST_CGROUP+1. It will either close the
859 * sync pipe, causing lxc_sync_barrier_child to return
860 * success, or return a different value, causing us to error
863 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CGROUP
))
866 if (detect_shared_rootfs())
867 umount2(handler
->conf
->rootfs
.mount
, MNT_DETACH
);
869 if (handler
->ops
->post_start(handler
, handler
->data
))
872 if (lxc_set_state(name
, handler
, RUNNING
)) {
873 ERROR("failed to set state to %s",
874 lxc_state2str(RUNNING
));
878 lxc_cgroup_put_meta(cgroup_meta
);
879 lxc_sync_fini(handler
);
884 if (handler
->clone_flags
& CLONE_NEWNET
)
885 lxc_delete_network(handler
);
887 lxc_cgroup_put_meta(cgroup_meta
);
888 lxc_abort(name
, handler
);
889 lxc_sync_fini(handler
);
890 if (handler
->pinfd
>= 0) {
891 close(handler
->pinfd
);
898 int __lxc_start(const char *name
, struct lxc_conf
*conf
,
899 struct lxc_operations
* ops
, void *data
, const char *lxcpath
)
901 struct lxc_handler
*handler
;
905 handler
= lxc_init(name
, conf
, lxcpath
);
907 ERROR("failed to initialize the container");
911 handler
->data
= data
;
913 if (must_drop_cap_sys_boot(handler
->conf
)) {
914 #if HAVE_SYS_CAPABILITY_H
915 DEBUG("Dropping cap_sys_boot\n");
917 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported\n");
920 DEBUG("Not dropping cap_sys_boot or watching utmp\n");
921 handler
->conf
->need_utmp_watch
= 0;
924 err
= lxc_spawn(handler
);
926 ERROR("failed to spawn '%s'", name
);
930 err
= lxc_poll(name
, handler
);
932 ERROR("mainloop exited with an error");
936 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
940 * If the child process exited but was not signaled,
941 * it didn't call reboot. This should mean it was an
942 * lxc-execute which simply exited. In any case, treat
945 if (WIFSIGNALED(status
)) {
946 switch(WTERMSIG(status
)) {
947 case SIGINT
: /* halt */
948 DEBUG("Container halting");
950 case SIGHUP
: /* reboot */
951 DEBUG("Container rebooting");
952 handler
->conf
->reboot
= 1;
955 DEBUG("unknown exit status for init: %d\n", WTERMSIG(status
));
960 lxc_rename_phys_nics_on_shutdown(handler
->conf
);
962 if (handler
->pinfd
>= 0) {
963 close(handler
->pinfd
);
967 err
= lxc_error_set_and_log(handler
->pid
, status
);
969 lxc_delete_network(handler
);
972 lxc_fini(name
, handler
);
976 lxc_abort(name
, handler
);
984 static int start(struct lxc_handler
*handler
, void* data
)
986 struct start_args
*arg
= data
;
988 NOTICE("exec'ing '%s'", arg
->argv
[0]);
990 execvp(arg
->argv
[0], arg
->argv
);
991 SYSERROR("failed to exec %s", arg
->argv
[0]);
995 static int post_start(struct lxc_handler
*handler
, void* data
)
997 struct start_args
*arg
= data
;
999 NOTICE("'%s' started with pid '%d'", arg
->argv
[0], handler
->pid
);
1003 static struct lxc_operations start_ops
= {
1005 .post_start
= post_start
1008 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
,
1009 const char *lxcpath
)
1011 struct start_args start_arg
= {
1015 if (lxc_check_inherited(conf
, -1))
1018 conf
->need_utmp_watch
= 1;
1019 return __lxc_start(name
, conf
, &start_ops
, &start_arg
, lxcpath
);