2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/param.h>
38 #include <sys/mount.h>
40 #include <sys/types.h>
41 #include <sys/prctl.h>
42 #include <sys/types.h>
43 #include <sys/capability.h>
48 #ifdef HAVE_SYS_SIGNALFD_H
49 # include <sys/signalfd.h>
51 /* assume kernel headers are too old */
53 struct signalfd_siginfo
74 # ifndef __NR_signalfd4
75 /* assume kernel headers are too old */
77 # define __NR_signalfd4 327
79 # define __NR_signalfd4 289
81 # define __NR_signalfd4 313
83 # define __NR_signalfd4 322
87 # ifndef __NR_signalfd
88 /* assume kernel headers are too old */
90 # define __NR_signalfd 321
92 # define __NR_signalfd 282
94 # define __NR_signalfd 305
96 # define __NR_signalfd 316
100 int signalfd(int fd
, const sigset_t
*mask
, int flags
)
104 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
105 if (errno
== ENOSYS
&& flags
== 0)
106 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
111 #if !HAVE_DECL_PR_CAPBSET_DROP
112 #define PR_CAPBSET_DROP 24
121 #include "mainloop.h"
125 #include "commands.h"
128 #include "namespace.h"
129 #include "apparmor.h"
130 #include "lxcseccomp.h"
132 lxc_log_define(lxc_start
, lxc
);
134 static int match_fd(int fd
)
136 return (fd
== 0 || fd
== 1 || fd
== 2);
139 int lxc_check_inherited(struct lxc_conf
*conf
, int fd_to_ignore
)
141 struct dirent dirent
, *direntp
;
146 dir
= opendir("/proc/self/fd");
148 WARN("failed to open directory: %m");
154 while (!readdir_r(dir
, &dirent
, &direntp
)) {
158 if (!strcmp(direntp
->d_name
, "."))
161 if (!strcmp(direntp
->d_name
, ".."))
164 fd
= atoi(direntp
->d_name
);
166 if (fd
== fddir
|| fd
== lxc_log_fd
|| fd
== fd_to_ignore
)
172 if (conf
->close_all_fds
) {
175 INFO("closed inherited fd %d", fd
);
178 WARN("inherited fd %d", fd
);
181 closedir(dir
); /* cannot fail */
185 static int setup_signal_fd(sigset_t
*oldmask
)
190 /* Block everything except serious error signals */
191 if (sigfillset(&mask
) ||
192 sigdelset(&mask
, SIGILL
) ||
193 sigdelset(&mask
, SIGSEGV
) ||
194 sigdelset(&mask
, SIGBUS
) ||
195 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
196 SYSERROR("failed to set signal mask");
200 fd
= signalfd(-1, &mask
, 0);
202 SYSERROR("failed to create the signal fd");
206 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
207 SYSERROR("failed to set sigfd to close-on-exec");
212 DEBUG("sigchild handler set");
217 static int signal_handler(int fd
, void *data
,
218 struct lxc_epoll_descr
*descr
)
220 struct signalfd_siginfo siginfo
;
224 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
226 ERROR("failed to read signal info");
230 if (ret
!= sizeof(siginfo
)) {
231 ERROR("unexpected siginfo size");
235 if (siginfo
.ssi_signo
!= SIGCHLD
) {
236 kill(*pid
, siginfo
.ssi_signo
);
237 INFO("forwarded signal %d to pid %d", siginfo
.ssi_signo
, *pid
);
241 if (siginfo
.ssi_code
== CLD_STOPPED
||
242 siginfo
.ssi_code
== CLD_CONTINUED
) {
243 INFO("container init process was stopped/continued");
247 /* more robustness, protect ourself from a SIGCHLD sent
248 * by a process different from the container init
250 if (siginfo
.ssi_pid
!= *pid
) {
251 WARN("invalid pid for SIGCHLD");
255 DEBUG("container init process exited");
259 int lxc_pid_callback(int fd
, struct lxc_request
*request
,
260 struct lxc_handler
*handler
)
262 struct lxc_answer answer
;
265 answer
.pid
= handler
->pid
;
268 ret
= send(fd
, &answer
, sizeof(answer
), 0);
270 WARN("failed to send answer to the peer");
274 if (ret
!= sizeof(answer
)) {
275 ERROR("partial answer sent");
282 int lxc_clone_flags_callback(int fd
, struct lxc_request
*request
,
283 struct lxc_handler
*handler
)
285 struct lxc_answer answer
;
289 answer
.ret
= handler
->clone_flags
;
291 ret
= send(fd
, &answer
, sizeof(answer
), 0);
293 WARN("failed to send answer to the peer");
297 if (ret
!= sizeof(answer
)) {
298 ERROR("partial answer sent");
305 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
307 handler
->state
= state
;
308 lxc_monitor_send_state(name
, state
);
312 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
314 int sigfd
= handler
->sigfd
;
315 int pid
= handler
->pid
;
316 struct lxc_epoll_descr descr
;
318 if (lxc_mainloop_open(&descr
)) {
319 ERROR("failed to create mainloop");
323 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
324 ERROR("failed to add handler for the signal");
325 goto out_mainloop_open
;
328 if (lxc_console_mainloop_add(&descr
, handler
)) {
329 ERROR("failed to add console handler to mainloop");
330 goto out_mainloop_open
;
333 if (lxc_command_mainloop_add(name
, &descr
, handler
)) {
334 ERROR("failed to add command handler to mainloop");
335 goto out_mainloop_open
;
338 if (handler
->conf
->need_utmp_watch
) {
339 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
340 ERROR("failed to add utmp handler to mainloop");
341 goto out_mainloop_open
;
345 return lxc_mainloop(&descr
);
348 lxc_mainloop_close(&descr
);
354 extern int lxc_caps_check(void);
356 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
)
358 struct lxc_handler
*handler
;
360 if (!lxc_caps_check()) {
361 ERROR("Not running with sufficient privilege");
365 handler
= malloc(sizeof(*handler
));
369 memset(handler
, 0, sizeof(*handler
));
371 handler
->conf
= conf
;
373 apparmor_handler_init(handler
);
374 handler
->name
= strdup(name
);
375 if (!handler
->name
) {
376 ERROR("failed to allocate memory");
380 if (lxc_command_init(name
, handler
))
383 if (lxc_read_seccomp_config(conf
) != 0) {
384 ERROR("failed loading seccomp policy");
385 goto out_close_maincmd_fd
;
388 /* Begin the set the state to STARTING*/
389 if (lxc_set_state(name
, handler
, STARTING
)) {
390 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
394 if (run_lxc_hooks(name
, "pre-start", conf
)) {
395 ERROR("failed to run pre-start hooks for container '%s'.", name
);
399 if (lxc_create_tty(name
, conf
)) {
400 ERROR("failed to create the ttys");
404 if (lxc_create_console(conf
)) {
405 ERROR("failed to create console");
409 /* the signal fd has to be created before forking otherwise
410 * if the child process exits before we setup the signal fd,
411 * the event will be lost and the command will be stuck */
412 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
413 if (handler
->sigfd
< 0) {
414 ERROR("failed to set sigchild fd handler");
415 goto out_delete_console
;
418 INFO("'%s' is initialized", name
);
422 lxc_delete_console(&conf
->console
);
424 lxc_delete_tty(&conf
->tty_info
);
426 lxc_set_state(name
, handler
, ABORTING
);
427 out_close_maincmd_fd
:
428 close(conf
->maincmd_fd
);
429 conf
->maincmd_fd
= -1;
432 handler
->name
= NULL
;
438 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
440 /* The STOPPING state is there for future cleanup code
441 * which can take awhile
443 lxc_set_state(name
, handler
, STOPPING
);
444 lxc_set_state(name
, handler
, STOPPED
);
446 if (run_lxc_hooks(name
, "post-stop", handler
->conf
))
447 ERROR("failed to run post-stop hooks for container '%s'.", name
);
449 /* reset mask set by setup_signal_fd */
450 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
451 WARN("failed to restore sigprocmask");
453 lxc_delete_console(&handler
->conf
->console
);
454 lxc_delete_tty(&handler
->conf
->tty_info
);
455 close(handler
->conf
->maincmd_fd
);
456 handler
->conf
->maincmd_fd
= -1;
461 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
463 lxc_set_state(name
, handler
, ABORTING
);
464 if (handler
->pid
> 0)
465 kill(handler
->pid
, SIGKILL
);
468 #include <sys/reboot.h>
469 #include <linux/reboot.h>
472 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
473 * in a child pid namespace if container reboot support exists.
474 * Otherwise, it will either succeed or return -EPERM.
476 static int container_reboot_supported(void *arg
)
482 if (ret
== -1 && errno
== EINVAL
)
487 static int must_drop_cap_sys_boot(void)
489 FILE *f
= fopen("/proc/sys/kernel/ctrl-alt-del", "r");
491 long stack_size
= 4096;
492 void *stack
= alloca(stack_size
) + stack_size
;
497 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
501 ret
= fscanf(f
, "%d", &v
);
504 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
507 cmd
= v
? LINUX_REBOOT_CMD_CAD_ON
: LINUX_REBOOT_CMD_CAD_OFF
;
509 pid
= clone(container_reboot_supported
, stack
, CLONE_NEWPID
| SIGCHLD
, &cmd
);
511 SYSERROR("failed to clone\n");
514 if (wait(&status
) < 0) {
515 SYSERROR("unexpected wait error: %m\n");
519 if (WEXITSTATUS(status
) != 1)
525 static int do_start(void *data
)
527 struct lxc_handler
*handler
= data
;
529 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
530 SYSERROR("failed to set sigprocmask");
534 /* This prctl must be before the synchro, so if the parent
535 * dies before we set the parent death signal, we will detect
536 * its death with the synchro right after, otherwise we have
537 * a window where the parent can exit before we set the pdeath
538 * signal leading to a unsupervized container.
540 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
541 SYSERROR("failed to set pdeath signal");
545 lxc_sync_fini_parent(handler
);
547 /* Tell the parent task it can begin to configure the
548 * container and wait for it to finish
550 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
553 if (handler
->conf
->need_utmp_watch
) {
554 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
555 SYSERROR("failed to remove CAP_SYS_BOOT capability");
558 DEBUG("Dropped cap_sys_boot\n");
561 /* Setup the container, ip, names, utsname, ... */
562 if (lxc_setup(handler
->name
, handler
->conf
)) {
563 ERROR("failed to setup the container");
564 goto out_warn_father
;
567 if (apparmor_load(handler
) < 0)
568 goto out_warn_father
;
570 if (lxc_seccomp_load(handler
->conf
) != 0)
571 goto out_warn_father
;
573 if (run_lxc_hooks(handler
->name
, "start", handler
->conf
)) {
574 ERROR("failed to run start hooks for container '%s'.", handler
->name
);
575 goto out_warn_father
;
578 close(handler
->sigfd
);
580 /* after this call, we are in error because this
581 * ops should not return as it execs */
582 if (handler
->ops
->start(handler
, handler
->data
))
586 lxc_sync_wake_parent(handler
, LXC_SYNC_POST_CONFIGURE
);
590 int save_phys_nics(struct lxc_conf
*conf
)
592 struct lxc_list
*iterator
;
594 lxc_list_for_each(iterator
, &conf
->network
) {
595 struct lxc_netdev
*netdev
= iterator
->elem
;
597 if (netdev
->type
!= LXC_NET_PHYS
)
599 conf
->saved_nics
= realloc(conf
->saved_nics
,
600 (conf
->num_savednics
+1)*sizeof(struct saved_nic
));
601 if (!conf
->saved_nics
) {
602 SYSERROR("failed to allocate memory");
605 conf
->saved_nics
[conf
->num_savednics
].ifindex
= netdev
->ifindex
;
606 conf
->saved_nics
[conf
->num_savednics
].orig_name
= strdup(netdev
->link
);
607 if (!conf
->saved_nics
[conf
->num_savednics
].orig_name
) {
608 SYSERROR("failed to allocate memory");
611 INFO("stored saved_nic #%d idx %d name %s\n", conf
->num_savednics
,
612 conf
->saved_nics
[conf
->num_savednics
].ifindex
,
613 conf
->saved_nics
[conf
->num_savednics
].orig_name
);
614 conf
->num_savednics
++;
621 int lxc_spawn(struct lxc_handler
*handler
)
623 int failed_before_rename
= 0;
624 const char *name
= handler
->name
;
627 if (lxc_sync_init(handler
))
630 handler
->clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
631 if (!lxc_list_empty(&handler
->conf
->network
)) {
633 handler
->clone_flags
|= CLONE_NEWNET
;
635 /* Find gateway addresses from the link device, which is
636 * no longer accessible inside the container. Do this
637 * before creating network interfaces, since goto
638 * out_delete_net does not work before lxc_clone. */
639 if (lxc_find_gateway_addresses(handler
)) {
640 ERROR("failed to find gateway addresses");
641 lxc_sync_fini(handler
);
645 /* that should be done before the clone because we will
646 * fill the netdev index and use them in the child
648 if (lxc_create_network(handler
)) {
649 ERROR("failed to create the network");
650 lxc_sync_fini(handler
);
655 if (save_phys_nics(handler
->conf
)) {
656 ERROR("failed to save physical nic info");
661 * if the rootfs is not a blockdev, prevent the container from
662 * marking it readonly.
665 pinfd
= pin_rootfs(handler
->conf
->rootfs
.path
);
667 ERROR("failed to pin the container's rootfs");
671 /* Create a process in a new set of namespaces */
672 handler
->pid
= lxc_clone(do_start
, handler
, handler
->clone_flags
);
673 if (handler
->pid
< 0) {
674 SYSERROR("failed to fork into a new namespace");
678 lxc_sync_fini_child(handler
);
680 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
681 failed_before_rename
= 1;
683 if (lxc_cgroup_create(name
, handler
->pid
))
686 if (failed_before_rename
)
689 /* Create the network configuration */
690 if (handler
->clone_flags
& CLONE_NEWNET
) {
691 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
692 ERROR("failed to create the configured network");
697 /* Tell the child to continue its initialization and wait for
698 * it to exec or return an error
700 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
703 if (handler
->ops
->post_start(handler
, handler
->data
))
706 if (lxc_set_state(name
, handler
, RUNNING
)) {
707 ERROR("failed to set state to %s",
708 lxc_state2str(RUNNING
));
712 lxc_sync_fini(handler
);
720 if (handler
->clone_flags
& CLONE_NEWNET
)
721 lxc_delete_network(handler
);
723 lxc_abort(name
, handler
);
724 lxc_sync_fini(handler
);
728 int __lxc_start(const char *name
, struct lxc_conf
*conf
,
729 struct lxc_operations
* ops
, void *data
)
731 struct lxc_handler
*handler
;
735 handler
= lxc_init(name
, conf
);
737 ERROR("failed to initialize the container");
741 handler
->data
= data
;
743 if (must_drop_cap_sys_boot()) {
744 DEBUG("Dropping cap_sys_boot\n");
746 DEBUG("Not dropping cap_sys_boot or watching utmp\n");
747 handler
->conf
->need_utmp_watch
= 0;
750 err
= lxc_spawn(handler
);
752 ERROR("failed to spawn '%s'", name
);
756 err
= lxc_poll(name
, handler
);
758 ERROR("mainloop exited with an error");
762 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
766 * If the child process exited but was not signaled,
767 * it didn't call reboot. This should mean it was an
768 * lxc-execute which simply exited. In any case, treat
771 if (WIFSIGNALED(status
)) {
772 switch(WTERMSIG(status
)) {
773 case SIGINT
: /* halt */
774 DEBUG("Container halting");
776 case SIGHUP
: /* reboot */
777 DEBUG("Container rebooting");
778 handler
->conf
->reboot
= 1;
781 DEBUG("unknown exit status for init: %d\n", WTERMSIG(status
));
786 lxc_rename_phys_nics_on_shutdown(handler
->conf
);
788 err
= lxc_error_set_and_log(handler
->pid
, status
);
790 lxc_delete_network(handler
);
793 lxc_cgroup_destroy(name
);
794 lxc_fini(name
, handler
);
798 lxc_abort(name
, handler
);
806 static int start(struct lxc_handler
*handler
, void* data
)
808 struct start_args
*arg
= data
;
810 NOTICE("exec'ing '%s'", arg
->argv
[0]);
812 execvp(arg
->argv
[0], arg
->argv
);
813 SYSERROR("failed to exec %s", arg
->argv
[0]);
817 static int post_start(struct lxc_handler
*handler
, void* data
)
819 struct start_args
*arg
= data
;
821 NOTICE("'%s' started with pid '%d'", arg
->argv
[0], handler
->pid
);
825 static struct lxc_operations start_ops
= {
827 .post_start
= post_start
830 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
832 struct start_args start_arg
= {
836 if (lxc_check_inherited(conf
, -1))
839 conf
->need_utmp_watch
= 1;
840 return __lxc_start(name
, conf
, &start_ops
, &start_arg
);