]>
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
41 #include <sys/mount.h>
42 #include <sys/param.h>
43 #include <sys/prctl.h>
44 #include <sys/socket.h>
46 #include <sys/syscall.h>
47 #include <sys/types.h>
52 #include <sys/capability.h>
55 #if !HAVE_DECL_PR_CAPBSET_DROP
56 #define PR_CAPBSET_DROP 24
59 #if !HAVE_DECL_PR_SET_NO_NEW_PRIVS
60 #define PR_SET_NO_NEW_PRIVS 38
63 #if !HAVE_DECL_PR_GET_NO_NEW_PRIVS
64 #define PR_GET_NO_NEW_PRIVS 39
71 #include "commands_utils.h"
73 #include "confile_utils.h"
78 #include "lxccontainer.h"
80 #include "lxcseccomp.h"
83 #include "namespace.h"
89 #include "storage/storage.h"
90 #include "storage/storage_utils.h"
92 lxc_log_define(lxc_start
, lxc
);
94 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
95 static bool do_destroy_container(struct lxc_handler
*handler
);
96 static int lxc_rmdir_onedev_wrapper(void *data
);
97 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
100 static void print_top_failing_dir(const char *path
)
104 char *copy
, *e
, *p
, saved
;
107 copy
= alloca(len
+ 1);
112 while (p
< e
&& *p
== '/')
115 while (p
< e
&& *p
!= '/')
121 ret
= access(copy
, X_OK
);
123 SYSERROR("Could not access %s. Please grant it x "
124 "access, or add an ACL for the container "
132 static void lxc_put_nsfds(struct lxc_handler
*handler
)
136 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
137 if (handler
->nsfd
[i
] < 0)
140 close(handler
->nsfd
[i
]);
141 handler
->nsfd
[i
] = -EBADF
;
145 /* lxc_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace specified
147 * Return true on success, false on failure.
149 static bool lxc_preserve_namespaces(struct lxc_handler
*handler
, int ns_clone_flags
, pid_t pid
)
153 for (i
= 0; i
< LXC_NS_MAX
; i
++)
154 handler
->nsfd
[i
] = -EBADF
;
156 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
157 if ((ns_clone_flags
& ns_info
[i
].clone_flag
) == 0)
160 handler
->nsfd
[i
] = lxc_preserve_ns(pid
, ns_info
[i
].proc_name
);
161 if (handler
->nsfd
[i
] < 0)
164 DEBUG("Preserved %s namespace via fd %d", ns_info
[i
].proc_name
, handler
->nsfd
[i
]);
171 SYSERROR("Kernel does not support attaching to %s namespaces",
172 ns_info
[i
].proc_name
);
174 SYSERROR("Failed to open file descriptor for %s namespace",
175 ns_info
[i
].proc_name
);
176 lxc_put_nsfds(handler
);
180 static int match_fd(int fd
)
182 return (fd
== 0 || fd
== 1 || fd
== 2);
185 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
186 int *fds_to_ignore
, size_t len_fds
)
191 struct dirent
*direntp
;
193 if (conf
&& conf
->close_all_fds
)
197 dir
= opendir("/proc/self/fd");
199 WARN("%s - Failed to open directory", strerror(errno
));
205 while ((direntp
= readdir(dir
))) {
207 struct lxc_list
*cur
;
208 bool matched
= false;
210 if (strcmp(direntp
->d_name
, ".") == 0)
213 if (strcmp(direntp
->d_name
, "..") == 0)
216 ret
= lxc_safe_int(direntp
->d_name
, &fd
);
218 INFO("Could not parse file descriptor for \"%s\"", direntp
->d_name
);
222 for (i
= 0; i
< len_fds
; i
++)
223 if (fds_to_ignore
[i
] == fd
)
226 if (fd
== fddir
|| fd
== lxc_log_fd
||
227 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
230 /* Keep state clients that wait on reboots. */
232 lxc_list_for_each(cur
, &conf
->state_clients
) {
233 struct lxc_state_client
*client
= cur
->elem
;
235 if (client
->clientfd
!= fd
)
246 if (current_config
&& fd
== current_config
->logfd
)
255 INFO("Closed inherited fd %d", fd
);
258 WARN("Inherited fd %d", fd
);
261 /* Only enable syslog at this point to avoid the above logging function
262 * to open a new fd and make the check_inherited function enter an
265 lxc_log_enable_syslog();
267 closedir(dir
); /* cannot fail */
271 static int setup_signal_fd(sigset_t
*oldmask
)
275 int signals
[] = {SIGBUS
, SIGILL
, SIGSEGV
, SIGWINCH
};
277 /* Block everything except serious error signals. */
278 ret
= sigfillset(&mask
);
282 for (sig
= 0; sig
< (sizeof(signals
) / sizeof(signals
[0])); sig
++) {
283 ret
= sigdelset(&mask
, signals
[sig
]);
288 ret
= sigprocmask(SIG_BLOCK
, &mask
, oldmask
);
290 SYSERROR("Failed to set signal mask");
294 ret
= signalfd(-1, &mask
, SFD_CLOEXEC
);
296 SYSERROR("Failed to create signal file descriptor");
300 TRACE("Created signal file descriptor %d", ret
);
305 static int signal_handler(int fd
, uint32_t events
, void *data
,
306 struct lxc_epoll_descr
*descr
)
310 struct signalfd_siginfo siginfo
;
311 struct lxc_handler
*hdlr
= data
;
313 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
315 ERROR("Failed to read signal info from signal file descriptor %d", fd
);
319 if (ret
!= sizeof(siginfo
)) {
320 ERROR("Unexpected size for struct signalfd_siginfo");
324 /* Check whether init is running. */
326 ret
= waitid(P_PID
, hdlr
->pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
327 if (ret
== 0 && info
.si_pid
== hdlr
->pid
)
328 hdlr
->init_died
= true;
330 /* Try to figure out a reasonable exit status to report. */
331 if (hdlr
->init_died
) {
332 switch (info
.si_code
) {
334 hdlr
->exit_status
= info
.si_status
<< 8;
339 hdlr
->exit_status
= info
.si_status
<< 8 | 0x7f;
342 /* Huh? The waitid() told us it's dead *and* continued? */
343 WARN("Init %d dead and continued?", hdlr
->pid
);
344 hdlr
->exit_status
= 1;
347 ERROR("Unknown si_code: %d", hdlr
->init_died
);
348 hdlr
->exit_status
= 1;
352 if (siginfo
.ssi_signo
== SIGHUP
) {
353 kill(hdlr
->pid
, SIGTERM
);
354 INFO("Killing %d since terminal hung up", hdlr
->pid
);
355 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
358 /* More robustness, protect ourself from a SIGCHLD sent
359 * by a process different from the container init.
361 if (siginfo
.ssi_pid
!= hdlr
->pid
) {
362 NOTICE("Received %d from pid %d instead of container init %d",
363 siginfo
.ssi_signo
, siginfo
.ssi_pid
, hdlr
->pid
);
364 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
367 if (siginfo
.ssi_signo
!= SIGCHLD
) {
368 kill(hdlr
->pid
, siginfo
.ssi_signo
);
369 INFO("Forwarded signal %d to pid %d", siginfo
.ssi_signo
, hdlr
->pid
);
370 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
373 if (siginfo
.ssi_code
== CLD_STOPPED
) {
374 INFO("Container init process was stopped");
375 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
376 } else if (siginfo
.ssi_code
== CLD_CONTINUED
) {
377 INFO("Container init process was continued");
378 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
381 DEBUG("Container init process %d exited", hdlr
->pid
);
382 return LXC_MAINLOOP_CLOSE
;
385 int lxc_serve_state_clients(const char *name
, struct lxc_handler
*handler
,
389 struct lxc_list
*cur
, *next
;
390 struct lxc_state_client
*client
;
391 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
394 handler
->state
= RUNNING
;
396 handler
->state
= state
;
398 TRACE("Set container state to %s", lxc_state2str(state
));
400 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
401 TRACE("No state clients registered");
405 strncpy(msg
.name
, name
, sizeof(msg
.name
));
406 msg
.name
[sizeof(msg
.name
) - 1] = 0;
408 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
411 if (client
->states
[state
] == 0) {
412 TRACE("State %s not registered for state client %d",
413 lxc_state2str(state
), client
->clientfd
);
417 TRACE("Sending state %s to state client %d",
418 lxc_state2str(state
), client
->clientfd
);
421 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), 0);
423 if (errno
== EINTR
) {
424 TRACE("Caught EINTR; retrying");
428 ERROR("%s - Failed to send message to client",
432 /* kick client from list */
434 close(client
->clientfd
);
442 static int lxc_serve_state_socket_pair(const char *name
,
443 struct lxc_handler
*handler
,
448 if (!handler
->backgrounded
||
449 handler
->state_socket_pair
[1] < 0 ||
453 /* Close read end of the socket pair. */
454 close(handler
->state_socket_pair
[0]);
455 handler
->state_socket_pair
[0] = -1;
458 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
459 &(int){state
}, sizeof(int));
460 if (ret
!= sizeof(int)) {
463 SYSERROR("Failed to send state to %d",
464 handler
->state_socket_pair
[1]);
468 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
469 handler
->state_socket_pair
[1]);
471 /* Close write end of the socket pair. */
472 close(handler
->state_socket_pair
[1]);
473 handler
->state_socket_pair
[1] = -1;
478 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
483 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
485 ERROR("Failed to synchronize via anonymous pair of unix sockets");
489 ret
= lxc_serve_state_clients(name
, handler
, state
);
493 /* This function will try to connect to the legacy lxc-monitord state
494 * server and only exists for backwards compatibility.
496 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
501 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
504 bool has_console
= true;
505 struct lxc_epoll_descr descr
, descr_console
;
507 if (handler
->conf
->console
.path
&&
508 strcmp(handler
->conf
->console
.path
, "none") == 0)
511 ret
= lxc_mainloop_open(&descr
);
513 ERROR("Failed to create mainloop");
518 ret
= lxc_mainloop_open(&descr_console
);
520 ERROR("Failed to create console mainloop");
525 ret
= lxc_mainloop_add_handler(&descr
, handler
->sigfd
, signal_handler
, handler
);
527 ERROR("Failed to add signal handler for %d to mainloop", handler
->sigfd
);
528 goto out_mainloop_console
;
532 struct lxc_console
*console
= &handler
->conf
->console
;
534 ret
= lxc_console_mainloop_add(&descr
, console
);
536 ERROR("Failed to add console handlers to mainloop");
537 goto out_mainloop_console
;
540 ret
= lxc_console_mainloop_add(&descr_console
, console
);
542 ERROR("Failed to add console handlers to console mainloop");
543 goto out_mainloop_console
;
546 handler
->conf
->console
.descr
= &descr
;
549 ret
= lxc_cmd_mainloop_add(name
, &descr
, handler
);
551 ERROR("Failed to add command handler to mainloop");
552 goto out_mainloop_console
;
555 TRACE("Mainloop is ready");
557 ret
= lxc_mainloop(&descr
, -1);
560 if (ret
< 0 || !handler
->init_died
)
561 goto out_mainloop_console
;
564 ret
= lxc_mainloop(&descr_console
, 0);
566 out_mainloop_console
:
568 lxc_mainloop_close(&descr_console
);
569 TRACE("Closed console mainloop");
573 lxc_mainloop_close(&descr
);
574 TRACE("Closed mainloop");
577 close(handler
->sigfd
);
578 TRACE("Closed signal file descriptor %d", handler
->sigfd
);
579 handler
->sigfd
= -EBADF
;
584 void lxc_zero_handler(struct lxc_handler
*handler
)
588 memset(handler
, 0, sizeof(struct lxc_handler
));
590 handler
->ns_clone_flags
= -1;
596 for (i
= 0; i
< LXC_NS_MAX
; i
++)
597 handler
->nsfd
[i
] = -1;
599 handler
->data_sock
[0] = -1;
600 handler
->data_sock
[1] = -1;
602 handler
->state_socket_pair
[0] = -1;
603 handler
->state_socket_pair
[1] = -1;
605 handler
->sync_sock
[0] = -1;
606 handler
->sync_sock
[1] = -1;
609 void lxc_free_handler(struct lxc_handler
*handler
)
611 if (handler
->pinfd
>= 0)
612 close(handler
->pinfd
);
614 if (handler
->sigfd
>= 0)
615 close(handler
->sigfd
);
617 lxc_put_nsfds(handler
);
619 if (handler
->conf
&& handler
->conf
->reboot
== 0)
620 if (handler
->conf
->maincmd_fd
>= 0)
621 close(handler
->conf
->maincmd_fd
);
623 if (handler
->state_socket_pair
[0] >= 0)
624 close(handler
->state_socket_pair
[0]);
626 if (handler
->state_socket_pair
[1] >= 0)
627 close(handler
->state_socket_pair
[1]);
629 handler
->conf
= NULL
;
634 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
635 const char *lxcpath
, bool daemonize
)
638 struct lxc_handler
*handler
;
640 handler
= malloc(sizeof(*handler
));
643 memset(handler
, 0, sizeof(*handler
));
645 /* Note that am_guest_unpriv() checks the effective uid. We
646 * probably don't care if we are real root only if we are running
647 * as root so this should be fine.
649 handler
->am_root
= !am_guest_unpriv();
650 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
651 handler
->conf
= conf
;
652 handler
->lxcpath
= lxcpath
;
654 handler
->sigfd
= -EBADF
;
655 handler
->init_died
= false;
656 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
657 if (handler
->conf
->reboot
== 0)
658 lxc_list_init(&handler
->conf
->state_clients
);
660 for (i
= 0; i
< LXC_NS_MAX
; i
++)
661 handler
->nsfd
[i
] = -1;
663 handler
->name
= name
;
665 if (daemonize
&& !handler
->conf
->reboot
) {
666 /* Create socketpair() to synchronize on daemonized startup.
667 * When the container reboots we don't need to synchronize
668 * again currently so don't open another socketpair().
670 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
671 handler
->state_socket_pair
);
673 ERROR("Failed to create anonymous pair of unix sockets");
676 TRACE("Created anonymous pair {%d,%d} of unix sockets",
677 handler
->state_socket_pair
[0],
678 handler
->state_socket_pair
[1]);
681 if (handler
->conf
->reboot
== 0) {
682 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
683 if (handler
->conf
->maincmd_fd
< 0) {
684 ERROR("Failed to set up command socket");
688 TRACE("Unix domain socket %d for command server is ready",
689 handler
->conf
->maincmd_fd
);
694 lxc_free_handler(handler
);
699 int lxc_init(const char *name
, struct lxc_handler
*handler
)
702 const char *loglevel
;
703 struct lxc_conf
*conf
= handler
->conf
;
706 TRACE("Initialized LSM");
708 ret
= lxc_read_seccomp_config(conf
);
710 ERROR("Failed loading seccomp policy");
711 goto out_close_maincmd_fd
;
713 TRACE("Read seccomp policy");
715 /* Begin by setting the state to STARTING. */
716 ret
= lxc_set_state(name
, handler
, STARTING
);
718 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING
));
719 goto out_close_maincmd_fd
;
721 TRACE("Set container state to \"STARTING\"");
723 /* Start of environment variable setup for hooks. */
725 ret
= setenv("LXC_NAME", name
, 1);
727 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name
);
731 ret
= setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1);
733 SYSERROR("Failed to set environment variable: "
734 "LXC_CONFIG_FILE=%s", conf
->rcfile
);
737 if (conf
->rootfs
.mount
) {
738 ret
= setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1);
740 SYSERROR("Failed to set environment variable: "
741 "LXC_ROOTFS_MOUNT=%s", conf
->rootfs
.mount
);
744 if (conf
->rootfs
.path
) {
745 ret
= setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1);
747 SYSERROR("Failed to set environment variable: "
748 "LXC_ROOTFS_PATH=%s", conf
->rootfs
.path
);
751 if (conf
->console
.path
) {
752 ret
= setenv("LXC_CONSOLE", conf
->console
.path
, 1);
754 SYSERROR("Failed to set environment variable: "
755 "LXC_CONSOLE=%s", conf
->console
.path
);
758 if (conf
->console
.log_path
) {
759 ret
= setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1);
761 SYSERROR("Failed to set environment variable: "
762 "LXC_CONSOLE_LOGPATH=%s", conf
->console
.log_path
);
765 if (cgns_supported()) {
766 ret
= setenv("LXC_CGNS_AWARE", "1", 1);
768 SYSERROR("Failed to set environment variable "
772 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
773 ret
= setenv("LXC_LOG_LEVEL", loglevel
, 1);
775 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
778 if (conf
->hooks_version
== 0)
779 ret
= setenv("LXC_HOOK_VERSION", "0", 1);
781 ret
= setenv("LXC_HOOK_VERSION", "1", 1);
783 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf
->hooks_version
);
784 /* End of environment variable setup for hooks. */
786 TRACE("Set environment variables");
788 ret
= run_lxc_hooks(name
, "pre-start", conf
, NULL
);
790 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name
);
793 TRACE("Ran pre-start hooks");
795 /* The signal fd has to be created before forking otherwise if the child
796 * process exits before we setup the signal fd, the event will be lost
797 * and the command will be stuck.
799 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
800 if (handler
->sigfd
< 0) {
801 ERROR("Failed to setup SIGCHLD fd handler.");
804 TRACE("Set up signal fd");
806 /* Do this after setting up signals since it might unblock SIGWINCH. */
807 ret
= lxc_console_create(conf
);
809 ERROR("Failed to create console");
810 goto out_restore_sigmask
;
812 TRACE("Created console");
814 ret
= lxc_pty_map_ids(conf
, &conf
->console
);
816 ERROR("Failed to chown console");
817 goto out_restore_sigmask
;
819 TRACE("Chowned console");
821 INFO("Container \"%s\" is initialized", name
);
825 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
827 lxc_delete_tty(&conf
->tty_info
);
829 lxc_set_state(name
, handler
, ABORTING
);
830 out_close_maincmd_fd
:
831 close(conf
->maincmd_fd
);
832 conf
->maincmd_fd
= -1;
836 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
840 struct lxc_list
*cur
, *next
;
841 char *namespaces
[LXC_NS_MAX
+ 1];
842 size_t namespace_count
= 0;
844 /* The STOPPING state is there for future cleanup code which can take
847 lxc_set_state(name
, handler
, STOPPING
);
849 self
= lxc_raw_getpid();
850 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
851 if (handler
->nsfd
[i
] < 0)
854 if (handler
->conf
->hooks_version
== 0)
855 ret
= asprintf(&namespaces
[namespace_count
],
856 "%s:/proc/%d/fd/%d", ns_info
[i
].proc_name
,
857 self
, handler
->nsfd
[i
]);
859 ret
= asprintf(&namespaces
[namespace_count
],
860 "/proc/%d/fd/%d", self
, handler
->nsfd
[i
]);
862 SYSERROR("Failed to allocate memory");
866 if (handler
->conf
->hooks_version
== 0) {
871 ret
= setenv(ns_info
[i
].env_name
, namespaces
[namespace_count
], 1);
873 SYSERROR("Failed to set environment variable %s=%s",
874 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
876 TRACE("Set environment variable %s=%s",
877 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
881 namespaces
[namespace_count
] = NULL
;
883 if (handler
->conf
->reboot
) {
884 ret
= setenv("LXC_TARGET", "reboot", 1);
886 SYSERROR("Failed to set environment variable: "
887 "LXC_TARGET=reboot");
890 if (!handler
->conf
->reboot
) {
891 ret
= setenv("LXC_TARGET", "stop", 1);
893 SYSERROR("Failed to set environment variable: "
897 if (handler
->conf
->hooks_version
== 0)
898 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, namespaces
);
900 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, NULL
);
902 ERROR("Failed to run \"lxc.hook.stop\" hook");
904 while (namespace_count
--)
905 free(namespaces
[namespace_count
]);
907 cgroup_destroy(handler
);
909 if (handler
->conf
->reboot
== 0) {
910 /* For all new state clients simply close the command socket.
911 * This will inform all state clients that the container is
912 * STOPPED and also prevents a race between a open()/close() on
913 * the command socket causing a new process to get ECONNREFUSED
914 * because we haven't yet closed the command socket.
916 close(handler
->conf
->maincmd_fd
);
917 handler
->conf
->maincmd_fd
= -1;
918 TRACE("Closed command socket");
920 /* This function will try to connect to the legacy lxc-monitord
921 * state server and only exists for backwards compatibility.
923 lxc_monitor_send_state(name
, STOPPED
, handler
->lxcpath
);
925 /* The command socket is closed so no one can acces the command
926 * socket anymore so there's no need to lock it.
928 handler
->state
= STOPPED
;
929 TRACE("Set container state to \"STOPPED\"");
931 lxc_set_state(name
, handler
, STOPPED
);
934 ret
= run_lxc_hooks(name
, "post-stop", handler
->conf
, NULL
);
936 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name
);
937 if (handler
->conf
->reboot
) {
938 WARN("Container will be stopped instead of rebooted");
939 handler
->conf
->reboot
= 0;
941 ret
= setenv("LXC_TARGET", "stop", 1);
943 WARN("Failed to set environment variable: "
948 /* Reset mask set by setup_signal_fd. */
949 ret
= sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
951 WARN("%s - Failed to restore signal mask", strerror(errno
));
953 lxc_console_delete(&handler
->conf
->console
);
954 lxc_delete_tty(&handler
->conf
->tty_info
);
956 /* The command socket is now closed, no more state clients can register
957 * themselves from now on. So free the list of state clients.
959 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
960 struct lxc_state_client
*client
= cur
->elem
;
962 /* Keep state clients that want to be notified about reboots. */
963 if ((handler
->conf
->reboot
> 0) && (client
->states
[RUNNING
] == 2))
966 /* close state client socket */
968 close(client
->clientfd
);
973 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= 1)
974 lxc_destroy_container_on_signal(handler
, name
);
976 lxc_free_handler(handler
);
979 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
983 lxc_set_state(name
, handler
, ABORTING
);
985 if (handler
->pid
> 0) {
986 ret
= kill(handler
->pid
, SIGKILL
);
988 SYSERROR("Failed to send SIGKILL to %d", handler
->pid
);
991 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
996 static int do_start(void *data
)
1000 bool have_cap_setgid
;
1003 struct lxc_list
*iterator
;
1004 int devnull_fd
= -1;
1005 struct lxc_handler
*handler
= data
;
1007 lxc_sync_fini_parent(handler
);
1009 /* This prctl must be before the synchro, so if the parent dies before
1010 * we set the parent death signal, we will detect its death with the
1011 * synchro right after, otherwise we have a window where the parent can
1012 * exit before we set the pdeath signal leading to a unsupervized
1015 ret
= lxc_set_death_signal(SIGKILL
);
1017 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1018 goto out_warn_father
;
1021 ret
= sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1023 SYSERROR("Failed to set signal mask");
1024 goto out_warn_father
;
1027 /* Don't leak the pinfd to the container. */
1028 if (handler
->pinfd
>= 0)
1029 close(handler
->pinfd
);
1031 ret
= lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
);
1033 goto out_warn_father
;
1035 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1036 * https://github.com/lxc/lxd/issues/1978.
1038 if ((handler
->ns_clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
1039 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
1040 ret
= unshare(CLONE_NEWNET
);
1042 SYSERROR("Failed to unshare CLONE_NEWNET");
1043 goto out_warn_father
;
1045 INFO("Unshared CLONE_NEWNET");
1048 /* Tell the parent task it can begin to configure the container and wait
1051 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
);
1055 ret
= lxc_network_recv_veth_names_from_parent(handler
);
1057 ERROR("Failed to receive veth names from parent");
1058 goto out_warn_father
;
1061 /* If we are in a new user namespace, become root there to have
1062 * privilege over our namespace.
1064 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
1065 uid_t nsuid
= (handler
->conf
->root_nsuid_map
!= NULL
)
1067 : handler
->conf
->init_uid
;
1068 gid_t nsgid
= (handler
->conf
->root_nsgid_map
!= NULL
)
1070 : handler
->conf
->init_gid
;
1072 ret
= lxc_switch_uid_gid(nsuid
, nsgid
);
1074 goto out_warn_father
;
1076 /* Drop groups only after we switched to a valid gid in the new
1079 ret
= lxc_setgroups(0, NULL
);
1080 if (ret
< 0 && (handler
->am_root
|| errno
!= EPERM
))
1081 goto out_warn_father
;
1083 if (!handler
->am_root
) {
1084 ret
= prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
1086 goto out_warn_father
;
1089 /* set{g,u}id() clears deathsignal */
1090 ret
= lxc_set_death_signal(SIGKILL
);
1092 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1093 goto out_warn_father
;
1097 ret
= access(handler
->lxcpath
, X_OK
);
1099 print_top_failing_dir(handler
->lxcpath
);
1100 goto out_warn_father
;
1103 ret
= snprintf(path
, sizeof(path
), "%s/dev/null",
1104 handler
->conf
->rootfs
.mount
);
1105 if (ret
< 0 || ret
>= sizeof(path
))
1106 goto out_warn_father
;
1108 /* In order to checkpoint restore, we need to have everything in the
1109 * same mount namespace. However, some containers may not have a
1110 * reasonable /dev (in particular, they may not have /dev/null), so we
1111 * can't set init's std fds to /dev/null by opening it from inside the
1114 * If that's the case, fall back to using the host's /dev/null. This
1115 * means that migration won't work, but at least we won't spew output
1116 * where it isn't wanted.
1118 if (handler
->backgrounded
&& !handler
->conf
->autodev
) {
1119 ret
= access(path
, F_OK
);
1121 devnull_fd
= open_devnull();
1124 goto out_warn_father
;
1125 WARN("Using /dev/null from the host for container "
1126 "init's standard file descriptors. Migration will "
1131 /* Ask father to setup cgroups and wait for him to finish. */
1132 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
);
1136 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1137 * earlier we end up with a wrong view of /proc/self/cgroup. For
1138 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1139 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1140 * /proc/self/cgroup would show us:
1144 * whereas it should actually show
1148 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1149 ret
= unshare(CLONE_NEWCGROUP
);
1151 INFO("Failed to unshare CLONE_NEWCGROUP");
1152 goto out_warn_father
;
1154 INFO("Unshared CLONE_NEWCGROUP");
1157 /* Add the requested environment variables to the current environment to
1158 * allow them to be used by the various hooks, such as the start hook
1161 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1162 ret
= putenv((char *)iterator
->elem
);
1164 SYSERROR("Failed to set environment variable: %s",
1165 (char *)iterator
->elem
);
1166 goto out_warn_father
;
1170 /* Setup the container, ip, names, utsname, ... */
1171 ret
= lxc_setup(handler
);
1172 close(handler
->data_sock
[1]);
1173 close(handler
->data_sock
[0]);
1175 ERROR("Failed to setup container \"%s\"", handler
->name
);
1176 goto out_warn_father
;
1179 /* Set the label to change to when we exec(2) the container's init. */
1180 ret
= lsm_process_label_set(NULL
, handler
->conf
, 1, 1);
1182 goto out_warn_father
;
1184 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1185 * before we aren't allowed anymore.
1187 if (handler
->conf
->no_new_privs
) {
1188 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1190 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1191 "execve() gainable privileges");
1192 goto out_warn_father
;
1194 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1198 /* Some init's such as busybox will set sane tty settings on stdin,
1199 * stdout, stderr which it thinks is the console. We already set them
1200 * the way we wanted on the real terminal, and we want init to do its
1201 * setup on its console ie. the pty allocated in lxc_console_create() so
1202 * make sure that that pty is stdin,stdout,stderr.
1204 if (handler
->conf
->console
.slave
>= 0) {
1205 if (handler
->backgrounded
|| handler
->conf
->is_execute
== 0)
1206 ret
= set_stdfds(handler
->conf
->console
.slave
);
1208 ret
= lxc_console_set_stdfds(handler
->conf
->console
.slave
);
1210 ERROR("Failed to redirect std{in,out,err} to pty file "
1211 "descriptor %d", handler
->conf
->console
.slave
);
1212 goto out_warn_father
;
1216 /* If we mounted a temporary proc, then unmount it now. */
1217 tmp_proc_unmount(handler
->conf
);
1219 ret
= lxc_seccomp_load(handler
->conf
);
1221 goto out_warn_father
;
1223 ret
= run_lxc_hooks(handler
->name
, "start", handler
->conf
, NULL
);
1225 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1227 goto out_warn_father
;
1230 close(handler
->sigfd
);
1232 if (devnull_fd
< 0) {
1233 devnull_fd
= open_devnull();
1235 goto out_warn_father
;
1238 if (handler
->conf
->console
.slave
< 0 && handler
->backgrounded
) {
1239 ret
= set_stdfds(devnull_fd
);
1241 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1242 goto out_warn_father
;
1246 if (devnull_fd
>= 0) {
1253 if (handler
->conf
->init_cwd
) {
1254 ret
= chdir(handler
->conf
->init_cwd
);
1256 SYSERROR("Could not change directory to \"%s\"",
1257 handler
->conf
->init_cwd
);
1258 goto out_warn_father
;
1262 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
);
1264 goto out_warn_father
;
1266 /* Reset the environment variables the user requested in a clear
1270 /* Don't error out though. */
1272 SYSERROR("Failed to clear environment.");
1274 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1275 ret
= putenv((char *)iterator
->elem
);
1277 SYSERROR("Failed to set environment variable: %s",
1278 (char *)iterator
->elem
);
1279 goto out_warn_father
;
1283 ret
= putenv("container=lxc");
1285 SYSERROR("Failed to set environment variable: container=lxc");
1286 goto out_warn_father
;
1289 if (handler
->conf
->pty_names
) {
1290 ret
= putenv(handler
->conf
->pty_names
);
1292 SYSERROR("Failed to set environment variable for container ptys");
1293 goto out_warn_father
;
1297 /* The container has been setup. We can now switch to an unprivileged
1300 new_uid
= handler
->conf
->init_uid
;
1301 new_gid
= handler
->conf
->init_gid
;
1303 /* If we are in a new user namespace we already dropped all groups when
1304 * we switched to root in the new user namespace further above. Only
1305 * drop groups if we can, so ensure that we have necessary privilege.
1308 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1310 have_cap_setgid
= false;
1312 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1313 ret
= lxc_setgroups(0, NULL
);
1315 goto out_warn_father
;
1318 ret
= lxc_switch_uid_gid(new_uid
, new_gid
);
1320 goto out_warn_father
;
1322 /* After this call, we are in error because this ops should not return
1325 handler
->ops
->start(handler
, handler
->data
);
1328 /* We want the parent to know something went wrong, so we return a
1329 * special error code.
1331 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1334 if (devnull_fd
>= 0)
1340 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1343 struct lxc_pty_info
*pty_info
;
1345 int sock
= handler
->data_sock
[1];
1346 struct lxc_conf
*conf
= handler
->conf
;
1347 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
1352 tty_info
->pty_info
= malloc(sizeof(*tty_info
->pty_info
) * conf
->tty
);
1353 if (!tty_info
->pty_info
)
1356 for (i
= 0; i
< conf
->tty
; i
++) {
1359 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1363 pty_info
= &tty_info
->pty_info
[i
];
1365 pty_info
->master
= ttyfds
[0];
1366 pty_info
->slave
= ttyfds
[1];
1367 TRACE("Received pty with master fd %d and slave fd %d from "
1368 "parent", pty_info
->master
, pty_info
->slave
);
1371 ERROR("Failed to receive %d ttys from child: %s", conf
->tty
,
1374 TRACE("Received %d ttys from child", conf
->tty
);
1376 tty_info
->nbtty
= conf
->tty
;
1381 int resolve_clone_flags(struct lxc_handler
*handler
)
1384 struct lxc_conf
*conf
= handler
->conf
;
1386 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1387 if (conf
->ns_keep
!= 0) {
1388 if ((conf
->ns_keep
& ns_info
[i
].clone_flag
) == 0)
1389 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1390 } else if (conf
->ns_clone
!= 0) {
1391 if ((conf
->ns_clone
& ns_info
[i
].clone_flag
) > 0)
1392 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1394 if (i
== LXC_NS_USER
&& lxc_list_empty(&handler
->conf
->id_map
))
1397 if (i
== LXC_NS_NET
&& lxc_requests_empty_network(handler
))
1400 if (i
== LXC_NS_CGROUP
&& !cgns_supported())
1403 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1406 if (!conf
->ns_share
[i
])
1409 handler
->ns_clone_flags
&= ~ns_info
[i
].clone_flag
;
1410 TRACE("Sharing %s namespace", ns_info
[i
].proc_name
);
1416 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1417 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1418 * But since the memory between parent and child is shared on CLONE_VM this
1419 * would invalidate the getpid() cache that glibc used to maintain and so
1420 * getpid() in the child would return the parent's pid. This is all fixed in
1421 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1422 * not reset anymore.
1423 * However, if for whatever reason you - dear commiter - somehow need to get the
1424 * pid of the dummy intermediate process for do_share_ns() you need to call
1425 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1428 static inline int do_share_ns(void *arg
)
1431 struct lxc_handler
*handler
= arg
;
1433 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1434 if (handler
->nsfd
[i
] < 0)
1437 ret
= setns(handler
->nsfd
[i
], 0);
1441 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1444 flags
= handler
->ns_on_clone_flags
;
1445 flags
|= CLONE_PARENT
;
1446 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, flags
);
1447 if (handler
->pid
< 0)
1453 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1454 * exec()s the requested container binary.
1455 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1456 * right here should be double checked if they'd pose a security risk. (For
1457 * example, any {u}mount() operations performed here will be reflected on the
1460 static int lxc_spawn(struct lxc_handler
*handler
)
1464 bool wants_to_map_ids
;
1465 struct lxc_list
*id_map
;
1466 const char *name
= handler
->name
;
1467 const char *lxcpath
= handler
->lxcpath
;
1468 bool cgroups_connected
= false, share_ns
= false;
1469 struct lxc_conf
*conf
= handler
->conf
;
1471 id_map
= &conf
->id_map
;
1472 wants_to_map_ids
= !lxc_list_empty(id_map
);
1474 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1475 if (!conf
->ns_share
[i
])
1478 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->ns_share
[i
], lxcpath
, ns_info
[i
].proc_name
);
1479 if (handler
->nsfd
[i
] < 0)
1485 ret
= lxc_sync_init(handler
);
1489 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1490 handler
->data_sock
);
1492 lxc_sync_fini(handler
);
1496 ret
= resolve_clone_flags(handler
);
1498 lxc_sync_fini(handler
);
1502 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1503 if (!lxc_list_empty(&conf
->network
)) {
1505 /* Find gateway addresses from the link device, which is
1506 * no longer accessible inside the container. Do this
1507 * before creating network interfaces, since goto
1508 * out_delete_net does not work before lxc_clone.
1510 ret
= lxc_find_gateway_addresses(handler
);
1512 ERROR("Failed to find gateway addresses");
1513 lxc_sync_fini(handler
);
1517 /* That should be done before the clone because we will
1518 * fill the netdev index and use them in the child.
1520 ret
= lxc_create_network_priv(handler
);
1522 ERROR("Failed to create the network");
1523 lxc_sync_fini(handler
);
1529 if (!cgroup_init(handler
)) {
1530 ERROR("Failed initializing cgroup support");
1531 goto out_delete_net
;
1534 cgroups_connected
= true;
1536 if (!cgroup_create(handler
)) {
1537 ERROR("Failed creating cgroups");
1538 goto out_delete_net
;
1541 /* If the rootfs is not a blockdev, prevent the container from marking
1543 * If the container is unprivileged then skip rootfs pinning.
1545 if (!wants_to_map_ids
) {
1546 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1547 if (handler
->pinfd
== -1)
1548 INFO("Failed to pin the rootfs for container \"%s\"", handler
->name
);
1551 /* Create a process in a new set of namespaces. */
1552 handler
->ns_on_clone_flags
= handler
->ns_clone_flags
;
1553 if (handler
->ns_clone_flags
& CLONE_NEWUSER
) {
1554 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1555 * clone a new user namespace first and only later unshare our
1556 * network namespace to ensure that network devices ownership is
1559 handler
->ns_on_clone_flags
&= ~CLONE_NEWNET
;
1561 /* The cgroup namespace gets unshare()ed not clone()ed. */
1562 handler
->ns_on_clone_flags
&= ~CLONE_NEWCGROUP
;
1567 attacher_pid
= lxc_clone(do_share_ns
, handler
,
1568 CLONE_VFORK
| CLONE_VM
| CLONE_FILES
);
1569 if (attacher_pid
< 0) {
1570 SYSERROR(LXC_CLONE_ERROR
);
1571 goto out_delete_net
;
1574 ret
= wait_for_pid(attacher_pid
);
1576 SYSERROR("Intermediate process failed");
1577 goto out_delete_net
;
1580 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
,
1581 handler
->ns_on_clone_flags
);
1583 if (handler
->pid
< 0) {
1584 SYSERROR(LXC_CLONE_ERROR
);
1585 goto out_delete_net
;
1587 TRACE("Cloned child process %d", handler
->pid
);
1589 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1590 if (handler
->ns_on_clone_flags
& ns_info
[i
].clone_flag
)
1591 INFO("Cloned %s", ns_info
[i
].flag_name
);
1593 if (!lxc_preserve_namespaces(handler
, handler
->ns_on_clone_flags
, handler
->pid
)) {
1594 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1595 goto out_delete_net
;
1598 lxc_sync_fini_child(handler
);
1600 /* Map the container uids. The container became an invalid userid the
1601 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1602 * anything immediately, but allows the container to setuid(0) (0 being
1603 * mapped to something else on the host.) later to become a valid uid
1606 if (wants_to_map_ids
) {
1607 if (!handler
->conf
->ns_share
[LXC_NS_USER
] &&
1608 (handler
->conf
->ns_keep
& CLONE_NEWUSER
) == 0) {
1609 ret
= lxc_map_ids(id_map
, handler
->pid
);
1611 ERROR("Failed to set up id mapping.");
1612 goto out_delete_net
;
1617 ret
= lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
);
1619 goto out_delete_net
;
1621 ret
= lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
);
1623 goto out_delete_net
;
1625 if (!cgroup_setup_limits(handler
, false)) {
1626 ERROR("Failed to setup cgroup limits for container \"%s\"", name
);
1627 goto out_delete_net
;
1630 if (!cgroup_enter(handler
))
1631 goto out_delete_net
;
1633 if (!cgroup_chown(handler
))
1634 goto out_delete_net
;
1636 /* Now we're ready to preserve the network namespace */
1637 ret
= lxc_preserve_ns(handler
->pid
, "net");
1639 ERROR("%s - Failed to preserve net namespace", strerror(errno
));
1640 goto out_delete_net
;
1642 handler
->nsfd
[LXC_NS_NET
] = ret
;
1643 DEBUG("Preserved net namespace via fd %d", ret
);
1645 /* Create the network configuration. */
1646 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1647 ret
= lxc_network_move_created_netdev_priv(handler
->lxcpath
,
1652 ERROR("Failed to create the configured network");
1653 goto out_delete_net
;
1656 ret
= lxc_create_network_unpriv(handler
->lxcpath
, handler
->name
,
1657 &conf
->network
, handler
->pid
);
1659 ERROR("Failed to create the configured network");
1660 goto out_delete_net
;
1664 ret
= lxc_network_send_veth_names_to_child(handler
);
1666 ERROR("Failed to send veth names to child");
1667 goto out_delete_net
;
1670 if (!lxc_list_empty(&conf
->procs
)) {
1671 ret
= setup_proc_filesystem(&conf
->procs
, handler
->pid
);
1673 goto out_delete_net
;
1676 /* Tell the child to continue its initialization. We'll get
1677 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1679 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
);
1681 goto out_delete_net
;
1683 if (!lxc_list_empty(&conf
->limits
)) {
1684 ret
= setup_resource_limits(&conf
->limits
, handler
->pid
);
1686 ERROR("Failed to setup resource limits");
1687 goto out_delete_net
;
1691 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
);
1693 goto out_delete_net
;
1695 if (!cgroup_setup_limits(handler
, true)) {
1696 ERROR("Failed to setup legacy device cgroup controller limits");
1697 goto out_delete_net
;
1699 TRACE("Set up legacy device cgroup controller limits");
1701 cgroup_disconnect();
1702 cgroups_connected
= false;
1704 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1705 /* Now we're ready to preserve the cgroup namespace */
1706 ret
= lxc_preserve_ns(handler
->pid
, "cgroup");
1708 ERROR("%s - Failed to preserve cgroup namespace", strerror(errno
));
1709 goto out_delete_net
;
1711 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1712 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1715 ret
= snprintf(pidstr
, 20, "%d", handler
->pid
);
1716 if (ret
< 0 || ret
>= 20)
1717 goto out_delete_net
;
1719 ret
= setenv("LXC_PID", pidstr
, 1);
1721 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr
);
1723 /* Run any host-side start hooks */
1724 ret
= run_lxc_hooks(name
, "start-host", conf
, NULL
);
1726 ERROR("Failed to run lxc.hook.start-host");
1727 goto out_delete_net
;
1730 /* Tell the child to complete its initialization and wait for it to exec
1731 * or return an error. (The child will never return
1732 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1733 * lxc_sync_barrier_child to return success, or return a different
1734 * value, causing us to error out).
1736 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
);
1738 goto out_delete_net
;
1740 ret
= lxc_network_recv_name_and_ifindex_from_child(handler
);
1742 ERROR("Failed to receive names and ifindices for network "
1743 "devices from child");
1744 goto out_delete_net
;
1747 /* Now all networks are created, network devices are moved into place,
1748 * and the correct names and ifindeces in the respective namespaces have
1749 * been recorded. The corresponding structs have now all been filled. So
1750 * log them for debugging purposes.
1752 lxc_log_configured_netdevs(conf
);
1754 /* Read tty fds allocated by child. */
1755 ret
= lxc_recv_ttys_from_child(handler
);
1757 ERROR("Failed to receive tty info from child process");
1758 goto out_delete_net
;
1761 ret
= handler
->ops
->post_start(handler
, handler
->data
);
1765 ret
= lxc_set_state(name
, handler
, RUNNING
);
1767 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING
));
1771 lxc_sync_fini(handler
);
1776 if (cgroups_connected
)
1777 cgroup_disconnect();
1779 if (handler
->ns_clone_flags
& CLONE_NEWNET
)
1780 lxc_delete_network(handler
);
1783 lxc_abort(name
, handler
);
1784 lxc_sync_fini(handler
);
1785 if (handler
->pinfd
>= 0) {
1786 close(handler
->pinfd
);
1787 handler
->pinfd
= -1;
1793 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1794 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1798 struct lxc_conf
*conf
= handler
->conf
;
1800 ret
= lxc_init(name
, handler
);
1802 ERROR("Failed to initialize container \"%s\"", name
);
1806 handler
->data
= data
;
1807 handler
->backgrounded
= backgrounded
;
1809 if (!attach_block_device(handler
->conf
)) {
1810 ERROR("Failed to attach block device");
1811 goto out_fini_nonet
;
1814 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1815 /* If the backing store is a device, mount it here and now. */
1816 if (rootfs_is_blockdev(conf
)) {
1817 ret
= unshare(CLONE_NEWNS
);
1819 ERROR("Failed to unshare CLONE_NEWNS");
1820 goto out_fini_nonet
;
1822 INFO("Unshared CLONE_NEWNS");
1824 remount_all_slave();
1825 ret
= do_rootfs_setup(conf
, name
, lxcpath
);
1827 ERROR("Error setting up rootfs mount as root before spawn");
1828 goto out_fini_nonet
;
1830 INFO("Set up container rootfs as host root");
1834 ret
= lxc_spawn(handler
);
1836 ERROR("Failed to spawn container \"%s\"", name
);
1837 goto out_detach_blockdev
;
1839 /* close parent side of data socket */
1840 close(handler
->data_sock
[0]);
1841 handler
->data_sock
[0] = -1;
1842 close(handler
->data_sock
[1]);
1843 handler
->data_sock
[1] = -1;
1845 handler
->conf
->reboot
= 0;
1847 ret
= lxc_poll(name
, handler
);
1849 ERROR("LXC mainloop exited with error: %d", ret
);
1853 status
= lxc_wait_for_pid_status(handler
->pid
);
1855 SYSERROR("Failed to retrieve status for %d", handler
->pid
);
1857 /* If the child process exited but was not signaled, it didn't call
1858 * reboot. This should mean it was an lxc-execute which simply exited.
1859 * In any case, treat it as a 'halt'.
1861 if (WIFSIGNALED(status
)) {
1862 switch(WTERMSIG(status
)) {
1863 case SIGINT
: /* halt */
1864 DEBUG("Container \"%s\" is halting", name
);
1866 case SIGHUP
: /* reboot */
1867 DEBUG("Container \"%s\" is rebooting", name
);
1868 handler
->conf
->reboot
= 1;
1870 case SIGSYS
: /* seccomp */
1871 DEBUG("Container \"%s\" violated its seccomp policy", name
);
1874 DEBUG("Unknown exit status for container \"%s\" init %d", name
, WTERMSIG(status
));
1879 ret
= lxc_restore_phys_nics_to_netns(handler
);
1881 ERROR("Failed to move physical network devices back to parent "
1882 "network namespace");
1884 if (handler
->pinfd
>= 0) {
1885 close(handler
->pinfd
);
1886 handler
->pinfd
= -1;
1889 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1890 lxc_error_set_and_log(handler
->pid
, status
);
1893 lxc_delete_network(handler
);
1895 out_detach_blockdev
:
1896 detach_block_device(handler
->conf
);
1899 lxc_fini(name
, handler
);
1903 lxc_abort(name
, handler
);
1911 static int start(struct lxc_handler
*handler
, void* data
)
1913 struct start_args
*arg
= data
;
1915 NOTICE("Exec'ing \"%s\"", arg
->argv
[0]);
1917 execvp(arg
->argv
[0], arg
->argv
);
1918 SYSERROR("Failed to exec \"%s\"", arg
->argv
[0]);
1922 static int post_start(struct lxc_handler
*handler
, void* data
)
1924 struct start_args
*arg
= data
;
1926 NOTICE("Started \"%s\" with pid \"%d\"", arg
->argv
[0], handler
->pid
);
1930 static struct lxc_operations start_ops
= {
1932 .post_start
= post_start
1935 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
1936 const char *lxcpath
, bool backgrounded
)
1938 struct start_args start_arg
= {
1942 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
);
1945 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
1948 char destroy
[MAXPATHLEN
];
1949 struct lxc_container
*c
;
1953 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
1954 bret
= do_destroy_container(handler
);
1956 ERROR("Error destroying rootfs for container \"%s\"", name
);
1960 INFO("Destroyed rootfs for container \"%s\"", name
);
1962 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
1963 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1964 ERROR("Error destroying directory for container \"%s\"", name
);
1968 c
= lxc_container_new(name
, handler
->lxcpath
);
1970 if (container_disk_lock(c
)) {
1971 INFO("Could not update lxc_snapshots file");
1972 lxc_container_put(c
);
1974 mod_all_rdeps(c
, false);
1975 container_disk_unlock(c
);
1976 lxc_container_put(c
);
1980 if (!handler
->am_root
)
1981 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
1982 destroy
, "lxc_rmdir_onedev_wrapper");
1984 ret
= lxc_rmdir_onedev(destroy
, NULL
);
1987 ERROR("Error destroying directory for container \"%s\"", name
);
1990 INFO("Destroyed directory for container \"%s\"", name
);
1993 static int lxc_rmdir_onedev_wrapper(void *data
)
1995 char *arg
= (char *) data
;
1996 return lxc_rmdir_onedev(arg
, NULL
);
1999 static bool do_destroy_container(struct lxc_handler
*handler
)
2003 if (!handler
->am_root
) {
2004 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
2005 handler
->conf
, "storage_destroy_wrapper");
2012 return storage_destroy(handler
->conf
);