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
37 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/prctl.h>
44 #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 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);
173 int lxc_check_inherited(struct lxc_conf
*conf
, int fd_to_ignore
)
175 struct dirent dirent
, *direntp
;
180 dir
= opendir("/proc/self/fd");
182 WARN("failed to open directory: %m");
188 while (!readdir_r(dir
, &dirent
, &direntp
)) {
192 if (!strcmp(direntp
->d_name
, "."))
195 if (!strcmp(direntp
->d_name
, ".."))
198 fd
= atoi(direntp
->d_name
);
200 if (fd
== fddir
|| fd
== lxc_log_fd
|| fd
== fd_to_ignore
)
206 if (conf
->close_all_fds
) {
209 INFO("closed inherited fd %d", fd
);
212 WARN("inherited fd %d", fd
);
215 closedir(dir
); /* cannot fail */
219 static int setup_signal_fd(sigset_t
*oldmask
)
224 /* Block everything except serious error signals */
225 if (sigfillset(&mask
) ||
226 sigdelset(&mask
, SIGILL
) ||
227 sigdelset(&mask
, SIGSEGV
) ||
228 sigdelset(&mask
, SIGBUS
) ||
229 sigdelset(&mask
, SIGWINCH
) ||
230 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
231 SYSERROR("failed to set signal mask");
235 fd
= signalfd(-1, &mask
, 0);
237 SYSERROR("failed to create the signal fd");
241 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
242 SYSERROR("failed to set sigfd to close-on-exec");
247 DEBUG("sigchild handler set");
252 static int signal_handler(int fd
, uint32_t events
, void *data
,
253 struct lxc_epoll_descr
*descr
)
255 struct signalfd_siginfo siginfo
;
259 bool init_died
= false;
261 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
263 ERROR("failed to read signal info");
267 if (ret
!= sizeof(siginfo
)) {
268 ERROR("unexpected siginfo size");
272 // check whether init is running
274 ret
= waitid(P_PID
, *pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
275 if (ret
== 0 && info
.si_pid
== *pid
) {
279 if (siginfo
.ssi_signo
!= SIGCHLD
) {
280 kill(*pid
, siginfo
.ssi_signo
);
281 INFO("forwarded signal %d to pid %d", siginfo
.ssi_signo
, *pid
);
282 return init_died
? 1 : 0;
285 if (siginfo
.ssi_code
== CLD_STOPPED
||
286 siginfo
.ssi_code
== CLD_CONTINUED
) {
287 INFO("container init process was stopped/continued");
288 return init_died
? 1 : 0;
291 /* more robustness, protect ourself from a SIGCHLD sent
292 * by a process different from the container init
294 if (siginfo
.ssi_pid
!= *pid
) {
295 WARN("invalid pid for SIGCHLD");
296 return init_died
? 1 : 0;
299 DEBUG("container init process exited");
303 static int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
305 handler
->state
= state
;
306 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
310 static int lxc_poll(const char *name
, struct lxc_handler
*handler
)
312 int sigfd
= handler
->sigfd
;
313 int pid
= handler
->pid
;
314 struct lxc_epoll_descr descr
;
316 if (lxc_mainloop_open(&descr
)) {
317 ERROR("failed to create mainloop");
321 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
322 ERROR("failed to add handler for the signal");
323 goto out_mainloop_open
;
326 if (lxc_console_mainloop_add(&descr
, handler
)) {
327 ERROR("failed to add console handler to mainloop");
328 goto out_mainloop_open
;
331 if (lxc_cmd_mainloop_add(name
, &descr
, handler
)) {
332 ERROR("failed to add command handler to mainloop");
333 goto out_mainloop_open
;
336 if (handler
->conf
->need_utmp_watch
) {
337 #if HAVE_SYS_CAPABILITY_H
338 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
339 ERROR("failed to add utmp handler to mainloop");
340 goto out_mainloop_open
;
343 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support");
347 return lxc_mainloop(&descr
, -1);
350 lxc_mainloop_close(&descr
);
356 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
, const char *lxcpath
)
358 struct lxc_handler
*handler
;
360 handler
= malloc(sizeof(*handler
));
364 memset(handler
, 0, sizeof(*handler
));
366 handler
->conf
= conf
;
367 handler
->lxcpath
= lxcpath
;
372 handler
->name
= strdup(name
);
373 if (!handler
->name
) {
374 ERROR("failed to allocate memory");
378 if (lxc_cmd_init(name
, handler
, lxcpath
))
381 if (lxc_read_seccomp_config(conf
) != 0) {
382 ERROR("failed loading seccomp policy");
383 goto out_close_maincmd_fd
;
386 /* Begin by setting the state to STARTING */
387 if (lxc_set_state(name
, handler
, STARTING
)) {
388 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
389 goto out_close_maincmd_fd
;
392 /* Start of environment variable setup for hooks */
393 if (setenv("LXC_NAME", name
, 1)) {
394 SYSERROR("failed to set environment variable for container name");
396 if (setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1)) {
397 SYSERROR("failed to set environment variable for config path");
399 if (setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1)) {
400 SYSERROR("failed to set environment variable for rootfs mount");
402 if (setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1)) {
403 SYSERROR("failed to set environment variable for rootfs mount");
405 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1)) {
406 SYSERROR("failed to set environment variable for console path");
408 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1)) {
409 SYSERROR("failed to set environment variable for console log");
411 /* End of environment variable setup for hooks */
413 if (run_lxc_hooks(name
, "pre-start", conf
, handler
->lxcpath
, NULL
)) {
414 ERROR("failed to run pre-start hooks for container '%s'.", name
);
418 if (lxc_create_tty(name
, conf
)) {
419 ERROR("failed to create the ttys");
423 /* the signal fd has to be created before forking otherwise
424 * if the child process exits before we setup the signal fd,
425 * the event will be lost and the command will be stuck */
426 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
427 if (handler
->sigfd
< 0) {
428 ERROR("failed to set sigchild fd handler");
432 /* do this after setting up signals since it might unblock SIGWINCH */
433 if (lxc_console_create(conf
)) {
434 ERROR("failed to create console");
435 goto out_restore_sigmask
;
438 if (ttys_shift_ids(conf
) < 0) {
439 ERROR("Failed to shift tty into container");
440 goto out_restore_sigmask
;
443 INFO("'%s' is initialized", name
);
447 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
449 lxc_delete_tty(&conf
->tty_info
);
451 lxc_set_state(name
, handler
, ABORTING
);
452 out_close_maincmd_fd
:
453 close(conf
->maincmd_fd
);
454 conf
->maincmd_fd
= -1;
457 handler
->name
= NULL
;
463 static void lxc_fini(const char *name
, struct lxc_handler
*handler
)
465 /* The STOPPING state is there for future cleanup code
466 * which can take awhile
468 lxc_set_state(name
, handler
, STOPPING
);
469 lxc_set_state(name
, handler
, STOPPED
);
471 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, handler
->lxcpath
, NULL
))
472 ERROR("failed to run post-stop hooks for container '%s'.", name
);
474 /* reset mask set by setup_signal_fd */
475 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
476 WARN("failed to restore sigprocmask");
478 lxc_console_delete(&handler
->conf
->console
);
479 lxc_delete_tty(&handler
->conf
->tty_info
);
480 close(handler
->conf
->maincmd_fd
);
481 handler
->conf
->maincmd_fd
= -1;
483 cgroup_destroy(handler
);
487 static void lxc_abort(const char *name
, struct lxc_handler
*handler
)
491 lxc_set_state(name
, handler
, ABORTING
);
492 if (handler
->pid
> 0)
493 kill(handler
->pid
, SIGKILL
);
494 while ((ret
= waitpid(-1, &status
, 0)) > 0) ;
497 #include <sys/reboot.h>
498 #include <linux/reboot.h>
501 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
502 * in a child pid namespace if container reboot support exists.
503 * Otherwise, it will either succeed or return -EPERM.
505 static int container_reboot_supported(void *arg
)
511 if (ret
== -1 && errno
== EINVAL
)
516 static int must_drop_cap_sys_boot(struct lxc_conf
*conf
)
519 int ret
, cmd
, v
, flags
;
520 long stack_size
= 4096;
521 void *stack
= alloca(stack_size
);
525 f
= fopen("/proc/sys/kernel/ctrl-alt-del", "r");
527 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
531 ret
= fscanf(f
, "%d", &v
);
534 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
537 cmd
= v
? LINUX_REBOOT_CMD_CAD_ON
: LINUX_REBOOT_CMD_CAD_OFF
;
539 flags
= CLONE_NEWPID
| SIGCHLD
;
540 if (!lxc_list_empty(&conf
->id_map
))
541 flags
|= CLONE_NEWUSER
;
544 pid
= __clone2(container_reboot_supported
, stack
, stack_size
, flags
, &cmd
);
547 pid
= clone(container_reboot_supported
, stack
, flags
, &cmd
);
550 SYSERROR("failed to clone");
553 if (wait(&status
) < 0) {
554 SYSERROR("unexpected wait error: %m");
558 if (WEXITSTATUS(status
) != 1)
565 * netpipe is used in the unprivileged case to transfer the ifindexes
566 * from parent to child
568 static int netpipe
= -1;
570 static inline int count_veths(struct lxc_list
*network
)
572 struct lxc_list
*iterator
;
573 struct lxc_netdev
*netdev
;
576 lxc_list_for_each(iterator
, network
) {
577 netdev
= iterator
->elem
;
578 if (netdev
->type
!= LXC_NET_VETH
)
585 static int read_unpriv_netifindex(struct lxc_list
*network
)
587 struct lxc_list
*iterator
;
588 struct lxc_netdev
*netdev
;
592 lxc_list_for_each(iterator
, network
) {
593 netdev
= iterator
->elem
;
594 if (netdev
->type
!= LXC_NET_VETH
)
596 if (!(netdev
->name
= malloc(IFNAMSIZ
))) {
597 ERROR("Out of memory");
601 if (read(netpipe
, netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
610 static int do_start(void *data
)
612 struct lxc_handler
*handler
= data
;
613 const char *lsm_label
= NULL
;
615 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
616 SYSERROR("failed to set sigprocmask");
620 /* This prctl must be before the synchro, so if the parent
621 * dies before we set the parent death signal, we will detect
622 * its death with the synchro right after, otherwise we have
623 * a window where the parent can exit before we set the pdeath
624 * signal leading to a unsupervized container.
626 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
627 SYSERROR("failed to set pdeath signal");
631 lxc_sync_fini_parent(handler
);
633 /* don't leak the pinfd to the container */
634 if (handler
->pinfd
>= 0) {
635 close(handler
->pinfd
);
638 /* Tell the parent task it can begin to configure the
639 * container and wait for it to finish
641 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
644 if (read_unpriv_netifindex(&handler
->conf
->network
) < 0)
645 goto out_warn_father
;
648 * if we are in a new user namespace, become root there to have
649 * privilege over our namespace
651 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
652 NOTICE("switching to gid/uid 0 in new user namespace");
655 goto out_warn_father
;
659 goto out_warn_father
;
661 if (setgroups(0, NULL
)) {
662 SYSERROR("setgroups");
663 goto out_warn_father
;
667 if (access(handler
->lxcpath
, X_OK
)) {
668 print_top_failing_dir(handler
->lxcpath
);
669 goto out_warn_father
;
672 #if HAVE_SYS_CAPABILITY_H
673 if (handler
->conf
->need_utmp_watch
) {
674 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
675 SYSERROR("failed to remove CAP_SYS_BOOT capability");
676 goto out_warn_father
;
678 DEBUG("Dropped cap_sys_boot");
682 /* Setup the container, ip, names, utsname, ... */
683 if (lxc_setup(handler
)) {
684 ERROR("failed to setup the container");
685 goto out_warn_father
;
688 /* ask father to setup cgroups and wait for him to finish */
689 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
692 /* Set the label to change to when we exec(2) the container's init */
693 if (!strcmp(lsm_name(), "AppArmor"))
694 lsm_label
= handler
->conf
->lsm_aa_profile
;
695 else if (!strcmp(lsm_name(), "SELinux"))
696 lsm_label
= handler
->conf
->lsm_se_context
;
697 if (lsm_process_label_set(lsm_label
, 1, 1) < 0)
698 goto out_warn_father
;
700 if (lxc_check_inherited(handler
->conf
, handler
->sigfd
))
703 /* If we mounted a temporary proc, then unmount it now */
704 tmp_proc_unmount(handler
->conf
);
706 if (lxc_seccomp_load(handler
->conf
) != 0)
707 goto out_warn_father
;
709 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, handler
->lxcpath
, NULL
)) {
710 ERROR("failed to run start hooks for container '%s'.", handler
->name
);
711 goto out_warn_father
;
714 /* The clearenv() and putenv() calls have been moved here
715 * to allow us to use enviroment variables passed to the various
716 * hooks, such as the start hook above. Not all of the
717 * variables like CONFIG_PATH or ROOTFS are valid in this
718 * context but others are. */
720 SYSERROR("failed to clear environment");
721 /* don't error out though */
724 if (putenv("container=lxc")) {
725 SYSERROR("failed to set environment variable");
726 goto out_warn_father
;
729 close(handler
->sigfd
);
731 /* after this call, we are in error because this
732 * ops should not return as it execs */
733 handler
->ops
->start(handler
, handler
->data
);
736 /* we want the parent to know something went wrong, so any
737 * value other than what it expects is ok. */
738 lxc_sync_wake_parent(handler
, LXC_SYNC_POST_CONFIGURE
);
742 static int save_phys_nics(struct lxc_conf
*conf
)
744 struct lxc_list
*iterator
;
746 lxc_list_for_each(iterator
, &conf
->network
) {
747 struct lxc_netdev
*netdev
= iterator
->elem
;
749 if (netdev
->type
!= LXC_NET_PHYS
)
751 conf
->saved_nics
= realloc(conf
->saved_nics
,
752 (conf
->num_savednics
+1)*sizeof(struct saved_nic
));
753 if (!conf
->saved_nics
) {
754 SYSERROR("failed to allocate memory");
757 conf
->saved_nics
[conf
->num_savednics
].ifindex
= netdev
->ifindex
;
758 conf
->saved_nics
[conf
->num_savednics
].orig_name
= strdup(netdev
->link
);
759 if (!conf
->saved_nics
[conf
->num_savednics
].orig_name
) {
760 SYSERROR("failed to allocate memory");
763 INFO("stored saved_nic #%d idx %d name %s", conf
->num_savednics
,
764 conf
->saved_nics
[conf
->num_savednics
].ifindex
,
765 conf
->saved_nics
[conf
->num_savednics
].orig_name
);
766 conf
->num_savednics
++;
772 static int lxc_spawn(struct lxc_handler
*handler
)
774 int failed_before_rename
= 0;
775 const char *name
= handler
->name
;
776 bool cgroups_connected
= false;
777 int saved_ns_fd
[LXC_NS_MAX
];
778 int preserve_mask
= 0, i
;
779 int netpipepair
[2], nveths
;
781 for (i
= 0; i
< LXC_NS_MAX
; i
++)
782 if (handler
->conf
->inherit_ns_fd
[i
] != -1)
783 preserve_mask
|= ns_info
[i
].clone_flag
;
785 if (lxc_sync_init(handler
))
788 handler
->clone_flags
= CLONE_NEWPID
|CLONE_NEWNS
;
789 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
790 INFO("Cloning a new user namespace");
791 handler
->clone_flags
|= CLONE_NEWUSER
;
794 if (handler
->conf
->inherit_ns_fd
[LXC_NS_NET
] == -1) {
795 if (!lxc_requests_empty_network(handler
))
796 handler
->clone_flags
|= CLONE_NEWNET
;
798 if (!lxc_list_empty(&handler
->conf
->network
)) {
800 /* Find gateway addresses from the link device, which is
801 * no longer accessible inside the container. Do this
802 * before creating network interfaces, since goto
803 * out_delete_net does not work before lxc_clone. */
804 if (lxc_find_gateway_addresses(handler
)) {
805 ERROR("failed to find gateway addresses");
806 lxc_sync_fini(handler
);
810 /* that should be done before the clone because we will
811 * fill the netdev index and use them in the child
813 if (lxc_create_network(handler
)) {
814 ERROR("failed to create the network");
815 lxc_sync_fini(handler
);
820 if (save_phys_nics(handler
->conf
)) {
821 ERROR("failed to save physical nic info");
825 INFO("Inheriting a net namespace");
828 if (handler
->conf
->inherit_ns_fd
[LXC_NS_IPC
] == -1) {
829 handler
->clone_flags
|= CLONE_NEWIPC
;
831 INFO("Inheriting an IPC namespace");
834 if (handler
->conf
->inherit_ns_fd
[LXC_NS_UTS
] == -1) {
835 handler
->clone_flags
|= CLONE_NEWUTS
;
837 INFO("Inheriting a UTS namespace");
841 if (!cgroup_init(handler
)) {
842 ERROR("failed initializing cgroup support");
846 cgroups_connected
= true;
848 if (!cgroup_create(handler
)) {
849 ERROR("failed creating cgroups");
854 * if the rootfs is not a blockdev, prevent the container from
855 * marking it readonly.
857 * if the container is unprivileged then skip rootfs pinning
859 if (lxc_list_empty(&handler
->conf
->id_map
)) {
860 handler
->pinfd
= pin_rootfs(handler
->conf
->rootfs
.path
);
861 if (handler
->pinfd
== -1)
862 INFO("failed to pin the container's rootfs");
865 if (preserve_ns(saved_ns_fd
, preserve_mask
) < 0)
867 if (attach_ns(handler
->conf
->inherit_ns_fd
) < 0)
870 if (am_unpriv() && (nveths
= count_veths(&handler
->conf
->network
))) {
871 if (pipe(netpipepair
) < 0) {
872 SYSERROR("Error creating pipe");
875 /* store netpipe in the global var for do_start's use */
876 netpipe
= netpipepair
[0];
879 /* Create a process in a new set of namespaces */
880 handler
->pid
= lxc_clone(do_start
, handler
, handler
->clone_flags
);
881 if (handler
->pid
< 0) {
882 SYSERROR("failed to fork into a new namespace");
886 if (attach_ns(saved_ns_fd
))
887 WARN("failed to restore saved namespaces");
889 lxc_sync_fini_child(handler
);
891 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
892 failed_before_rename
= 1;
894 if (!cgroup_create_legacy(handler
)) {
895 ERROR("failed to setup the legacy cgroups for %s", name
);
898 if (!cgroup_setup_limits(handler
, false)) {
899 ERROR("failed to setup the cgroup limits for '%s'", name
);
903 if (!cgroup_enter(handler
))
906 if (!cgroup_chown(handler
))
909 if (failed_before_rename
)
912 /* Create the network configuration */
913 if (handler
->clone_flags
& CLONE_NEWNET
) {
914 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
915 ERROR("failed to create the configured network");
921 struct lxc_list
*iterator
;
922 struct lxc_netdev
*netdev
;
925 lxc_list_for_each(iterator
, &handler
->conf
->network
) {
926 netdev
= iterator
->elem
;
927 if (netdev
->type
!= LXC_NET_VETH
)
929 if (write(netpipepair
[1], netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
930 ERROR("Error writing veth name to container");
934 close(netpipepair
[1]);
937 /* map the container uids - the container became an invalid
938 * userid the moment it was cloned with CLONE_NEWUSER - this
939 * call doesn't change anything immediately, but allows the
940 * container to setuid(0) (0 being mapped to something else on
941 * the host) later to become a valid uid again */
942 if (lxc_map_ids(&handler
->conf
->id_map
, handler
->pid
)) {
943 ERROR("failed to set up id mapping");
947 /* Tell the child to continue its initialization. we'll get
948 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
950 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
953 if (!cgroup_setup_limits(handler
, true)) {
954 ERROR("failed to setup the devices cgroup for '%s'", name
);
959 cgroups_connected
= false;
961 /* Tell the child to complete its initialization and wait for
962 * it to exec or return an error. (the child will never
963 * return LXC_SYNC_POST_CGROUP+1. It will either close the
964 * sync pipe, causing lxc_sync_barrier_child to return
965 * success, or return a different value, causing us to error
968 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CGROUP
))
971 if (detect_shared_rootfs())
972 umount2(handler
->conf
->rootfs
.mount
, MNT_DETACH
);
974 if (handler
->ops
->post_start(handler
, handler
->data
))
977 if (lxc_set_state(name
, handler
, RUNNING
)) {
978 ERROR("failed to set state to %s",
979 lxc_state2str(RUNNING
));
983 lxc_sync_fini(handler
);
988 if (cgroups_connected
)
990 if (handler
->clone_flags
& CLONE_NEWNET
)
991 lxc_delete_network(handler
);
993 lxc_abort(name
, handler
);
994 lxc_sync_fini(handler
);
995 if (handler
->pinfd
>= 0) {
996 close(handler
->pinfd
);
1003 int __lxc_start(const char *name
, struct lxc_conf
*conf
,
1004 struct lxc_operations
* ops
, void *data
, const char *lxcpath
)
1006 struct lxc_handler
*handler
;
1010 handler
= lxc_init(name
, conf
, lxcpath
);
1012 ERROR("failed to initialize the container");
1016 handler
->data
= data
;
1018 if (must_drop_cap_sys_boot(handler
->conf
)) {
1019 #if HAVE_SYS_CAPABILITY_H
1020 DEBUG("Dropping cap_sys_boot");
1022 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported");
1025 DEBUG("Not dropping cap_sys_boot or watching utmp");
1026 handler
->conf
->need_utmp_watch
= 0;
1029 err
= lxc_spawn(handler
);
1031 ERROR("failed to spawn '%s'", name
);
1032 goto out_fini_nonet
;
1035 err
= lxc_poll(name
, handler
);
1037 ERROR("mainloop exited with an error");
1041 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
1045 * If the child process exited but was not signaled,
1046 * it didn't call reboot. This should mean it was an
1047 * lxc-execute which simply exited. In any case, treat
1050 if (WIFSIGNALED(status
)) {
1051 switch(WTERMSIG(status
)) {
1052 case SIGINT
: /* halt */
1053 DEBUG("Container halting");
1055 case SIGHUP
: /* reboot */
1056 DEBUG("Container rebooting");
1057 handler
->conf
->reboot
= 1;
1059 case SIGSYS
: /* seccomp */
1060 DEBUG("Container violated its seccomp policy");
1063 DEBUG("unknown exit status for init: %d", WTERMSIG(status
));
1068 lxc_rename_phys_nics_on_shutdown(handler
->conf
);
1070 if (handler
->pinfd
>= 0) {
1071 close(handler
->pinfd
);
1072 handler
->pinfd
= -1;
1075 err
= lxc_error_set_and_log(handler
->pid
, status
);
1077 lxc_delete_network(handler
);
1080 lxc_fini(name
, handler
);
1084 lxc_abort(name
, handler
);
1092 static int start(struct lxc_handler
*handler
, void* data
)
1094 struct start_args
*arg
= data
;
1096 NOTICE("exec'ing '%s'", arg
->argv
[0]);
1098 execvp(arg
->argv
[0], arg
->argv
);
1099 SYSERROR("failed to exec %s", arg
->argv
[0]);
1103 static int post_start(struct lxc_handler
*handler
, void* data
)
1105 struct start_args
*arg
= data
;
1107 NOTICE("'%s' started with pid '%d'", arg
->argv
[0], handler
->pid
);
1111 static struct lxc_operations start_ops
= {
1113 .post_start
= post_start
1116 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
,
1117 const char *lxcpath
)
1119 struct start_args start_arg
= {
1123 conf
->need_utmp_watch
= 1;
1124 return __lxc_start(name
, conf
, &start_ops
, &start_arg
, lxcpath
);