]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Serge Hallyn <serge@hallyn.com>
9 * Christian Brauner <christian.brauner@ubuntu.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
40 #include <sys/mount.h>
41 #include <sys/param.h>
42 #include <sys/prctl.h>
43 #include <sys/socket.h>
45 #include <sys/syscall.h>
46 #include <sys/types.h>
55 #include "commands_utils.h"
58 #include "confile_utils.h"
60 #include "file_utils.h"
64 #include "lxccontainer.h"
66 #include "lxcseccomp.h"
69 #include "memory_utils.h"
71 #include "namespace.h"
73 #include "raw_syscalls.h"
75 #include "storage/storage.h"
76 #include "storage/storage_utils.h"
78 #include "syscall_wrappers.h"
83 #include <sys/capability.h>
87 #include "include/strlcpy.h"
90 lxc_log_define(start
, lxc
);
92 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
93 static bool do_destroy_container(struct lxc_handler
*handler
);
94 static int lxc_rmdir_onedev_wrapper(void *data
);
95 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
98 static void print_top_failing_dir(const char *path
)
100 __do_free
char *copy
;
104 copy
= must_copy_string(path
);
106 e
= copy
+ strlen(path
);
109 while (p
< e
&& *p
== '/')
112 while (p
< e
&& *p
!= '/')
118 ret
= access(copy
, X_OK
);
120 SYSERROR("Could not access %s. Please grant it x "
121 "access, or add an ACL for the container "
129 static void lxc_put_nsfds(struct lxc_handler
*handler
)
133 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
134 if (handler
->nsfd
[i
] < 0)
137 close(handler
->nsfd
[i
]);
138 handler
->nsfd
[i
] = -EBADF
;
142 static int lxc_try_preserve_ns(const int pid
, const char *ns
)
146 fd
= lxc_preserve_ns(pid
, ns
);
148 if (errno
!= ENOENT
) {
149 SYSERROR("Failed to preserve %s namespace", ns
);
153 SYSWARN("Kernel does not support preserving %s namespaces", ns
);
160 /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
161 * specified in ns_clone_flags.
162 * Return true on success, false on failure.
164 static bool lxc_try_preserve_namespaces(struct lxc_handler
*handler
,
165 int ns_clone_flags
, pid_t pid
)
169 for (i
= 0; i
< LXC_NS_MAX
; i
++)
170 handler
->nsfd
[i
] = -EBADF
;
172 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
175 if ((ns_clone_flags
& ns_info
[i
].clone_flag
) == 0)
178 fd
= lxc_try_preserve_ns(pid
, ns_info
[i
].proc_name
);
180 /* Do not fail to start container on kernels that do
181 * not support interacting with namespaces through
184 if (fd
== -EOPNOTSUPP
)
187 lxc_put_nsfds(handler
);
191 handler
->nsfd
[i
] = fd
;
192 DEBUG("Preserved %s namespace via fd %d", ns_info
[i
].proc_name
,
199 static inline bool match_stdfds(int fd
)
201 return (fd
== STDIN_FILENO
|| fd
== STDOUT_FILENO
|| fd
== STDERR_FILENO
);
205 static bool match_dlog_fds(struct dirent
*direntp
)
207 char path
[PATH_MAX
] = {0};
208 char link
[PATH_MAX
] = {0};
212 ret
= snprintf(path
, PATH_MAX
, "/proc/self/fd/%s", direntp
->d_name
);
213 if (ret
< 0 || ret
>= PATH_MAX
) {
214 ERROR("Failed to create file descriptor name");
218 linklen
= readlink(path
, link
, PATH_MAX
);
220 SYSERROR("Failed to read link path - \"%s\"", path
);
222 } else if (linklen
>= PATH_MAX
) {
223 ERROR("The name of link path is too long - \"%s\"", path
);
227 if (strcmp(link
, "/dev/log_main") == 0 ||
228 strcmp(link
, "/dev/log_system") == 0 ||
229 strcmp(link
, "/dev/log_radio") == 0)
236 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
237 int *fds_to_ignore
, size_t len_fds
)
242 struct dirent
*direntp
;
244 if (conf
&& conf
->close_all_fds
)
248 dir
= opendir("/proc/self/fd");
250 SYSWARN("Failed to open directory");
256 while ((direntp
= readdir(dir
))) {
258 struct lxc_list
*cur
;
259 bool matched
= false;
261 if (strcmp(direntp
->d_name
, ".") == 0)
264 if (strcmp(direntp
->d_name
, "..") == 0)
267 ret
= lxc_safe_int(direntp
->d_name
, &fd
);
269 INFO("Could not parse file descriptor for \"%s\"", direntp
->d_name
);
273 for (i
= 0; i
< len_fds
; i
++)
274 if (fds_to_ignore
[i
] == fd
)
277 if (fd
== fddir
|| fd
== lxc_log_fd
||
278 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
281 /* Keep state clients that wait on reboots. */
283 lxc_list_for_each(cur
, &conf
->state_clients
) {
284 struct lxc_state_client
*client
= cur
->elem
;
286 if (client
->clientfd
!= fd
)
297 if (current_config
&& fd
== current_config
->logfd
)
300 if (match_stdfds(fd
))
304 if (match_dlog_fds(direntp
))
311 INFO("Closed inherited fd %d", fd
);
314 WARN("Inherited fd %d", fd
);
317 /* Only enable syslog at this point to avoid the above logging function
318 * to open a new fd and make the check_inherited function enter an
321 lxc_log_enable_syslog();
323 closedir(dir
); /* cannot fail */
327 static int setup_signal_fd(sigset_t
*oldmask
)
331 const int signals
[] = {SIGBUS
, SIGILL
, SIGSEGV
, SIGWINCH
};
333 /* Block everything except serious error signals. */
334 ret
= sigfillset(&mask
);
338 for (int sig
= 0; sig
< (sizeof(signals
) / sizeof(signals
[0])); sig
++) {
339 ret
= sigdelset(&mask
, signals
[sig
]);
344 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, oldmask
);
346 SYSERROR("Failed to set signal mask");
350 ret
= signalfd(-1, &mask
, SFD_CLOEXEC
);
352 SYSERROR("Failed to create signal file descriptor");
356 TRACE("Created signal file descriptor %d", ret
);
361 static int signal_handler(int fd
, uint32_t events
, void *data
,
362 struct lxc_epoll_descr
*descr
)
366 struct signalfd_siginfo siginfo
;
367 struct lxc_handler
*hdlr
= data
;
369 ret
= lxc_read_nointr(fd
, &siginfo
, sizeof(siginfo
));
371 ERROR("Failed to read signal info from signal file descriptor %d", fd
);
372 return LXC_MAINLOOP_ERROR
;
375 if (ret
!= sizeof(siginfo
)) {
376 ERROR("Unexpected size for struct signalfd_siginfo");
380 /* Check whether init is running. */
382 ret
= waitid(P_PID
, hdlr
->pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
383 if (ret
== 0 && info
.si_pid
== hdlr
->pid
)
384 hdlr
->init_died
= true;
386 /* Try to figure out a reasonable exit status to report. */
387 if (hdlr
->init_died
) {
388 switch (info
.si_code
) {
390 hdlr
->exit_status
= info
.si_status
<< 8;
395 hdlr
->exit_status
= info
.si_status
<< 8 | 0x7f;
398 /* Huh? The waitid() told us it's dead *and* continued? */
399 WARN("Init %d dead and continued?", hdlr
->pid
);
400 hdlr
->exit_status
= 1;
403 ERROR("Unknown si_code: %d", info
.si_code
);
404 hdlr
->exit_status
= 1;
408 if (siginfo
.ssi_signo
== SIGHUP
) {
409 kill(hdlr
->pid
, SIGTERM
);
410 INFO("Killing %d since terminal hung up", hdlr
->pid
);
411 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
412 : LXC_MAINLOOP_CONTINUE
;
415 if (siginfo
.ssi_signo
!= SIGCHLD
) {
416 kill(hdlr
->pid
, siginfo
.ssi_signo
);
417 INFO("Forwarded signal %d to pid %d", siginfo
.ssi_signo
, hdlr
->pid
);
418 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
419 : LXC_MAINLOOP_CONTINUE
;
422 /* More robustness, protect ourself from a SIGCHLD sent
423 * by a process different from the container init.
425 if (siginfo
.ssi_pid
!= hdlr
->pid
) {
426 NOTICE("Received %d from pid %d instead of container init %d",
427 siginfo
.ssi_signo
, siginfo
.ssi_pid
, hdlr
->pid
);
428 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
429 : LXC_MAINLOOP_CONTINUE
;
432 if (siginfo
.ssi_code
== CLD_STOPPED
) {
433 INFO("Container init process was stopped");
434 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
435 : LXC_MAINLOOP_CONTINUE
;
438 if (siginfo
.ssi_code
== CLD_CONTINUED
) {
439 INFO("Container init process was continued");
440 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
441 : LXC_MAINLOOP_CONTINUE
;
444 DEBUG("Container init process %d exited", hdlr
->pid
);
446 return LXC_MAINLOOP_CLOSE
;
449 int lxc_serve_state_clients(const char *name
, struct lxc_handler
*handler
,
454 struct lxc_list
*cur
, *next
;
455 struct lxc_state_client
*client
;
456 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
459 handler
->state
= RUNNING
;
461 handler
->state
= state
;
463 TRACE("Set container state to %s", lxc_state2str(state
));
465 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
466 TRACE("No state clients registered");
470 retlen
= strlcpy(msg
.name
, name
, sizeof(msg
.name
));
471 if (retlen
>= sizeof(msg
.name
))
474 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
477 if (client
->states
[state
] == 0) {
478 TRACE("State %s not registered for state client %d",
479 lxc_state2str(state
), client
->clientfd
);
483 TRACE("Sending state %s to state client %d",
484 lxc_state2str(state
), client
->clientfd
);
486 ret
= lxc_send_nointr(client
->clientfd
, &msg
, sizeof(msg
), MSG_NOSIGNAL
);
488 SYSERROR("Failed to send message to client");
490 /* kick client from list */
492 close(client
->clientfd
);
500 static int lxc_serve_state_socket_pair(const char *name
,
501 struct lxc_handler
*handler
,
506 if (!handler
->daemonize
||
507 handler
->state_socket_pair
[1] < 0 ||
511 /* Close read end of the socket pair. */
512 close(handler
->state_socket_pair
[0]);
513 handler
->state_socket_pair
[0] = -1;
516 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
517 &(int){state
}, sizeof(int));
519 SYSERROR("Failed to send state to %d", handler
->state_socket_pair
[1]);
527 if (ret
!= sizeof(int)) {
528 ERROR("Message too long : %d", handler
->state_socket_pair
[1]);
532 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
533 handler
->state_socket_pair
[1]);
535 /* Close write end of the socket pair. */
536 close(handler
->state_socket_pair
[1]);
537 handler
->state_socket_pair
[1] = -1;
542 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
547 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
549 ERROR("Failed to synchronize via anonymous pair of unix sockets");
553 ret
= lxc_serve_state_clients(name
, handler
, state
);
557 /* This function will try to connect to the legacy lxc-monitord state
558 * server and only exists for backwards compatibility.
560 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
565 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
568 bool has_console
= true;
569 struct lxc_epoll_descr descr
, descr_console
;
571 if (handler
->conf
->console
.path
&&
572 strcmp(handler
->conf
->console
.path
, "none") == 0)
575 ret
= lxc_mainloop_open(&descr
);
577 ERROR("Failed to create mainloop");
582 ret
= lxc_mainloop_open(&descr_console
);
584 ERROR("Failed to create console mainloop");
589 ret
= lxc_mainloop_add_handler(&descr
, handler
->sigfd
, signal_handler
, handler
);
591 ERROR("Failed to add signal handler for %d to mainloop", handler
->sigfd
);
592 goto out_mainloop_console
;
596 struct lxc_terminal
*console
= &handler
->conf
->console
;
598 ret
= lxc_terminal_mainloop_add(&descr
, console
);
600 ERROR("Failed to add console handlers to mainloop");
601 goto out_mainloop_console
;
604 ret
= lxc_terminal_mainloop_add(&descr_console
, console
);
606 ERROR("Failed to add console handlers to console mainloop");
607 goto out_mainloop_console
;
610 handler
->conf
->console
.descr
= &descr
;
613 ret
= lxc_cmd_mainloop_add(name
, &descr
, handler
);
615 ERROR("Failed to add command handler to mainloop");
616 goto out_mainloop_console
;
619 TRACE("Mainloop is ready");
621 ret
= lxc_mainloop(&descr
, -1);
624 if (ret
< 0 || !handler
->init_died
)
625 goto out_mainloop_console
;
628 ret
= lxc_mainloop(&descr_console
, 0);
630 out_mainloop_console
:
632 lxc_mainloop_close(&descr_console
);
633 TRACE("Closed console mainloop");
637 lxc_mainloop_close(&descr
);
638 TRACE("Closed mainloop");
641 close(handler
->sigfd
);
642 TRACE("Closed signal file descriptor %d", handler
->sigfd
);
643 handler
->sigfd
= -EBADF
;
648 void lxc_zero_handler(struct lxc_handler
*handler
)
652 memset(handler
, 0, sizeof(struct lxc_handler
));
658 for (i
= 0; i
< LXC_NS_MAX
; i
++)
659 handler
->nsfd
[i
] = -1;
661 handler
->data_sock
[0] = -1;
662 handler
->data_sock
[1] = -1;
664 handler
->state_socket_pair
[0] = -1;
665 handler
->state_socket_pair
[1] = -1;
667 handler
->sync_sock
[0] = -1;
668 handler
->sync_sock
[1] = -1;
671 void lxc_free_handler(struct lxc_handler
*handler
)
673 if (handler
->pinfd
>= 0)
674 close(handler
->pinfd
);
676 if (handler
->sigfd
>= 0)
677 close(handler
->sigfd
);
679 lxc_put_nsfds(handler
);
681 if (handler
->conf
&& handler
->conf
->reboot
== REBOOT_NONE
)
682 if (handler
->conf
->maincmd_fd
>= 0)
683 lxc_abstract_unix_close(handler
->conf
->maincmd_fd
);
685 if (handler
->state_socket_pair
[0] >= 0)
686 close(handler
->state_socket_pair
[0]);
688 if (handler
->state_socket_pair
[1] >= 0)
689 close(handler
->state_socket_pair
[1]);
691 if (handler
->cgroup_ops
)
692 cgroup_exit(handler
->cgroup_ops
);
694 handler
->conf
= NULL
;
699 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
700 const char *lxcpath
, bool daemonize
)
703 struct lxc_handler
*handler
;
705 handler
= malloc(sizeof(*handler
));
709 memset(handler
, 0, sizeof(*handler
));
711 /* Note that am_guest_unpriv() checks the effective uid. We
712 * probably don't care if we are real root only if we are running
713 * as root so this should be fine.
715 handler
->am_root
= !am_guest_unpriv();
716 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
717 handler
->conf
= conf
;
718 handler
->lxcpath
= lxcpath
;
720 handler
->sigfd
= -EBADF
;
721 handler
->init_died
= false;
722 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
723 if (handler
->conf
->reboot
== REBOOT_NONE
)
724 lxc_list_init(&handler
->conf
->state_clients
);
726 for (i
= 0; i
< LXC_NS_MAX
; i
++)
727 handler
->nsfd
[i
] = -1;
729 handler
->name
= name
;
731 if (daemonize
&& handler
->conf
->reboot
== REBOOT_NONE
) {
732 /* Create socketpair() to synchronize on daemonized startup.
733 * When the container reboots we don't need to synchronize
734 * again currently so don't open another socketpair().
736 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
737 handler
->state_socket_pair
);
739 ERROR("Failed to create anonymous pair of unix sockets");
743 TRACE("Created anonymous pair {%d,%d} of unix sockets",
744 handler
->state_socket_pair
[0],
745 handler
->state_socket_pair
[1]);
748 if (handler
->conf
->reboot
== REBOOT_NONE
) {
749 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
750 if (handler
->conf
->maincmd_fd
< 0) {
751 ERROR("Failed to set up command socket");
756 TRACE("Unix domain socket %d for command server is ready",
757 handler
->conf
->maincmd_fd
);
762 lxc_free_handler(handler
);
767 int lxc_init(const char *name
, struct lxc_handler
*handler
)
770 const char *loglevel
;
771 struct lxc_conf
*conf
= handler
->conf
;
773 handler
->monitor_pid
= lxc_raw_getpid();
776 TRACE("Initialized LSM");
778 ret
= lxc_read_seccomp_config(conf
);
780 ERROR("Failed loading seccomp policy");
781 goto out_close_maincmd_fd
;
783 TRACE("Read seccomp policy");
785 /* Begin by setting the state to STARTING. */
786 ret
= lxc_set_state(name
, handler
, STARTING
);
788 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING
));
789 goto out_close_maincmd_fd
;
791 TRACE("Set container state to \"STARTING\"");
793 /* Start of environment variable setup for hooks. */
794 ret
= setenv("LXC_NAME", name
, 1);
796 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name
);
799 ret
= setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1);
801 SYSERROR("Failed to set environment variable: "
802 "LXC_CONFIG_FILE=%s", conf
->rcfile
);
805 if (conf
->rootfs
.mount
) {
806 ret
= setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1);
808 SYSERROR("Failed to set environment variable: "
809 "LXC_ROOTFS_MOUNT=%s", conf
->rootfs
.mount
);
812 if (conf
->rootfs
.path
) {
813 ret
= setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1);
815 SYSERROR("Failed to set environment variable: "
816 "LXC_ROOTFS_PATH=%s", conf
->rootfs
.path
);
819 if (conf
->console
.path
) {
820 ret
= setenv("LXC_CONSOLE", conf
->console
.path
, 1);
822 SYSERROR("Failed to set environment variable: "
823 "LXC_CONSOLE=%s", conf
->console
.path
);
826 if (conf
->console
.log_path
) {
827 ret
= setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1);
829 SYSERROR("Failed to set environment variable: "
830 "LXC_CONSOLE_LOGPATH=%s", conf
->console
.log_path
);
833 if (cgns_supported()) {
834 ret
= setenv("LXC_CGNS_AWARE", "1", 1);
836 SYSERROR("Failed to set environment variable "
840 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
841 ret
= setenv("LXC_LOG_LEVEL", loglevel
, 1);
843 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
846 if (conf
->hooks_version
== 0)
847 ret
= setenv("LXC_HOOK_VERSION", "0", 1);
849 ret
= setenv("LXC_HOOK_VERSION", "1", 1);
851 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf
->hooks_version
);
852 /* End of environment variable setup for hooks. */
854 TRACE("Set environment variables");
856 ret
= run_lxc_hooks(name
, "pre-start", conf
, NULL
);
858 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name
);
861 TRACE("Ran pre-start hooks");
863 /* The signal fd has to be created before forking otherwise if the child
864 * process exits before we setup the signal fd, the event will be lost
865 * and the command will be stuck.
867 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
868 if (handler
->sigfd
< 0) {
869 ERROR("Failed to setup SIGCHLD fd handler.");
872 TRACE("Set up signal fd");
874 /* Do this after setting up signals since it might unblock SIGWINCH. */
875 ret
= lxc_terminal_setup(conf
);
877 ERROR("Failed to create console");
878 goto out_restore_sigmask
;
880 TRACE("Created console");
882 ret
= lxc_terminal_map_ids(conf
, &conf
->console
);
884 ERROR("Failed to chown console");
885 goto out_delete_terminal
;
887 TRACE("Chowned console");
889 handler
->cgroup_ops
= cgroup_init(handler
->conf
);
890 if (!handler
->cgroup_ops
) {
891 ERROR("Failed to initialize cgroup driver");
892 goto out_delete_terminal
;
894 TRACE("Initialized cgroup driver");
896 ret
= lsm_process_prepare(conf
, handler
->lxcpath
);
898 ERROR("Failed to initialize LSM");
899 goto out_destroy_cgroups
;
901 TRACE("Initialized LSM");
903 INFO("Container \"%s\" is initialized", name
);
907 handler
->cgroup_ops
->payload_destroy(handler
->cgroup_ops
, handler
);
908 handler
->cgroup_ops
->monitor_destroy(handler
->cgroup_ops
, handler
);
911 lxc_terminal_delete(&handler
->conf
->console
);
914 (void)pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
917 lxc_delete_tty(&conf
->ttys
);
920 (void)lxc_set_state(name
, handler
, ABORTING
);
922 out_close_maincmd_fd
:
923 lxc_abstract_unix_close(conf
->maincmd_fd
);
924 conf
->maincmd_fd
= -1;
928 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
932 struct lxc_list
*cur
, *next
;
933 char *namespaces
[LXC_NS_MAX
+ 1];
934 size_t namespace_count
= 0;
935 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
937 /* The STOPPING state is there for future cleanup code which can take
940 lxc_set_state(name
, handler
, STOPPING
);
942 self
= lxc_raw_getpid();
943 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
944 if (handler
->nsfd
[i
] < 0)
947 if (handler
->conf
->hooks_version
== 0)
948 ret
= asprintf(&namespaces
[namespace_count
],
949 "%s:/proc/%d/fd/%d", ns_info
[i
].proc_name
,
950 self
, handler
->nsfd
[i
]);
952 ret
= asprintf(&namespaces
[namespace_count
],
953 "/proc/%d/fd/%d", self
, handler
->nsfd
[i
]);
955 SYSERROR("Failed to allocate memory");
959 if (handler
->conf
->hooks_version
== 0) {
964 ret
= setenv(ns_info
[i
].env_name
, namespaces
[namespace_count
], 1);
966 SYSERROR("Failed to set environment variable %s=%s",
967 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
969 TRACE("Set environment variable %s=%s",
970 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
974 namespaces
[namespace_count
] = NULL
;
976 if (handler
->conf
->reboot
> REBOOT_NONE
) {
977 ret
= setenv("LXC_TARGET", "reboot", 1);
979 SYSERROR("Failed to set environment variable: "
980 "LXC_TARGET=reboot");
983 if (handler
->conf
->reboot
== REBOOT_NONE
) {
984 ret
= setenv("LXC_TARGET", "stop", 1);
986 SYSERROR("Failed to set environment variable: "
990 if (handler
->conf
->hooks_version
== 0)
991 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, namespaces
);
993 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, NULL
);
995 ERROR("Failed to run \"lxc.hook.stop\" hook");
997 while (namespace_count
--)
998 free(namespaces
[namespace_count
]);
1000 lsm_process_cleanup(handler
->conf
, handler
->lxcpath
);
1002 cgroup_ops
->payload_destroy(cgroup_ops
, handler
);
1003 cgroup_ops
->monitor_destroy(cgroup_ops
, handler
);
1005 if (handler
->conf
->reboot
== REBOOT_NONE
) {
1006 /* For all new state clients simply close the command socket.
1007 * This will inform all state clients that the container is
1008 * STOPPED and also prevents a race between a open()/close() on
1009 * the command socket causing a new process to get ECONNREFUSED
1010 * because we haven't yet closed the command socket.
1012 lxc_abstract_unix_close(handler
->conf
->maincmd_fd
);
1013 handler
->conf
->maincmd_fd
= -1;
1014 TRACE("Closed command socket");
1016 /* This function will try to connect to the legacy lxc-monitord
1017 * state server and only exists for backwards compatibility.
1019 lxc_monitor_send_state(name
, STOPPED
, handler
->lxcpath
);
1021 /* The command socket is closed so no one can acces the command
1022 * socket anymore so there's no need to lock it.
1024 handler
->state
= STOPPED
;
1025 TRACE("Set container state to \"STOPPED\"");
1027 lxc_set_state(name
, handler
, STOPPED
);
1030 ret
= run_lxc_hooks(name
, "post-stop", handler
->conf
, NULL
);
1032 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name
);
1033 if (handler
->conf
->reboot
> REBOOT_NONE
) {
1034 WARN("Container will be stopped instead of rebooted");
1035 handler
->conf
->reboot
= REBOOT_NONE
;
1037 ret
= setenv("LXC_TARGET", "stop", 1);
1039 WARN("Failed to set environment variable: "
1044 /* Reset mask set by setup_signal_fd. */
1045 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1047 SYSWARN("Failed to restore signal mask");
1049 lxc_terminal_delete(&handler
->conf
->console
);
1050 lxc_delete_tty(&handler
->conf
->ttys
);
1052 /* The command socket is now closed, no more state clients can register
1053 * themselves from now on. So free the list of state clients.
1055 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
1056 struct lxc_state_client
*client
= cur
->elem
;
1058 /* Keep state clients that want to be notified about reboots. */
1059 if ((handler
->conf
->reboot
> REBOOT_NONE
) &&
1060 (client
->states
[RUNNING
] == 2))
1063 /* close state client socket */
1065 close(client
->clientfd
);
1070 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= REBOOT_REQ
)
1071 lxc_destroy_container_on_signal(handler
, name
);
1073 lxc_free_handler(handler
);
1076 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
1080 lxc_set_state(name
, handler
, ABORTING
);
1082 if (handler
->pid
> 0) {
1083 ret
= kill(handler
->pid
, SIGKILL
);
1085 SYSERROR("Failed to send SIGKILL to %d", handler
->pid
);
1088 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
1093 static int do_start(void *data
)
1096 char path
[PATH_MAX
];
1099 struct lxc_list
*iterator
;
1102 int devnull_fd
= -1;
1103 struct lxc_handler
*handler
= data
;
1105 lxc_sync_fini_parent(handler
);
1107 /* This prctl must be before the synchro, so if the parent dies before
1108 * we set the parent death signal, we will detect its death with the
1109 * synchro right after, otherwise we have a window where the parent can
1110 * exit before we set the pdeath signal leading to a unsupervized
1113 ret
= lxc_set_death_signal(SIGKILL
, 0);
1115 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1116 goto out_warn_father
;
1119 ret
= lxc_ambient_caps_up();
1121 ERROR("Failed to raise ambient capabilities");
1122 goto out_warn_father
;
1125 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1127 SYSERROR("Failed to set signal mask");
1128 goto out_warn_father
;
1131 /* Don't leak the pinfd to the container. */
1132 if (handler
->pinfd
>= 0)
1133 close(handler
->pinfd
);
1135 ret
= lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
);
1137 goto out_warn_father
;
1139 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1140 * https://github.com/lxc/lxd/issues/1978.
1142 if ((handler
->ns_clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
1143 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
1144 ret
= unshare(CLONE_NEWNET
);
1146 SYSERROR("Failed to unshare CLONE_NEWNET");
1147 goto out_warn_father
;
1149 INFO("Unshared CLONE_NEWNET");
1152 /* Tell the parent task it can begin to configure the container and wait
1155 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
);
1159 ret
= lxc_network_recv_veth_names_from_parent(handler
);
1161 ERROR("Failed to receive veth names from parent");
1162 goto out_warn_father
;
1165 /* If we are in a new user namespace, become root there to have
1166 * privilege over our namespace.
1168 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
1169 if (!handler
->conf
->root_nsuid_map
)
1170 nsuid
= handler
->conf
->init_uid
;
1172 if (!handler
->conf
->root_nsgid_map
)
1173 nsgid
= handler
->conf
->init_gid
;
1175 if (!lxc_switch_uid_gid(nsuid
, nsgid
))
1176 goto out_warn_father
;
1178 /* Drop groups only after we switched to a valid gid in the new
1181 if (!lxc_setgroups(0, NULL
) &&
1182 (handler
->am_root
|| errno
!= EPERM
))
1183 goto out_warn_father
;
1185 ret
= prctl(PR_SET_DUMPABLE
, prctl_arg(1), prctl_arg(0),
1186 prctl_arg(0), prctl_arg(0));
1188 goto out_warn_father
;
1190 /* set{g,u}id() clears deathsignal */
1191 ret
= lxc_set_death_signal(SIGKILL
, 0);
1193 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1194 goto out_warn_father
;
1198 ret
= access(handler
->lxcpath
, X_OK
);
1200 print_top_failing_dir(handler
->lxcpath
);
1201 goto out_warn_father
;
1204 ret
= snprintf(path
, sizeof(path
), "%s/dev/null",
1205 handler
->conf
->rootfs
.mount
);
1206 if (ret
< 0 || ret
>= sizeof(path
))
1207 goto out_warn_father
;
1209 /* In order to checkpoint restore, we need to have everything in the
1210 * same mount namespace. However, some containers may not have a
1211 * reasonable /dev (in particular, they may not have /dev/null), so we
1212 * can't set init's std fds to /dev/null by opening it from inside the
1215 * If that's the case, fall back to using the host's /dev/null. This
1216 * means that migration won't work, but at least we won't spew output
1217 * where it isn't wanted.
1219 if (handler
->daemonize
&& !handler
->conf
->autodev
) {
1220 ret
= access(path
, F_OK
);
1222 devnull_fd
= open_devnull();
1225 goto out_warn_father
;
1226 WARN("Using /dev/null from the host for container "
1227 "init's standard file descriptors. Migration will "
1232 /* Ask father to setup cgroups and wait for him to finish. */
1233 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
);
1237 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1238 * earlier we end up with a wrong view of /proc/self/cgroup. For
1239 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1240 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1241 * /proc/self/cgroup would show us:
1245 * whereas it should actually show
1249 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1250 ret
= unshare(CLONE_NEWCGROUP
);
1252 if (errno
!= EINVAL
) {
1253 SYSERROR("Failed to unshare CLONE_NEWCGROUP");
1254 goto out_warn_father
;
1257 handler
->ns_clone_flags
&= ~CLONE_NEWCGROUP
;
1258 SYSINFO("Kernel does not support CLONE_NEWCGROUP");
1260 INFO("Unshared CLONE_NEWCGROUP");
1264 /* Add the requested environment variables to the current environment to
1265 * allow them to be used by the various hooks, such as the start hook
1268 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1269 ret
= putenv((char *)iterator
->elem
);
1271 SYSERROR("Failed to set environment variable: %s",
1272 (char *)iterator
->elem
);
1273 goto out_warn_father
;
1277 /* Setup the container, ip, names, utsname, ... */
1278 ret
= lxc_setup(handler
);
1279 close(handler
->data_sock
[1]);
1280 close(handler
->data_sock
[0]);
1282 ERROR("Failed to setup container \"%s\"", handler
->name
);
1283 goto out_warn_father
;
1286 /* Set the label to change to when we exec(2) the container's init. */
1287 ret
= lsm_process_label_set(NULL
, handler
->conf
, true);
1289 goto out_warn_father
;
1291 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1292 * before we aren't allowed anymore.
1294 if (handler
->conf
->no_new_privs
) {
1295 ret
= prctl(PR_SET_NO_NEW_PRIVS
, prctl_arg(1), prctl_arg(0),
1296 prctl_arg(0), prctl_arg(0));
1298 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1299 "execve() gainable privileges");
1300 goto out_warn_father
;
1302 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1306 /* Some init's such as busybox will set sane tty settings on stdin,
1307 * stdout, stderr which it thinks is the console. We already set them
1308 * the way we wanted on the real terminal, and we want init to do its
1309 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1310 * make sure that that pty is stdin,stdout,stderr.
1312 if (handler
->conf
->console
.slave
>= 0) {
1313 if (handler
->daemonize
|| !handler
->conf
->is_execute
)
1314 ret
= set_stdfds(handler
->conf
->console
.slave
);
1316 ret
= lxc_terminal_set_stdfds(handler
->conf
->console
.slave
);
1318 ERROR("Failed to redirect std{in,out,err} to pty file "
1319 "descriptor %d", handler
->conf
->console
.slave
);
1320 goto out_warn_father
;
1324 /* If we mounted a temporary proc, then unmount it now. */
1325 tmp_proc_unmount(handler
->conf
);
1327 ret
= lxc_seccomp_load(handler
->conf
);
1329 goto out_warn_father
;
1331 ret
= run_lxc_hooks(handler
->name
, "start", handler
->conf
, NULL
);
1333 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1335 goto out_warn_father
;
1338 close(handler
->sigfd
);
1340 if (handler
->conf
->console
.slave
< 0 && handler
->daemonize
) {
1341 if (devnull_fd
< 0) {
1342 devnull_fd
= open_devnull();
1344 goto out_warn_father
;
1347 ret
= set_stdfds(devnull_fd
);
1349 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1350 goto out_warn_father
;
1354 if (devnull_fd
>= 0) {
1361 if (handler
->conf
->init_cwd
) {
1362 ret
= chdir(handler
->conf
->init_cwd
);
1364 SYSERROR("Could not change directory to \"%s\"",
1365 handler
->conf
->init_cwd
);
1366 goto out_warn_father
;
1370 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
);
1372 goto out_warn_father
;
1374 /* Reset the environment variables the user requested in a clear
1378 /* Don't error out though. */
1380 SYSERROR("Failed to clear environment.");
1382 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1383 ret
= putenv((char *)iterator
->elem
);
1385 SYSERROR("Failed to set environment variable: %s",
1386 (char *)iterator
->elem
);
1387 goto out_warn_father
;
1391 ret
= putenv("container=lxc");
1393 SYSERROR("Failed to set environment variable: container=lxc");
1394 goto out_warn_father
;
1397 if (handler
->conf
->ttys
.tty_names
) {
1398 ret
= putenv(handler
->conf
->ttys
.tty_names
);
1400 SYSERROR("Failed to set environment variable for container ptys");
1401 goto out_warn_father
;
1405 /* The container has been setup. We can now switch to an unprivileged
1408 new_uid
= handler
->conf
->init_uid
;
1409 new_gid
= handler
->conf
->init_gid
;
1411 /* Avoid unnecessary syscalls. */
1412 if (new_uid
== nsuid
)
1413 new_uid
= LXC_INVALID_UID
;
1415 if (new_gid
== nsgid
)
1416 new_gid
= LXC_INVALID_GID
;
1418 if (!lxc_switch_uid_gid(new_uid
, new_gid
))
1419 goto out_warn_father
;
1421 /* If we are in a new user namespace we already dropped all groups when
1422 * we switched to root in the new user namespace further above. Only
1423 * drop groups if we can, so ensure that we have necessary privilege.
1425 if (lxc_list_empty(&handler
->conf
->id_map
))
1427 if (lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
))
1429 if (!lxc_setgroups(0, NULL
))
1430 goto out_warn_father
;
1432 ret
= lxc_ambient_caps_down();
1434 ERROR("Failed to clear ambient capabilities");
1435 goto out_warn_father
;
1438 if (handler
->conf
->monitor_signal_pdeath
!= SIGKILL
) {
1439 ret
= lxc_set_death_signal(handler
->conf
->monitor_signal_pdeath
, 0);
1441 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d",
1442 handler
->conf
->monitor_signal_pdeath
);
1443 goto out_warn_father
;
1447 /* After this call, we are in error because this ops should not return
1450 handler
->ops
->start(handler
, handler
->data
);
1453 /* We want the parent to know something went wrong, so we return a
1454 * special error code.
1456 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1459 if (devnull_fd
>= 0)
1465 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1468 struct lxc_terminal_info
*tty
;
1470 int sock
= handler
->data_sock
[1];
1471 struct lxc_conf
*conf
= handler
->conf
;
1472 struct lxc_tty_info
*ttys
= &conf
->ttys
;
1474 if (!conf
->ttys
.max
)
1477 ttys
->tty
= malloc(sizeof(*ttys
->tty
) * ttys
->max
);
1481 for (i
= 0; i
< conf
->ttys
.max
; i
++) {
1484 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1488 tty
= &ttys
->tty
[i
];
1490 tty
->master
= ttyfds
[0];
1491 tty
->slave
= ttyfds
[1];
1492 TRACE("Received pty with master fd %d and slave fd %d from "
1493 "parent", tty
->master
, tty
->slave
);
1497 SYSERROR("Failed to receive %zu ttys from child", ttys
->max
);
1499 TRACE("Received %zu ttys from child", ttys
->max
);
1504 int resolve_clone_flags(struct lxc_handler
*handler
)
1507 struct lxc_conf
*conf
= handler
->conf
;
1509 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1510 if (conf
->ns_keep
> 0) {
1511 if ((conf
->ns_keep
& ns_info
[i
].clone_flag
) == 0)
1512 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1513 } else if (conf
->ns_clone
> 0) {
1514 if ((conf
->ns_clone
& ns_info
[i
].clone_flag
) > 0)
1515 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1517 if (i
== LXC_NS_USER
&& lxc_list_empty(&handler
->conf
->id_map
))
1520 if (i
== LXC_NS_NET
&& lxc_requests_empty_network(handler
))
1523 if (i
== LXC_NS_CGROUP
&& !cgns_supported())
1526 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1529 if (!conf
->ns_share
[i
])
1532 handler
->ns_clone_flags
&= ~ns_info
[i
].clone_flag
;
1533 TRACE("Sharing %s namespace", ns_info
[i
].proc_name
);
1539 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1540 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1541 * But since the memory between parent and child is shared on CLONE_VM this
1542 * would invalidate the getpid() cache that glibc used to maintain and so
1543 * getpid() in the child would return the parent's pid. This is all fixed in
1544 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1545 * not reset anymore.
1546 * However, if for whatever reason you - dear committer - somehow need to get the
1547 * pid of the dummy intermediate process for do_share_ns() you need to call
1548 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1551 static inline int do_share_ns(void *arg
)
1554 struct lxc_handler
*handler
= arg
;
1556 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1557 if (handler
->nsfd
[i
] < 0)
1560 ret
= setns(handler
->nsfd
[i
], 0);
1563 * Note that joining a user and/or mount namespace
1564 * requires the process is not multithreaded otherwise
1565 * setns() will fail here.
1567 SYSERROR("Failed to inherit %s namespace",
1568 ns_info
[i
].proc_name
);
1572 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1575 flags
= handler
->ns_on_clone_flags
;
1576 flags
|= CLONE_PARENT
;
1577 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, flags
);
1578 if (handler
->pid
< 0)
1584 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1585 * exec()s the requested container binary.
1586 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1587 * right here should be double checked if they'd pose a security risk. (For
1588 * example, any {u}mount() operations performed here will be reflected on the
1591 static int lxc_spawn(struct lxc_handler
*handler
)
1595 bool wants_to_map_ids
;
1596 struct lxc_list
*id_map
;
1597 const char *name
= handler
->name
;
1598 const char *lxcpath
= handler
->lxcpath
;
1599 bool share_ns
= false;
1600 struct lxc_conf
*conf
= handler
->conf
;
1601 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
1603 id_map
= &conf
->id_map
;
1604 wants_to_map_ids
= !lxc_list_empty(id_map
);
1606 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1607 if (!conf
->ns_share
[i
])
1610 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->ns_share
[i
], lxcpath
, ns_info
[i
].proc_name
);
1611 if (handler
->nsfd
[i
] < 0)
1617 ret
= lxc_sync_init(handler
);
1621 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1622 handler
->data_sock
);
1626 ret
= resolve_clone_flags(handler
);
1630 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1631 if (!lxc_list_empty(&conf
->network
)) {
1633 /* Find gateway addresses from the link device, which is
1634 * no longer accessible inside the container. Do this
1635 * before creating network interfaces, since goto
1636 * out_delete_net does not work before lxc_clone.
1638 ret
= lxc_find_gateway_addresses(handler
);
1640 ERROR("Failed to find gateway addresses");
1644 /* That should be done before the clone because we will
1645 * fill the netdev index and use them in the child.
1647 ret
= lxc_create_network_priv(handler
);
1649 ERROR("Failed to create the network");
1650 goto out_delete_net
;
1655 if (!cgroup_ops
->payload_create(cgroup_ops
, handler
)) {
1656 ERROR("Failed creating cgroups");
1657 goto out_delete_net
;
1660 /* If the rootfs is not a blockdev, prevent the container from marking
1662 * If the container is unprivileged then skip rootfs pinning.
1664 if (!wants_to_map_ids
) {
1665 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1666 if (handler
->pinfd
== -1)
1667 INFO("Failed to pin the rootfs for container \"%s\"", handler
->name
);
1670 /* Create a process in a new set of namespaces. */
1671 handler
->ns_on_clone_flags
= handler
->ns_clone_flags
;
1672 if (handler
->ns_clone_flags
& CLONE_NEWUSER
) {
1673 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1674 * clone a new user namespace first and only later unshare our
1675 * network namespace to ensure that network devices ownership is
1678 handler
->ns_on_clone_flags
&= ~CLONE_NEWNET
;
1680 /* The cgroup namespace gets unshare()ed not clone()ed. */
1681 handler
->ns_on_clone_flags
&= ~CLONE_NEWCGROUP
;
1686 attacher_pid
= lxc_clone(do_share_ns
, handler
,
1687 CLONE_VFORK
| CLONE_VM
| CLONE_FILES
);
1688 if (attacher_pid
< 0) {
1689 SYSERROR(LXC_CLONE_ERROR
);
1690 goto out_delete_net
;
1693 ret
= wait_for_pid(attacher_pid
);
1695 SYSERROR("Intermediate process failed");
1696 goto out_delete_net
;
1699 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
,
1700 handler
->ns_on_clone_flags
);
1702 if (handler
->pid
< 0) {
1703 SYSERROR(LXC_CLONE_ERROR
);
1704 goto out_delete_net
;
1706 TRACE("Cloned child process %d", handler
->pid
);
1708 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1709 if (handler
->ns_on_clone_flags
& ns_info
[i
].clone_flag
)
1710 INFO("Cloned %s", ns_info
[i
].flag_name
);
1712 if (!lxc_try_preserve_namespaces(handler
, handler
->ns_on_clone_flags
, handler
->pid
)) {
1713 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1714 goto out_delete_net
;
1717 lxc_sync_fini_child(handler
);
1719 /* Map the container uids. The container became an invalid userid the
1720 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1721 * anything immediately, but allows the container to setuid(0) (0 being
1722 * mapped to something else on the host.) later to become a valid uid
1725 if (wants_to_map_ids
) {
1726 if (!handler
->conf
->ns_share
[LXC_NS_USER
] &&
1727 (handler
->conf
->ns_keep
& CLONE_NEWUSER
) == 0) {
1728 ret
= lxc_map_ids(id_map
, handler
->pid
);
1730 ERROR("Failed to set up id mapping.");
1731 goto out_delete_net
;
1736 ret
= lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
);
1738 goto out_delete_net
;
1740 ret
= lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
);
1742 goto out_delete_net
;
1744 if (!cgroup_ops
->setup_limits(cgroup_ops
, handler
->conf
, false)) {
1745 ERROR("Failed to setup cgroup limits for container \"%s\"", name
);
1746 goto out_delete_net
;
1749 if (!cgroup_ops
->payload_enter(cgroup_ops
, handler
->pid
))
1750 goto out_delete_net
;
1752 if (!cgroup_ops
->chown(cgroup_ops
, handler
->conf
))
1753 goto out_delete_net
;
1755 /* Now we're ready to preserve the network namespace */
1756 ret
= lxc_try_preserve_ns(handler
->pid
, "net");
1758 if (ret
!= -EOPNOTSUPP
) {
1759 SYSERROR("Failed to preserve net namespace");
1760 goto out_delete_net
;
1763 handler
->nsfd
[LXC_NS_NET
] = ret
;
1764 DEBUG("Preserved net namespace via fd %d", ret
);
1766 ret
= lxc_netns_set_nsid(handler
->nsfd
[LXC_NS_NET
]);
1768 SYSWARN("Failed to allocate new network namespace id");
1770 TRACE("Allocated new network namespace id");
1773 /* Create the network configuration. */
1774 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1775 ret
= lxc_network_move_created_netdev_priv(handler
->lxcpath
,
1780 ERROR("Failed to create the configured network");
1781 goto out_delete_net
;
1784 ret
= lxc_create_network_unpriv(handler
->lxcpath
, handler
->name
,
1785 &conf
->network
, handler
->pid
, conf
->hooks_version
);
1787 ERROR("Failed to create the configured network");
1788 goto out_delete_net
;
1792 ret
= lxc_network_send_veth_names_to_child(handler
);
1794 ERROR("Failed to send veth names to child");
1795 goto out_delete_net
;
1798 if (!lxc_list_empty(&conf
->procs
)) {
1799 ret
= setup_proc_filesystem(&conf
->procs
, handler
->pid
);
1801 goto out_delete_net
;
1804 /* Tell the child to continue its initialization. We'll get
1805 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1807 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
);
1809 goto out_delete_net
;
1811 if (!lxc_list_empty(&conf
->limits
)) {
1812 ret
= setup_resource_limits(&conf
->limits
, handler
->pid
);
1814 ERROR("Failed to setup resource limits");
1815 goto out_delete_net
;
1819 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
);
1821 goto out_delete_net
;
1823 if (!cgroup_ops
->setup_limits(cgroup_ops
, handler
->conf
, true)) {
1824 ERROR("Failed to setup legacy device cgroup controller limits");
1825 goto out_delete_net
;
1827 TRACE("Set up legacy device cgroup controller limits");
1829 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1830 /* Now we're ready to preserve the cgroup namespace */
1831 ret
= lxc_try_preserve_ns(handler
->pid
, "cgroup");
1833 if (ret
!= -EOPNOTSUPP
) {
1834 SYSERROR("Failed to preserve cgroup namespace");
1835 goto out_delete_net
;
1838 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1839 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1843 ret
= snprintf(pidstr
, 20, "%d", handler
->pid
);
1844 if (ret
< 0 || ret
>= 20)
1845 goto out_delete_net
;
1847 ret
= setenv("LXC_PID", pidstr
, 1);
1849 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr
);
1851 /* Run any host-side start hooks */
1852 ret
= run_lxc_hooks(name
, "start-host", conf
, NULL
);
1854 ERROR("Failed to run lxc.hook.start-host");
1855 goto out_delete_net
;
1858 /* Tell the child to complete its initialization and wait for it to exec
1859 * or return an error. (The child will never return
1860 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1861 * lxc_sync_barrier_child to return success, or return a different
1862 * value, causing us to error out).
1864 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
);
1866 goto out_delete_net
;
1868 ret
= lxc_network_recv_name_and_ifindex_from_child(handler
);
1870 ERROR("Failed to receive names and ifindices for network "
1871 "devices from child");
1872 goto out_delete_net
;
1875 /* Now all networks are created, network devices are moved into place,
1876 * and the correct names and ifindices in the respective namespaces have
1877 * been recorded. The corresponding structs have now all been filled. So
1878 * log them for debugging purposes.
1880 lxc_log_configured_netdevs(conf
);
1882 /* Read tty fds allocated by child. */
1883 ret
= lxc_recv_ttys_from_child(handler
);
1885 ERROR("Failed to receive tty info from child process");
1886 goto out_delete_net
;
1889 ret
= handler
->ops
->post_start(handler
, handler
->data
);
1893 ret
= lxc_set_state(name
, handler
, RUNNING
);
1895 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING
));
1899 lxc_sync_fini(handler
);
1904 if (handler
->ns_clone_flags
& CLONE_NEWNET
)
1905 lxc_delete_network(handler
);
1908 lxc_abort(name
, handler
);
1911 lxc_sync_fini(handler
);
1912 if (handler
->pinfd
>= 0) {
1913 close(handler
->pinfd
);
1914 handler
->pinfd
= -1;
1920 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1921 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1922 bool daemonize
, int *error_num
)
1925 struct lxc_conf
*conf
= handler
->conf
;
1926 struct cgroup_ops
*cgroup_ops
;
1928 ret
= lxc_init(name
, handler
);
1930 ERROR("Failed to initialize container \"%s\"", name
);
1934 handler
->data
= data
;
1935 handler
->daemonize
= daemonize
;
1936 cgroup_ops
= handler
->cgroup_ops
;
1938 if (!attach_block_device(handler
->conf
)) {
1939 ERROR("Failed to attach block device");
1941 goto out_fini_nonet
;
1944 if (!cgroup_ops
->monitor_create(cgroup_ops
, handler
)) {
1945 ERROR("Failed to create monitor cgroup");
1947 goto out_fini_nonet
;
1950 if (!cgroup_ops
->monitor_enter(cgroup_ops
, handler
->monitor_pid
)) {
1951 ERROR("Failed to enter monitor cgroup");
1953 goto out_fini_nonet
;
1956 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1957 /* If the backing store is a device, mount it here and now. */
1958 if (rootfs_is_blockdev(conf
)) {
1959 ret
= unshare(CLONE_NEWNS
);
1961 ERROR("Failed to unshare CLONE_NEWNS");
1962 goto out_fini_nonet
;
1964 INFO("Unshared CLONE_NEWNS");
1966 remount_all_slave();
1967 ret
= lxc_setup_rootfs_prepare_root(conf
, name
, lxcpath
);
1969 ERROR("Error setting up rootfs mount as root before spawn");
1970 goto out_fini_nonet
;
1972 INFO("Set up container rootfs as host root");
1976 ret
= lxc_spawn(handler
);
1978 ERROR("Failed to spawn container \"%s\"", name
);
1979 goto out_detach_blockdev
;
1981 /* close parent side of data socket */
1982 close(handler
->data_sock
[0]);
1983 handler
->data_sock
[0] = -1;
1984 close(handler
->data_sock
[1]);
1985 handler
->data_sock
[1] = -1;
1987 handler
->conf
->reboot
= REBOOT_NONE
;
1989 ret
= lxc_poll(name
, handler
);
1991 ERROR("LXC mainloop exited with error: %d", ret
);
1995 if (!handler
->init_died
&& handler
->pid
> 0) {
1996 ERROR("Child process is not killed");
2001 status
= lxc_wait_for_pid_status(handler
->pid
);
2003 SYSERROR("Failed to retrieve status for %d", handler
->pid
);
2005 /* If the child process exited but was not signaled, it didn't call
2006 * reboot. This should mean it was an lxc-execute which simply exited.
2007 * In any case, treat it as a 'halt'.
2009 if (WIFSIGNALED(status
)) {
2010 switch(WTERMSIG(status
)) {
2011 case SIGINT
: /* halt */
2012 DEBUG("Container \"%s\" is halting", name
);
2014 case SIGHUP
: /* reboot */
2015 DEBUG("Container \"%s\" is rebooting", name
);
2016 handler
->conf
->reboot
= REBOOT_REQ
;
2018 case SIGSYS
: /* seccomp */
2019 DEBUG("Container \"%s\" violated its seccomp policy", name
);
2022 DEBUG("Unknown exit status for container \"%s\" init %d", name
, WTERMSIG(status
));
2027 ret
= lxc_restore_phys_nics_to_netns(handler
);
2029 ERROR("Failed to move physical network devices back to parent "
2030 "network namespace");
2032 if (handler
->pinfd
>= 0) {
2033 close(handler
->pinfd
);
2034 handler
->pinfd
= -1;
2037 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
2038 lxc_error_set_and_log(handler
->pid
, status
);
2040 *error_num
= handler
->exit_status
;
2043 lxc_delete_network(handler
);
2045 out_detach_blockdev
:
2046 detach_block_device(handler
->conf
);
2049 lxc_fini(name
, handler
);
2053 lxc_abort(name
, handler
);
2061 static int start(struct lxc_handler
*handler
, void* data
)
2063 struct start_args
*arg
= data
;
2065 NOTICE("Exec'ing \"%s\"", arg
->argv
[0]);
2067 execvp(arg
->argv
[0], arg
->argv
);
2068 SYSERROR("Failed to exec \"%s\"", arg
->argv
[0]);
2072 static int post_start(struct lxc_handler
*handler
, void* data
)
2074 struct start_args
*arg
= data
;
2076 NOTICE("Started \"%s\" with pid \"%d\"", arg
->argv
[0], handler
->pid
);
2080 static struct lxc_operations start_ops
= {
2082 .post_start
= post_start
2085 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
2086 const char *lxcpath
, bool daemonize
, int *error_num
)
2088 struct start_args start_arg
= {
2092 TRACE("Doing lxc_start");
2093 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, daemonize
, error_num
);
2096 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
2099 char destroy
[PATH_MAX
];
2100 struct lxc_container
*c
;
2104 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
2105 bret
= do_destroy_container(handler
);
2107 ERROR("Error destroying rootfs for container \"%s\"", name
);
2111 INFO("Destroyed rootfs for container \"%s\"", name
);
2113 ret
= snprintf(destroy
, PATH_MAX
, "%s/%s", handler
->lxcpath
, name
);
2114 if (ret
< 0 || ret
>= PATH_MAX
) {
2115 ERROR("Error destroying directory for container \"%s\"", name
);
2119 c
= lxc_container_new(name
, handler
->lxcpath
);
2121 if (container_disk_lock(c
)) {
2122 INFO("Could not update lxc_snapshots file");
2123 lxc_container_put(c
);
2125 mod_all_rdeps(c
, false);
2126 container_disk_unlock(c
);
2127 lxc_container_put(c
);
2131 if (!handler
->am_root
)
2132 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
2133 destroy
, "lxc_rmdir_onedev_wrapper");
2135 ret
= lxc_rmdir_onedev(destroy
, NULL
);
2138 ERROR("Error destroying directory for container \"%s\"", name
);
2141 INFO("Destroyed directory for container \"%s\"", name
);
2144 static int lxc_rmdir_onedev_wrapper(void *data
)
2146 char *arg
= (char *) data
;
2147 return lxc_rmdir_onedev(arg
, NULL
);
2150 static bool do_destroy_container(struct lxc_handler
*handler
)
2154 if (!handler
->am_root
) {
2155 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
2156 handler
->conf
, "storage_destroy_wrapper");
2163 return storage_destroy(handler
->conf
);