]>
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 /* reset mask set by setup_sigchld_fd */
402 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
403 WARN("failed to restore sigprocmask");
405 lxc_delete_console(&handler
->conf
->console
);
406 lxc_delete_tty(&handler
->conf
->tty_info
);
410 LXC_TTY_DEL_HANDLER(SIGQUIT
);
411 LXC_TTY_DEL_HANDLER(SIGINT
);
414 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
416 lxc_set_state(name
, handler
, ABORTING
);
417 if (handler
->pid
> 0)
418 kill(handler
->pid
, SIGKILL
);
424 struct lxc_handler
*handler
;
428 static int do_start(void *arg
)
430 struct start_arg
*start_arg
= arg
;
431 struct lxc_handler
*handler
= start_arg
->handler
;
432 const char *name
= start_arg
->name
;
433 char *const *argv
= start_arg
->argv
;
434 int *sv
= start_arg
->sv
;
437 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
438 SYSERROR("failed to set sigprocmask");
444 /* Be sure we don't inherit this after the exec */
445 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
447 /* Tell our father he can begin to configure the container */
448 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
449 SYSERROR("failed to write socket");
453 /* Wait for the father to finish the configuration */
454 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
455 SYSERROR("failed to read socket");
459 /* Setup the container, ip, names, utsname, ... */
460 if (lxc_setup(name
, handler
->conf
)) {
461 ERROR("failed to setup the container");
462 goto out_warn_father
;
465 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
466 SYSERROR("failed to remove CAP_SYS_BOOT capability");
470 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
471 SYSERROR("failed to set pdeath signal");
475 NOTICE("exec'ing '%s'", argv
[0]);
477 execvp(argv
[0], argv
);
478 SYSERROR("failed to exec %s", argv
[0]);
481 /* If the exec fails, tell that to our father */
482 if (write(sv
[0], &err
, sizeof(err
)) < 0)
483 SYSERROR("failed to write the socket");
487 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
492 int failed_before_rename
= 0;
494 struct start_arg start_arg
= {
501 /* Synchro socketpair */
502 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
503 SYSERROR("failed to create communication socketpair");
507 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
508 if (!lxc_list_empty(&handler
->conf
->network
)) {
510 clone_flags
|= CLONE_NEWNET
;
512 /* that should be done before the clone because we will
513 * fill the netdev index and use them in the child
515 if (lxc_create_network(&handler
->conf
->network
)) {
516 ERROR("failed to create the network");
521 /* Create a process in a new set of namespaces */
522 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
523 if (handler
->pid
< 0) {
524 SYSERROR("failed to fork into a new namespace");
530 /* Wait for the child to be ready */
531 if (read(sv
[1], &sync
, sizeof(sync
)) <= 0) {
532 ERROR("sync read failure : %m");
533 failed_before_rename
= 1;
536 if (lxc_rename_nsgroup(name
, handler
))
539 if (failed_before_rename
)
542 /* Create the network configuration */
543 if (clone_flags
& CLONE_NEWNET
) {
544 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
545 ERROR("failed to create the configured network");
550 /* Tell the child to continue its initialization */
551 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
552 SYSERROR("failed to write the socket");
556 /* Wait for the child to exec or returning an error */
557 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
558 ERROR("failed to read the socket");
562 if (lxc_set_state(name
, handler
, RUNNING
)) {
563 ERROR("failed to set state to %s",
564 lxc_state2str(RUNNING
));
570 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
578 if (clone_flags
& CLONE_NEWNET
)
579 lxc_delete_network(&handler
->conf
->network
);
581 lxc_abort(name
, handler
);
586 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
588 struct lxc_handler
*handler
;
592 if (lxc_check_inherited(-1))
595 handler
= lxc_init(name
, conf
);
597 ERROR("failed to initialize the container");
601 err
= lxc_spawn(name
, handler
, argv
);
603 ERROR("failed to spawn '%s'", argv
[0]);
607 err
= lxc_poll(name
, handler
);
609 ERROR("mainloop exited with an error");
613 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
616 err
= lxc_error_set_and_log(handler
->pid
, status
);
618 lxc_unlink_nsgroup(name
);
619 lxc_fini(name
, handler
);
623 lxc_abort(name
, handler
);