]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
aa9f5c0eca8f93b11bcd6bffd849bbdbfe2fc7fb
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
42 #include <sys/mount.h>
43 #include <sys/param.h>
44 #include <sys/prctl.h>
45 #include <sys/socket.h>
47 #include <sys/syscall.h>
48 #include <sys/types.h>
53 #include <sys/capability.h>
56 #if !HAVE_DECL_PR_CAPBSET_DROP
57 #define PR_CAPBSET_DROP 24
60 #if !HAVE_DECL_PR_SET_NO_NEW_PRIVS
61 #define PR_SET_NO_NEW_PRIVS 38
64 #if !HAVE_DECL_PR_GET_NO_NEW_PRIVS
65 #define PR_GET_NO_NEW_PRIVS 39
72 #include "commands_utils.h"
74 #include "confile_utils.h"
79 #include "lxccontainer.h"
81 #include "lxcseccomp.h"
84 #include "namespace.h"
87 #include "storage/storage.h"
88 #include "storage/storage_utils.h"
94 #include "include/strlcpy.h"
97 lxc_log_define(start
, lxc
);
99 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
100 static bool do_destroy_container(struct lxc_handler
*handler
);
101 static int lxc_rmdir_onedev_wrapper(void *data
);
102 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
105 static void print_top_failing_dir(const char *path
)
109 char *copy
, *e
, *p
, saved
;
112 copy
= alloca(len
+ 1);
113 (void)strlcpy(copy
, path
, len
+ 1);
119 while (p
< e
&& *p
== '/')
122 while (p
< e
&& *p
!= '/')
128 ret
= access(copy
, X_OK
);
130 SYSERROR("Could not access %s. Please grant it x "
131 "access, or add an ACL for the container "
139 static void lxc_put_nsfds(struct lxc_handler
*handler
)
143 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
144 if (handler
->nsfd
[i
] < 0)
147 close(handler
->nsfd
[i
]);
148 handler
->nsfd
[i
] = -EBADF
;
152 static int lxc_try_preserve_ns(const int pid
, const char *ns
)
156 fd
= lxc_preserve_ns(pid
, ns
);
158 if (errno
!= ENOENT
) {
159 SYSERROR("Failed to preserve %s namespace", ns
);
163 SYSWARN("Kernel does not support preserving %s namespaces", ns
);
170 /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
171 * specified in ns_clone_flags.
172 * Return true on success, false on failure.
174 static bool lxc_try_preserve_namespaces(struct lxc_handler
*handler
,
175 int ns_clone_flags
, pid_t pid
)
179 for (i
= 0; i
< LXC_NS_MAX
; i
++)
180 handler
->nsfd
[i
] = -EBADF
;
182 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
185 if ((ns_clone_flags
& ns_info
[i
].clone_flag
) == 0)
188 fd
= lxc_try_preserve_ns(pid
, ns_info
[i
].proc_name
);
190 handler
->nsfd
[i
] = -EBADF
;
192 /* Do not fail to start container on kernels that do
193 * not support interacting with namespaces through
196 if (fd
== -EOPNOTSUPP
)
199 lxc_put_nsfds(handler
);
203 handler
->nsfd
[i
] = fd
;
204 DEBUG("Preserved %s namespace via fd %d", ns_info
[i
].proc_name
,
211 static inline bool match_stdfds(int fd
)
213 return (fd
== STDIN_FILENO
|| fd
== STDOUT_FILENO
|| fd
== STDERR_FILENO
);
216 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
217 int *fds_to_ignore
, size_t len_fds
)
222 struct dirent
*direntp
;
224 if (conf
&& conf
->close_all_fds
)
228 dir
= opendir("/proc/self/fd");
230 SYSWARN("Failed to open directory");
236 while ((direntp
= readdir(dir
))) {
238 struct lxc_list
*cur
;
239 bool matched
= false;
241 if (strcmp(direntp
->d_name
, ".") == 0)
244 if (strcmp(direntp
->d_name
, "..") == 0)
247 ret
= lxc_safe_int(direntp
->d_name
, &fd
);
249 INFO("Could not parse file descriptor for \"%s\"", direntp
->d_name
);
253 for (i
= 0; i
< len_fds
; i
++)
254 if (fds_to_ignore
[i
] == fd
)
257 if (fd
== fddir
|| fd
== lxc_log_fd
||
258 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
261 /* Keep state clients that wait on reboots. */
263 lxc_list_for_each(cur
, &conf
->state_clients
) {
264 struct lxc_state_client
*client
= cur
->elem
;
266 if (client
->clientfd
!= fd
)
277 if (current_config
&& fd
== current_config
->logfd
)
280 if (match_stdfds(fd
))
286 INFO("Closed inherited fd %d", fd
);
289 WARN("Inherited fd %d", fd
);
292 /* Only enable syslog at this point to avoid the above logging function
293 * to open a new fd and make the check_inherited function enter an
296 lxc_log_enable_syslog();
298 closedir(dir
); /* cannot fail */
302 static int setup_signal_fd(sigset_t
*oldmask
)
307 const int signals
[] = {SIGBUS
, SIGILL
, SIGSEGV
, SIGWINCH
};
309 /* Block everything except serious error signals. */
310 ret
= sigfillset(&mask
);
314 for (sig
= 0; sig
< (sizeof(signals
) / sizeof(signals
[0])); sig
++) {
315 ret
= sigdelset(&mask
, signals
[sig
]);
320 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, oldmask
);
322 SYSERROR("Failed to set signal mask");
326 ret
= signalfd(-1, &mask
, SFD_CLOEXEC
);
328 SYSERROR("Failed to create signal file descriptor");
332 TRACE("Created signal file descriptor %d", ret
);
337 static int signal_handler(int fd
, uint32_t events
, void *data
,
338 struct lxc_epoll_descr
*descr
)
342 struct signalfd_siginfo siginfo
;
343 struct lxc_handler
*hdlr
= data
;
345 ret
= lxc_read_nointr(fd
, &siginfo
, sizeof(siginfo
));
347 ERROR("Failed to read signal info from signal file descriptor %d", fd
);
348 return LXC_MAINLOOP_ERROR
;
351 if (ret
!= sizeof(siginfo
)) {
352 ERROR("Unexpected size for struct signalfd_siginfo");
356 /* Check whether init is running. */
358 ret
= waitid(P_PID
, hdlr
->pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
359 if (ret
== 0 && info
.si_pid
== hdlr
->pid
)
360 hdlr
->init_died
= true;
362 /* Try to figure out a reasonable exit status to report. */
363 if (hdlr
->init_died
) {
364 switch (info
.si_code
) {
366 hdlr
->exit_status
= info
.si_status
<< 8;
371 hdlr
->exit_status
= info
.si_status
<< 8 | 0x7f;
374 /* Huh? The waitid() told us it's dead *and* continued? */
375 WARN("Init %d dead and continued?", hdlr
->pid
);
376 hdlr
->exit_status
= 1;
379 ERROR("Unknown si_code: %d", info
.si_code
);
380 hdlr
->exit_status
= 1;
384 if (siginfo
.ssi_signo
== SIGHUP
) {
385 kill(hdlr
->pid
, SIGTERM
);
386 INFO("Killing %d since terminal hung up", hdlr
->pid
);
387 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
390 if (siginfo
.ssi_signo
!= SIGCHLD
) {
391 kill(hdlr
->pid
, siginfo
.ssi_signo
);
392 INFO("Forwarded signal %d to pid %d", siginfo
.ssi_signo
, hdlr
->pid
);
393 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
396 /* More robustness, protect ourself from a SIGCHLD sent
397 * by a process different from the container init.
399 if (siginfo
.ssi_pid
!= hdlr
->pid
) {
400 NOTICE("Received %d from pid %d instead of container init %d",
401 siginfo
.ssi_signo
, siginfo
.ssi_pid
, hdlr
->pid
);
402 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
405 if (siginfo
.ssi_code
== CLD_STOPPED
) {
406 INFO("Container init process was stopped");
407 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
408 } else if (siginfo
.ssi_code
== CLD_CONTINUED
) {
409 INFO("Container init process was continued");
410 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
413 DEBUG("Container init process %d exited", hdlr
->pid
);
414 return LXC_MAINLOOP_CLOSE
;
417 int lxc_serve_state_clients(const char *name
, struct lxc_handler
*handler
,
422 struct lxc_list
*cur
, *next
;
423 struct lxc_state_client
*client
;
424 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
427 handler
->state
= RUNNING
;
429 handler
->state
= state
;
431 TRACE("Set container state to %s", lxc_state2str(state
));
433 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
434 TRACE("No state clients registered");
438 retlen
= strlcpy(msg
.name
, name
, sizeof(msg
.name
));
439 if (retlen
>= sizeof(msg
.name
))
442 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
445 if (client
->states
[state
] == 0) {
446 TRACE("State %s not registered for state client %d",
447 lxc_state2str(state
), client
->clientfd
);
451 TRACE("Sending state %s to state client %d",
452 lxc_state2str(state
), client
->clientfd
);
455 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), MSG_NOSIGNAL
);
457 if (errno
== EINTR
) {
458 TRACE("Caught EINTR; retrying");
462 SYSERROR("Failed to send message to client");
465 /* kick client from list */
467 close(client
->clientfd
);
475 static int lxc_serve_state_socket_pair(const char *name
,
476 struct lxc_handler
*handler
,
481 if (!handler
->backgrounded
||
482 handler
->state_socket_pair
[1] < 0 ||
486 /* Close read end of the socket pair. */
487 close(handler
->state_socket_pair
[0]);
488 handler
->state_socket_pair
[0] = -1;
491 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
492 &(int){state
}, sizeof(int));
494 SYSERROR("Failed to send state to %d", handler
->state_socket_pair
[1]);
502 if (ret
!= sizeof(int)) {
503 ERROR("Message too long : %d", handler
->state_socket_pair
[1]);
507 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
508 handler
->state_socket_pair
[1]);
510 /* Close write end of the socket pair. */
511 close(handler
->state_socket_pair
[1]);
512 handler
->state_socket_pair
[1] = -1;
517 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
522 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
524 ERROR("Failed to synchronize via anonymous pair of unix sockets");
528 ret
= lxc_serve_state_clients(name
, handler
, state
);
532 /* This function will try to connect to the legacy lxc-monitord state
533 * server and only exists for backwards compatibility.
535 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
540 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
543 bool has_console
= true;
544 struct lxc_epoll_descr descr
, descr_console
;
546 if (handler
->conf
->console
.path
&&
547 strcmp(handler
->conf
->console
.path
, "none") == 0)
550 ret
= lxc_mainloop_open(&descr
);
552 ERROR("Failed to create mainloop");
557 ret
= lxc_mainloop_open(&descr_console
);
559 ERROR("Failed to create console mainloop");
564 ret
= lxc_mainloop_add_handler(&descr
, handler
->sigfd
, signal_handler
, handler
);
566 ERROR("Failed to add signal handler for %d to mainloop", handler
->sigfd
);
567 goto out_mainloop_console
;
571 struct lxc_terminal
*console
= &handler
->conf
->console
;
573 ret
= lxc_terminal_mainloop_add(&descr
, console
);
575 ERROR("Failed to add console handlers to mainloop");
576 goto out_mainloop_console
;
579 ret
= lxc_terminal_mainloop_add(&descr_console
, console
);
581 ERROR("Failed to add console handlers to console mainloop");
582 goto out_mainloop_console
;
585 handler
->conf
->console
.descr
= &descr
;
588 ret
= lxc_cmd_mainloop_add(name
, &descr
, handler
);
590 ERROR("Failed to add command handler to mainloop");
591 goto out_mainloop_console
;
594 TRACE("Mainloop is ready");
596 ret
= lxc_mainloop(&descr
, -1);
599 if (ret
< 0 || !handler
->init_died
)
600 goto out_mainloop_console
;
603 ret
= lxc_mainloop(&descr_console
, 0);
605 out_mainloop_console
:
607 lxc_mainloop_close(&descr_console
);
608 TRACE("Closed console mainloop");
612 lxc_mainloop_close(&descr
);
613 TRACE("Closed mainloop");
616 close(handler
->sigfd
);
617 TRACE("Closed signal file descriptor %d", handler
->sigfd
);
618 handler
->sigfd
= -EBADF
;
623 void lxc_zero_handler(struct lxc_handler
*handler
)
627 memset(handler
, 0, sizeof(struct lxc_handler
));
633 for (i
= 0; i
< LXC_NS_MAX
; i
++)
634 handler
->nsfd
[i
] = -1;
636 handler
->data_sock
[0] = -1;
637 handler
->data_sock
[1] = -1;
639 handler
->state_socket_pair
[0] = -1;
640 handler
->state_socket_pair
[1] = -1;
642 handler
->sync_sock
[0] = -1;
643 handler
->sync_sock
[1] = -1;
646 void lxc_free_handler(struct lxc_handler
*handler
)
648 if (handler
->pinfd
>= 0)
649 close(handler
->pinfd
);
651 if (handler
->sigfd
>= 0)
652 close(handler
->sigfd
);
654 lxc_put_nsfds(handler
);
656 if (handler
->conf
&& handler
->conf
->reboot
== REBOOT_NONE
)
657 if (handler
->conf
->maincmd_fd
>= 0)
658 lxc_abstract_unix_close(handler
->conf
->maincmd_fd
);
660 if (handler
->state_socket_pair
[0] >= 0)
661 close(handler
->state_socket_pair
[0]);
663 if (handler
->state_socket_pair
[1] >= 0)
664 close(handler
->state_socket_pair
[1]);
666 handler
->conf
= NULL
;
671 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
672 const char *lxcpath
, bool daemonize
)
675 struct lxc_handler
*handler
;
677 handler
= malloc(sizeof(*handler
));
681 memset(handler
, 0, sizeof(*handler
));
683 /* Note that am_guest_unpriv() checks the effective uid. We
684 * probably don't care if we are real root only if we are running
685 * as root so this should be fine.
687 handler
->am_root
= !am_guest_unpriv();
688 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
689 handler
->conf
= conf
;
690 handler
->lxcpath
= lxcpath
;
692 handler
->sigfd
= -EBADF
;
693 handler
->init_died
= false;
694 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
695 if (handler
->conf
->reboot
== REBOOT_NONE
)
696 lxc_list_init(&handler
->conf
->state_clients
);
698 for (i
= 0; i
< LXC_NS_MAX
; i
++)
699 handler
->nsfd
[i
] = -1;
701 handler
->name
= name
;
703 if (daemonize
&& handler
->conf
->reboot
== REBOOT_NONE
) {
704 /* Create socketpair() to synchronize on daemonized startup.
705 * When the container reboots we don't need to synchronize
706 * again currently so don't open another socketpair().
708 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
709 handler
->state_socket_pair
);
711 ERROR("Failed to create anonymous pair of unix sockets");
715 TRACE("Created anonymous pair {%d,%d} of unix sockets",
716 handler
->state_socket_pair
[0],
717 handler
->state_socket_pair
[1]);
720 if (handler
->conf
->reboot
== REBOOT_NONE
) {
721 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
722 if (handler
->conf
->maincmd_fd
< 0) {
723 ERROR("Failed to set up command socket");
728 TRACE("Unix domain socket %d for command server is ready",
729 handler
->conf
->maincmd_fd
);
734 lxc_free_handler(handler
);
739 int lxc_init(const char *name
, struct lxc_handler
*handler
)
742 const char *loglevel
;
743 struct lxc_conf
*conf
= handler
->conf
;
746 TRACE("Initialized LSM");
748 ret
= lxc_read_seccomp_config(conf
);
750 ERROR("Failed loading seccomp policy");
751 goto out_close_maincmd_fd
;
753 TRACE("Read seccomp policy");
755 /* Begin by setting the state to STARTING. */
756 ret
= lxc_set_state(name
, handler
, STARTING
);
758 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING
));
759 goto out_close_maincmd_fd
;
761 TRACE("Set container state to \"STARTING\"");
763 /* Start of environment variable setup for hooks. */
764 ret
= setenv("LXC_NAME", name
, 1);
766 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name
);
769 ret
= setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1);
771 SYSERROR("Failed to set environment variable: "
772 "LXC_CONFIG_FILE=%s", conf
->rcfile
);
775 if (conf
->rootfs
.mount
) {
776 ret
= setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1);
778 SYSERROR("Failed to set environment variable: "
779 "LXC_ROOTFS_MOUNT=%s", conf
->rootfs
.mount
);
782 if (conf
->rootfs
.path
) {
783 ret
= setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1);
785 SYSERROR("Failed to set environment variable: "
786 "LXC_ROOTFS_PATH=%s", conf
->rootfs
.path
);
789 if (conf
->console
.path
) {
790 ret
= setenv("LXC_CONSOLE", conf
->console
.path
, 1);
792 SYSERROR("Failed to set environment variable: "
793 "LXC_CONSOLE=%s", conf
->console
.path
);
796 if (conf
->console
.log_path
) {
797 ret
= setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1);
799 SYSERROR("Failed to set environment variable: "
800 "LXC_CONSOLE_LOGPATH=%s", conf
->console
.log_path
);
803 if (cgns_supported()) {
804 ret
= setenv("LXC_CGNS_AWARE", "1", 1);
806 SYSERROR("Failed to set environment variable "
810 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
811 ret
= setenv("LXC_LOG_LEVEL", loglevel
, 1);
813 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
816 if (conf
->hooks_version
== 0)
817 ret
= setenv("LXC_HOOK_VERSION", "0", 1);
819 ret
= setenv("LXC_HOOK_VERSION", "1", 1);
821 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf
->hooks_version
);
822 /* End of environment variable setup for hooks. */
824 TRACE("Set environment variables");
826 ret
= run_lxc_hooks(name
, "pre-start", conf
, NULL
);
828 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name
);
831 TRACE("Ran pre-start hooks");
833 /* The signal fd has to be created before forking otherwise if the child
834 * process exits before we setup the signal fd, the event will be lost
835 * and the command will be stuck.
837 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
838 if (handler
->sigfd
< 0) {
839 ERROR("Failed to setup SIGCHLD fd handler.");
842 TRACE("Set up signal fd");
844 /* Do this after setting up signals since it might unblock SIGWINCH. */
845 ret
= lxc_terminal_setup(conf
);
847 ERROR("Failed to create console");
848 goto out_restore_sigmask
;
850 TRACE("Created console");
852 ret
= lxc_terminal_map_ids(conf
, &conf
->console
);
854 ERROR("Failed to chown console");
855 goto out_delete_terminal
;
857 TRACE("Chowned console");
859 handler
->cgroup_ops
= cgroup_init(handler
);
860 if (!handler
->cgroup_ops
) {
861 ERROR("Failed to initialize cgroup driver");
862 goto out_delete_terminal
;
864 TRACE("Initialized cgroup driver");
866 INFO("Container \"%s\" is initialized", name
);
870 lxc_terminal_delete(&handler
->conf
->console
);
873 (void)pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
876 lxc_delete_tty(&conf
->ttys
);
879 (void)lxc_set_state(name
, handler
, ABORTING
);
881 out_close_maincmd_fd
:
882 lxc_abstract_unix_close(conf
->maincmd_fd
);
883 conf
->maincmd_fd
= -1;
887 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
891 struct lxc_list
*cur
, *next
;
892 char *namespaces
[LXC_NS_MAX
+ 1];
893 size_t namespace_count
= 0;
894 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
896 /* The STOPPING state is there for future cleanup code which can take
899 lxc_set_state(name
, handler
, STOPPING
);
901 self
= lxc_raw_getpid();
902 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
903 if (handler
->nsfd
[i
] < 0)
906 if (handler
->conf
->hooks_version
== 0)
907 ret
= asprintf(&namespaces
[namespace_count
],
908 "%s:/proc/%d/fd/%d", ns_info
[i
].proc_name
,
909 self
, handler
->nsfd
[i
]);
911 ret
= asprintf(&namespaces
[namespace_count
],
912 "/proc/%d/fd/%d", self
, handler
->nsfd
[i
]);
914 SYSERROR("Failed to allocate memory");
918 if (handler
->conf
->hooks_version
== 0) {
923 ret
= setenv(ns_info
[i
].env_name
, namespaces
[namespace_count
], 1);
925 SYSERROR("Failed to set environment variable %s=%s",
926 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
928 TRACE("Set environment variable %s=%s",
929 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
933 namespaces
[namespace_count
] = NULL
;
935 if (handler
->conf
->reboot
> REBOOT_NONE
) {
936 ret
= setenv("LXC_TARGET", "reboot", 1);
938 SYSERROR("Failed to set environment variable: "
939 "LXC_TARGET=reboot");
942 if (handler
->conf
->reboot
== REBOOT_NONE
) {
943 ret
= setenv("LXC_TARGET", "stop", 1);
945 SYSERROR("Failed to set environment variable: "
949 if (handler
->conf
->hooks_version
== 0)
950 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, namespaces
);
952 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, NULL
);
954 ERROR("Failed to run \"lxc.hook.stop\" hook");
956 while (namespace_count
--)
957 free(namespaces
[namespace_count
]);
959 cgroup_ops
->destroy(cgroup_ops
, handler
);
960 cgroup_exit(cgroup_ops
);
962 if (handler
->conf
->reboot
== REBOOT_NONE
) {
963 /* For all new state clients simply close the command socket.
964 * This will inform all state clients that the container is
965 * STOPPED and also prevents a race between a open()/close() on
966 * the command socket causing a new process to get ECONNREFUSED
967 * because we haven't yet closed the command socket.
969 lxc_abstract_unix_close(handler
->conf
->maincmd_fd
);
970 handler
->conf
->maincmd_fd
= -1;
971 TRACE("Closed command socket");
973 /* This function will try to connect to the legacy lxc-monitord
974 * state server and only exists for backwards compatibility.
976 lxc_monitor_send_state(name
, STOPPED
, handler
->lxcpath
);
978 /* The command socket is closed so no one can acces the command
979 * socket anymore so there's no need to lock it.
981 handler
->state
= STOPPED
;
982 TRACE("Set container state to \"STOPPED\"");
984 lxc_set_state(name
, handler
, STOPPED
);
987 ret
= run_lxc_hooks(name
, "post-stop", handler
->conf
, NULL
);
989 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name
);
990 if (handler
->conf
->reboot
> REBOOT_NONE
) {
991 WARN("Container will be stopped instead of rebooted");
992 handler
->conf
->reboot
= REBOOT_NONE
;
994 ret
= setenv("LXC_TARGET", "stop", 1);
996 WARN("Failed to set environment variable: "
1001 /* Reset mask set by setup_signal_fd. */
1002 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1004 SYSWARN("Failed to restore signal mask");
1006 lxc_terminal_delete(&handler
->conf
->console
);
1007 lxc_delete_tty(&handler
->conf
->ttys
);
1009 /* The command socket is now closed, no more state clients can register
1010 * themselves from now on. So free the list of state clients.
1012 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
1013 struct lxc_state_client
*client
= cur
->elem
;
1015 /* Keep state clients that want to be notified about reboots. */
1016 if ((handler
->conf
->reboot
> REBOOT_NONE
) &&
1017 (client
->states
[RUNNING
] == 2))
1020 /* close state client socket */
1022 close(client
->clientfd
);
1027 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= REBOOT_REQ
)
1028 lxc_destroy_container_on_signal(handler
, name
);
1030 lxc_free_handler(handler
);
1033 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
1037 lxc_set_state(name
, handler
, ABORTING
);
1039 if (handler
->pid
> 0) {
1040 ret
= kill(handler
->pid
, SIGKILL
);
1042 SYSERROR("Failed to send SIGKILL to %d", handler
->pid
);
1045 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
1050 static int do_start(void *data
)
1053 char path
[PATH_MAX
];
1054 bool have_cap_setgid
;
1057 struct lxc_list
*iterator
;
1058 int devnull_fd
= -1;
1059 struct lxc_handler
*handler
= data
;
1061 lxc_sync_fini_parent(handler
);
1063 /* This prctl must be before the synchro, so if the parent dies before
1064 * we set the parent death signal, we will detect its death with the
1065 * synchro right after, otherwise we have a window where the parent can
1066 * exit before we set the pdeath signal leading to a unsupervized
1069 ret
= lxc_set_death_signal(SIGKILL
);
1071 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1072 goto out_warn_father
;
1075 ret
= lxc_ambient_caps_up();
1077 SYSERROR("Failed to raise ambient capabilities");
1078 goto out_warn_father
;
1081 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1083 SYSERROR("Failed to set signal mask");
1084 goto out_warn_father
;
1087 /* Don't leak the pinfd to the container. */
1088 if (handler
->pinfd
>= 0)
1089 close(handler
->pinfd
);
1091 ret
= lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
);
1093 goto out_warn_father
;
1095 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1096 * https://github.com/lxc/lxd/issues/1978.
1098 if ((handler
->ns_clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
1099 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
1100 ret
= unshare(CLONE_NEWNET
);
1102 SYSERROR("Failed to unshare CLONE_NEWNET");
1103 goto out_warn_father
;
1105 INFO("Unshared CLONE_NEWNET");
1108 /* Tell the parent task it can begin to configure the container and wait
1111 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
);
1115 ret
= lxc_network_recv_veth_names_from_parent(handler
);
1117 ERROR("Failed to receive veth names from parent");
1118 goto out_warn_father
;
1121 /* If we are in a new user namespace, become root there to have
1122 * privilege over our namespace.
1124 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
1125 uid_t nsuid
= (handler
->conf
->root_nsuid_map
!= NULL
)
1127 : handler
->conf
->init_uid
;
1128 gid_t nsgid
= (handler
->conf
->root_nsgid_map
!= NULL
)
1130 : handler
->conf
->init_gid
;
1132 ret
= lxc_switch_uid_gid(nsuid
, nsgid
);
1134 goto out_warn_father
;
1136 /* Drop groups only after we switched to a valid gid in the new
1139 ret
= lxc_setgroups(0, NULL
);
1140 if (ret
< 0 && (handler
->am_root
|| errno
!= EPERM
))
1141 goto out_warn_father
;
1143 ret
= prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
1145 goto out_warn_father
;
1147 /* set{g,u}id() clears deathsignal */
1148 ret
= lxc_set_death_signal(SIGKILL
);
1150 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1151 goto out_warn_father
;
1155 ret
= access(handler
->lxcpath
, X_OK
);
1157 print_top_failing_dir(handler
->lxcpath
);
1158 goto out_warn_father
;
1161 ret
= snprintf(path
, sizeof(path
), "%s/dev/null",
1162 handler
->conf
->rootfs
.mount
);
1163 if (ret
< 0 || ret
>= sizeof(path
))
1164 goto out_warn_father
;
1166 /* In order to checkpoint restore, we need to have everything in the
1167 * same mount namespace. However, some containers may not have a
1168 * reasonable /dev (in particular, they may not have /dev/null), so we
1169 * can't set init's std fds to /dev/null by opening it from inside the
1172 * If that's the case, fall back to using the host's /dev/null. This
1173 * means that migration won't work, but at least we won't spew output
1174 * where it isn't wanted.
1176 if (handler
->backgrounded
&& !handler
->conf
->autodev
) {
1177 ret
= access(path
, F_OK
);
1179 devnull_fd
= open_devnull();
1182 goto out_warn_father
;
1183 WARN("Using /dev/null from the host for container "
1184 "init's standard file descriptors. Migration will "
1189 /* Ask father to setup cgroups and wait for him to finish. */
1190 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
);
1194 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1195 * earlier we end up with a wrong view of /proc/self/cgroup. For
1196 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1197 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1198 * /proc/self/cgroup would show us:
1202 * whereas it should actually show
1206 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1207 ret
= unshare(CLONE_NEWCGROUP
);
1209 INFO("Failed to unshare CLONE_NEWCGROUP");
1210 goto out_warn_father
;
1212 INFO("Unshared CLONE_NEWCGROUP");
1215 /* Add the requested environment variables to the current environment to
1216 * allow them to be used by the various hooks, such as the start hook
1219 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1220 ret
= putenv((char *)iterator
->elem
);
1222 SYSERROR("Failed to set environment variable: %s",
1223 (char *)iterator
->elem
);
1224 goto out_warn_father
;
1228 /* Setup the container, ip, names, utsname, ... */
1229 ret
= lxc_setup(handler
);
1230 close(handler
->data_sock
[1]);
1231 close(handler
->data_sock
[0]);
1233 ERROR("Failed to setup container \"%s\"", handler
->name
);
1234 goto out_warn_father
;
1237 /* Set the label to change to when we exec(2) the container's init. */
1238 ret
= lsm_process_label_set(NULL
, handler
->conf
, 1, 1);
1240 goto out_warn_father
;
1242 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1243 * before we aren't allowed anymore.
1245 if (handler
->conf
->no_new_privs
) {
1246 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1248 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1249 "execve() gainable privileges");
1250 goto out_warn_father
;
1252 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1256 /* Some init's such as busybox will set sane tty settings on stdin,
1257 * stdout, stderr which it thinks is the console. We already set them
1258 * the way we wanted on the real terminal, and we want init to do its
1259 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1260 * make sure that that pty is stdin,stdout,stderr.
1262 if (handler
->conf
->console
.slave
>= 0) {
1263 if (handler
->backgrounded
|| !handler
->conf
->is_execute
)
1264 ret
= set_stdfds(handler
->conf
->console
.slave
);
1266 ret
= lxc_terminal_set_stdfds(handler
->conf
->console
.slave
);
1268 ERROR("Failed to redirect std{in,out,err} to pty file "
1269 "descriptor %d", handler
->conf
->console
.slave
);
1270 goto out_warn_father
;
1274 /* If we mounted a temporary proc, then unmount it now. */
1275 tmp_proc_unmount(handler
->conf
);
1277 ret
= lxc_seccomp_load(handler
->conf
);
1279 goto out_warn_father
;
1281 ret
= run_lxc_hooks(handler
->name
, "start", handler
->conf
, NULL
);
1283 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1285 goto out_warn_father
;
1288 close(handler
->sigfd
);
1290 if (handler
->conf
->console
.slave
< 0 && handler
->backgrounded
) {
1291 if (devnull_fd
< 0) {
1292 devnull_fd
= open_devnull();
1294 goto out_warn_father
;
1297 ret
= set_stdfds(devnull_fd
);
1299 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1300 goto out_warn_father
;
1304 if (devnull_fd
>= 0) {
1311 if (handler
->conf
->init_cwd
) {
1312 ret
= chdir(handler
->conf
->init_cwd
);
1314 SYSERROR("Could not change directory to \"%s\"",
1315 handler
->conf
->init_cwd
);
1316 goto out_warn_father
;
1320 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
);
1322 goto out_warn_father
;
1324 /* Reset the environment variables the user requested in a clear
1328 /* Don't error out though. */
1330 SYSERROR("Failed to clear environment.");
1332 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1333 ret
= putenv((char *)iterator
->elem
);
1335 SYSERROR("Failed to set environment variable: %s",
1336 (char *)iterator
->elem
);
1337 goto out_warn_father
;
1341 ret
= putenv("container=lxc");
1343 SYSERROR("Failed to set environment variable: container=lxc");
1344 goto out_warn_father
;
1347 if (handler
->conf
->ttys
.tty_names
) {
1348 ret
= putenv(handler
->conf
->ttys
.tty_names
);
1350 SYSERROR("Failed to set environment variable for container ptys");
1351 goto out_warn_father
;
1355 /* The container has been setup. We can now switch to an unprivileged
1358 new_uid
= handler
->conf
->init_uid
;
1359 new_gid
= handler
->conf
->init_gid
;
1361 /* If we are in a new user namespace we already dropped all groups when
1362 * we switched to root in the new user namespace further above. Only
1363 * drop groups if we can, so ensure that we have necessary privilege.
1366 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1368 have_cap_setgid
= false;
1370 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1371 ret
= lxc_setgroups(0, NULL
);
1373 goto out_warn_father
;
1376 ret
= lxc_switch_uid_gid(new_uid
, new_gid
);
1378 goto out_warn_father
;
1380 ret
= lxc_ambient_caps_down();
1382 SYSERROR("Failed to clear ambient capabilities");
1383 goto out_warn_father
;
1386 /* After this call, we are in error because this ops should not return
1389 handler
->ops
->start(handler
, handler
->data
);
1392 /* We want the parent to know something went wrong, so we return a
1393 * special error code.
1395 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1398 if (devnull_fd
>= 0)
1404 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1407 struct lxc_terminal_info
*tty
;
1409 int sock
= handler
->data_sock
[1];
1410 struct lxc_conf
*conf
= handler
->conf
;
1411 struct lxc_tty_info
*ttys
= &conf
->ttys
;
1413 if (!conf
->ttys
.max
)
1416 ttys
->tty
= malloc(sizeof(*ttys
->tty
) * ttys
->max
);
1420 for (i
= 0; i
< conf
->ttys
.max
; i
++) {
1423 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1427 tty
= &ttys
->tty
[i
];
1429 tty
->master
= ttyfds
[0];
1430 tty
->slave
= ttyfds
[1];
1431 TRACE("Received pty with master fd %d and slave fd %d from "
1432 "parent", tty
->master
, tty
->slave
);
1436 SYSERROR("Failed to receive %zu ttys from child", ttys
->max
);
1438 TRACE("Received %zu ttys from child", ttys
->max
);
1443 int resolve_clone_flags(struct lxc_handler
*handler
)
1446 struct lxc_conf
*conf
= handler
->conf
;
1448 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1449 if (conf
->ns_keep
!= 0) {
1450 if ((conf
->ns_keep
& ns_info
[i
].clone_flag
) == 0)
1451 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1452 } else if (conf
->ns_clone
!= 0) {
1453 if ((conf
->ns_clone
& ns_info
[i
].clone_flag
) > 0)
1454 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1456 if (i
== LXC_NS_USER
&& lxc_list_empty(&handler
->conf
->id_map
))
1459 if (i
== LXC_NS_NET
&& lxc_requests_empty_network(handler
))
1462 if (i
== LXC_NS_CGROUP
&& !cgns_supported())
1465 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1468 if (!conf
->ns_share
[i
])
1471 handler
->ns_clone_flags
&= ~ns_info
[i
].clone_flag
;
1472 TRACE("Sharing %s namespace", ns_info
[i
].proc_name
);
1478 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1479 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1480 * But since the memory between parent and child is shared on CLONE_VM this
1481 * would invalidate the getpid() cache that glibc used to maintain and so
1482 * getpid() in the child would return the parent's pid. This is all fixed in
1483 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1484 * not reset anymore.
1485 * However, if for whatever reason you - dear commiter - somehow need to get the
1486 * pid of the dummy intermediate process for do_share_ns() you need to call
1487 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1490 static inline int do_share_ns(void *arg
)
1493 struct lxc_handler
*handler
= arg
;
1495 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1496 if (handler
->nsfd
[i
] < 0)
1499 ret
= setns(handler
->nsfd
[i
], 0);
1502 * Note that joining a user and/or mount namespace
1503 * requires the process is not multithreaded otherwise
1504 * setns() will fail here.
1506 SYSERROR("Failed to inherit %s namespace",
1507 ns_info
[i
].proc_name
);
1511 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1514 flags
= handler
->ns_on_clone_flags
;
1515 flags
|= CLONE_PARENT
;
1516 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, flags
);
1517 if (handler
->pid
< 0)
1523 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1524 * exec()s the requested container binary.
1525 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1526 * right here should be double checked if they'd pose a security risk. (For
1527 * example, any {u}mount() operations performed here will be reflected on the
1530 static int lxc_spawn(struct lxc_handler
*handler
)
1534 bool wants_to_map_ids
;
1535 struct lxc_list
*id_map
;
1536 const char *name
= handler
->name
;
1537 const char *lxcpath
= handler
->lxcpath
;
1538 bool share_ns
= false;
1539 struct lxc_conf
*conf
= handler
->conf
;
1540 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
1542 id_map
= &conf
->id_map
;
1543 wants_to_map_ids
= !lxc_list_empty(id_map
);
1545 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1546 if (!conf
->ns_share
[i
])
1549 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->ns_share
[i
], lxcpath
, ns_info
[i
].proc_name
);
1550 if (handler
->nsfd
[i
] < 0)
1556 ret
= lxc_sync_init(handler
);
1560 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1561 handler
->data_sock
);
1563 lxc_sync_fini(handler
);
1567 ret
= resolve_clone_flags(handler
);
1569 lxc_sync_fini(handler
);
1573 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1574 if (!lxc_list_empty(&conf
->network
)) {
1576 /* Find gateway addresses from the link device, which is
1577 * no longer accessible inside the container. Do this
1578 * before creating network interfaces, since goto
1579 * out_delete_net does not work before lxc_clone.
1581 ret
= lxc_find_gateway_addresses(handler
);
1583 ERROR("Failed to find gateway addresses");
1584 lxc_sync_fini(handler
);
1588 /* That should be done before the clone because we will
1589 * fill the netdev index and use them in the child.
1591 ret
= lxc_create_network_priv(handler
);
1593 ERROR("Failed to create the network");
1594 lxc_sync_fini(handler
);
1600 if (!cgroup_ops
->create(cgroup_ops
, handler
)) {
1601 ERROR("Failed creating cgroups");
1602 goto out_delete_net
;
1605 /* If the rootfs is not a blockdev, prevent the container from marking
1607 * If the container is unprivileged then skip rootfs pinning.
1609 if (!wants_to_map_ids
) {
1610 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1611 if (handler
->pinfd
== -1)
1612 INFO("Failed to pin the rootfs for container \"%s\"", handler
->name
);
1615 /* Create a process in a new set of namespaces. */
1616 handler
->ns_on_clone_flags
= handler
->ns_clone_flags
;
1617 if (handler
->ns_clone_flags
& CLONE_NEWUSER
) {
1618 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1619 * clone a new user namespace first and only later unshare our
1620 * network namespace to ensure that network devices ownership is
1623 handler
->ns_on_clone_flags
&= ~CLONE_NEWNET
;
1625 /* The cgroup namespace gets unshare()ed not clone()ed. */
1626 handler
->ns_on_clone_flags
&= ~CLONE_NEWCGROUP
;
1631 attacher_pid
= lxc_clone(do_share_ns
, handler
,
1632 CLONE_VFORK
| CLONE_VM
| CLONE_FILES
);
1633 if (attacher_pid
< 0) {
1634 SYSERROR(LXC_CLONE_ERROR
);
1635 goto out_delete_net
;
1638 ret
= wait_for_pid(attacher_pid
);
1640 SYSERROR("Intermediate process failed");
1641 goto out_delete_net
;
1644 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
,
1645 handler
->ns_on_clone_flags
);
1647 if (handler
->pid
< 0) {
1648 SYSERROR(LXC_CLONE_ERROR
);
1649 goto out_delete_net
;
1651 TRACE("Cloned child process %d", handler
->pid
);
1653 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1654 if (handler
->ns_on_clone_flags
& ns_info
[i
].clone_flag
)
1655 INFO("Cloned %s", ns_info
[i
].flag_name
);
1657 if (!lxc_try_preserve_namespaces(handler
, handler
->ns_on_clone_flags
, handler
->pid
)) {
1658 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1659 goto out_delete_net
;
1662 lxc_sync_fini_child(handler
);
1664 /* Map the container uids. The container became an invalid userid the
1665 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1666 * anything immediately, but allows the container to setuid(0) (0 being
1667 * mapped to something else on the host.) later to become a valid uid
1670 if (wants_to_map_ids
) {
1671 if (!handler
->conf
->ns_share
[LXC_NS_USER
] &&
1672 (handler
->conf
->ns_keep
& CLONE_NEWUSER
) == 0) {
1673 ret
= lxc_map_ids(id_map
, handler
->pid
);
1675 ERROR("Failed to set up id mapping.");
1676 goto out_delete_net
;
1681 ret
= lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
);
1683 goto out_delete_net
;
1685 ret
= lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
);
1687 goto out_delete_net
;
1689 if (!cgroup_ops
->setup_limits(cgroup_ops
, handler
->conf
, false)) {
1690 ERROR("Failed to setup cgroup limits for container \"%s\"", name
);
1691 goto out_delete_net
;
1694 if (!cgroup_ops
->enter(cgroup_ops
, handler
->pid
))
1695 goto out_delete_net
;
1697 if (!cgroup_ops
->chown(cgroup_ops
, handler
->conf
))
1698 goto out_delete_net
;
1700 /* Now we're ready to preserve the network namespace */
1701 ret
= lxc_try_preserve_ns(handler
->pid
, "net");
1703 if (ret
!= -EOPNOTSUPP
) {
1704 SYSERROR("Failed to preserve net namespace");
1705 goto out_delete_net
;
1708 handler
->nsfd
[LXC_NS_NET
] = ret
;
1709 DEBUG("Preserved net namespace via fd %d", ret
);
1712 /* Create the network configuration. */
1713 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1714 ret
= lxc_network_move_created_netdev_priv(handler
->lxcpath
,
1719 ERROR("Failed to create the configured network");
1720 goto out_delete_net
;
1723 ret
= lxc_create_network_unpriv(handler
->lxcpath
, handler
->name
,
1724 &conf
->network
, handler
->pid
, conf
->hooks_version
);
1726 ERROR("Failed to create the configured network");
1727 goto out_delete_net
;
1731 ret
= lxc_network_send_veth_names_to_child(handler
);
1733 ERROR("Failed to send veth names to child");
1734 goto out_delete_net
;
1737 if (!lxc_list_empty(&conf
->procs
)) {
1738 ret
= setup_proc_filesystem(&conf
->procs
, handler
->pid
);
1740 goto out_delete_net
;
1743 /* Tell the child to continue its initialization. We'll get
1744 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1746 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
);
1748 goto out_delete_net
;
1750 if (!lxc_list_empty(&conf
->limits
)) {
1751 ret
= setup_resource_limits(&conf
->limits
, handler
->pid
);
1753 ERROR("Failed to setup resource limits");
1754 goto out_delete_net
;
1758 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
);
1760 goto out_delete_net
;
1762 if (!cgroup_ops
->setup_limits(cgroup_ops
, handler
->conf
, true)) {
1763 ERROR("Failed to setup legacy device cgroup controller limits");
1764 goto out_delete_net
;
1766 TRACE("Set up legacy device cgroup controller limits");
1768 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1769 /* Now we're ready to preserve the cgroup namespace */
1770 ret
= lxc_try_preserve_ns(handler
->pid
, "cgroup");
1772 if (ret
!= -EOPNOTSUPP
) {
1773 SYSERROR("Failed to preserve cgroup namespace");
1774 goto out_delete_net
;
1777 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1778 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1782 ret
= snprintf(pidstr
, 20, "%d", handler
->pid
);
1783 if (ret
< 0 || ret
>= 20)
1784 goto out_delete_net
;
1786 ret
= setenv("LXC_PID", pidstr
, 1);
1788 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr
);
1790 /* Run any host-side start hooks */
1791 ret
= run_lxc_hooks(name
, "start-host", conf
, NULL
);
1793 ERROR("Failed to run lxc.hook.start-host");
1794 goto out_delete_net
;
1797 /* Tell the child to complete its initialization and wait for it to exec
1798 * or return an error. (The child will never return
1799 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1800 * lxc_sync_barrier_child to return success, or return a different
1801 * value, causing us to error out).
1803 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
);
1805 goto out_delete_net
;
1807 ret
= lxc_network_recv_name_and_ifindex_from_child(handler
);
1809 ERROR("Failed to receive names and ifindices for network "
1810 "devices from child");
1811 goto out_delete_net
;
1814 /* Now all networks are created, network devices are moved into place,
1815 * and the correct names and ifindeces in the respective namespaces have
1816 * been recorded. The corresponding structs have now all been filled. So
1817 * log them for debugging purposes.
1819 lxc_log_configured_netdevs(conf
);
1821 /* Read tty fds allocated by child. */
1822 ret
= lxc_recv_ttys_from_child(handler
);
1824 ERROR("Failed to receive tty info from child process");
1825 goto out_delete_net
;
1828 ret
= handler
->ops
->post_start(handler
, handler
->data
);
1832 ret
= lxc_set_state(name
, handler
, RUNNING
);
1834 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING
));
1838 lxc_sync_fini(handler
);
1843 if (handler
->ns_clone_flags
& CLONE_NEWNET
)
1844 lxc_delete_network(handler
);
1847 lxc_abort(name
, handler
);
1848 lxc_sync_fini(handler
);
1849 if (handler
->pinfd
>= 0) {
1850 close(handler
->pinfd
);
1851 handler
->pinfd
= -1;
1857 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1858 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1859 bool backgrounded
, int *error_num
)
1862 struct lxc_conf
*conf
= handler
->conf
;
1864 ret
= lxc_init(name
, handler
);
1866 ERROR("Failed to initialize container \"%s\"", name
);
1870 handler
->data
= data
;
1871 handler
->backgrounded
= backgrounded
;
1873 if (!attach_block_device(handler
->conf
)) {
1874 ERROR("Failed to attach block device");
1875 goto out_fini_nonet
;
1878 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1879 /* If the backing store is a device, mount it here and now. */
1880 if (rootfs_is_blockdev(conf
)) {
1881 ret
= unshare(CLONE_NEWNS
);
1883 ERROR("Failed to unshare CLONE_NEWNS");
1884 goto out_fini_nonet
;
1886 INFO("Unshared CLONE_NEWNS");
1888 remount_all_slave();
1889 ret
= do_rootfs_setup(conf
, name
, lxcpath
);
1891 ERROR("Error setting up rootfs mount as root before spawn");
1892 goto out_fini_nonet
;
1894 INFO("Set up container rootfs as host root");
1898 ret
= lxc_spawn(handler
);
1900 ERROR("Failed to spawn container \"%s\"", name
);
1901 goto out_detach_blockdev
;
1903 /* close parent side of data socket */
1904 close(handler
->data_sock
[0]);
1905 handler
->data_sock
[0] = -1;
1906 close(handler
->data_sock
[1]);
1907 handler
->data_sock
[1] = -1;
1909 handler
->conf
->reboot
= REBOOT_NONE
;
1911 ret
= lxc_poll(name
, handler
);
1913 ERROR("LXC mainloop exited with error: %d", ret
);
1917 if (!handler
->init_died
&& handler
->pid
> 0) {
1918 ERROR("Child process is not killed");
1922 status
= lxc_wait_for_pid_status(handler
->pid
);
1924 SYSERROR("Failed to retrieve status for %d", handler
->pid
);
1926 /* If the child process exited but was not signaled, it didn't call
1927 * reboot. This should mean it was an lxc-execute which simply exited.
1928 * In any case, treat it as a 'halt'.
1930 if (WIFSIGNALED(status
)) {
1931 switch(WTERMSIG(status
)) {
1932 case SIGINT
: /* halt */
1933 DEBUG("Container \"%s\" is halting", name
);
1935 case SIGHUP
: /* reboot */
1936 DEBUG("Container \"%s\" is rebooting", name
);
1937 handler
->conf
->reboot
= REBOOT_REQ
;
1939 case SIGSYS
: /* seccomp */
1940 DEBUG("Container \"%s\" violated its seccomp policy", name
);
1943 DEBUG("Unknown exit status for container \"%s\" init %d", name
, WTERMSIG(status
));
1948 ret
= lxc_restore_phys_nics_to_netns(handler
);
1950 ERROR("Failed to move physical network devices back to parent "
1951 "network namespace");
1953 if (handler
->pinfd
>= 0) {
1954 close(handler
->pinfd
);
1955 handler
->pinfd
= -1;
1958 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1959 lxc_error_set_and_log(handler
->pid
, status
);
1961 *error_num
= handler
->exit_status
;
1964 lxc_delete_network(handler
);
1966 out_detach_blockdev
:
1967 detach_block_device(handler
->conf
);
1970 lxc_fini(name
, handler
);
1974 lxc_abort(name
, handler
);
1982 static int start(struct lxc_handler
*handler
, void* data
)
1984 struct start_args
*arg
= data
;
1986 NOTICE("Exec'ing \"%s\"", arg
->argv
[0]);
1988 execvp(arg
->argv
[0], arg
->argv
);
1989 SYSERROR("Failed to exec \"%s\"", arg
->argv
[0]);
1993 static int post_start(struct lxc_handler
*handler
, void* data
)
1995 struct start_args
*arg
= data
;
1997 NOTICE("Started \"%s\" with pid \"%d\"", arg
->argv
[0], handler
->pid
);
2001 static struct lxc_operations start_ops
= {
2003 .post_start
= post_start
2006 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
2007 const char *lxcpath
, bool backgrounded
, int *error_num
)
2009 struct start_args start_arg
= {
2013 TRACE("Doing lxc_start");
2014 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
, error_num
);
2017 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
2020 char destroy
[MAXPATHLEN
];
2021 struct lxc_container
*c
;
2025 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
2026 bret
= do_destroy_container(handler
);
2028 ERROR("Error destroying rootfs for container \"%s\"", name
);
2032 INFO("Destroyed rootfs for container \"%s\"", name
);
2034 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
2035 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2036 ERROR("Error destroying directory for container \"%s\"", name
);
2040 c
= lxc_container_new(name
, handler
->lxcpath
);
2042 if (container_disk_lock(c
)) {
2043 INFO("Could not update lxc_snapshots file");
2044 lxc_container_put(c
);
2046 mod_all_rdeps(c
, false);
2047 container_disk_unlock(c
);
2048 lxc_container_put(c
);
2052 if (!handler
->am_root
)
2053 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
2054 destroy
, "lxc_rmdir_onedev_wrapper");
2056 ret
= lxc_rmdir_onedev(destroy
, NULL
);
2059 ERROR("Error destroying directory for container \"%s\"", name
);
2062 INFO("Destroyed directory for container \"%s\"", name
);
2065 static int lxc_rmdir_onedev_wrapper(void *data
)
2067 char *arg
= (char *) data
;
2068 return lxc_rmdir_onedev(arg
, NULL
);
2071 static bool do_destroy_container(struct lxc_handler
*handler
)
2075 if (!handler
->am_root
) {
2076 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
2077 handler
->conf
, "storage_destroy_wrapper");
2084 return storage_destroy(handler
->conf
);