]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
Add check for sys/timerfd.h
[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
f549edcc
GK
24#include "config.h"
25
0ad19a3f 26#include <stdio.h>
27#undef _GNU_SOURCE
28#include <string.h>
29#include <stdlib.h>
30#include <dirent.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
b0a33c1e 34#include <fcntl.h>
35#include <termios.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>
8173e600 41#include <sys/socket.h>
0ad19a3f 42#include <sys/prctl.h>
ddceb1f9 43#include <sys/types.h>
0ad19a3f 44#include <sys/wait.h>
b0a33c1e 45#include <sys/un.h>
46#include <sys/poll.h>
8173e600 47#include <sys/syscall.h>
ff218c25 48
495d2046
SG
49#if HAVE_SYS_CAPABILITY_H
50#include <sys/capability.h>
51#endif
52
15cd25fd 53#ifdef HAVE_SYS_SIGNALFD_H
8ca61733 54# include <sys/signalfd.h>
ff218c25 55#else
15cd25fd 56/* assume kernel headers are too old */
09d1bd23 57#include <stdint.h>
15cd25fd
DL
58struct signalfd_siginfo
59{
da2aef7f
DL
60 uint32_t ssi_signo;
61 int32_t ssi_errno;
62 int32_t ssi_code;
63 uint32_t ssi_pid;
64 uint32_t ssi_uid;
65 int32_t ssi_fd;
66 uint32_t ssi_tid;
67 uint32_t ssi_band;
68 uint32_t ssi_overrun;
69 uint32_t ssi_trapno;
70 int32_t ssi_status;
71 int32_t ssi_int;
72 uint64_t ssi_ptr;
73 uint64_t ssi_utime;
74 uint64_t ssi_stime;
75 uint64_t ssi_addr;
76 uint8_t __pad[48];
15cd25fd
DL
77};
78
8ca61733
MJ
79# ifndef __NR_signalfd4
80/* assume kernel headers are too old */
81# if __i386__
82# define __NR_signalfd4 327
83# elif __x86_64__
84# define __NR_signalfd4 289
bfa38025
MH
85# elif __powerpc__
86# define __NR_signalfd4 313
47f38330
SH
87# elif __s390x__
88# define __NR_signalfd4 322
8ca61733
MJ
89# endif
90#endif
91
92# ifndef __NR_signalfd
93/* assume kernel headers are too old */
94# if __i386__
95# define __NR_signalfd 321
96# elif __x86_64__
97# define __NR_signalfd 282
bfa38025
MH
98# elif __powerpc__
99# define __NR_signalfd 305
47f38330
SH
100# elif __s390x__
101# define __NR_signalfd 316
8ca61733
MJ
102# endif
103#endif
104
105int signalfd(int fd, const sigset_t *mask, int flags)
106{
107 int retval;
108
109 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
110 if (errno == ENOSYS && flags == 0)
111 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
112 return retval;
113}
ff218c25 114#endif
0ad19a3f 115
656994bb
MH
116#if !HAVE_DECL_PR_CAPBSET_DROP
117#define PR_CAPBSET_DROP 24
118#endif
119
63376d7d
DL
120#include "start.h"
121#include "conf.h"
122#include "log.h"
563f2f2c 123#include "cgroup.h"
e2bcd7db 124#include "error.h"
b0a33c1e 125#include "af_unix.h"
126#include "mainloop.h"
63376d7d 127#include "utils.h"
563f2f2c 128#include "utmp.h"
63376d7d 129#include "monitor.h"
96fa1ff0 130#include "commands.h"
63376d7d 131#include "console.h"
3c22086f 132#include "sync.h"
f549edcc 133#include "namespace.h"
e075f5d9 134#include "apparmor.h"
0d0527a9 135#include "lxcseccomp.h"
8173e600 136#include "caps.h"
36eb9bde
CLG
137
138lxc_log_define(lxc_start, lxc);
139
80090207
CLG
140static int match_fd(int fd)
141{
142 return (fd == 0 || fd == 1 || fd == 2);
143}
144
b119f362 145int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore)
80090207
CLG
146{
147 struct dirent dirent, *direntp;
148 int fd, fddir;
149 DIR *dir;
80090207 150
b119f362 151restart:
80090207
CLG
152 dir = opendir("/proc/self/fd");
153 if (!dir) {
154 WARN("failed to open directory: %m");
155 return -1;
156 }
157
158 fddir = dirfd(dir);
159
160 while (!readdir_r(dir, &dirent, &direntp)) {
80090207
CLG
161 if (!direntp)
162 break;
163
164 if (!strcmp(direntp->d_name, "."))
165 continue;
166
167 if (!strcmp(direntp->d_name, ".."))
168 continue;
169
170 fd = atoi(direntp->d_name);
171
f2faa8fa 172 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
80090207
CLG
173 continue;
174
175 if (match_fd(fd))
176 continue;
80090207 177
b119f362
SH
178 if (conf->close_all_fds) {
179 close(fd);
180 closedir(dir);
181 INFO("closed inherited fd %d", fd);
182 goto restart;
183 }
92c7f629 184 WARN("inherited fd %d", fd);
80090207
CLG
185 }
186
92c7f629
GK
187 closedir(dir); /* cannot fail */
188 return 0;
80090207
CLG
189}
190
83ee7875 191static int setup_signal_fd(sigset_t *oldmask)
b0a33c1e 192{
193 sigset_t mask;
194 int fd;
195
f3304a29
FW
196 /* Block everything except serious error signals */
197 if (sigfillset(&mask) ||
198 sigdelset(&mask, SIGILL) ||
199 sigdelset(&mask, SIGSEGV) ||
200 sigdelset(&mask, SIGBUS) ||
201 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
202 SYSERROR("failed to set signal mask");
b0a33c1e 203 return -1;
204 }
205
206 fd = signalfd(-1, &mask, 0);
207 if (fd < 0) {
36eb9bde 208 SYSERROR("failed to create the signal fd");
b0a33c1e 209 return -1;
210 }
211
212 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 213 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 214 close(fd);
215 return -1;
216 }
217
1ac470c0
DL
218 DEBUG("sigchild handler set");
219
b0a33c1e 220 return fd;
221}
222
83ee7875 223static int signal_handler(int fd, void *data,
b0a33c1e 224 struct lxc_epoll_descr *descr)
225{
15cd25fd
DL
226 struct signalfd_siginfo siginfo;
227 int ret;
82d89dce 228 pid_t *pid = data;
15cd25fd
DL
229
230 ret = read(fd, &siginfo, sizeof(siginfo));
231 if (ret < 0) {
f3304a29 232 ERROR("failed to read signal info");
15cd25fd
DL
233 return -1;
234 }
235
236 if (ret != sizeof(siginfo)) {
237 ERROR("unexpected siginfo size");
238 return -1;
239 }
240
f3304a29
FW
241 if (siginfo.ssi_signo != SIGCHLD) {
242 kill(*pid, siginfo.ssi_signo);
243 INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
244 return 0;
245 }
246
15cd25fd
DL
247 if (siginfo.ssi_code == CLD_STOPPED ||
248 siginfo.ssi_code == CLD_CONTINUED) {
249 INFO("container init process was stopped/continued");
250 return 0;
251 }
1ac470c0 252
82d89dce
DL
253 /* more robustness, protect ourself from a SIGCHLD sent
254 * by a process different from the container init
255 */
256 if (siginfo.ssi_pid != *pid) {
257 WARN("invalid pid for SIGCHLD");
258 return 0;
259 }
260
15cd25fd 261 DEBUG("container init process exited");
b0a33c1e 262 return 1;
263}
264
0a3ec350
DL
265int lxc_pid_callback(int fd, struct lxc_request *request,
266 struct lxc_handler *handler)
81c75799
DL
267{
268 struct lxc_answer answer;
269 int ret;
270
271 answer.pid = handler->pid;
272 answer.ret = 0;
273
274 ret = send(fd, &answer, sizeof(answer), 0);
275 if (ret < 0) {
276 WARN("failed to send answer to the peer");
277 return -1;
278 }
279
280 if (ret != sizeof(answer)) {
281 ERROR("partial answer sent");
282 return -1;
283 }
284
285 return 0;
286}
287
d5088cf2
CS
288int lxc_clone_flags_callback(int fd, struct lxc_request *request,
289 struct lxc_handler *handler)
290{
291 struct lxc_answer answer;
292 int ret;
293
294 answer.pid = 0;
295 answer.ret = handler->clone_flags;
296
297 ret = send(fd, &answer, sizeof(answer), 0);
298 if (ret < 0) {
299 WARN("failed to send answer to the peer");
300 return -1;
301 }
302
303 if (ret != sizeof(answer)) {
304 ERROR("partial answer sent");
305 return -1;
306 }
307
308 return 0;
309}
310
25c2aca5 311int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
312{
313 handler->state = state;
314 lxc_monitor_send_state(name, state);
315 return 0;
316}
317
1bc5cc8c 318int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 319{
ca5f7926
DL
320 int sigfd = handler->sigfd;
321 int pid = handler->pid;
b0a33c1e 322 struct lxc_epoll_descr descr;
323
a9e61274 324 if (lxc_mainloop_open(&descr)) {
36eb9bde 325 ERROR("failed to create mainloop");
50c8bf05 326 goto out_sigfd;
b0a33c1e 327 }
328
83ee7875 329 if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
36eb9bde 330 ERROR("failed to add handler for the signal");
b0a33c1e 331 goto out_mainloop_open;
332 }
333
63376d7d
DL
334 if (lxc_console_mainloop_add(&descr, handler)) {
335 ERROR("failed to add console handler to mainloop");
336 goto out_mainloop_open;
337 }
338
563f2f2c
DL
339 if (lxc_command_mainloop_add(name, &descr, handler)) {
340 ERROR("failed to add command handler to mainloop");
96fa1ff0 341 goto out_mainloop_open;
563f2f2c
DL
342 }
343
828695d9 344 if (handler->conf->need_utmp_watch) {
495d2046 345 #if HAVE_SYS_CAPABILITY_H
828695d9
SH
346 if (lxc_utmp_mainloop_add(&descr, handler)) {
347 ERROR("failed to add utmp handler to mainloop");
348 goto out_mainloop_open;
349 }
495d2046
SG
350 #else
351 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support\n");
352 #endif
563f2f2c 353 }
b0a33c1e 354
c3e13372 355 return lxc_mainloop(&descr);
b0a33c1e 356
357out_mainloop_open:
358 lxc_mainloop_close(&descr);
b0a33c1e 359out_sigfd:
360 close(sigfd);
c3e13372 361 return -1;
b0a33c1e 362}
363
4a2ca8b2
SH
364extern int lxc_caps_check(void);
365
fae349da 366struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
59eb99ba 367{
3a0f472d
DL
368 struct lxc_handler *handler;
369
4a2ca8b2
SH
370 if (!lxc_caps_check()) {
371 ERROR("Not running with sufficient privilege");
372 return NULL;
373 }
374
3a0f472d
DL
375 handler = malloc(sizeof(*handler));
376 if (!handler)
377 return NULL;
59eb99ba
DL
378
379 memset(handler, 0, sizeof(*handler));
380
fae349da
DL
381 handler->conf = conf;
382
e075f5d9 383 apparmor_handler_init(handler);
3bdf52d7
DL
384 handler->name = strdup(name);
385 if (!handler->name) {
386 ERROR("failed to allocate memory");
387 goto out_free;
388 }
389
d2e30e99
DE
390 if (lxc_command_init(name, handler))
391 goto out_free_name;
392
8f2c3a70
SH
393 if (lxc_read_seccomp_config(conf) != 0) {
394 ERROR("failed loading seccomp policy");
d2e30e99 395 goto out_close_maincmd_fd;
8f2c3a70
SH
396 }
397
0ad19a3f 398 /* Begin the set the state to STARTING*/
25c2aca5 399 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 400 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
3bdf52d7 401 goto out_free_name;
0ad19a3f 402 }
403
773fb9ca
SH
404 if (run_lxc_hooks(name, "pre-start", conf)) {
405 ERROR("failed to run pre-start hooks for container '%s'.", name);
406 goto out_aborting;
407 }
26ddeedd 408
fae349da 409 if (lxc_create_tty(name, conf)) {
36eb9bde 410 ERROR("failed to create the ttys");
59eb99ba 411 goto out_aborting;
b0a33c1e 412 }
413
1560f6c9 414 if (lxc_create_console(conf)) {
63376d7d
DL
415 ERROR("failed to create console");
416 goto out_delete_tty;
417 }
418
b0a33c1e 419 /* the signal fd has to be created before forking otherwise
420 * if the child process exits before we setup the signal fd,
421 * the event will be lost and the command will be stuck */
83ee7875 422 handler->sigfd = setup_signal_fd(&handler->oldmask);
59eb99ba 423 if (handler->sigfd < 0) {
36eb9bde 424 ERROR("failed to set sigchild fd handler");
63376d7d 425 goto out_delete_console;
b0a33c1e 426 }
427
c3e13372 428 INFO("'%s' is initialized", name);
3a0f472d 429 return handler;
59eb99ba 430
63376d7d
DL
431out_delete_console:
432 lxc_delete_console(&conf->console);
59eb99ba 433out_delete_tty:
fae349da 434 lxc_delete_tty(&conf->tty_info);
59eb99ba 435out_aborting:
25c2aca5 436 lxc_set_state(name, handler, ABORTING);
d2e30e99
DE
437out_close_maincmd_fd:
438 close(conf->maincmd_fd);
439 conf->maincmd_fd = -1;
3bdf52d7
DL
440out_free_name:
441 free(handler->name);
442 handler->name = NULL;
3a0f472d
DL
443out_free:
444 free(handler);
c3e13372 445 return NULL;
59eb99ba
DL
446}
447
1bc5cc8c 448void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
449{
450 /* The STOPPING state is there for future cleanup code
451 * which can take awhile
452 */
25c2aca5
MN
453 lxc_set_state(name, handler, STOPPING);
454 lxc_set_state(name, handler, STOPPED);
59eb99ba 455
773fb9ca
SH
456 if (run_lxc_hooks(name, "post-stop", handler->conf))
457 ERROR("failed to run post-stop hooks for container '%s'.", name);
26ddeedd 458
83ee7875 459 /* reset mask set by setup_signal_fd */
8f64a3f6
MN
460 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
461 WARN("failed to restore sigprocmask");
462
63376d7d 463 lxc_delete_console(&handler->conf->console);
b2431939 464 lxc_delete_tty(&handler->conf->tty_info);
d2e30e99
DE
465 close(handler->conf->maincmd_fd);
466 handler->conf->maincmd_fd = -1;
3bdf52d7 467 free(handler->name);
b2431939 468 free(handler);
59eb99ba
DL
469}
470
1bc5cc8c 471void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 472{
25c2aca5 473 lxc_set_state(name, handler, ABORTING);
7d9fb3e9
DL
474 if (handler->pid > 0)
475 kill(handler->pid, SIGKILL);
59eb99ba
DL
476}
477
828695d9
SH
478#include <sys/reboot.h>
479#include <linux/reboot.h>
480
e2fa1520
SH
481/*
482 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
483 * in a child pid namespace if container reboot support exists.
484 * Otherwise, it will either succeed or return -EPERM.
485 */
486static int container_reboot_supported(void *arg)
828695d9 487{
e2fa1520 488 int *cmd = arg;
828695d9 489 int ret;
828695d9 490
e2fa1520
SH
491 ret = reboot(*cmd);
492 if (ret == -1 && errno == EINVAL)
493 return 1;
494 return 0;
495}
496
497static int must_drop_cap_sys_boot(void)
498{
499 FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
500 int ret, cmd, v;
501 long stack_size = 4096;
502 void *stack = alloca(stack_size) + stack_size;
503 int status;
504 pid_t pid;
505
506 if (!f) {
507 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
828695d9 508 return 1;
e2fa1520 509 }
828695d9
SH
510
511 ret = fscanf(f, "%d", &v);
512 fclose(f);
e2fa1520
SH
513 if (ret != 1) {
514 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
828695d9 515 return 1;
e2fa1520
SH
516 }
517 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
518
519 pid = clone(container_reboot_supported, stack, CLONE_NEWPID | SIGCHLD, &cmd);
520 if (pid < 0) {
521 SYSERROR("failed to clone\n");
522 return -1;
523 }
524 if (wait(&status) < 0) {
525 SYSERROR("unexpected wait error: %m\n");
526 return -1;
527 }
528
529 if (WEXITSTATUS(status) != 1)
828695d9 530 return 1;
e2fa1520 531
828695d9
SH
532 return 0;
533}
534
ffe1e01a 535static int do_start(void *data)
50e98013 536{
23c53af9 537 struct lxc_handler *handler = data;
50e98013
DL
538
539 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
540 SYSERROR("failed to set sigprocmask");
9d7f9e52 541 return -1;
50e98013
DL
542 }
543
743ecd2e
DL
544 /* This prctl must be before the synchro, so if the parent
545 * dies before we set the parent death signal, we will detect
546 * its death with the synchro right after, otherwise we have
547 * a window where the parent can exit before we set the pdeath
548 * signal leading to a unsupervized container.
549 */
550 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
551 SYSERROR("failed to set pdeath signal");
552 return -1;
553 }
554
3c22086f 555 lxc_sync_fini_parent(handler);
50e98013 556
3c22086f
CLG
557 /* Tell the parent task it can begin to configure the
558 * container and wait for it to finish
559 */
560 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
9d7f9e52 561 return -1;
50e98013 562
495d2046 563 #if HAVE_SYS_CAPABILITY_H
69182a31 564 if (handler->conf->need_utmp_watch) {
828695d9
SH
565 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
566 SYSERROR("failed to remove CAP_SYS_BOOT capability");
567 return -1;
568 }
e2fa1520 569 DEBUG("Dropped cap_sys_boot\n");
e2fa1520 570 }
495d2046 571 #endif
e2fa1520
SH
572
573 /* Setup the container, ip, names, utsname, ... */
574 if (lxc_setup(handler->name, handler->conf)) {
575 ERROR("failed to setup the container");
576 goto out_warn_father;
577 }
50e98013 578
e075f5d9
SH
579 if (apparmor_load(handler) < 0)
580 goto out_warn_father;
581
8f2c3a70
SH
582 if (lxc_seccomp_load(handler->conf) != 0)
583 goto out_warn_father;
584
773fb9ca
SH
585 if (run_lxc_hooks(handler->name, "start", handler->conf)) {
586 ERROR("failed to run start hooks for container '%s'.", handler->name);
587 goto out_warn_father;
588 }
fc25b815 589
773fb9ca 590 close(handler->sigfd);
26ddeedd 591
e6126dbe
MN
592 /* after this call, we are in error because this
593 * ops should not return as it execs */
23c53af9 594 if (handler->ops->start(handler, handler->data))
e6126dbe 595 return -1;
50e98013
DL
596
597out_warn_father:
3c22086f 598 lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
50e98013
DL
599 return -1;
600}
601
7b35f3d6
SH
602int save_phys_nics(struct lxc_conf *conf)
603{
604 struct lxc_list *iterator;
605
606 lxc_list_for_each(iterator, &conf->network) {
607 struct lxc_netdev *netdev = iterator->elem;
608
609 if (netdev->type != LXC_NET_PHYS)
610 continue;
611 conf->saved_nics = realloc(conf->saved_nics,
612 (conf->num_savednics+1)*sizeof(struct saved_nic));
613 if (!conf->saved_nics) {
614 SYSERROR("failed to allocate memory");
615 return -1;
616 }
617 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
618 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
619 if (!conf->saved_nics[conf->num_savednics].orig_name) {
620 SYSERROR("failed to allocate memory");
621 return -1;
622 }
623 INFO("stored saved_nic #%d idx %d name %s\n", conf->num_savednics,
624 conf->saved_nics[conf->num_savednics].ifindex,
625 conf->saved_nics[conf->num_savednics].orig_name);
626 conf->num_savednics++;
627 }
628
629 return 0;
630}
631
632
23c53af9 633int lxc_spawn(struct lxc_handler *handler)
59eb99ba 634{
99a6af52 635 int failed_before_rename = 0;
ffe1e01a 636 const char *name = handler->name;
0c547523 637 int pinfd;
50e98013 638
3c22086f 639 if (lxc_sync_init(handler))
9d7f9e52 640 return -1;
0ad19a3f 641
d5088cf2 642 handler->clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
fae349da 643 if (!lxc_list_empty(&handler->conf->network)) {
82d5ae15 644
d5088cf2 645 handler->clone_flags |= CLONE_NEWNET;
0ad19a3f 646
19a26f82
MK
647 /* Find gateway addresses from the link device, which is
648 * no longer accessible inside the container. Do this
649 * before creating network interfaces, since goto
650 * out_delete_net does not work before lxc_clone. */
651 if (lxc_find_gateway_addresses(handler)) {
652 ERROR("failed to find gateway addresses");
653 lxc_sync_fini(handler);
654 return -1;
655 }
656
82d5ae15
DL
657 /* that should be done before the clone because we will
658 * fill the netdev index and use them in the child
659 */
e3b4c4c4 660 if (lxc_create_network(handler)) {
82d5ae15 661 ERROR("failed to create the network");
3c22086f 662 lxc_sync_fini(handler);
32e1c760 663 return -1;
82d5ae15
DL
664 }
665 }
666
7b35f3d6
SH
667 if (save_phys_nics(handler->conf)) {
668 ERROR("failed to save physical nic info");
669 goto out_abort;
670 }
671
0c547523
SH
672 /*
673 * if the rootfs is not a blockdev, prevent the container from
674 * marking it readonly.
675 */
676
677 pinfd = pin_rootfs(handler->conf->rootfs.path);
678 if (pinfd == -1) {
679 ERROR("failed to pin the container's rootfs");
680 goto out_abort;
681 }
682
0ad19a3f 683 /* Create a process in a new set of namespaces */
d5088cf2 684 handler->pid = lxc_clone(do_start, handler, handler->clone_flags);
59eb99ba 685 if (handler->pid < 0) {
36eb9bde 686 SYSERROR("failed to fork into a new namespace");
7fef7a06 687 goto out_delete_net;
0ad19a3f 688 }
689
3c22086f
CLG
690 lxc_sync_fini_child(handler);
691
692 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
99a6af52 693 failed_before_rename = 1;
0ad19a3f 694
36b86299 695 if (lxc_cgroup_create(name, handler->pid))
7fef7a06 696 goto out_delete_net;
218d4250 697
99a6af52
MN
698 if (failed_before_rename)
699 goto out_delete_net;
700
0ad19a3f 701 /* Create the network configuration */
d5088cf2 702 if (handler->clone_flags & CLONE_NEWNET) {
fae349da 703 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15 704 ERROR("failed to create the configured network");
7fef7a06 705 goto out_delete_net;
82d5ae15 706 }
0ad19a3f 707 }
708
3c22086f
CLG
709 /* Tell the child to continue its initialization and wait for
710 * it to exec or return an error
711 */
712 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
713 return -1;
0ad19a3f 714
cc28d0b0
SH
715 if (detect_shared_rootfs())
716 umount2(handler->conf->rootfs.mount, MNT_DETACH);
717
718 if (setup_cgroup(name, &handler->conf->cgroup)) {
719 ERROR("failed to setup the cgroups for '%s'", name);
720 goto out_delete_net;
721 }
722
23c53af9 723 if (handler->ops->post_start(handler, handler->data))
e6126dbe
MN
724 goto out_abort;
725
25c2aca5 726 if (lxc_set_state(name, handler, RUNNING)) {
59eb99ba
DL
727 ERROR("failed to set state to %s",
728 lxc_state2str(RUNNING));
729 goto out_abort;
3f21c114 730 }
22ebac19 731
3c22086f 732 lxc_sync_fini(handler);
0c547523
SH
733
734 if (pinfd >= 0)
735 close(pinfd);
736
e6126dbe 737 return 0;
1ac470c0 738
7fef7a06 739out_delete_net:
d5088cf2 740 if (handler->clone_flags & CLONE_NEWNET)
74a2b586 741 lxc_delete_network(handler);
59eb99ba
DL
742out_abort:
743 lxc_abort(name, handler);
3c22086f 744 lxc_sync_fini(handler);
b79fcd86 745 return -1;
59eb99ba 746}
0ad19a3f 747
ee70bf78
CLG
748int __lxc_start(const char *name, struct lxc_conf *conf,
749 struct lxc_operations* ops, void *data)
59eb99ba 750{
3a0f472d 751 struct lxc_handler *handler;
e043236e 752 int err = -1;
59eb99ba 753 int status;
80090207 754
fae349da 755 handler = lxc_init(name, conf);
3a0f472d 756 if (!handler) {
59eb99ba 757 ERROR("failed to initialize the container");
66aeffc7 758 return -1;
0ad19a3f 759 }
ee70bf78
CLG
760 handler->ops = ops;
761 handler->data = data;
e6126dbe 762
69182a31 763 if (must_drop_cap_sys_boot()) {
495d2046 764 #if HAVE_SYS_CAPABILITY_H
f51db2b3 765 DEBUG("Dropping cap_sys_boot\n");
495d2046
SG
766 #else
767 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported\n");
768 #endif
69182a31
SH
769 } else {
770 DEBUG("Not dropping cap_sys_boot or watching utmp\n");
771 handler->conf->need_utmp_watch = 0;
772 }
773
23c53af9 774 err = lxc_spawn(handler);
59eb99ba 775 if (err) {
ee70bf78 776 ERROR("failed to spawn '%s'", name);
74a2b586 777 goto out_fini_nonet;
0ad19a3f 778 }
779
3a0f472d 780 err = lxc_poll(name, handler);
e043236e 781 if (err) {
59eb99ba
DL
782 ERROR("mainloop exited with an error");
783 goto out_abort;
784 }
0ad19a3f 785
3a0f472d 786 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 787 continue;
e043236e 788
8b004f07
SH
789 /*
790 * If the child process exited but was not signaled,
791 * it didn't call reboot. This should mean it was an
792 * lxc-execute which simply exited. In any case, treat
793 * it as a 'halt'
794 */
795 if (WIFSIGNALED(status)) {
796 switch(WTERMSIG(status)) {
797 case SIGINT: /* halt */
798 DEBUG("Container halting");
799 break;
800 case SIGHUP: /* reboot */
801 DEBUG("Container rebooting");
802 handler->conf->reboot = 1;
803 break;
804 default:
805 DEBUG("unknown exit status for init: %d\n", WTERMSIG(status));
806 break;
807 }
828695d9
SH
808 }
809
7b35f3d6
SH
810 lxc_rename_phys_nics_on_shutdown(handler->conf);
811
3a0f472d 812 err = lxc_error_set_and_log(handler->pid, status);
9d7f9e52 813out_fini:
74a2b586
JK
814 lxc_delete_network(handler);
815
816out_fini_nonet:
36b86299 817 lxc_cgroup_destroy(name);
3a0f472d 818 lxc_fini(name, handler);
0ad19a3f 819 return err;
820
59eb99ba 821out_abort:
3a0f472d 822 lxc_abort(name, handler);
9d7f9e52 823 goto out_fini;
0ad19a3f 824}
ee70bf78
CLG
825
826struct start_args {
827 char *const *argv;
828};
829
830static int start(struct lxc_handler *handler, void* data)
831{
832 struct start_args *arg = data;
833
834 NOTICE("exec'ing '%s'", arg->argv[0]);
835
836 execvp(arg->argv[0], arg->argv);
837 SYSERROR("failed to exec %s", arg->argv[0]);
838 return 0;
839}
840
841static int post_start(struct lxc_handler *handler, void* data)
842{
843 struct start_args *arg = data;
844
845 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
846 return 0;
847}
848
849static struct lxc_operations start_ops = {
850 .start = start,
851 .post_start = post_start
852};
853
854int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
855{
856 struct start_args start_arg = {
857 .argv = argv,
858 };
859
b119f362 860 if (lxc_check_inherited(conf, -1))
ee70bf78
CLG
861 return -1;
862
828695d9 863 conf->need_utmp_watch = 1;
ee70bf78
CLG
864 return __lxc_start(name, conf, &start_ops, &start_arg);
865}