]>
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 <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
24 #include "../config.h"
35 #include <namespace.h>
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"
129 lxc_log_define(lxc_start
, lxc
);
131 LXC_TTY_HANDLER(SIGINT
);
132 LXC_TTY_HANDLER(SIGQUIT
);
134 static int match_fd(int fd
)
136 return (fd
== 0 || fd
== 1 || fd
== 2);
139 int lxc_check_inherited(int fd_to_ignore
)
141 struct dirent dirent
, *direntp
;
145 dir
= opendir("/proc/self/fd");
147 WARN("failed to open directory: %m");
153 while (!readdir_r(dir
, &dirent
, &direntp
)) {
157 if (!strcmp(direntp
->d_name
, "."))
160 if (!strcmp(direntp
->d_name
, ".."))
163 fd
= atoi(direntp
->d_name
);
165 if (fd
== fddir
|| fd
== lxc_log_fd
|| fd
== fd_to_ignore
)
171 WARN("inherited fd %d", fd
);
174 closedir(dir
); /* cannot fail */
178 static int setup_signal_fd(sigset_t
*oldmask
)
183 /* Block everything except serious error signals */
184 if (sigfillset(&mask
) ||
185 sigdelset(&mask
, SIGILL
) ||
186 sigdelset(&mask
, SIGSEGV
) ||
187 sigdelset(&mask
, SIGBUS
) ||
188 sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
189 SYSERROR("failed to set signal mask");
193 fd
= signalfd(-1, &mask
, 0);
195 SYSERROR("failed to create the signal fd");
199 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
200 SYSERROR("failed to set sigfd to close-on-exec");
205 DEBUG("sigchild handler set");
210 static int signal_handler(int fd
, void *data
,
211 struct lxc_epoll_descr
*descr
)
213 struct signalfd_siginfo siginfo
;
217 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
219 ERROR("failed to read signal info");
223 if (ret
!= sizeof(siginfo
)) {
224 ERROR("unexpected siginfo size");
228 if (siginfo
.ssi_signo
!= SIGCHLD
) {
229 kill(*pid
, siginfo
.ssi_signo
);
230 INFO("forwarded signal %d to pid %d", siginfo
.ssi_signo
, *pid
);
234 if (siginfo
.ssi_code
== CLD_STOPPED
||
235 siginfo
.ssi_code
== CLD_CONTINUED
) {
236 INFO("container init process was stopped/continued");
240 /* more robustness, protect ourself from a SIGCHLD sent
241 * by a process different from the container init
243 if (siginfo
.ssi_pid
!= *pid
) {
244 WARN("invalid pid for SIGCHLD");
248 DEBUG("container init process exited");
252 int lxc_pid_callback(int fd
, struct lxc_request
*request
,
253 struct lxc_handler
*handler
)
255 struct lxc_answer answer
;
258 answer
.pid
= handler
->pid
;
261 ret
= send(fd
, &answer
, sizeof(answer
), 0);
263 WARN("failed to send answer to the peer");
267 if (ret
!= sizeof(answer
)) {
268 ERROR("partial answer sent");
275 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
277 handler
->state
= state
;
278 lxc_monitor_send_state(name
, state
);
282 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
284 int sigfd
= handler
->sigfd
;
285 int pid
= handler
->pid
;
286 struct lxc_epoll_descr descr
;
288 if (lxc_mainloop_open(&descr
)) {
289 ERROR("failed to create mainloop");
293 if (lxc_mainloop_add_handler(&descr
, sigfd
, signal_handler
, &pid
)) {
294 ERROR("failed to add handler for the signal");
295 goto out_mainloop_open
;
298 if (lxc_console_mainloop_add(&descr
, handler
)) {
299 ERROR("failed to add console handler to mainloop");
300 goto out_mainloop_open
;
303 if (lxc_command_mainloop_add(name
, &descr
, handler
)) {
304 ERROR("failed to add command handler to mainloop");
305 goto out_mainloop_open
;
308 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
309 ERROR("failed to add utmp handler to mainloop");
310 goto out_mainloop_open
;
313 return lxc_mainloop(&descr
);
316 lxc_mainloop_close(&descr
);
322 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
)
324 struct lxc_handler
*handler
;
326 handler
= malloc(sizeof(*handler
));
330 memset(handler
, 0, sizeof(*handler
));
332 handler
->conf
= conf
;
334 handler
->name
= strdup(name
);
335 if (!handler
->name
) {
336 ERROR("failed to allocate memory");
340 /* Begin the set the state to STARTING*/
341 if (lxc_set_state(name
, handler
, STARTING
)) {
342 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
346 if (lxc_create_tty(name
, conf
)) {
347 ERROR("failed to create the ttys");
351 if (lxc_create_console(conf
)) {
352 ERROR("failed to create console");
356 /* the signal fd has to be created before forking otherwise
357 * if the child process exits before we setup the signal fd,
358 * the event will be lost and the command will be stuck */
359 handler
->sigfd
= setup_signal_fd(&handler
->oldmask
);
360 if (handler
->sigfd
< 0) {
361 ERROR("failed to set sigchild fd handler");
362 goto out_delete_console
;
365 INFO("'%s' is initialized", name
);
369 lxc_delete_console(&conf
->console
);
371 lxc_delete_tty(&conf
->tty_info
);
373 lxc_set_state(name
, handler
, ABORTING
);
376 handler
->name
= NULL
;
382 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
384 /* The STOPPING state is there for future cleanup code
385 * which can take awhile
387 lxc_set_state(name
, handler
, STOPPING
);
388 lxc_set_state(name
, handler
, STOPPED
);
390 /* reset mask set by setup_signal_fd */
391 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
392 WARN("failed to restore sigprocmask");
394 lxc_delete_console(&handler
->conf
->console
);
395 lxc_delete_tty(&handler
->conf
->tty_info
);
400 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
402 lxc_set_state(name
, handler
, ABORTING
);
403 if (handler
->pid
> 0)
404 kill(handler
->pid
, SIGKILL
);
407 static int do_start(void *data
)
409 struct lxc_handler
*handler
= data
;
411 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
412 SYSERROR("failed to set sigprocmask");
416 /* This prctl must be before the synchro, so if the parent
417 * dies before we set the parent death signal, we will detect
418 * its death with the synchro right after, otherwise we have
419 * a window where the parent can exit before we set the pdeath
420 * signal leading to a unsupervized container.
422 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
423 SYSERROR("failed to set pdeath signal");
427 lxc_sync_fini_parent(handler
);
429 /* Tell the parent task it can begin to configure the
430 * container and wait for it to finish
432 if (lxc_sync_barrier_parent(handler
, LXC_SYNC_CONFIGURE
))
435 /* Setup the container, ip, names, utsname, ... */
436 if (lxc_setup(handler
->name
, handler
->conf
)) {
437 ERROR("failed to setup the container");
438 goto out_warn_father
;
441 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
442 SYSERROR("failed to remove CAP_SYS_BOOT capability");
446 close(handler
->sigfd
);
448 /* after this call, we are in error because this
449 * ops should not return as it execs */
450 if (handler
->ops
->start(handler
, handler
->data
))
454 lxc_sync_wake_parent(handler
, LXC_SYNC_POST_CONFIGURE
);
458 int lxc_spawn(struct lxc_handler
*handler
)
461 int failed_before_rename
= 0;
462 const char *name
= handler
->name
;
464 if (lxc_sync_init(handler
))
467 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
468 if (!lxc_list_empty(&handler
->conf
->network
)) {
470 clone_flags
|= CLONE_NEWNET
;
472 /* Find gateway addresses from the link device, which is
473 * no longer accessible inside the container. Do this
474 * before creating network interfaces, since goto
475 * out_delete_net does not work before lxc_clone. */
476 if (lxc_find_gateway_addresses(handler
)) {
477 ERROR("failed to find gateway addresses");
478 lxc_sync_fini(handler
);
482 /* that should be done before the clone because we will
483 * fill the netdev index and use them in the child
485 if (lxc_create_network(handler
)) {
486 ERROR("failed to create the network");
487 lxc_sync_fini(handler
);
493 /* Create a process in a new set of namespaces */
494 handler
->pid
= lxc_clone(do_start
, handler
, clone_flags
);
495 if (handler
->pid
< 0) {
496 SYSERROR("failed to fork into a new namespace");
500 lxc_sync_fini_child(handler
);
502 if (lxc_sync_wait_child(handler
, LXC_SYNC_CONFIGURE
))
503 failed_before_rename
= 1;
505 if (lxc_cgroup_create(name
, handler
->pid
))
508 if (failed_before_rename
)
511 /* Create the network configuration */
512 if (clone_flags
& CLONE_NEWNET
) {
513 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
514 ERROR("failed to create the configured network");
519 /* Tell the child to continue its initialization and wait for
520 * it to exec or return an error
522 if (lxc_sync_barrier_child(handler
, LXC_SYNC_POST_CONFIGURE
))
525 if (handler
->ops
->post_start(handler
, handler
->data
))
528 if (lxc_set_state(name
, handler
, RUNNING
)) {
529 ERROR("failed to set state to %s",
530 lxc_state2str(RUNNING
));
534 lxc_sync_fini(handler
);
538 if (clone_flags
& CLONE_NEWNET
)
539 lxc_delete_network(&handler
->conf
->network
);
541 lxc_abort(name
, handler
);
542 lxc_sync_fini(handler
);
546 int __lxc_start(const char *name
, struct lxc_conf
*conf
,
547 struct lxc_operations
* ops
, void *data
)
549 struct lxc_handler
*handler
;
553 handler
= lxc_init(name
, conf
);
555 ERROR("failed to initialize the container");
559 handler
->data
= data
;
561 err
= lxc_spawn(handler
);
563 ERROR("failed to spawn '%s'", name
);
567 /* Avoid signals from terminal */
568 LXC_TTY_ADD_HANDLER(SIGINT
);
569 LXC_TTY_ADD_HANDLER(SIGQUIT
);
571 err
= lxc_poll(name
, handler
);
573 ERROR("mainloop exited with an error");
577 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
580 err
= lxc_error_set_and_log(handler
->pid
, status
);
582 LXC_TTY_DEL_HANDLER(SIGQUIT
);
583 LXC_TTY_DEL_HANDLER(SIGINT
);
584 lxc_cgroup_destroy(name
);
585 lxc_fini(name
, handler
);
589 lxc_abort(name
, handler
);
597 static int start(struct lxc_handler
*handler
, void* data
)
599 struct start_args
*arg
= data
;
601 NOTICE("exec'ing '%s'", arg
->argv
[0]);
603 execvp(arg
->argv
[0], arg
->argv
);
604 SYSERROR("failed to exec %s", arg
->argv
[0]);
608 static int post_start(struct lxc_handler
*handler
, void* data
)
610 struct start_args
*arg
= data
;
612 NOTICE("'%s' started with pid '%d'", arg
->argv
[0], handler
->pid
);
616 static struct lxc_operations start_ops
= {
618 .post_start
= post_start
621 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
623 struct start_args start_arg
= {
627 if (lxc_check_inherited(-1))
630 return __lxc_start(name
, conf
, &start_ops
, &start_arg
);