]>
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
};
395 handler
->state
= RUNNING
;
397 handler
->state
= state
;
399 TRACE("Set container state to %s", lxc_state2str(state
));
401 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
402 TRACE("No state clients registered");
407 strncpy(msg
.name
, name
, sizeof(msg
.name
));
408 msg
.name
[sizeof(msg
.name
) - 1] = 0;
410 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
413 if (client
->states
[state
] == 0) {
414 TRACE("State %s not registered for state client %d",
415 lxc_state2str(state
), client
->clientfd
);
419 TRACE("Sending state %s to state client %d",
420 lxc_state2str(state
), client
->clientfd
);
423 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), 0);
425 if (errno
== EINTR
) {
426 TRACE("Caught EINTR; retrying");
430 ERROR("%s - Failed to send message to client",
434 /* kick client from list */
436 close(client
->clientfd
);
445 static int lxc_serve_state_socket_pair(const char *name
,
446 struct lxc_handler
*handler
,
451 if (!handler
->backgrounded
||
452 handler
->state_socket_pair
[1] < 0 ||
456 /* Close read end of the socket pair. */
457 close(handler
->state_socket_pair
[0]);
458 handler
->state_socket_pair
[0] = -1;
461 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
462 &(int){state
}, sizeof(int));
463 if (ret
!= sizeof(int)) {
466 SYSERROR("Failed to send state to %d",
467 handler
->state_socket_pair
[1]);
471 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
472 handler
->state_socket_pair
[1]);
474 /* Close write end of the socket pair. */
475 close(handler
->state_socket_pair
[1]);
476 handler
->state_socket_pair
[1] = -1;
481 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
486 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
488 ERROR("Failed to synchronize via anonymous pair of unix sockets");
492 ret
= lxc_serve_state_clients(name
, handler
, state
);
496 /* This function will try to connect to the legacy lxc-monitord state
497 * server and only exists for backwards compatibility.
499 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
504 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
507 bool has_console
= true;
508 struct lxc_epoll_descr descr
, descr_console
;
510 if (handler
->conf
->console
.path
&&
511 strcmp(handler
->conf
->console
.path
, "none") == 0)
514 ret
= lxc_mainloop_open(&descr
);
516 ERROR("Failed to create mainloop");
521 ret
= lxc_mainloop_open(&descr_console
);
523 ERROR("Failed to create console mainloop");
528 ret
= lxc_mainloop_add_handler(&descr
, handler
->sigfd
, signal_handler
, handler
);
530 ERROR("Failed to add signal handler for %d to mainloop", handler
->sigfd
);
531 goto out_mainloop_console
;
535 struct lxc_console
*console
= &handler
->conf
->console
;
537 ret
= lxc_console_mainloop_add(&descr
, console
);
539 ERROR("Failed to add console handlers to mainloop");
540 goto out_mainloop_console
;
543 ret
= lxc_console_mainloop_add(&descr_console
, console
);
545 ERROR("Failed to add console handlers to console mainloop");
546 goto out_mainloop_console
;
549 handler
->conf
->console
.descr
= &descr
;
552 ret
= lxc_cmd_mainloop_add(name
, &descr
, handler
);
554 ERROR("Failed to add command handler to mainloop");
555 goto out_mainloop_console
;
558 TRACE("Mainloop is ready");
560 ret
= lxc_mainloop(&descr
, -1);
563 if (ret
< 0 || !handler
->init_died
)
564 goto out_mainloop_console
;
567 ret
= lxc_mainloop(&descr_console
, 0);
569 out_mainloop_console
:
571 lxc_mainloop_close(&descr_console
);
572 TRACE("Closed console mainloop");
576 lxc_mainloop_close(&descr
);
577 TRACE("Closed mainloop");
580 close(handler
->sigfd
);
581 TRACE("Closed signal file descriptor %d", handler
->sigfd
);
582 handler
->sigfd
= -EBADF
;
587 void lxc_zero_handler(struct lxc_handler
*handler
)
591 memset(handler
, 0, sizeof(struct lxc_handler
));
593 handler
->ns_clone_flags
= -1;
599 for (i
= 0; i
< LXC_NS_MAX
; i
++)
600 handler
->nsfd
[i
] = -1;
602 handler
->data_sock
[0] = -1;
603 handler
->data_sock
[1] = -1;
605 handler
->state_socket_pair
[0] = -1;
606 handler
->state_socket_pair
[1] = -1;
608 handler
->sync_sock
[0] = -1;
609 handler
->sync_sock
[1] = -1;
612 void lxc_free_handler(struct lxc_handler
*handler
)
614 if (handler
->pinfd
>= 0)
615 close(handler
->pinfd
);
617 if (handler
->sigfd
>= 0)
618 close(handler
->sigfd
);
620 lxc_put_nsfds(handler
);
622 if (handler
->conf
&& handler
->conf
->reboot
== 0)
623 if (handler
->conf
->maincmd_fd
>= 0)
624 close(handler
->conf
->maincmd_fd
);
626 if (handler
->state_socket_pair
[0] >= 0)
627 close(handler
->state_socket_pair
[0]);
629 if (handler
->state_socket_pair
[1] >= 0)
630 close(handler
->state_socket_pair
[1]);
632 handler
->conf
= NULL
;
637 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
638 const char *lxcpath
, bool daemonize
)
641 struct lxc_handler
*handler
;
643 handler
= malloc(sizeof(*handler
));
646 memset(handler
, 0, sizeof(*handler
));
648 /* Note that am_guest_unpriv() checks the effective uid. We
649 * probably don't care if we are real root only if we are running
650 * as root so this should be fine.
652 handler
->am_root
= !am_guest_unpriv();
653 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
654 handler
->conf
= conf
;
655 handler
->lxcpath
= lxcpath
;
657 handler
->sigfd
= -EBADF
;
658 handler
->init_died
= false;
659 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
660 if (handler
->conf
->reboot
== 0)
661 lxc_list_init(&handler
->conf
->state_clients
);
663 for (i
= 0; i
< LXC_NS_MAX
; i
++)
664 handler
->nsfd
[i
] = -1;
666 handler
->name
= name
;
668 if (daemonize
&& !handler
->conf
->reboot
) {
669 /* Create socketpair() to synchronize on daemonized startup.
670 * When the container reboots we don't need to synchronize
671 * again currently so don't open another socketpair().
673 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
674 handler
->state_socket_pair
);
676 ERROR("Failed to create anonymous pair of unix sockets");
679 TRACE("Created anonymous pair {%d,%d} of unix sockets",
680 handler
->state_socket_pair
[0],
681 handler
->state_socket_pair
[1]);
684 if (handler
->conf
->reboot
== 0) {
685 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
686 if (handler
->conf
->maincmd_fd
< 0) {
687 ERROR("Failed to set up command socket");
691 TRACE("Unix domain socket %d for command server is ready",
692 handler
->conf
->maincmd_fd
);
697 lxc_free_handler(handler
);
702 int lxc_init(const char *name
, struct lxc_handler
*handler
)
705 const char *loglevel
;
706 struct lxc_conf
*conf
= handler
->conf
;
709 TRACE("Initialized LSM");
711 ret
= lxc_read_seccomp_config(conf
);
713 ERROR("Failed loading seccomp policy");
714 goto out_close_maincmd_fd
;
716 TRACE("Read seccomp policy");
718 /* Begin by setting the state to STARTING. */
719 ret
= lxc_set_state(name
, handler
, STARTING
);
721 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING
));
722 goto out_close_maincmd_fd
;
724 TRACE("Set container state to \"STARTING\"");
726 /* Start of environment variable setup for hooks. */
728 ret
= setenv("LXC_NAME", name
, 1);
730 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name
);
734 ret
= setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1);
736 SYSERROR("Failed to set environment variable: "
737 "LXC_CONFIG_FILE=%s", conf
->rcfile
);
740 if (conf
->rootfs
.mount
) {
741 ret
= setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1);
743 SYSERROR("Failed to set environment variable: "
744 "LXC_ROOTFS_MOUNT=%s", conf
->rootfs
.mount
);
747 if (conf
->rootfs
.path
) {
748 ret
= setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1);
750 SYSERROR("Failed to set environment variable: "
751 "LXC_ROOTFS_PATH=%s", conf
->rootfs
.path
);
754 if (conf
->console
.path
) {
755 ret
= setenv("LXC_CONSOLE", conf
->console
.path
, 1);
757 SYSERROR("Failed to set environment variable: "
758 "LXC_CONSOLE=%s", conf
->console
.path
);
761 if (conf
->console
.log_path
) {
762 ret
= setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1);
764 SYSERROR("Failed to set environment variable: "
765 "LXC_CONSOLE_LOGPATH=%s", conf
->console
.log_path
);
768 if (cgns_supported()) {
769 ret
= setenv("LXC_CGNS_AWARE", "1", 1);
771 SYSERROR("Failed to set environment variable "
775 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
776 ret
= setenv("LXC_LOG_LEVEL", loglevel
, 1);
778 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
781 if (conf
->hooks_version
== 0)
782 ret
= setenv("LXC_HOOK_VERSION", "0", 1);
784 ret
= setenv("LXC_HOOK_VERSION", "1", 1);
786 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf
->hooks_version
);
787 /* End of environment variable setup for hooks. */
789 TRACE("Set environment variables");
791 ret
= run_lxc_hooks(name
, "pre-start", conf
, NULL
);
793 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name
);
796 TRACE("Ran pre-start hooks");
798 /* The signal fd has to be created before forking otherwise if the child
799 * process exits before we setup the signal fd, the event will be lost
800 * and the command will be stuck.
802 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
803 if (handler
->sigfd
< 0) {
804 ERROR("Failed to setup SIGCHLD fd handler.");
807 TRACE("Set up signal fd");
809 /* Do this after setting up signals since it might unblock SIGWINCH. */
810 ret
= lxc_console_create(conf
);
812 ERROR("Failed to create console");
813 goto out_restore_sigmask
;
815 TRACE("Created console");
817 ret
= lxc_pty_map_ids(conf
, &conf
->console
);
819 ERROR("Failed to chown console");
820 goto out_restore_sigmask
;
822 TRACE("Chowned console");
824 INFO("Container \"%s\" is initialized", name
);
828 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
830 lxc_delete_tty(&conf
->tty_info
);
832 lxc_set_state(name
, handler
, ABORTING
);
833 out_close_maincmd_fd
:
834 close(conf
->maincmd_fd
);
835 conf
->maincmd_fd
= -1;
839 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
843 struct lxc_list
*cur
, *next
;
844 char *namespaces
[LXC_NS_MAX
+ 1];
845 size_t namespace_count
= 0;
847 /* The STOPPING state is there for future cleanup code which can take
850 lxc_set_state(name
, handler
, STOPPING
);
852 self
= lxc_raw_getpid();
853 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
854 if (handler
->nsfd
[i
] < 0)
857 if (handler
->conf
->hooks_version
== 0)
858 ret
= asprintf(&namespaces
[namespace_count
],
859 "%s:/proc/%d/fd/%d", ns_info
[i
].proc_name
,
860 self
, handler
->nsfd
[i
]);
862 ret
= asprintf(&namespaces
[namespace_count
],
863 "/proc/%d/fd/%d", self
, handler
->nsfd
[i
]);
865 SYSERROR("Failed to allocate memory");
869 if (handler
->conf
->hooks_version
== 0) {
874 ret
= setenv(ns_info
[i
].env_name
, namespaces
[namespace_count
], 1);
876 SYSERROR("Failed to set environment variable %s=%s",
877 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
879 TRACE("Set environment variable %s=%s",
880 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
884 namespaces
[namespace_count
] = NULL
;
886 if (handler
->conf
->reboot
) {
887 ret
= setenv("LXC_TARGET", "reboot", 1);
889 SYSERROR("Failed to set environment variable: "
890 "LXC_TARGET=reboot");
893 if (!handler
->conf
->reboot
) {
894 ret
= setenv("LXC_TARGET", "stop", 1);
896 SYSERROR("Failed to set environment variable: "
900 if (handler
->conf
->hooks_version
== 0)
901 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, namespaces
);
903 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, NULL
);
905 ERROR("Failed to run \"lxc.hook.stop\" hook");
907 while (namespace_count
--)
908 free(namespaces
[namespace_count
]);
910 cgroup_destroy(handler
);
912 if (handler
->conf
->reboot
== 0) {
913 /* For all new state clients simply close the command socket.
914 * This will inform all state clients that the container is
915 * STOPPED and also prevents a race between a open()/close() on
916 * the command socket causing a new process to get ECONNREFUSED
917 * because we haven't yet closed the command socket.
919 close(handler
->conf
->maincmd_fd
);
920 handler
->conf
->maincmd_fd
= -1;
921 TRACE("Closed command socket");
923 /* This function will try to connect to the legacy lxc-monitord
924 * state server and only exists for backwards compatibility.
926 lxc_monitor_send_state(name
, STOPPED
, handler
->lxcpath
);
928 /* The command socket is closed so no one can acces the command
929 * socket anymore so there's no need to lock it.
931 handler
->state
= STOPPED
;
932 TRACE("Set container state to \"STOPPED\"");
934 lxc_set_state(name
, handler
, STOPPED
);
937 ret
= run_lxc_hooks(name
, "post-stop", handler
->conf
, NULL
);
939 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name
);
940 if (handler
->conf
->reboot
) {
941 WARN("Container will be stopped instead of rebooted");
942 handler
->conf
->reboot
= 0;
944 ret
= setenv("LXC_TARGET", "stop", 1);
946 WARN("Failed to set environment variable: "
951 /* Reset mask set by setup_signal_fd. */
952 ret
= sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
954 WARN("%s - Failed to restore signal mask", strerror(errno
));
956 lxc_console_delete(&handler
->conf
->console
);
957 lxc_delete_tty(&handler
->conf
->tty_info
);
959 /* The command socket is now closed, no more state clients can register
960 * themselves from now on. So free the list of state clients.
962 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
963 struct lxc_state_client
*client
= cur
->elem
;
965 /* Keep state clients that want to be notified about reboots. */
966 if ((handler
->conf
->reboot
> 0) && (client
->states
[RUNNING
] == 2))
969 /* close state client socket */
971 close(client
->clientfd
);
976 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= 1)
977 lxc_destroy_container_on_signal(handler
, name
);
979 lxc_free_handler(handler
);
982 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
986 lxc_set_state(name
, handler
, ABORTING
);
988 if (handler
->pid
> 0) {
989 ret
= kill(handler
->pid
, SIGKILL
);
991 SYSERROR("Failed to send SIGKILL to %d", handler
->pid
);
994 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
999 static int do_start(void *data
)
1002 char path
[PATH_MAX
];
1003 bool have_cap_setgid
;
1006 struct lxc_list
*iterator
;
1007 int devnull_fd
= -1;
1008 struct lxc_handler
*handler
= data
;
1010 lxc_sync_fini_parent(handler
);
1012 /* This prctl must be before the synchro, so if the parent dies before
1013 * we set the parent death signal, we will detect its death with the
1014 * synchro right after, otherwise we have a window where the parent can
1015 * exit before we set the pdeath signal leading to a unsupervized
1018 ret
= lxc_set_death_signal(SIGKILL
);
1020 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1021 goto out_warn_father
;
1024 ret
= sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1026 SYSERROR("Failed to set signal mask");
1027 goto out_warn_father
;
1030 /* Don't leak the pinfd to the container. */
1031 if (handler
->pinfd
>= 0)
1032 close(handler
->pinfd
);
1034 ret
= lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
);
1036 goto out_warn_father
;
1038 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1039 * https://github.com/lxc/lxd/issues/1978.
1041 if ((handler
->ns_clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
1042 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
1043 ret
= unshare(CLONE_NEWNET
);
1045 SYSERROR("Failed to unshare CLONE_NEWNET");
1046 goto out_warn_father
;
1048 INFO("Unshared CLONE_NEWNET");
1051 /* Tell the parent task it can begin to configure the container and wait
1054 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
);
1058 ret
= lxc_network_recv_veth_names_from_parent(handler
);
1060 ERROR("Failed to receive veth names from parent");
1061 goto out_warn_father
;
1064 /* If we are in a new user namespace, become root there to have
1065 * privilege over our namespace.
1067 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
1068 uid_t nsuid
= (handler
->conf
->root_nsuid_map
!= NULL
)
1070 : handler
->conf
->init_uid
;
1071 gid_t nsgid
= (handler
->conf
->root_nsgid_map
!= NULL
)
1073 : handler
->conf
->init_gid
;
1075 ret
= lxc_switch_uid_gid(nsuid
, nsgid
);
1077 goto out_warn_father
;
1079 /* Drop groups only after we switched to a valid gid in the new
1082 ret
= lxc_setgroups(0, NULL
);
1083 if (ret
< 0 && (handler
->am_root
|| errno
!= EPERM
))
1084 goto out_warn_father
;
1086 if (!handler
->am_root
) {
1087 ret
= prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
1089 goto out_warn_father
;
1092 /* set{g,u}id() clears deathsignal */
1093 ret
= lxc_set_death_signal(SIGKILL
);
1095 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1096 goto out_warn_father
;
1100 ret
= access(handler
->lxcpath
, X_OK
);
1102 print_top_failing_dir(handler
->lxcpath
);
1103 goto out_warn_father
;
1106 ret
= snprintf(path
, sizeof(path
), "%s/dev/null",
1107 handler
->conf
->rootfs
.mount
);
1108 if (ret
< 0 || ret
>= sizeof(path
))
1109 goto out_warn_father
;
1111 /* In order to checkpoint restore, we need to have everything in the
1112 * same mount namespace. However, some containers may not have a
1113 * reasonable /dev (in particular, they may not have /dev/null), so we
1114 * can't set init's std fds to /dev/null by opening it from inside the
1117 * If that's the case, fall back to using the host's /dev/null. This
1118 * means that migration won't work, but at least we won't spew output
1119 * where it isn't wanted.
1121 if (handler
->backgrounded
&& !handler
->conf
->autodev
) {
1122 ret
= access(path
, F_OK
);
1124 devnull_fd
= open_devnull();
1127 goto out_warn_father
;
1128 WARN("Using /dev/null from the host for container "
1129 "init's standard file descriptors. Migration will "
1134 /* Ask father to setup cgroups and wait for him to finish. */
1135 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
);
1139 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1140 * earlier we end up with a wrong view of /proc/self/cgroup. For
1141 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1142 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1143 * /proc/self/cgroup would show us:
1147 * whereas it should actually show
1151 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1152 ret
= unshare(CLONE_NEWCGROUP
);
1154 INFO("Failed to unshare CLONE_NEWCGROUP");
1155 goto out_warn_father
;
1157 INFO("Unshared CLONE_NEWCGROUP");
1160 /* Add the requested environment variables to the current environment to
1161 * allow them to be used by the various hooks, such as the start hook
1164 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1165 ret
= putenv((char *)iterator
->elem
);
1167 SYSERROR("Failed to set environment variable: %s",
1168 (char *)iterator
->elem
);
1169 goto out_warn_father
;
1173 /* Setup the container, ip, names, utsname, ... */
1174 ret
= lxc_setup(handler
);
1175 close(handler
->data_sock
[1]);
1176 close(handler
->data_sock
[0]);
1178 ERROR("Failed to setup container \"%s\"", handler
->name
);
1179 goto out_warn_father
;
1182 /* Set the label to change to when we exec(2) the container's init. */
1183 ret
= lsm_process_label_set(NULL
, handler
->conf
, 1, 1);
1185 goto out_warn_father
;
1187 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1188 * before we aren't allowed anymore.
1190 if (handler
->conf
->no_new_privs
) {
1191 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1193 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1194 "execve() gainable privileges");
1195 goto out_warn_father
;
1197 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1201 /* Some init's such as busybox will set sane tty settings on stdin,
1202 * stdout, stderr which it thinks is the console. We already set them
1203 * the way we wanted on the real terminal, and we want init to do its
1204 * setup on its console ie. the pty allocated in lxc_console_create() so
1205 * make sure that that pty is stdin,stdout,stderr.
1207 if (handler
->conf
->console
.slave
>= 0) {
1208 if (handler
->backgrounded
|| handler
->conf
->is_execute
== 0)
1209 ret
= set_stdfds(handler
->conf
->console
.slave
);
1211 ret
= lxc_console_set_stdfds(handler
->conf
->console
.slave
);
1213 ERROR("Failed to redirect std{in,out,err} to pty file "
1214 "descriptor %d", handler
->conf
->console
.slave
);
1215 goto out_warn_father
;
1219 /* If we mounted a temporary proc, then unmount it now. */
1220 tmp_proc_unmount(handler
->conf
);
1222 ret
= lxc_seccomp_load(handler
->conf
);
1224 goto out_warn_father
;
1226 ret
= run_lxc_hooks(handler
->name
, "start", handler
->conf
, NULL
);
1228 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1230 goto out_warn_father
;
1233 close(handler
->sigfd
);
1235 if (devnull_fd
< 0) {
1236 devnull_fd
= open_devnull();
1238 goto out_warn_father
;
1241 if (handler
->conf
->console
.slave
< 0 && handler
->backgrounded
) {
1242 ret
= set_stdfds(devnull_fd
);
1244 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1245 goto out_warn_father
;
1249 if (devnull_fd
>= 0) {
1256 if (handler
->conf
->init_cwd
) {
1257 ret
= chdir(handler
->conf
->init_cwd
);
1259 SYSERROR("Could not change directory to \"%s\"",
1260 handler
->conf
->init_cwd
);
1261 goto out_warn_father
;
1265 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
);
1267 goto out_warn_father
;
1269 /* Reset the environment variables the user requested in a clear
1273 /* Don't error out though. */
1275 SYSERROR("Failed to clear environment.");
1277 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1278 ret
= putenv((char *)iterator
->elem
);
1280 SYSERROR("Failed to set environment variable: %s",
1281 (char *)iterator
->elem
);
1282 goto out_warn_father
;
1286 ret
= putenv("container=lxc");
1288 SYSERROR("Failed to set environment variable: container=lxc");
1289 goto out_warn_father
;
1292 if (handler
->conf
->pty_names
) {
1293 ret
= putenv(handler
->conf
->pty_names
);
1295 SYSERROR("Failed to set environment variable for container ptys");
1296 goto out_warn_father
;
1300 /* The container has been setup. We can now switch to an unprivileged
1303 new_uid
= handler
->conf
->init_uid
;
1304 new_gid
= handler
->conf
->init_gid
;
1306 /* If we are in a new user namespace we already dropped all groups when
1307 * we switched to root in the new user namespace further above. Only
1308 * drop groups if we can, so ensure that we have necessary privilege.
1311 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1313 have_cap_setgid
= false;
1315 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1316 ret
= lxc_setgroups(0, NULL
);
1318 goto out_warn_father
;
1321 ret
= lxc_switch_uid_gid(new_uid
, new_gid
);
1323 goto out_warn_father
;
1325 /* After this call, we are in error because this ops should not return
1328 handler
->ops
->start(handler
, handler
->data
);
1331 /* We want the parent to know something went wrong, so we return a
1332 * special error code.
1334 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1337 if (devnull_fd
>= 0)
1343 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1346 struct lxc_pty_info
*pty_info
;
1348 int sock
= handler
->data_sock
[1];
1349 struct lxc_conf
*conf
= handler
->conf
;
1350 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
1355 tty_info
->pty_info
= malloc(sizeof(*tty_info
->pty_info
) * conf
->tty
);
1356 if (!tty_info
->pty_info
)
1359 for (i
= 0; i
< conf
->tty
; i
++) {
1362 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1366 pty_info
= &tty_info
->pty_info
[i
];
1368 pty_info
->master
= ttyfds
[0];
1369 pty_info
->slave
= ttyfds
[1];
1370 TRACE("Received pty with master fd %d and slave fd %d from "
1371 "parent", pty_info
->master
, pty_info
->slave
);
1374 ERROR("Failed to receive %d ttys from child: %s", conf
->tty
,
1377 TRACE("Received %d ttys from child", conf
->tty
);
1379 tty_info
->nbtty
= conf
->tty
;
1384 int resolve_clone_flags(struct lxc_handler
*handler
)
1387 struct lxc_conf
*conf
= handler
->conf
;
1389 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1390 if (conf
->ns_keep
!= 0) {
1391 if ((conf
->ns_keep
& ns_info
[i
].clone_flag
) == 0)
1392 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1393 } else if (conf
->ns_clone
!= 0) {
1394 if ((conf
->ns_clone
& ns_info
[i
].clone_flag
) > 0)
1395 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1397 if (i
== LXC_NS_USER
&& lxc_list_empty(&handler
->conf
->id_map
))
1400 if (i
== LXC_NS_NET
&& lxc_requests_empty_network(handler
))
1403 if (i
== LXC_NS_CGROUP
&& !cgns_supported())
1406 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1409 if (!conf
->ns_share
[i
])
1412 handler
->ns_clone_flags
&= ~ns_info
[i
].clone_flag
;
1413 TRACE("Sharing %s namespace", ns_info
[i
].proc_name
);
1419 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1420 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1421 * But since the memory between parent and child is shared on CLONE_VM this
1422 * would invalidate the getpid() cache that glibc used to maintain and so
1423 * getpid() in the child would return the parent's pid. This is all fixed in
1424 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1425 * not reset anymore.
1426 * However, if for whatever reason you - dear commiter - somehow need to get the
1427 * pid of the dummy intermediate process for do_share_ns() you need to call
1428 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1431 static inline int do_share_ns(void *arg
)
1434 struct lxc_handler
*handler
= arg
;
1436 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1437 if (handler
->nsfd
[i
] < 0)
1440 ret
= setns(handler
->nsfd
[i
], 0);
1444 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1447 flags
= handler
->ns_on_clone_flags
;
1448 flags
|= CLONE_PARENT
;
1449 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, flags
);
1450 if (handler
->pid
< 0)
1456 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1457 * exec()s the requested container binary.
1458 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1459 * right here should be double checked if they'd pose a security risk. (For
1460 * example, any {u}mount() operations performed here will be reflected on the
1463 static int lxc_spawn(struct lxc_handler
*handler
)
1467 bool wants_to_map_ids
;
1468 struct lxc_list
*id_map
;
1469 const char *name
= handler
->name
;
1470 const char *lxcpath
= handler
->lxcpath
;
1471 bool cgroups_connected
= false, share_ns
= false;
1472 struct lxc_conf
*conf
= handler
->conf
;
1474 id_map
= &conf
->id_map
;
1475 wants_to_map_ids
= !lxc_list_empty(id_map
);
1477 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1478 if (!conf
->ns_share
[i
])
1481 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->ns_share
[i
], lxcpath
, ns_info
[i
].proc_name
);
1482 if (handler
->nsfd
[i
] < 0)
1488 ret
= lxc_sync_init(handler
);
1492 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1493 handler
->data_sock
);
1495 lxc_sync_fini(handler
);
1499 ret
= resolve_clone_flags(handler
);
1501 lxc_sync_fini(handler
);
1505 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1506 if (!lxc_list_empty(&conf
->network
)) {
1508 /* Find gateway addresses from the link device, which is
1509 * no longer accessible inside the container. Do this
1510 * before creating network interfaces, since goto
1511 * out_delete_net does not work before lxc_clone.
1513 ret
= lxc_find_gateway_addresses(handler
);
1515 ERROR("Failed to find gateway addresses");
1516 lxc_sync_fini(handler
);
1520 /* That should be done before the clone because we will
1521 * fill the netdev index and use them in the child.
1523 ret
= lxc_create_network_priv(handler
);
1525 ERROR("Failed to create the network");
1526 lxc_sync_fini(handler
);
1532 if (!cgroup_init(handler
)) {
1533 ERROR("Failed initializing cgroup support");
1534 goto out_delete_net
;
1537 cgroups_connected
= true;
1539 if (!cgroup_create(handler
)) {
1540 ERROR("Failed creating cgroups");
1541 goto out_delete_net
;
1544 /* If the rootfs is not a blockdev, prevent the container from marking
1546 * If the container is unprivileged then skip rootfs pinning.
1548 if (!wants_to_map_ids
) {
1549 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1550 if (handler
->pinfd
== -1)
1551 INFO("Failed to pin the rootfs for container \"%s\"", handler
->name
);
1554 /* Create a process in a new set of namespaces. */
1555 handler
->ns_on_clone_flags
= handler
->ns_clone_flags
;
1556 if (handler
->ns_clone_flags
& CLONE_NEWUSER
) {
1557 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1558 * clone a new user namespace first and only later unshare our
1559 * network namespace to ensure that network devices ownership is
1562 handler
->ns_on_clone_flags
&= ~CLONE_NEWNET
;
1564 /* The cgroup namespace gets unshare()ed not clone()ed. */
1565 handler
->ns_on_clone_flags
&= ~CLONE_NEWCGROUP
;
1570 attacher_pid
= lxc_clone(do_share_ns
, handler
,
1571 CLONE_VFORK
| CLONE_VM
| CLONE_FILES
);
1572 if (attacher_pid
< 0) {
1573 SYSERROR(LXC_CLONE_ERROR
);
1574 goto out_delete_net
;
1577 ret
= wait_for_pid(attacher_pid
);
1579 SYSERROR("Intermediate process failed");
1580 goto out_delete_net
;
1583 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
,
1584 handler
->ns_on_clone_flags
);
1586 if (handler
->pid
< 0) {
1587 SYSERROR(LXC_CLONE_ERROR
);
1588 goto out_delete_net
;
1590 TRACE("Cloned child process %d", handler
->pid
);
1592 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1593 if (handler
->ns_on_clone_flags
& ns_info
[i
].clone_flag
)
1594 INFO("Cloned %s", ns_info
[i
].flag_name
);
1596 if (!lxc_preserve_namespaces(handler
, handler
->ns_on_clone_flags
, handler
->pid
)) {
1597 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1598 goto out_delete_net
;
1601 lxc_sync_fini_child(handler
);
1603 /* Map the container uids. The container became an invalid userid the
1604 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1605 * anything immediately, but allows the container to setuid(0) (0 being
1606 * mapped to something else on the host.) later to become a valid uid
1609 if (wants_to_map_ids
) {
1610 if (!handler
->conf
->ns_share
[LXC_NS_USER
] &&
1611 (handler
->conf
->ns_keep
& CLONE_NEWUSER
) == 0) {
1612 ret
= lxc_map_ids(id_map
, handler
->pid
);
1614 ERROR("Failed to set up id mapping.");
1615 goto out_delete_net
;
1620 ret
= lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
);
1622 goto out_delete_net
;
1624 ret
= lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
);
1626 goto out_delete_net
;
1628 if (!cgroup_setup_limits(handler
, false)) {
1629 ERROR("Failed to setup cgroup limits for container \"%s\"", name
);
1630 goto out_delete_net
;
1633 if (!cgroup_enter(handler
))
1634 goto out_delete_net
;
1636 if (!cgroup_chown(handler
))
1637 goto out_delete_net
;
1639 /* Now we're ready to preserve the network namespace */
1640 ret
= lxc_preserve_ns(handler
->pid
, "net");
1642 ERROR("%s - Failed to preserve net namespace", strerror(errno
));
1643 goto out_delete_net
;
1645 handler
->nsfd
[LXC_NS_NET
] = ret
;
1646 DEBUG("Preserved net namespace via fd %d", ret
);
1648 /* Create the network configuration. */
1649 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1650 ret
= lxc_network_move_created_netdev_priv(handler
->lxcpath
,
1655 ERROR("Failed to create the configured network");
1656 goto out_delete_net
;
1659 ret
= lxc_create_network_unpriv(handler
->lxcpath
, handler
->name
,
1660 &conf
->network
, handler
->pid
);
1662 ERROR("Failed to create the configured network");
1663 goto out_delete_net
;
1667 ret
= lxc_network_send_veth_names_to_child(handler
);
1669 ERROR("Failed to send veth names to child");
1670 goto out_delete_net
;
1673 if (!lxc_list_empty(&conf
->procs
)) {
1674 ret
= setup_proc_filesystem(&conf
->procs
, handler
->pid
);
1676 goto out_delete_net
;
1679 /* Tell the child to continue its initialization. We'll get
1680 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1682 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
);
1684 goto out_delete_net
;
1686 if (!lxc_list_empty(&conf
->limits
)) {
1687 ret
= setup_resource_limits(&conf
->limits
, handler
->pid
);
1689 ERROR("Failed to setup resource limits");
1690 goto out_delete_net
;
1694 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
);
1696 goto out_delete_net
;
1698 if (!cgroup_setup_limits(handler
, true)) {
1699 ERROR("Failed to setup legacy device cgroup controller limits");
1700 goto out_delete_net
;
1702 TRACE("Set up legacy device cgroup controller limits");
1704 cgroup_disconnect();
1705 cgroups_connected
= false;
1707 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1708 /* Now we're ready to preserve the cgroup namespace */
1709 ret
= lxc_preserve_ns(handler
->pid
, "cgroup");
1711 ERROR("%s - Failed to preserve cgroup namespace", strerror(errno
));
1712 goto out_delete_net
;
1714 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1715 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1718 ret
= snprintf(pidstr
, 20, "%d", handler
->pid
);
1719 if (ret
< 0 || ret
>= 20)
1720 goto out_delete_net
;
1722 ret
= setenv("LXC_PID", pidstr
, 1);
1724 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr
);
1726 /* Run any host-side start hooks */
1727 ret
= run_lxc_hooks(name
, "start-host", conf
, NULL
);
1729 ERROR("Failed to run lxc.hook.start-host");
1730 goto out_delete_net
;
1733 /* Tell the child to complete its initialization and wait for it to exec
1734 * or return an error. (The child will never return
1735 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1736 * lxc_sync_barrier_child to return success, or return a different
1737 * value, causing us to error out).
1739 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
);
1741 goto out_delete_net
;
1743 ret
= lxc_network_recv_name_and_ifindex_from_child(handler
);
1745 ERROR("Failed to receive names and ifindices for network "
1746 "devices from child");
1747 goto out_delete_net
;
1750 /* Now all networks are created, network devices are moved into place,
1751 * and the correct names and ifindeces in the respective namespaces have
1752 * been recorded. The corresponding structs have now all been filled. So
1753 * log them for debugging purposes.
1755 lxc_log_configured_netdevs(conf
);
1757 /* Read tty fds allocated by child. */
1758 ret
= lxc_recv_ttys_from_child(handler
);
1760 ERROR("Failed to receive tty info from child process");
1761 goto out_delete_net
;
1764 ret
= handler
->ops
->post_start(handler
, handler
->data
);
1768 ret
= lxc_set_state(name
, handler
, RUNNING
);
1770 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING
));
1774 lxc_sync_fini(handler
);
1779 if (cgroups_connected
)
1780 cgroup_disconnect();
1782 if (handler
->ns_clone_flags
& CLONE_NEWNET
)
1783 lxc_delete_network(handler
);
1786 lxc_abort(name
, handler
);
1787 lxc_sync_fini(handler
);
1788 if (handler
->pinfd
>= 0) {
1789 close(handler
->pinfd
);
1790 handler
->pinfd
= -1;
1796 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1797 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1801 struct lxc_conf
*conf
= handler
->conf
;
1803 ret
= lxc_init(name
, handler
);
1805 ERROR("Failed to initialize container \"%s\"", name
);
1809 handler
->data
= data
;
1810 handler
->backgrounded
= backgrounded
;
1812 if (!attach_block_device(handler
->conf
)) {
1813 ERROR("Failed to attach block device");
1814 goto out_fini_nonet
;
1817 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1818 /* If the backing store is a device, mount it here and now. */
1819 if (rootfs_is_blockdev(conf
)) {
1820 ret
= unshare(CLONE_NEWNS
);
1822 ERROR("Failed to unshare CLONE_NEWNS");
1823 goto out_fini_nonet
;
1825 INFO("Unshared CLONE_NEWNS");
1827 remount_all_slave();
1828 ret
= do_rootfs_setup(conf
, name
, lxcpath
);
1830 ERROR("Error setting up rootfs mount as root before spawn");
1831 goto out_fini_nonet
;
1833 INFO("Set up container rootfs as host root");
1837 ret
= lxc_spawn(handler
);
1839 ERROR("Failed to spawn container \"%s\"", name
);
1840 goto out_detach_blockdev
;
1842 /* close parent side of data socket */
1843 close(handler
->data_sock
[0]);
1844 handler
->data_sock
[0] = -1;
1845 close(handler
->data_sock
[1]);
1846 handler
->data_sock
[1] = -1;
1848 handler
->conf
->reboot
= 0;
1850 ret
= lxc_poll(name
, handler
);
1852 ERROR("LXC mainloop exited with error: %d", ret
);
1856 status
= lxc_wait_for_pid_status(handler
->pid
);
1858 SYSERROR("Failed to retrieve status for %d", handler
->pid
);
1860 /* If the child process exited but was not signaled, it didn't call
1861 * reboot. This should mean it was an lxc-execute which simply exited.
1862 * In any case, treat it as a 'halt'.
1864 if (WIFSIGNALED(status
)) {
1865 switch(WTERMSIG(status
)) {
1866 case SIGINT
: /* halt */
1867 DEBUG("Container \"%s\" is halting", name
);
1869 case SIGHUP
: /* reboot */
1870 DEBUG("Container \"%s\" is rebooting", name
);
1871 handler
->conf
->reboot
= 1;
1873 case SIGSYS
: /* seccomp */
1874 DEBUG("Container \"%s\" violated its seccomp policy", name
);
1877 DEBUG("Unknown exit status for container \"%s\" init %d", name
, WTERMSIG(status
));
1882 ret
= lxc_restore_phys_nics_to_netns(handler
);
1884 ERROR("Failed to move physical network devices back to parent "
1885 "network namespace");
1887 if (handler
->pinfd
>= 0) {
1888 close(handler
->pinfd
);
1889 handler
->pinfd
= -1;
1892 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1893 lxc_error_set_and_log(handler
->pid
, status
);
1896 lxc_delete_network(handler
);
1898 out_detach_blockdev
:
1899 detach_block_device(handler
->conf
);
1902 lxc_fini(name
, handler
);
1906 lxc_abort(name
, handler
);
1914 static int start(struct lxc_handler
*handler
, void* data
)
1916 struct start_args
*arg
= data
;
1918 NOTICE("Exec'ing \"%s\"", arg
->argv
[0]);
1920 execvp(arg
->argv
[0], arg
->argv
);
1921 SYSERROR("Failed to exec \"%s\"", arg
->argv
[0]);
1925 static int post_start(struct lxc_handler
*handler
, void* data
)
1927 struct start_args
*arg
= data
;
1929 NOTICE("Started \"%s\" with pid \"%d\"", arg
->argv
[0], handler
->pid
);
1933 static struct lxc_operations start_ops
= {
1935 .post_start
= post_start
1938 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
1939 const char *lxcpath
, bool backgrounded
)
1941 struct start_args start_arg
= {
1945 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
);
1948 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
1951 char destroy
[MAXPATHLEN
];
1952 struct lxc_container
*c
;
1956 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
1957 bret
= do_destroy_container(handler
);
1959 ERROR("Error destroying rootfs for container \"%s\"", name
);
1963 INFO("Destroyed rootfs for container \"%s\"", name
);
1965 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
1966 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1967 ERROR("Error destroying directory for container \"%s\"", name
);
1971 c
= lxc_container_new(name
, handler
->lxcpath
);
1973 if (container_disk_lock(c
)) {
1974 INFO("Could not update lxc_snapshots file");
1975 lxc_container_put(c
);
1977 mod_all_rdeps(c
, false);
1978 container_disk_unlock(c
);
1979 lxc_container_put(c
);
1983 if (!handler
->am_root
)
1984 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
1985 destroy
, "lxc_rmdir_onedev_wrapper");
1987 ret
= lxc_rmdir_onedev(destroy
, NULL
);
1990 ERROR("Error destroying directory for container \"%s\"", name
);
1993 INFO("Destroyed directory for container \"%s\"", name
);
1996 static int lxc_rmdir_onedev_wrapper(void *data
)
1998 char *arg
= (char *) data
;
1999 return lxc_rmdir_onedev(arg
, NULL
);
2002 static bool do_destroy_container(struct lxc_handler
*handler
)
2006 if (!handler
->am_root
) {
2007 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
2008 handler
->conf
, "storage_destroy_wrapper");
2015 return storage_destroy(handler
->conf
);