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
70 #include "commands_utils.h"
75 #include "lxccontainer.h"
77 #include "lxcseccomp.h"
81 #include "namespace.h"
83 #include "storage_utils.h"
88 lxc_log_define(lxc_start
, lxc
);
90 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
91 static bool do_destroy_container(struct lxc_conf
*conf
);
92 static int lxc_rmdir_onedev_wrapper(void *data
);
93 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
96 static void print_top_failing_dir(const char *path
)
98 size_t len
= strlen(path
);
99 char *copy
= alloca(len
+ 1), *p
, *e
, saved
;
105 while (p
< e
&& *p
== '/')
107 while (p
< e
&& *p
!= '/')
111 if (access(copy
, X_OK
)) {
112 SYSERROR("Could not access %s. Please grant it x "
113 "access, or add an ACL for the container "
121 static void close_ns(int ns_fd
[LXC_NS_MAX
])
125 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
133 /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified
135 * Return true on success, false on failure.
137 static bool preserve_ns(int ns_fd
[LXC_NS_MAX
], int clone_flags
, pid_t pid
)
141 for (i
= 0; i
< LXC_NS_MAX
; i
++)
144 ret
= lxc_preserve_ns(pid
, "");
146 SYSERROR("Kernel does not support attaching to namespaces.");
152 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
153 if ((clone_flags
& ns_info
[i
].clone_flag
) == 0)
155 ns_fd
[i
] = lxc_preserve_ns(pid
, ns_info
[i
].proc_name
);
164 SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info
[i
].proc_name
);
166 SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info
[i
].proc_name
, strerror(errno
));
171 static int attach_ns(const int ns_fd
[LXC_NS_MAX
]) {
174 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
178 if (setns(ns_fd
[i
], 0) != 0)
184 SYSERROR("Failed to attach %s namespace.", ns_info
[i
].proc_name
);
188 static int match_fd(int fd
)
190 return (fd
== 0 || fd
== 1 || fd
== 2);
193 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
194 int *fds_to_ignore
, size_t len_fds
)
196 struct dirent
*direntp
;
201 if (conf
&& conf
->close_all_fds
)
205 dir
= opendir("/proc/self/fd");
207 WARN("Failed to open directory: %s.", strerror(errno
));
213 while ((direntp
= readdir(dir
))) {
217 if (!strcmp(direntp
->d_name
, "."))
220 if (!strcmp(direntp
->d_name
, ".."))
223 if (lxc_safe_int(direntp
->d_name
, &fd
) < 0) {
224 INFO("Could not parse file descriptor for: %s", direntp
->d_name
);
228 for (i
= 0; i
< len_fds
; i
++)
229 if (fds_to_ignore
[i
] == fd
)
232 if (fd
== fddir
|| fd
== lxc_log_fd
||
233 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
236 if (current_config
&& fd
== current_config
->logfd
)
245 INFO("Closed inherited fd: %d.", fd
);
248 WARN("Inherited fd: %d.", fd
);
251 /* Only enable syslog at this point to avoid the above logging function
252 * to open a new fd and make the check_inherited function enter an
255 lxc_log_enable_syslog();
257 closedir(dir
); /* cannot fail */
261 static int setup_signal_fd(sigset_t
*oldmask
)
266 /* Block everything except serious error signals. */
267 if (sigfillset(&mask
) ||
268 sigdelset(&mask
, SIGILL
) ||
269 sigdelset(&mask
, SIGSEGV
) ||
270 sigdelset(&mask
, SIGBUS
) ||
271 sigdelset(&mask
, SIGWINCH
) ||
272 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
273 SYSERROR("Failed to set signal mask.");
277 fd
= signalfd(-1, &mask
, 0);
279 SYSERROR("Failed to create signal file descriptor.");
283 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
284 SYSERROR("Failed to set FD_CLOEXEC on the signal file descriptor: %d.", fd
);
289 DEBUG("Set SIGCHLD handler with file descriptor: %d.", fd
);
294 static int signal_handler(int fd
, uint32_t events
, void *data
,
295 struct lxc_epoll_descr
*descr
)
297 struct signalfd_siginfo siginfo
;
301 bool init_died
= false;
303 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
305 ERROR("Failed to read signal info from signal file descriptor: %d.", fd
);
309 if (ret
!= sizeof(siginfo
)) {
310 ERROR("Unexpected size for siginfo struct.");
314 /* Check whether init is running. */
316 ret
= waitid(P_PID
, *pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
317 if (ret
== 0 && info
.si_pid
== *pid
)
320 if (siginfo
.ssi_signo
!= SIGCHLD
) {
321 kill(*pid
, siginfo
.ssi_signo
);
322 INFO("Forwarded signal %d to pid %d.", siginfo
.ssi_signo
, *pid
);
323 return init_died
? 1 : 0;
326 if (siginfo
.ssi_code
== CLD_STOPPED
) {
327 INFO("Container init process was stopped.");
328 return init_died
? 1 : 0;
329 } else if (siginfo
.ssi_code
== CLD_CONTINUED
) {
330 INFO("Container init process was continued.");
331 return init_died
? 1 : 0;
334 /* More robustness, protect ourself from a SIGCHLD sent
335 * by a process different from the container init.
337 if (siginfo
.ssi_pid
!= *pid
) {
338 NOTICE("Received SIGCHLD from pid %d instead of container init %d.", siginfo
.ssi_pid
, *pid
);
339 return init_died
? 1 : 0;
342 DEBUG("Container init process %d exited.", *pid
);
346 static int lxc_serve_state_clients(const char *name
,
347 struct lxc_handler
*handler
,
351 struct lxc_list
*cur
, *next
;
352 struct state_client
*client
;
353 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
357 /* Only set state under process lock held so that we don't cause
358 * lxc_cmd_add_state_client() to miss a state.
360 handler
->state
= state
;
361 TRACE("set container state to %s", lxc_state2str(state
));
363 if (lxc_list_empty(&handler
->state_clients
)) {
364 TRACE("no state clients registered");
366 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
370 strncpy(msg
.name
, name
, sizeof(msg
.name
));
371 msg
.name
[sizeof(msg
.name
) - 1] = 0;
373 lxc_list_for_each_safe(cur
, &handler
->state_clients
, next
) {
376 if (!client
->states
[state
]) {
377 TRACE("state %s not registered for state client %d",
378 lxc_state2str(state
), client
->clientfd
);
382 TRACE("sending state %s to state client %d",
383 lxc_state2str(state
), client
->clientfd
);
386 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), 0);
388 if (errno
== EINTR
) {
389 TRACE("Caught EINTR; retrying");
393 ERROR("failed to send message to client");
396 /* kick client from list */
397 close(client
->clientfd
);
407 static int lxc_serve_state_socket_pair(const char *name
,
408 struct lxc_handler
*handler
,
413 if (!handler
->backgrounded
||
414 handler
->state_socket_pair
[1] < 0 ||
418 /* Close read end of the socket pair. */
419 close(handler
->state_socket_pair
[0]);
420 handler
->state_socket_pair
[0] = -1;
423 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
424 &(int){state
}, sizeof(int));
425 if (ret
!= sizeof(int)) {
428 SYSERROR("Failed to send state to %d",
429 handler
->state_socket_pair
[1]);
433 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
434 handler
->state_socket_pair
[1]);
436 /* Close write end of the socket pair. */
437 close(handler
->state_socket_pair
[1]);
438 handler
->state_socket_pair
[1] = -1;
443 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
448 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
450 ERROR("Failed to synchronize via anonymous pair of unix sockets");
454 ret
= lxc_serve_state_clients(name
, handler
, state
);
458 /* This function will try to connect to the legacy lxc-monitord state
459 * server and only exists for backwards compatibility.
461 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
466 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
468 int sigfd
= handler
->sigfd
;
469 int pid
= handler
->pid
;
470 struct lxc_epoll_descr descr
;
472 if (lxc_mainloop_open(&descr
)) {
473 ERROR("Failed to create LXC mainloop.");
477 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
478 ERROR("Failed to add signal handler with file descriptor %d to LXC mainloop.", sigfd
);
479 goto out_mainloop_open
;
482 if (lxc_console_mainloop_add(&descr
, handler
->conf
)) {
483 ERROR("Failed to add console handler to LXC mainloop.");
484 goto out_mainloop_open
;
487 if (lxc_cmd_mainloop_add(name
, &descr
, handler
)) {
488 ERROR("Failed to add command handler to LXC mainloop.");
489 goto out_mainloop_open
;
492 if (handler
->conf
->need_utmp_watch
) {
494 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
495 ERROR("Failed to add utmp handler to LXC mainloop.");
496 goto out_mainloop_open
;
499 DEBUG("Not starting utmp handler as CAP_SYS_BOOT cannot be dropped without capabilities support.");
502 TRACE("lxc mainloop is ready");
504 return lxc_mainloop(&descr
, -1);
507 lxc_mainloop_close(&descr
);
515 void lxc_free_handler(struct lxc_handler
*handler
)
517 if (handler
->conf
&& handler
->conf
->maincmd_fd
)
518 close(handler
->conf
->maincmd_fd
);
520 if (handler
->state_socket_pair
[0] >= 0)
521 close(handler
->state_socket_pair
[0]);
523 if (handler
->state_socket_pair
[1] >= 0)
524 close(handler
->state_socket_pair
[1]);
529 handler
->conf
= NULL
;
533 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
534 const char *lxcpath
, bool daemonize
)
537 struct lxc_handler
*handler
;
539 handler
= malloc(sizeof(*handler
));
541 ERROR("failed to allocate memory");
545 memset(handler
, 0, sizeof(*handler
));
547 handler
->ttysock
[0] = handler
->ttysock
[1] = -1;
548 handler
->conf
= conf
;
549 handler
->lxcpath
= lxcpath
;
551 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
552 lxc_list_init(&handler
->state_clients
);
554 for (i
= 0; i
< LXC_NS_MAX
; i
++)
555 handler
->nsfd
[i
] = -1;
557 handler
->name
= strdup(name
);
558 if (!handler
->name
) {
559 ERROR("failed to allocate memory");
563 if (daemonize
&& !handler
->conf
->reboot
) {
564 /* Create socketpair() to synchronize on daemonized startup.
565 * When the container reboots we don't need to synchronize again
566 * currently so don't open another socketpair().
568 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
569 handler
->state_socket_pair
);
571 ERROR("Failed to create anonymous pair of unix sockets");
574 TRACE("Created anonymous pair {%d,%d} of unix sockets",
575 handler
->state_socket_pair
[0],
576 handler
->state_socket_pair
[1]);
579 if (lxc_cmd_init(name
, handler
, lxcpath
)) {
580 ERROR("failed to set up command socket");
584 TRACE("unix domain socket %d for command server is ready",
585 handler
->conf
->maincmd_fd
);
590 lxc_free_handler(handler
);
595 int lxc_init(const char *name
, struct lxc_handler
*handler
)
597 struct lxc_conf
*conf
= handler
->conf
;
600 TRACE("initialized LSM");
602 if (lxc_read_seccomp_config(conf
) != 0) {
603 ERROR("Failed loading seccomp policy.");
604 goto out_close_maincmd_fd
;
606 TRACE("read seccomp policy");
608 /* Begin by setting the state to STARTING. */
609 if (lxc_set_state(name
, handler
, STARTING
)) {
610 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
, lxc_state2str(STARTING
));
611 goto out_close_maincmd_fd
;
613 TRACE("set container state to \"STARTING\"");
615 /* Start of environment variable setup for hooks. */
616 if (name
&& setenv("LXC_NAME", name
, 1))
617 SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name
);
619 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
620 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf
->rcfile
);
622 if (conf
->rootfs
.mount
&& setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1))
623 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf
->rootfs
.mount
);
625 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
626 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf
->rootfs
.path
);
628 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1))
629 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf
->console
.path
);
631 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1))
632 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf
->console
.log_path
);
634 if (setenv("LXC_CGNS_AWARE", "1", 1))
635 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1.");
636 /* End of environment variable setup for hooks. */
638 TRACE("set environment variables");
640 if (run_lxc_hooks(name
, "pre-start", conf
, handler
->lxcpath
, NULL
)) {
641 ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name
);
644 TRACE("ran pre-start hooks");
646 /* The signal fd has to be created before forking otherwise if the child
647 * process exits before we setup the signal fd, the event will be lost
648 * and the command will be stuck.
650 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
651 if (handler
->sigfd
< 0) {
652 ERROR("Failed to setup SIGCHLD fd handler.");
655 TRACE("set up signal fd");
657 /* Do this after setting up signals since it might unblock SIGWINCH. */
658 if (lxc_console_create(conf
)) {
659 ERROR("Failed to create console for container \"%s\".", name
);
660 goto out_restore_sigmask
;
662 TRACE("created console");
664 if (lxc_ttys_shift_ids(conf
) < 0) {
665 ERROR("Failed to shift tty into container.");
666 goto out_restore_sigmask
;
668 TRACE("shifted tty ids");
670 INFO("container \"%s\" is initialized", name
);
674 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
676 lxc_delete_tty(&conf
->tty_info
);
678 lxc_set_state(name
, handler
, ABORTING
);
679 out_close_maincmd_fd
:
680 close(conf
->maincmd_fd
);
681 conf
->maincmd_fd
= -1;
685 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
688 struct lxc_list
*cur
, *next
;
689 pid_t self
= getpid();
690 char *namespaces
[LXC_NS_MAX
+ 1];
691 size_t namespace_count
= 0;
693 /* The STOPPING state is there for future cleanup code which can take
696 lxc_set_state(name
, handler
, STOPPING
);
698 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
699 if (handler
->nsfd
[i
] != -1) {
700 rc
= asprintf(&namespaces
[namespace_count
], "%s:/proc/%d/fd/%d",
701 ns_info
[i
].proc_name
, self
, handler
->nsfd
[i
]);
703 SYSERROR("Failed to allocate memory.");
709 namespaces
[namespace_count
] = NULL
;
711 if (handler
->conf
->reboot
&& setenv("LXC_TARGET", "reboot", 1))
712 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot.");
714 if (!handler
->conf
->reboot
&& setenv("LXC_TARGET", "stop", 1))
715 SYSERROR("Failed to set environment variable: LXC_TARGET=stop.");
717 if (run_lxc_hooks(name
, "stop", handler
->conf
, handler
->lxcpath
, namespaces
))
718 ERROR("Failed to run lxc.hook.stop for container \"%s\".", name
);
720 while (namespace_count
--)
721 free(namespaces
[namespace_count
]);
722 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
723 if (handler
->nsfd
[i
] != -1) {
724 close(handler
->nsfd
[i
]);
725 handler
->nsfd
[i
] = -1;
729 if (handler
->netnsfd
>= 0) {
730 close(handler
->netnsfd
);
731 handler
->netnsfd
= -1;
734 lxc_set_state(name
, handler
, STOPPED
);
736 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, handler
->lxcpath
, NULL
)) {
737 ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name
);
738 if (handler
->conf
->reboot
) {
739 WARN("Container will be stopped instead of rebooted.");
740 handler
->conf
->reboot
= 0;
741 if (setenv("LXC_TARGET", "stop", 1))
742 WARN("Failed to set environment variable: LXC_TARGET=stop.");
746 /* Reset mask set by setup_signal_fd. */
747 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
748 WARN("Failed to restore signal mask.");
750 lxc_console_delete(&handler
->conf
->console
);
751 lxc_delete_tty(&handler
->conf
->tty_info
);
753 /* close the command socket */
754 close(handler
->conf
->maincmd_fd
);
755 handler
->conf
->maincmd_fd
= -1;
757 /* The command socket is now closed, no more state clients can register
758 * themselves from now on. So free the list of state clients.
760 lxc_list_for_each_safe(cur
, &handler
->state_clients
, next
) {
761 struct state_client
*client
= cur
->elem
;
762 /* close state client socket */
763 close(client
->clientfd
);
770 if (handler
->ttysock
[0] != -1) {
771 close(handler
->ttysock
[0]);
772 close(handler
->ttysock
[1]);
775 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= 1)
776 lxc_destroy_container_on_signal(handler
, name
);
778 cgroup_destroy(handler
);
782 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
786 lxc_set_state(name
, handler
, ABORTING
);
787 if (handler
->pid
> 0)
788 kill(handler
->pid
, SIGKILL
);
789 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
794 #include <sys/reboot.h>
795 #include <linux/reboot.h>
797 /* reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL in a child pid namespace
798 * if container reboot support exists. Otherwise, it will either succeed or
801 static int container_reboot_supported(void *arg
)
807 if (ret
== -1 && errno
== EINVAL
)
812 static int must_drop_cap_sys_boot(struct lxc_conf
*conf
)
815 int ret
, cmd
, v
, flags
;
816 long stack_size
= 4096;
817 void *stack
= alloca(stack_size
);
821 f
= fopen("/proc/sys/kernel/ctrl-alt-del", "r");
823 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
827 ret
= fscanf(f
, "%d", &v
);
830 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del.");
833 cmd
= v
? LINUX_REBOOT_CMD_CAD_ON
: LINUX_REBOOT_CMD_CAD_OFF
;
835 flags
= CLONE_NEWPID
| SIGCHLD
;
836 if (!lxc_list_empty(&conf
->id_map
))
837 flags
|= CLONE_NEWUSER
;
840 pid
= __clone2(container_reboot_supported
, stack
, stack_size
, flags
, &cmd
);
843 pid
= clone(container_reboot_supported
, stack
, flags
, &cmd
);
846 if (flags
& CLONE_NEWUSER
)
847 ERROR("Failed to clone (%#x): %s (includes CLONE_NEWUSER).", flags
, strerror(errno
));
849 ERROR("Failed to clone (%#x): %s.", flags
, strerror(errno
));
852 if (wait(&status
) < 0) {
853 SYSERROR("Unexpected wait error: %s.", strerror(errno
));
857 if (WEXITSTATUS(status
) != 1)
863 /* netpipe is used in the unprivileged case to transfer the ifindexes from
866 static int netpipe
= -1;
868 static inline int count_veths(struct lxc_list
*network
)
870 struct lxc_list
*iterator
;
871 struct lxc_netdev
*netdev
;
874 lxc_list_for_each(iterator
, network
) {
875 netdev
= iterator
->elem
;
876 if (netdev
->type
!= LXC_NET_VETH
)
883 static int read_unpriv_netifindex(struct lxc_list
*network
)
885 struct lxc_list
*iterator
;
886 struct lxc_netdev
*netdev
;
890 lxc_list_for_each(iterator
, network
) {
891 netdev
= iterator
->elem
;
892 if (netdev
->type
!= LXC_NET_VETH
)
894 if (!(netdev
->name
= malloc(IFNAMSIZ
))) {
895 ERROR("Out of memory.");
899 if (read(netpipe
, netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
908 static int do_start(void *data
)
910 struct lxc_list
*iterator
;
911 struct lxc_handler
*handler
= data
;
912 int devnull_fd
= -1, ret
;
915 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
916 SYSERROR("Failed to set signal mask.");
920 /* This prctl must be before the synchro, so if the parent dies before
921 * we set the parent death signal, we will detect its death with the
922 * synchro right after, otherwise we have a window where the parent can
923 * exit before we set the pdeath signal leading to a unsupervized
926 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
927 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL.");
931 lxc_sync_fini_parent(handler
);
933 /* Don't leak the pinfd to the container. */
934 if (handler
->pinfd
>= 0) {
935 close(handler
->pinfd
);
938 if (lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
))
941 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
942 * https://github.com/lxc/lxd/issues/1978.
944 if ((handler
->clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
945 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
946 ret
= unshare(CLONE_NEWNET
);
948 SYSERROR("Failed to unshare CLONE_NEWNET.");
949 goto out_warn_father
;
951 INFO("Unshared CLONE_NEWNET.");
954 /* Tell the parent task it can begin to configure the container and wait
957 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
960 if (read_unpriv_netifindex(&handler
->conf
->network
) < 0)
961 goto out_warn_father
;
963 /* If we are in a new user namespace, become root there to have
964 * privilege over our namespace.
966 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
967 if (lxc_switch_uid_gid(0, 0) < 0)
968 goto out_warn_father
;
970 /* Drop groups only after we switched to a valid gid in the new
973 if (lxc_setgroups(0, NULL
) < 0)
974 goto out_warn_father
;
977 if (access(handler
->lxcpath
, X_OK
)) {
978 print_top_failing_dir(handler
->lxcpath
);
979 goto out_warn_father
;
983 if (handler
->conf
->need_utmp_watch
) {
984 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
985 SYSERROR("Failed to remove the CAP_SYS_BOOT capability.");
986 goto out_warn_father
;
988 DEBUG("Dropped the CAP_SYS_BOOT capability.");
992 ret
= snprintf(path
, sizeof(path
), "%s/dev/null", handler
->conf
->rootfs
.mount
);
993 if (ret
< 0 || ret
>= sizeof(path
))
994 goto out_warn_father
;
996 /* In order to checkpoint restore, we need to have everything in the
997 * same mount namespace. However, some containers may not have a
998 * reasonable /dev (in particular, they may not have /dev/null), so we
999 * can't set init's std fds to /dev/null by opening it from inside the
1002 * If that's the case, fall back to using the host's /dev/null. This
1003 * means that migration won't work, but at least we won't spew output
1004 * where it isn't wanted.
1006 if (handler
->backgrounded
&& !handler
->conf
->autodev
&& access(path
, F_OK
) < 0) {
1007 devnull_fd
= open_devnull();
1010 goto out_warn_father
;
1011 WARN("Using /dev/null from the host for container init's "
1012 "standard file descriptors. Migration will not work.");
1015 /* Ask father to setup cgroups and wait for him to finish. */
1016 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
1019 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1020 * earlier we end up with a wrong view of /proc/self/cgroup. For
1021 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1022 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1023 * /proc/self/cgroup would show us:
1027 * whereas it should actually show
1031 if (cgns_supported()) {
1032 if (unshare(CLONE_NEWCGROUP
) < 0) {
1033 INFO("Failed to unshare CLONE_NEWCGROUP.");
1034 goto out_warn_father
;
1036 INFO("Unshared CLONE_NEWCGROUP.");
1039 /* Setup the container, ip, names, utsname, ... */
1040 if (lxc_setup(handler
)) {
1041 ERROR("Failed to setup container \"%s\".", handler
->name
);
1042 goto out_warn_father
;
1045 /* Set the label to change to when we exec(2) the container's init. */
1046 if (lsm_process_label_set(NULL
, handler
->conf
, 1, 1) < 0)
1047 goto out_warn_father
;
1049 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1050 * before we aren't allowed anymore.
1052 if (handler
->conf
->no_new_privs
) {
1053 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) < 0) {
1054 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
1055 goto out_warn_father
;
1057 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
1060 /* Some init's such as busybox will set sane tty settings on stdin,
1061 * stdout, stderr which it thinks is the console. We already set them
1062 * the way we wanted on the real terminal, and we want init to do its
1063 * setup on its console ie. the pty allocated in lxc_console_create() so
1064 * make sure that that pty is stdin,stdout,stderr.
1066 if (handler
->conf
->console
.slave
>= 0)
1067 if (set_stdfds(handler
->conf
->console
.slave
) < 0) {
1068 ERROR("Failed to redirect std{in,out,err} to pty file "
1070 handler
->conf
->console
.slave
);
1071 goto out_warn_father
;
1074 /* If we mounted a temporary proc, then unmount it now. */
1075 tmp_proc_unmount(handler
->conf
);
1077 if (lxc_seccomp_load(handler
->conf
) != 0)
1078 goto out_warn_father
;
1080 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, handler
->lxcpath
, NULL
)) {
1081 ERROR("Failed to run lxc.hook.start for container \"%s\".", handler
->name
);
1082 goto out_warn_father
;
1085 /* The container has been setup. We can now switch to an unprivileged
1088 if (handler
->conf
->is_execute
) {
1089 bool have_cap_setgid
;
1090 uid_t new_uid
= handler
->conf
->init_uid
;
1091 gid_t new_gid
= handler
->conf
->init_gid
;
1093 /* If we are in a new user namespace we already dropped all
1094 * groups when we switched to root in the new user namespace
1095 * further above. Only drop groups if we can, so ensure that we
1096 * have necessary privilege.
1099 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1101 have_cap_setgid
= false;
1103 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1104 if (lxc_setgroups(0, NULL
) < 0)
1105 goto out_warn_father
;
1108 if (lxc_switch_uid_gid(new_uid
, new_gid
) < 0)
1109 goto out_warn_father
;
1112 /* The clearenv() and putenv() calls have been moved here to allow us to
1113 * use environment variables passed to the various hooks, such as the
1114 * start hook above. Not all of the variables like CONFIG_PATH or ROOTFS
1115 * are valid in this context but others are.
1118 SYSERROR("Failed to clear environment.");
1119 /* Don't error out though. */
1122 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1123 if (putenv((char *)iterator
->elem
)) {
1124 SYSERROR("Failed to set environment variable: %s.", (char *)iterator
->elem
);
1125 goto out_warn_father
;
1129 if (putenv("container=lxc")) {
1130 SYSERROR("Failed to set environment variable: container=lxc.");
1131 goto out_warn_father
;
1134 if (handler
->conf
->pty_names
) {
1135 if (putenv(handler
->conf
->pty_names
)) {
1136 SYSERROR("Failed to set environment variable for container ptys.");
1137 goto out_warn_father
;
1141 close(handler
->sigfd
);
1143 if (devnull_fd
< 0) {
1144 devnull_fd
= open_devnull();
1147 goto out_warn_father
;
1150 if (handler
->conf
->console
.slave
< 0 && handler
->backgrounded
)
1151 if (set_stdfds(devnull_fd
) < 0) {
1152 ERROR("Failed to redirect std{in,out,err} to "
1154 goto out_warn_father
;
1157 if (devnull_fd
>= 0) {
1164 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
))
1165 goto out_warn_father
;
1167 /* After this call, we are in error because this ops should not return
1170 handler
->ops
->start(handler
, handler
->data
);
1173 /* We want the parent to know something went wrong, so we return a
1174 * special error code.
1176 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1179 if (devnull_fd
>= 0)
1185 static int save_phys_nics(struct lxc_conf
*conf
)
1187 struct lxc_list
*iterator
;
1188 int am_root
= (getuid() == 0);
1193 lxc_list_for_each(iterator
, &conf
->network
) {
1194 struct lxc_netdev
*netdev
= iterator
->elem
;
1196 if (netdev
->type
!= LXC_NET_PHYS
)
1198 conf
->saved_nics
= realloc(conf
->saved_nics
,
1199 (conf
->num_savednics
+1)*sizeof(struct saved_nic
));
1200 if (!conf
->saved_nics
)
1202 conf
->saved_nics
[conf
->num_savednics
].ifindex
= netdev
->ifindex
;
1203 conf
->saved_nics
[conf
->num_savednics
].orig_name
= strdup(netdev
->link
);
1204 if (!conf
->saved_nics
[conf
->num_savednics
].orig_name
)
1206 INFO("Stored saved_nic #%d idx %d name %s.", conf
->num_savednics
,
1207 conf
->saved_nics
[conf
->num_savednics
].ifindex
,
1208 conf
->saved_nics
[conf
->num_savednics
].orig_name
);
1209 conf
->num_savednics
++;
1215 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1219 struct lxc_pty_info
*pty_info
;
1221 int sock
= handler
->ttysock
[1];
1222 struct lxc_conf
*conf
= handler
->conf
;
1223 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
1224 size_t num_ttyfds
= (2 * conf
->tty
);
1229 tty_info
->pty_info
= malloc(sizeof(*tty_info
->pty_info
) * conf
->tty
);
1230 if (!tty_info
->pty_info
)
1233 ttyfds
= malloc(num_ttyfds
* sizeof(int));
1237 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, num_ttyfds
, NULL
, 0);
1238 for (i
= 0; (ret
>= 0 && *ttyfds
!= -1) && (i
< num_ttyfds
); i
++) {
1239 pty_info
= &tty_info
->pty_info
[i
/ 2];
1241 pty_info
->slave
= ttyfds
[i
++];
1242 pty_info
->master
= ttyfds
[i
];
1243 TRACE("received pty with master fd %d and slave fd %d from "
1244 "parent", pty_info
->master
, pty_info
->slave
);
1247 tty_info
->nbtty
= conf
->tty
;
1252 ERROR("failed to receive %d ttys from child: %s", conf
->tty
,
1255 TRACE("received %d ttys from child", conf
->tty
);
1260 void resolve_clone_flags(struct lxc_handler
*handler
)
1262 handler
->clone_flags
= CLONE_NEWPID
| CLONE_NEWNS
;
1264 if (!lxc_list_empty(&handler
->conf
->id_map
))
1265 handler
->clone_flags
|= CLONE_NEWUSER
;
1267 if (handler
->conf
->inherit_ns_fd
[LXC_NS_NET
] == -1) {
1268 if (!lxc_requests_empty_network(handler
))
1269 handler
->clone_flags
|= CLONE_NEWNET
;
1271 INFO("Inheriting a NET namespace.");
1274 if (handler
->conf
->inherit_ns_fd
[LXC_NS_IPC
] == -1)
1275 handler
->clone_flags
|= CLONE_NEWIPC
;
1277 INFO("Inheriting an IPC namespace.");
1279 if (handler
->conf
->inherit_ns_fd
[LXC_NS_UTS
] == -1)
1280 handler
->clone_flags
|= CLONE_NEWUTS
;
1282 INFO("Inheriting a UTS namespace.");
1285 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1286 * exec()s the requested container binary.
1287 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1288 * right here should be double checked if they'd pose a security risk. (For
1289 * example, any {u}mount() operations performed here will be reflected on the
1292 static int lxc_spawn(struct lxc_handler
*handler
)
1294 int failed_before_rename
= 0;
1295 const char *name
= handler
->name
;
1296 bool cgroups_connected
= false;
1297 int saved_ns_fd
[LXC_NS_MAX
];
1298 int preserve_mask
= 0, i
, flags
;
1299 int netpipepair
[2], nveths
;
1300 bool wants_to_map_ids
;
1301 struct lxc_list
*id_map
;
1304 id_map
= &handler
->conf
->id_map
;
1305 wants_to_map_ids
= !lxc_list_empty(id_map
);
1307 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1308 if (handler
->conf
->inherit_ns_fd
[i
] != -1)
1309 preserve_mask
|= ns_info
[i
].clone_flag
;
1311 if (lxc_sync_init(handler
))
1314 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, handler
->ttysock
) < 0) {
1315 lxc_sync_fini(handler
);
1319 resolve_clone_flags(handler
);
1321 if (handler
->clone_flags
& CLONE_NEWNET
) {
1322 if (!lxc_list_empty(&handler
->conf
->network
)) {
1324 /* Find gateway addresses from the link device, which is
1325 * no longer accessible inside the container. Do this
1326 * before creating network interfaces, since goto
1327 * out_delete_net does not work before lxc_clone.
1329 if (lxc_find_gateway_addresses(handler
)) {
1330 ERROR("Failed to find gateway addresses.");
1331 lxc_sync_fini(handler
);
1335 /* That should be done before the clone because we will
1336 * fill the netdev index and use them in the child.
1338 if (lxc_setup_networks_in_parent_namespaces(handler
)) {
1339 ERROR("Failed to create the network.");
1340 lxc_sync_fini(handler
);
1345 if (save_phys_nics(handler
->conf
)) {
1346 ERROR("Failed to save physical nic info.");
1351 if (!cgroup_init(handler
)) {
1352 ERROR("Failed initializing cgroup support.");
1353 goto out_delete_net
;
1356 cgroups_connected
= true;
1358 if (!cgroup_create(handler
)) {
1359 ERROR("Failed creating cgroups.");
1360 goto out_delete_net
;
1363 /* If the rootfs is not a blockdev, prevent the container from marking
1365 * If the container is unprivileged then skip rootfs pinning.
1367 if (!wants_to_map_ids
) {
1368 handler
->pinfd
= pin_rootfs(handler
->conf
->rootfs
.path
);
1369 if (handler
->pinfd
== -1)
1370 INFO("Failed to pin the rootfs for container \"%s\".", handler
->name
);
1373 if (!preserve_ns(saved_ns_fd
, preserve_mask
, getpid()))
1374 goto out_delete_net
;
1376 if (attach_ns(handler
->conf
->inherit_ns_fd
) < 0)
1377 goto out_delete_net
;
1379 if (am_unpriv() && (nveths
= count_veths(&handler
->conf
->network
))) {
1380 if (pipe(netpipepair
) < 0) {
1381 SYSERROR("Failed to create pipe.");
1382 goto out_delete_net
;
1384 /* Store netpipe in the global var for do_start's use. */
1385 netpipe
= netpipepair
[0];
1388 /* Create a process in a new set of namespaces. */
1389 flags
= handler
->clone_flags
;
1390 if (handler
->clone_flags
& CLONE_NEWUSER
) {
1391 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1392 * clone a new user namespace first and only later unshare our
1393 * network namespace to ensure that network devices ownership is
1396 flags
&= ~CLONE_NEWNET
;
1398 handler
->pid
= lxc_clone(do_start
, handler
, flags
);
1399 if (handler
->pid
< 0) {
1400 SYSERROR("Failed to clone a new set of namespaces.");
1401 goto out_delete_net
;
1403 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1404 if (flags
& ns_info
[i
].clone_flag
)
1405 INFO("Cloned %s.", ns_info
[i
].flag_name
);
1407 if (!preserve_ns(handler
->nsfd
, handler
->clone_flags
| preserve_mask
, handler
->pid
))
1408 INFO("Failed to preserve namespace for lxc.hook.stop.");
1410 if (attach_ns(saved_ns_fd
))
1411 WARN("Failed to restore saved namespaces.");
1413 lxc_sync_fini_child(handler
);
1415 /* Map the container uids. The container became an invalid userid the
1416 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1417 * anything immediately, but allows the container to setuid(0) (0 being
1418 * mapped to something else on the host.) later to become a valid uid
1421 if (wants_to_map_ids
&& lxc_map_ids(id_map
, handler
->pid
)) {
1422 ERROR("Failed to set up id mapping.");
1423 goto out_delete_net
;
1426 if (lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
)) {
1427 failed_before_rename
= 1;
1428 goto out_delete_net
;
1431 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
)) {
1432 failed_before_rename
= 1;
1433 goto out_delete_net
;
1436 if (!cgroup_create_legacy(handler
)) {
1437 ERROR("Failed to setup legacy cgroups for container \"%s\".", name
);
1438 goto out_delete_net
;
1440 if (!cgroup_setup_limits(handler
, false)) {
1441 ERROR("Failed to setup cgroup limits for container \"%s\".", name
);
1442 goto out_delete_net
;
1445 if (!cgroup_enter(handler
))
1446 goto out_delete_net
;
1448 if (!cgroup_chown(handler
))
1449 goto out_delete_net
;
1451 if (failed_before_rename
)
1452 goto out_delete_net
;
1454 /* Create the network configuration. */
1455 if (handler
->clone_flags
& CLONE_NEWNET
) {
1456 if (lxc_assign_network(handler
->lxcpath
, handler
->name
,
1457 &handler
->conf
->network
, handler
->pid
)) {
1458 ERROR("Failed to create the configured network.");
1459 goto out_delete_net
;
1463 if (netpipe
!= -1) {
1464 struct lxc_list
*iterator
;
1465 struct lxc_netdev
*netdev
;
1468 lxc_list_for_each(iterator
, &handler
->conf
->network
) {
1469 netdev
= iterator
->elem
;
1470 if (netdev
->type
!= LXC_NET_VETH
)
1472 if (write(netpipepair
[1], netdev
->name
, IFNAMSIZ
) != IFNAMSIZ
) {
1473 ERROR("Error writing veth name to container.");
1474 goto out_delete_net
;
1477 close(netpipepair
[1]);
1480 /* Tell the child to continue its initialization. We'll get
1481 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1483 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
1484 goto out_delete_net
;
1486 if (!lxc_list_empty(&handler
->conf
->limits
) && setup_resource_limits(&handler
->conf
->limits
, handler
->pid
)) {
1487 ERROR("failed to setup resource limits for '%s'", name
);
1488 goto out_delete_net
;
1491 if (lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
))
1492 goto out_delete_net
;
1494 if (!cgroup_setup_limits(handler
, true)) {
1495 ERROR("Failed to setup the devices cgroup for container \"%s\".", name
);
1496 goto out_delete_net
;
1498 TRACE("Set up cgroup device limits");
1500 cgroup_disconnect();
1501 cgroups_connected
= false;
1503 /* Tell the child to complete its initialization and wait for it to exec
1504 * or return an error. (The child will never return
1505 * LXC_SYNC_POST_CGROUP+1. It will either close the sync pipe, causing
1506 * lxc_sync_barrier_child to return success, or return a different
1507 * value, causing us to error out).
1509 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CGROUP
))
1512 /* Read tty fds allocated by child. */
1513 if (lxc_recv_ttys_from_child(handler
) < 0) {
1514 ERROR("Failed to receive tty info from child process.");
1515 goto out_delete_net
;
1518 if (handler
->ops
->post_start(handler
, handler
->data
))
1521 if (lxc_set_state(name
, handler
, RUNNING
)) {
1522 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
,
1523 lxc_state2str(RUNNING
));
1527 lxc_sync_fini(handler
);
1528 handler
->netnsfd
= lxc_preserve_ns(handler
->pid
, "net");
1533 if (cgroups_connected
)
1534 cgroup_disconnect();
1535 if (handler
->clone_flags
& CLONE_NEWNET
)
1536 lxc_delete_network(handler
);
1538 lxc_abort(name
, handler
);
1539 lxc_sync_fini(handler
);
1540 if (handler
->pinfd
>= 0) {
1541 close(handler
->pinfd
);
1542 handler
->pinfd
= -1;
1548 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1549 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1554 bool removed_all_netdevs
= true;
1555 struct lxc_conf
*conf
= handler
->conf
;
1557 if (lxc_init(name
, handler
) < 0) {
1558 ERROR("Failed to initialize container \"%s\".", name
);
1562 handler
->data
= data
;
1563 handler
->backgrounded
= backgrounded
;
1564 handler
->netnsfd
= -1;
1566 if (must_drop_cap_sys_boot(handler
->conf
)) {
1568 DEBUG("Dropping CAP_SYS_BOOT capability.");
1570 DEBUG("Not dropping CAP_SYS_BOOT capability as capabilities aren't supported.");
1573 DEBUG("Not dropping CAP_SYS_BOOT or watching utmp.");
1574 handler
->conf
->need_utmp_watch
= 0;
1577 if (!attach_block_device(handler
->conf
)) {
1578 ERROR("Failed to attach block device.");
1579 goto out_fini_nonet
;
1582 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1583 /* If the backing store is a device, mount it here and now. */
1584 if (rootfs_is_blockdev(conf
)) {
1585 if (unshare(CLONE_NEWNS
) < 0) {
1586 ERROR("Failed to unshare CLONE_NEWNS.");
1587 goto out_fini_nonet
;
1589 INFO("Unshared CLONE_NEWNS.");
1591 remount_all_slave();
1592 if (do_rootfs_setup(conf
, name
, lxcpath
) < 0) {
1593 ERROR("Error setting up rootfs mount as root before spawn.");
1594 goto out_fini_nonet
;
1596 INFO("Set up container rootfs as host root.");
1600 err
= lxc_spawn(handler
);
1602 ERROR("Failed to spawn container \"%s\".", name
);
1603 goto out_detach_blockdev
;
1606 handler
->conf
->reboot
= 0;
1608 err
= lxc_poll(name
, handler
);
1610 ERROR("LXC mainloop exited with error: %d.", err
);
1611 if (handler
->netnsfd
>= 0) {
1612 close(handler
->netnsfd
);
1613 handler
->netnsfd
= -1;
1618 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
1621 /* If the child process exited but was not signaled, it didn't call
1622 * reboot. This should mean it was an lxc-execute which simply exited.
1623 * In any case, treat it as a 'halt'.
1625 if (WIFSIGNALED(status
)) {
1626 switch(WTERMSIG(status
)) {
1627 case SIGINT
: /* halt */
1628 DEBUG("Container \"%s\" is halting.", name
);
1630 case SIGHUP
: /* reboot */
1631 DEBUG("Container \"%s\" is rebooting.", name
);
1632 handler
->conf
->reboot
= 1;
1634 case SIGSYS
: /* seccomp */
1635 DEBUG("Container \"%s\" violated its seccomp policy.", name
);
1638 DEBUG("Unknown exit status for container \"%s\" init %d.", name
, WTERMSIG(status
));
1643 DEBUG("Pushing physical nics back to host namespace");
1644 lxc_restore_phys_nics_to_netns(handler
->netnsfd
, handler
->conf
);
1646 DEBUG("Tearing down virtual network devices used by container \"%s\".", name
);
1647 removed_all_netdevs
= lxc_delete_network(handler
);
1649 if (handler
->pinfd
>= 0) {
1650 close(handler
->pinfd
);
1651 handler
->pinfd
= -1;
1654 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1655 err
= lxc_error_set_and_log(handler
->pid
, status
);
1657 if (!removed_all_netdevs
) {
1658 DEBUG("Failed tearing down network devices used by container. Trying again!");
1659 removed_all_netdevs
= lxc_delete_network(handler
);
1660 if (!removed_all_netdevs
)
1661 DEBUG("Failed tearing down network devices used by container. Not trying again!");
1664 out_detach_blockdev
:
1665 detach_block_device(handler
->conf
);
1668 lxc_fini(name
, handler
);
1672 lxc_abort(name
, handler
);
1680 static int start(struct lxc_handler
*handler
, void* data
)
1682 struct start_args
*arg
= data
;
1684 NOTICE("Exec'ing \"%s\".", arg
->argv
[0]);
1686 execvp(arg
->argv
[0], arg
->argv
);
1687 SYSERROR("Failed to exec \"%s\".", arg
->argv
[0]);
1691 static int post_start(struct lxc_handler
*handler
, void* data
)
1693 struct start_args
*arg
= data
;
1695 NOTICE("Started \"%s\" with pid \"%d\".", arg
->argv
[0], handler
->pid
);
1699 static struct lxc_operations start_ops
= {
1701 .post_start
= post_start
1704 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
1705 const char *lxcpath
, bool backgrounded
)
1707 struct start_args start_arg
= {
1711 handler
->conf
->need_utmp_watch
= 1;
1712 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
);
1715 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
1718 char destroy
[MAXPATHLEN
];
1721 struct lxc_container
*c
;
1722 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
1723 bret
= do_destroy_container(handler
->conf
);
1725 ERROR("Error destroying rootfs for container \"%s\".", name
);
1729 INFO("Destroyed rootfs for container \"%s\".", name
);
1731 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
1732 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1733 ERROR("Error destroying directory for container \"%s\".", name
);
1737 c
= lxc_container_new(name
, handler
->lxcpath
);
1739 if (container_disk_lock(c
)) {
1740 INFO("Could not update lxc_snapshots file.");
1741 lxc_container_put(c
);
1743 mod_all_rdeps(c
, false);
1744 container_disk_unlock(c
);
1745 lxc_container_put(c
);
1750 ret
= userns_exec_1(handler
->conf
, lxc_rmdir_onedev_wrapper
,
1751 destroy
, "lxc_rmdir_onedev_wrapper");
1753 ret
= lxc_rmdir_onedev(destroy
, NULL
);
1756 ERROR("Error destroying directory for container \"%s\".", name
);
1759 INFO("Destroyed directory for container \"%s\".", name
);
1762 static int lxc_rmdir_onedev_wrapper(void *data
)
1764 char *arg
= (char *) data
;
1765 return lxc_rmdir_onedev(arg
, NULL
);
1768 static bool do_destroy_container(struct lxc_conf
*conf
) {
1770 if (userns_exec_1(conf
, bdev_destroy_wrapper
, conf
,
1771 "bdev_destroy_wrapper") < 0)
1775 return bdev_destroy(conf
);