]>
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
101 #include "commands.h"
104 lxc_log_define(lxc_start
, lxc
);
106 LXC_TTY_HANDLER(SIGINT
);
107 LXC_TTY_HANDLER(SIGQUIT
);
109 static int setup_sigchld_fd(sigset_t
*oldmask
)
114 if (sigprocmask(SIG_BLOCK
, NULL
, &mask
)) {
115 SYSERROR("failed to get mask signal");
119 if (sigaddset(&mask
, SIGCHLD
) || sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
120 SYSERROR("failed to set mask signal");
124 fd
= signalfd(-1, &mask
, 0);
126 SYSERROR("failed to create the signal fd");
130 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
131 SYSERROR("failed to set sigfd to close-on-exec");
136 DEBUG("sigchild handler set");
141 static int sigchld_handler(int fd
, void *data
,
142 struct lxc_epoll_descr
*descr
)
144 DEBUG("child exited");
149 int lxc_set_state(const char *name
, struct lxc_handler
*handler
, lxc_state_t state
)
151 handler
->state
= state
;
152 lxc_monitor_send_state(name
, state
);
156 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
158 int sigfd
= handler
->sigfd
;
159 int pid
= handler
->pid
;
160 struct lxc_epoll_descr descr
;
162 if (lxc_mainloop_open(&descr
)) {
163 ERROR("failed to create mainloop");
167 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
168 ERROR("failed to add handler for the signal");
169 goto out_mainloop_open
;
172 if (lxc_console_mainloop_add(&descr
, handler
)) {
173 ERROR("failed to add console handler to mainloop");
174 goto out_mainloop_open
;
177 if (lxc_command_mainloop_add(name
, &descr
, handler
))
178 goto out_mainloop_open
;
180 return lxc_mainloop(&descr
);
183 lxc_mainloop_close(&descr
);
189 struct lxc_handler
*lxc_init(const char *name
, struct lxc_conf
*conf
)
191 struct lxc_handler
*handler
;
193 handler
= malloc(sizeof(*handler
));
197 memset(handler
, 0, sizeof(*handler
));
199 handler
->conf
= conf
;
201 /* Begin the set the state to STARTING*/
202 if (lxc_set_state(name
, handler
, STARTING
)) {
203 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
207 if (lxc_create_tty(name
, conf
)) {
208 ERROR("failed to create the ttys");
212 if (lxc_create_console(conf
)) {
213 ERROR("failed to create console");
217 /* the signal fd has to be created before forking otherwise
218 * if the child process exits before we setup the signal fd,
219 * the event will be lost and the command will be stuck */
220 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
221 if (handler
->sigfd
< 0) {
222 ERROR("failed to set sigchild fd handler");
223 goto out_delete_console
;
226 /* Avoid signals from terminal */
227 LXC_TTY_ADD_HANDLER(SIGINT
);
228 LXC_TTY_ADD_HANDLER(SIGQUIT
);
230 INFO("'%s' is initialized", name
);
234 lxc_delete_console(&conf
->console
);
236 lxc_delete_tty(&conf
->tty_info
);
238 lxc_set_state(name
, handler
, ABORTING
);
244 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
246 /* The STOPPING state is there for future cleanup code
247 * which can take awhile
249 lxc_set_state(name
, handler
, STOPPING
);
250 lxc_set_state(name
, handler
, STOPPED
);
251 lxc_unlink_nsgroup(name
);
253 lxc_delete_console(&handler
->conf
->console
);
254 lxc_delete_tty(&handler
->conf
->tty_info
);
257 LXC_TTY_DEL_HANDLER(SIGQUIT
);
258 LXC_TTY_DEL_HANDLER(SIGINT
);
261 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
263 lxc_set_state(name
, handler
, ABORTING
);
264 kill(handler
->pid
, SIGKILL
);
270 struct lxc_handler
*handler
;
274 static int do_start(void *arg
)
276 struct start_arg
*start_arg
= arg
;
277 struct lxc_handler
*handler
= start_arg
->handler
;
278 const char *name
= start_arg
->name
;
279 char *const *argv
= start_arg
->argv
;
280 int *sv
= start_arg
->sv
;
283 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
284 SYSERROR("failed to set sigprocmask");
290 /* Be sure we don't inherit this after the exec */
291 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
293 /* Tell our father he can begin to configure the container */
294 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
295 SYSERROR("failed to write socket");
299 /* Wait for the father to finish the configuration */
300 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
301 SYSERROR("failed to read socket");
305 /* Setup the container, ip, names, utsname, ... */
306 if (lxc_setup(name
, handler
->conf
)) {
307 ERROR("failed to setup the container");
308 goto out_warn_father
;
311 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
312 SYSERROR("failed to remove CAP_SYS_BOOT capability");
316 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0)) {
317 SYSERROR("failed to set pdeath signal");
321 NOTICE("exec'ing '%s'", argv
[0]);
323 execvp(argv
[0], argv
);
324 SYSERROR("failed to exec %s", argv
[0]);
327 /* If the exec fails, tell that to our father */
328 if (write(sv
[0], &err
, sizeof(err
)) < 0)
329 SYSERROR("failed to write the socket");
333 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
339 struct start_arg start_arg
= {
346 /* Synchro socketpair */
347 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
348 SYSERROR("failed to create communication socketpair");
352 clone_flags
= CLONE_NEWUTS
|CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
353 if (!lxc_list_empty(&handler
->conf
->network
)) {
355 clone_flags
|= CLONE_NEWNET
;
357 /* that should be done before the clone because we will
358 * fill the netdev index and use them in the child
360 if (lxc_create_network(&handler
->conf
->network
)) {
361 ERROR("failed to create the network");
366 /* Create a process in a new set of namespaces */
367 handler
->pid
= lxc_clone(do_start
, &start_arg
, clone_flags
);
368 if (handler
->pid
< 0) {
369 SYSERROR("failed to fork into a new namespace");
375 /* Wait for the child to be ready */
376 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
377 SYSERROR("failed to read the socket");
381 if (lxc_rename_nsgroup(name
, handler
))
384 /* Create the network configuration */
385 if (clone_flags
& CLONE_NEWNET
) {
386 if (lxc_assign_network(&handler
->conf
->network
, handler
->pid
)) {
387 ERROR("failed to create the configured network");
392 /* Tell the child to continue its initialization */
393 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
394 SYSERROR("failed to write the socket");
398 /* Wait for the child to exec or returning an error */
399 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
400 ERROR("failed to read the socket");
404 if (lxc_set_state(name
, handler
, RUNNING
)) {
405 ERROR("failed to set state to %s",
406 lxc_state2str(RUNNING
));
412 NOTICE("'%s' started with pid '%d'", argv
[0], handler
->pid
);
420 if (clone_flags
& CLONE_NEWNET
)
421 lxc_delete_network(&handler
->conf
->network
);
423 lxc_abort(name
, handler
);
428 int lxc_start(const char *name
, char *const argv
[], struct lxc_conf
*conf
)
430 struct lxc_handler
*handler
;
434 handler
= lxc_init(name
, conf
);
436 ERROR("failed to initialize the container");
440 err
= lxc_spawn(name
, handler
, argv
);
442 ERROR("failed to spawn '%s'", argv
[0]);
446 err
= lxc_close_all_inherited_fd();
448 ERROR("unable to close inherited fds");
452 err
= lxc_poll(name
, handler
);
454 ERROR("mainloop exited with an error");
458 while (waitpid(handler
->pid
, &status
, 0) < 0 && errno
== EINTR
)
461 err
= lxc_error_set_and_log(handler
->pid
, status
);
463 lxc_fini(name
, handler
);
467 lxc_abort(name
, handler
);