]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Serge Hallyn <serge@hallyn.com>
9 * Christian Brauner <christian.brauner@ubuntu.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
41 #include <sys/mount.h>
42 #include <sys/param.h>
43 #include <sys/prctl.h>
44 #include <sys/socket.h>
46 #include <sys/syscall.h>
47 #include <sys/types.h>
52 #include <sys/capability.h>
55 #if !HAVE_DECL_PR_CAPBSET_DROP
56 #define PR_CAPBSET_DROP 24
59 #if !HAVE_DECL_PR_SET_NO_NEW_PRIVS
60 #define PR_SET_NO_NEW_PRIVS 38
63 #if !HAVE_DECL_PR_GET_NO_NEW_PRIVS
64 #define PR_GET_NO_NEW_PRIVS 39
71 #include "commands_utils.h"
73 #include "confile_utils.h"
78 #include "lxccontainer.h"
80 #include "lxcseccomp.h"
83 #include "namespace.h"
87 #include "storage_utils.h"
92 lxc_log_define(lxc_start
, lxc
);
94 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
95 static bool do_destroy_container(struct lxc_handler
*handler
);
96 static int lxc_rmdir_onedev_wrapper(void *data
);
97 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
100 static void print_top_failing_dir(const char *path
)
102 size_t len
= strlen(path
);
103 char *copy
= alloca(len
+ 1), *p
, *e
, saved
;
109 while (p
< e
&& *p
== '/')
111 while (p
< e
&& *p
!= '/')
115 if (access(copy
, X_OK
)) {
116 SYSERROR("Could not access %s. Please grant it x "
117 "access, or add an ACL for the container "
125 static void close_ns(int ns_fd
[LXC_NS_MAX
])
129 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
137 /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified
139 * Return true on success, false on failure.
141 static bool preserve_ns(int ns_fd
[LXC_NS_MAX
], int clone_flags
, pid_t pid
)
145 for (i
= 0; i
< LXC_NS_MAX
; i
++)
148 ret
= lxc_preserve_ns(pid
, "");
150 SYSERROR("Kernel does not support attaching to namespaces.");
156 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
157 if ((clone_flags
& ns_info
[i
].clone_flag
) == 0)
160 ns_fd
[i
] = lxc_preserve_ns(pid
, ns_info
[i
].proc_name
);
164 DEBUG("Preserved %s namespace via fd %d", ns_info
[i
].proc_name
, ns_fd
[i
]);
171 SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info
[i
].proc_name
);
173 SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info
[i
].proc_name
, strerror(errno
));
178 static int match_fd(int fd
)
180 return (fd
== 0 || fd
== 1 || fd
== 2);
183 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
184 int *fds_to_ignore
, size_t len_fds
)
186 struct dirent
*direntp
;
191 if (conf
&& conf
->close_all_fds
)
195 dir
= opendir("/proc/self/fd");
197 WARN("Failed to open directory: %s.", strerror(errno
));
203 while ((direntp
= readdir(dir
))) {
204 struct lxc_list
*cur
;
205 bool matched
= false;
207 if (!strcmp(direntp
->d_name
, "."))
210 if (!strcmp(direntp
->d_name
, ".."))
213 if (lxc_safe_int(direntp
->d_name
, &fd
) < 0) {
214 INFO("Could not parse file descriptor for: %s", direntp
->d_name
);
218 for (i
= 0; i
< len_fds
; i
++)
219 if (fds_to_ignore
[i
] == fd
)
222 if (fd
== fddir
|| fd
== lxc_log_fd
||
223 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
226 /* Keep state clients that wait on reboots. */
228 lxc_list_for_each(cur
, &conf
->state_clients
) {
229 struct lxc_state_client
*client
= cur
->elem
;
231 if (client
->clientfd
!= fd
)
242 if (current_config
&& fd
== current_config
->logfd
)
251 INFO("Closed inherited fd %d", fd
);
254 WARN("Inherited fd %d", fd
);
257 /* Only enable syslog at this point to avoid the above logging function
258 * to open a new fd and make the check_inherited function enter an
261 lxc_log_enable_syslog();
263 closedir(dir
); /* cannot fail */
267 static int setup_signal_fd(sigset_t
*oldmask
)
271 int signals
[] = {SIGBUS
, SIGILL
, SIGSEGV
, SIGWINCH
};
273 /* Block everything except serious error signals. */
274 ret
= sigfillset(&mask
);
278 for (sig
= 0; sig
< (sizeof(signals
) / sizeof(signals
[0])); sig
++) {
279 ret
= sigdelset(&mask
, signals
[sig
]);
284 ret
= sigprocmask(SIG_BLOCK
, &mask
, oldmask
);
286 SYSERROR("Failed to set signal mask");
290 ret
= signalfd(-1, &mask
, SFD_CLOEXEC
);
292 SYSERROR("Failed to create signal file descriptor");
296 TRACE("Created signal file descriptor %d", ret
);
301 static int signal_handler(int fd
, uint32_t events
, void *data
,
302 struct lxc_epoll_descr
*descr
)
306 struct signalfd_siginfo siginfo
;
307 struct lxc_handler
*hdlr
= data
;
309 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
311 ERROR("Failed to read signal info from signal file descriptor: %d.", fd
);
315 if (ret
!= sizeof(siginfo
)) {
316 ERROR("Unexpected size for siginfo struct.");
320 /* Check whether init is running. */
322 ret
= waitid(P_PID
, hdlr
->pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
323 if (ret
== 0 && info
.si_pid
== hdlr
->pid
)
324 hdlr
->init_died
= true;
326 if (siginfo
.ssi_signo
!= SIGCHLD
) {
327 kill(hdlr
->pid
, siginfo
.ssi_signo
);
328 INFO("Forwarded signal %d to pid %d.", siginfo
.ssi_signo
, hdlr
->pid
);
329 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
332 if (siginfo
.ssi_code
== CLD_STOPPED
) {
333 INFO("Container init process was stopped.");
334 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
335 } else if (siginfo
.ssi_code
== CLD_CONTINUED
) {
336 INFO("Container init process was continued.");
337 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
340 /* More robustness, protect ourself from a SIGCHLD sent
341 * by a process different from the container init.
343 if (siginfo
.ssi_pid
!= hdlr
->pid
) {
344 NOTICE("Received SIGCHLD from pid %d instead of container init %d.", siginfo
.ssi_pid
, hdlr
->pid
);
345 return hdlr
->init_died
? LXC_MAINLOOP_CLOSE
: 0;
348 DEBUG("Container init process %d exited.", hdlr
->pid
);
349 return LXC_MAINLOOP_CLOSE
;
352 int lxc_serve_state_clients(const char *name
, struct lxc_handler
*handler
,
356 struct lxc_list
*cur
, *next
;
357 struct lxc_state_client
*client
;
358 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
362 handler
->state
= RUNNING
;
364 handler
->state
= state
;
366 TRACE("Set container state to %s", lxc_state2str(state
));
368 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
369 TRACE("No state clients registered");
371 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
375 strncpy(msg
.name
, name
, sizeof(msg
.name
));
376 msg
.name
[sizeof(msg
.name
) - 1] = 0;
378 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
381 if (client
->states
[state
] == 0) {
382 TRACE("State %s not registered for state client %d",
383 lxc_state2str(state
), client
->clientfd
);
387 TRACE("Sending state %s to state client %d",
388 lxc_state2str(state
), client
->clientfd
);
391 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), 0);
393 if (errno
== EINTR
) {
394 TRACE("Caught EINTR; retrying");
398 ERROR("%s - Failed to send message to client",
402 /* kick client from list */
403 close(client
->clientfd
);
413 static int lxc_serve_state_socket_pair(const char *name
,
414 struct lxc_handler
*handler
,
419 if (!handler
->backgrounded
||
420 handler
->state_socket_pair
[1] < 0 ||
424 /* Close read end of the socket pair. */
425 close(handler
->state_socket_pair
[0]);
426 handler
->state_socket_pair
[0] = -1;
429 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
430 &(int){state
}, sizeof(int));
431 if (ret
!= sizeof(int)) {
434 SYSERROR("Failed to send state to %d",
435 handler
->state_socket_pair
[1]);
439 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
440 handler
->state_socket_pair
[1]);
442 /* Close write end of the socket pair. */
443 close(handler
->state_socket_pair
[1]);
444 handler
->state_socket_pair
[1] = -1;
449 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
454 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
456 ERROR("Failed to synchronize via anonymous pair of unix sockets");
460 ret
= lxc_serve_state_clients(name
, handler
, state
);
464 /* This function will try to connect to the legacy lxc-monitord state
465 * server and only exists for backwards compatibility.
467 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
472 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
475 bool has_console
= true;
476 struct lxc_epoll_descr descr
, descr_console
;
478 if (handler
->conf
->console
.path
&& !strcmp(handler
->conf
->console
.path
, "none"))
481 ret
= lxc_mainloop_open(&descr
);
483 ERROR("Failed to create mainloop");
488 ret
= lxc_mainloop_open(&descr_console
);
490 ERROR("Failed to create console mainloop");
495 ret
= lxc_mainloop_add_handler(&descr
, handler
->sigfd
, signal_handler
, handler
);
497 ERROR("Failed to add signal handler for %d to mainloop", handler
->sigfd
);
498 goto out_mainloop_console
;
502 struct lxc_console
*console
= &handler
->conf
->console
;
504 ret
= lxc_console_mainloop_add(&descr
, console
);
506 ERROR("Failed to add console handlers to mainloop");
507 goto out_mainloop_console
;
510 ret
= lxc_console_mainloop_add(&descr_console
, console
);
512 ERROR("Failed to add console handlers to console mainloop");
513 goto out_mainloop_console
;
517 ret
= lxc_cmd_mainloop_add(name
, &descr
, handler
);
519 ERROR("Failed to add command handler to mainloop");
520 goto out_mainloop_console
;
523 TRACE("Mainloop is ready");
525 ret
= lxc_mainloop(&descr
, -1);
528 if (ret
< 0 || !handler
->init_died
)
532 ret
= lxc_mainloop(&descr_console
, 0);
536 lxc_mainloop_close(&descr
);
537 TRACE("Closed mainloop");
539 out_mainloop_console
:
541 lxc_mainloop_close(&descr_console
);
542 TRACE("Closed console mainloop");
546 close(handler
->sigfd
);
547 TRACE("Closed signal file descriptor %d", handler
->sigfd
);
548 handler
->sigfd
= -EBADF
;
553 void lxc_zero_handler(struct lxc_handler
*handler
)
557 memset(handler
, 0, sizeof(struct lxc_handler
));
559 handler
->clone_flags
= -1;
565 for (i
= 0; i
< LXC_NS_MAX
; i
++)
566 handler
->nsfd
[i
] = -1;
568 handler
->data_sock
[0] = -1;
569 handler
->data_sock
[1] = -1;
571 handler
->state_socket_pair
[0] = -1;
572 handler
->state_socket_pair
[1] = -1;
574 handler
->sync_sock
[0] = -1;
575 handler
->sync_sock
[1] = -1;
578 static void lxc_put_nsfds(struct lxc_handler
*handler
)
582 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
583 if (handler
->nsfd
[i
] < 0)
586 close(handler
->nsfd
[i
]);
587 handler
->nsfd
[i
] = -EBADF
;
591 void lxc_free_handler(struct lxc_handler
*handler
)
593 if (handler
->pinfd
>= 0)
594 close(handler
->pinfd
);
596 if (handler
->sigfd
>= 0)
597 close(handler
->sigfd
);
599 lxc_put_nsfds(handler
);
601 if (handler
->conf
&& handler
->conf
->reboot
== 0)
602 if (handler
->conf
->maincmd_fd
)
603 close(handler
->conf
->maincmd_fd
);
605 if (handler
->state_socket_pair
[0] >= 0)
606 close(handler
->state_socket_pair
[0]);
608 if (handler
->state_socket_pair
[1] >= 0)
609 close(handler
->state_socket_pair
[1]);
611 handler
->conf
= NULL
;
616 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
617 const char *lxcpath
, bool daemonize
)
620 struct lxc_handler
*handler
;
622 handler
= malloc(sizeof(*handler
));
624 ERROR("failed to allocate memory");
628 memset(handler
, 0, sizeof(*handler
));
630 /* Note that am_unpriv() checks the effective uid. We probably don't
631 * care if we are real root only if we are running as root so this
634 handler
->am_root
= !am_unpriv();
635 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
636 handler
->conf
= conf
;
637 handler
->lxcpath
= lxcpath
;
639 handler
->sigfd
= -EBADF
;
640 handler
->init_died
= false;
641 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
642 if (handler
->conf
->reboot
== 0)
643 lxc_list_init(&handler
->conf
->state_clients
);
645 for (i
= 0; i
< LXC_NS_MAX
; i
++)
646 handler
->nsfd
[i
] = -1;
648 handler
->name
= name
;
650 if (daemonize
&& !handler
->conf
->reboot
) {
651 /* Create socketpair() to synchronize on daemonized startup.
652 * When the container reboots we don't need to synchronize again
653 * currently so don't open another socketpair().
655 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
656 handler
->state_socket_pair
);
658 ERROR("Failed to create anonymous pair of unix sockets");
661 TRACE("Created anonymous pair {%d,%d} of unix sockets",
662 handler
->state_socket_pair
[0],
663 handler
->state_socket_pair
[1]);
666 if (handler
->conf
->reboot
== 0) {
667 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
668 if (handler
->conf
->maincmd_fd
< 0) {
669 ERROR("Failed to set up command socket");
673 TRACE("Unix domain socket %d for command server is ready",
674 handler
->conf
->maincmd_fd
);
679 lxc_free_handler(handler
);
684 int lxc_init(const char *name
, struct lxc_handler
*handler
)
687 const char *loglevel
;
688 struct lxc_conf
*conf
= handler
->conf
;
691 TRACE("initialized LSM");
693 if (lxc_read_seccomp_config(conf
) != 0) {
694 ERROR("Failed loading seccomp policy.");
695 goto out_close_maincmd_fd
;
697 TRACE("read seccomp policy");
699 /* Begin by setting the state to STARTING. */
700 if (lxc_set_state(name
, handler
, STARTING
)) {
701 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
, lxc_state2str(STARTING
));
702 goto out_close_maincmd_fd
;
704 TRACE("set container state to \"STARTING\"");
706 /* Start of environment variable setup for hooks. */
707 if (name
&& setenv("LXC_NAME", name
, 1))
708 SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name
);
710 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
711 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf
->rcfile
);
713 if (conf
->rootfs
.mount
&& setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1))
714 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf
->rootfs
.mount
);
716 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
717 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf
->rootfs
.path
);
719 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1))
720 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf
->console
.path
);
722 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1))
723 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf
->console
.log_path
);
725 if (setenv("LXC_CGNS_AWARE", "1", 1))
726 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1.");
728 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
729 if (setenv("LXC_LOG_LEVEL", loglevel
, 1))
730 SYSERROR("Failed to set environment variable LXC_LOG_LEVEL=%s", loglevel
);
732 if (conf
->hooks_version
== 0)
733 ret
= setenv("LXC_HOOK_VERSION", "0", 1);
735 ret
= setenv("LXC_HOOK_VERSION", "1", 1);
737 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf
->hooks_version
);
738 /* End of environment variable setup for hooks. */
740 TRACE("set environment variables");
742 if (run_lxc_hooks(name
, "pre-start", conf
, NULL
)) {
743 ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name
);
746 TRACE("ran pre-start hooks");
748 /* The signal fd has to be created before forking otherwise if the child
749 * process exits before we setup the signal fd, the event will be lost
750 * and the command will be stuck.
752 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
753 if (handler
->sigfd
< 0) {
754 ERROR("Failed to setup SIGCHLD fd handler.");
757 TRACE("set up signal fd");
759 /* Do this after setting up signals since it might unblock SIGWINCH. */
760 ret
= lxc_console_create(conf
);
762 ERROR("Failed to create console");
763 goto out_restore_sigmask
;
765 TRACE("Created console");
767 ret
= lxc_pty_map_ids(conf
, &conf
->console
);
769 ERROR("Failed to shift tty into container.");
770 goto out_restore_sigmask
;
772 TRACE("shifted tty ids");
774 INFO("container \"%s\" is initialized", name
);
778 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
780 lxc_delete_tty(&conf
->tty_info
);
782 lxc_set_state(name
, handler
, ABORTING
);
783 out_close_maincmd_fd
:
784 close(conf
->maincmd_fd
);
785 conf
->maincmd_fd
= -1;
789 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
793 struct lxc_list
*cur
, *next
;
794 char *namespaces
[LXC_NS_MAX
+ 1];
795 size_t namespace_count
= 0;
797 /* The STOPPING state is there for future cleanup code which can take
800 lxc_set_state(name
, handler
, STOPPING
);
802 self
= lxc_raw_getpid();
803 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
804 if (handler
->nsfd
[i
] < 0)
807 if (handler
->conf
->hooks_version
== 0)
808 rc
= asprintf(&namespaces
[namespace_count
],
809 "%s:/proc/%d/fd/%d", ns_info
[i
].proc_name
,
810 self
, handler
->nsfd
[i
]);
812 rc
= asprintf(&namespaces
[namespace_count
],
813 "/proc/%d/fd/%d", self
, handler
->nsfd
[i
]);
815 SYSERROR("Failed to allocate memory.");
819 if (handler
->conf
->hooks_version
== 0) {
824 rc
= setenv(ns_info
[i
].env_name
, namespaces
[namespace_count
], 1);
826 SYSERROR("Failed to set environment variable %s=%s",
827 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
829 TRACE("Set environment variable %s=%s",
830 ns_info
[i
].env_name
, namespaces
[namespace_count
]);
834 namespaces
[namespace_count
] = NULL
;
836 if (handler
->conf
->reboot
&& setenv("LXC_TARGET", "reboot", 1))
837 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot.");
839 if (!handler
->conf
->reboot
&& setenv("LXC_TARGET", "stop", 1))
840 SYSERROR("Failed to set environment variable: LXC_TARGET=stop.");
842 if (handler
->conf
->hooks_version
== 0)
843 rc
= run_lxc_hooks(name
, "stop", handler
->conf
, namespaces
);
845 rc
= run_lxc_hooks(name
, "stop", handler
->conf
, NULL
);
847 while (namespace_count
--)
848 free(namespaces
[namespace_count
]);
850 cgroup_destroy(handler
);
852 if (handler
->conf
->reboot
== 0) {
853 /* For all new state clients simply close the command socket.
854 * This will inform all state clients that the container is
855 * STOPPED and also prevents a race between a open()/close() on
856 * the command socket causing a new process to get ECONNREFUSED
857 * because we haven't yet closed the command socket.
859 close(handler
->conf
->maincmd_fd
);
860 handler
->conf
->maincmd_fd
= -1;
861 TRACE("Closed command socket");
863 /* This function will try to connect to the legacy lxc-monitord
864 * state server and only exists for backwards compatibility.
866 lxc_monitor_send_state(name
, STOPPED
, handler
->lxcpath
);
868 /* The command socket is closed so no one can acces the command
869 * socket anymore so there's no need to lock it.
871 handler
->state
= STOPPED
;
872 TRACE("Set container state to \"STOPPED\"");
874 lxc_set_state(name
, handler
, STOPPED
);
877 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, NULL
)) {
878 ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name
);
879 if (handler
->conf
->reboot
) {
880 WARN("Container will be stopped instead of rebooted.");
881 handler
->conf
->reboot
= 0;
882 if (setenv("LXC_TARGET", "stop", 1))
883 WARN("Failed to set environment variable: LXC_TARGET=stop.");
887 /* Reset mask set by setup_signal_fd. */
888 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
889 WARN("Failed to restore signal mask.");
891 lxc_console_delete(&handler
->conf
->console
);
892 lxc_delete_tty(&handler
->conf
->tty_info
);
894 /* The command socket is now closed, no more state clients can register
895 * themselves from now on. So free the list of state clients.
897 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
898 struct lxc_state_client
*client
= cur
->elem
;
900 /* Keep state clients that want to be notified about reboots. */
901 if ((handler
->conf
->reboot
> 0) && (client
->states
[RUNNING
] == 2))
904 /* close state client socket */
905 close(client
->clientfd
);
911 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= 1)
912 lxc_destroy_container_on_signal(handler
, name
);
914 lxc_free_handler(handler
);
917 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
921 lxc_set_state(name
, handler
, ABORTING
);
922 if (handler
->pid
> 0)
923 kill(handler
->pid
, SIGKILL
);
924 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
929 static int lxc_set_death_signal(int signal
)
934 ret
= prctl(PR_SET_PDEATHSIG
, signal
, 0, 0, 0);
936 /* Check whether we have been orphaned. */
937 ppid
= (pid_t
)syscall(SYS_getppid
);
941 self
= lxc_raw_getpid();
942 ret
= kill(self
, SIGKILL
);
948 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal
);
955 static int do_start(void *data
)
958 struct lxc_list
*iterator
;
960 struct lxc_handler
*handler
= data
;
961 bool have_cap_setgid
;
966 lxc_sync_fini_parent(handler
);
968 /* This prctl must be before the synchro, so if the parent dies before
969 * we set the parent death signal, we will detect its death with the
970 * synchro right after, otherwise we have a window where the parent can
971 * exit before we set the pdeath signal leading to a unsupervized
974 ret
= lxc_set_death_signal(SIGKILL
);
976 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
977 goto out_warn_father
;
980 ret
= sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
982 SYSERROR("Failed to set signal mask");
983 goto out_warn_father
;
986 /* Don't leak the pinfd to the container. */
987 if (handler
->pinfd
>= 0)
988 close(handler
->pinfd
);
990 ret
= lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
);
992 goto out_warn_father
;
994 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
995 * https://github.com/lxc/lxd/issues/1978.
997 if ((handler
->clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
998 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
999 ret
= unshare(CLONE_NEWNET
);
1001 SYSERROR("Failed to unshare CLONE_NEWNET.");
1002 goto out_warn_father
;
1004 INFO("Unshared CLONE_NEWNET.");
1007 /* Tell the parent task it can begin to configure the container and wait
1010 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
1013 if (lxc_network_recv_veth_names_from_parent(handler
) < 0) {
1014 ERROR("Failed to receive veth names from parent");
1015 goto out_warn_father
;
1018 /* If we are in a new user namespace, become root there to have
1019 * privilege over our namespace.
1021 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
1022 uid_t nsuid
= (handler
->conf
->root_nsuid_map
!= NULL
)
1024 : handler
->conf
->init_uid
;
1025 gid_t nsgid
= (handler
->conf
->root_nsgid_map
!= NULL
)
1027 : handler
->conf
->init_gid
;
1029 ret
= lxc_switch_uid_gid(nsuid
, nsgid
);
1031 goto out_warn_father
;
1033 /* Drop groups only after we switched to a valid gid in the new
1036 ret
= lxc_setgroups(0, NULL
);
1037 if (ret
< 0 && (handler
->am_root
|| errno
!= EPERM
))
1038 goto out_warn_father
;
1040 if (!handler
->am_root
) {
1041 ret
= prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
1043 goto out_warn_father
;
1046 /* set{g,u}id() clears deathsignal */
1047 ret
= lxc_set_death_signal(SIGKILL
);
1049 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1050 goto out_warn_father
;
1054 if (access(handler
->lxcpath
, X_OK
)) {
1055 print_top_failing_dir(handler
->lxcpath
);
1056 goto out_warn_father
;
1059 ret
= snprintf(path
, sizeof(path
), "%s/dev/null", handler
->conf
->rootfs
.mount
);
1060 if (ret
< 0 || ret
>= sizeof(path
))
1061 goto out_warn_father
;
1063 /* In order to checkpoint restore, we need to have everything in the
1064 * same mount namespace. However, some containers may not have a
1065 * reasonable /dev (in particular, they may not have /dev/null), so we
1066 * can't set init's std fds to /dev/null by opening it from inside the
1069 * If that's the case, fall back to using the host's /dev/null. This
1070 * means that migration won't work, but at least we won't spew output
1071 * where it isn't wanted.
1073 if (handler
->backgrounded
&& !handler
->conf
->autodev
&& access(path
, F_OK
) < 0) {
1074 devnull_fd
= open_devnull();
1077 goto out_warn_father
;
1078 WARN("Using /dev/null from the host for container init's "
1079 "standard file descriptors. Migration will not work.");
1082 /* Ask father to setup cgroups and wait for him to finish. */
1083 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
1086 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1087 * earlier we end up with a wrong view of /proc/self/cgroup. For
1088 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1089 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1090 * /proc/self/cgroup would show us:
1094 * whereas it should actually show
1098 if (handler
->clone_flags
& CLONE_NEWCGROUP
) {
1099 if (unshare(CLONE_NEWCGROUP
) < 0) {
1100 INFO("Failed to unshare CLONE_NEWCGROUP.");
1101 goto out_warn_father
;
1103 INFO("Unshared CLONE_NEWCGROUP.");
1106 /* Add the requested environment variables to the current environment to
1107 * allow them to be used by the various hooks, such as the start hook
1110 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1111 if (putenv((char *)iterator
->elem
)) {
1112 SYSERROR("Failed to set environment variable: %s.", (char *)iterator
->elem
);
1113 goto out_warn_father
;
1117 /* Setup the container, ip, names, utsname, ... */
1118 ret
= lxc_setup(handler
);
1119 close(handler
->data_sock
[1]);
1120 close(handler
->data_sock
[0]);
1122 ERROR("Failed to setup container \"%s\".", handler
->name
);
1123 goto out_warn_father
;
1126 /* Set the label to change to when we exec(2) the container's init. */
1127 if (lsm_process_label_set(NULL
, handler
->conf
, 1, 1) < 0)
1128 goto out_warn_father
;
1130 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1131 * before we aren't allowed anymore.
1133 if (handler
->conf
->no_new_privs
) {
1134 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) < 0) {
1135 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
1136 goto out_warn_father
;
1138 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
1141 /* Some init's such as busybox will set sane tty settings on stdin,
1142 * stdout, stderr which it thinks is the console. We already set them
1143 * the way we wanted on the real terminal, and we want init to do its
1144 * setup on its console ie. the pty allocated in lxc_console_create() so
1145 * make sure that that pty is stdin,stdout,stderr.
1147 if (handler
->conf
->console
.slave
>= 0) {
1148 if (handler
->backgrounded
|| handler
->conf
->is_execute
== 0)
1149 ret
= set_stdfds(handler
->conf
->console
.slave
);
1151 ret
= lxc_console_set_stdfds(handler
->conf
->console
.slave
);
1153 ERROR("Failed to redirect std{in,out,err} to pty file "
1155 handler
->conf
->console
.slave
);
1156 goto out_warn_father
;
1160 /* If we mounted a temporary proc, then unmount it now. */
1161 tmp_proc_unmount(handler
->conf
);
1163 if (lxc_seccomp_load(handler
->conf
) != 0)
1164 goto out_warn_father
;
1166 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, NULL
)) {
1167 ERROR("Failed to run lxc.hook.start for container \"%s\".", handler
->name
);
1168 goto out_warn_father
;
1171 close(handler
->sigfd
);
1173 if (devnull_fd
< 0) {
1174 devnull_fd
= open_devnull();
1177 goto out_warn_father
;
1180 if (handler
->conf
->console
.slave
< 0 && handler
->backgrounded
)
1181 if (set_stdfds(devnull_fd
) < 0) {
1182 ERROR("Failed to redirect std{in,out,err} to "
1184 goto out_warn_father
;
1187 if (devnull_fd
>= 0) {
1194 if (handler
->conf
->init_cwd
&& chdir(handler
->conf
->init_cwd
)) {
1195 SYSERROR("Could not change directory to \"%s\"", handler
->conf
->init_cwd
);
1196 goto out_warn_father
;
1199 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
))
1200 goto out_warn_father
;
1202 /* Reset the environment variables the user requested in a clear
1206 SYSERROR("Failed to clear environment.");
1207 /* Don't error out though. */
1210 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1211 if (putenv((char *)iterator
->elem
)) {
1212 SYSERROR("Failed to set environment variable: %s.", (char *)iterator
->elem
);
1213 goto out_warn_father
;
1217 if (putenv("container=lxc")) {
1218 SYSERROR("Failed to set environment variable: container=lxc.");
1219 goto out_warn_father
;
1222 if (handler
->conf
->pty_names
) {
1223 if (putenv(handler
->conf
->pty_names
)) {
1224 SYSERROR("Failed to set environment variable for container ptys.");
1225 goto out_warn_father
;
1229 /* The container has been setup. We can now switch to an unprivileged
1232 new_uid
= handler
->conf
->init_uid
;
1233 new_gid
= handler
->conf
->init_gid
;
1235 /* If we are in a new user namespace we already dropped all
1236 * groups when we switched to root in the new user namespace
1237 * further above. Only drop groups if we can, so ensure that we
1238 * have necessary privilege.
1241 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1243 have_cap_setgid
= false;
1245 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1246 if (lxc_setgroups(0, NULL
) < 0)
1247 goto out_warn_father
;
1250 if (lxc_switch_uid_gid(new_uid
, new_gid
) < 0)
1251 goto out_warn_father
;
1253 /* After this call, we are in error because this ops should not return
1256 handler
->ops
->start(handler
, handler
->data
);
1259 /* We want the parent to know something went wrong, so we return a
1260 * special error code.
1262 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1265 if (devnull_fd
>= 0)
1271 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1274 struct lxc_pty_info
*pty_info
;
1276 int sock
= handler
->data_sock
[1];
1277 struct lxc_conf
*conf
= handler
->conf
;
1278 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
1283 tty_info
->pty_info
= malloc(sizeof(*tty_info
->pty_info
) * conf
->tty
);
1284 if (!tty_info
->pty_info
)
1287 for (i
= 0; i
< conf
->tty
; i
++) {
1290 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1294 pty_info
= &tty_info
->pty_info
[i
];
1296 pty_info
->master
= ttyfds
[0];
1297 pty_info
->slave
= ttyfds
[1];
1298 TRACE("Received pty with master fd %d and slave fd %d from "
1299 "parent", pty_info
->master
, pty_info
->slave
);
1302 ERROR("Failed to receive %d ttys from child: %s", conf
->tty
,
1305 TRACE("Received %d ttys from child", conf
->tty
);
1307 tty_info
->nbtty
= conf
->tty
;
1312 int resolve_clone_flags(struct lxc_handler
*handler
)
1314 handler
->clone_flags
= CLONE_NEWNS
;
1316 if (!handler
->conf
->inherit_ns
[LXC_NS_USER
]) {
1317 if (!lxc_list_empty(&handler
->conf
->id_map
))
1318 handler
->clone_flags
|= CLONE_NEWUSER
;
1320 INFO("Inheriting user namespace");
1323 if (!handler
->conf
->inherit_ns
[LXC_NS_NET
]) {
1324 if (!lxc_requests_empty_network(handler
))
1325 handler
->clone_flags
|= CLONE_NEWNET
;
1327 INFO("Inheriting net namespace");
1330 if (!handler
->conf
->inherit_ns
[LXC_NS_IPC
])
1331 handler
->clone_flags
|= CLONE_NEWIPC
;
1333 INFO("Inheriting ipc namespace");
1335 if (!handler
->conf
->inherit_ns
[LXC_NS_UTS
])
1336 handler
->clone_flags
|= CLONE_NEWUTS
;
1338 INFO("Inheriting uts namespace");
1340 if (!handler
->conf
->inherit_ns
[LXC_NS_PID
])
1341 handler
->clone_flags
|= CLONE_NEWPID
;
1343 INFO("Inheriting pid namespace");
1345 if (cgns_supported()) {
1346 if (!handler
->conf
->inherit_ns
[LXC_NS_CGROUP
])
1347 handler
->clone_flags
|= CLONE_NEWCGROUP
;
1349 INFO("Inheriting cgroup namespace");
1350 } else if (handler
->conf
->inherit_ns
[LXC_NS_CGROUP
]) {
1357 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1358 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1359 * But since the memory between parent and child is shared on CLONE_VM this
1360 * would invalidate the getpid() cache that glibc used to maintain and so
1361 * getpid() in the child would return the parent's pid. This is all fixed in
1362 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1363 * not reset anymore.
1364 * However, if for whatever reason you - dear commiter - somehow need to get the
1365 * pid of the dummy intermediate process for do_share_ns() you need to call
1366 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1369 static inline int do_share_ns(void *arg
)
1372 struct lxc_handler
*handler
= arg
;
1374 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1375 if (handler
->nsfd
[i
] < 0)
1378 ret
= setns(handler
->nsfd
[i
], 0);
1382 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1385 flags
= handler
->on_clone_flags
;
1386 flags
|= CLONE_PARENT
;
1387 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, flags
);
1388 if (handler
->pid
< 0)
1394 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1395 * exec()s the requested container binary.
1396 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1397 * right here should be double checked if they'd pose a security risk. (For
1398 * example, any {u}mount() operations performed here will be reflected on the
1401 static int lxc_spawn(struct lxc_handler
*handler
)
1405 bool wants_to_map_ids
;
1406 struct lxc_list
*id_map
;
1407 const char *name
= handler
->name
;
1408 const char *lxcpath
= handler
->lxcpath
;
1409 bool cgroups_connected
= false, share_ns
= false;
1410 struct lxc_conf
*conf
= handler
->conf
;
1412 id_map
= &conf
->id_map
;
1413 wants_to_map_ids
= !lxc_list_empty(id_map
);
1415 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1416 if (!conf
->inherit_ns
[i
])
1419 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->inherit_ns
[i
], lxcpath
, ns_info
[i
].proc_name
);
1420 if (handler
->nsfd
[i
] < 0)
1426 if (lxc_sync_init(handler
))
1429 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1430 handler
->data_sock
);
1432 lxc_sync_fini(handler
);
1436 ret
= resolve_clone_flags(handler
);
1438 lxc_sync_fini(handler
);
1442 if (handler
->clone_flags
& CLONE_NEWNET
) {
1443 if (!lxc_list_empty(&conf
->network
)) {
1445 /* Find gateway addresses from the link device, which is
1446 * no longer accessible inside the container. Do this
1447 * before creating network interfaces, since goto
1448 * out_delete_net does not work before lxc_clone.
1450 if (lxc_find_gateway_addresses(handler
)) {
1451 ERROR("Failed to find gateway addresses.");
1452 lxc_sync_fini(handler
);
1456 /* That should be done before the clone because we will
1457 * fill the netdev index and use them in the child.
1459 if (lxc_create_network_priv(handler
)) {
1460 ERROR("Failed to create the network.");
1461 lxc_sync_fini(handler
);
1467 if (!cgroup_init(handler
)) {
1468 ERROR("Failed initializing cgroup support.");
1469 goto out_delete_net
;
1472 cgroups_connected
= true;
1474 if (!cgroup_create(handler
)) {
1475 ERROR("Failed creating cgroups.");
1476 goto out_delete_net
;
1479 /* If the rootfs is not a blockdev, prevent the container from marking
1481 * If the container is unprivileged then skip rootfs pinning.
1483 if (!wants_to_map_ids
) {
1484 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1485 if (handler
->pinfd
== -1)
1486 INFO("Failed to pin the rootfs for container \"%s\".", handler
->name
);
1489 /* Create a process in a new set of namespaces. */
1490 handler
->on_clone_flags
= handler
->clone_flags
;
1491 if (handler
->clone_flags
& CLONE_NEWUSER
) {
1492 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1493 * clone a new user namespace first and only later unshare our
1494 * network namespace to ensure that network devices ownership is
1497 handler
->on_clone_flags
&= ~CLONE_NEWNET
;
1499 /* The cgroup namespace gets unshare()ed not clone()ed. */
1500 handler
->on_clone_flags
&= ~CLONE_NEWCGROUP
;
1505 attacher_pid
= lxc_clone(do_share_ns
, handler
, CLONE_VFORK
| CLONE_VM
| CLONE_FILES
);
1506 if (attacher_pid
< 0) {
1507 SYSERROR(LXC_CLONE_ERROR
);
1508 goto out_delete_net
;
1511 ret
= wait_for_pid(attacher_pid
);
1513 SYSERROR("Intermediate process failed");
1514 goto out_delete_net
;
1517 handler
->pid
= lxc_raw_clone_cb(do_start
, handler
, handler
->on_clone_flags
);
1519 if (handler
->pid
< 0) {
1520 SYSERROR(LXC_CLONE_ERROR
);
1521 goto out_delete_net
;
1523 TRACE("Cloned child process %d", handler
->pid
);
1525 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1526 if (handler
->on_clone_flags
& ns_info
[i
].clone_flag
)
1527 INFO("Cloned %s", ns_info
[i
].flag_name
);
1529 if (!preserve_ns(handler
->nsfd
, handler
->on_clone_flags
, handler
->pid
)) {
1530 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1531 goto out_delete_net
;
1534 lxc_sync_fini_child(handler
);
1536 /* Map the container uids. The container became an invalid userid the
1537 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1538 * anything immediately, but allows the container to setuid(0) (0 being
1539 * mapped to something else on the host.) later to become a valid uid
1542 if (wants_to_map_ids
) {
1543 if (!handler
->conf
->inherit_ns
[LXC_NS_USER
]) {
1544 ret
= lxc_map_ids(id_map
, handler
->pid
);
1546 ERROR("Failed to set up id mapping.");
1547 goto out_delete_net
;
1552 if (lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
))
1553 goto out_delete_net
;
1555 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
1556 goto out_delete_net
;
1558 if (!cgroup_create_legacy(handler
)) {
1559 ERROR("Failed to setup legacy cgroups for container \"%s\".", name
);
1560 goto out_delete_net
;
1562 if (!cgroup_setup_limits(handler
, false)) {
1563 ERROR("Failed to setup cgroup limits for container \"%s\".", name
);
1564 goto out_delete_net
;
1567 if (!cgroup_enter(handler
))
1568 goto out_delete_net
;
1570 if (!cgroup_chown(handler
))
1571 goto out_delete_net
;
1573 /* Now we're ready to preserve the network namespace */
1574 ret
= lxc_preserve_ns(handler
->pid
, "net");
1576 ERROR("%s - Failed to preserve net namespace", strerror(errno
));
1577 goto out_delete_net
;
1579 handler
->nsfd
[LXC_NS_NET
] = ret
;
1580 DEBUG("Preserved net namespace via fd %d", ret
);
1582 /* Create the network configuration. */
1583 if (handler
->clone_flags
& CLONE_NEWNET
) {
1584 if (lxc_network_move_created_netdev_priv(handler
->lxcpath
,
1588 ERROR("Failed to create the configured network.");
1589 goto out_delete_net
;
1592 if (lxc_create_network_unpriv(handler
->lxcpath
, handler
->name
,
1595 ERROR("Failed to create the configured network.");
1596 goto out_delete_net
;
1600 if (lxc_network_send_veth_names_to_child(handler
) < 0) {
1601 ERROR("Failed to send veth names to child");
1602 goto out_delete_net
;
1605 if (!lxc_list_empty(&conf
->procs
)) {
1606 ret
= setup_proc_filesystem(&conf
->procs
, handler
->pid
);
1608 goto out_delete_net
;
1611 /* Tell the child to continue its initialization. We'll get
1612 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1614 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
1615 goto out_delete_net
;
1617 if (!lxc_list_empty(&conf
->limits
) && setup_resource_limits(&conf
->limits
, handler
->pid
)) {
1618 ERROR("failed to setup resource limits for '%s'", name
);
1619 goto out_delete_net
;
1622 if (lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
))
1623 goto out_delete_net
;
1625 if (!cgroup_setup_limits(handler
, true)) {
1626 ERROR("Failed to setup the devices cgroup for container \"%s\".", name
);
1627 goto out_delete_net
;
1629 TRACE("Set up cgroup device limits");
1631 cgroup_disconnect();
1632 cgroups_connected
= false;
1634 if (handler
->clone_flags
& CLONE_NEWCGROUP
) {
1635 /* Now we're ready to preserve the cgroup namespace */
1636 ret
= lxc_preserve_ns(handler
->pid
, "cgroup");
1638 ERROR("%s - Failed to preserve cgroup namespace", strerror(errno
));
1639 goto out_delete_net
;
1641 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1642 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1645 snprintf(pidstr
, 20, "%d", handler
->pid
);
1646 if (setenv("LXC_PID", pidstr
, 1))
1647 SYSERROR("Failed to set environment variable: LXC_PID=%s.", pidstr
);
1649 /* Run any host-side start hooks */
1650 if (run_lxc_hooks(name
, "start-host", conf
, NULL
)) {
1651 ERROR("Failed to run lxc.hook.start-host for container \"%s\".", name
);
1655 /* Tell the child to complete its initialization and wait for it to exec
1656 * or return an error. (The child will never return
1657 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1658 * lxc_sync_barrier_child to return success, or return a different
1659 * value, causing us to error out).
1661 if (lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
))
1664 if (lxc_network_recv_name_and_ifindex_from_child(handler
) < 0) {
1665 ERROR("Failed to receive names and ifindices for network "
1666 "devices from child");
1667 goto out_delete_net
;
1670 /* Now all networks are created, network devices are moved into place,
1671 * and the correct names and ifindeces in the respective namespaces have
1672 * been recorded. The corresponding structs have now all been filled. So
1673 * log them for debugging purposes.
1675 lxc_log_configured_netdevs(conf
);
1677 /* Read tty fds allocated by child. */
1678 if (lxc_recv_ttys_from_child(handler
) < 0) {
1679 ERROR("Failed to receive tty info from child process.");
1680 goto out_delete_net
;
1683 if (handler
->ops
->post_start(handler
, handler
->data
))
1686 if (lxc_set_state(name
, handler
, RUNNING
)) {
1687 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
,
1688 lxc_state2str(RUNNING
));
1692 lxc_sync_fini(handler
);
1697 if (cgroups_connected
)
1698 cgroup_disconnect();
1700 if (handler
->clone_flags
& CLONE_NEWNET
)
1701 lxc_delete_network(handler
);
1704 lxc_abort(name
, handler
);
1705 lxc_sync_fini(handler
);
1706 if (handler
->pinfd
>= 0) {
1707 close(handler
->pinfd
);
1708 handler
->pinfd
= -1;
1714 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1715 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1720 struct lxc_conf
*conf
= handler
->conf
;
1722 if (lxc_init(name
, handler
) < 0) {
1723 ERROR("Failed to initialize container \"%s\".", name
);
1727 handler
->data
= data
;
1728 handler
->backgrounded
= backgrounded
;
1730 if (!attach_block_device(handler
->conf
)) {
1731 ERROR("Failed to attach block device.");
1732 goto out_fini_nonet
;
1735 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1736 /* If the backing store is a device, mount it here and now. */
1737 if (rootfs_is_blockdev(conf
)) {
1738 if (unshare(CLONE_NEWNS
) < 0) {
1739 ERROR("Failed to unshare CLONE_NEWNS.");
1740 goto out_fini_nonet
;
1742 INFO("Unshared CLONE_NEWNS.");
1744 remount_all_slave();
1745 if (do_rootfs_setup(conf
, name
, lxcpath
) < 0) {
1746 ERROR("Error setting up rootfs mount as root before spawn.");
1747 goto out_fini_nonet
;
1749 INFO("Set up container rootfs as host root.");
1753 err
= lxc_spawn(handler
);
1755 ERROR("Failed to spawn container \"%s\".", name
);
1756 goto out_detach_blockdev
;
1758 /* close parent side of data socket */
1759 close(handler
->data_sock
[0]);
1760 handler
->data_sock
[0] = -1;
1761 close(handler
->data_sock
[1]);
1762 handler
->data_sock
[1] = -1;
1764 handler
->conf
->reboot
= 0;
1766 err
= lxc_poll(name
, handler
);
1768 ERROR("LXC mainloop exited with error: %d.", err
);
1772 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
1775 /* If the child process exited but was not signaled, it didn't call
1776 * reboot. This should mean it was an lxc-execute which simply exited.
1777 * In any case, treat it as a 'halt'.
1779 if (WIFSIGNALED(status
)) {
1780 switch(WTERMSIG(status
)) {
1781 case SIGINT
: /* halt */
1782 DEBUG("Container \"%s\" is halting.", name
);
1784 case SIGHUP
: /* reboot */
1785 DEBUG("Container \"%s\" is rebooting.", name
);
1786 handler
->conf
->reboot
= 1;
1788 case SIGSYS
: /* seccomp */
1789 DEBUG("Container \"%s\" violated its seccomp policy.", name
);
1792 DEBUG("Unknown exit status for container \"%s\" init %d.", name
, WTERMSIG(status
));
1797 err
= lxc_restore_phys_nics_to_netns(handler
);
1799 ERROR("Failed to move physical network devices back to parent "
1800 "network namespace");
1802 if (handler
->pinfd
>= 0) {
1803 close(handler
->pinfd
);
1804 handler
->pinfd
= -1;
1807 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1808 err
= lxc_error_set_and_log(handler
->pid
, status
);
1811 lxc_delete_network(handler
);
1813 out_detach_blockdev
:
1814 detach_block_device(handler
->conf
);
1817 lxc_fini(name
, handler
);
1821 lxc_abort(name
, handler
);
1829 static int start(struct lxc_handler
*handler
, void* data
)
1831 struct start_args
*arg
= data
;
1833 NOTICE("Exec'ing \"%s\".", arg
->argv
[0]);
1835 execvp(arg
->argv
[0], arg
->argv
);
1836 SYSERROR("Failed to exec \"%s\".", arg
->argv
[0]);
1840 static int post_start(struct lxc_handler
*handler
, void* data
)
1842 struct start_args
*arg
= data
;
1844 NOTICE("Started \"%s\" with pid \"%d\".", arg
->argv
[0], handler
->pid
);
1848 static struct lxc_operations start_ops
= {
1850 .post_start
= post_start
1853 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
1854 const char *lxcpath
, bool backgrounded
)
1856 struct start_args start_arg
= {
1860 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
);
1863 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
1866 char destroy
[MAXPATHLEN
];
1869 struct lxc_container
*c
;
1870 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
1871 bret
= do_destroy_container(handler
);
1873 ERROR("Error destroying rootfs for container \"%s\".", name
);
1877 INFO("Destroyed rootfs for container \"%s\".", name
);
1879 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
1880 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1881 ERROR("Error destroying directory for container \"%s\".", name
);
1885 c
= lxc_container_new(name
, handler
->lxcpath
);
1887 if (container_disk_lock(c
)) {
1888 INFO("Could not update lxc_snapshots file.");
1889 lxc_container_put(c
);
1891 mod_all_rdeps(c
, false);
1892 container_disk_unlock(c
);
1893 lxc_container_put(c
);
1897 if (!handler
->am_root
)
1898 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
1899 destroy
, "lxc_rmdir_onedev_wrapper");
1901 ret
= lxc_rmdir_onedev(destroy
, NULL
);
1904 ERROR("Error destroying directory for container \"%s\".", name
);
1907 INFO("Destroyed directory for container \"%s\".", name
);
1910 static int lxc_rmdir_onedev_wrapper(void *data
)
1912 char *arg
= (char *) data
;
1913 return lxc_rmdir_onedev(arg
, NULL
);
1916 static bool do_destroy_container(struct lxc_handler
*handler
) {
1919 if (!handler
->am_root
) {
1920 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
1921 handler
->conf
, "storage_destroy_wrapper");
1928 return storage_destroy(handler
->conf
);