]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
lxc: remove unused lxc_bridge_detach
[mirror_lxc.git] / src / lxc / start.c
CommitLineData
0ad19a3f 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
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.
13 *
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.
18 *
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
22 */
23
ff218c25 24#include "../config.h"
0ad19a3f 25#include <stdio.h>
26#undef _GNU_SOURCE
27#include <string.h>
28#include <stdlib.h>
29#include <dirent.h>
30#include <errno.h>
31#include <unistd.h>
32#include <signal.h>
b0a33c1e 33#include <fcntl.h>
34#include <termios.h>
50e98013 35#include <namespace.h>
0ad19a3f 36#include <sys/param.h>
37#include <sys/file.h>
f4d507d5 38#include <sys/mount.h>
b4f8660e 39#include <sys/stat.h>
0ad19a3f 40#include <sys/types.h>
0ad19a3f 41#include <sys/prctl.h>
ddceb1f9 42#include <sys/types.h>
42ff343d 43#include <sys/capability.h>
0ad19a3f 44#include <sys/wait.h>
b0a33c1e 45#include <sys/un.h>
46#include <sys/poll.h>
ff218c25 47
48#ifdef HAVE_SYS_SIGNALFD_H
8ca61733 49# include <sys/signalfd.h>
ff218c25 50#else
8ca61733
MJ
51# ifndef __NR_signalfd4
52/* assume kernel headers are too old */
53# if __i386__
54# define __NR_signalfd4 327
55# elif __x86_64__
56# define __NR_signalfd4 289
bfa38025
MH
57# elif __powerpc__
58# define __NR_signalfd4 313
47f38330
SH
59# elif __s390x__
60# define __NR_signalfd4 322
8ca61733
MJ
61# endif
62#endif
63
64# ifndef __NR_signalfd
65/* assume kernel headers are too old */
66# if __i386__
67# define __NR_signalfd 321
68# elif __x86_64__
69# define __NR_signalfd 282
bfa38025
MH
70# elif __powerpc__
71# define __NR_signalfd 305
47f38330
SH
72# elif __s390x__
73# define __NR_signalfd 316
8ca61733
MJ
74# endif
75#endif
76
77int signalfd(int fd, const sigset_t *mask, int flags)
78{
79 int retval;
80
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);
84 return retval;
85}
ff218c25 86#endif
0ad19a3f 87
656994bb
MH
88#if !HAVE_DECL_PR_CAPBSET_DROP
89#define PR_CAPBSET_DROP 24
90#endif
91
63376d7d
DL
92#include "start.h"
93#include "conf.h"
6a3111b8 94#include "cgroup.h"
63376d7d 95#include "log.h"
563f2f2c 96#include "cgroup.h"
e2bcd7db 97#include "error.h"
b0a33c1e 98#include "af_unix.h"
99#include "mainloop.h"
63376d7d 100#include "utils.h"
563f2f2c 101#include "utmp.h"
63376d7d 102#include "monitor.h"
96fa1ff0 103#include "commands.h"
63376d7d 104#include "console.h"
36eb9bde
CLG
105
106lxc_log_define(lxc_start, lxc);
107
0ad19a3f 108LXC_TTY_HANDLER(SIGINT);
109LXC_TTY_HANDLER(SIGQUIT);
110
80090207
CLG
111static int match_fd(int fd)
112{
113 return (fd == 0 || fd == 1 || fd == 2);
114}
115
116int lxc_check_inherited(void)
117{
118 struct dirent dirent, *direntp;
119 int fd, fddir;
120 DIR *dir;
121 int ret = 0;
122
123 dir = opendir("/proc/self/fd");
124 if (!dir) {
125 WARN("failed to open directory: %m");
126 return -1;
127 }
128
129 fddir = dirfd(dir);
130
131 while (!readdir_r(dir, &dirent, &direntp)) {
132 char procpath[64];
133 char path[PATH_MAX];
134
135 if (!direntp)
136 break;
137
138 if (!strcmp(direntp->d_name, "."))
139 continue;
140
141 if (!strcmp(direntp->d_name, ".."))
142 continue;
143
144 fd = atoi(direntp->d_name);
145
146 if (fd == fddir || fd == lxc_log_fd)
147 continue;
148
149 if (match_fd(fd))
150 continue;
151 /*
152 * found inherited fd
153 */
154 ret = -1;
155
156 snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fd);
157
158 if (readlink(procpath, path, sizeof(path)) == -1)
159 ERROR("readlink(%s) failed : %m", procpath);
160 else
161 ERROR("inherited fd %d on %s", fd, path);
162 }
163
164 if (closedir(dir))
165 ERROR("failed to close directory");
166 return ret;
167}
168
b0a33c1e 169static int setup_sigchld_fd(sigset_t *oldmask)
170{
171 sigset_t mask;
172 int fd;
173
174 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
36eb9bde 175 SYSERROR("failed to get mask signal");
b0a33c1e 176 return -1;
177 }
178
179 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
36eb9bde 180 SYSERROR("failed to set mask signal");
b0a33c1e 181 return -1;
182 }
183
184 fd = signalfd(-1, &mask, 0);
185 if (fd < 0) {
36eb9bde 186 SYSERROR("failed to create the signal fd");
b0a33c1e 187 return -1;
188 }
189
190 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 191 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 192 close(fd);
193 return -1;
194 }
195
1ac470c0
DL
196 DEBUG("sigchild handler set");
197
b0a33c1e 198 return fd;
199}
200
b0a33c1e 201static int sigchld_handler(int fd, void *data,
202 struct lxc_epoll_descr *descr)
203{
1ac470c0
DL
204 DEBUG("child exited");
205
b0a33c1e 206 return 1;
207}
208
0a3ec350
DL
209int lxc_pid_callback(int fd, struct lxc_request *request,
210 struct lxc_handler *handler)
81c75799
DL
211{
212 struct lxc_answer answer;
213 int ret;
214
215 answer.pid = handler->pid;
216 answer.ret = 0;
217
218 ret = send(fd, &answer, sizeof(answer), 0);
219 if (ret < 0) {
220 WARN("failed to send answer to the peer");
221 return -1;
222 }
223
224 if (ret != sizeof(answer)) {
225 ERROR("partial answer sent");
226 return -1;
227 }
228
229 return 0;
230}
231
25c2aca5 232int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
233{
234 handler->state = state;
235 lxc_monitor_send_state(name, state);
236 return 0;
237}
238
1bc5cc8c 239int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 240{
ca5f7926
DL
241 int sigfd = handler->sigfd;
242 int pid = handler->pid;
b0a33c1e 243 struct lxc_epoll_descr descr;
244
a9e61274 245 if (lxc_mainloop_open(&descr)) {
36eb9bde 246 ERROR("failed to create mainloop");
50c8bf05 247 goto out_sigfd;
b0a33c1e 248 }
249
250 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
36eb9bde 251 ERROR("failed to add handler for the signal");
b0a33c1e 252 goto out_mainloop_open;
253 }
254
63376d7d
DL
255 if (lxc_console_mainloop_add(&descr, handler)) {
256 ERROR("failed to add console handler to mainloop");
257 goto out_mainloop_open;
258 }
259
563f2f2c
DL
260 if (lxc_command_mainloop_add(name, &descr, handler)) {
261 ERROR("failed to add command handler to mainloop");
96fa1ff0 262 goto out_mainloop_open;
563f2f2c
DL
263 }
264
265 if (lxc_utmp_mainloop_add(&descr, handler)) {
266 ERROR("failed to add utmp handler to mainloop");
267 goto out_mainloop_open;
268 }
b0a33c1e 269
c3e13372 270 return lxc_mainloop(&descr);
b0a33c1e 271
272out_mainloop_open:
273 lxc_mainloop_close(&descr);
b0a33c1e 274out_sigfd:
275 close(sigfd);
c3e13372 276 return -1;
b0a33c1e 277}
278
fae349da 279struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
59eb99ba 280{
3a0f472d
DL
281 struct lxc_handler *handler;
282
283 handler = malloc(sizeof(*handler));
284 if (!handler)
285 return NULL;
59eb99ba
DL
286
287 memset(handler, 0, sizeof(*handler));
288
fae349da
DL
289 handler->conf = conf;
290
3bdf52d7
DL
291 handler->name = strdup(name);
292 if (!handler->name) {
293 ERROR("failed to allocate memory");
294 goto out_free;
295 }
296
0ad19a3f 297 /* Begin the set the state to STARTING*/
25c2aca5 298 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 299 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
3bdf52d7 300 goto out_free_name;
0ad19a3f 301 }
302
fae349da 303 if (lxc_create_tty(name, conf)) {
36eb9bde 304 ERROR("failed to create the ttys");
59eb99ba 305 goto out_aborting;
b0a33c1e 306 }
307
1560f6c9 308 if (lxc_create_console(conf)) {
63376d7d
DL
309 ERROR("failed to create console");
310 goto out_delete_tty;
311 }
312
b0a33c1e 313 /* the signal fd has to be created before forking otherwise
314 * if the child process exits before we setup the signal fd,
315 * the event will be lost and the command will be stuck */
59eb99ba
DL
316 handler->sigfd = setup_sigchld_fd(&handler->oldmask);
317 if (handler->sigfd < 0) {
36eb9bde 318 ERROR("failed to set sigchild fd handler");
63376d7d 319 goto out_delete_console;
b0a33c1e 320 }
321
59eb99ba
DL
322 /* Avoid signals from terminal */
323 LXC_TTY_ADD_HANDLER(SIGINT);
324 LXC_TTY_ADD_HANDLER(SIGQUIT);
325
c3e13372 326 INFO("'%s' is initialized", name);
3a0f472d 327 return handler;
59eb99ba 328
63376d7d
DL
329out_delete_console:
330 lxc_delete_console(&conf->console);
59eb99ba 331out_delete_tty:
fae349da 332 lxc_delete_tty(&conf->tty_info);
59eb99ba 333out_aborting:
25c2aca5 334 lxc_set_state(name, handler, ABORTING);
3bdf52d7
DL
335out_free_name:
336 free(handler->name);
337 handler->name = NULL;
3a0f472d
DL
338out_free:
339 free(handler);
c3e13372 340 return NULL;
59eb99ba
DL
341}
342
1bc5cc8c 343void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
344{
345 /* The STOPPING state is there for future cleanup code
346 * which can take awhile
347 */
25c2aca5
MN
348 lxc_set_state(name, handler, STOPPING);
349 lxc_set_state(name, handler, STOPPED);
59eb99ba
DL
350 lxc_unlink_nsgroup(name);
351
63376d7d 352 lxc_delete_console(&handler->conf->console);
b2431939 353 lxc_delete_tty(&handler->conf->tty_info);
3bdf52d7 354 free(handler->name);
b2431939 355 free(handler);
59eb99ba
DL
356
357 LXC_TTY_DEL_HANDLER(SIGQUIT);
358 LXC_TTY_DEL_HANDLER(SIGINT);
359}
360
1bc5cc8c 361void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 362{
25c2aca5 363 lxc_set_state(name, handler, ABORTING);
7d9fb3e9
DL
364 if (handler->pid > 0)
365 kill(handler->pid, SIGKILL);
59eb99ba
DL
366}
367
50e98013
DL
368struct start_arg {
369 const char *name;
370 char *const *argv;
371 struct lxc_handler *handler;
372 int *sv;
373};
374
375static int do_start(void *arg)
376{
377 struct start_arg *start_arg = arg;
378 struct lxc_handler *handler = start_arg->handler;
379 const char *name = start_arg->name;
380 char *const *argv = start_arg->argv;
381 int *sv = start_arg->sv;
382 int err = -1, sync;
383
384 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
385 SYSERROR("failed to set sigprocmask");
9d7f9e52 386 return -1;
50e98013
DL
387 }
388
389 close(sv[1]);
390
391 /* Be sure we don't inherit this after the exec */
392 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
393
394 /* Tell our father he can begin to configure the container */
395 if (write(sv[0], &sync, sizeof(sync)) < 0) {
396 SYSERROR("failed to write socket");
9d7f9e52 397 return -1;
50e98013
DL
398 }
399
400 /* Wait for the father to finish the configuration */
401 if (read(sv[0], &sync, sizeof(sync)) < 0) {
402 SYSERROR("failed to read socket");
9d7f9e52 403 return -1;
50e98013
DL
404 }
405
406 /* Setup the container, ip, names, utsname, ... */
fae349da 407 if (lxc_setup(name, handler->conf)) {
50e98013
DL
408 ERROR("failed to setup the container");
409 goto out_warn_father;
410 }
411
412 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
413 SYSERROR("failed to remove CAP_SYS_BOOT capability");
9d7f9e52 414 return -1;
50e98013
DL
415 }
416
6a6ad7af
DL
417 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
418 SYSERROR("failed to set pdeath signal");
9d7f9e52 419 return -1;
6a6ad7af
DL
420 }
421
1ac470c0
DL
422 NOTICE("exec'ing '%s'", argv[0]);
423
50e98013
DL
424 execvp(argv[0], argv);
425 SYSERROR("failed to exec %s", argv[0]);
426
427out_warn_father:
428 /* If the exec fails, tell that to our father */
429 if (write(sv[0], &err, sizeof(err)) < 0)
430 SYSERROR("failed to write the socket");
50e98013
DL
431 return -1;
432}
433
9618063c 434int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
59eb99ba
DL
435{
436 int sv[2];
437 int clone_flags;
438 int err = -1, sync;
439
50e98013
DL
440 struct start_arg start_arg = {
441 .name = name,
442 .argv = argv,
443 .handler = handler,
444 .sv = sv,
445 };
446
0ad19a3f 447 /* Synchro socketpair */
448 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
36eb9bde 449 SYSERROR("failed to create communication socketpair");
9d7f9e52 450 return -1;
0ad19a3f 451 }
452
1ea6db29 453 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
fae349da 454 if (!lxc_list_empty(&handler->conf->network)) {
82d5ae15 455
0ad19a3f 456 clone_flags |= CLONE_NEWNET;
457
82d5ae15
DL
458 /* that should be done before the clone because we will
459 * fill the netdev index and use them in the child
460 */
fae349da 461 if (lxc_create_network(&handler->conf->network)) {
82d5ae15
DL
462 ERROR("failed to create the network");
463 goto out_close;
464 }
465 }
466
0ad19a3f 467 /* Create a process in a new set of namespaces */
50e98013 468 handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
59eb99ba 469 if (handler->pid < 0) {
36eb9bde 470 SYSERROR("failed to fork into a new namespace");
7fef7a06 471 goto out_delete_net;
0ad19a3f 472 }
473
0ad19a3f 474 close(sv[0]);
475
476 /* Wait for the child to be ready */
477 if (read(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 478 SYSERROR("failed to read the socket");
7fef7a06 479 goto out_delete_net;
0ad19a3f 480 }
481
9f44c578 482 if (lxc_rename_nsgroup(name, handler))
7fef7a06 483 goto out_delete_net;
218d4250 484
0ad19a3f 485 /* Create the network configuration */
82d5ae15 486 if (clone_flags & CLONE_NEWNET) {
fae349da 487 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15 488 ERROR("failed to create the configured network");
7fef7a06 489 goto out_delete_net;
82d5ae15 490 }
0ad19a3f 491 }
492
493 /* Tell the child to continue its initialization */
494 if (write(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 495 SYSERROR("failed to write the socket");
59eb99ba 496 goto out_abort;
0ad19a3f 497 }
498
499 /* Wait for the child to exec or returning an error */
e043236e 500 if (read(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 501 ERROR("failed to read the socket");
59eb99ba 502 goto out_abort;
0ad19a3f 503 }
504
25c2aca5 505 if (lxc_set_state(name, handler, RUNNING)) {
59eb99ba
DL
506 ERROR("failed to set state to %s",
507 lxc_state2str(RUNNING));
508 goto out_abort;
3f21c114 509 }
22ebac19 510
59eb99ba 511 err = 0;
22ebac19 512
1ac470c0
DL
513 NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
514
59eb99ba
DL
515out_close:
516 close(sv[0]);
517 close(sv[1]);
59eb99ba 518 return err;
0ad19a3f 519
7fef7a06
DL
520out_delete_net:
521 if (clone_flags & CLONE_NEWNET)
522 lxc_delete_network(&handler->conf->network);
59eb99ba
DL
523out_abort:
524 lxc_abort(name, handler);
b79fcd86
GK
525 close(sv[1]);
526 return -1;
59eb99ba 527}
0ad19a3f 528
fae349da 529int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
59eb99ba 530{
3a0f472d 531 struct lxc_handler *handler;
e043236e 532 int err = -1;
59eb99ba
DL
533 int status;
534
80090207
CLG
535 if (lxc_check_inherited())
536 return -1;
537
fae349da 538 handler = lxc_init(name, conf);
3a0f472d 539 if (!handler) {
59eb99ba 540 ERROR("failed to initialize the container");
66aeffc7 541 return -1;
0ad19a3f 542 }
543
3a0f472d 544 err = lxc_spawn(name, handler, argv);
59eb99ba
DL
545 if (err) {
546 ERROR("failed to spawn '%s'", argv[0]);
9d7f9e52 547 goto out_fini;
0ad19a3f 548 }
549
affaa6da
MN
550 /* no need of other inherited fds but stderr */
551 close(fileno(stdin));
552 close(fileno(stdout));
553
3a0f472d 554 err = lxc_poll(name, handler);
e043236e 555 if (err) {
59eb99ba
DL
556 ERROR("mainloop exited with an error");
557 goto out_abort;
558 }
0ad19a3f 559
3a0f472d 560 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 561 continue;
e043236e 562
91480a0f
DL
563 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
564 WARN("failed to restore sigprocmask");
565
3a0f472d 566 err = lxc_error_set_and_log(handler->pid, status);
9d7f9e52 567out_fini:
3a0f472d 568 lxc_fini(name, handler);
0ad19a3f 569 return err;
570
59eb99ba 571out_abort:
3a0f472d 572 lxc_abort(name, handler);
9d7f9e52 573 goto out_fini;
0ad19a3f 574}