]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
rename network type enum
[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>
0ad19a3f 39#include <sys/types.h>
0ad19a3f 40#include <sys/prctl.h>
ddceb1f9 41#include <sys/types.h>
42ff343d 42#include <sys/capability.h>
0ad19a3f 43#include <sys/wait.h>
b0a33c1e 44#include <sys/un.h>
45#include <sys/poll.h>
ff218c25 46
47#ifdef HAVE_SYS_SIGNALFD_H
8ca61733 48# include <sys/signalfd.h>
ff218c25 49#else
8ca61733
MJ
50# ifndef __NR_signalfd4
51/* assume kernel headers are too old */
52# if __i386__
53# define __NR_signalfd4 327
54# elif __x86_64__
55# define __NR_signalfd4 289
bfa38025
MH
56# elif __powerpc__
57# define __NR_signalfd4 313
47f38330
SH
58# elif __s390x__
59# define __NR_signalfd4 322
8ca61733
MJ
60# endif
61#endif
62
63# ifndef __NR_signalfd
64/* assume kernel headers are too old */
65# if __i386__
66# define __NR_signalfd 321
67# elif __x86_64__
68# define __NR_signalfd 282
bfa38025
MH
69# elif __powerpc__
70# define __NR_signalfd 305
47f38330
SH
71# elif __s390x__
72# define __NR_signalfd 316
8ca61733
MJ
73# endif
74#endif
75
76int signalfd(int fd, const sigset_t *mask, int flags)
77{
78 int retval;
79
80 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
81 if (errno == ENOSYS && flags == 0)
82 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
83 return retval;
84}
ff218c25 85#endif
0ad19a3f 86
656994bb
MH
87#if !HAVE_DECL_PR_CAPBSET_DROP
88#define PR_CAPBSET_DROP 24
89#endif
90
00b3c2e2
CLG
91#include <lxc/log.h>
92#include <lxc/conf.h>
93#include <lxc/confile.h>
94#include <lxc/start.h>
95#include <lxc/utils.h>
96#include <lxc/cgroup.h>
97#include <lxc/monitor.h>
98
e2bcd7db 99#include "error.h"
b0a33c1e 100#include "af_unix.h"
101#include "mainloop.h"
96fa1ff0 102#include "commands.h"
e2bcd7db 103
36eb9bde
CLG
104
105lxc_log_define(lxc_start, lxc);
106
0ad19a3f 107LXC_TTY_HANDLER(SIGINT);
108LXC_TTY_HANDLER(SIGQUIT);
109
b0a33c1e 110static int setup_sigchld_fd(sigset_t *oldmask)
111{
112 sigset_t mask;
113 int fd;
114
115 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
36eb9bde 116 SYSERROR("failed to get mask signal");
b0a33c1e 117 return -1;
118 }
119
120 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
36eb9bde 121 SYSERROR("failed to set mask signal");
b0a33c1e 122 return -1;
123 }
124
125 fd = signalfd(-1, &mask, 0);
126 if (fd < 0) {
36eb9bde 127 SYSERROR("failed to create the signal fd");
b0a33c1e 128 return -1;
129 }
130
131 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 132 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 133 close(fd);
134 return -1;
135 }
136
1ac470c0
DL
137 DEBUG("sigchild handler set");
138
b0a33c1e 139 return fd;
140}
141
b0a33c1e 142static int sigchld_handler(int fd, void *data,
143 struct lxc_epoll_descr *descr)
144{
1ac470c0
DL
145 DEBUG("child exited");
146
b0a33c1e 147 return 1;
148}
149
25c2aca5 150int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
151{
152 handler->state = state;
153 lxc_monitor_send_state(name, state);
154 return 0;
155}
156
1bc5cc8c 157int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 158{
ca5f7926
DL
159 int sigfd = handler->sigfd;
160 int pid = handler->pid;
b0a33c1e 161 struct lxc_epoll_descr descr;
162
a9e61274 163 if (lxc_mainloop_open(&descr)) {
36eb9bde 164 ERROR("failed to create mainloop");
50c8bf05 165 goto out_sigfd;
b0a33c1e 166 }
167
168 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
36eb9bde 169 ERROR("failed to add handler for the signal");
b0a33c1e 170 goto out_mainloop_open;
171 }
172
724e753c 173 if (lxc_command_mainloop_add(name, &descr, handler))
96fa1ff0 174 goto out_mainloop_open;
b0a33c1e 175
c3e13372 176 return lxc_mainloop(&descr);
b0a33c1e 177
178out_mainloop_open:
179 lxc_mainloop_close(&descr);
b0a33c1e 180out_sigfd:
181 close(sigfd);
c3e13372 182 return -1;
b0a33c1e 183}
184
b8f57738
DL
185static int fdname(int fd, char *name, size_t size)
186{
187 char path[MAXPATHLEN];
acc86941 188 ssize_t len;
b8f57738
DL
189
190 snprintf(path, MAXPATHLEN, "/proc/self/fd/%d", fd);
191
acc86941
MN
192 len = readlink(path, name, size);
193 if (len > 0)
194 path[len] = '\0';
195
196 return (len <= 0) ? -1 : 0;
b8f57738
DL
197}
198
199static int console_init(char *console, size_t size)
200{
201 struct stat stat;
202 int i;
203
204 for (i = 0; i < 3; i++) {
205 if (!isatty(i))
206 continue;
207
208 if (ttyname_r(i, console, size)) {
209 SYSERROR("failed to retrieve tty name");
210 return -1;
211 }
af795875 212
b8f57738
DL
213 return 0;
214 }
215
216 if (!fstat(0, &stat)) {
217 if (S_ISREG(stat.st_mode) || S_ISCHR(stat.st_mode) ||
218 S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode))
219 return fdname(0, console, size);
220 }
221
222 console[0] = '\0';
1ac470c0
DL
223
224 DEBUG("console initialized");
225
b8f57738
DL
226 return 0;
227}
228
fae349da 229struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
59eb99ba 230{
3a0f472d
DL
231 struct lxc_handler *handler;
232
233 handler = malloc(sizeof(*handler));
234 if (!handler)
235 return NULL;
59eb99ba
DL
236
237 memset(handler, 0, sizeof(*handler));
238
fae349da
DL
239 handler->conf = conf;
240
0ad19a3f 241 /* Begin the set the state to STARTING*/
25c2aca5 242 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 243 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
884866b3 244 goto out_free;
0ad19a3f 245 }
246
fae349da 247 if (console_init(conf->console, sizeof(conf->console))) {
b8f57738
DL
248 ERROR("failed to initialize the console");
249 goto out_aborting;
250 }
939229eb 251
fae349da 252 if (lxc_create_tty(name, conf)) {
36eb9bde 253 ERROR("failed to create the ttys");
59eb99ba 254 goto out_aborting;
b0a33c1e 255 }
256
257 /* the signal fd has to be created before forking otherwise
258 * if the child process exits before we setup the signal fd,
259 * the event will be lost and the command will be stuck */
59eb99ba
DL
260 handler->sigfd = setup_sigchld_fd(&handler->oldmask);
261 if (handler->sigfd < 0) {
36eb9bde 262 ERROR("failed to set sigchild fd handler");
59eb99ba 263 goto out_delete_tty;
b0a33c1e 264 }
265
59eb99ba
DL
266 /* Avoid signals from terminal */
267 LXC_TTY_ADD_HANDLER(SIGINT);
268 LXC_TTY_ADD_HANDLER(SIGQUIT);
269
c3e13372 270 INFO("'%s' is initialized", name);
3a0f472d 271 return handler;
59eb99ba
DL
272
273out_delete_tty:
fae349da 274 lxc_delete_tty(&conf->tty_info);
59eb99ba 275out_aborting:
25c2aca5 276 lxc_set_state(name, handler, ABORTING);
3a0f472d
DL
277out_free:
278 free(handler);
c3e13372 279 return NULL;
59eb99ba
DL
280}
281
1bc5cc8c 282void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
283{
284 /* The STOPPING state is there for future cleanup code
285 * which can take awhile
286 */
25c2aca5
MN
287 lxc_set_state(name, handler, STOPPING);
288 lxc_set_state(name, handler, STOPPED);
59eb99ba
DL
289 lxc_unlink_nsgroup(name);
290
b2431939
GK
291 lxc_delete_tty(&handler->conf->tty_info);
292 free(handler);
59eb99ba
DL
293
294 LXC_TTY_DEL_HANDLER(SIGQUIT);
295 LXC_TTY_DEL_HANDLER(SIGINT);
296}
297
1bc5cc8c 298void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 299{
25c2aca5 300 lxc_set_state(name, handler, ABORTING);
59eb99ba
DL
301 kill(handler->pid, SIGKILL);
302}
303
50e98013
DL
304struct start_arg {
305 const char *name;
306 char *const *argv;
307 struct lxc_handler *handler;
308 int *sv;
309};
310
311static int do_start(void *arg)
312{
313 struct start_arg *start_arg = arg;
314 struct lxc_handler *handler = start_arg->handler;
315 const char *name = start_arg->name;
316 char *const *argv = start_arg->argv;
317 int *sv = start_arg->sv;
318 int err = -1, sync;
319
320 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
321 SYSERROR("failed to set sigprocmask");
9d7f9e52 322 return -1;
50e98013
DL
323 }
324
325 close(sv[1]);
326
327 /* Be sure we don't inherit this after the exec */
328 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
329
330 /* Tell our father he can begin to configure the container */
331 if (write(sv[0], &sync, sizeof(sync)) < 0) {
332 SYSERROR("failed to write socket");
9d7f9e52 333 return -1;
50e98013
DL
334 }
335
336 /* Wait for the father to finish the configuration */
337 if (read(sv[0], &sync, sizeof(sync)) < 0) {
338 SYSERROR("failed to read socket");
9d7f9e52 339 return -1;
50e98013
DL
340 }
341
342 /* Setup the container, ip, names, utsname, ... */
fae349da 343 if (lxc_setup(name, handler->conf)) {
50e98013
DL
344 ERROR("failed to setup the container");
345 goto out_warn_father;
346 }
347
348 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
349 SYSERROR("failed to remove CAP_SYS_BOOT capability");
9d7f9e52 350 return -1;
50e98013
DL
351 }
352
6a6ad7af
DL
353 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
354 SYSERROR("failed to set pdeath signal");
9d7f9e52 355 return -1;
6a6ad7af
DL
356 }
357
1ac470c0
DL
358 NOTICE("exec'ing '%s'", argv[0]);
359
50e98013
DL
360 execvp(argv[0], argv);
361 SYSERROR("failed to exec %s", argv[0]);
362
363out_warn_father:
364 /* If the exec fails, tell that to our father */
365 if (write(sv[0], &err, sizeof(err)) < 0)
366 SYSERROR("failed to write the socket");
50e98013
DL
367 return -1;
368}
369
9618063c 370int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
59eb99ba
DL
371{
372 int sv[2];
373 int clone_flags;
374 int err = -1, sync;
375
50e98013
DL
376 struct start_arg start_arg = {
377 .name = name,
378 .argv = argv,
379 .handler = handler,
380 .sv = sv,
381 };
382
0ad19a3f 383 /* Synchro socketpair */
384 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
36eb9bde 385 SYSERROR("failed to create communication socketpair");
9d7f9e52 386 return -1;
0ad19a3f 387 }
388
1ea6db29 389 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
fae349da 390 if (!lxc_list_empty(&handler->conf->network)) {
82d5ae15 391
0ad19a3f 392 clone_flags |= CLONE_NEWNET;
393
82d5ae15
DL
394 /* that should be done before the clone because we will
395 * fill the netdev index and use them in the child
396 */
fae349da 397 if (lxc_create_network(&handler->conf->network)) {
82d5ae15
DL
398 ERROR("failed to create the network");
399 goto out_close;
400 }
401 }
402
0ad19a3f 403 /* Create a process in a new set of namespaces */
50e98013 404 handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
59eb99ba 405 if (handler->pid < 0) {
36eb9bde 406 SYSERROR("failed to fork into a new namespace");
59eb99ba 407 goto out_close;
0ad19a3f 408 }
409
0ad19a3f 410 close(sv[0]);
411
412 /* Wait for the child to be ready */
413 if (read(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 414 SYSERROR("failed to read the socket");
59eb99ba 415 goto out_abort;
0ad19a3f 416 }
417
9f44c578 418 if (lxc_rename_nsgroup(name, handler))
2b31f553 419 goto out_abort;
218d4250 420
0ad19a3f 421 /* Create the network configuration */
82d5ae15 422 if (clone_flags & CLONE_NEWNET) {
fae349da 423 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15
DL
424 ERROR("failed to create the configured network");
425 goto out_abort;
426 }
0ad19a3f 427 }
428
429 /* Tell the child to continue its initialization */
430 if (write(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 431 SYSERROR("failed to write the socket");
59eb99ba 432 goto out_abort;
0ad19a3f 433 }
434
435 /* Wait for the child to exec or returning an error */
e043236e 436 if (read(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 437 ERROR("failed to read the socket");
59eb99ba 438 goto out_abort;
0ad19a3f 439 }
440
25c2aca5 441 if (lxc_set_state(name, handler, RUNNING)) {
59eb99ba
DL
442 ERROR("failed to set state to %s",
443 lxc_state2str(RUNNING));
444 goto out_abort;
3f21c114 445 }
22ebac19 446
59eb99ba 447 err = 0;
22ebac19 448
1ac470c0
DL
449 NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
450
59eb99ba
DL
451out_close:
452 close(sv[0]);
453 close(sv[1]);
59eb99ba 454 return err;
0ad19a3f 455
59eb99ba
DL
456out_abort:
457 lxc_abort(name, handler);
b79fcd86
GK
458 close(sv[1]);
459 return -1;
59eb99ba 460}
0ad19a3f 461
fae349da 462int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
59eb99ba 463{
3a0f472d 464 struct lxc_handler *handler;
e043236e 465 int err = -1;
59eb99ba
DL
466 int status;
467
fae349da 468 handler = lxc_init(name, conf);
3a0f472d 469 if (!handler) {
59eb99ba 470 ERROR("failed to initialize the container");
66aeffc7 471 return -1;
0ad19a3f 472 }
473
3a0f472d 474 err = lxc_spawn(name, handler, argv);
59eb99ba
DL
475 if (err) {
476 ERROR("failed to spawn '%s'", argv[0]);
9d7f9e52 477 goto out_fini;
0ad19a3f 478 }
479
af795875 480 err = lxc_close_all_inherited_fd();
d983b93c
MN
481 if (err) {
482 ERROR("unable to close inherited fds");
483 goto out_abort;
484 }
485
3a0f472d 486 err = lxc_poll(name, handler);
e043236e 487 if (err) {
59eb99ba
DL
488 ERROR("mainloop exited with an error");
489 goto out_abort;
490 }
0ad19a3f 491
3a0f472d 492 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 493 continue;
e043236e 494
3a0f472d 495 err = lxc_error_set_and_log(handler->pid, status);
9d7f9e52 496out_fini:
3a0f472d 497 lxc_fini(name, handler);
0ad19a3f 498 return err;
499
59eb99ba 500out_abort:
3a0f472d 501 lxc_abort(name, handler);
9d7f9e52 502 goto out_fini;
0ad19a3f 503}