]>
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
;
229 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
231 ERROR("failed to read sigchld info");
235 if (ret
!= sizeof(siginfo
)) {
236 ERROR("unexpected siginfo size");
240 if (siginfo
.ssi_code
== CLD_STOPPED
||
241 siginfo
.ssi_code
== CLD_CONTINUED
) {
242 INFO("container init process was stopped/continued");
246 DEBUG("container init process exited");
250 int lxc_pid_callback(int fd
, struct lxc_request
*request
,
251 struct lxc_handler
*handler
)
253 struct lxc_answer answer
;
256 answer
.pid
= handler
->pid
;
259 ret
= send(fd
, &answer
, sizeof(answer
), 0);
261 WARN("failed to send answer to the peer");
265 if (ret
!= sizeof(answer
)) {
266 ERROR("partial answer sent");
273 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
275 handler
->state
= state
;
276 lxc_monitor_send_state(name
, state
);
280 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
282 int sigfd
= handler
->sigfd
;
283 int pid
= handler
->pid
;
284 struct lxc_epoll_descr descr
;
286 if (lxc_mainloop_open(&descr
)) {
287 ERROR("failed to create mainloop");
291 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
292 ERROR("failed to add handler for the signal");
293 goto out_mainloop_open
;
296 if (lxc_console_mainloop_add(&descr
, handler
)) {
297 ERROR("failed to add console handler to mainloop");
298 goto out_mainloop_open
;
301 if (lxc_command_mainloop_add(name
, &descr
, handler
)) {
302 ERROR("failed to add command handler to mainloop");
303 goto out_mainloop_open
;
306 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
307 ERROR("failed to add utmp handler to mainloop");
308 goto out_mainloop_open
;
311 return lxc_mainloop(&descr
);
314 lxc_mainloop_close(&descr
);
320 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
)
322 struct lxc_handler
*handler
;
324 handler
= malloc(sizeof(*handler
));
328 memset(handler
, 0, sizeof(*handler
));
330 handler
->conf
= conf
;
332 handler
->name
= strdup(name
);
333 if (!handler
->name
) {
334 ERROR("failed to allocate memory");
338 /* Begin the set the state to STARTING*/
339 if (lxc_set_state(name
, handler
, STARTING
)) {
340 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
344 if (lxc_create_tty(name
, conf
)) {
345 ERROR("failed to create the ttys");
349 if (lxc_create_console(conf
)) {
350 ERROR("failed to create console");
354 /* the signal fd has to be created before forking otherwise
355 * if the child process exits before we setup the signal fd,
356 * the event will be lost and the command will be stuck */
357 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
358 if (handler
->sigfd
< 0) {
359 ERROR("failed to set sigchild fd handler");
360 goto out_delete_console
;
363 /* Avoid signals from terminal */
364 LXC_TTY_ADD_HANDLER(SIGINT
);
365 LXC_TTY_ADD_HANDLER(SIGQUIT
);
367 INFO("'%s' is initialized", name
);
371 lxc_delete_console(&conf
->console
);
373 lxc_delete_tty(&conf
->tty_info
);
375 lxc_set_state(name
, handler
, ABORTING
);
378 handler
->name
= NULL
;
384 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
386 /* The STOPPING state is there for future cleanup code
387 * which can take awhile
389 lxc_set_state(name
, handler
, STOPPING
);
390 lxc_set_state(name
, handler
, STOPPED
);
392 lxc_delete_console(&handler
->conf
->console
);
393 lxc_delete_tty(&handler
->conf
->tty_info
);
397 LXC_TTY_DEL_HANDLER(SIGQUIT
);
398 LXC_TTY_DEL_HANDLER(SIGINT
);
401 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
403 lxc_set_state(name
, handler
, ABORTING
);
404 if (handler
->pid
> 0)
405 kill(handler
->pid
, SIGKILL
);
411 struct lxc_handler
*handler
;
415 static int do_start(void *arg
)
417 struct start_arg
*start_arg
= arg
;
418 struct lxc_handler
*handler
= start_arg
->handler
;
419 const char *name
= start_arg
->name
;
420 char *const *argv
= start_arg
->argv
;
421 int *sv
= start_arg
->sv
;
424 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
425 SYSERROR("failed to set sigprocmask");
431 /* Be sure we don't inherit this after the exec */
432 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
434 /* Tell our father he can begin to configure the container */
435 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
436 SYSERROR("failed to write socket");
440 /* Wait for the father to finish the configuration */
441 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
442 SYSERROR("failed to read socket");
446 /* Setup the container, ip, names, utsname, ... */
447 if (lxc_setup(name
, handler
->conf
)) {
448 ERROR("failed to setup the container");
449 goto out_warn_father
;
452 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
453 SYSERROR("failed to remove CAP_SYS_BOOT capability");
457 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
458 SYSERROR("failed to set pdeath signal");
462 NOTICE("exec'ing '%s'", argv
[0]);
464 execvp(argv
[0], argv
);
465 SYSERROR("failed to exec %s", argv
[0]);
468 /* If the exec fails, tell that to our father */
469 if (write(sv
[0], &err
, sizeof(err
)) < 0)
470 SYSERROR("failed to write the socket");
474 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
479 int failed_before_rename
= 0;
481 struct start_arg start_arg
= {
488 /* Synchro socketpair */
489 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
490 SYSERROR("failed to create communication socketpair");
494 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
495 if (!lxc_list_empty(&handler
->conf
->network
)) {
497 clone_flags
|= CLONE_NEWNET
;
499 /* that should be done before the clone because we will
500 * fill the netdev index and use them in the child
502 if (lxc_create_network(&handler
->conf
->network
)) {
503 ERROR("failed to create the network");
508 /* Create a process in a new set of namespaces */
509 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
510 if (handler
->pid
< 0) {
511 SYSERROR("failed to fork into a new namespace");
517 /* Wait for the child to be ready */
518 if (read(sv
[1], &sync
, sizeof(sync
)) <= 0) {
519 ERROR("sync read failure : %m");
520 failed_before_rename
= 1;
523 if (lxc_rename_nsgroup(name
, handler
))
526 if (failed_before_rename
)
529 /* Create the network configuration */
530 if (clone_flags
& CLONE_NEWNET
) {
531 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
532 ERROR("failed to create the configured network");
537 /* Tell the child to continue its initialization */
538 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
539 SYSERROR("failed to write the socket");
543 /* Wait for the child to exec or returning an error */
544 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
545 ERROR("failed to read the socket");
549 if (lxc_set_state(name
, handler
, RUNNING
)) {
550 ERROR("failed to set state to %s",
551 lxc_state2str(RUNNING
));
557 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
565 if (clone_flags
& CLONE_NEWNET
)
566 lxc_delete_network(&handler
->conf
->network
);
568 lxc_abort(name
, handler
);
573 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
575 struct lxc_handler
*handler
;
579 if (lxc_check_inherited(-1))
582 handler
= lxc_init(name
, conf
);
584 ERROR("failed to initialize the container");
588 err
= lxc_spawn(name
, handler
, argv
);
590 ERROR("failed to spawn '%s'", argv
[0]);
594 /* no need of other inherited fds but stderr */
595 close(fileno(stdin
));
596 close(fileno(stdout
));
598 err
= lxc_poll(name
, handler
);
600 ERROR("mainloop exited with an error");
604 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
607 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
608 WARN("failed to restore sigprocmask");
610 err
= lxc_error_set_and_log(handler
->pid
, status
);
612 lxc_unlink_nsgroup(name
);
613 lxc_fini(name
, handler
);
617 lxc_abort(name
, handler
);