]>
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"
77 #include "lxccontainer.h"
79 #include "lxcseccomp.h"
82 #include "namespace.h"
86 #include "storage_utils.h"
91 lxc_log_define(lxc_start
, lxc
);
93 extern void mod_all_rdeps(struct lxc_container
*c
, bool inc
);
94 static bool do_destroy_container(struct lxc_handler
*handler
);
95 static int lxc_rmdir_onedev_wrapper(void *data
);
96 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
99 static void print_top_failing_dir(const char *path
)
101 size_t len
= strlen(path
);
102 char *copy
= alloca(len
+ 1), *p
, *e
, saved
;
108 while (p
< e
&& *p
== '/')
110 while (p
< e
&& *p
!= '/')
114 if (access(copy
, X_OK
)) {
115 SYSERROR("Could not access %s. Please grant it x "
116 "access, or add an ACL for the container "
124 static void close_ns(int ns_fd
[LXC_NS_MAX
])
128 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
136 /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified
138 * Return true on success, false on failure.
140 static bool preserve_ns(int ns_fd
[LXC_NS_MAX
], int clone_flags
, pid_t pid
)
144 for (i
= 0; i
< LXC_NS_MAX
; i
++)
147 ret
= lxc_preserve_ns(pid
, "");
149 SYSERROR("Kernel does not support attaching to namespaces.");
155 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
156 if ((clone_flags
& ns_info
[i
].clone_flag
) == 0)
159 ns_fd
[i
] = lxc_preserve_ns(pid
, ns_info
[i
].proc_name
);
163 DEBUG("Preserved %s namespace via fd %d", ns_info
[i
].proc_name
, ns_fd
[i
]);
170 SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info
[i
].proc_name
);
172 SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info
[i
].proc_name
, strerror(errno
));
177 static int match_fd(int fd
)
179 return (fd
== 0 || fd
== 1 || fd
== 2);
182 int lxc_check_inherited(struct lxc_conf
*conf
, bool closeall
,
183 int *fds_to_ignore
, size_t len_fds
)
185 struct dirent
*direntp
;
190 if (conf
&& conf
->close_all_fds
)
194 dir
= opendir("/proc/self/fd");
196 WARN("Failed to open directory: %s.", strerror(errno
));
202 while ((direntp
= readdir(dir
))) {
203 struct lxc_list
*cur
;
204 bool matched
= false;
209 if (!strcmp(direntp
->d_name
, "."))
212 if (!strcmp(direntp
->d_name
, ".."))
215 if (lxc_safe_int(direntp
->d_name
, &fd
) < 0) {
216 INFO("Could not parse file descriptor for: %s", direntp
->d_name
);
220 for (i
= 0; i
< len_fds
; i
++)
221 if (fds_to_ignore
[i
] == fd
)
224 if (fd
== fddir
|| fd
== lxc_log_fd
||
225 (i
< len_fds
&& fd
== fds_to_ignore
[i
]))
228 /* Keep state clients that wait on reboots. */
230 lxc_list_for_each(cur
, &conf
->state_clients
) {
231 struct lxc_state_client
*client
= cur
->elem
;
233 if (client
->clientfd
!= fd
)
244 if (current_config
&& fd
== current_config
->logfd
)
253 INFO("Closed inherited fd %d", fd
);
256 WARN("Inherited fd %d", fd
);
259 /* Only enable syslog at this point to avoid the above logging function
260 * to open a new fd and make the check_inherited function enter an
263 lxc_log_enable_syslog();
265 closedir(dir
); /* cannot fail */
269 static int setup_signal_fd(sigset_t
*oldmask
)
274 /* Block everything except serious error signals. */
275 if (sigfillset(&mask
) ||
276 sigdelset(&mask
, SIGILL
) ||
277 sigdelset(&mask
, SIGSEGV
) ||
278 sigdelset(&mask
, SIGBUS
) ||
279 sigdelset(&mask
, SIGWINCH
) ||
280 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
281 SYSERROR("Failed to set signal mask.");
285 fd
= signalfd(-1, &mask
, 0);
287 SYSERROR("Failed to create signal file descriptor.");
291 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
292 SYSERROR("Failed to set FD_CLOEXEC on the signal file descriptor: %d.", fd
);
297 DEBUG("Set SIGCHLD handler with file descriptor: %d.", fd
);
302 static int signal_handler(int fd
, uint32_t events
, void *data
,
303 struct lxc_epoll_descr
*descr
)
305 struct signalfd_siginfo siginfo
;
309 bool init_died
= false;
311 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
313 ERROR("Failed to read signal info from signal file descriptor: %d.", fd
);
317 if (ret
!= sizeof(siginfo
)) {
318 ERROR("Unexpected size for siginfo struct.");
322 /* Check whether init is running. */
324 ret
= waitid(P_PID
, *pid
, &info
, WEXITED
| WNOWAIT
| WNOHANG
);
325 if (ret
== 0 && info
.si_pid
== *pid
)
328 if (siginfo
.ssi_signo
!= SIGCHLD
) {
329 kill(*pid
, siginfo
.ssi_signo
);
330 INFO("Forwarded signal %d to pid %d.", siginfo
.ssi_signo
, *pid
);
331 return init_died
? 1 : 0;
334 if (siginfo
.ssi_code
== CLD_STOPPED
) {
335 INFO("Container init process was stopped.");
336 return init_died
? 1 : 0;
337 } else if (siginfo
.ssi_code
== CLD_CONTINUED
) {
338 INFO("Container init process was continued.");
339 return init_died
? 1 : 0;
342 /* More robustness, protect ourself from a SIGCHLD sent
343 * by a process different from the container init.
345 if (siginfo
.ssi_pid
!= *pid
) {
346 NOTICE("Received SIGCHLD from pid %d instead of container init %d.", siginfo
.ssi_pid
, *pid
);
347 return init_died
? 1 : 0;
350 DEBUG("Container init process %d exited.", *pid
);
354 static int lxc_serve_state_clients(const char *name
,
355 struct lxc_handler
*handler
,
359 struct lxc_list
*cur
, *next
;
360 struct lxc_state_client
*client
;
361 struct lxc_msg msg
= {.type
= lxc_msg_state
, .value
= state
};
364 handler
->state
= state
;
365 TRACE("Set container state to %s", lxc_state2str(state
));
367 if (lxc_list_empty(&handler
->conf
->state_clients
)) {
368 TRACE("No state clients registered");
370 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
374 strncpy(msg
.name
, name
, sizeof(msg
.name
));
375 msg
.name
[sizeof(msg
.name
) - 1] = 0;
377 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
380 if (client
->states
[state
] == 0) {
381 TRACE("State %s not registered for state client %d",
382 lxc_state2str(state
), client
->clientfd
);
386 TRACE("Sending state %s to state client %d",
387 lxc_state2str(state
), client
->clientfd
);
390 ret
= send(client
->clientfd
, &msg
, sizeof(msg
), 0);
392 if (errno
== EINTR
) {
393 TRACE("Caught EINTR; retrying");
397 ERROR("%s - Failed to send message to client",
401 /* kick client from list */
402 close(client
->clientfd
);
412 static int lxc_serve_state_socket_pair(const char *name
,
413 struct lxc_handler
*handler
,
418 if (!handler
->backgrounded
||
419 handler
->state_socket_pair
[1] < 0 ||
423 /* Close read end of the socket pair. */
424 close(handler
->state_socket_pair
[0]);
425 handler
->state_socket_pair
[0] = -1;
428 ret
= lxc_abstract_unix_send_credential(handler
->state_socket_pair
[1],
429 &(int){state
}, sizeof(int));
430 if (ret
!= sizeof(int)) {
433 SYSERROR("Failed to send state to %d",
434 handler
->state_socket_pair
[1]);
438 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state
),
439 handler
->state_socket_pair
[1]);
441 /* Close write end of the socket pair. */
442 close(handler
->state_socket_pair
[1]);
443 handler
->state_socket_pair
[1] = -1;
448 int lxc_set_state(const char *name
, struct lxc_handler
*handler
,
453 ret
= lxc_serve_state_socket_pair(name
, handler
, state
);
455 ERROR("Failed to synchronize via anonymous pair of unix sockets");
459 ret
= lxc_serve_state_clients(name
, handler
, state
);
463 /* This function will try to connect to the legacy lxc-monitord state
464 * server and only exists for backwards compatibility.
466 lxc_monitor_send_state(name
, state
, handler
->lxcpath
);
471 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
473 int sigfd
= handler
->sigfd
;
474 int pid
= handler
->pid
;
475 struct lxc_epoll_descr descr
;
477 if (lxc_mainloop_open(&descr
)) {
478 ERROR("Failed to create LXC mainloop.");
482 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
483 ERROR("Failed to add signal handler with file descriptor %d to LXC mainloop.", sigfd
);
484 goto out_mainloop_open
;
487 if (lxc_console_mainloop_add(&descr
, handler
->conf
)) {
488 ERROR("Failed to add console handler to LXC mainloop.");
489 goto out_mainloop_open
;
492 if (lxc_cmd_mainloop_add(name
, &descr
, handler
)) {
493 ERROR("Failed to add command handler to LXC mainloop.");
494 goto out_mainloop_open
;
497 TRACE("lxc mainloop is ready");
499 return lxc_mainloop(&descr
, -1);
502 lxc_mainloop_close(&descr
);
510 void lxc_free_handler(struct lxc_handler
*handler
)
512 if (handler
->conf
&& handler
->conf
->maincmd_fd
)
513 close(handler
->conf
->maincmd_fd
);
515 if (handler
->state_socket_pair
[0] >= 0)
516 close(handler
->state_socket_pair
[0]);
518 if (handler
->state_socket_pair
[1] >= 0)
519 close(handler
->state_socket_pair
[1]);
521 handler
->conf
= NULL
;
525 struct lxc_handler
*lxc_init_handler(const char *name
, struct lxc_conf
*conf
,
526 const char *lxcpath
, bool daemonize
)
529 struct lxc_handler
*handler
;
531 handler
= malloc(sizeof(*handler
));
533 ERROR("failed to allocate memory");
537 memset(handler
, 0, sizeof(*handler
));
539 /* Note that am_unpriv() checks the effective uid. We probably don't
540 * care if we are real root only if we are running as root so this
543 handler
->am_root
= !am_unpriv();
544 handler
->data_sock
[0] = handler
->data_sock
[1] = -1;
545 handler
->conf
= conf
;
546 handler
->lxcpath
= lxcpath
;
548 handler
->state_socket_pair
[0] = handler
->state_socket_pair
[1] = -1;
549 if (handler
->conf
->reboot
== 0)
550 lxc_list_init(&handler
->conf
->state_clients
);
552 for (i
= 0; i
< LXC_NS_MAX
; i
++)
553 handler
->nsfd
[i
] = -1;
555 handler
->name
= name
;
557 if (daemonize
&& !handler
->conf
->reboot
) {
558 /* Create socketpair() to synchronize on daemonized startup.
559 * When the container reboots we don't need to synchronize again
560 * currently so don't open another socketpair().
562 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
563 handler
->state_socket_pair
);
565 ERROR("Failed to create anonymous pair of unix sockets");
568 TRACE("Created anonymous pair {%d,%d} of unix sockets",
569 handler
->state_socket_pair
[0],
570 handler
->state_socket_pair
[1]);
573 if (handler
->conf
->reboot
== 0) {
574 handler
->conf
->maincmd_fd
= lxc_cmd_init(name
, lxcpath
, "command");
575 if (handler
->conf
->maincmd_fd
< 0) {
576 ERROR("Failed to set up command socket");
580 TRACE("Unix domain socket %d for command server is ready",
581 handler
->conf
->maincmd_fd
);
586 lxc_free_handler(handler
);
591 int lxc_init(const char *name
, struct lxc_handler
*handler
)
593 const char *loglevel
;
594 struct lxc_conf
*conf
= handler
->conf
;
597 TRACE("initialized LSM");
599 if (lxc_read_seccomp_config(conf
) != 0) {
600 ERROR("Failed loading seccomp policy.");
601 goto out_close_maincmd_fd
;
603 TRACE("read seccomp policy");
605 /* Begin by setting the state to STARTING. */
606 if (lxc_set_state(name
, handler
, STARTING
)) {
607 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
, lxc_state2str(STARTING
));
608 goto out_close_maincmd_fd
;
610 TRACE("set container state to \"STARTING\"");
612 /* Start of environment variable setup for hooks. */
613 if (name
&& setenv("LXC_NAME", name
, 1))
614 SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name
);
616 if (conf
->rcfile
&& setenv("LXC_CONFIG_FILE", conf
->rcfile
, 1))
617 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf
->rcfile
);
619 if (conf
->rootfs
.mount
&& setenv("LXC_ROOTFS_MOUNT", conf
->rootfs
.mount
, 1))
620 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf
->rootfs
.mount
);
622 if (conf
->rootfs
.path
&& setenv("LXC_ROOTFS_PATH", conf
->rootfs
.path
, 1))
623 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf
->rootfs
.path
);
625 if (conf
->console
.path
&& setenv("LXC_CONSOLE", conf
->console
.path
, 1))
626 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf
->console
.path
);
628 if (conf
->console
.log_path
&& setenv("LXC_CONSOLE_LOGPATH", conf
->console
.log_path
, 1))
629 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf
->console
.log_path
);
631 if (setenv("LXC_CGNS_AWARE", "1", 1))
632 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1.");
634 loglevel
= lxc_log_priority_to_string(lxc_log_get_level());
635 if (setenv("LXC_LOG_LEVEL", loglevel
, 1))
636 SYSERROR("Failed to set environment variable LXC_LOG_LEVEL=%s", loglevel
);
637 /* End of environment variable setup for hooks. */
639 TRACE("set environment variables");
641 if (run_lxc_hooks(name
, "pre-start", conf
, handler
->lxcpath
, NULL
)) {
642 ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name
);
645 TRACE("ran pre-start hooks");
647 /* The signal fd has to be created before forking otherwise if the child
648 * process exits before we setup the signal fd, the event will be lost
649 * and the command will be stuck.
651 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
652 if (handler
->sigfd
< 0) {
653 ERROR("Failed to setup SIGCHLD fd handler.");
656 TRACE("set up signal fd");
658 /* Do this after setting up signals since it might unblock SIGWINCH. */
659 if (lxc_console_create(conf
)) {
660 ERROR("Failed to create console for container \"%s\".", name
);
661 goto out_restore_sigmask
;
663 TRACE("created console");
665 if (lxc_ttys_shift_ids(conf
) < 0) {
666 ERROR("Failed to shift tty into container.");
667 goto out_restore_sigmask
;
669 TRACE("shifted tty ids");
671 INFO("container \"%s\" is initialized", name
);
675 sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
);
677 lxc_delete_tty(&conf
->tty_info
);
679 lxc_set_state(name
, handler
, ABORTING
);
680 out_close_maincmd_fd
:
681 close(conf
->maincmd_fd
);
682 conf
->maincmd_fd
= -1;
686 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
689 struct lxc_list
*cur
, *next
;
690 pid_t self
= getpid();
691 char *namespaces
[LXC_NS_MAX
+ 1];
692 size_t namespace_count
= 0;
694 /* The STOPPING state is there for future cleanup code which can take
697 lxc_set_state(name
, handler
, STOPPING
);
699 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
700 if (handler
->nsfd
[i
] != -1) {
701 rc
= asprintf(&namespaces
[namespace_count
], "%s:/proc/%d/fd/%d",
702 ns_info
[i
].proc_name
, self
, handler
->nsfd
[i
]);
704 SYSERROR("Failed to allocate memory.");
710 namespaces
[namespace_count
] = NULL
;
712 if (handler
->conf
->reboot
&& setenv("LXC_TARGET", "reboot", 1))
713 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot.");
715 if (!handler
->conf
->reboot
&& setenv("LXC_TARGET", "stop", 1))
716 SYSERROR("Failed to set environment variable: LXC_TARGET=stop.");
718 if (run_lxc_hooks(name
, "stop", handler
->conf
, handler
->lxcpath
, namespaces
))
719 ERROR("Failed to run lxc.hook.stop for container \"%s\".", name
);
721 while (namespace_count
--)
722 free(namespaces
[namespace_count
]);
724 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
725 if (handler
->nsfd
[i
] < 0)
728 close(handler
->nsfd
[i
]);
729 handler
->nsfd
[i
] = -1;
732 cgroup_destroy(handler
);
734 lxc_set_state(name
, handler
, STOPPED
);
736 if (handler
->conf
->reboot
== 0) {
737 /* close command socket */
738 close(handler
->conf
->maincmd_fd
);
739 handler
->conf
->maincmd_fd
= -1;
742 if (run_lxc_hooks(name
, "post-stop", handler
->conf
, handler
->lxcpath
, NULL
)) {
743 ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name
);
744 if (handler
->conf
->reboot
) {
745 WARN("Container will be stopped instead of rebooted.");
746 handler
->conf
->reboot
= 0;
747 if (setenv("LXC_TARGET", "stop", 1))
748 WARN("Failed to set environment variable: LXC_TARGET=stop.");
752 /* Reset mask set by setup_signal_fd. */
753 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
754 WARN("Failed to restore signal mask.");
756 lxc_console_delete(&handler
->conf
->console
);
757 lxc_delete_tty(&handler
->conf
->tty_info
);
759 /* The command socket is now closed, no more state clients can register
760 * themselves from now on. So free the list of state clients.
762 lxc_list_for_each_safe(cur
, &handler
->conf
->state_clients
, next
) {
763 struct lxc_state_client
*client
= cur
->elem
;
765 /* Keep state clients that want to be notified about reboots. */
766 if ((handler
->conf
->reboot
> 0) && (client
->states
[RUNNING
] == 2))
769 /* close state client socket */
770 close(client
->clientfd
);
776 if (handler
->data_sock
[0] != -1) {
777 close(handler
->data_sock
[0]);
778 close(handler
->data_sock
[1]);
781 if (handler
->conf
->ephemeral
== 1 && handler
->conf
->reboot
!= 1)
782 lxc_destroy_container_on_signal(handler
, name
);
787 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
791 lxc_set_state(name
, handler
, ABORTING
);
792 if (handler
->pid
> 0)
793 kill(handler
->pid
, SIGKILL
);
794 while ((ret
= waitpid(-1, &status
, 0)) > 0) {
799 static int do_start(void *data
)
802 struct lxc_list
*iterator
;
805 struct lxc_handler
*handler
= data
;
806 bool have_cap_setgid
;
810 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
811 SYSERROR("Failed to set signal mask.");
815 /* This prctl must be before the synchro, so if the parent dies before
816 * we set the parent death signal, we will detect its death with the
817 * synchro right after, otherwise we have a window where the parent can
818 * exit before we set the pdeath signal leading to a unsupervized
821 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
822 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL.");
826 lxc_sync_fini_parent(handler
);
828 /* Don't leak the pinfd to the container. */
829 if (handler
->pinfd
>= 0)
830 close(handler
->pinfd
);
832 if (lxc_sync_wait_parent(handler
, LXC_SYNC_STARTUP
))
835 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
836 * https://github.com/lxc/lxd/issues/1978.
838 if ((handler
->clone_flags
& (CLONE_NEWNET
| CLONE_NEWUSER
)) ==
839 (CLONE_NEWNET
| CLONE_NEWUSER
)) {
840 ret
= unshare(CLONE_NEWNET
);
842 SYSERROR("Failed to unshare CLONE_NEWNET.");
843 goto out_warn_father
;
845 INFO("Unshared CLONE_NEWNET.");
848 /* Tell the parent task it can begin to configure the container and wait
851 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
854 if (lxc_network_recv_veth_names_from_parent(handler
) < 0) {
855 ERROR("Failed to receive veth names from parent");
856 goto out_warn_father
;
859 /* If we are in a new user namespace, become root there to have
860 * privilege over our namespace.
862 if (!lxc_list_empty(&handler
->conf
->id_map
)) {
863 if (lxc_switch_uid_gid(0, 0) < 0)
864 goto out_warn_father
;
866 /* Drop groups only after we switched to a valid gid in the new
869 if (lxc_setgroups(0, NULL
) < 0)
870 goto out_warn_father
;
873 if (access(handler
->lxcpath
, X_OK
)) {
874 print_top_failing_dir(handler
->lxcpath
);
875 goto out_warn_father
;
878 ret
= snprintf(path
, sizeof(path
), "%s/dev/null", handler
->conf
->rootfs
.mount
);
879 if (ret
< 0 || ret
>= sizeof(path
))
880 goto out_warn_father
;
882 /* In order to checkpoint restore, we need to have everything in the
883 * same mount namespace. However, some containers may not have a
884 * reasonable /dev (in particular, they may not have /dev/null), so we
885 * can't set init's std fds to /dev/null by opening it from inside the
888 * If that's the case, fall back to using the host's /dev/null. This
889 * means that migration won't work, but at least we won't spew output
890 * where it isn't wanted.
892 if (handler
->backgrounded
&& !handler
->conf
->autodev
&& access(path
, F_OK
) < 0) {
893 devnull_fd
= open_devnull();
896 goto out_warn_father
;
897 WARN("Using /dev/null from the host for container init's "
898 "standard file descriptors. Migration will not work.");
901 /* Ask father to setup cgroups and wait for him to finish. */
902 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP
))
905 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
906 * earlier we end up with a wrong view of /proc/self/cgroup. For
907 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
908 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
909 * /proc/self/cgroup would show us:
913 * whereas it should actually show
917 if (cgns_supported()) {
918 if (unshare(CLONE_NEWCGROUP
) < 0) {
919 INFO("Failed to unshare CLONE_NEWCGROUP.");
920 goto out_warn_father
;
922 INFO("Unshared CLONE_NEWCGROUP.");
925 /* Add the requested environment variables to the current environment to
926 * allow them to be used by the various hooks, such as the start hook
929 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
930 if (putenv((char *)iterator
->elem
)) {
931 SYSERROR("Failed to set environment variable: %s.", (char *)iterator
->elem
);
932 goto out_warn_father
;
936 /* Setup the container, ip, names, utsname, ... */
937 ret
= lxc_setup(handler
);
938 close(handler
->data_sock
[0]);
939 close(handler
->data_sock
[1]);
941 ERROR("Failed to setup container \"%s\".", handler
->name
);
942 goto out_warn_father
;
945 /* Set the label to change to when we exec(2) the container's init. */
946 if (lsm_process_label_set(NULL
, handler
->conf
, 1, 1) < 0)
947 goto out_warn_father
;
949 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
950 * before we aren't allowed anymore.
952 if (handler
->conf
->no_new_privs
) {
953 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) < 0) {
954 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
955 goto out_warn_father
;
957 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
960 /* Some init's such as busybox will set sane tty settings on stdin,
961 * stdout, stderr which it thinks is the console. We already set them
962 * the way we wanted on the real terminal, and we want init to do its
963 * setup on its console ie. the pty allocated in lxc_console_create() so
964 * make sure that that pty is stdin,stdout,stderr.
966 if (handler
->conf
->console
.slave
>= 0) {
967 if (handler
->backgrounded
|| handler
->conf
->is_execute
== 0)
968 ret
= set_stdfds(handler
->conf
->console
.slave
);
970 ret
= lxc_console_set_stdfds(handler
->conf
->console
.slave
);
972 ERROR("Failed to redirect std{in,out,err} to pty file "
974 handler
->conf
->console
.slave
);
975 goto out_warn_father
;
979 /* If we mounted a temporary proc, then unmount it now. */
980 tmp_proc_unmount(handler
->conf
);
982 if (lxc_seccomp_load(handler
->conf
) != 0)
983 goto out_warn_father
;
985 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
, handler
->lxcpath
, NULL
)) {
986 ERROR("Failed to run lxc.hook.start for container \"%s\".", handler
->name
);
987 goto out_warn_father
;
990 close(handler
->sigfd
);
992 if (devnull_fd
< 0) {
993 devnull_fd
= open_devnull();
996 goto out_warn_father
;
999 if (handler
->conf
->console
.slave
< 0 && handler
->backgrounded
)
1000 if (set_stdfds(devnull_fd
) < 0) {
1001 ERROR("Failed to redirect std{in,out,err} to "
1003 goto out_warn_father
;
1006 if (devnull_fd
>= 0) {
1013 if (handler
->conf
->init_cwd
&& chdir(handler
->conf
->init_cwd
)) {
1014 SYSERROR("Could not change directory to \"%s\"", handler
->conf
->init_cwd
);
1015 goto out_warn_father
;
1018 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CGROUP_LIMITS
))
1019 goto out_warn_father
;
1021 /* Reset the environment variables the user requested in a clear
1025 SYSERROR("Failed to clear environment.");
1026 /* Don't error out though. */
1029 lxc_list_for_each(iterator
, &handler
->conf
->environment
) {
1030 if (putenv((char *)iterator
->elem
)) {
1031 SYSERROR("Failed to set environment variable: %s.", (char *)iterator
->elem
);
1032 goto out_warn_father
;
1036 if (putenv("container=lxc")) {
1037 SYSERROR("Failed to set environment variable: container=lxc.");
1038 goto out_warn_father
;
1041 if (handler
->conf
->pty_names
) {
1042 if (putenv(handler
->conf
->pty_names
)) {
1043 SYSERROR("Failed to set environment variable for container ptys.");
1044 goto out_warn_father
;
1048 /* The container has been setup. We can now switch to an unprivileged
1051 new_uid
= handler
->conf
->init_uid
;
1052 new_gid
= handler
->conf
->init_gid
;
1054 /* If we are in a new user namespace we already dropped all
1055 * groups when we switched to root in the new user namespace
1056 * further above. Only drop groups if we can, so ensure that we
1057 * have necessary privilege.
1060 have_cap_setgid
= lxc_proc_cap_is_set(CAP_SETGID
, CAP_EFFECTIVE
);
1062 have_cap_setgid
= false;
1064 if (lxc_list_empty(&handler
->conf
->id_map
) && have_cap_setgid
) {
1065 if (lxc_setgroups(0, NULL
) < 0)
1066 goto out_warn_father
;
1069 if (lxc_switch_uid_gid(new_uid
, new_gid
) < 0)
1070 goto out_warn_father
;
1072 /* After this call, we are in error because this ops should not return
1075 handler
->ops
->start(handler
, handler
->data
);
1078 /* We want the parent to know something went wrong, so we return a
1079 * special error code.
1081 lxc_sync_wake_parent(handler
, LXC_SYNC_ERROR
);
1084 if (devnull_fd
>= 0)
1090 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
1093 struct lxc_pty_info
*pty_info
;
1095 int sock
= handler
->data_sock
[1];
1096 struct lxc_conf
*conf
= handler
->conf
;
1097 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
1102 tty_info
->pty_info
= malloc(sizeof(*tty_info
->pty_info
) * conf
->tty
);
1103 if (!tty_info
->pty_info
)
1106 for (i
= 0; i
< conf
->tty
; i
++) {
1109 ret
= lxc_abstract_unix_recv_fds(sock
, ttyfds
, 2, NULL
, 0);
1113 pty_info
= &tty_info
->pty_info
[i
];
1115 pty_info
->master
= ttyfds
[0];
1116 pty_info
->slave
= ttyfds
[1];
1117 TRACE("Received pty with master fd %d and slave fd %d from "
1118 "parent", pty_info
->master
, pty_info
->slave
);
1121 ERROR("Failed to receive %d ttys from child: %s", conf
->tty
,
1124 TRACE("Received %d ttys from child", conf
->tty
);
1126 tty_info
->nbtty
= conf
->tty
;
1131 void resolve_clone_flags(struct lxc_handler
*handler
)
1133 handler
->clone_flags
= CLONE_NEWNS
;
1135 if (!handler
->conf
->inherit_ns
[LXC_NS_USER
]) {
1136 if (!lxc_list_empty(&handler
->conf
->id_map
))
1137 handler
->clone_flags
|= CLONE_NEWUSER
;
1139 INFO("Inheriting user namespace");
1142 if (!handler
->conf
->inherit_ns
[LXC_NS_NET
]) {
1143 if (!lxc_requests_empty_network(handler
))
1144 handler
->clone_flags
|= CLONE_NEWNET
;
1146 INFO("Inheriting net namespace");
1149 if (!handler
->conf
->inherit_ns
[LXC_NS_IPC
])
1150 handler
->clone_flags
|= CLONE_NEWIPC
;
1152 INFO("Inheriting ipc namespace");
1154 if (!handler
->conf
->inherit_ns
[LXC_NS_UTS
])
1155 handler
->clone_flags
|= CLONE_NEWUTS
;
1157 INFO("Inheriting uts namespace");
1159 if (!handler
->conf
->inherit_ns
[LXC_NS_PID
])
1160 handler
->clone_flags
|= CLONE_NEWPID
;
1162 INFO("Inheriting pid namespace");
1165 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1166 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1167 * But since the memory between parent and child is shared on CLONE_VM this
1168 * would invalidate the getpid() cache that glibc used to maintain and so
1169 * getpid() in the child would return the parent's pid. This is all fixed in
1170 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1171 * not reset anymore.
1172 * However, if for whatever reason you - dear commiter - somehow need to get the
1173 * pid of the dummy intermediate process for do_share_ns() you need to call
1174 * syscall(__NR_getpid) directly. The next lxc_clone() call does not employ
1175 * CLONE_VM and will be fine.
1177 static inline int do_share_ns(void *arg
)
1180 struct lxc_handler
*handler
= arg
;
1182 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1183 if (handler
->nsfd
[i
] < 0)
1186 ret
= setns(handler
->nsfd
[i
], 0);
1190 DEBUG("Inherited %s namespace", ns_info
[i
].proc_name
);
1193 flags
= handler
->on_clone_flags
;
1194 flags
|= CLONE_PARENT
;
1195 handler
->pid
= lxc_clone(do_start
, handler
, flags
);
1196 if (handler
->pid
< 0)
1202 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1203 * exec()s the requested container binary.
1204 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1205 * right here should be double checked if they'd pose a security risk. (For
1206 * example, any {u}mount() operations performed here will be reflected on the
1209 static int lxc_spawn(struct lxc_handler
*handler
)
1213 bool wants_to_map_ids
;
1214 struct lxc_list
*id_map
;
1215 const char *name
= handler
->name
;
1216 const char *lxcpath
= handler
->lxcpath
;
1217 bool cgroups_connected
= false, share_ns
= false;
1218 struct lxc_conf
*conf
= handler
->conf
;
1220 id_map
= &conf
->id_map
;
1221 wants_to_map_ids
= !lxc_list_empty(id_map
);
1223 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
1224 if (!conf
->inherit_ns
[i
])
1227 handler
->nsfd
[i
] = lxc_inherit_namespace(conf
->inherit_ns
[i
], lxcpath
, ns_info
[i
].proc_name
);
1228 if (handler
->nsfd
[i
] < 0)
1234 if (lxc_sync_init(handler
))
1237 ret
= socketpair(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0,
1238 handler
->data_sock
);
1240 lxc_sync_fini(handler
);
1244 resolve_clone_flags(handler
);
1246 if (handler
->clone_flags
& CLONE_NEWNET
) {
1247 if (!lxc_list_empty(&conf
->network
)) {
1249 /* Find gateway addresses from the link device, which is
1250 * no longer accessible inside the container. Do this
1251 * before creating network interfaces, since goto
1252 * out_delete_net does not work before lxc_clone.
1254 if (lxc_find_gateway_addresses(handler
)) {
1255 ERROR("Failed to find gateway addresses.");
1256 lxc_sync_fini(handler
);
1260 /* That should be done before the clone because we will
1261 * fill the netdev index and use them in the child.
1263 if (lxc_create_network_priv(handler
)) {
1264 ERROR("Failed to create the network.");
1265 lxc_sync_fini(handler
);
1271 if (!cgroup_init(handler
)) {
1272 ERROR("Failed initializing cgroup support.");
1273 goto out_delete_net
;
1276 cgroups_connected
= true;
1278 if (!cgroup_create(handler
)) {
1279 ERROR("Failed creating cgroups.");
1280 goto out_delete_net
;
1283 /* If the rootfs is not a blockdev, prevent the container from marking
1285 * If the container is unprivileged then skip rootfs pinning.
1287 if (!wants_to_map_ids
) {
1288 handler
->pinfd
= pin_rootfs(conf
->rootfs
.path
);
1289 if (handler
->pinfd
== -1)
1290 INFO("Failed to pin the rootfs for container \"%s\".", handler
->name
);
1293 /* Create a process in a new set of namespaces. */
1294 handler
->on_clone_flags
= handler
->clone_flags
;
1295 if (handler
->clone_flags
& CLONE_NEWUSER
) {
1296 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1297 * clone a new user namespace first and only later unshare our
1298 * network namespace to ensure that network devices ownership is
1301 handler
->on_clone_flags
&= ~CLONE_NEWNET
;
1305 ret
= lxc_clone(do_share_ns
, handler
, CLONE_VFORK
| CLONE_VM
| CLONE_FILES
);
1307 handler
->pid
= lxc_clone(do_start
, handler
, handler
->on_clone_flags
);
1308 if (handler
->pid
< 0 || ret
< 0) {
1309 SYSERROR("Failed to clone a new set of namespaces.");
1310 goto out_delete_net
;
1312 TRACE("Cloned child process %d", handler
->pid
);
1314 for (i
= 0; i
< LXC_NS_MAX
; i
++)
1315 if (handler
->on_clone_flags
& ns_info
[i
].clone_flag
)
1316 INFO("Cloned %s", ns_info
[i
].flag_name
);
1318 if (!preserve_ns(handler
->nsfd
, handler
->clone_flags
& ~CLONE_NEWNET
, handler
->pid
)) {
1319 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1320 goto out_delete_net
;
1323 lxc_sync_fini_child(handler
);
1325 /* Map the container uids. The container became an invalid userid the
1326 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1327 * anything immediately, but allows the container to setuid(0) (0 being
1328 * mapped to something else on the host.) later to become a valid uid
1331 if (wants_to_map_ids
) {
1332 if (!handler
->conf
->inherit_ns
[LXC_NS_USER
]) {
1333 ret
= lxc_map_ids(id_map
, handler
->pid
);
1335 ERROR("Failed to set up id mapping.");
1336 goto out_delete_net
;
1341 if (lxc_sync_wake_child(handler
, LXC_SYNC_STARTUP
))
1342 goto out_delete_net
;
1344 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
1345 goto out_delete_net
;
1347 if (!cgroup_create_legacy(handler
)) {
1348 ERROR("Failed to setup legacy cgroups for container \"%s\".", name
);
1349 goto out_delete_net
;
1351 if (!cgroup_setup_limits(handler
, false)) {
1352 ERROR("Failed to setup cgroup limits for container \"%s\".", name
);
1353 goto out_delete_net
;
1356 if (!cgroup_enter(handler
))
1357 goto out_delete_net
;
1359 if (!cgroup_chown(handler
))
1360 goto out_delete_net
;
1362 /* Now we're ready to preserve the network namespace */
1363 ret
= lxc_preserve_ns(handler
->pid
, "net");
1365 ERROR("%s - Failed to preserve net namespace", strerror(errno
));
1366 goto out_delete_net
;
1368 handler
->nsfd
[LXC_NS_NET
] = ret
;
1369 DEBUG("Preserved net namespace via fd %d", ret
);
1371 /* Create the network configuration. */
1372 if (handler
->clone_flags
& CLONE_NEWNET
) {
1373 if (lxc_network_move_created_netdev_priv(handler
->lxcpath
,
1377 ERROR("Failed to create the configured network.");
1378 goto out_delete_net
;
1381 if (lxc_create_network_unpriv(handler
->lxcpath
, handler
->name
,
1384 ERROR("Failed to create the configured network.");
1385 goto out_delete_net
;
1389 if (lxc_network_send_veth_names_to_child(handler
) < 0) {
1390 ERROR("Failed to send veth names to child");
1391 goto out_delete_net
;
1394 /* Tell the child to continue its initialization. We'll get
1395 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1397 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
1398 goto out_delete_net
;
1400 if (!lxc_list_empty(&conf
->limits
) && setup_resource_limits(&conf
->limits
, handler
->pid
)) {
1401 ERROR("failed to setup resource limits for '%s'", name
);
1402 goto out_delete_net
;
1405 if (lxc_sync_barrier_child(handler
, LXC_SYNC_CGROUP_UNSHARE
))
1406 goto out_delete_net
;
1408 if (!cgroup_setup_limits(handler
, true)) {
1409 ERROR("Failed to setup the devices cgroup for container \"%s\".", name
);
1410 goto out_delete_net
;
1412 TRACE("Set up cgroup device limits");
1414 cgroup_disconnect();
1415 cgroups_connected
= false;
1417 snprintf(pidstr
, 20, "%d", handler
->pid
);
1418 if (setenv("LXC_PID", pidstr
, 1))
1419 SYSERROR("Failed to set environment variable: LXC_PID=%s.", pidstr
);
1421 /* Run any host-side start hooks */
1422 if (run_lxc_hooks(name
, "start-host", conf
, handler
->lxcpath
, NULL
)) {
1423 ERROR("Failed to run lxc.hook.start-host for container \"%s\".", name
);
1427 /* Tell the child to complete its initialization and wait for it to exec
1428 * or return an error. (The child will never return
1429 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1430 * lxc_sync_barrier_child to return success, or return a different
1431 * value, causing us to error out).
1433 if (lxc_sync_barrier_child(handler
, LXC_SYNC_READY_START
))
1436 if (cgns_supported()) {
1437 ret
= lxc_preserve_ns(handler
->pid
, "cgroup");
1439 ERROR("%s - Failed to preserve cgroup namespace", strerror(errno
));
1440 goto out_delete_net
;
1442 handler
->nsfd
[LXC_NS_CGROUP
] = ret
;
1443 DEBUG("Preserved cgroup namespace via fd %d", ret
);
1446 if (lxc_network_recv_name_and_ifindex_from_child(handler
) < 0) {
1447 ERROR("Failed to receive names and ifindices for network "
1448 "devices from child");
1449 goto out_delete_net
;
1452 /* Now all networks are created, network devices are moved into place,
1453 * and the correct names and ifindeces in the respective namespaces have
1454 * been recorded. The corresponding structs have now all been filled. So
1455 * log them for debugging purposes.
1457 lxc_log_configured_netdevs(conf
);
1459 /* Read tty fds allocated by child. */
1460 if (lxc_recv_ttys_from_child(handler
) < 0) {
1461 ERROR("Failed to receive tty info from child process.");
1462 goto out_delete_net
;
1465 if (handler
->ops
->post_start(handler
, handler
->data
))
1468 if (lxc_set_state(name
, handler
, RUNNING
)) {
1469 ERROR("Failed to set state for container \"%s\" to \"%s\".", name
,
1470 lxc_state2str(RUNNING
));
1474 lxc_sync_fini(handler
);
1479 if (cgroups_connected
)
1480 cgroup_disconnect();
1482 if (handler
->clone_flags
& CLONE_NEWNET
)
1483 lxc_delete_network(handler
);
1486 lxc_abort(name
, handler
);
1487 lxc_sync_fini(handler
);
1488 if (handler
->pinfd
>= 0) {
1489 close(handler
->pinfd
);
1490 handler
->pinfd
= -1;
1496 int __lxc_start(const char *name
, struct lxc_handler
*handler
,
1497 struct lxc_operations
* ops
, void *data
, const char *lxcpath
,
1502 struct lxc_conf
*conf
= handler
->conf
;
1504 if (lxc_init(name
, handler
) < 0) {
1505 ERROR("Failed to initialize container \"%s\".", name
);
1509 handler
->data
= data
;
1510 handler
->backgrounded
= backgrounded
;
1512 if (!attach_block_device(handler
->conf
)) {
1513 ERROR("Failed to attach block device.");
1514 goto out_fini_nonet
;
1517 if (geteuid() == 0 && !lxc_list_empty(&conf
->id_map
)) {
1518 /* If the backing store is a device, mount it here and now. */
1519 if (rootfs_is_blockdev(conf
)) {
1520 if (unshare(CLONE_NEWNS
) < 0) {
1521 ERROR("Failed to unshare CLONE_NEWNS.");
1522 goto out_fini_nonet
;
1524 INFO("Unshared CLONE_NEWNS.");
1526 remount_all_slave();
1527 if (do_rootfs_setup(conf
, name
, lxcpath
) < 0) {
1528 ERROR("Error setting up rootfs mount as root before spawn.");
1529 goto out_fini_nonet
;
1531 INFO("Set up container rootfs as host root.");
1535 err
= lxc_spawn(handler
);
1537 ERROR("Failed to spawn container \"%s\".", name
);
1538 goto out_detach_blockdev
;
1540 /* close parent side of data socket */
1541 close(handler
->data_sock
[0]);
1542 handler
->data_sock
[0] = -1;
1543 close(handler
->data_sock
[1]);
1544 handler
->data_sock
[1] = -1;
1546 handler
->conf
->reboot
= 0;
1548 err
= lxc_poll(name
, handler
);
1550 ERROR("LXC mainloop exited with error: %d.", err
);
1554 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
1557 /* If the child process exited but was not signaled, it didn't call
1558 * reboot. This should mean it was an lxc-execute which simply exited.
1559 * In any case, treat it as a 'halt'.
1561 if (WIFSIGNALED(status
)) {
1562 switch(WTERMSIG(status
)) {
1563 case SIGINT
: /* halt */
1564 DEBUG("Container \"%s\" is halting.", name
);
1566 case SIGHUP
: /* reboot */
1567 DEBUG("Container \"%s\" is rebooting.", name
);
1568 handler
->conf
->reboot
= 1;
1570 case SIGSYS
: /* seccomp */
1571 DEBUG("Container \"%s\" violated its seccomp policy.", name
);
1574 DEBUG("Unknown exit status for container \"%s\" init %d.", name
, WTERMSIG(status
));
1579 err
= lxc_restore_phys_nics_to_netns(handler
);
1581 ERROR("Failed to move physical network devices back to parent "
1582 "network namespace");
1584 if (handler
->pinfd
>= 0) {
1585 close(handler
->pinfd
);
1586 handler
->pinfd
= -1;
1589 lxc_monitor_send_exit_code(name
, status
, handler
->lxcpath
);
1590 err
= lxc_error_set_and_log(handler
->pid
, status
);
1593 lxc_delete_network(handler
);
1595 out_detach_blockdev
:
1596 detach_block_device(handler
->conf
);
1599 lxc_fini(name
, handler
);
1603 lxc_abort(name
, handler
);
1611 static int start(struct lxc_handler
*handler
, void* data
)
1613 struct start_args
*arg
= data
;
1615 NOTICE("Exec'ing \"%s\".", arg
->argv
[0]);
1617 execvp(arg
->argv
[0], arg
->argv
);
1618 SYSERROR("Failed to exec \"%s\".", arg
->argv
[0]);
1622 static int post_start(struct lxc_handler
*handler
, void* data
)
1624 struct start_args
*arg
= data
;
1626 NOTICE("Started \"%s\" with pid \"%d\".", arg
->argv
[0], handler
->pid
);
1630 static struct lxc_operations start_ops
= {
1632 .post_start
= post_start
1635 int lxc_start(const char *name
, char *const argv
[], struct lxc_handler
*handler
,
1636 const char *lxcpath
, bool backgrounded
)
1638 struct start_args start_arg
= {
1642 return __lxc_start(name
, handler
, &start_ops
, &start_arg
, lxcpath
, backgrounded
);
1645 static void lxc_destroy_container_on_signal(struct lxc_handler
*handler
,
1648 char destroy
[MAXPATHLEN
];
1651 struct lxc_container
*c
;
1652 if (handler
->conf
->rootfs
.path
&& handler
->conf
->rootfs
.mount
) {
1653 bret
= do_destroy_container(handler
);
1655 ERROR("Error destroying rootfs for container \"%s\".", name
);
1659 INFO("Destroyed rootfs for container \"%s\".", name
);
1661 ret
= snprintf(destroy
, MAXPATHLEN
, "%s/%s", handler
->lxcpath
, name
);
1662 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1663 ERROR("Error destroying directory for container \"%s\".", name
);
1667 c
= lxc_container_new(name
, handler
->lxcpath
);
1669 if (container_disk_lock(c
)) {
1670 INFO("Could not update lxc_snapshots file.");
1671 lxc_container_put(c
);
1673 mod_all_rdeps(c
, false);
1674 container_disk_unlock(c
);
1675 lxc_container_put(c
);
1679 if (!handler
->am_root
)
1680 ret
= userns_exec_full(handler
->conf
, lxc_rmdir_onedev_wrapper
,
1681 destroy
, "lxc_rmdir_onedev_wrapper");
1683 ret
= lxc_rmdir_onedev(destroy
, NULL
);
1686 ERROR("Error destroying directory for container \"%s\".", name
);
1689 INFO("Destroyed directory for container \"%s\".", name
);
1692 static int lxc_rmdir_onedev_wrapper(void *data
)
1694 char *arg
= (char *) data
;
1695 return lxc_rmdir_onedev(arg
, NULL
);
1698 static bool do_destroy_container(struct lxc_handler
*handler
) {
1701 if (!handler
->am_root
) {
1702 ret
= userns_exec_full(handler
->conf
, storage_destroy_wrapper
,
1703 handler
->conf
, "storage_destroy_wrapper");
1710 return storage_destroy(handler
->conf
);