]>
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>
39 #include <sys/types.h>
40 #include <sys/prctl.h>
41 #include <sys/types.h>
42 #include <sys/capability.h>
47 #ifdef HAVE_SYS_SIGNALFD_H
48 # include <sys/signalfd.h>
50 # ifndef __NR_signalfd4
51 /* assume kernel headers are too old */
53 # define __NR_signalfd4 327
55 # define __NR_signalfd4 289
57 # define __NR_signalfd4 313
59 # define __NR_signalfd4 322
63 # ifndef __NR_signalfd
64 /* assume kernel headers are too old */
66 # define __NR_signalfd 321
68 # define __NR_signalfd 282
70 # define __NR_signalfd 305
72 # define __NR_signalfd 316
76 int signalfd(int fd
, const sigset_t
*mask
, int flags
)
80 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
81 if (errno
== ENOSYS
&& flags
== 0)
82 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
87 #if !HAVE_DECL_PR_CAPBSET_DROP
88 #define PR_CAPBSET_DROP 24
93 #include <lxc/confile.h>
94 #include <lxc/start.h>
95 #include <lxc/utils.h>
96 #include <lxc/cgroup.h>
97 #include <lxc/monitor.h>
101 #include "mainloop.h"
102 #include "commands.h"
105 lxc_log_define(lxc_start
, lxc
);
107 LXC_TTY_HANDLER(SIGINT
);
108 LXC_TTY_HANDLER(SIGQUIT
);
110 static int setup_sigchld_fd(sigset_t
*oldmask
)
115 if (sigprocmask(SIG_BLOCK
, NULL
, &mask
)) {
116 SYSERROR("failed to get mask signal");
120 if (sigaddset(&mask
, SIGCHLD
) || sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
121 SYSERROR("failed to set mask signal");
125 fd
= signalfd(-1, &mask
, 0);
127 SYSERROR("failed to create the signal fd");
131 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
132 SYSERROR("failed to set sigfd to close-on-exec");
137 DEBUG("sigchild handler set");
142 static int sigchld_handler(int fd
, void *data
,
143 struct lxc_epoll_descr
*descr
)
145 DEBUG("child exited");
150 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
152 handler
->state
= state
;
153 lxc_monitor_send_state(name
, state
);
157 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
159 int sigfd
= handler
->sigfd
;
160 int pid
= handler
->pid
;
162 struct lxc_epoll_descr descr
;
164 if (lxc_mainloop_open(&descr
)) {
165 ERROR("failed to create mainloop");
169 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
170 ERROR("failed to add handler for the signal");
171 goto out_mainloop_open
;
174 if (lxc_command_mainloop_add(name
, &descr
, handler
))
175 goto out_mainloop_open
;
177 ret
= lxc_mainloop(&descr
);
183 lxc_mainloop_close(&descr
);
189 static int fdname(int fd
, char *name
, size_t size
)
191 char path
[MAXPATHLEN
];
194 snprintf(path
, MAXPATHLEN
, "/proc/self/fd/%d", fd
);
196 len
= readlink(path
, name
, size
);
200 return (len
<= 0) ? -1 : 0;
203 static int console_init(char *console
, size_t size
)
208 for (i
= 0; i
< 3; i
++) {
212 if (ttyname_r(i
, console
, size
)) {
213 SYSERROR("failed to retrieve tty name");
220 if (!fstat(0, &stat
)) {
221 if (S_ISREG(stat
.st_mode
) || S_ISCHR(stat
.st_mode
) ||
222 S_ISFIFO(stat
.st_mode
) || S_ISLNK(stat
.st_mode
))
223 return fdname(0, console
, size
);
228 DEBUG("console initialized");
233 struct lxc_handler
*lxc_init(const char *name
, const char *rcfile
)
235 struct lxc_handler
*handler
;
237 handler
= malloc(sizeof(*handler
));
241 memset(handler
, 0, sizeof(*handler
));
243 /* Begin the set the state to STARTING*/
244 if (lxc_set_state(name
, handler
, STARTING
)) {
245 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
249 if (lxc_conf_init(&handler
->conf
)) {
250 ERROR("failed to initialize the configuration");
255 if (access(rcfile
, F_OK
)) {
256 ERROR("failed to access rcfile");
260 if (lxc_config_read(rcfile
, &handler
->conf
)) {
261 ERROR("failed to read the configuration file");
266 if (console_init(handler
->conf
.console
,
267 sizeof(handler
->conf
.console
))) {
268 ERROR("failed to initialize the console");
272 if (lxc_create_tty(name
, &handler
->conf
)) {
273 ERROR("failed to create the ttys");
277 /* the signal fd has to be created before forking otherwise
278 * if the child process exits before we setup the signal fd,
279 * the event will be lost and the command will be stuck */
280 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
281 if (handler
->sigfd
< 0) {
282 ERROR("failed to set sigchild fd handler");
286 /* Avoid signals from terminal */
287 LXC_TTY_ADD_HANDLER(SIGINT
);
288 LXC_TTY_ADD_HANDLER(SIGQUIT
);
292 INFO("'%s' is initialized", name
);
297 lxc_delete_tty(&handler
->conf
.tty_info
);
299 lxc_set_state(name
, handler
, ABORTING
);
306 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
308 /* The STOPPING state is there for future cleanup code
309 * which can take awhile
311 lxc_set_state(name
, handler
, STOPPING
);
312 lxc_set_state(name
, handler
, STOPPED
);
313 lxc_unlink_nsgroup(name
);
316 lxc_delete_tty(&handler
->conf
.tty_info
);
320 LXC_TTY_DEL_HANDLER(SIGQUIT
);
321 LXC_TTY_DEL_HANDLER(SIGINT
);
324 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
326 lxc_set_state(name
, handler
, ABORTING
);
327 kill(handler
->pid
, SIGKILL
);
333 struct lxc_handler
*handler
;
337 static int do_start(void *arg
)
339 struct start_arg
*start_arg
= arg
;
340 struct lxc_handler
*handler
= start_arg
->handler
;
341 const char *name
= start_arg
->name
;
342 char *const *argv
= start_arg
->argv
;
343 int *sv
= start_arg
->sv
;
346 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
347 SYSERROR("failed to set sigprocmask");
353 /* Be sure we don't inherit this after the exec */
354 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
356 /* Tell our father he can begin to configure the container */
357 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
358 SYSERROR("failed to write socket");
362 /* Wait for the father to finish the configuration */
363 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
364 SYSERROR("failed to read socket");
368 /* Setup the container, ip, names, utsname, ... */
369 if (lxc_setup(name
, &handler
->conf
)) {
370 ERROR("failed to setup the container");
371 goto out_warn_father
;
374 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
375 SYSERROR("failed to remove CAP_SYS_BOOT capability");
379 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
380 SYSERROR("failed to set pdeath signal");
384 NOTICE("exec'ing '%s'", argv
[0]);
386 execvp(argv
[0], argv
);
387 SYSERROR("failed to exec %s", argv
[0]);
390 /* If the exec fails, tell that to our father */
391 if (write(sv
[0], &err
, sizeof(err
)) < 0)
392 SYSERROR("failed to write the socket");
397 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
403 struct start_arg start_arg
= {
410 /* Synchro socketpair */
411 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
412 SYSERROR("failed to create communication socketpair");
416 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
417 if (!lxc_list_empty(&handler
->conf
.network
)) {
419 clone_flags
|= CLONE_NEWNET
;
421 /* that should be done before the clone because we will
422 * fill the netdev index and use them in the child
424 if (lxc_create_network(&handler
->conf
.network
)) {
425 ERROR("failed to create the network");
430 /* Create a process in a new set of namespaces */
431 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
432 if (handler
->pid
< 0) {
433 SYSERROR("failed to fork into a new namespace");
439 /* Wait for the child to be ready */
440 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
441 SYSERROR("failed to read the socket");
445 if (lxc_rename_nsgroup(name
, handler
))
448 /* Create the network configuration */
449 if (clone_flags
& CLONE_NEWNET
) {
450 if (lxc_assign_network(&handler
->conf
.network
, handler
->pid
)) {
451 ERROR("failed to create the configured network");
456 /* Tell the child to continue its initialization */
457 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
458 SYSERROR("failed to write the socket");
462 /* Wait for the child to exec or returning an error */
463 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
464 ERROR("failed to read the socket");
468 if (lxc_set_state(name
, handler
, RUNNING
)) {
469 ERROR("failed to set state to %s",
470 lxc_state2str(RUNNING
));
476 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
485 lxc_abort(name
, handler
);
489 int lxc_start(const char *name
, char *const argv
[], const char *rcfile
)
491 struct lxc_handler
*handler
;
495 handler
= lxc_init(name
, rcfile
);
497 ERROR("failed to initialize the container");
501 err
= lxc_spawn(name
, handler
, argv
);
503 ERROR("failed to spawn '%s'", argv
[0]);
507 err
= lxc_close_all_inherited_fd();
509 ERROR("unable to close inherited fds");
513 err
= lxc_poll(name
, handler
);
515 ERROR("mainloop exited with an error");
519 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
522 err
= lxc_error_set_and_log(handler
->pid
, status
);
524 lxc_fini(name
, handler
);
528 lxc_abort(name
, handler
);