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
39 #include <sys/mount.h>
40 #include <sys/param.h>
41 #include <sys/prctl.h>
42 #include <sys/socket.h>
44 #include <sys/syscall.h>
45 #include <sys/types.h>
50 #include <sys/capability.h>
53 #ifndef HAVE_DECL_PR_CAPBSET_DROP
54 #define PR_CAPBSET_DROP 24
57 #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS
58 #define PR_SET_NO_NEW_PRIVS 38
61 #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS
62 #define PR_GET_NO_NEW_PRIVS 39
74 #include "lxccontainer.h"
76 #include "lxcseccomp.h"
80 #include "namespace.h"
82 #include "storage_utils.h"
87 lxc_log_define(lxc_start
, lxc
);
89 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
90 static bool do_destroy_container(struct lxc_conf
*conf
);
91 static int lxc_rmdir_onedev_wrapper(void *data
);
92 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
95 static void print_top_failing_dir(const char *path
)
97 size_t len
= strlen(path
);
98 char *copy
= alloca(len
+ 1), *p
, *e
, saved
;
104 while (p
< e
&& *p
== '/')
106 while (p
< e
&& *p
!= '/')
110 if (access(copy
, X_OK
)) {
111 SYSERROR("Could not access %s. Please grant it x "
112 "access, or add an ACL for the container "
120 static void close_ns(int ns_fd
[LXC_NS_MAX
])
124 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
132 /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified
134 * Return true on success, false on failure.
136 static bool preserve_ns(int ns_fd
[LXC_NS_MAX
], int clone_flags
, pid_t pid
)
140 for (i
= 0; i
< LXC_NS_MAX
; i
++)
143 ret
= lxc_preserve_ns(pid
, "");
145 SYSERROR("Kernel does not support attaching to namespaces.");
151 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
152 if ((clone_flags
& ns_info
[i
].clone_flag
) == 0)
154 ns_fd
[i
] = lxc_preserve_ns(pid
, ns_info
[i
].proc_name
);
163 SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info
[i
].proc_name
);
165 SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info
[i
].proc_name
, strerror(errno
));
170 static int attach_ns(const int ns_fd
[LXC_NS_MAX
]) {
173 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
177 if (setns(ns_fd
[i
], 0) != 0)
183 SYSERROR("Failed to attach %s namespace.", ns_info
[i
].proc_name
);
187 static int match_fd(int fd
)
189 return (fd
== 0 || fd
== 1 || fd
== 2);
192 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
193 int *fds_to_ignore
, size_t len_fds
)
195 struct dirent
*direntp
;
200 if (conf
&& conf
->close_all_fds
)
204 dir
= opendir("/proc/self/fd");
206 WARN("Failed to open directory: %s.", strerror(errno
));
212 while ((direntp
= readdir(dir
))) {
216 if (!strcmp(direntp
->d_name
, "."))
219 if (!strcmp(direntp
->d_name
, ".."))
222 if (lxc_safe_int(direntp
->d_name
, &fd
) < 0) {
223 INFO("Could not parse file descriptor for: %s", direntp
->d_name
);
227 for (i
= 0; i
< len_fds
; i
++)
228 if (fds_to_ignore
[i
] == fd
)
231 if (fd
== fddir
|| fd
== lxc_log_fd
||
232 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
235 if (current_config
&& fd
== current_config
->logfd
)
244 INFO("Closed inherited fd: %d.", fd
);
247 WARN("Inherited fd: %d.", fd
);
250 /* Only enable syslog at this point to avoid the above logging function
251 * to open a new fd and make the check_inherited function enter an
254 lxc_log_enable_syslog();
256 closedir(dir
); /* cannot fail */
260 static int setup_signal_fd(sigset_t
*oldmask
)
265 /* Block everything except serious error signals. */
266 if (sigfillset(&mask
) ||
267 sigdelset(&mask
, SIGILL
) ||
268 sigdelset(&mask
, SIGSEGV
) ||
269 sigdelset(&mask
, SIGBUS
) ||
270 sigdelset(&mask
, SIGWINCH
) ||
271 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
272 SYSERROR("Failed to set signal mask.");
276 fd
= signalfd(-1, &mask
, 0);
278 SYSERROR("Failed to create signal file descriptor.");
282 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
283 SYSERROR("Failed to set FD_CLOEXEC on the signal file descriptor: %d.", fd
);
288 DEBUG("Set SIGCHLD handler with file descriptor: %d.", fd
);
293 static int signal_handler(int fd
, uint32_t events
, void *data
,
294 struct lxc_epoll_descr
*descr
)
296 struct signalfd_siginfo siginfo
;
300 bool init_died
= false;
302 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
304 ERROR("Failed to read signal info from signal file descriptor: %d.", fd
);
308 if (ret
!= sizeof(siginfo
)) {
309 ERROR("Unexpected size for siginfo struct.");
313 /* Check whether init is running. */
315 ret
= waitid(P_PID
, *pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
316 if (ret
== 0 && info
.si_pid
== *pid
)
319 if (siginfo
.ssi_signo
!= SIGCHLD
) {
320 kill(*pid
, siginfo
.ssi_signo
);
321 INFO("Forwarded signal %d to pid %d.", siginfo
.ssi_signo
, *pid
);
322 return init_died
? 1 : 0;
325 if (siginfo
.ssi_code
== CLD_STOPPED
) {
326 INFO("Container init process was stopped.");
327 return init_died
? 1 : 0;
328 } else if (siginfo
.ssi_code
== CLD_CONTINUED
) {
329 INFO("Container init process was continued.");
330 return init_died
? 1 : 0;
333 /* More robustness, protect ourself from a SIGCHLD sent
334 * by a process different from the container init.
336 if (siginfo
.ssi_pid
!= *pid
) {
337 NOTICE("Received SIGCHLD from pid %d instead of container init %d.", siginfo
.ssi_pid
, *pid
);
338 return init_died
? 1 : 0;
341 DEBUG("Container init process %d exited.", *pid
);
345 static int lxc_serve_state_clients(const char *name
,
346 struct lxc_handler
*handler
,
350 struct lxc_list
*cur
, *next
;
351 struct state_client
*client
;
352 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
356 /* Only set state under process lock held so that we don't cause
357 * lxc_cmd_add_state_client() to miss a state.
359 handler
->state
= state
;
360 TRACE("set container state to %s", lxc_state2str(state
));
362 if (lxc_list_empty(&handler
->state_clients
)) {
363 TRACE("no state clients registered");
365 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
369 strncpy(msg
.name
, name
, sizeof(msg
.name
));
370 msg
.name
[sizeof(msg
.name
) - 1] = 0;
372 lxc_list_for_each_safe(cur
, &handler
->state_clients
, next
) {
375 if (!client
->states
[state
]) {
376 TRACE("state %s not registered for state client %d",
377 lxc_state2str(state
), client
->clientfd
);
381 TRACE("sending state %s to state client %d",
382 lxc_state2str(state
), client
->clientfd
);
385 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), 0);
390 ERROR("failed to send message to client");
393 /* kick client from list */
394 close(client
->clientfd
);
404 static int lxc_serve_state_socket_pair(const char *name
,
405 struct lxc_handler
*handler
,
410 if (!handler
->backgrounded
||
411 handler
->state_socket_pair
[1] < 0 ||
415 /* Close read end of the socket pair. */
416 close(handler
->state_socket_pair
[0]);
417 handler
->state_socket_pair
[0] = -1;
419 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
420 &(int){state
}, sizeof(int));
421 if (ret
!= sizeof(int))
422 SYSERROR("Failed to send state to %d",
423 handler
->state_socket_pair
[1]);
425 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
426 handler
->state_socket_pair
[1]);
428 /* Close write end of the socket pair. */
429 close(handler
->state_socket_pair
[1]);
430 handler
->state_socket_pair
[1] = -1;
435 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
440 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
442 ERROR("Failed to synchronize via anonymous pair of unix sockets");
446 ret
= lxc_serve_state_clients(name
, handler
, state
);
450 /* This function will try to connect to the legacy lxc-monitord state
451 * server and only exists for backwards compatibility.
453 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
458 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
460 int sigfd
= handler
->sigfd
;
461 int pid
= handler
->pid
;
462 struct lxc_epoll_descr descr
;
464 if (lxc_mainloop_open(&descr
)) {
465 ERROR("Failed to create LXC mainloop.");
469 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
470 ERROR("Failed to add signal handler with file descriptor %d to LXC mainloop.", sigfd
);
471 goto out_mainloop_open
;
474 if (lxc_console_mainloop_add(&descr
, handler
->conf
)) {
475 ERROR("Failed to add console handler to LXC mainloop.");
476 goto out_mainloop_open
;
479 if (lxc_cmd_mainloop_add(name
, &descr
, handler
)) {
480 ERROR("Failed to add command handler to LXC mainloop.");
481 goto out_mainloop_open
;
484 if (handler
->conf
->need_utmp_watch
) {
486 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
487 ERROR("Failed to add utmp handler to LXC mainloop.");
488 goto out_mainloop_open
;
491 DEBUG("Not starting utmp handler as CAP_SYS_BOOT cannot be dropped without capabilities support.");
494 TRACE("lxc mainloop is ready");
496 return lxc_mainloop(&descr
, -1);
499 lxc_mainloop_close(&descr
);
507 void lxc_free_handler(struct lxc_handler
*handler
)
509 if (handler
->conf
&& handler
->conf
->maincmd_fd
)
510 close(handler
->conf
->maincmd_fd
);
512 if (handler
->state_socket_pair
[0] >= 0)
513 close(handler
->state_socket_pair
[0]);
515 if (handler
->state_socket_pair
[1] >= 0)
516 close(handler
->state_socket_pair
[1]);
521 handler
->conf
= NULL
;
525 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
526 const char *lxcpath
, bool daemonize
)
529 struct lxc_handler
*handler
;
531 handler
= malloc(sizeof(*handler
));
533 ERROR("failed to allocate memory");
537 memset(handler
, 0, sizeof(*handler
));
539 handler
->ttysock
[0] = handler
->ttysock
[1] = -1;
540 handler
->conf
= conf
;
541 handler
->lxcpath
= lxcpath
;
543 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
544 lxc_list_init(&handler
->state_clients
);
546 for (i
= 0; i
< LXC_NS_MAX
; i
++)
547 handler
->nsfd
[i
] = -1;
549 handler
->name
= strdup(name
);
550 if (!handler
->name
) {
551 ERROR("failed to allocate memory");
555 if (daemonize
&& !handler
->conf
->reboot
) {
556 /* Create socketpair() to synchronize on daemonized startup.
557 * When the container reboots we don't need to synchronize again
558 * currently so don't open another socketpair().
560 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
561 handler
->state_socket_pair
);
563 ERROR("Failed to create anonymous pair of unix sockets");
566 TRACE("Created anonymous pair {%d,%d} of unix sockets",
567 handler
->state_socket_pair
[0],
568 handler
->state_socket_pair
[1]);
571 if (lxc_cmd_init(name
, handler
, lxcpath
)) {
572 ERROR("failed to set up command socket");
576 TRACE("unix domain socket %d for command server is ready",
577 handler
->conf
->maincmd_fd
);
582 lxc_free_handler(handler
);
587 int lxc_init(const char *name
, struct lxc_handler
*handler
)
589 struct lxc_conf
*conf
= handler
->conf
;
592 TRACE("initialized LSM");
594 if (lxc_read_seccomp_config(conf
) != 0) {
595 ERROR("Failed loading seccomp policy.");
596 goto out_close_maincmd_fd
;
598 TRACE("read seccomp policy");
600 /* Begin by setting the state to STARTING. */
601 if (lxc_set_state(name
, handler
, STARTING
)) {
602 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
, lxc_state2str(STARTING
));
603 goto out_close_maincmd_fd
;
605 TRACE("set container state to \"STARTING\"");
607 /* Start of environment variable setup for hooks. */
608 if (name
&& setenv("LXC_NAME", name
, 1))
609 SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name
);
611 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
612 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf
->rcfile
);
614 if (conf
->rootfs
.mount
&& setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1))
615 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf
->rootfs
.mount
);
617 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
618 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf
->rootfs
.path
);
620 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1))
621 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf
->console
.path
);
623 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1))
624 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf
->console
.log_path
);
626 if (setenv("LXC_CGNS_AWARE", "1", 1))
627 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1.");
628 /* End of environment variable setup for hooks. */
630 TRACE("set environment variables");
632 if (run_lxc_hooks(name
, "pre-start", conf
, handler
->lxcpath
, NULL
)) {
633 ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name
);
636 TRACE("ran pre-start hooks");
638 /* The signal fd has to be created before forking otherwise if the child
639 * process exits before we setup the signal fd, the event will be lost
640 * and the command will be stuck.
642 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
643 if (handler
->sigfd
< 0) {
644 ERROR("Failed to setup SIGCHLD fd handler.");
647 TRACE("set up signal fd");
649 /* Do this after setting up signals since it might unblock SIGWINCH. */
650 if (lxc_console_create(conf
)) {
651 ERROR("Failed to create console for container \"%s\".", name
);
652 goto out_restore_sigmask
;
654 TRACE("created console");
656 if (lxc_ttys_shift_ids(conf
) < 0) {
657 ERROR("Failed to shift tty into container.");
658 goto out_restore_sigmask
;
660 TRACE("shifted tty ids");
662 INFO("container \"%s\" is initialized", name
);
666 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
668 lxc_delete_tty(&conf
->tty_info
);
670 lxc_set_state(name
, handler
, ABORTING
);
671 out_close_maincmd_fd
:
672 close(conf
->maincmd_fd
);
673 conf
->maincmd_fd
= -1;
677 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
680 struct lxc_list
*cur
, *next
;
681 pid_t self
= getpid();
682 char *namespaces
[LXC_NS_MAX
+ 1];
683 size_t namespace_count
= 0;
685 /* The STOPPING state is there for future cleanup code which can take
688 lxc_set_state(name
, handler
, STOPPING
);
690 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
691 if (handler
->nsfd
[i
] != -1) {
692 rc
= asprintf(&namespaces
[namespace_count
], "%s:/proc/%d/fd/%d",
693 ns_info
[i
].proc_name
, self
, handler
->nsfd
[i
]);
695 SYSERROR("Failed to allocate memory.");
701 namespaces
[namespace_count
] = NULL
;
703 if (handler
->conf
->reboot
&& setenv("LXC_TARGET", "reboot", 1))
704 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot.");
706 if (!handler
->conf
->reboot
&& setenv("LXC_TARGET", "stop", 1))
707 SYSERROR("Failed to set environment variable: LXC_TARGET=stop.");
709 if (run_lxc_hooks(name
, "stop", handler
->conf
, handler
->lxcpath
, namespaces
))
710 ERROR("Failed to run lxc.hook.stop for container \"%s\".", name
);
712 while (namespace_count
--)
713 free(namespaces
[namespace_count
]);
714 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
715 if (handler
->nsfd
[i
] != -1) {
716 close(handler
->nsfd
[i
]);
717 handler
->nsfd
[i
] = -1;
721 if (handler
->netnsfd
>= 0) {
722 close(handler
->netnsfd
);
723 handler
->netnsfd
= -1;
726 lxc_set_state(name
, handler
, STOPPED
);
728 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, handler
->lxcpath
, NULL
)) {
729 ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name
);
730 if (handler
->conf
->reboot
) {
731 WARN("Container will be stopped instead of rebooted.");
732 handler
->conf
->reboot
= 0;
733 if (setenv("LXC_TARGET", "stop", 1))
734 WARN("Failed to set environment variable: LXC_TARGET=stop.");
738 /* Reset mask set by setup_signal_fd. */
739 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
740 WARN("Failed to restore signal mask.");
742 lxc_console_delete(&handler
->conf
->console
);
743 lxc_delete_tty(&handler
->conf
->tty_info
);
745 /* close the command socket */
746 close(handler
->conf
->maincmd_fd
);
747 handler
->conf
->maincmd_fd
= -1;
749 /* The command socket is now closed, no more state clients can register
750 * themselves from now on. So free the list of state clients.
752 lxc_list_for_each_safe(cur
, &handler
->state_clients
, next
) {
753 struct state_client
*client
= cur
->elem
;
754 /* close state client socket */
755 close(client
->clientfd
);
762 if (handler
->ttysock
[0] != -1) {
763 close(handler
->ttysock
[0]);
764 close(handler
->ttysock
[1]);
767 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= 1)
768 lxc_destroy_container_on_signal(handler
, name
);
770 cgroup_destroy(handler
);
774 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
778 lxc_set_state(name
, handler
, ABORTING
);
779 if (handler
->pid
> 0)
780 kill(handler
->pid
, SIGKILL
);
781 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
786 #include <sys/reboot.h>
787 #include <linux/reboot.h>
789 /* reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL in a child pid namespace
790 * if container reboot support exists. Otherwise, it will either succeed or
793 static int container_reboot_supported(void *arg
)
799 if (ret
== -1 && errno
== EINVAL
)
804 static int must_drop_cap_sys_boot(struct lxc_conf
*conf
)
807 int ret
, cmd
, v
, flags
;
808 long stack_size
= 4096;
809 void *stack
= alloca(stack_size
);
813 f
= fopen("/proc/sys/kernel/ctrl-alt-del", "r");
815 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
819 ret
= fscanf(f
, "%d", &v
);
822 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del.");
825 cmd
= v
? LINUX_REBOOT_CMD_CAD_ON
: LINUX_REBOOT_CMD_CAD_OFF
;
827 flags
= CLONE_NEWPID
| SIGCHLD
;
828 if (!lxc_list_empty(&conf
->id_map
))
829 flags
|= CLONE_NEWUSER
;
832 pid
= __clone2(container_reboot_supported
, stack
, stack_size
, flags
, &cmd
);
835 pid
= clone(container_reboot_supported
, stack
, flags
, &cmd
);
838 if (flags
& CLONE_NEWUSER
)
839 ERROR("Failed to clone (%#x): %s (includes CLONE_NEWUSER).", flags
, strerror(errno
));
841 ERROR("Failed to clone (%#x): %s.", flags
, strerror(errno
));
844 if (wait(&status
) < 0) {
845 SYSERROR("Unexpected wait error: %s.", strerror(errno
));
849 if (WEXITSTATUS(status
) != 1)
855 /* netpipe is used in the unprivileged case to transfer the ifindexes from
858 static int netpipe
= -1;
860 static inline int count_veths(struct lxc_list
*network
)
862 struct lxc_list
*iterator
;
863 struct lxc_netdev
*netdev
;
866 lxc_list_for_each(iterator
, network
) {
867 netdev
= iterator
->elem
;
868 if (netdev
->type
!= LXC_NET_VETH
)
875 static int read_unpriv_netifindex(struct lxc_list
*network
)
877 struct lxc_list
*iterator
;
878 struct lxc_netdev
*netdev
;
882 lxc_list_for_each(iterator
, network
) {
883 netdev
= iterator
->elem
;
884 if (netdev
->type
!= LXC_NET_VETH
)
886 if (!(netdev
->name
= malloc(IFNAMSIZ
))) {
887 ERROR("Out of memory.");
891 if (read(netpipe
, netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
900 static int do_start(void *data
)
902 struct lxc_list
*iterator
;
903 struct lxc_handler
*handler
= data
;
904 int devnull_fd
= -1, ret
;
907 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
908 SYSERROR("Failed to set signal mask.");
912 /* This prctl must be before the synchro, so if the parent dies before
913 * we set the parent death signal, we will detect its death with the
914 * synchro right after, otherwise we have a window where the parent can
915 * exit before we set the pdeath signal leading to a unsupervized
918 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
919 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL.");
923 lxc_sync_fini_parent(handler
);
925 /* Don't leak the pinfd to the container. */
926 if (handler
->pinfd
>= 0) {
927 close(handler
->pinfd
);
930 if (lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
))
933 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
934 * https://github.com/lxc/lxd/issues/1978.
936 if ((handler
->clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
937 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
938 ret
= unshare(CLONE_NEWNET
);
940 SYSERROR("Failed to unshare CLONE_NEWNET.");
941 goto out_warn_father
;
943 INFO("Unshared CLONE_NEWNET.");
946 /* Tell the parent task it can begin to configure the container and wait
949 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
952 if (read_unpriv_netifindex(&handler
->conf
->network
) < 0)
953 goto out_warn_father
;
955 /* If we are in a new user namespace, become root there to have
956 * privilege over our namespace.
958 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
959 if (lxc_switch_uid_gid(0, 0) < 0)
960 goto out_warn_father
;
962 /* Drop groups only after we switched to a valid gid in the new
965 if (lxc_setgroups(0, NULL
) < 0)
966 goto out_warn_father
;
969 if (access(handler
->lxcpath
, X_OK
)) {
970 print_top_failing_dir(handler
->lxcpath
);
971 goto out_warn_father
;
975 if (handler
->conf
->need_utmp_watch
) {
976 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
977 SYSERROR("Failed to remove the CAP_SYS_BOOT capability.");
978 goto out_warn_father
;
980 DEBUG("Dropped the CAP_SYS_BOOT capability.");
984 ret
= snprintf(path
, sizeof(path
), "%s/dev/null", handler
->conf
->rootfs
.mount
);
985 if (ret
< 0 || ret
>= sizeof(path
))
986 goto out_warn_father
;
988 /* In order to checkpoint restore, we need to have everything in the
989 * same mount namespace. However, some containers may not have a
990 * reasonable /dev (in particular, they may not have /dev/null), so we
991 * can't set init's std fds to /dev/null by opening it from inside the
994 * If that's the case, fall back to using the host's /dev/null. This
995 * means that migration won't work, but at least we won't spew output
996 * where it isn't wanted.
998 if (handler
->backgrounded
&& !handler
->conf
->autodev
&& access(path
, F_OK
) < 0) {
999 devnull_fd
= open_devnull();
1002 goto out_warn_father
;
1003 WARN("Using /dev/null from the host for container init's "
1004 "standard file descriptors. Migration will not work.");
1007 /* Setup the container, ip, names, utsname, ... */
1008 if (lxc_setup(handler
)) {
1009 ERROR("Failed to setup container \"%s\".", handler
->name
);
1010 goto out_warn_father
;
1013 /* Ask father to setup cgroups and wait for him to finish. */
1014 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
1017 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1018 * earlier we end up with a wrong view of /proc/self/cgroup. For
1019 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1020 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1021 * /proc/self/cgroup would show us:
1025 * whereas it should actually show
1029 if (cgns_supported()) {
1030 if (unshare(CLONE_NEWCGROUP
) < 0) {
1031 INFO("Failed to unshare CLONE_NEWCGROUP.");
1032 goto out_warn_father
;
1034 INFO("Unshared CLONE_NEWCGROUP.");
1037 /* Set the label to change to when we exec(2) the container's init. */
1038 if (lsm_process_label_set(NULL
, handler
->conf
, 1, 1) < 0)
1039 goto out_warn_father
;
1041 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1042 * before we aren't allowed anymore.
1044 if (handler
->conf
->no_new_privs
) {
1045 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) < 0) {
1046 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
1047 goto out_warn_father
;
1049 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
1052 /* Some init's such as busybox will set sane tty settings on stdin,
1053 * stdout, stderr which it thinks is the console. We already set them
1054 * the way we wanted on the real terminal, and we want init to do its
1055 * setup on its console ie. the pty allocated in lxc_console_create() so
1056 * make sure that that pty is stdin,stdout,stderr.
1058 if (lxc_console_set_stdfds(handler
->conf
->console
.slave
) < 0)
1059 goto out_warn_father
;
1061 /* If we mounted a temporary proc, then unmount it now. */
1062 tmp_proc_unmount(handler
->conf
);
1064 if (lxc_seccomp_load(handler
->conf
) != 0)
1065 goto out_warn_father
;
1067 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, handler
->lxcpath
, NULL
)) {
1068 ERROR("Failed to run lxc.hook.start for container \"%s\".", handler
->name
);
1069 goto out_warn_father
;
1072 /* The container has been setup. We can now switch to an unprivileged
1075 if (handler
->conf
->is_execute
) {
1076 bool have_cap_setgid
;
1077 uid_t new_uid
= handler
->conf
->init_uid
;
1078 gid_t new_gid
= handler
->conf
->init_gid
;
1080 /* If we are in a new user namespace we already dropped all
1081 * groups when we switched to root in the new user namespace
1082 * further above. Only drop groups if we can, so ensure that we
1083 * have necessary privilege.
1086 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1088 have_cap_setgid
= false;
1090 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1091 if (lxc_setgroups(0, NULL
) < 0)
1092 goto out_warn_father
;
1095 if (lxc_switch_uid_gid(new_uid
, new_gid
) < 0)
1096 goto out_warn_father
;
1099 /* The clearenv() and putenv() calls have been moved here to allow us to
1100 * use environment variables passed to the various hooks, such as the
1101 * start hook above. Not all of the variables like CONFIG_PATH or ROOTFS
1102 * are valid in this context but others are.
1105 SYSERROR("Failed to clear environment.");
1106 /* Don't error out though. */
1109 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1110 if (putenv((char *)iterator
->elem
)) {
1111 SYSERROR("Failed to set environment variable: %s.", (char *)iterator
->elem
);
1112 goto out_warn_father
;
1116 if (putenv("container=lxc")) {
1117 SYSERROR("Failed to set environment variable: container=lxc.");
1118 goto out_warn_father
;
1121 if (handler
->conf
->pty_names
) {
1122 if (putenv(handler
->conf
->pty_names
)) {
1123 SYSERROR("Failed to set environment variable for container ptys.");
1124 goto out_warn_father
;
1128 close(handler
->sigfd
);
1130 if (devnull_fd
< 0) {
1131 devnull_fd
= open_devnull();
1134 goto out_warn_father
;
1137 if (handler
->backgrounded
&& set_stdfds(devnull_fd
))
1138 goto out_warn_father
;
1140 if (devnull_fd
>= 0) {
1147 /* After this call, we are in error because this ops should not return
1150 handler
->ops
->start(handler
, handler
->data
);
1153 /* We want the parent to know something went wrong, so we return a
1154 * special error code.
1156 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1159 if (devnull_fd
>= 0)
1165 static int save_phys_nics(struct lxc_conf
*conf
)
1167 struct lxc_list
*iterator
;
1168 int am_root
= (getuid() == 0);
1173 lxc_list_for_each(iterator
, &conf
->network
) {
1174 struct lxc_netdev
*netdev
= iterator
->elem
;
1176 if (netdev
->type
!= LXC_NET_PHYS
)
1178 conf
->saved_nics
= realloc(conf
->saved_nics
,
1179 (conf
->num_savednics
+1)*sizeof(struct saved_nic
));
1180 if (!conf
->saved_nics
)
1182 conf
->saved_nics
[conf
->num_savednics
].ifindex
= netdev
->ifindex
;
1183 conf
->saved_nics
[conf
->num_savednics
].orig_name
= strdup(netdev
->link
);
1184 if (!conf
->saved_nics
[conf
->num_savednics
].orig_name
)
1186 INFO("Stored saved_nic #%d idx %d name %s.", conf
->num_savednics
,
1187 conf
->saved_nics
[conf
->num_savednics
].ifindex
,
1188 conf
->saved_nics
[conf
->num_savednics
].orig_name
);
1189 conf
->num_savednics
++;
1195 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1199 struct lxc_pty_info
*pty_info
;
1201 int sock
= handler
->ttysock
[1];
1202 struct lxc_conf
*conf
= handler
->conf
;
1203 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
1204 size_t num_ttyfds
= (2 * conf
->tty
);
1209 tty_info
->pty_info
= malloc(sizeof(*tty_info
->pty_info
) * conf
->tty
);
1210 if (!tty_info
->pty_info
)
1213 ttyfds
= malloc(num_ttyfds
* sizeof(int));
1217 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, num_ttyfds
, NULL
, 0);
1218 for (i
= 0; (ret
>= 0 && *ttyfds
!= -1) && (i
< num_ttyfds
); i
++) {
1219 pty_info
= &tty_info
->pty_info
[i
/ 2];
1221 pty_info
->slave
= ttyfds
[i
++];
1222 pty_info
->master
= ttyfds
[i
];
1223 TRACE("received pty with master fd %d and slave fd %d from "
1224 "parent", pty_info
->master
, pty_info
->slave
);
1227 tty_info
->nbtty
= conf
->tty
;
1232 ERROR("failed to receive %d ttys from child: %s", conf
->tty
,
1235 TRACE("received %d ttys from child", conf
->tty
);
1240 void resolve_clone_flags(struct lxc_handler
*handler
)
1242 handler
->clone_flags
= CLONE_NEWPID
| CLONE_NEWNS
;
1244 if (!lxc_list_empty(&handler
->conf
->id_map
))
1245 handler
->clone_flags
|= CLONE_NEWUSER
;
1247 if (handler
->conf
->inherit_ns_fd
[LXC_NS_NET
] == -1) {
1248 if (!lxc_requests_empty_network(handler
))
1249 handler
->clone_flags
|= CLONE_NEWNET
;
1251 INFO("Inheriting a NET namespace.");
1254 if (handler
->conf
->inherit_ns_fd
[LXC_NS_IPC
] == -1)
1255 handler
->clone_flags
|= CLONE_NEWIPC
;
1257 INFO("Inheriting an IPC namespace.");
1259 if (handler
->conf
->inherit_ns_fd
[LXC_NS_UTS
] == -1)
1260 handler
->clone_flags
|= CLONE_NEWUTS
;
1262 INFO("Inheriting a UTS namespace.");
1265 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1266 * exec()s the requested container binary.
1267 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1268 * right here should be double checked if they'd pose a security risk. (For
1269 * example, any {u}mount() operations performed here will be reflected on the
1272 static int lxc_spawn(struct lxc_handler
*handler
)
1274 int failed_before_rename
= 0;
1275 const char *name
= handler
->name
;
1276 bool cgroups_connected
= false;
1277 int saved_ns_fd
[LXC_NS_MAX
];
1278 int preserve_mask
= 0, i
, flags
;
1279 int netpipepair
[2], nveths
;
1280 bool wants_to_map_ids
;
1281 struct lxc_list
*id_map
;
1284 id_map
= &handler
->conf
->id_map
;
1285 wants_to_map_ids
= !lxc_list_empty(id_map
);
1287 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1288 if (handler
->conf
->inherit_ns_fd
[i
] != -1)
1289 preserve_mask
|= ns_info
[i
].clone_flag
;
1291 if (lxc_sync_init(handler
))
1294 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, handler
->ttysock
) < 0) {
1295 lxc_sync_fini(handler
);
1299 resolve_clone_flags(handler
);
1301 if (handler
->clone_flags
& CLONE_NEWNET
) {
1302 if (!lxc_list_empty(&handler
->conf
->network
)) {
1304 /* Find gateway addresses from the link device, which is
1305 * no longer accessible inside the container. Do this
1306 * before creating network interfaces, since goto
1307 * out_delete_net does not work before lxc_clone.
1309 if (lxc_find_gateway_addresses(handler
)) {
1310 ERROR("Failed to find gateway addresses.");
1311 lxc_sync_fini(handler
);
1315 /* That should be done before the clone because we will
1316 * fill the netdev index and use them in the child.
1318 if (lxc_setup_networks_in_parent_namespaces(handler
)) {
1319 ERROR("Failed to create the network.");
1320 lxc_sync_fini(handler
);
1325 if (save_phys_nics(handler
->conf
)) {
1326 ERROR("Failed to save physical nic info.");
1331 if (!cgroup_init(handler
)) {
1332 ERROR("Failed initializing cgroup support.");
1333 goto out_delete_net
;
1336 cgroups_connected
= true;
1338 if (!cgroup_create(handler
)) {
1339 ERROR("Failed creating cgroups.");
1340 goto out_delete_net
;
1343 /* If the rootfs is not a blockdev, prevent the container from marking
1345 * If the container is unprivileged then skip rootfs pinning.
1347 if (!wants_to_map_ids
) {
1348 handler
->pinfd
= pin_rootfs(handler
->conf
->rootfs
.path
);
1349 if (handler
->pinfd
== -1)
1350 INFO("Failed to pin the rootfs for container \"%s\".", handler
->name
);
1353 if (!preserve_ns(saved_ns_fd
, preserve_mask
, getpid()))
1354 goto out_delete_net
;
1356 if (attach_ns(handler
->conf
->inherit_ns_fd
) < 0)
1357 goto out_delete_net
;
1359 if (am_unpriv() && (nveths
= count_veths(&handler
->conf
->network
))) {
1360 if (pipe(netpipepair
) < 0) {
1361 SYSERROR("Failed to create pipe.");
1362 goto out_delete_net
;
1364 /* Store netpipe in the global var for do_start's use. */
1365 netpipe
= netpipepair
[0];
1368 /* Create a process in a new set of namespaces. */
1369 flags
= handler
->clone_flags
;
1370 if (handler
->clone_flags
& CLONE_NEWUSER
) {
1371 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1372 * clone a new user namespace first and only later unshare our
1373 * network namespace to ensure that network devices ownership is
1376 flags
&= ~CLONE_NEWNET
;
1378 handler
->pid
= lxc_clone(do_start
, handler
, flags
);
1379 if (handler
->pid
< 0) {
1380 SYSERROR("Failed to clone a new set of namespaces.");
1381 goto out_delete_net
;
1383 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1384 if (flags
& ns_info
[i
].clone_flag
)
1385 INFO("Cloned %s.", ns_info
[i
].flag_name
);
1387 if (!preserve_ns(handler
->nsfd
, handler
->clone_flags
| preserve_mask
, handler
->pid
))
1388 INFO("Failed to preserve namespace for lxc.hook.stop.");
1390 if (attach_ns(saved_ns_fd
))
1391 WARN("Failed to restore saved namespaces.");
1393 lxc_sync_fini_child(handler
);
1395 /* Map the container uids. The container became an invalid userid the
1396 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1397 * anything immediately, but allows the container to setuid(0) (0 being
1398 * mapped to something else on the host.) later to become a valid uid
1401 if (wants_to_map_ids
&& lxc_map_ids(id_map
, handler
->pid
)) {
1402 ERROR("Failed to set up id mapping.");
1403 goto out_delete_net
;
1406 if (lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
)) {
1407 failed_before_rename
= 1;
1408 goto out_delete_net
;
1411 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
)) {
1412 failed_before_rename
= 1;
1413 goto out_delete_net
;
1416 if (!cgroup_create_legacy(handler
)) {
1417 ERROR("Failed to setup legacy cgroups for container \"%s\".", name
);
1418 goto out_delete_net
;
1420 if (!cgroup_setup_limits(handler
, false)) {
1421 ERROR("Failed to setup cgroup limits for container \"%s\".", name
);
1422 goto out_delete_net
;
1425 if (!cgroup_enter(handler
))
1426 goto out_delete_net
;
1428 if (!cgroup_chown(handler
))
1429 goto out_delete_net
;
1431 if (failed_before_rename
)
1432 goto out_delete_net
;
1434 /* Create the network configuration. */
1435 if (handler
->clone_flags
& CLONE_NEWNET
) {
1436 if (lxc_assign_network(handler
->lxcpath
, handler
->name
,
1437 &handler
->conf
->network
, handler
->pid
)) {
1438 ERROR("Failed to create the configured network.");
1439 goto out_delete_net
;
1443 if (netpipe
!= -1) {
1444 struct lxc_list
*iterator
;
1445 struct lxc_netdev
*netdev
;
1448 lxc_list_for_each(iterator
, &handler
->conf
->network
) {
1449 netdev
= iterator
->elem
;
1450 if (netdev
->type
!= LXC_NET_VETH
)
1452 if (write(netpipepair
[1], netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
1453 ERROR("Error writing veth name to container.");
1454 goto out_delete_net
;
1457 close(netpipepair
[1]);
1460 /* Tell the child to continue its initialization. We'll get
1461 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1463 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
1464 goto out_delete_net
;
1466 if (!lxc_list_empty(&handler
->conf
->limits
) && setup_resource_limits(&handler
->conf
->limits
, handler
->pid
)) {
1467 ERROR("failed to setup resource limits for '%s'", name
);
1468 goto out_delete_net
;
1471 if (!cgroup_setup_limits(handler
, true)) {
1472 ERROR("Failed to setup the devices cgroup for container \"%s\".", name
);
1473 goto out_delete_net
;
1476 cgroup_disconnect();
1477 cgroups_connected
= false;
1479 /* Read tty fds allocated by child. */
1480 if (lxc_recv_ttys_from_child(handler
) < 0) {
1481 ERROR("Failed to receive tty info from child process.");
1482 goto out_delete_net
;
1485 /* Tell the child to complete its initialization and wait for it to exec
1486 * or return an error. (The child will never return
1487 * LXC_SYNC_POST_CGROUP+1. It will either close the sync pipe, causing
1488 * lxc_sync_barrier_child to return success, or return a different
1489 * value, causing us to error out).
1491 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CGROUP
))
1494 if (handler
->ops
->post_start(handler
, handler
->data
))
1497 if (lxc_set_state(name
, handler
, RUNNING
)) {
1498 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
,
1499 lxc_state2str(RUNNING
));
1503 lxc_sync_fini(handler
);
1504 handler
->netnsfd
= lxc_preserve_ns(handler
->pid
, "net");
1509 if (cgroups_connected
)
1510 cgroup_disconnect();
1511 if (handler
->clone_flags
& CLONE_NEWNET
)
1512 lxc_delete_network(handler
);
1514 lxc_abort(name
, handler
);
1515 lxc_sync_fini(handler
);
1516 if (handler
->pinfd
>= 0) {
1517 close(handler
->pinfd
);
1518 handler
->pinfd
= -1;
1524 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1525 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1530 bool removed_all_netdevs
= true;
1531 struct lxc_conf
*conf
= handler
->conf
;
1533 if (lxc_init(name
, handler
) < 0) {
1534 ERROR("Failed to initialize container \"%s\".", name
);
1538 handler
->data
= data
;
1539 handler
->backgrounded
= backgrounded
;
1540 handler
->netnsfd
= -1;
1542 if (must_drop_cap_sys_boot(handler
->conf
)) {
1544 DEBUG("Dropping CAP_SYS_BOOT capability.");
1546 DEBUG("Not dropping CAP_SYS_BOOT capability as capabilities aren't supported.");
1549 DEBUG("Not dropping CAP_SYS_BOOT or watching utmp.");
1550 handler
->conf
->need_utmp_watch
= 0;
1553 if (!attach_block_device(handler
->conf
)) {
1554 ERROR("Failed to attach block device.");
1555 goto out_fini_nonet
;
1558 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1559 /* If the backing store is a device, mount it here and now. */
1560 if (rootfs_is_blockdev(conf
)) {
1561 if (unshare(CLONE_NEWNS
) < 0) {
1562 ERROR("Failed to unshare CLONE_NEWNS.");
1563 goto out_fini_nonet
;
1565 INFO("Unshared CLONE_NEWNS.");
1567 remount_all_slave();
1568 if (do_rootfs_setup(conf
, name
, lxcpath
) < 0) {
1569 ERROR("Error setting up rootfs mount as root before spawn.");
1570 goto out_fini_nonet
;
1572 INFO("Set up container rootfs as host root.");
1576 err
= lxc_spawn(handler
);
1578 ERROR("Failed to spawn container \"%s\".", name
);
1579 goto out_detach_blockdev
;
1582 handler
->conf
->reboot
= 0;
1584 err
= lxc_poll(name
, handler
);
1586 ERROR("LXC mainloop exited with error: %d.", err
);
1587 if (handler
->netnsfd
>= 0) {
1588 close(handler
->netnsfd
);
1589 handler
->netnsfd
= -1;
1594 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
1597 /* If the child process exited but was not signaled, it didn't call
1598 * reboot. This should mean it was an lxc-execute which simply exited.
1599 * In any case, treat it as a 'halt'.
1601 if (WIFSIGNALED(status
)) {
1602 switch(WTERMSIG(status
)) {
1603 case SIGINT
: /* halt */
1604 DEBUG("Container \"%s\" is halting.", name
);
1606 case SIGHUP
: /* reboot */
1607 DEBUG("Container \"%s\" is rebooting.", name
);
1608 handler
->conf
->reboot
= 1;
1610 case SIGSYS
: /* seccomp */
1611 DEBUG("Container \"%s\" violated its seccomp policy.", name
);
1614 DEBUG("Unknown exit status for container \"%s\" init %d.", name
, WTERMSIG(status
));
1619 DEBUG("Pushing physical nics back to host namespace");
1620 lxc_restore_phys_nics_to_netns(handler
->netnsfd
, handler
->conf
);
1622 DEBUG("Tearing down virtual network devices used by container \"%s\".", name
);
1623 removed_all_netdevs
= lxc_delete_network(handler
);
1625 if (handler
->pinfd
>= 0) {
1626 close(handler
->pinfd
);
1627 handler
->pinfd
= -1;
1630 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1631 err
= lxc_error_set_and_log(handler
->pid
, status
);
1633 if (!removed_all_netdevs
) {
1634 DEBUG("Failed tearing down network devices used by container. Trying again!");
1635 removed_all_netdevs
= lxc_delete_network(handler
);
1636 if (!removed_all_netdevs
)
1637 DEBUG("Failed tearing down network devices used by container. Not trying again!");
1640 out_detach_blockdev
:
1641 detach_block_device(handler
->conf
);
1644 lxc_fini(name
, handler
);
1648 lxc_abort(name
, handler
);
1656 static int start(struct lxc_handler
*handler
, void* data
)
1658 struct start_args
*arg
= data
;
1660 NOTICE("Exec'ing \"%s\".", arg
->argv
[0]);
1662 execvp(arg
->argv
[0], arg
->argv
);
1663 SYSERROR("Failed to exec \"%s\".", arg
->argv
[0]);
1667 static int post_start(struct lxc_handler
*handler
, void* data
)
1669 struct start_args
*arg
= data
;
1671 NOTICE("Started \"%s\" with pid \"%d\".", arg
->argv
[0], handler
->pid
);
1675 static struct lxc_operations start_ops
= {
1677 .post_start
= post_start
1680 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
1681 const char *lxcpath
, bool backgrounded
)
1683 struct start_args start_arg
= {
1687 handler
->conf
->need_utmp_watch
= 1;
1688 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
);
1691 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
1694 char destroy
[MAXPATHLEN
];
1697 struct lxc_container
*c
;
1698 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
1699 bret
= do_destroy_container(handler
->conf
);
1701 ERROR("Error destroying rootfs for container \"%s\".", name
);
1705 INFO("Destroyed rootfs for container \"%s\".", name
);
1707 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
1708 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1709 ERROR("Error destroying directory for container \"%s\".", name
);
1713 c
= lxc_container_new(name
, handler
->lxcpath
);
1715 if (container_disk_lock(c
)) {
1716 INFO("Could not update lxc_snapshots file.");
1717 lxc_container_put(c
);
1719 mod_all_rdeps(c
, false);
1720 container_disk_unlock(c
);
1721 lxc_container_put(c
);
1726 ret
= userns_exec_1(handler
->conf
, lxc_rmdir_onedev_wrapper
,
1727 destroy
, "lxc_rmdir_onedev_wrapper");
1729 ret
= lxc_rmdir_onedev(destroy
, NULL
);
1732 ERROR("Error destroying directory for container \"%s\".", name
);
1735 INFO("Destroyed directory for container \"%s\".", name
);
1738 static int lxc_rmdir_onedev_wrapper(void *data
)
1740 char *arg
= (char *) data
;
1741 return lxc_rmdir_onedev(arg
, NULL
);
1744 static bool do_destroy_container(struct lxc_conf
*conf
) {
1746 if (userns_exec_1(conf
, bdev_destroy_wrapper
, conf
,
1747 "bdev_destroy_wrapper") < 0)
1751 return bdev_destroy(conf
);