]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
304c73c4f1ad87663ebcbdb44a44295df4efafb8
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
;
161 struct lxc_epoll_descr descr
;
163 if (lxc_mainloop_open(&descr
)) {
164 ERROR("failed to create mainloop");
168 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
169 ERROR("failed to add handler for the signal");
170 goto out_mainloop_open
;
173 if (lxc_command_mainloop_add(name
, &descr
, handler
))
174 goto out_mainloop_open
;
176 return lxc_mainloop(&descr
);
179 lxc_mainloop_close(&descr
);
185 static int fdname(int fd
, char *name
, size_t size
)
187 char path
[MAXPATHLEN
];
190 snprintf(path
, MAXPATHLEN
, "/proc/self/fd/%d", fd
);
192 len
= readlink(path
, name
, size
);
196 return (len
<= 0) ? -1 : 0;
199 static int console_init(char *console
, size_t size
)
204 for (i
= 0; i
< 3; i
++) {
208 if (ttyname_r(i
, console
, size
)) {
209 SYSERROR("failed to retrieve tty name");
216 if (!fstat(0, &stat
)) {
217 if (S_ISREG(stat
.st_mode
) || S_ISCHR(stat
.st_mode
) ||
218 S_ISFIFO(stat
.st_mode
) || S_ISLNK(stat
.st_mode
))
219 return fdname(0, console
, size
);
224 DEBUG("console initialized");
229 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
)
231 struct lxc_handler
*handler
;
233 handler
= malloc(sizeof(*handler
));
237 memset(handler
, 0, sizeof(*handler
));
239 handler
->conf
= conf
;
241 /* Begin the set the state to STARTING*/
242 if (lxc_set_state(name
, handler
, STARTING
)) {
243 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
247 if (console_init(conf
->console
, sizeof(conf
->console
))) {
248 ERROR("failed to initialize the console");
252 if (lxc_create_tty(name
, conf
)) {
253 ERROR("failed to create the ttys");
257 /* the signal fd has to be created before forking otherwise
258 * if the child process exits before we setup the signal fd,
259 * the event will be lost and the command will be stuck */
260 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
261 if (handler
->sigfd
< 0) {
262 ERROR("failed to set sigchild fd handler");
266 /* Avoid signals from terminal */
267 LXC_TTY_ADD_HANDLER(SIGINT
);
268 LXC_TTY_ADD_HANDLER(SIGQUIT
);
270 INFO("'%s' is initialized", name
);
274 lxc_delete_tty(&conf
->tty_info
);
276 lxc_set_state(name
, handler
, ABORTING
);
282 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
284 /* The STOPPING state is there for future cleanup code
285 * which can take awhile
287 lxc_set_state(name
, handler
, STOPPING
);
288 lxc_set_state(name
, handler
, STOPPED
);
289 lxc_unlink_nsgroup(name
);
292 lxc_delete_tty(&handler
->conf
->tty_info
);
296 LXC_TTY_DEL_HANDLER(SIGQUIT
);
297 LXC_TTY_DEL_HANDLER(SIGINT
);
300 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
302 lxc_set_state(name
, handler
, ABORTING
);
303 kill(handler
->pid
, SIGKILL
);
309 struct lxc_handler
*handler
;
313 static int do_start(void *arg
)
315 struct start_arg
*start_arg
= arg
;
316 struct lxc_handler
*handler
= start_arg
->handler
;
317 const char *name
= start_arg
->name
;
318 char *const *argv
= start_arg
->argv
;
319 int *sv
= start_arg
->sv
;
322 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
323 SYSERROR("failed to set sigprocmask");
329 /* Be sure we don't inherit this after the exec */
330 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
332 /* Tell our father he can begin to configure the container */
333 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
334 SYSERROR("failed to write socket");
338 /* Wait for the father to finish the configuration */
339 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
340 SYSERROR("failed to read socket");
344 /* Setup the container, ip, names, utsname, ... */
345 if (lxc_setup(name
, handler
->conf
)) {
346 ERROR("failed to setup the container");
347 goto out_warn_father
;
350 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
351 SYSERROR("failed to remove CAP_SYS_BOOT capability");
355 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
356 SYSERROR("failed to set pdeath signal");
360 NOTICE("exec'ing '%s'", argv
[0]);
362 execvp(argv
[0], argv
);
363 SYSERROR("failed to exec %s", argv
[0]);
366 /* If the exec fails, tell that to our father */
367 if (write(sv
[0], &err
, sizeof(err
)) < 0)
368 SYSERROR("failed to write the socket");
372 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
378 struct start_arg start_arg
= {
385 /* Synchro socketpair */
386 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
387 SYSERROR("failed to create communication socketpair");
391 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
392 if (!lxc_list_empty(&handler
->conf
->network
)) {
394 clone_flags
|= CLONE_NEWNET
;
396 /* that should be done before the clone because we will
397 * fill the netdev index and use them in the child
399 if (lxc_create_network(&handler
->conf
->network
)) {
400 ERROR("failed to create the network");
405 /* Create a process in a new set of namespaces */
406 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
407 if (handler
->pid
< 0) {
408 SYSERROR("failed to fork into a new namespace");
414 /* Wait for the child to be ready */
415 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
416 SYSERROR("failed to read the socket");
420 if (lxc_rename_nsgroup(name
, handler
))
423 /* Create the network configuration */
424 if (clone_flags
& CLONE_NEWNET
) {
425 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
426 ERROR("failed to create the configured network");
431 /* Tell the child to continue its initialization */
432 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
433 SYSERROR("failed to write the socket");
437 /* Wait for the child to exec or returning an error */
438 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
439 ERROR("failed to read the socket");
443 if (lxc_set_state(name
, handler
, RUNNING
)) {
444 ERROR("failed to set state to %s",
445 lxc_state2str(RUNNING
));
451 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
459 lxc_abort(name
, handler
);
463 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
465 struct lxc_handler
*handler
;
469 handler
= lxc_init(name
, conf
);
471 ERROR("failed to initialize the container");
475 err
= lxc_spawn(name
, handler
, argv
);
477 ERROR("failed to spawn '%s'", argv
[0]);
481 err
= lxc_close_all_inherited_fd();
483 ERROR("unable to close inherited fds");
487 err
= lxc_poll(name
, handler
);
489 ERROR("mainloop exited with an error");
493 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
496 err
= lxc_error_set_and_log(handler
->pid
, status
);
498 lxc_fini(name
, handler
);
502 lxc_abort(name
, handler
);