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