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