]>
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 # ifndef __NR_signalfd4
52 /* assume kernel headers are too old */
54 # define __NR_signalfd4 327
56 # define __NR_signalfd4 289
58 # define __NR_signalfd4 313
60 # define __NR_signalfd4 322
64 # ifndef __NR_signalfd
65 /* assume kernel headers are too old */
67 # define __NR_signalfd 321
69 # define __NR_signalfd 282
71 # define __NR_signalfd 305
73 # define __NR_signalfd 316
77 int signalfd(int fd
, const sigset_t
*mask
, int flags
)
81 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
82 if (errno
== ENOSYS
&& flags
== 0)
83 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
88 #if !HAVE_DECL_PR_CAPBSET_DROP
89 #define PR_CAPBSET_DROP 24
103 #include "commands.h"
106 lxc_log_define(lxc_start
, lxc
);
108 LXC_TTY_HANDLER(SIGINT
);
109 LXC_TTY_HANDLER(SIGQUIT
);
111 static int match_fd(int fd
)
113 return (fd
== 0 || fd
== 1 || fd
== 2);
116 int lxc_check_inherited(void)
118 struct dirent dirent
, *direntp
;
123 dir
= opendir("/proc/self/fd");
125 WARN("failed to open directory: %m");
131 while (!readdir_r(dir
, &dirent
, &direntp
)) {
138 if (!strcmp(direntp
->d_name
, "."))
141 if (!strcmp(direntp
->d_name
, ".."))
144 fd
= atoi(direntp
->d_name
);
146 if (fd
== fddir
|| fd
== lxc_log_fd
)
156 snprintf(procpath
, sizeof(procpath
), "/proc/self/fd/%d", fd
);
158 if (readlink(procpath
, path
, sizeof(path
)) == -1)
159 ERROR("readlink(%s) failed : %m", procpath
);
161 ERROR("inherited fd %d on %s", fd
, path
);
165 ERROR("failed to close directory");
169 static int setup_sigchld_fd(sigset_t
*oldmask
)
174 if (sigprocmask(SIG_BLOCK
, NULL
, &mask
)) {
175 SYSERROR("failed to get mask signal");
179 if (sigaddset(&mask
, SIGCHLD
) || sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
180 SYSERROR("failed to set mask signal");
184 fd
= signalfd(-1, &mask
, 0);
186 SYSERROR("failed to create the signal fd");
190 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
191 SYSERROR("failed to set sigfd to close-on-exec");
196 DEBUG("sigchild handler set");
201 static int sigchld_handler(int fd
, void *data
,
202 struct lxc_epoll_descr
*descr
)
204 DEBUG("child exited");
209 int lxc_pid_callback(int fd
, struct lxc_request
*request
,
210 struct lxc_handler
*handler
)
212 struct lxc_answer answer
;
215 answer
.pid
= handler
->pid
;
218 ret
= send(fd
, &answer
, sizeof(answer
), 0);
220 WARN("failed to send answer to the peer");
224 if (ret
!= sizeof(answer
)) {
225 ERROR("partial answer sent");
232 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
234 handler
->state
= state
;
235 lxc_monitor_send_state(name
, state
);
239 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
241 int sigfd
= handler
->sigfd
;
242 int pid
= handler
->pid
;
243 struct lxc_epoll_descr descr
;
245 if (lxc_mainloop_open(&descr
)) {
246 ERROR("failed to create mainloop");
250 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
251 ERROR("failed to add handler for the signal");
252 goto out_mainloop_open
;
255 if (lxc_console_mainloop_add(&descr
, handler
)) {
256 ERROR("failed to add console handler to mainloop");
257 goto out_mainloop_open
;
260 if (lxc_command_mainloop_add(name
, &descr
, handler
)) {
261 ERROR("failed to add command handler to mainloop");
262 goto out_mainloop_open
;
265 if (lxc_utmp_mainloop_add(&descr
, handler
)) {
266 ERROR("failed to add utmp handler to mainloop");
267 goto out_mainloop_open
;
270 return lxc_mainloop(&descr
);
273 lxc_mainloop_close(&descr
);
279 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
)
281 struct lxc_handler
*handler
;
283 handler
= malloc(sizeof(*handler
));
287 memset(handler
, 0, sizeof(*handler
));
289 handler
->conf
= conf
;
291 handler
->name
= strdup(name
);
292 if (!handler
->name
) {
293 ERROR("failed to allocate memory");
297 /* Begin the set the state to STARTING*/
298 if (lxc_set_state(name
, handler
, STARTING
)) {
299 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
303 if (lxc_create_tty(name
, conf
)) {
304 ERROR("failed to create the ttys");
308 if (lxc_create_console(conf
)) {
309 ERROR("failed to create console");
313 /* the signal fd has to be created before forking otherwise
314 * if the child process exits before we setup the signal fd,
315 * the event will be lost and the command will be stuck */
316 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
317 if (handler
->sigfd
< 0) {
318 ERROR("failed to set sigchild fd handler");
319 goto out_delete_console
;
322 /* Avoid signals from terminal */
323 LXC_TTY_ADD_HANDLER(SIGINT
);
324 LXC_TTY_ADD_HANDLER(SIGQUIT
);
326 INFO("'%s' is initialized", name
);
330 lxc_delete_console(&conf
->console
);
332 lxc_delete_tty(&conf
->tty_info
);
334 lxc_set_state(name
, handler
, ABORTING
);
337 handler
->name
= NULL
;
343 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
345 /* The STOPPING state is there for future cleanup code
346 * which can take awhile
348 lxc_set_state(name
, handler
, STOPPING
);
349 lxc_set_state(name
, handler
, STOPPED
);
350 lxc_unlink_nsgroup(name
);
352 lxc_delete_console(&handler
->conf
->console
);
353 lxc_delete_tty(&handler
->conf
->tty_info
);
357 LXC_TTY_DEL_HANDLER(SIGQUIT
);
358 LXC_TTY_DEL_HANDLER(SIGINT
);
361 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
363 lxc_set_state(name
, handler
, ABORTING
);
364 if (handler
->pid
> 0)
365 kill(handler
->pid
, SIGKILL
);
371 struct lxc_handler
*handler
;
375 static int do_start(void *arg
)
377 struct start_arg
*start_arg
= arg
;
378 struct lxc_handler
*handler
= start_arg
->handler
;
379 const char *name
= start_arg
->name
;
380 char *const *argv
= start_arg
->argv
;
381 int *sv
= start_arg
->sv
;
384 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
385 SYSERROR("failed to set sigprocmask");
391 /* Be sure we don't inherit this after the exec */
392 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
394 /* Tell our father he can begin to configure the container */
395 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
396 SYSERROR("failed to write socket");
400 /* Wait for the father to finish the configuration */
401 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
402 SYSERROR("failed to read socket");
406 /* Setup the container, ip, names, utsname, ... */
407 if (lxc_setup(name
, handler
->conf
)) {
408 ERROR("failed to setup the container");
409 goto out_warn_father
;
412 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
413 SYSERROR("failed to remove CAP_SYS_BOOT capability");
417 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
418 SYSERROR("failed to set pdeath signal");
422 NOTICE("exec'ing '%s'", argv
[0]);
424 execvp(argv
[0], argv
);
425 SYSERROR("failed to exec %s", argv
[0]);
428 /* If the exec fails, tell that to our father */
429 if (write(sv
[0], &err
, sizeof(err
)) < 0)
430 SYSERROR("failed to write the socket");
434 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
439 int failed_before_rename
= 0;
441 struct start_arg start_arg
= {
448 /* Synchro socketpair */
449 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
450 SYSERROR("failed to create communication socketpair");
454 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
455 if (!lxc_list_empty(&handler
->conf
->network
)) {
457 clone_flags
|= CLONE_NEWNET
;
459 /* that should be done before the clone because we will
460 * fill the netdev index and use them in the child
462 if (lxc_create_network(&handler
->conf
->network
)) {
463 ERROR("failed to create the network");
468 /* Create a process in a new set of namespaces */
469 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
470 if (handler
->pid
< 0) {
471 SYSERROR("failed to fork into a new namespace");
477 /* Wait for the child to be ready */
478 if (read(sv
[1], &sync
, sizeof(sync
)) <= 0) {
479 ERROR("sync read failure : %m");
480 failed_before_rename
= 1;
483 if (lxc_rename_nsgroup(name
, handler
))
486 if (failed_before_rename
)
489 /* Create the network configuration */
490 if (clone_flags
& CLONE_NEWNET
) {
491 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
492 ERROR("failed to create the configured network");
497 /* Tell the child to continue its initialization */
498 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
499 SYSERROR("failed to write the socket");
503 /* Wait for the child to exec or returning an error */
504 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
505 ERROR("failed to read the socket");
509 if (lxc_set_state(name
, handler
, RUNNING
)) {
510 ERROR("failed to set state to %s",
511 lxc_state2str(RUNNING
));
517 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
525 if (clone_flags
& CLONE_NEWNET
)
526 lxc_delete_network(&handler
->conf
->network
);
528 lxc_abort(name
, handler
);
533 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
535 struct lxc_handler
*handler
;
539 if (lxc_check_inherited())
542 handler
= lxc_init(name
, conf
);
544 ERROR("failed to initialize the container");
548 err
= lxc_spawn(name
, handler
, argv
);
550 ERROR("failed to spawn '%s'", argv
[0]);
554 /* no need of other inherited fds but stderr */
555 close(fileno(stdin
));
556 close(fileno(stdout
));
558 err
= lxc_poll(name
, handler
);
560 ERROR("mainloop exited with an error");
564 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
567 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
))
568 WARN("failed to restore sigprocmask");
570 err
= lxc_error_set_and_log(handler
->pid
, status
);
572 lxc_fini(name
, handler
);
576 lxc_abort(name
, handler
);