1 /* SPDX-License-Identifier: LGPL-2.1+ */
17 #include <sys/mount.h>
18 #include <sys/param.h>
19 #include <sys/prctl.h>
20 #include <sys/socket.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
32 #include "commands_utils.h"
35 #include "confile_utils.h"
37 #include "file_utils.h"
41 #include "lxccontainer.h"
43 #include "lxcseccomp.h"
46 #include "memory_utils.h"
48 #include "namespace.h"
50 #include "raw_syscalls.h"
52 #include "storage/storage.h"
53 #include "storage/storage_utils.h"
55 #include "syscall_wrappers.h"
60 #include <sys/capability.h>
64 #include "include/strlcpy.h"
67 lxc_log_define(start
, lxc
);
69 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
70 static bool do_destroy_container(struct lxc_handler
*handler
);
71 static int lxc_rmdir_onedev_wrapper(void *data
);
72 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
75 static void print_top_failing_dir(const char *path
)
77 __do_free
char *copy
= NULL
;
81 copy
= must_copy_string(path
);
83 e
= copy
+ strlen(path
);
86 while (p
< e
&& *p
== '/')
89 while (p
< e
&& *p
!= '/')
95 ret
= access(copy
, X_OK
);
97 SYSERROR("Could not access %s. Please grant it x "
98 "access, or add an ACL for the container "
106 static void lxc_put_nsfds(struct lxc_handler
*handler
)
110 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
111 if (handler
->nsfd
[i
] < 0)
114 close(handler
->nsfd
[i
]);
115 handler
->nsfd
[i
] = -EBADF
;
119 static int lxc_try_preserve_ns(const int pid
, const char *ns
)
123 fd
= lxc_preserve_ns(pid
, ns
);
125 if (errno
!= ENOENT
) {
126 SYSERROR("Failed to preserve %s namespace", ns
);
130 SYSWARN("Kernel does not support preserving %s namespaces", ns
);
137 /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
138 * specified in ns_clone_flags.
139 * Return true on success, false on failure.
141 static bool lxc_try_preserve_namespaces(struct lxc_handler
*handler
,
142 int ns_clone_flags
, pid_t pid
)
146 for (i
= 0; i
< LXC_NS_MAX
; i
++)
147 handler
->nsfd
[i
] = -EBADF
;
149 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
152 if ((ns_clone_flags
& ns_info
[i
].clone_flag
) == 0)
155 fd
= lxc_try_preserve_ns(pid
, ns_info
[i
].proc_name
);
157 /* Do not fail to start container on kernels that do
158 * not support interacting with namespaces through
161 if (fd
== -EOPNOTSUPP
)
164 lxc_put_nsfds(handler
);
168 handler
->nsfd
[i
] = fd
;
169 DEBUG("Preserved %s namespace via fd %d", ns_info
[i
].proc_name
,
176 static inline bool match_stdfds(int fd
)
178 return (fd
== STDIN_FILENO
|| fd
== STDOUT_FILENO
|| fd
== STDERR_FILENO
);
182 static bool match_dlog_fds(struct dirent
*direntp
)
184 char path
[PATH_MAX
] = {0};
185 char link
[PATH_MAX
] = {0};
189 ret
= snprintf(path
, PATH_MAX
, "/proc/self/fd/%s", direntp
->d_name
);
190 if (ret
< 0 || ret
>= PATH_MAX
) {
191 ERROR("Failed to create file descriptor name");
195 linklen
= readlink(path
, link
, PATH_MAX
);
197 SYSERROR("Failed to read link path - \"%s\"", path
);
199 } else if (linklen
>= PATH_MAX
) {
200 ERROR("The name of link path is too long - \"%s\"", path
);
204 if (strcmp(link
, "/dev/log_main") == 0 ||
205 strcmp(link
, "/dev/log_system") == 0 ||
206 strcmp(link
, "/dev/log_radio") == 0)
213 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
214 int *fds_to_ignore
, size_t len_fds
)
219 struct dirent
*direntp
;
221 if (conf
&& conf
->close_all_fds
)
225 dir
= opendir("/proc/self/fd");
227 SYSWARN("Failed to open directory");
233 while ((direntp
= readdir(dir
))) {
235 struct lxc_list
*cur
;
236 bool matched
= false;
238 if (strcmp(direntp
->d_name
, ".") == 0)
241 if (strcmp(direntp
->d_name
, "..") == 0)
244 ret
= lxc_safe_int(direntp
->d_name
, &fd
);
246 INFO("Could not parse file descriptor for \"%s\"", direntp
->d_name
);
250 for (i
= 0; i
< len_fds
; i
++)
251 if (fds_to_ignore
[i
] == fd
)
254 if (fd
== fddir
|| fd
== lxc_log_fd
||
255 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
258 /* Keep state clients that wait on reboots. */
260 lxc_list_for_each(cur
, &conf
->state_clients
) {
261 struct lxc_state_client
*client
= cur
->elem
;
263 if (client
->clientfd
!= fd
)
274 if (current_config
&& fd
== current_config
->logfd
)
277 if (match_stdfds(fd
))
281 if (match_dlog_fds(direntp
))
288 INFO("Closed inherited fd %d", fd
);
291 WARN("Inherited fd %d", fd
);
294 /* Only enable syslog at this point to avoid the above logging function
295 * to open a new fd and make the check_inherited function enter an
298 lxc_log_enable_syslog();
300 closedir(dir
); /* cannot fail */
304 static int setup_signal_fd(sigset_t
*oldmask
)
308 const int signals
[] = {SIGBUS
, SIGILL
, SIGSEGV
, SIGWINCH
};
310 /* Block everything except serious error signals. */
311 ret
= sigfillset(&mask
);
315 for (int sig
= 0; sig
< (sizeof(signals
) / sizeof(signals
[0])); sig
++) {
316 ret
= sigdelset(&mask
, signals
[sig
]);
321 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, oldmask
);
323 SYSERROR("Failed to set signal mask");
327 ret
= signalfd(-1, &mask
, SFD_CLOEXEC
);
329 SYSERROR("Failed to create signal file descriptor");
333 TRACE("Created signal file descriptor %d", ret
);
338 static int signal_handler(int fd
, uint32_t events
, void *data
,
339 struct lxc_epoll_descr
*descr
)
343 struct signalfd_siginfo siginfo
;
344 struct lxc_handler
*hdlr
= data
;
346 ret
= lxc_read_nointr(fd
, &siginfo
, sizeof(siginfo
));
348 ERROR("Failed to read signal info from signal file descriptor %d", fd
);
349 return LXC_MAINLOOP_ERROR
;
352 if (ret
!= sizeof(siginfo
)) {
353 ERROR("Unexpected size for struct signalfd_siginfo");
357 /* Check whether init is running. */
359 ret
= waitid(P_PID
, hdlr
->pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
360 if (ret
== 0 && info
.si_pid
== hdlr
->pid
)
361 hdlr
->init_died
= true;
363 /* Try to figure out a reasonable exit status to report. */
364 if (hdlr
->init_died
) {
365 switch (info
.si_code
) {
367 hdlr
->exit_status
= info
.si_status
<< 8;
372 hdlr
->exit_status
= info
.si_status
<< 8 | 0x7f;
375 /* Huh? The waitid() told us it's dead *and* continued? */
376 WARN("Init %d dead and continued?", hdlr
->pid
);
377 hdlr
->exit_status
= 1;
380 ERROR("Unknown si_code: %d", info
.si_code
);
381 hdlr
->exit_status
= 1;
385 if (siginfo
.ssi_signo
== SIGHUP
) {
386 if (hdlr
->pidfd
>= 0)
387 lxc_raw_pidfd_send_signal(hdlr
->pidfd
, SIGTERM
, NULL
, 0);
388 else if (hdlr
->proc_pidfd
>= 0)
389 lxc_raw_pidfd_send_signal(hdlr
->proc_pidfd
, SIGTERM
, NULL
, 0);
391 kill(hdlr
->pid
, SIGTERM
);
392 INFO("Killing %d since terminal hung up", hdlr
->pid
);
393 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
394 : LXC_MAINLOOP_CONTINUE
;
397 if (siginfo
.ssi_signo
!= SIGCHLD
) {
398 if (hdlr
->pidfd
>= 0)
399 lxc_raw_pidfd_send_signal(hdlr
->pidfd
,
400 siginfo
.ssi_signo
, NULL
, 0);
401 else if (hdlr
->proc_pidfd
>= 0)
402 lxc_raw_pidfd_send_signal(hdlr
->proc_pidfd
,
403 siginfo
.ssi_signo
, NULL
, 0);
405 kill(hdlr
->pid
, siginfo
.ssi_signo
);
406 INFO("Forwarded signal %d to pid %d", siginfo
.ssi_signo
, hdlr
->pid
);
407 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
408 : LXC_MAINLOOP_CONTINUE
;
411 /* More robustness, protect ourself from a SIGCHLD sent
412 * by a process different from the container init.
414 if (siginfo
.ssi_pid
!= hdlr
->pid
) {
415 NOTICE("Received %d from pid %d instead of container init %d",
416 siginfo
.ssi_signo
, siginfo
.ssi_pid
, hdlr
->pid
);
417 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
418 : LXC_MAINLOOP_CONTINUE
;
421 if (siginfo
.ssi_code
== CLD_STOPPED
) {
422 INFO("Container init process was stopped");
423 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
424 : LXC_MAINLOOP_CONTINUE
;
427 if (siginfo
.ssi_code
== CLD_CONTINUED
) {
428 INFO("Container init process was continued");
429 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
430 : LXC_MAINLOOP_CONTINUE
;
433 DEBUG("Container init process %d exited", hdlr
->pid
);
435 return LXC_MAINLOOP_CLOSE
;
438 int lxc_serve_state_clients(const char *name
, struct lxc_handler
*handler
,
443 struct lxc_list
*cur
, *next
;
444 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
447 handler
->state
= RUNNING
;
449 handler
->state
= state
;
451 TRACE("Set container state to %s", lxc_state2str(state
));
453 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
454 TRACE("No state clients registered");
458 retlen
= strlcpy(msg
.name
, name
, sizeof(msg
.name
));
459 if (retlen
>= sizeof(msg
.name
))
462 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
463 struct lxc_state_client
*client
= cur
->elem
;
465 if (client
->states
[state
] == 0) {
466 TRACE("State %s not registered for state client %d",
467 lxc_state2str(state
), client
->clientfd
);
471 TRACE("Sending state %s to state client %d",
472 lxc_state2str(state
), client
->clientfd
);
474 ret
= lxc_send_nointr(client
->clientfd
, &msg
, sizeof(msg
), MSG_NOSIGNAL
);
476 SYSERROR("Failed to send message to client");
478 /* kick client from list */
480 close(client
->clientfd
);
488 static int lxc_serve_state_socket_pair(const char *name
,
489 struct lxc_handler
*handler
,
494 if (!handler
->daemonize
||
495 handler
->state_socket_pair
[1] < 0 ||
499 /* Close read end of the socket pair. */
500 close(handler
->state_socket_pair
[0]);
501 handler
->state_socket_pair
[0] = -1;
504 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
505 &(int){state
}, sizeof(int));
507 SYSERROR("Failed to send state to %d", handler
->state_socket_pair
[1]);
515 if (ret
!= sizeof(int)) {
516 ERROR("Message too long : %d", handler
->state_socket_pair
[1]);
520 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
521 handler
->state_socket_pair
[1]);
523 /* Close write end of the socket pair. */
524 close(handler
->state_socket_pair
[1]);
525 handler
->state_socket_pair
[1] = -1;
530 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
535 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
537 ERROR("Failed to synchronize via anonymous pair of unix sockets");
541 ret
= lxc_serve_state_clients(name
, handler
, state
);
545 /* This function will try to connect to the legacy lxc-monitord state
546 * server and only exists for backwards compatibility.
548 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
553 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
556 bool has_console
= true;
557 struct lxc_epoll_descr descr
, descr_console
;
559 if (handler
->conf
->console
.path
&&
560 strcmp(handler
->conf
->console
.path
, "none") == 0)
563 ret
= lxc_mainloop_open(&descr
);
565 ERROR("Failed to create mainloop");
570 ret
= lxc_mainloop_open(&descr_console
);
572 ERROR("Failed to create console mainloop");
577 ret
= lxc_mainloop_add_handler(&descr
, handler
->sigfd
, signal_handler
, handler
);
579 ERROR("Failed to add signal handler for %d to mainloop", handler
->sigfd
);
580 goto out_mainloop_console
;
583 ret
= lxc_seccomp_setup_proxy(&handler
->conf
->seccomp
, &descr
, handler
);
585 ERROR("Failed to setup seccomp proxy");
586 goto out_mainloop_console
;
590 struct lxc_terminal
*console
= &handler
->conf
->console
;
592 ret
= lxc_terminal_mainloop_add(&descr
, console
);
594 ERROR("Failed to add console handlers to mainloop");
595 goto out_mainloop_console
;
598 ret
= lxc_terminal_mainloop_add(&descr_console
, console
);
600 ERROR("Failed to add console handlers to console mainloop");
601 goto out_mainloop_console
;
604 handler
->conf
->console
.descr
= &descr
;
607 ret
= lxc_cmd_mainloop_add(name
, &descr
, handler
);
609 ERROR("Failed to add command handler to mainloop");
610 goto out_mainloop_console
;
613 TRACE("Mainloop is ready");
615 ret
= lxc_mainloop(&descr
, -1);
618 if (ret
< 0 || !handler
->init_died
)
619 goto out_mainloop_console
;
622 ret
= lxc_mainloop(&descr_console
, 0);
624 out_mainloop_console
:
626 lxc_mainloop_close(&descr_console
);
627 TRACE("Closed console mainloop");
631 lxc_mainloop_close(&descr
);
632 TRACE("Closed mainloop");
635 close(handler
->sigfd
);
636 TRACE("Closed signal file descriptor %d", handler
->sigfd
);
637 handler
->sigfd
= -EBADF
;
642 void lxc_zero_handler(struct lxc_handler
*handler
)
646 memset(handler
, 0, sizeof(struct lxc_handler
));
650 handler
->pidfd
= -EBADF
;
652 handler
->proc_pidfd
= -EBADF
;
656 for (i
= 0; i
< LXC_NS_MAX
; i
++)
657 handler
->nsfd
[i
] = -1;
659 handler
->data_sock
[0] = -1;
660 handler
->data_sock
[1] = -1;
662 handler
->state_socket_pair
[0] = -1;
663 handler
->state_socket_pair
[1] = -1;
665 handler
->sync_sock
[0] = -1;
666 handler
->sync_sock
[1] = -1;
669 void lxc_free_handler(struct lxc_handler
*handler
)
671 if (handler
->pinfd
>= 0)
672 close(handler
->pinfd
);
674 if (handler
->pidfd
>= 0)
675 close(handler
->pidfd
);
677 if (handler
->proc_pidfd
>= 0)
678 close(handler
->proc_pidfd
);
680 if (handler
->sigfd
>= 0)
681 close(handler
->sigfd
);
683 lxc_put_nsfds(handler
);
685 if (handler
->conf
&& handler
->conf
->reboot
== REBOOT_NONE
)
686 if (handler
->conf
->maincmd_fd
>= 0)
687 lxc_abstract_unix_close(handler
->conf
->maincmd_fd
);
689 if (handler
->monitor_status_fd
>= 0)
690 close(handler
->monitor_status_fd
);
692 if (handler
->state_socket_pair
[0] >= 0)
693 close(handler
->state_socket_pair
[0]);
695 if (handler
->state_socket_pair
[1] >= 0)
696 close(handler
->state_socket_pair
[1]);
698 if (handler
->cgroup_ops
)
699 cgroup_exit(handler
->cgroup_ops
);
701 handler
->conf
= NULL
;
706 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
707 const char *lxcpath
, bool daemonize
)
710 struct lxc_handler
*handler
;
712 handler
= malloc(sizeof(*handler
));
716 memset(handler
, 0, sizeof(*handler
));
718 /* Note that am_guest_unpriv() checks the effective uid. We
719 * probably don't care if we are real root only if we are running
720 * as root so this should be fine.
722 handler
->am_root
= !am_guest_unpriv();
723 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
724 handler
->conf
= conf
;
725 handler
->lxcpath
= lxcpath
;
726 handler
->monitor_status_fd
= -EBADF
;
728 handler
->pidfd
= -EBADF
;
729 handler
->proc_pidfd
= -EBADF
;
730 handler
->sigfd
= -EBADF
;
731 handler
->init_died
= false;
732 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
733 if (handler
->conf
->reboot
== REBOOT_NONE
)
734 lxc_list_init(&handler
->conf
->state_clients
);
736 for (i
= 0; i
< LXC_NS_MAX
; i
++)
737 handler
->nsfd
[i
] = -1;
739 handler
->name
= name
;
741 handler
->transient_pid
= lxc_raw_getpid();
743 handler
->transient_pid
= -1;
745 if (daemonize
&& handler
->conf
->reboot
== REBOOT_NONE
) {
746 /* Create socketpair() to synchronize on daemonized startup.
747 * When the container reboots we don't need to synchronize
748 * again currently so don't open another socketpair().
750 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
751 handler
->state_socket_pair
);
753 ERROR("Failed to create anonymous pair of unix sockets");
757 TRACE("Created anonymous pair {%d,%d} of unix sockets",
758 handler
->state_socket_pair
[0],
759 handler
->state_socket_pair
[1]);
762 if (handler
->conf
->reboot
== REBOOT_NONE
) {
763 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
764 if (handler
->conf
->maincmd_fd
< 0) {
765 ERROR("Failed to set up command socket");
770 TRACE("Unix domain socket %d for command server is ready",
771 handler
->conf
->maincmd_fd
);
776 lxc_free_handler(handler
);
781 int lxc_init(const char *name
, struct lxc_handler
*handler
)
783 __do_close_prot_errno
int status_fd
= -EBADF
;
785 const char *loglevel
;
786 struct lxc_conf
*conf
= handler
->conf
;
788 handler
->monitor_pid
= lxc_raw_getpid();
789 status_fd
= open("/proc/self/status", O_RDONLY
| O_CLOEXEC
);
791 SYSERROR("Failed to open monitor status fd");
792 goto out_close_maincmd_fd
;
796 TRACE("Initialized LSM");
798 ret
= lxc_read_seccomp_config(conf
);
800 ERROR("Failed loading seccomp policy");
801 goto out_close_maincmd_fd
;
803 TRACE("Read seccomp policy");
805 /* Begin by setting the state to STARTING. */
806 ret
= lxc_set_state(name
, handler
, STARTING
);
808 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING
));
809 goto out_close_maincmd_fd
;
811 TRACE("Set container state to \"STARTING\"");
813 /* Start of environment variable setup for hooks. */
814 ret
= setenv("LXC_NAME", name
, 1);
816 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name
);
819 ret
= setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1);
821 SYSERROR("Failed to set environment variable: "
822 "LXC_CONFIG_FILE=%s", conf
->rcfile
);
825 if (conf
->rootfs
.mount
) {
826 ret
= setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1);
828 SYSERROR("Failed to set environment variable: "
829 "LXC_ROOTFS_MOUNT=%s", conf
->rootfs
.mount
);
832 if (conf
->rootfs
.path
) {
833 ret
= setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1);
835 SYSERROR("Failed to set environment variable: "
836 "LXC_ROOTFS_PATH=%s", conf
->rootfs
.path
);
839 if (conf
->console
.path
) {
840 ret
= setenv("LXC_CONSOLE", conf
->console
.path
, 1);
842 SYSERROR("Failed to set environment variable: "
843 "LXC_CONSOLE=%s", conf
->console
.path
);
846 if (conf
->console
.log_path
) {
847 ret
= setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1);
849 SYSERROR("Failed to set environment variable: "
850 "LXC_CONSOLE_LOGPATH=%s", conf
->console
.log_path
);
853 if (cgns_supported()) {
854 ret
= setenv("LXC_CGNS_AWARE", "1", 1);
856 SYSERROR("Failed to set environment variable "
860 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
861 ret
= setenv("LXC_LOG_LEVEL", loglevel
, 1);
863 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
866 if (conf
->hooks_version
== 0)
867 ret
= setenv("LXC_HOOK_VERSION", "0", 1);
869 ret
= setenv("LXC_HOOK_VERSION", "1", 1);
871 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf
->hooks_version
);
872 /* End of environment variable setup for hooks. */
874 TRACE("Set environment variables");
876 ret
= run_lxc_hooks(name
, "pre-start", conf
, NULL
);
878 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name
);
881 TRACE("Ran pre-start hooks");
883 /* The signal fd has to be created before forking otherwise if the child
884 * process exits before we setup the signal fd, the event will be lost
885 * and the command will be stuck.
887 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
888 if (handler
->sigfd
< 0) {
889 ERROR("Failed to setup SIGCHLD fd handler.");
892 TRACE("Set up signal fd");
894 /* Do this after setting up signals since it might unblock SIGWINCH. */
895 ret
= lxc_terminal_setup(conf
);
897 ERROR("Failed to create console");
898 goto out_restore_sigmask
;
900 TRACE("Created console");
902 ret
= lxc_terminal_map_ids(conf
, &conf
->console
);
904 ERROR("Failed to chown console");
905 goto out_delete_terminal
;
907 TRACE("Chowned console");
909 handler
->cgroup_ops
= cgroup_init(handler
->conf
);
910 if (!handler
->cgroup_ops
) {
911 ERROR("Failed to initialize cgroup driver");
912 goto out_delete_terminal
;
914 TRACE("Initialized cgroup driver");
916 ret
= lsm_process_prepare(conf
, handler
->lxcpath
);
918 ERROR("Failed to initialize LSM");
919 goto out_delete_terminal
;
921 TRACE("Initialized LSM");
923 INFO("Container \"%s\" is initialized", name
);
924 handler
->monitor_status_fd
= move_fd(status_fd
);
928 lxc_terminal_delete(&handler
->conf
->console
);
931 (void)pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
934 lxc_delete_tty(&conf
->ttys
);
937 (void)lxc_set_state(name
, handler
, ABORTING
);
939 out_close_maincmd_fd
:
940 lxc_abstract_unix_close(conf
->maincmd_fd
);
941 conf
->maincmd_fd
= -1;
945 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
949 struct lxc_list
*cur
, *next
;
950 char *namespaces
[LXC_NS_MAX
+ 1];
951 size_t namespace_count
= 0;
952 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
954 /* The STOPPING state is there for future cleanup code which can take
957 lxc_set_state(name
, handler
, STOPPING
);
959 self
= lxc_raw_getpid();
960 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
961 if (handler
->nsfd
[i
] < 0)
964 if (handler
->conf
->hooks_version
== 0)
965 ret
= asprintf(&namespaces
[namespace_count
],
966 "%s:/proc/%d/fd/%d", ns_info
[i
].proc_name
,
967 self
, handler
->nsfd
[i
]);
969 ret
= asprintf(&namespaces
[namespace_count
],
970 "/proc/%d/fd/%d", self
, handler
->nsfd
[i
]);
972 SYSERROR("Failed to allocate memory");
976 if (handler
->conf
->hooks_version
== 0) {
981 ret
= setenv(ns_info
[i
].env_name
, namespaces
[namespace_count
], 1);
983 SYSERROR("Failed to set environment variable %s=%s",
984 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
986 TRACE("Set environment variable %s=%s",
987 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
991 namespaces
[namespace_count
] = NULL
;
993 if (handler
->conf
->reboot
> REBOOT_NONE
) {
994 ret
= setenv("LXC_TARGET", "reboot", 1);
996 SYSERROR("Failed to set environment variable: "
997 "LXC_TARGET=reboot");
1000 if (handler
->conf
->reboot
== REBOOT_NONE
) {
1001 ret
= setenv("LXC_TARGET", "stop", 1);
1003 SYSERROR("Failed to set environment variable: "
1007 if (handler
->conf
->hooks_version
== 0)
1008 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, namespaces
);
1010 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, NULL
);
1012 ERROR("Failed to run \"lxc.hook.stop\" hook");
1014 while (namespace_count
--)
1015 free(namespaces
[namespace_count
]);
1017 lsm_process_cleanup(handler
->conf
, handler
->lxcpath
);
1020 cgroup_ops
->payload_destroy(cgroup_ops
, handler
);
1021 cgroup_ops
->monitor_destroy(cgroup_ops
, handler
);
1024 if (handler
->conf
->reboot
== REBOOT_NONE
) {
1025 /* For all new state clients simply close the command socket.
1026 * This will inform all state clients that the container is
1027 * STOPPED and also prevents a race between a open()/close() on
1028 * the command socket causing a new process to get ECONNREFUSED
1029 * because we haven't yet closed the command socket.
1031 lxc_abstract_unix_close(handler
->conf
->maincmd_fd
);
1032 handler
->conf
->maincmd_fd
= -1;
1033 TRACE("Closed command socket");
1035 /* This function will try to connect to the legacy lxc-monitord
1036 * state server and only exists for backwards compatibility.
1038 lxc_monitor_send_state(name
, STOPPED
, handler
->lxcpath
);
1040 /* The command socket is closed so no one can acces the command
1041 * socket anymore so there's no need to lock it.
1043 handler
->state
= STOPPED
;
1044 TRACE("Set container state to \"STOPPED\"");
1046 lxc_set_state(name
, handler
, STOPPED
);
1049 /* Avoid lingering namespace references. */
1050 lxc_put_nsfds(handler
);
1052 ret
= run_lxc_hooks(name
, "post-stop", handler
->conf
, NULL
);
1054 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name
);
1055 if (handler
->conf
->reboot
> REBOOT_NONE
) {
1056 WARN("Container will be stopped instead of rebooted");
1057 handler
->conf
->reboot
= REBOOT_NONE
;
1059 ret
= setenv("LXC_TARGET", "stop", 1);
1061 WARN("Failed to set environment variable: "
1066 /* Reset mask set by setup_signal_fd. */
1067 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1069 SYSWARN("Failed to restore signal mask");
1071 lxc_terminal_delete(&handler
->conf
->console
);
1072 lxc_delete_tty(&handler
->conf
->ttys
);
1074 /* The command socket is now closed, no more state clients can register
1075 * themselves from now on. So free the list of state clients.
1077 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
1078 struct lxc_state_client
*client
= cur
->elem
;
1080 /* Keep state clients that want to be notified about reboots. */
1081 if ((handler
->conf
->reboot
> REBOOT_NONE
) &&
1082 (client
->states
[RUNNING
] == 2))
1085 /* close state client socket */
1087 close(client
->clientfd
);
1092 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= REBOOT_REQ
)
1093 lxc_destroy_container_on_signal(handler
, name
);
1095 lxc_free_handler(handler
);
1098 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
1103 lxc_set_state(name
, handler
, ABORTING
);
1105 if (handler
->pidfd
>= 0)
1106 ret
= lxc_raw_pidfd_send_signal(handler
->pidfd
, SIGKILL
, NULL
, 0);
1107 else if (handler
->proc_pidfd
>= 0)
1108 ret
= lxc_raw_pidfd_send_signal(handler
->proc_pidfd
, SIGKILL
, NULL
, 0);
1109 else if (handler
->pid
> 0)
1110 ret
= kill(handler
->pid
, SIGKILL
);
1112 SYSERROR("Failed to send SIGKILL to %d", handler
->pid
);
1115 ret
= waitpid(-1, &status
, 0);
1119 static int do_start(void *data
)
1121 struct lxc_handler
*handler
= data
;
1122 __lxc_unused __do_close_prot_errno
int data_sock0
= handler
->data_sock
[0],
1123 data_sock1
= handler
->data_sock
[1];
1124 __do_close_prot_errno
int status_fd
= -EBADF
;
1128 struct lxc_list
*iterator
;
1131 int devnull_fd
= -1;
1133 lxc_sync_fini_parent(handler
);
1135 if (lxc_abstract_unix_recv_fds(data_sock1
, &status_fd
, 1, NULL
, 0) < 0) {
1136 ERROR("Failed to receive status file descriptor to child process");
1137 goto out_warn_father
;
1140 /* This prctl must be before the synchro, so if the parent dies before
1141 * we set the parent death signal, we will detect its death with the
1142 * synchro right after, otherwise we have a window where the parent can
1143 * exit before we set the pdeath signal leading to a unsupervized
1146 ret
= lxc_set_death_signal(SIGKILL
, handler
->monitor_pid
, status_fd
);
1148 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1149 goto out_warn_father
;
1152 ret
= lxc_ambient_caps_up();
1154 ERROR("Failed to raise ambient capabilities");
1155 goto out_warn_father
;
1158 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1160 SYSERROR("Failed to set signal mask");
1161 goto out_warn_father
;
1164 /* Don't leak the pinfd to the container. */
1165 if (handler
->pinfd
>= 0)
1166 close(handler
->pinfd
);
1168 ret
= lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
);
1170 goto out_warn_father
;
1172 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1173 * https://github.com/lxc/lxd/issues/1978.
1175 if ((handler
->ns_clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
1176 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
1177 ret
= unshare(CLONE_NEWNET
);
1179 SYSERROR("Failed to unshare CLONE_NEWNET");
1180 goto out_warn_father
;
1182 INFO("Unshared CLONE_NEWNET");
1185 /* Tell the parent task it can begin to configure the container and wait
1188 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
);
1192 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1193 ret
= lxc_network_recv_from_parent(handler
);
1195 ERROR("Failed to receive veth names from parent");
1196 goto out_warn_father
;
1200 /* If we are in a new user namespace, become root there to have
1201 * privilege over our namespace.
1203 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
1204 if (!handler
->conf
->root_nsuid_map
)
1205 nsuid
= handler
->conf
->init_uid
;
1207 if (!handler
->conf
->root_nsgid_map
)
1208 nsgid
= handler
->conf
->init_gid
;
1210 if (!lxc_switch_uid_gid(nsuid
, nsgid
))
1211 goto out_warn_father
;
1213 /* Drop groups only after we switched to a valid gid in the new
1216 if (!lxc_setgroups(0, NULL
) &&
1217 (handler
->am_root
|| errno
!= EPERM
))
1218 goto out_warn_father
;
1220 ret
= prctl(PR_SET_DUMPABLE
, prctl_arg(1), prctl_arg(0),
1221 prctl_arg(0), prctl_arg(0));
1223 goto out_warn_father
;
1225 /* set{g,u}id() clears deathsignal */
1226 ret
= lxc_set_death_signal(SIGKILL
, handler
->monitor_pid
, status_fd
);
1228 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1229 goto out_warn_father
;
1233 ret
= access(handler
->lxcpath
, X_OK
);
1235 print_top_failing_dir(handler
->lxcpath
);
1236 goto out_warn_father
;
1239 /* In order to checkpoint restore, we need to have everything in the
1240 * same mount namespace. However, some containers may not have a
1241 * reasonable /dev (in particular, they may not have /dev/null), so we
1242 * can't set init's std fds to /dev/null by opening it from inside the
1245 * If that's the case, fall back to using the host's /dev/null. This
1246 * means that migration won't work, but at least we won't spew output
1247 * where it isn't wanted.
1249 if (handler
->daemonize
&& !handler
->conf
->autodev
) {
1250 char path
[PATH_MAX
];
1252 ret
= snprintf(path
, sizeof(path
), "%s/dev/null",
1253 handler
->conf
->rootfs
.mount
);
1254 if (ret
< 0 || ret
>= sizeof(path
))
1255 goto out_warn_father
;
1257 ret
= access(path
, F_OK
);
1259 devnull_fd
= open_devnull();
1262 goto out_warn_father
;
1263 WARN("Using /dev/null from the host for container "
1264 "init's standard file descriptors. Migration will "
1269 /* Ask father to setup cgroups and wait for him to finish. */
1270 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
);
1274 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1275 * earlier we end up with a wrong view of /proc/self/cgroup. For
1276 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1277 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1278 * /proc/self/cgroup would show us:
1282 * whereas it should actually show
1286 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1287 ret
= unshare(CLONE_NEWCGROUP
);
1289 if (errno
!= EINVAL
) {
1290 SYSERROR("Failed to unshare CLONE_NEWCGROUP");
1291 goto out_warn_father
;
1294 handler
->ns_clone_flags
&= ~CLONE_NEWCGROUP
;
1295 SYSINFO("Kernel does not support CLONE_NEWCGROUP");
1297 INFO("Unshared CLONE_NEWCGROUP");
1301 /* Add the requested environment variables to the current environment to
1302 * allow them to be used by the various hooks, such as the start hook
1305 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1306 ret
= putenv((char *)iterator
->elem
);
1308 SYSERROR("Failed to set environment variable: %s",
1309 (char *)iterator
->elem
);
1310 goto out_warn_father
;
1314 /* Setup the container, ip, names, utsname, ... */
1315 ret
= lxc_setup(handler
);
1317 ERROR("Failed to setup container \"%s\"", handler
->name
);
1318 goto out_warn_father
;
1321 /* Set the label to change to when we exec(2) the container's init. */
1322 ret
= lsm_process_label_set(NULL
, handler
->conf
, true);
1324 goto out_warn_father
;
1326 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1327 * before we aren't allowed anymore.
1329 if (handler
->conf
->no_new_privs
) {
1330 ret
= prctl(PR_SET_NO_NEW_PRIVS
, prctl_arg(1), prctl_arg(0),
1331 prctl_arg(0), prctl_arg(0));
1333 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1334 "execve() gainable privileges");
1335 goto out_warn_father
;
1337 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1341 /* Some init's such as busybox will set sane tty settings on stdin,
1342 * stdout, stderr which it thinks is the console. We already set them
1343 * the way we wanted on the real terminal, and we want init to do its
1344 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1345 * make sure that that pty is stdin,stdout,stderr.
1347 if (handler
->conf
->console
.slave
>= 0) {
1348 if (handler
->daemonize
|| !handler
->conf
->is_execute
)
1349 ret
= set_stdfds(handler
->conf
->console
.slave
);
1351 ret
= lxc_terminal_set_stdfds(handler
->conf
->console
.slave
);
1353 ERROR("Failed to redirect std{in,out,err} to pty file "
1354 "descriptor %d", handler
->conf
->console
.slave
);
1355 goto out_warn_father
;
1359 /* If we mounted a temporary proc, then unmount it now. */
1360 tmp_proc_unmount(handler
->conf
);
1362 ret
= lxc_seccomp_load(handler
->conf
);
1364 goto out_warn_father
;
1366 ret
= lxc_seccomp_send_notifier_fd(&handler
->conf
->seccomp
, data_sock0
);
1368 SYSERROR("Failed to send seccomp notify fd to parent");
1369 goto out_warn_father
;
1372 ret
= run_lxc_hooks(handler
->name
, "start", handler
->conf
, NULL
);
1374 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1376 goto out_warn_father
;
1379 close(handler
->sigfd
);
1381 if (handler
->conf
->console
.slave
< 0 && handler
->daemonize
) {
1382 if (devnull_fd
< 0) {
1383 devnull_fd
= open_devnull();
1385 goto out_warn_father
;
1388 ret
= set_stdfds(devnull_fd
);
1390 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1391 goto out_warn_father
;
1395 if (devnull_fd
>= 0) {
1402 if (handler
->conf
->init_cwd
) {
1403 ret
= chdir(handler
->conf
->init_cwd
);
1405 SYSERROR("Could not change directory to \"%s\"",
1406 handler
->conf
->init_cwd
);
1407 goto out_warn_father
;
1411 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
);
1413 goto out_warn_father
;
1415 /* Reset the environment variables the user requested in a clear
1419 /* Don't error out though. */
1421 SYSERROR("Failed to clear environment.");
1423 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1424 ret
= putenv((char *)iterator
->elem
);
1426 SYSERROR("Failed to set environment variable: %s",
1427 (char *)iterator
->elem
);
1428 goto out_warn_father
;
1432 ret
= putenv("container=lxc");
1434 SYSERROR("Failed to set environment variable: container=lxc");
1435 goto out_warn_father
;
1438 if (handler
->conf
->ttys
.tty_names
) {
1439 ret
= putenv(handler
->conf
->ttys
.tty_names
);
1441 SYSERROR("Failed to set environment variable for container ptys");
1442 goto out_warn_father
;
1446 /* The container has been setup. We can now switch to an unprivileged
1449 new_uid
= handler
->conf
->init_uid
;
1450 new_gid
= handler
->conf
->init_gid
;
1452 /* Avoid unnecessary syscalls. */
1453 if (new_uid
== nsuid
)
1454 new_uid
= LXC_INVALID_UID
;
1456 if (new_gid
== nsgid
)
1457 new_gid
= LXC_INVALID_GID
;
1459 if (!lxc_switch_uid_gid(new_uid
, new_gid
))
1460 goto out_warn_father
;
1462 /* If we are in a new user namespace we already dropped all groups when
1463 * we switched to root in the new user namespace further above. Only
1464 * drop groups if we can, so ensure that we have necessary privilege.
1466 if (lxc_list_empty(&handler
->conf
->id_map
))
1468 if (lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
))
1470 if (!lxc_setgroups(0, NULL
))
1471 goto out_warn_father
;
1473 ret
= lxc_ambient_caps_down();
1475 ERROR("Failed to clear ambient capabilities");
1476 goto out_warn_father
;
1479 if (handler
->conf
->monitor_signal_pdeath
!= SIGKILL
) {
1480 ret
= lxc_set_death_signal(handler
->conf
->monitor_signal_pdeath
,
1481 handler
->monitor_pid
, status_fd
);
1483 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d",
1484 handler
->conf
->monitor_signal_pdeath
);
1485 goto out_warn_father
;
1489 /* After this call, we are in error because this ops should not return
1492 handler
->ops
->start(handler
, handler
->data
);
1495 /* We want the parent to know something went wrong, so we return a
1496 * special error code.
1498 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1501 if (devnull_fd
>= 0)
1507 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1510 struct lxc_terminal_info
*tty
;
1512 int sock
= handler
->data_sock
[1];
1513 struct lxc_conf
*conf
= handler
->conf
;
1514 struct lxc_tty_info
*ttys
= &conf
->ttys
;
1516 if (!conf
->ttys
.max
)
1519 ttys
->tty
= malloc(sizeof(*ttys
->tty
) * ttys
->max
);
1523 for (i
= 0; i
< conf
->ttys
.max
; i
++) {
1526 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1530 tty
= &ttys
->tty
[i
];
1532 tty
->master
= ttyfds
[0];
1533 tty
->slave
= ttyfds
[1];
1534 TRACE("Received pty with master fd %d and slave fd %d from "
1535 "child", tty
->master
, tty
->slave
);
1539 SYSERROR("Failed to receive %zu ttys from child", ttys
->max
);
1541 TRACE("Received %zu ttys from child", ttys
->max
);
1546 int resolve_clone_flags(struct lxc_handler
*handler
)
1549 struct lxc_conf
*conf
= handler
->conf
;
1551 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1552 if (conf
->ns_keep
) {
1553 if (!(conf
->ns_keep
& ns_info
[i
].clone_flag
))
1554 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1555 } else if (conf
->ns_clone
) {
1556 if ((conf
->ns_clone
& ns_info
[i
].clone_flag
))
1557 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1559 if (i
== LXC_NS_USER
&& lxc_list_empty(&handler
->conf
->id_map
))
1562 if (i
== LXC_NS_NET
&& lxc_requests_empty_network(handler
))
1565 if (i
== LXC_NS_CGROUP
&& !cgns_supported())
1568 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1571 if (!conf
->ns_share
[i
])
1574 handler
->ns_clone_flags
&= ~ns_info
[i
].clone_flag
;
1575 TRACE("Sharing %s namespace", ns_info
[i
].proc_name
);
1581 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1582 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1583 * But since the memory between parent and child is shared on CLONE_VM this
1584 * would invalidate the getpid() cache that glibc used to maintain and so
1585 * getpid() in the child would return the parent's pid. This is all fixed in
1586 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1587 * not reset anymore.
1588 * However, if for whatever reason you - dear committer - somehow need to get the
1589 * pid of the dummy intermediate process for do_share_ns() you need to call
1590 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1593 static inline int do_share_ns(void *arg
)
1596 struct lxc_handler
*handler
= arg
;
1598 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1599 if (handler
->nsfd
[i
] < 0)
1602 ret
= setns(handler
->nsfd
[i
], 0);
1605 * Note that joining a user and/or mount namespace
1606 * requires the process is not multithreaded otherwise
1607 * setns() will fail here.
1609 SYSERROR("Failed to inherit %s namespace",
1610 ns_info
[i
].proc_name
);
1614 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1617 flags
= handler
->ns_on_clone_flags
;
1618 flags
|= CLONE_PARENT
;
1619 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, CLONE_PIDFD
| flags
,
1621 if (handler
->pid
< 0)
1627 static int proc_pidfd_open(pid_t pid
)
1629 __do_close_prot_errno
int proc_pidfd
= -EBADF
;
1632 snprintf(path
, sizeof(path
), "/proc/%d", pid
);
1633 proc_pidfd
= open(path
, O_DIRECTORY
| O_RDONLY
| O_CLOEXEC
);
1634 if (proc_pidfd
< 0) {
1635 SYSERROR("Failed to open %s", path
);
1639 /* Test whether we can send signals. */
1640 if (lxc_raw_pidfd_send_signal(proc_pidfd
, 0, NULL
, 0)) {
1641 if (errno
!= ENOSYS
)
1642 SYSERROR("Failed to send signal through pidfd");
1644 INFO("Sending signals through pidfds not supported on this kernel");
1648 return move_fd(proc_pidfd
);
1651 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1652 * exec()s the requested container binary.
1653 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1654 * right here should be double checked if they'd pose a security risk. (For
1655 * example, any {u}mount() operations performed here will be reflected on the
1658 static int lxc_spawn(struct lxc_handler
*handler
)
1660 __do_close_prot_errno
int data_sock0
= -EBADF
, data_sock1
= -EBADF
;
1663 bool wants_to_map_ids
;
1664 struct lxc_list
*id_map
;
1665 const char *name
= handler
->name
;
1666 const char *lxcpath
= handler
->lxcpath
;
1667 bool share_ns
= false;
1668 struct lxc_conf
*conf
= handler
->conf
;
1669 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
1671 id_map
= &conf
->id_map
;
1672 wants_to_map_ids
= !lxc_list_empty(id_map
);
1674 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1675 if (!conf
->ns_share
[i
])
1678 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->ns_share
[i
], lxcpath
, ns_info
[i
].proc_name
);
1679 if (handler
->nsfd
[i
] < 0)
1685 ret
= lxc_sync_init(handler
);
1689 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1690 handler
->data_sock
);
1693 data_sock0
= handler
->data_sock
[0];
1694 data_sock1
= handler
->data_sock
[1];
1696 ret
= resolve_clone_flags(handler
);
1700 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1701 ret
= lxc_find_gateway_addresses(handler
);
1703 ERROR("Failed to find gateway addresses");
1708 if (!cgroup_ops
->payload_create(cgroup_ops
, handler
)) {
1709 ERROR("Failed creating cgroups");
1710 goto out_delete_net
;
1713 /* If the rootfs is not a blockdev, prevent the container from marking
1715 * If the container is unprivileged then skip rootfs pinning.
1717 if (!wants_to_map_ids
) {
1718 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1719 if (handler
->pinfd
== -1)
1720 INFO("Failed to pin the rootfs for container \"%s\"", handler
->name
);
1723 /* Create a process in a new set of namespaces. */
1724 handler
->ns_on_clone_flags
= handler
->ns_clone_flags
;
1725 if (handler
->ns_clone_flags
& CLONE_NEWUSER
) {
1726 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1727 * clone a new user namespace first and only later unshare our
1728 * network namespace to ensure that network devices ownership is
1731 handler
->ns_on_clone_flags
&= ~CLONE_NEWNET
;
1733 /* The cgroup namespace gets unshare()ed not clone()ed. */
1734 handler
->ns_on_clone_flags
&= ~CLONE_NEWCGROUP
;
1739 attacher_pid
= lxc_clone(do_share_ns
, handler
,
1740 CLONE_VFORK
| CLONE_VM
| CLONE_FILES
, NULL
);
1741 if (attacher_pid
< 0) {
1742 SYSERROR(LXC_CLONE_ERROR
);
1743 goto out_delete_net
;
1746 ret
= wait_for_pid(attacher_pid
);
1748 SYSERROR("Intermediate process failed");
1749 goto out_delete_net
;
1752 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
,
1753 CLONE_PIDFD
| handler
->ns_on_clone_flags
,
1756 if (handler
->pid
< 0) {
1757 SYSERROR(LXC_CLONE_ERROR
);
1758 goto out_delete_net
;
1760 TRACE("Cloned child process %d", handler
->pid
);
1762 if (handler
->pidfd
< 0) {
1763 handler
->proc_pidfd
= proc_pidfd_open(handler
->pid
);
1764 if (handler
->proc_pidfd
< 0 && (errno
!= ENOSYS
))
1765 goto out_delete_net
;
1768 ret
= snprintf(pidstr
, 20, "%d", handler
->pid
);
1769 if (ret
< 0 || ret
>= 20)
1770 goto out_delete_net
;
1772 ret
= setenv("LXC_PID", pidstr
, 1);
1774 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr
);
1776 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1777 if (handler
->ns_on_clone_flags
& ns_info
[i
].clone_flag
)
1778 INFO("Cloned %s", ns_info
[i
].flag_name
);
1780 if (!lxc_try_preserve_namespaces(handler
, handler
->ns_on_clone_flags
, handler
->pid
)) {
1781 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1782 goto out_delete_net
;
1785 lxc_sync_fini_child(handler
);
1787 if (lxc_abstract_unix_send_fds(handler
->data_sock
[0], &handler
->monitor_status_fd
, 1, NULL
, 0) < 0) {
1788 ERROR("Failed to send status file descriptor to child process");
1789 goto out_delete_net
;
1791 close_prot_errno_disarm(handler
->monitor_status_fd
);
1793 /* Map the container uids. The container became an invalid userid the
1794 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1795 * anything immediately, but allows the container to setuid(0) (0 being
1796 * mapped to something else on the host.) later to become a valid uid
1799 if (wants_to_map_ids
) {
1800 if (!handler
->conf
->ns_share
[LXC_NS_USER
] &&
1801 (handler
->conf
->ns_keep
& CLONE_NEWUSER
) == 0) {
1802 ret
= lxc_map_ids(id_map
, handler
->pid
);
1804 ERROR("Failed to set up id mapping.");
1805 goto out_delete_net
;
1810 ret
= lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
);
1812 goto out_delete_net
;
1814 ret
= lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
);
1816 goto out_delete_net
;
1818 if (!cgroup_ops
->setup_limits_legacy(cgroup_ops
, handler
->conf
, false)) {
1819 ERROR("Failed to setup cgroup limits for container \"%s\"", name
);
1820 goto out_delete_net
;
1823 if (!cgroup_ops
->payload_enter(cgroup_ops
, handler
)) {
1824 goto out_delete_net
;
1827 if (!cgroup_ops
->payload_delegate_controllers(cgroup_ops
)) {
1828 ERROR("Failed to delegate controllers to payload cgroup");
1829 goto out_delete_net
;
1832 if (!cgroup_ops
->setup_limits(cgroup_ops
, handler
)) {
1833 ERROR("Failed to setup cgroup limits for container \"%s\"", name
);
1834 goto out_delete_net
;
1837 if (!cgroup_ops
->chown(cgroup_ops
, handler
->conf
))
1838 goto out_delete_net
;
1840 /* If not done yet, we're now ready to preserve the network namespace */
1841 if (handler
->nsfd
[LXC_NS_NET
] < 0) {
1842 ret
= lxc_try_preserve_ns(handler
->pid
, "net");
1844 if (ret
!= -EOPNOTSUPP
) {
1845 SYSERROR("Failed to preserve net namespace");
1846 goto out_delete_net
;
1849 handler
->nsfd
[LXC_NS_NET
] = ret
;
1850 DEBUG("Preserved net namespace via fd %d", ret
);
1853 ret
= lxc_netns_set_nsid(handler
->nsfd
[LXC_NS_NET
]);
1855 SYSWARN("Failed to allocate new network namespace id");
1857 TRACE("Allocated new network namespace id");
1859 /* Create the network configuration. */
1860 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1861 ret
= lxc_create_network(handler
);
1863 ERROR("Failed to create the network");
1864 goto out_delete_net
;
1867 ret
= lxc_network_send_to_child(handler
);
1869 ERROR("Failed to send veth names to child");
1870 goto out_delete_net
;
1874 if (!lxc_list_empty(&conf
->procs
)) {
1875 ret
= setup_proc_filesystem(&conf
->procs
, handler
->pid
);
1877 goto out_delete_net
;
1880 /* Tell the child to continue its initialization. We'll get
1881 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1883 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
);
1885 goto out_delete_net
;
1887 if (!lxc_list_empty(&conf
->limits
)) {
1888 ret
= setup_resource_limits(&conf
->limits
, handler
->pid
);
1890 ERROR("Failed to setup resource limits");
1891 goto out_delete_net
;
1895 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
);
1897 goto out_delete_net
;
1899 if (!cgroup_ops
->setup_limits_legacy(cgroup_ops
, handler
->conf
, true)) {
1900 ERROR("Failed to setup legacy device cgroup controller limits");
1901 goto out_delete_net
;
1903 TRACE("Set up legacy device cgroup controller limits");
1905 if (!cgroup_ops
->devices_activate(cgroup_ops
, handler
)) {
1906 ERROR("Failed to setup cgroup2 device controller limits");
1907 goto out_delete_net
;
1909 TRACE("Set up cgroup2 device controller limits");
1911 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1912 /* Now we're ready to preserve the cgroup namespace */
1913 ret
= lxc_try_preserve_ns(handler
->pid
, "cgroup");
1915 if (ret
!= -EOPNOTSUPP
) {
1916 SYSERROR("Failed to preserve cgroup namespace");
1917 goto out_delete_net
;
1920 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1921 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1925 /* Run any host-side start hooks */
1926 ret
= run_lxc_hooks(name
, "start-host", conf
, NULL
);
1928 ERROR("Failed to run lxc.hook.start-host");
1929 goto out_delete_net
;
1932 /* Tell the child to complete its initialization and wait for it to exec
1933 * or return an error. (The child will never return
1934 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1935 * lxc_sync_barrier_child to return success, or return a different
1936 * value, causing us to error out).
1938 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
);
1940 goto out_delete_net
;
1942 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1943 ret
= lxc_network_recv_name_and_ifindex_from_child(handler
);
1945 ERROR("Failed to receive names and ifindices for network devices from child");
1946 goto out_delete_net
;
1950 /* Now all networks are created, network devices are moved into place,
1951 * and the correct names and ifindices in the respective namespaces have
1952 * been recorded. The corresponding structs have now all been filled. So
1953 * log them for debugging purposes.
1955 lxc_log_configured_netdevs(conf
);
1957 /* Read tty fds allocated by child. */
1958 ret
= lxc_recv_ttys_from_child(handler
);
1960 ERROR("Failed to receive tty info from child process");
1961 goto out_delete_net
;
1964 ret
= lxc_seccomp_recv_notifier_fd(&handler
->conf
->seccomp
, data_sock1
);
1966 SYSERROR("Failed to receive seccomp notify fd from child");
1967 goto out_delete_net
;
1970 ret
= handler
->ops
->post_start(handler
, handler
->data
);
1974 ret
= lxc_set_state(name
, handler
, RUNNING
);
1976 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING
));
1980 lxc_sync_fini(handler
);
1985 if (handler
->ns_clone_flags
& CLONE_NEWNET
)
1986 lxc_delete_network(handler
);
1989 lxc_abort(name
, handler
);
1992 lxc_sync_fini(handler
);
1993 if (handler
->pinfd
>= 0) {
1994 close(handler
->pinfd
);
1995 handler
->pinfd
= -1;
2001 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
2002 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
2003 bool daemonize
, int *error_num
)
2006 struct lxc_conf
*conf
= handler
->conf
;
2007 struct cgroup_ops
*cgroup_ops
;
2009 ret
= lxc_init(name
, handler
);
2011 ERROR("Failed to initialize container \"%s\"", name
);
2012 goto out_fini_nonet
;
2015 handler
->data
= data
;
2016 handler
->daemonize
= daemonize
;
2017 cgroup_ops
= handler
->cgroup_ops
;
2019 if (!attach_block_device(handler
->conf
)) {
2020 ERROR("Failed to attach block device");
2022 goto out_fini_nonet
;
2025 if (!cgroup_ops
->monitor_create(cgroup_ops
, handler
)) {
2026 ERROR("Failed to create monitor cgroup");
2028 goto out_fini_nonet
;
2031 if (!cgroup_ops
->monitor_enter(cgroup_ops
, handler
)) {
2032 ERROR("Failed to enter monitor cgroup");
2034 goto out_fini_nonet
;
2037 if (!cgroup_ops
->monitor_delegate_controllers(cgroup_ops
)) {
2038 ERROR("Failed to delegate controllers to monitor cgroup");
2040 goto out_fini_nonet
;
2043 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
2044 /* If the backing store is a device, mount it here and now. */
2045 if (rootfs_is_blockdev(conf
)) {
2046 ret
= unshare(CLONE_NEWNS
);
2048 ERROR("Failed to unshare CLONE_NEWNS");
2049 goto out_fini_nonet
;
2051 INFO("Unshared CLONE_NEWNS");
2053 remount_all_slave();
2054 ret
= lxc_setup_rootfs_prepare_root(conf
, name
, lxcpath
);
2056 ERROR("Error setting up rootfs mount as root before spawn");
2057 goto out_fini_nonet
;
2059 INFO("Set up container rootfs as host root");
2063 ret
= lxc_spawn(handler
);
2065 ERROR("Failed to spawn container \"%s\"", name
);
2066 goto out_detach_blockdev
;
2069 handler
->conf
->reboot
= REBOOT_NONE
;
2071 ret
= lxc_poll(name
, handler
);
2073 ERROR("LXC mainloop exited with error: %d", ret
);
2077 if (!handler
->init_died
&& handler
->pid
> 0) {
2078 ERROR("Child process is not killed");
2083 status
= lxc_wait_for_pid_status(handler
->pid
);
2085 SYSERROR("Failed to retrieve status for %d", handler
->pid
);
2087 /* If the child process exited but was not signaled, it didn't call
2088 * reboot. This should mean it was an lxc-execute which simply exited.
2089 * In any case, treat it as a 'halt'.
2091 if (WIFSIGNALED(status
)) {
2092 switch(WTERMSIG(status
)) {
2093 case SIGINT
: /* halt */
2094 DEBUG("Container \"%s\" is halting", name
);
2096 case SIGHUP
: /* reboot */
2097 DEBUG("Container \"%s\" is rebooting", name
);
2098 handler
->conf
->reboot
= REBOOT_REQ
;
2100 case SIGSYS
: /* seccomp */
2101 DEBUG("Container \"%s\" violated its seccomp policy", name
);
2104 DEBUG("Unknown exit status for container \"%s\" init %d", name
, WTERMSIG(status
));
2109 ret
= lxc_restore_phys_nics_to_netns(handler
);
2111 ERROR("Failed to move physical network devices back to parent "
2112 "network namespace");
2114 if (handler
->pinfd
>= 0) {
2115 close(handler
->pinfd
);
2116 handler
->pinfd
= -1;
2119 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
2120 lxc_error_set_and_log(handler
->pid
, status
);
2122 *error_num
= handler
->exit_status
;
2125 lxc_delete_network(handler
);
2127 out_detach_blockdev
:
2128 detach_block_device(handler
->conf
);
2131 lxc_fini(name
, handler
);
2135 lxc_abort(name
, handler
);
2143 static int start(struct lxc_handler
*handler
, void* data
)
2145 struct start_args
*arg
= data
;
2147 NOTICE("Exec'ing \"%s\"", arg
->argv
[0]);
2149 execvp(arg
->argv
[0], arg
->argv
);
2150 SYSERROR("Failed to exec \"%s\"", arg
->argv
[0]);
2154 static int post_start(struct lxc_handler
*handler
, void* data
)
2156 struct start_args
*arg
= data
;
2158 NOTICE("Started \"%s\" with pid \"%d\"", arg
->argv
[0], handler
->pid
);
2162 static struct lxc_operations start_ops
= {
2164 .post_start
= post_start
2167 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
2168 const char *lxcpath
, bool daemonize
, int *error_num
)
2170 struct start_args start_arg
= {
2174 TRACE("Doing lxc_start");
2175 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, daemonize
, error_num
);
2178 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
2181 char destroy
[PATH_MAX
];
2182 struct lxc_container
*c
;
2186 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
2187 bret
= do_destroy_container(handler
);
2189 ERROR("Error destroying rootfs for container \"%s\"", name
);
2193 INFO("Destroyed rootfs for container \"%s\"", name
);
2195 ret
= snprintf(destroy
, PATH_MAX
, "%s/%s", handler
->lxcpath
, name
);
2196 if (ret
< 0 || ret
>= PATH_MAX
) {
2197 ERROR("Error destroying directory for container \"%s\"", name
);
2201 c
= lxc_container_new(name
, handler
->lxcpath
);
2203 if (container_disk_lock(c
)) {
2204 INFO("Could not update lxc_snapshots file");
2205 lxc_container_put(c
);
2207 mod_all_rdeps(c
, false);
2208 container_disk_unlock(c
);
2209 lxc_container_put(c
);
2213 if (!handler
->am_root
)
2214 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
2215 destroy
, "lxc_rmdir_onedev_wrapper");
2217 ret
= lxc_rmdir_onedev(destroy
, NULL
);
2220 ERROR("Error destroying directory for container \"%s\"", name
);
2223 INFO("Destroyed directory for container \"%s\"", name
);
2226 static int lxc_rmdir_onedev_wrapper(void *data
)
2228 char *arg
= (char *) data
;
2229 return lxc_rmdir_onedev(arg
, NULL
);
2232 static bool do_destroy_container(struct lxc_handler
*handler
)
2236 if (!handler
->am_root
) {
2237 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
2238 handler
->conf
, "storage_destroy_wrapper");
2245 return storage_destroy(handler
->conf
);