]>
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
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(lxc_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);
117 while (p
< e
&& *p
== '/')
120 while (p
< e
&& *p
!= '/')
126 ret
= access(copy
, X_OK
);
128 SYSERROR("Could not access %s. Please grant it x "
129 "access, or add an ACL for the container "
137 static void lxc_put_nsfds(struct lxc_handler
*handler
)
141 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
142 if (handler
->nsfd
[i
] < 0)
145 close(handler
->nsfd
[i
]);
146 handler
->nsfd
[i
] = -EBADF
;
150 static int lxc_try_preserve_ns(const int pid
, const char *ns
)
154 fd
= lxc_preserve_ns(pid
, ns
);
156 if (errno
!= ENOENT
) {
157 SYSERROR("Failed to preserve %s namespace", ns
);
161 WARN("%s - Kernel does not support preserving %s namespaces",
162 strerror(errno
), ns
);
169 /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
170 * specified in ns_clone_flags.
171 * Return true on success, false on failure.
173 static bool lxc_try_preserve_namespaces(struct lxc_handler
*handler
,
174 int ns_clone_flags
, pid_t pid
)
178 for (i
= 0; i
< LXC_NS_MAX
; i
++)
179 handler
->nsfd
[i
] = -EBADF
;
181 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
184 if ((ns_clone_flags
& ns_info
[i
].clone_flag
) == 0)
187 fd
= lxc_try_preserve_ns(pid
, ns_info
[i
].proc_name
);
189 handler
->nsfd
[i
] = -EBADF
;
191 /* Do not fail to start container on kernels that do
192 * not support interacting with namespaces through
195 if (fd
== -EOPNOTSUPP
)
198 lxc_put_nsfds(handler
);
202 handler
->nsfd
[i
] = fd
;
203 DEBUG("Preserved %s namespace via fd %d", ns_info
[i
].proc_name
,
210 static int match_fd(int fd
)
212 return (fd
== 0 || fd
== 1 || fd
== 2);
215 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
216 int *fds_to_ignore
, size_t len_fds
)
221 struct dirent
*direntp
;
223 if (conf
&& conf
->close_all_fds
)
227 dir
= opendir("/proc/self/fd");
229 WARN("%s - Failed to open directory", strerror(errno
));
235 while ((direntp
= readdir(dir
))) {
237 struct lxc_list
*cur
;
238 bool matched
= false;
240 if (strcmp(direntp
->d_name
, ".") == 0)
243 if (strcmp(direntp
->d_name
, "..") == 0)
246 ret
= lxc_safe_int(direntp
->d_name
, &fd
);
248 INFO("Could not parse file descriptor for \"%s\"", direntp
->d_name
);
252 for (i
= 0; i
< len_fds
; i
++)
253 if (fds_to_ignore
[i
] == fd
)
256 if (fd
== fddir
|| fd
== lxc_log_fd
||
257 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
260 /* Keep state clients that wait on reboots. */
262 lxc_list_for_each(cur
, &conf
->state_clients
) {
263 struct lxc_state_client
*client
= cur
->elem
;
265 if (client
->clientfd
!= fd
)
276 if (current_config
&& fd
== current_config
->logfd
)
285 INFO("Closed inherited fd %d", fd
);
288 WARN("Inherited fd %d", fd
);
291 /* Only enable syslog at this point to avoid the above logging function
292 * to open a new fd and make the check_inherited function enter an
295 lxc_log_enable_syslog();
297 closedir(dir
); /* cannot fail */
301 static int setup_signal_fd(sigset_t
*oldmask
)
305 int signals
[] = {SIGBUS
, SIGILL
, SIGSEGV
, SIGWINCH
};
307 /* Block everything except serious error signals. */
308 ret
= sigfillset(&mask
);
312 for (sig
= 0; sig
< (sizeof(signals
) / sizeof(signals
[0])); sig
++) {
313 ret
= sigdelset(&mask
, signals
[sig
]);
318 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, oldmask
);
320 SYSERROR("Failed to set signal mask");
324 ret
= signalfd(-1, &mask
, SFD_CLOEXEC
);
326 SYSERROR("Failed to create signal file descriptor");
330 TRACE("Created signal file descriptor %d", ret
);
335 static int signal_handler(int fd
, uint32_t events
, void *data
,
336 struct lxc_epoll_descr
*descr
)
340 struct signalfd_siginfo siginfo
;
341 struct lxc_handler
*hdlr
= data
;
343 ret
= lxc_read_nointr(fd
, &siginfo
, sizeof(siginfo
));
345 ERROR("Failed to read signal info from signal file descriptor %d", fd
);
346 return LXC_MAINLOOP_ERROR
;
349 if (ret
!= sizeof(siginfo
)) {
350 ERROR("Unexpected size for struct signalfd_siginfo");
354 /* Check whether init is running. */
356 ret
= waitid(P_PID
, hdlr
->pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
357 if (ret
== 0 && info
.si_pid
== hdlr
->pid
)
358 hdlr
->init_died
= true;
360 /* Try to figure out a reasonable exit status to report. */
361 if (hdlr
->init_died
) {
362 switch (info
.si_code
) {
364 hdlr
->exit_status
= info
.si_status
<< 8;
369 hdlr
->exit_status
= info
.si_status
<< 8 | 0x7f;
372 /* Huh? The waitid() told us it's dead *and* continued? */
373 WARN("Init %d dead and continued?", hdlr
->pid
);
374 hdlr
->exit_status
= 1;
377 ERROR("Unknown si_code: %d", info
.si_code
);
378 hdlr
->exit_status
= 1;
382 if (siginfo
.ssi_signo
== SIGHUP
) {
383 kill(hdlr
->pid
, SIGTERM
);
384 INFO("Killing %d since terminal hung up", hdlr
->pid
);
385 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
388 if (siginfo
.ssi_signo
!= SIGCHLD
) {
389 kill(hdlr
->pid
, siginfo
.ssi_signo
);
390 INFO("Forwarded signal %d to pid %d", siginfo
.ssi_signo
, hdlr
->pid
);
391 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
394 /* More robustness, protect ourself from a SIGCHLD sent
395 * by a process different from the container init.
397 if (siginfo
.ssi_pid
!= hdlr
->pid
) {
398 NOTICE("Received %d from pid %d instead of container init %d",
399 siginfo
.ssi_signo
, siginfo
.ssi_pid
, hdlr
->pid
);
400 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
403 if (siginfo
.ssi_code
== CLD_STOPPED
) {
404 INFO("Container init process was stopped");
405 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
406 } else if (siginfo
.ssi_code
== CLD_CONTINUED
) {
407 INFO("Container init process was continued");
408 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: LXC_MAINLOOP_CONTINUE
;
411 DEBUG("Container init process %d exited", hdlr
->pid
);
412 return LXC_MAINLOOP_CLOSE
;
415 int lxc_serve_state_clients(const char *name
, struct lxc_handler
*handler
,
420 struct lxc_list
*cur
, *next
;
421 struct lxc_state_client
*client
;
422 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
425 handler
->state
= RUNNING
;
427 handler
->state
= state
;
429 TRACE("Set container state to %s", lxc_state2str(state
));
431 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
432 TRACE("No state clients registered");
436 retlen
= strlcpy(msg
.name
, name
, sizeof(msg
.name
));
437 if (retlen
>= sizeof(msg
.name
))
440 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
443 if (client
->states
[state
] == 0) {
444 TRACE("State %s not registered for state client %d",
445 lxc_state2str(state
), client
->clientfd
);
449 TRACE("Sending state %s to state client %d",
450 lxc_state2str(state
), client
->clientfd
);
453 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), 0);
455 if (errno
== EINTR
) {
456 TRACE("Caught EINTR; retrying");
460 ERROR("%s - Failed to send message to client",
464 /* kick client from list */
466 close(client
->clientfd
);
474 static int lxc_serve_state_socket_pair(const char *name
,
475 struct lxc_handler
*handler
,
480 if (!handler
->backgrounded
||
481 handler
->state_socket_pair
[1] < 0 ||
485 /* Close read end of the socket pair. */
486 close(handler
->state_socket_pair
[0]);
487 handler
->state_socket_pair
[0] = -1;
490 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
491 &(int){state
}, sizeof(int));
492 if (ret
!= sizeof(int)) {
495 SYSERROR("Failed to send state to %d",
496 handler
->state_socket_pair
[1]);
500 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
501 handler
->state_socket_pair
[1]);
503 /* Close write end of the socket pair. */
504 close(handler
->state_socket_pair
[1]);
505 handler
->state_socket_pair
[1] = -1;
510 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
515 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
517 ERROR("Failed to synchronize via anonymous pair of unix sockets");
521 ret
= lxc_serve_state_clients(name
, handler
, state
);
525 /* This function will try to connect to the legacy lxc-monitord state
526 * server and only exists for backwards compatibility.
528 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
533 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
536 bool has_console
= true;
537 struct lxc_epoll_descr descr
, descr_console
;
539 if (handler
->conf
->console
.path
&&
540 strcmp(handler
->conf
->console
.path
, "none") == 0)
543 ret
= lxc_mainloop_open(&descr
);
545 ERROR("Failed to create mainloop");
550 ret
= lxc_mainloop_open(&descr_console
);
552 ERROR("Failed to create console mainloop");
557 ret
= lxc_mainloop_add_handler(&descr
, handler
->sigfd
, signal_handler
, handler
);
559 ERROR("Failed to add signal handler for %d to mainloop", handler
->sigfd
);
560 goto out_mainloop_console
;
564 struct lxc_terminal
*console
= &handler
->conf
->console
;
566 ret
= lxc_terminal_mainloop_add(&descr
, console
);
568 ERROR("Failed to add console handlers to mainloop");
569 goto out_mainloop_console
;
572 ret
= lxc_terminal_mainloop_add(&descr_console
, console
);
574 ERROR("Failed to add console handlers to console mainloop");
575 goto out_mainloop_console
;
578 handler
->conf
->console
.descr
= &descr
;
581 ret
= lxc_cmd_mainloop_add(name
, &descr
, handler
);
583 ERROR("Failed to add command handler to mainloop");
584 goto out_mainloop_console
;
587 TRACE("Mainloop is ready");
589 ret
= lxc_mainloop(&descr
, -1);
592 if (ret
< 0 || !handler
->init_died
)
593 goto out_mainloop_console
;
596 ret
= lxc_mainloop(&descr_console
, 0);
598 out_mainloop_console
:
600 lxc_mainloop_close(&descr_console
);
601 TRACE("Closed console mainloop");
605 lxc_mainloop_close(&descr
);
606 TRACE("Closed mainloop");
609 close(handler
->sigfd
);
610 TRACE("Closed signal file descriptor %d", handler
->sigfd
);
611 handler
->sigfd
= -EBADF
;
616 void lxc_zero_handler(struct lxc_handler
*handler
)
620 memset(handler
, 0, sizeof(struct lxc_handler
));
626 for (i
= 0; i
< LXC_NS_MAX
; i
++)
627 handler
->nsfd
[i
] = -1;
629 handler
->data_sock
[0] = -1;
630 handler
->data_sock
[1] = -1;
632 handler
->state_socket_pair
[0] = -1;
633 handler
->state_socket_pair
[1] = -1;
635 handler
->sync_sock
[0] = -1;
636 handler
->sync_sock
[1] = -1;
639 void lxc_free_handler(struct lxc_handler
*handler
)
641 if (handler
->pinfd
>= 0)
642 close(handler
->pinfd
);
644 if (handler
->sigfd
>= 0)
645 close(handler
->sigfd
);
647 lxc_put_nsfds(handler
);
649 if (handler
->conf
&& handler
->conf
->reboot
== REBOOT_NONE
)
650 if (handler
->conf
->maincmd_fd
>= 0)
651 close(handler
->conf
->maincmd_fd
);
653 if (handler
->state_socket_pair
[0] >= 0)
654 close(handler
->state_socket_pair
[0]);
656 if (handler
->state_socket_pair
[1] >= 0)
657 close(handler
->state_socket_pair
[1]);
659 handler
->conf
= NULL
;
664 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
665 const char *lxcpath
, bool daemonize
)
668 struct lxc_handler
*handler
;
670 handler
= malloc(sizeof(*handler
));
673 memset(handler
, 0, sizeof(*handler
));
675 /* Note that am_guest_unpriv() checks the effective uid. We
676 * probably don't care if we are real root only if we are running
677 * as root so this should be fine.
679 handler
->am_root
= !am_guest_unpriv();
680 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
681 handler
->conf
= conf
;
682 handler
->lxcpath
= lxcpath
;
684 handler
->sigfd
= -EBADF
;
685 handler
->init_died
= false;
686 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
687 if (handler
->conf
->reboot
== REBOOT_NONE
)
688 lxc_list_init(&handler
->conf
->state_clients
);
690 for (i
= 0; i
< LXC_NS_MAX
; i
++)
691 handler
->nsfd
[i
] = -1;
693 handler
->name
= name
;
695 if (daemonize
&& handler
->conf
->reboot
== REBOOT_NONE
) {
696 /* Create socketpair() to synchronize on daemonized startup.
697 * When the container reboots we don't need to synchronize
698 * again currently so don't open another socketpair().
700 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
701 handler
->state_socket_pair
);
703 ERROR("Failed to create anonymous pair of unix sockets");
706 TRACE("Created anonymous pair {%d,%d} of unix sockets",
707 handler
->state_socket_pair
[0],
708 handler
->state_socket_pair
[1]);
711 if (handler
->conf
->reboot
== REBOOT_NONE
) {
712 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
713 if (handler
->conf
->maincmd_fd
< 0) {
714 ERROR("Failed to set up command socket");
718 TRACE("Unix domain socket %d for command server is ready",
719 handler
->conf
->maincmd_fd
);
724 lxc_free_handler(handler
);
729 int lxc_init(const char *name
, struct lxc_handler
*handler
)
732 const char *loglevel
;
733 struct lxc_conf
*conf
= handler
->conf
;
736 TRACE("Initialized LSM");
738 ret
= lxc_read_seccomp_config(conf
);
740 ERROR("Failed loading seccomp policy");
741 goto out_close_maincmd_fd
;
743 TRACE("Read seccomp policy");
745 /* Begin by setting the state to STARTING. */
746 ret
= lxc_set_state(name
, handler
, STARTING
);
748 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING
));
749 goto out_close_maincmd_fd
;
751 TRACE("Set container state to \"STARTING\"");
753 /* Start of environment variable setup for hooks. */
755 ret
= setenv("LXC_NAME", name
, 1);
757 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name
);
761 ret
= setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1);
763 SYSERROR("Failed to set environment variable: "
764 "LXC_CONFIG_FILE=%s", conf
->rcfile
);
767 if (conf
->rootfs
.mount
) {
768 ret
= setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1);
770 SYSERROR("Failed to set environment variable: "
771 "LXC_ROOTFS_MOUNT=%s", conf
->rootfs
.mount
);
774 if (conf
->rootfs
.path
) {
775 ret
= setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1);
777 SYSERROR("Failed to set environment variable: "
778 "LXC_ROOTFS_PATH=%s", conf
->rootfs
.path
);
781 if (conf
->console
.path
) {
782 ret
= setenv("LXC_CONSOLE", conf
->console
.path
, 1);
784 SYSERROR("Failed to set environment variable: "
785 "LXC_CONSOLE=%s", conf
->console
.path
);
788 if (conf
->console
.log_path
) {
789 ret
= setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1);
791 SYSERROR("Failed to set environment variable: "
792 "LXC_CONSOLE_LOGPATH=%s", conf
->console
.log_path
);
795 if (cgns_supported()) {
796 ret
= setenv("LXC_CGNS_AWARE", "1", 1);
798 SYSERROR("Failed to set environment variable "
802 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
803 ret
= setenv("LXC_LOG_LEVEL", loglevel
, 1);
805 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
808 if (conf
->hooks_version
== 0)
809 ret
= setenv("LXC_HOOK_VERSION", "0", 1);
811 ret
= setenv("LXC_HOOK_VERSION", "1", 1);
813 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf
->hooks_version
);
814 /* End of environment variable setup for hooks. */
816 TRACE("Set environment variables");
818 ret
= run_lxc_hooks(name
, "pre-start", conf
, NULL
);
820 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name
);
823 TRACE("Ran pre-start hooks");
825 /* The signal fd has to be created before forking otherwise if the child
826 * process exits before we setup the signal fd, the event will be lost
827 * and the command will be stuck.
829 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
830 if (handler
->sigfd
< 0) {
831 ERROR("Failed to setup SIGCHLD fd handler.");
834 TRACE("Set up signal fd");
836 /* Do this after setting up signals since it might unblock SIGWINCH. */
837 ret
= lxc_terminal_setup(conf
);
839 ERROR("Failed to create console");
840 goto out_restore_sigmask
;
842 TRACE("Created console");
844 ret
= lxc_terminal_map_ids(conf
, &conf
->console
);
846 ERROR("Failed to chown console");
847 goto out_restore_sigmask
;
849 TRACE("Chowned console");
851 handler
->cgroup_ops
= cgroup_init(handler
);
852 if (!handler
->cgroup_ops
) {
853 ERROR("Failed to initialize cgroup driver");
854 goto out_restore_sigmask
;
856 TRACE("Initialized cgroup driver");
858 INFO("Container \"%s\" is initialized", name
);
862 (void)pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
864 lxc_delete_tty(&conf
->ttys
);
866 (void)lxc_set_state(name
, handler
, ABORTING
);
867 out_close_maincmd_fd
:
868 close(conf
->maincmd_fd
);
869 conf
->maincmd_fd
= -1;
873 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
877 struct lxc_list
*cur
, *next
;
878 char *namespaces
[LXC_NS_MAX
+ 1];
879 size_t namespace_count
= 0;
880 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
882 /* The STOPPING state is there for future cleanup code which can take
885 lxc_set_state(name
, handler
, STOPPING
);
887 self
= lxc_raw_getpid();
888 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
889 if (handler
->nsfd
[i
] < 0)
892 if (handler
->conf
->hooks_version
== 0)
893 ret
= asprintf(&namespaces
[namespace_count
],
894 "%s:/proc/%d/fd/%d", ns_info
[i
].proc_name
,
895 self
, handler
->nsfd
[i
]);
897 ret
= asprintf(&namespaces
[namespace_count
],
898 "/proc/%d/fd/%d", self
, handler
->nsfd
[i
]);
900 SYSERROR("Failed to allocate memory");
904 if (handler
->conf
->hooks_version
== 0) {
909 ret
= setenv(ns_info
[i
].env_name
, namespaces
[namespace_count
], 1);
911 SYSERROR("Failed to set environment variable %s=%s",
912 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
914 TRACE("Set environment variable %s=%s",
915 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
919 namespaces
[namespace_count
] = NULL
;
921 if (handler
->conf
->reboot
> REBOOT_NONE
) {
922 ret
= setenv("LXC_TARGET", "reboot", 1);
924 SYSERROR("Failed to set environment variable: "
925 "LXC_TARGET=reboot");
928 if (handler
->conf
->reboot
== REBOOT_NONE
) {
929 ret
= setenv("LXC_TARGET", "stop", 1);
931 SYSERROR("Failed to set environment variable: "
935 if (handler
->conf
->hooks_version
== 0)
936 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, namespaces
);
938 ret
= run_lxc_hooks(name
, "stop", handler
->conf
, NULL
);
940 ERROR("Failed to run \"lxc.hook.stop\" hook");
942 while (namespace_count
--)
943 free(namespaces
[namespace_count
]);
945 cgroup_ops
->destroy(cgroup_ops
, handler
);
946 cgroup_exit(cgroup_ops
);
948 if (handler
->conf
->reboot
== REBOOT_NONE
) {
949 /* For all new state clients simply close the command socket.
950 * This will inform all state clients that the container is
951 * STOPPED and also prevents a race between a open()/close() on
952 * the command socket causing a new process to get ECONNREFUSED
953 * because we haven't yet closed the command socket.
955 close(handler
->conf
->maincmd_fd
);
956 handler
->conf
->maincmd_fd
= -1;
957 TRACE("Closed command socket");
959 /* This function will try to connect to the legacy lxc-monitord
960 * state server and only exists for backwards compatibility.
962 lxc_monitor_send_state(name
, STOPPED
, handler
->lxcpath
);
964 /* The command socket is closed so no one can acces the command
965 * socket anymore so there's no need to lock it.
967 handler
->state
= STOPPED
;
968 TRACE("Set container state to \"STOPPED\"");
970 lxc_set_state(name
, handler
, STOPPED
);
973 ret
= run_lxc_hooks(name
, "post-stop", handler
->conf
, NULL
);
975 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name
);
976 if (handler
->conf
->reboot
> REBOOT_NONE
) {
977 WARN("Container will be stopped instead of rebooted");
978 handler
->conf
->reboot
= REBOOT_NONE
;
980 ret
= setenv("LXC_TARGET", "stop", 1);
982 WARN("Failed to set environment variable: "
987 /* Reset mask set by setup_signal_fd. */
988 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
990 WARN("%s - Failed to restore signal mask", strerror(errno
));
992 lxc_terminal_delete(&handler
->conf
->console
);
993 lxc_delete_tty(&handler
->conf
->ttys
);
995 /* The command socket is now closed, no more state clients can register
996 * themselves from now on. So free the list of state clients.
998 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
999 struct lxc_state_client
*client
= cur
->elem
;
1001 /* Keep state clients that want to be notified about reboots. */
1002 if ((handler
->conf
->reboot
> REBOOT_NONE
) &&
1003 (client
->states
[RUNNING
] == 2))
1006 /* close state client socket */
1008 close(client
->clientfd
);
1013 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= REBOOT_REQ
)
1014 lxc_destroy_container_on_signal(handler
, name
);
1016 lxc_free_handler(handler
);
1019 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
1023 lxc_set_state(name
, handler
, ABORTING
);
1025 if (handler
->pid
> 0) {
1026 ret
= kill(handler
->pid
, SIGKILL
);
1028 SYSERROR("Failed to send SIGKILL to %d", handler
->pid
);
1031 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
1036 static int do_start(void *data
)
1039 char path
[PATH_MAX
];
1040 bool have_cap_setgid
;
1043 struct lxc_list
*iterator
;
1044 int devnull_fd
= -1;
1045 struct lxc_handler
*handler
= data
;
1047 lxc_sync_fini_parent(handler
);
1049 /* This prctl must be before the synchro, so if the parent dies before
1050 * we set the parent death signal, we will detect its death with the
1051 * synchro right after, otherwise we have a window where the parent can
1052 * exit before we set the pdeath signal leading to a unsupervized
1055 ret
= lxc_set_death_signal(SIGKILL
);
1057 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1058 goto out_warn_father
;
1061 ret
= lxc_ambient_caps_up();
1063 SYSERROR("Failed to raise ambient capabilities");
1064 goto out_warn_father
;
1067 ret
= pthread_sigmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
1069 SYSERROR("Failed to set signal mask");
1070 goto out_warn_father
;
1073 /* Don't leak the pinfd to the container. */
1074 if (handler
->pinfd
>= 0)
1075 close(handler
->pinfd
);
1077 ret
= lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
);
1079 goto out_warn_father
;
1081 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1082 * https://github.com/lxc/lxd/issues/1978.
1084 if ((handler
->ns_clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
1085 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
1086 ret
= unshare(CLONE_NEWNET
);
1088 SYSERROR("Failed to unshare CLONE_NEWNET");
1089 goto out_warn_father
;
1091 INFO("Unshared CLONE_NEWNET");
1094 /* Tell the parent task it can begin to configure the container and wait
1097 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
);
1101 ret
= lxc_network_recv_veth_names_from_parent(handler
);
1103 ERROR("Failed to receive veth names from parent");
1104 goto out_warn_father
;
1107 /* If we are in a new user namespace, become root there to have
1108 * privilege over our namespace.
1110 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
1111 uid_t nsuid
= (handler
->conf
->root_nsuid_map
!= NULL
)
1113 : handler
->conf
->init_uid
;
1114 gid_t nsgid
= (handler
->conf
->root_nsgid_map
!= NULL
)
1116 : handler
->conf
->init_gid
;
1118 ret
= lxc_switch_uid_gid(nsuid
, nsgid
);
1120 goto out_warn_father
;
1122 /* Drop groups only after we switched to a valid gid in the new
1125 ret
= lxc_setgroups(0, NULL
);
1126 if (ret
< 0 && (handler
->am_root
|| errno
!= EPERM
))
1127 goto out_warn_father
;
1129 ret
= prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
1131 goto out_warn_father
;
1133 /* set{g,u}id() clears deathsignal */
1134 ret
= lxc_set_death_signal(SIGKILL
);
1136 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1137 goto out_warn_father
;
1141 ret
= access(handler
->lxcpath
, X_OK
);
1143 print_top_failing_dir(handler
->lxcpath
);
1144 goto out_warn_father
;
1147 ret
= snprintf(path
, sizeof(path
), "%s/dev/null",
1148 handler
->conf
->rootfs
.mount
);
1149 if (ret
< 0 || ret
>= sizeof(path
))
1150 goto out_warn_father
;
1152 /* In order to checkpoint restore, we need to have everything in the
1153 * same mount namespace. However, some containers may not have a
1154 * reasonable /dev (in particular, they may not have /dev/null), so we
1155 * can't set init's std fds to /dev/null by opening it from inside the
1158 * If that's the case, fall back to using the host's /dev/null. This
1159 * means that migration won't work, but at least we won't spew output
1160 * where it isn't wanted.
1162 if (handler
->backgrounded
&& !handler
->conf
->autodev
) {
1163 ret
= access(path
, F_OK
);
1165 devnull_fd
= open_devnull();
1168 goto out_warn_father
;
1169 WARN("Using /dev/null from the host for container "
1170 "init's standard file descriptors. Migration will "
1175 /* Ask father to setup cgroups and wait for him to finish. */
1176 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
);
1180 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1181 * earlier we end up with a wrong view of /proc/self/cgroup. For
1182 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1183 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1184 * /proc/self/cgroup would show us:
1188 * whereas it should actually show
1192 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1193 ret
= unshare(CLONE_NEWCGROUP
);
1195 INFO("Failed to unshare CLONE_NEWCGROUP");
1196 goto out_warn_father
;
1198 INFO("Unshared CLONE_NEWCGROUP");
1201 /* Add the requested environment variables to the current environment to
1202 * allow them to be used by the various hooks, such as the start hook
1205 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1206 ret
= putenv((char *)iterator
->elem
);
1208 SYSERROR("Failed to set environment variable: %s",
1209 (char *)iterator
->elem
);
1210 goto out_warn_father
;
1214 /* Setup the container, ip, names, utsname, ... */
1215 ret
= lxc_setup(handler
);
1216 close(handler
->data_sock
[1]);
1217 close(handler
->data_sock
[0]);
1219 ERROR("Failed to setup container \"%s\"", handler
->name
);
1220 goto out_warn_father
;
1223 /* Set the label to change to when we exec(2) the container's init. */
1224 ret
= lsm_process_label_set(NULL
, handler
->conf
, 1, 1);
1226 goto out_warn_father
;
1228 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1229 * before we aren't allowed anymore.
1231 if (handler
->conf
->no_new_privs
) {
1232 ret
= prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0);
1234 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1235 "execve() gainable privileges");
1236 goto out_warn_father
;
1238 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1242 /* Some init's such as busybox will set sane tty settings on stdin,
1243 * stdout, stderr which it thinks is the console. We already set them
1244 * the way we wanted on the real terminal, and we want init to do its
1245 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1246 * make sure that that pty is stdin,stdout,stderr.
1248 if (handler
->conf
->console
.slave
>= 0) {
1249 if (handler
->backgrounded
|| !handler
->conf
->is_execute
)
1250 ret
= set_stdfds(handler
->conf
->console
.slave
);
1252 ret
= lxc_terminal_set_stdfds(handler
->conf
->console
.slave
);
1254 ERROR("Failed to redirect std{in,out,err} to pty file "
1255 "descriptor %d", handler
->conf
->console
.slave
);
1256 goto out_warn_father
;
1260 /* If we mounted a temporary proc, then unmount it now. */
1261 tmp_proc_unmount(handler
->conf
);
1263 ret
= lxc_seccomp_load(handler
->conf
);
1265 goto out_warn_father
;
1267 ret
= run_lxc_hooks(handler
->name
, "start", handler
->conf
, NULL
);
1269 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1271 goto out_warn_father
;
1274 close(handler
->sigfd
);
1276 if (devnull_fd
< 0) {
1277 devnull_fd
= open_devnull();
1279 goto out_warn_father
;
1282 if (handler
->conf
->console
.slave
< 0 && handler
->backgrounded
) {
1283 ret
= set_stdfds(devnull_fd
);
1285 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1286 goto out_warn_father
;
1290 if (devnull_fd
>= 0) {
1297 if (handler
->conf
->init_cwd
) {
1298 ret
= chdir(handler
->conf
->init_cwd
);
1300 SYSERROR("Could not change directory to \"%s\"",
1301 handler
->conf
->init_cwd
);
1302 goto out_warn_father
;
1306 ret
= lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
);
1308 goto out_warn_father
;
1310 /* Reset the environment variables the user requested in a clear
1314 /* Don't error out though. */
1316 SYSERROR("Failed to clear environment.");
1318 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1319 ret
= putenv((char *)iterator
->elem
);
1321 SYSERROR("Failed to set environment variable: %s",
1322 (char *)iterator
->elem
);
1323 goto out_warn_father
;
1327 ret
= putenv("container=lxc");
1329 SYSERROR("Failed to set environment variable: container=lxc");
1330 goto out_warn_father
;
1333 if (handler
->conf
->ttys
.tty_names
) {
1334 ret
= putenv(handler
->conf
->ttys
.tty_names
);
1336 SYSERROR("Failed to set environment variable for container ptys");
1337 goto out_warn_father
;
1341 /* The container has been setup. We can now switch to an unprivileged
1344 new_uid
= handler
->conf
->init_uid
;
1345 new_gid
= handler
->conf
->init_gid
;
1347 /* If we are in a new user namespace we already dropped all groups when
1348 * we switched to root in the new user namespace further above. Only
1349 * drop groups if we can, so ensure that we have necessary privilege.
1352 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1354 have_cap_setgid
= false;
1356 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1357 ret
= lxc_setgroups(0, NULL
);
1359 goto out_warn_father
;
1362 ret
= lxc_switch_uid_gid(new_uid
, new_gid
);
1364 goto out_warn_father
;
1366 ret
= lxc_ambient_caps_down();
1368 SYSERROR("Failed to clear ambient capabilities");
1369 goto out_warn_father
;
1372 /* After this call, we are in error because this ops should not return
1375 handler
->ops
->start(handler
, handler
->data
);
1378 /* We want the parent to know something went wrong, so we return a
1379 * special error code.
1381 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1384 if (devnull_fd
>= 0)
1390 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1393 struct lxc_terminal_info
*tty
;
1395 int sock
= handler
->data_sock
[1];
1396 struct lxc_conf
*conf
= handler
->conf
;
1397 struct lxc_tty_info
*ttys
= &conf
->ttys
;
1399 if (!conf
->ttys
.max
)
1402 ttys
->tty
= malloc(sizeof(*ttys
->tty
) * ttys
->max
);
1406 for (i
= 0; i
< conf
->ttys
.max
; i
++) {
1409 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1413 tty
= &ttys
->tty
[i
];
1415 tty
->master
= ttyfds
[0];
1416 tty
->slave
= ttyfds
[1];
1417 TRACE("Received pty with master fd %d and slave fd %d from "
1418 "parent", tty
->master
, tty
->slave
);
1421 ERROR("Failed to receive %zu ttys from child: %s", ttys
->max
,
1424 TRACE("Received %zu ttys from child", ttys
->max
);
1429 int resolve_clone_flags(struct lxc_handler
*handler
)
1432 struct lxc_conf
*conf
= handler
->conf
;
1434 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1435 if (conf
->ns_keep
!= 0) {
1436 if ((conf
->ns_keep
& ns_info
[i
].clone_flag
) == 0)
1437 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1438 } else if (conf
->ns_clone
!= 0) {
1439 if ((conf
->ns_clone
& ns_info
[i
].clone_flag
) > 0)
1440 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1442 if (i
== LXC_NS_USER
&& lxc_list_empty(&handler
->conf
->id_map
))
1445 if (i
== LXC_NS_NET
&& lxc_requests_empty_network(handler
))
1448 if (i
== LXC_NS_CGROUP
&& !cgns_supported())
1451 handler
->ns_clone_flags
|= ns_info
[i
].clone_flag
;
1454 if (!conf
->ns_share
[i
])
1457 handler
->ns_clone_flags
&= ~ns_info
[i
].clone_flag
;
1458 TRACE("Sharing %s namespace", ns_info
[i
].proc_name
);
1464 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1465 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1466 * But since the memory between parent and child is shared on CLONE_VM this
1467 * would invalidate the getpid() cache that glibc used to maintain and so
1468 * getpid() in the child would return the parent's pid. This is all fixed in
1469 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1470 * not reset anymore.
1471 * However, if for whatever reason you - dear commiter - somehow need to get the
1472 * pid of the dummy intermediate process for do_share_ns() you need to call
1473 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1476 static inline int do_share_ns(void *arg
)
1479 struct lxc_handler
*handler
= arg
;
1481 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1482 if (handler
->nsfd
[i
] < 0)
1485 ret
= setns(handler
->nsfd
[i
], 0);
1488 * Note that joining a user and/or mount namespace
1489 * requires the process is not multithreaded otherwise
1490 * setns() will fail here.
1492 SYSERROR("Failed to inherit %s namespace",
1493 ns_info
[i
].proc_name
);
1497 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1500 flags
= handler
->ns_on_clone_flags
;
1501 flags
|= CLONE_PARENT
;
1502 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, flags
);
1503 if (handler
->pid
< 0)
1509 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1510 * exec()s the requested container binary.
1511 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1512 * right here should be double checked if they'd pose a security risk. (For
1513 * example, any {u}mount() operations performed here will be reflected on the
1516 static int lxc_spawn(struct lxc_handler
*handler
)
1520 bool wants_to_map_ids
;
1521 struct lxc_list
*id_map
;
1522 const char *name
= handler
->name
;
1523 const char *lxcpath
= handler
->lxcpath
;
1524 bool share_ns
= false;
1525 struct lxc_conf
*conf
= handler
->conf
;
1526 struct cgroup_ops
*cgroup_ops
= handler
->cgroup_ops
;
1528 id_map
= &conf
->id_map
;
1529 wants_to_map_ids
= !lxc_list_empty(id_map
);
1531 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1532 if (!conf
->ns_share
[i
])
1535 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->ns_share
[i
], lxcpath
, ns_info
[i
].proc_name
);
1536 if (handler
->nsfd
[i
] < 0)
1542 ret
= lxc_sync_init(handler
);
1546 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1547 handler
->data_sock
);
1549 lxc_sync_fini(handler
);
1553 ret
= resolve_clone_flags(handler
);
1555 lxc_sync_fini(handler
);
1559 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1560 if (!lxc_list_empty(&conf
->network
)) {
1562 /* Find gateway addresses from the link device, which is
1563 * no longer accessible inside the container. Do this
1564 * before creating network interfaces, since goto
1565 * out_delete_net does not work before lxc_clone.
1567 ret
= lxc_find_gateway_addresses(handler
);
1569 ERROR("Failed to find gateway addresses");
1570 lxc_sync_fini(handler
);
1574 /* That should be done before the clone because we will
1575 * fill the netdev index and use them in the child.
1577 ret
= lxc_create_network_priv(handler
);
1579 ERROR("Failed to create the network");
1580 lxc_sync_fini(handler
);
1586 if (!cgroup_ops
->create(cgroup_ops
, handler
)) {
1587 ERROR("Failed creating cgroups");
1588 goto out_delete_net
;
1591 /* If the rootfs is not a blockdev, prevent the container from marking
1593 * If the container is unprivileged then skip rootfs pinning.
1595 if (!wants_to_map_ids
) {
1596 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1597 if (handler
->pinfd
== -1)
1598 INFO("Failed to pin the rootfs for container \"%s\"", handler
->name
);
1601 /* Create a process in a new set of namespaces. */
1602 handler
->ns_on_clone_flags
= handler
->ns_clone_flags
;
1603 if (handler
->ns_clone_flags
& CLONE_NEWUSER
) {
1604 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1605 * clone a new user namespace first and only later unshare our
1606 * network namespace to ensure that network devices ownership is
1609 handler
->ns_on_clone_flags
&= ~CLONE_NEWNET
;
1611 /* The cgroup namespace gets unshare()ed not clone()ed. */
1612 handler
->ns_on_clone_flags
&= ~CLONE_NEWCGROUP
;
1617 attacher_pid
= lxc_clone(do_share_ns
, handler
,
1618 CLONE_VFORK
| CLONE_VM
| CLONE_FILES
);
1619 if (attacher_pid
< 0) {
1620 SYSERROR(LXC_CLONE_ERROR
);
1621 goto out_delete_net
;
1624 ret
= wait_for_pid(attacher_pid
);
1626 SYSERROR("Intermediate process failed");
1627 goto out_delete_net
;
1630 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
,
1631 handler
->ns_on_clone_flags
);
1633 if (handler
->pid
< 0) {
1634 SYSERROR(LXC_CLONE_ERROR
);
1635 goto out_delete_net
;
1637 TRACE("Cloned child process %d", handler
->pid
);
1639 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1640 if (handler
->ns_on_clone_flags
& ns_info
[i
].clone_flag
)
1641 INFO("Cloned %s", ns_info
[i
].flag_name
);
1643 if (!lxc_try_preserve_namespaces(handler
, handler
->ns_on_clone_flags
, handler
->pid
)) {
1644 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1645 goto out_delete_net
;
1648 lxc_sync_fini_child(handler
);
1650 /* Map the container uids. The container became an invalid userid the
1651 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1652 * anything immediately, but allows the container to setuid(0) (0 being
1653 * mapped to something else on the host.) later to become a valid uid
1656 if (wants_to_map_ids
) {
1657 if (!handler
->conf
->ns_share
[LXC_NS_USER
] &&
1658 (handler
->conf
->ns_keep
& CLONE_NEWUSER
) == 0) {
1659 ret
= lxc_map_ids(id_map
, handler
->pid
);
1661 ERROR("Failed to set up id mapping.");
1662 goto out_delete_net
;
1667 ret
= lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
);
1669 goto out_delete_net
;
1671 ret
= lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
);
1673 goto out_delete_net
;
1675 if (!cgroup_ops
->setup_limits(cgroup_ops
, handler
->conf
, false)) {
1676 ERROR("Failed to setup cgroup limits for container \"%s\"", name
);
1677 goto out_delete_net
;
1680 if (!cgroup_ops
->enter(cgroup_ops
, handler
->pid
))
1681 goto out_delete_net
;
1683 if (!cgroup_ops
->chown(cgroup_ops
, handler
->conf
))
1684 goto out_delete_net
;
1686 /* Now we're ready to preserve the network namespace */
1687 ret
= lxc_try_preserve_ns(handler
->pid
, "net");
1689 if (ret
!= -EOPNOTSUPP
) {
1690 ERROR("%s - Failed to preserve net namespace", strerror(errno
));
1691 goto out_delete_net
;
1694 handler
->nsfd
[LXC_NS_NET
] = ret
;
1695 DEBUG("Preserved net namespace via fd %d", ret
);
1698 /* Create the network configuration. */
1699 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
1700 ret
= lxc_network_move_created_netdev_priv(handler
->lxcpath
,
1705 ERROR("Failed to create the configured network");
1706 goto out_delete_net
;
1709 ret
= lxc_create_network_unpriv(handler
->lxcpath
, handler
->name
,
1710 &conf
->network
, handler
->pid
, conf
->hooks_version
);
1712 ERROR("Failed to create the configured network");
1713 goto out_delete_net
;
1717 ret
= lxc_network_send_veth_names_to_child(handler
);
1719 ERROR("Failed to send veth names to child");
1720 goto out_delete_net
;
1723 if (!lxc_list_empty(&conf
->procs
)) {
1724 ret
= setup_proc_filesystem(&conf
->procs
, handler
->pid
);
1726 goto out_delete_net
;
1729 /* Tell the child to continue its initialization. We'll get
1730 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1732 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
);
1734 goto out_delete_net
;
1736 if (!lxc_list_empty(&conf
->limits
)) {
1737 ret
= setup_resource_limits(&conf
->limits
, handler
->pid
);
1739 ERROR("Failed to setup resource limits");
1740 goto out_delete_net
;
1744 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
);
1746 goto out_delete_net
;
1748 if (!cgroup_ops
->setup_limits(cgroup_ops
, handler
->conf
, true)) {
1749 ERROR("Failed to setup legacy device cgroup controller limits");
1750 goto out_delete_net
;
1752 TRACE("Set up legacy device cgroup controller limits");
1754 if (handler
->ns_clone_flags
& CLONE_NEWCGROUP
) {
1755 /* Now we're ready to preserve the cgroup namespace */
1756 ret
= lxc_try_preserve_ns(handler
->pid
, "cgroup");
1758 if (ret
!= -EOPNOTSUPP
) {
1759 ERROR("%s - Failed to preserve cgroup namespace",
1761 goto out_delete_net
;
1764 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1765 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1769 ret
= snprintf(pidstr
, 20, "%d", handler
->pid
);
1770 if (ret
< 0 || ret
>= 20)
1771 goto out_delete_net
;
1773 ret
= setenv("LXC_PID", pidstr
, 1);
1775 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr
);
1777 /* Run any host-side start hooks */
1778 ret
= run_lxc_hooks(name
, "start-host", conf
, NULL
);
1780 ERROR("Failed to run lxc.hook.start-host");
1781 goto out_delete_net
;
1784 /* Tell the child to complete its initialization and wait for it to exec
1785 * or return an error. (The child will never return
1786 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1787 * lxc_sync_barrier_child to return success, or return a different
1788 * value, causing us to error out).
1790 ret
= lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
);
1792 goto out_delete_net
;
1794 ret
= lxc_network_recv_name_and_ifindex_from_child(handler
);
1796 ERROR("Failed to receive names and ifindices for network "
1797 "devices from child");
1798 goto out_delete_net
;
1801 /* Now all networks are created, network devices are moved into place,
1802 * and the correct names and ifindeces in the respective namespaces have
1803 * been recorded. The corresponding structs have now all been filled. So
1804 * log them for debugging purposes.
1806 lxc_log_configured_netdevs(conf
);
1808 /* Read tty fds allocated by child. */
1809 ret
= lxc_recv_ttys_from_child(handler
);
1811 ERROR("Failed to receive tty info from child process");
1812 goto out_delete_net
;
1815 ret
= handler
->ops
->post_start(handler
, handler
->data
);
1819 ret
= lxc_set_state(name
, handler
, RUNNING
);
1821 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING
));
1825 lxc_sync_fini(handler
);
1830 if (handler
->ns_clone_flags
& CLONE_NEWNET
)
1831 lxc_delete_network(handler
);
1834 lxc_abort(name
, handler
);
1835 lxc_sync_fini(handler
);
1836 if (handler
->pinfd
>= 0) {
1837 close(handler
->pinfd
);
1838 handler
->pinfd
= -1;
1844 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1845 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1846 bool backgrounded
, int *error_num
)
1849 struct lxc_conf
*conf
= handler
->conf
;
1851 ret
= lxc_init(name
, handler
);
1853 ERROR("Failed to initialize container \"%s\"", name
);
1857 handler
->data
= data
;
1858 handler
->backgrounded
= backgrounded
;
1860 if (!attach_block_device(handler
->conf
)) {
1861 ERROR("Failed to attach block device");
1862 goto out_fini_nonet
;
1865 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1866 /* If the backing store is a device, mount it here and now. */
1867 if (rootfs_is_blockdev(conf
)) {
1868 ret
= unshare(CLONE_NEWNS
);
1870 ERROR("Failed to unshare CLONE_NEWNS");
1871 goto out_fini_nonet
;
1873 INFO("Unshared CLONE_NEWNS");
1875 remount_all_slave();
1876 ret
= do_rootfs_setup(conf
, name
, lxcpath
);
1878 ERROR("Error setting up rootfs mount as root before spawn");
1879 goto out_fini_nonet
;
1881 INFO("Set up container rootfs as host root");
1885 ret
= lxc_spawn(handler
);
1887 ERROR("Failed to spawn container \"%s\"", name
);
1888 goto out_detach_blockdev
;
1890 /* close parent side of data socket */
1891 close(handler
->data_sock
[0]);
1892 handler
->data_sock
[0] = -1;
1893 close(handler
->data_sock
[1]);
1894 handler
->data_sock
[1] = -1;
1896 handler
->conf
->reboot
= REBOOT_NONE
;
1898 ret
= lxc_poll(name
, handler
);
1900 ERROR("LXC mainloop exited with error: %d", ret
);
1904 if (!handler
->init_died
&& handler
->pid
> 0) {
1905 ERROR("Child process is not killed");
1909 status
= lxc_wait_for_pid_status(handler
->pid
);
1911 SYSERROR("Failed to retrieve status for %d", handler
->pid
);
1913 /* If the child process exited but was not signaled, it didn't call
1914 * reboot. This should mean it was an lxc-execute which simply exited.
1915 * In any case, treat it as a 'halt'.
1917 if (WIFSIGNALED(status
)) {
1918 switch(WTERMSIG(status
)) {
1919 case SIGINT
: /* halt */
1920 DEBUG("Container \"%s\" is halting", name
);
1922 case SIGHUP
: /* reboot */
1923 DEBUG("Container \"%s\" is rebooting", name
);
1924 handler
->conf
->reboot
= REBOOT_REQ
;
1926 case SIGSYS
: /* seccomp */
1927 DEBUG("Container \"%s\" violated its seccomp policy", name
);
1930 DEBUG("Unknown exit status for container \"%s\" init %d", name
, WTERMSIG(status
));
1935 ret
= lxc_restore_phys_nics_to_netns(handler
);
1937 ERROR("Failed to move physical network devices back to parent "
1938 "network namespace");
1940 if (handler
->pinfd
>= 0) {
1941 close(handler
->pinfd
);
1942 handler
->pinfd
= -1;
1945 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1946 lxc_error_set_and_log(handler
->pid
, status
);
1948 *error_num
= handler
->exit_status
;
1951 lxc_delete_network(handler
);
1953 out_detach_blockdev
:
1954 detach_block_device(handler
->conf
);
1957 lxc_fini(name
, handler
);
1961 lxc_abort(name
, handler
);
1969 static int start(struct lxc_handler
*handler
, void* data
)
1971 struct start_args
*arg
= data
;
1973 NOTICE("Exec'ing \"%s\"", arg
->argv
[0]);
1975 execvp(arg
->argv
[0], arg
->argv
);
1976 SYSERROR("Failed to exec \"%s\"", arg
->argv
[0]);
1980 static int post_start(struct lxc_handler
*handler
, void* data
)
1982 struct start_args
*arg
= data
;
1984 NOTICE("Started \"%s\" with pid \"%d\"", arg
->argv
[0], handler
->pid
);
1988 static struct lxc_operations start_ops
= {
1990 .post_start
= post_start
1993 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
1994 const char *lxcpath
, bool backgrounded
, int *error_num
)
1996 struct start_args start_arg
= {
2000 TRACE("Doing lxc_start");
2001 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
, error_num
);
2004 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
2007 char destroy
[MAXPATHLEN
];
2008 struct lxc_container
*c
;
2012 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
2013 bret
= do_destroy_container(handler
);
2015 ERROR("Error destroying rootfs for container \"%s\"", name
);
2019 INFO("Destroyed rootfs for container \"%s\"", name
);
2021 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
2022 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2023 ERROR("Error destroying directory for container \"%s\"", name
);
2027 c
= lxc_container_new(name
, handler
->lxcpath
);
2029 if (container_disk_lock(c
)) {
2030 INFO("Could not update lxc_snapshots file");
2031 lxc_container_put(c
);
2033 mod_all_rdeps(c
, false);
2034 container_disk_unlock(c
);
2035 lxc_container_put(c
);
2039 if (!handler
->am_root
)
2040 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
2041 destroy
, "lxc_rmdir_onedev_wrapper");
2043 ret
= lxc_rmdir_onedev(destroy
, NULL
);
2046 ERROR("Error destroying directory for container \"%s\"", name
);
2049 INFO("Destroyed directory for container \"%s\"", name
);
2052 static int lxc_rmdir_onedev_wrapper(void *data
)
2054 char *arg
= (char *) data
;
2055 return lxc_rmdir_onedev(arg
, NULL
);
2058 static bool do_destroy_container(struct lxc_handler
*handler
)
2062 if (!handler
->am_root
) {
2063 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
2064 handler
->conf
, "storage_destroy_wrapper");
2071 return storage_destroy(handler
->conf
);