]>
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 */
52 struct signalfd_siginfo
73 # ifndef __NR_signalfd4
74 /* assume kernel headers are too old */
76 # define __NR_signalfd4 327
78 # define __NR_signalfd4 289
80 # define __NR_signalfd4 313
82 # define __NR_signalfd4 322
86 # ifndef __NR_signalfd
87 /* assume kernel headers are too old */
89 # define __NR_signalfd 321
91 # define __NR_signalfd 282
93 # define __NR_signalfd 305
95 # define __NR_signalfd 316
99 int signalfd(int fd
, const sigset_t
*mask
, int flags
)
103 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
104 if (errno
== ENOSYS
&& flags
== 0)
105 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
110 #if !HAVE_DECL_PR_CAPBSET_DROP
111 #define PR_CAPBSET_DROP 24
121 #include "mainloop.h"
125 #include "commands.h"
128 lxc_log_define(lxc_start
, lxc
);
130 LXC_TTY_HANDLER(SIGINT
);
131 LXC_TTY_HANDLER(SIGQUIT
);
133 static int match_fd(int fd
)
135 return (fd
== 0 || fd
== 1 || fd
== 2);
138 int lxc_check_inherited(int fd_to_ignore
)
140 struct dirent dirent
, *direntp
;
145 dir
= opendir("/proc/self/fd");
147 WARN("failed to open directory: %m");
153 while (!readdir_r(dir
, &dirent
, &direntp
)) {
160 if (!strcmp(direntp
->d_name
, "."))
163 if (!strcmp(direntp
->d_name
, ".."))
166 fd
= atoi(direntp
->d_name
);
168 if (fd
== fddir
|| fd
== lxc_log_fd
|| fd
== fd_to_ignore
)
178 snprintf(procpath
, sizeof(procpath
), "/proc/self/fd/%d", fd
);
180 if (readlink(procpath
, path
, sizeof(path
)) == -1)
181 ERROR("readlink(%s) failed : %m", procpath
);
183 ERROR("inherited fd %d on %s", fd
, path
);
187 ERROR("failed to close directory");
191 static int setup_sigchld_fd(sigset_t
*oldmask
)
196 if (sigprocmask(SIG_BLOCK
, NULL
, &mask
)) {
197 SYSERROR("failed to get mask signal");
201 if (sigaddset(&mask
, SIGCHLD
) || sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
202 SYSERROR("failed to set mask signal");
206 fd
= signalfd(-1, &mask
, 0);
208 SYSERROR("failed to create the signal fd");
212 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
213 SYSERROR("failed to set sigfd to close-on-exec");
218 DEBUG("sigchild handler set");
223 static int sigchld_handler(int fd
, void *data
,
224 struct lxc_epoll_descr
*descr
)
226 struct signalfd_siginfo siginfo
;
230 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
232 ERROR("failed to read sigchld info");
236 if (ret
!= sizeof(siginfo
)) {
237 ERROR("unexpected siginfo size");
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_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
284 handler
->state
= state
;
285 lxc_monitor_send_state(name
, state
);
289 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
291 int sigfd
= handler
->sigfd
;
292 int pid
= handler
->pid
;
293 struct lxc_epoll_descr descr
;
295 if (lxc_mainloop_open(&descr
)) {
296 ERROR("failed to create mainloop");
300 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
301 ERROR("failed to add handler for the signal");
302 goto out_mainloop_open
;
305 if (lxc_console_mainloop_add(&descr
, handler
)) {
306 ERROR("failed to add console handler to mainloop");
307 goto out_mainloop_open
;
310 if (lxc_command_mainloop_add(name
, &descr
, handler
)) {
311 ERROR("failed to add command handler to mainloop");
312 goto out_mainloop_open
;
315 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
316 ERROR("failed to add utmp handler to mainloop");
317 goto out_mainloop_open
;
320 return lxc_mainloop(&descr
);
323 lxc_mainloop_close(&descr
);
329 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
)
331 struct lxc_handler
*handler
;
333 handler
= malloc(sizeof(*handler
));
337 memset(handler
, 0, sizeof(*handler
));
339 handler
->conf
= conf
;
341 handler
->name
= strdup(name
);
342 if (!handler
->name
) {
343 ERROR("failed to allocate memory");
347 /* Begin the set the state to STARTING*/
348 if (lxc_set_state(name
, handler
, STARTING
)) {
349 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
353 if (lxc_create_tty(name
, conf
)) {
354 ERROR("failed to create the ttys");
358 if (lxc_create_console(conf
)) {
359 ERROR("failed to create console");
363 /* the signal fd has to be created before forking otherwise
364 * if the child process exits before we setup the signal fd,
365 * the event will be lost and the command will be stuck */
366 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
367 if (handler
->sigfd
< 0) {
368 ERROR("failed to set sigchild fd handler");
369 goto out_delete_console
;
372 /* Avoid signals from terminal */
373 LXC_TTY_ADD_HANDLER(SIGINT
);
374 LXC_TTY_ADD_HANDLER(SIGQUIT
);
376 INFO("'%s' is initialized", name
);
380 lxc_delete_console(&conf
->console
);
382 lxc_delete_tty(&conf
->tty_info
);
384 lxc_set_state(name
, handler
, ABORTING
);
387 handler
->name
= NULL
;
393 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
395 /* The STOPPING state is there for future cleanup code
396 * which can take awhile
398 lxc_set_state(name
, handler
, STOPPING
);
399 lxc_set_state(name
, handler
, STOPPED
);
401 lxc_delete_console(&handler
->conf
->console
);
402 lxc_delete_tty(&handler
->conf
->tty_info
);
406 LXC_TTY_DEL_HANDLER(SIGQUIT
);
407 LXC_TTY_DEL_HANDLER(SIGINT
);
410 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
412 lxc_set_state(name
, handler
, ABORTING
);
413 if (handler
->pid
> 0)
414 kill(handler
->pid
, SIGKILL
);
420 struct lxc_handler
*handler
;
424 static int do_start(void *arg
)
426 struct start_arg
*start_arg
= arg
;
427 struct lxc_handler
*handler
= start_arg
->handler
;
428 const char *name
= start_arg
->name
;
429 char *const *argv
= start_arg
->argv
;
430 int *sv
= start_arg
->sv
;
433 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
434 SYSERROR("failed to set sigprocmask");
440 /* Be sure we don't inherit this after the exec */
441 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
443 /* Tell our father he can begin to configure the container */
444 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
445 SYSERROR("failed to write socket");
449 /* Wait for the father to finish the configuration */
450 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
451 SYSERROR("failed to read socket");
455 /* Setup the container, ip, names, utsname, ... */
456 if (lxc_setup(name
, handler
->conf
)) {
457 ERROR("failed to setup the container");
458 goto out_warn_father
;
461 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
462 SYSERROR("failed to remove CAP_SYS_BOOT capability");
466 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
467 SYSERROR("failed to set pdeath signal");
471 NOTICE("exec'ing '%s'", argv
[0]);
473 execvp(argv
[0], argv
);
474 SYSERROR("failed to exec %s", argv
[0]);
477 /* If the exec fails, tell that to our father */
478 if (write(sv
[0], &err
, sizeof(err
)) < 0)
479 SYSERROR("failed to write the socket");
483 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
488 int failed_before_rename
= 0;
490 struct start_arg start_arg
= {
497 /* Synchro socketpair */
498 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
499 SYSERROR("failed to create communication socketpair");
503 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
504 if (!lxc_list_empty(&handler
->conf
->network
)) {
506 clone_flags
|= CLONE_NEWNET
;
508 /* that should be done before the clone because we will
509 * fill the netdev index and use them in the child
511 if (lxc_create_network(&handler
->conf
->network
)) {
512 ERROR("failed to create the network");
517 /* Create a process in a new set of namespaces */
518 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
519 if (handler
->pid
< 0) {
520 SYSERROR("failed to fork into a new namespace");
526 /* Wait for the child to be ready */
527 if (read(sv
[1], &sync
, sizeof(sync
)) <= 0) {
528 ERROR("sync read failure : %m");
529 failed_before_rename
= 1;
532 if (lxc_rename_nsgroup(name
, handler
))
535 if (failed_before_rename
)
538 /* Create the network configuration */
539 if (clone_flags
& CLONE_NEWNET
) {
540 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
541 ERROR("failed to create the configured network");
546 /* Tell the child to continue its initialization */
547 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
548 SYSERROR("failed to write the socket");
552 /* Wait for the child to exec or returning an error */
553 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
554 ERROR("failed to read the socket");
558 if (lxc_set_state(name
, handler
, RUNNING
)) {
559 ERROR("failed to set state to %s",
560 lxc_state2str(RUNNING
));
566 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
574 if (clone_flags
& CLONE_NEWNET
)
575 lxc_delete_network(&handler
->conf
->network
);
577 lxc_abort(name
, handler
);
582 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
584 struct lxc_handler
*handler
;
588 if (lxc_check_inherited(-1))
591 handler
= lxc_init(name
, conf
);
593 ERROR("failed to initialize the container");
597 err
= lxc_spawn(name
, handler
, argv
);
599 ERROR("failed to spawn '%s'", argv
[0]);
603 /* no need of other inherited fds but stderr */
604 close(fileno(stdin
));
605 close(fileno(stdout
));
607 err
= lxc_poll(name
, handler
);
609 ERROR("mainloop exited with an error");
613 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
616 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
617 WARN("failed to restore sigprocmask");
619 err
= lxc_error_set_and_log(handler
->pid
, status
);
621 lxc_unlink_nsgroup(name
);
622 lxc_fini(name
, handler
);
626 lxc_abort(name
, handler
);