]>
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>
48 #include <lxc/confile.h>
50 #ifdef HAVE_SYS_SIGNALFD_H
51 # include <sys/signalfd.h>
53 # ifndef __NR_signalfd4
54 /* assume kernel headers are too old */
56 # define __NR_signalfd4 327
58 # define __NR_signalfd4 289
60 # define __NR_signalfd4 313
62 # define __NR_signalfd4 322
66 # ifndef __NR_signalfd
67 /* assume kernel headers are too old */
69 # define __NR_signalfd 321
71 # define __NR_signalfd 282
73 # define __NR_signalfd 305
75 # define __NR_signalfd 316
79 int signalfd(int fd
, const sigset_t
*mask
, int flags
)
83 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
84 if (errno
== ENOSYS
&& flags
== 0)
85 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
90 #if !HAVE_DECL_PR_CAPBSET_DROP
91 #define PR_CAPBSET_DROP 24
102 lxc_log_define(lxc_start
, lxc
);
104 LXC_TTY_HANDLER(SIGINT
);
105 LXC_TTY_HANDLER(SIGQUIT
);
107 static int setup_sigchld_fd(sigset_t
*oldmask
)
112 if (sigprocmask(SIG_BLOCK
, NULL
, &mask
)) {
113 SYSERROR("failed to get mask signal");
117 if (sigaddset(&mask
, SIGCHLD
) || sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
118 SYSERROR("failed to set mask signal");
122 fd
= signalfd(-1, &mask
, 0);
124 SYSERROR("failed to create the signal fd");
128 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
129 SYSERROR("failed to set sigfd to close-on-exec");
134 DEBUG("sigchild handler set");
139 static int sigchld_handler(int fd
, void *data
,
140 struct lxc_epoll_descr
*descr
)
142 DEBUG("child exited");
147 static int set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
149 handler
->state
= state
;
150 lxc_monitor_send_state(name
, state
);
154 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
156 int sigfd
= handler
->sigfd
;
157 int pid
= handler
->pid
;
159 struct lxc_epoll_descr descr
;
161 if (lxc_mainloop_open(&descr
)) {
162 ERROR("failed to create mainloop");
166 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
167 ERROR("failed to add handler for the signal");
168 goto out_mainloop_open
;
171 if (lxc_command_mainloop_add(name
, &descr
, handler
))
172 goto out_mainloop_open
;
174 ret
= lxc_mainloop(&descr
);
180 lxc_mainloop_close(&descr
);
186 static void remove_init_pid(const char *name
, pid_t pid
)
188 char init
[MAXPATHLEN
];
190 snprintf(init
, MAXPATHLEN
, LXCPATH
"/%s/init", name
);
194 static int fdname(int fd
, char *name
, size_t size
)
196 char path
[MAXPATHLEN
];
199 snprintf(path
, MAXPATHLEN
, "/proc/self/fd/%d", fd
);
201 len
= readlink(path
, name
, size
);
205 return (len
<= 0) ? -1 : 0;
208 static int console_init(char *console
, size_t size
)
213 for (i
= 0; i
< 3; i
++) {
217 if (ttyname_r(i
, console
, size
)) {
218 SYSERROR("failed to retrieve tty name");
225 if (!fstat(0, &stat
)) {
226 if (S_ISREG(stat
.st_mode
) || S_ISCHR(stat
.st_mode
) ||
227 S_ISFIFO(stat
.st_mode
) || S_ISLNK(stat
.st_mode
))
228 return fdname(0, console
, size
);
233 DEBUG("console initialized");
238 struct lxc_handler
*lxc_init(const char *name
)
240 struct lxc_handler
*handler
;
241 char path
[MAXPATHLEN
];
243 handler
= malloc(sizeof(*handler
));
247 memset(handler
, 0, sizeof(*handler
));
249 handler
->lock
= lxc_get_lock(name
);
250 if (handler
->lock
< 0)
253 /* Begin the set the state to STARTING*/
254 if (set_state(name
, handler
, STARTING
)) {
255 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
259 if (lxc_conf_init(&handler
->lxc_conf
)) {
260 ERROR("failed to initialize the configuration");
264 snprintf(path
, sizeof(path
), LXCPATH
"/%s/config", name
);
266 if (!access(path
, F_OK
)) {
268 if (lxc_config_read(path
, &handler
->lxc_conf
)) {
269 ERROR("failed to read the configuration file");
274 if (console_init(handler
->tty
, sizeof(handler
->tty
))) {
275 ERROR("failed to initialize the console");
279 if (lxc_create_tty(name
, &handler
->tty_info
)) {
280 ERROR("failed to create the ttys");
284 /* the signal fd has to be created before forking otherwise
285 * if the child process exits before we setup the signal fd,
286 * the event will be lost and the command will be stuck */
287 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
288 if (handler
->sigfd
< 0) {
289 ERROR("failed to set sigchild fd handler");
293 /* Avoid signals from terminal */
294 LXC_TTY_ADD_HANDLER(SIGINT
);
295 LXC_TTY_ADD_HANDLER(SIGQUIT
);
299 INFO("'%s' is initialized", name
);
304 lxc_delete_tty(&handler
->tty_info
);
306 set_state(name
, handler
, ABORTING
);
308 lxc_put_lock(handler
->lock
);
315 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
317 /* The STOPPING state is there for future cleanup code
318 * which can take awhile
320 set_state(name
, handler
, STOPPING
);
321 set_state(name
, handler
, STOPPED
);
322 lxc_unlink_nsgroup(name
);
325 remove_init_pid(name
, handler
->pid
);
326 lxc_delete_tty(&handler
->tty_info
);
327 lxc_put_lock(handler
->lock
);
331 LXC_TTY_DEL_HANDLER(SIGQUIT
);
332 LXC_TTY_DEL_HANDLER(SIGINT
);
335 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
337 set_state(name
, handler
, ABORTING
);
338 kill(handler
->pid
, SIGKILL
);
344 struct lxc_handler
*handler
;
348 static int do_start(void *arg
)
350 struct start_arg
*start_arg
= arg
;
351 struct lxc_handler
*handler
= start_arg
->handler
;
352 const char *name
= start_arg
->name
;
353 char *const *argv
= start_arg
->argv
;
354 int *sv
= start_arg
->sv
;
357 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
358 SYSERROR("failed to set sigprocmask");
364 /* Be sure we don't inherit this after the exec */
365 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
367 /* Tell our father he can begin to configure the container */
368 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
369 SYSERROR("failed to write socket");
373 /* Wait for the father to finish the configuration */
374 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
375 SYSERROR("failed to read socket");
379 /* Setup the container, ip, names, utsname, ... */
380 if (lxc_setup(name
, handler
)) {
381 ERROR("failed to setup the container");
382 goto out_warn_father
;
385 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
386 SYSERROR("failed to remove CAP_SYS_BOOT capability");
390 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
391 SYSERROR("failed to set pdeath signal");
395 NOTICE("exec'ing '%s'", argv
[0]);
397 execvp(argv
[0], argv
);
398 SYSERROR("failed to exec %s", argv
[0]);
401 /* If the exec fails, tell that to our father */
402 if (write(sv
[0], &err
, sizeof(err
)) < 0)
403 SYSERROR("failed to write the socket");
408 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
414 struct start_arg start_arg
= {
421 /* Synchro socketpair */
422 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
423 SYSERROR("failed to create communication socketpair");
427 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
428 if (conf_has_network(name
))
429 clone_flags
|= CLONE_NEWNET
;
431 /* Create a process in a new set of namespaces */
432 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
433 if (handler
->pid
< 0) {
434 SYSERROR("failed to fork into a new namespace");
440 /* Wait for the child to be ready */
441 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
442 SYSERROR("failed to read the socket");
446 if (lxc_rename_nsgroup(name
, handler
))
449 /* Create the network configuration */
450 if (clone_flags
& CLONE_NEWNET
&&
451 conf_create_network(name
, handler
->pid
)) {
452 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 (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
[])
491 struct lxc_handler
*handler
;
495 handler
= lxc_init(name
);
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
);