]>
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 /* Begin the set the state to STARTING*/
250 if (set_state(name
, handler
, STARTING
)) {
251 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
255 if (lxc_conf_init(&handler
->conf
)) {
256 ERROR("failed to initialize the configuration");
260 snprintf(path
, sizeof(path
), LXCPATH
"/%s/config", name
);
262 if (!access(path
, F_OK
) && lxc_config_read(path
, &handler
->conf
)) {
263 ERROR("failed to read the configuration file");
267 if (console_init(handler
->conf
.console
,
268 sizeof(handler
->conf
.console
))) {
269 ERROR("failed to initialize the console");
273 if (lxc_create_tty(name
, &handler
->conf
)) {
274 ERROR("failed to create the ttys");
278 /* the signal fd has to be created before forking otherwise
279 * if the child process exits before we setup the signal fd,
280 * the event will be lost and the command will be stuck */
281 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
282 if (handler
->sigfd
< 0) {
283 ERROR("failed to set sigchild fd handler");
287 /* Avoid signals from terminal */
288 LXC_TTY_ADD_HANDLER(SIGINT
);
289 LXC_TTY_ADD_HANDLER(SIGQUIT
);
293 INFO("'%s' is initialized", name
);
298 lxc_delete_tty(&handler
->conf
.tty_info
);
300 set_state(name
, handler
, ABORTING
);
307 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
309 /* The STOPPING state is there for future cleanup code
310 * which can take awhile
312 set_state(name
, handler
, STOPPING
);
313 set_state(name
, handler
, STOPPED
);
314 lxc_unlink_nsgroup(name
);
317 remove_init_pid(name
, handler
->pid
);
318 lxc_delete_tty(&handler
->conf
.tty_info
);
322 LXC_TTY_DEL_HANDLER(SIGQUIT
);
323 LXC_TTY_DEL_HANDLER(SIGINT
);
326 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
328 set_state(name
, handler
, ABORTING
);
329 kill(handler
->pid
, SIGKILL
);
335 struct lxc_handler
*handler
;
339 static int do_start(void *arg
)
341 struct start_arg
*start_arg
= arg
;
342 struct lxc_handler
*handler
= start_arg
->handler
;
343 const char *name
= start_arg
->name
;
344 char *const *argv
= start_arg
->argv
;
345 int *sv
= start_arg
->sv
;
348 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
349 SYSERROR("failed to set sigprocmask");
355 /* Be sure we don't inherit this after the exec */
356 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
358 /* Tell our father he can begin to configure the container */
359 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
360 SYSERROR("failed to write socket");
364 /* Wait for the father to finish the configuration */
365 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
366 SYSERROR("failed to read socket");
370 /* Setup the container, ip, names, utsname, ... */
371 if (lxc_setup(name
, &handler
->conf
)) {
372 ERROR("failed to setup the container");
373 goto out_warn_father
;
376 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
377 SYSERROR("failed to remove CAP_SYS_BOOT capability");
381 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
382 SYSERROR("failed to set pdeath signal");
386 NOTICE("exec'ing '%s'", argv
[0]);
388 execvp(argv
[0], argv
);
389 SYSERROR("failed to exec %s", argv
[0]);
392 /* If the exec fails, tell that to our father */
393 if (write(sv
[0], &err
, sizeof(err
)) < 0)
394 SYSERROR("failed to write the socket");
399 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
405 struct start_arg start_arg
= {
412 /* Synchro socketpair */
413 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
414 SYSERROR("failed to create communication socketpair");
418 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
419 if (!lxc_list_empty(&handler
->conf
.network
)) {
421 clone_flags
|= CLONE_NEWNET
;
423 /* that should be done before the clone because we will
424 * fill the netdev index and use them in the child
426 if (lxc_create_network(&handler
->conf
.network
)) {
427 ERROR("failed to create the network");
432 /* Create a process in a new set of namespaces */
433 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
434 if (handler
->pid
< 0) {
435 SYSERROR("failed to fork into a new namespace");
441 /* Wait for the child to be ready */
442 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
443 SYSERROR("failed to read the socket");
447 if (lxc_rename_nsgroup(name
, handler
))
450 /* Create the network configuration */
451 if (clone_flags
& CLONE_NEWNET
) {
452 if (lxc_assign_network(&handler
->conf
.network
, handler
->pid
)) {
453 ERROR("failed to create the configured network");
458 /* Tell the child to continue its initialization */
459 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
460 SYSERROR("failed to write the socket");
464 /* Wait for the child to exec or returning an error */
465 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
466 ERROR("failed to read the socket");
470 if (set_state(name
, handler
, RUNNING
)) {
471 ERROR("failed to set state to %s",
472 lxc_state2str(RUNNING
));
478 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
487 lxc_abort(name
, handler
);
491 int lxc_start(const char *name
, char *const argv
[])
493 struct lxc_handler
*handler
;
497 handler
= lxc_init(name
);
499 ERROR("failed to initialize the container");
503 err
= lxc_spawn(name
, handler
, argv
);
505 ERROR("failed to spawn '%s'", argv
[0]);
509 err
= lxc_close_all_inherited_fd();
511 ERROR("unable to close inherited fds");
515 err
= lxc_poll(name
, handler
);
517 ERROR("mainloop exited with an error");
521 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
524 err
= lxc_error_set_and_log(handler
->pid
, status
);
526 lxc_fini(name
, handler
);
530 lxc_abort(name
, handler
);