]>
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 <sys/param.h>
37 #include <sys/mount.h>
38 #include <sys/types.h>
39 #include <sys/prctl.h>
40 #include <sys/capability.h>
45 #ifdef HAVE_SYS_SIGNALFD_H
46 # include <sys/signalfd.h>
48 # ifndef __NR_signalfd4
49 /* assume kernel headers are too old */
51 # define __NR_signalfd4 327
53 # define __NR_signalfd4 289
55 # define __NR_signalfd4 313
57 # define __NR_signalfd4 322
61 # ifndef __NR_signalfd
62 /* assume kernel headers are too old */
64 # define __NR_signalfd 321
66 # define __NR_signalfd 282
68 # define __NR_signalfd 305
70 # define __NR_signalfd 316
74 int signalfd(int fd
, const sigset_t
*mask
, int flags
)
78 retval
= syscall (__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
79 if (errno
== ENOSYS
&& flags
== 0)
80 retval
= syscall (__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
85 #if !HAVE_DECL_PR_CAPBSET_DROP
86 #define PR_CAPBSET_DROP 24
96 lxc_log_define(lxc_start
, lxc
);
98 LXC_TTY_HANDLER(SIGINT
);
99 LXC_TTY_HANDLER(SIGQUIT
);
101 static int setup_sigchld_fd(sigset_t
*oldmask
)
106 if (sigprocmask(SIG_BLOCK
, NULL
, &mask
)) {
107 SYSERROR("failed to get mask signal");
111 if (sigaddset(&mask
, SIGCHLD
) || sigprocmask(SIG_BLOCK
, &mask
, oldmask
)) {
112 SYSERROR("failed to set mask signal");
116 fd
= signalfd(-1, &mask
, 0);
118 SYSERROR("failed to create the signal fd");
122 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
123 SYSERROR("failed to set sigfd to close-on-exec");
131 static int setup_tty_service(const char *name
, int *ttyfd
)
134 struct sockaddr_un addr
= { 0 };
135 char *offset
= &addr
.sun_path
[1];
137 strcpy(offset
, name
);
138 addr
.sun_path
[0] = '\0';
140 fd
= lxc_af_unix_open(addr
.sun_path
, SOCK_STREAM
, 0);
144 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
145 SYSERROR("failed to close-on-exec flag");
155 static int sigchld_handler(int fd
, void *data
,
156 struct lxc_epoll_descr
*descr
)
161 static int ttyclient_handler(int fd
, void *data
,
162 struct lxc_epoll_descr
*descr
)
165 struct lxc_tty_info
*tty_info
= data
;
167 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
169 if (tty_info
->pty_info
[i
].busy
!= fd
)
172 lxc_mainloop_del_handler(descr
, fd
);
173 tty_info
->pty_info
[i
].busy
= 0;
180 static int ttyservice_handler(int fd
, void *data
,
181 struct lxc_epoll_descr
*descr
)
183 int conn
, ttynum
, val
= 1, ret
= -1;
184 struct lxc_tty_info
*tty_info
= data
;
186 conn
= accept(fd
, NULL
, 0);
188 SYSERROR("failed to accept tty client");
192 if (setsockopt(conn
, SOL_SOCKET
, SO_PASSCRED
, &val
, sizeof(val
))) {
193 SYSERROR("failed to enable credential on socket");
197 if (lxc_af_unix_rcv_credential(conn
, &ttynum
, sizeof(ttynum
)))
201 if (ttynum
> tty_info
->nbtty
)
204 if (tty_info
->pty_info
[ttynum
- 1].busy
)
210 /* fixup index tty1 => [0] */
212 ttynum
<= tty_info
->nbtty
&& tty_info
->pty_info
[ttynum
- 1].busy
;
215 /* we didn't find any available slot for tty */
216 if (ttynum
> tty_info
->nbtty
)
220 if (lxc_af_unix_send_fd(conn
, tty_info
->pty_info
[ttynum
- 1].master
,
221 &ttynum
, sizeof(ttynum
)) < 0) {
222 ERROR("failed to send tty to client");
226 if (lxc_mainloop_add_handler(descr
, conn
,
227 ttyclient_handler
, tty_info
)) {
228 ERROR("failed to add tty client handler");
232 tty_info
->pty_info
[ttynum
- 1].busy
= conn
;
242 int lxc_poll(const char *name
, struct lxc_handler
*handler
)
244 int sigfd
= handler
->sigfd
;
245 int pid
= handler
->pid
;
246 const struct lxc_tty_info
*tty_info
= &handler
->tty_info
;
248 int nfds
, ttyfd
= -1, ret
= -1;
249 struct lxc_epoll_descr descr
;
251 if (tty_info
->nbtty
&& setup_tty_service(name
, &ttyfd
)) {
252 ERROR("failed to create the tty service point");
256 /* sigfd + nb tty + tty service
257 * if tty is enabled */
258 nfds
= tty_info
->nbtty
+ 1 + tty_info
->nbtty
? 1 : 0;
260 if (lxc_mainloop_open(nfds
, &descr
)) {
261 ERROR("failed to create mainloop");
265 if (lxc_mainloop_add_handler(&descr
, sigfd
, sigchld_handler
, &pid
)) {
266 ERROR("failed to add handler for the signal");
267 goto out_mainloop_open
;
270 if (tty_info
->nbtty
) {
271 if (lxc_mainloop_add_handler(&descr
, ttyfd
,
274 ERROR("failed to add handler for the tty");
275 goto out_mainloop_open
;
279 ret
= lxc_mainloop(&descr
);
285 lxc_mainloop_close(&descr
);
293 static int save_init_pid(const char *name
, pid_t pid
)
295 char init
[MAXPATHLEN
];
299 snprintf(init
, MAXPATHLEN
, LXCPATH
"/%s/init", name
);
301 if (!asprintf(&val
, "%d\n", pid
)) {
302 SYSERROR("failed to allocate memory");
306 fd
= open(init
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRUSR
|S_IWUSR
);
308 SYSERROR("failed to open '%s'", init
);
312 if (write(fd
, val
, strlen(val
)) < 0) {
313 SYSERROR("failed to write the init pid");
327 static void remove_init_pid(const char *name
, pid_t pid
)
329 char init
[MAXPATHLEN
];
331 snprintf(init
, MAXPATHLEN
, LXCPATH
"/%s/init", name
);
335 int lxc_init(const char *name
, struct lxc_handler
*handler
)
339 memset(handler
, 0, sizeof(*handler
));
341 handler
->lock
= lxc_get_lock(name
);
342 if (handler
->lock
< 0)
345 /* Begin the set the state to STARTING*/
346 if (lxc_setstate(name
, STARTING
)) {
347 ERROR("failed to set state '%s'", lxc_state2str(STARTING
));
351 /* If we are not attached to a tty, disable it */
352 if (ttyname_r(0, handler
->tty
, sizeof(handler
->tty
)))
353 handler
->tty
[0] = '\0';
355 if (lxc_create_tty(name
, &handler
->tty_info
)) {
356 ERROR("failed to create the ttys");
360 /* the signal fd has to be created before forking otherwise
361 * if the child process exits before we setup the signal fd,
362 * the event will be lost and the command will be stuck */
363 handler
->sigfd
= setup_sigchld_fd(&handler
->oldmask
);
364 if (handler
->sigfd
< 0) {
365 ERROR("failed to set sigchild fd handler");
369 /* Avoid signals from terminal */
370 LXC_TTY_ADD_HANDLER(SIGINT
);
371 LXC_TTY_ADD_HANDLER(SIGQUIT
);
378 lxc_delete_tty(&handler
->tty_info
);
380 lxc_setstate(name
, ABORTING
);
382 lxc_put_lock(handler
->lock
);
386 void lxc_fini(const char *name
, struct lxc_handler
*handler
)
388 /* The STOPPING state is there for future cleanup code
389 * which can take awhile
391 lxc_setstate(name
, STOPPING
);
393 lxc_setstate(name
, STOPPED
);
395 remove_init_pid(name
, handler
->pid
);
397 lxc_delete_tty(&handler
->tty_info
);
399 lxc_unlink_nsgroup(name
);
401 lxc_put_lock(handler
->lock
);
403 LXC_TTY_DEL_HANDLER(SIGQUIT
);
404 LXC_TTY_DEL_HANDLER(SIGINT
);
407 void lxc_abort(const char *name
, struct lxc_handler
*handler
)
409 lxc_setstate(name
, ABORTING
);
410 kill(handler
->pid
, SIGKILL
);
413 int lxc_spawn(const char *name
, struct lxc_handler
*handler
, char *const argv
[])
419 /* Synchro socketpair */
420 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sv
)) {
421 SYSERROR("failed to create communication socketpair");
425 clone_flags
= CLONE_NEWPID
|CLONE_NEWIPC
|CLONE_NEWNS
;
426 if (conf_has_utsname(name
))
427 clone_flags
|= CLONE_NEWUTS
;
428 if (conf_has_network(name
))
429 clone_flags
|= CLONE_NEWNET
;
431 /* Create a process in a new set of namespaces */
432 handler
->pid
= fork_ns(clone_flags
);
433 if (handler
->pid
< 0) {
434 SYSERROR("failed to fork into a new namespace");
440 if (sigprocmask(SIG_SETMASK
, &handler
->oldmask
, NULL
)) {
441 SYSERROR("failed to set sigprocmask");
447 /* Be sure we don't inherit this after the exec */
448 fcntl(sv
[0], F_SETFD
, FD_CLOEXEC
);
450 /* Tell our father he can begin to configure the container */
451 if (write(sv
[0], &sync
, sizeof(sync
)) < 0) {
452 SYSERROR("failed to write socket");
456 /* Wait for the father to finish the configuration */
457 if (read(sv
[0], &sync
, sizeof(sync
)) < 0) {
458 SYSERROR("failed to read socket");
462 /* Setup the container, ip, names, utsname, ... */
463 if (lxc_setup(name
, handler
->tty
, &handler
->tty_info
)) {
464 ERROR("failed to setup the container");
465 goto out_warn_father
;
468 if (prctl(PR_CAPBSET_DROP
, CAP_SYS_BOOT
, 0, 0, 0)) {
469 SYSERROR("failed to remove CAP_SYS_BOOT capability");
473 execvp(argv
[0], argv
);
474 SYSERROR("failed to exec %s", argv
[0]);
477 /* If the exec fails, tell that to our father */
478 if (write(sv
[0], &err
, sizeof(err
)) < 0)
479 SYSERROR("failed to write the socket");
486 /* Wait for the child to be ready */
487 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
488 SYSERROR("failed to read the socket");
492 if (lxc_link_nsgroup(name
, handler
->pid
))
493 WARN("cgroupfs not found: cgroup disabled");
495 /* Create the network configuration */
496 if (clone_flags
& CLONE_NEWNET
&& conf_create_network(name
, handler
->pid
)) {
497 ERROR("failed to create the configured network");
501 /* Tell the child to continue its initialization */
502 if (write(sv
[1], &sync
, sizeof(sync
)) < 0) {
503 SYSERROR("failed to write the socket");
507 /* Wait for the child to exec or returning an error */
508 if (read(sv
[1], &sync
, sizeof(sync
)) < 0) {
509 ERROR("failed to read the socket");
513 if (save_init_pid(name
, handler
->pid
)) {
514 ERROR("failed to save the init pid info");
518 if (lxc_setstate(name
, RUNNING
)) {
519 ERROR("failed to set state to %s",
520 lxc_state2str(RUNNING
));
533 lxc_abort(name
, handler
);
537 int lxc_start(const char *name
, char *const argv
[])
539 struct lxc_handler handler
= { 0 };
543 if (lxc_init(name
, &handler
)) {
544 ERROR("failed to initialize the container");
548 err
= lxc_spawn(name
, &handler
, argv
);
550 ERROR("failed to spawn '%s'", argv
[0]);
554 err
= lxc_poll(name
, &handler
);
556 ERROR("mainloop exited with an error");
560 while (waitpid(handler
.pid
, &status
, 0) < 0 && errno
== EINTR
)
563 err
= lxc_error_set_and_log(handler
.pid
, status
);
565 lxc_fini(name
, &handler
);
569 lxc_abort(name
, &handler
);