]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
lxc-alpine: create /dev/zero
[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:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ad19a3f 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
656994bb
MH
53#if !HAVE_DECL_PR_CAPBSET_DROP
54#define PR_CAPBSET_DROP 24
55#endif
56
63376d7d
DL
57#include "start.h"
58#include "conf.h"
59#include "log.h"
563f2f2c 60#include "cgroup.h"
e2bcd7db 61#include "error.h"
b0a33c1e 62#include "af_unix.h"
63#include "mainloop.h"
63376d7d 64#include "utils.h"
565c2d76 65#include "lxcutmp.h"
63376d7d 66#include "monitor.h"
96fa1ff0 67#include "commands.h"
63376d7d 68#include "console.h"
3c22086f 69#include "sync.h"
f549edcc 70#include "namespace.h"
e075f5d9 71#include "apparmor.h"
0d0527a9 72#include "lxcseccomp.h"
8173e600 73#include "caps.h"
36eb9bde
CLG
74
75lxc_log_define(lxc_start, lxc);
76
80090207
CLG
77static int match_fd(int fd)
78{
79 return (fd == 0 || fd == 1 || fd == 2);
80}
81
b119f362 82int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore)
80090207
CLG
83{
84 struct dirent dirent, *direntp;
85 int fd, fddir;
86 DIR *dir;
80090207 87
b119f362 88restart:
80090207
CLG
89 dir = opendir("/proc/self/fd");
90 if (!dir) {
91 WARN("failed to open directory: %m");
92 return -1;
93 }
94
95 fddir = dirfd(dir);
96
97 while (!readdir_r(dir, &dirent, &direntp)) {
80090207
CLG
98 if (!direntp)
99 break;
100
101 if (!strcmp(direntp->d_name, "."))
102 continue;
103
104 if (!strcmp(direntp->d_name, ".."))
105 continue;
106
107 fd = atoi(direntp->d_name);
108
f2faa8fa 109 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
80090207
CLG
110 continue;
111
112 if (match_fd(fd))
113 continue;
80090207 114
b119f362
SH
115 if (conf->close_all_fds) {
116 close(fd);
117 closedir(dir);
118 INFO("closed inherited fd %d", fd);
119 goto restart;
120 }
92c7f629 121 WARN("inherited fd %d", fd);
80090207
CLG
122 }
123
92c7f629
GK
124 closedir(dir); /* cannot fail */
125 return 0;
80090207
CLG
126}
127
83ee7875 128static int setup_signal_fd(sigset_t *oldmask)
b0a33c1e 129{
130 sigset_t mask;
131 int fd;
132
f3304a29
FW
133 /* Block everything except serious error signals */
134 if (sigfillset(&mask) ||
135 sigdelset(&mask, SIGILL) ||
136 sigdelset(&mask, SIGSEGV) ||
137 sigdelset(&mask, SIGBUS) ||
b5159817 138 sigdelset(&mask, SIGWINCH) ||
f3304a29
FW
139 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
140 SYSERROR("failed to set signal mask");
b0a33c1e 141 return -1;
142 }
143
144 fd = signalfd(-1, &mask, 0);
145 if (fd < 0) {
36eb9bde 146 SYSERROR("failed to create the signal fd");
b0a33c1e 147 return -1;
148 }
149
150 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 151 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 152 close(fd);
153 return -1;
154 }
155
1ac470c0
DL
156 DEBUG("sigchild handler set");
157
b0a33c1e 158 return fd;
159}
160
83ee7875 161static int signal_handler(int fd, void *data,
b0a33c1e 162 struct lxc_epoll_descr *descr)
163{
15cd25fd
DL
164 struct signalfd_siginfo siginfo;
165 int ret;
82d89dce 166 pid_t *pid = data;
15cd25fd
DL
167
168 ret = read(fd, &siginfo, sizeof(siginfo));
169 if (ret < 0) {
f3304a29 170 ERROR("failed to read signal info");
15cd25fd
DL
171 return -1;
172 }
173
174 if (ret != sizeof(siginfo)) {
175 ERROR("unexpected siginfo size");
176 return -1;
177 }
178
f3304a29
FW
179 if (siginfo.ssi_signo != SIGCHLD) {
180 kill(*pid, siginfo.ssi_signo);
181 INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
182 return 0;
183 }
184
15cd25fd
DL
185 if (siginfo.ssi_code == CLD_STOPPED ||
186 siginfo.ssi_code == CLD_CONTINUED) {
187 INFO("container init process was stopped/continued");
188 return 0;
189 }
1ac470c0 190
82d89dce
DL
191 /* more robustness, protect ourself from a SIGCHLD sent
192 * by a process different from the container init
193 */
194 if (siginfo.ssi_pid != *pid) {
195 WARN("invalid pid for SIGCHLD");
196 return 0;
197 }
198
15cd25fd 199 DEBUG("container init process exited");
b0a33c1e 200 return 1;
201}
202
25c2aca5 203int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
204{
205 handler->state = state;
9123e471 206 lxc_monitor_send_state(name, state, handler->lxcpath);
66aeffc7
DL
207 return 0;
208}
209
1bc5cc8c 210int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 211{
ca5f7926
DL
212 int sigfd = handler->sigfd;
213 int pid = handler->pid;
b0a33c1e 214 struct lxc_epoll_descr descr;
215
a9e61274 216 if (lxc_mainloop_open(&descr)) {
36eb9bde 217 ERROR("failed to create mainloop");
50c8bf05 218 goto out_sigfd;
b0a33c1e 219 }
220
83ee7875 221 if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
36eb9bde 222 ERROR("failed to add handler for the signal");
b0a33c1e 223 goto out_mainloop_open;
224 }
225
63376d7d
DL
226 if (lxc_console_mainloop_add(&descr, handler)) {
227 ERROR("failed to add console handler to mainloop");
228 goto out_mainloop_open;
229 }
230
ef6e34ee 231 if (lxc_cmd_mainloop_add(name, &descr, handler)) {
563f2f2c 232 ERROR("failed to add command handler to mainloop");
96fa1ff0 233 goto out_mainloop_open;
563f2f2c
DL
234 }
235
828695d9 236 if (handler->conf->need_utmp_watch) {
495d2046 237 #if HAVE_SYS_CAPABILITY_H
828695d9
SH
238 if (lxc_utmp_mainloop_add(&descr, handler)) {
239 ERROR("failed to add utmp handler to mainloop");
240 goto out_mainloop_open;
241 }
495d2046
SG
242 #else
243 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support\n");
244 #endif
563f2f2c 245 }
b0a33c1e 246
e51d4895 247 return lxc_mainloop(&descr, -1);
b0a33c1e 248
249out_mainloop_open:
250 lxc_mainloop_close(&descr);
b0a33c1e 251out_sigfd:
252 close(sigfd);
c3e13372 253 return -1;
b0a33c1e 254}
255
13f5be62 256struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath)
59eb99ba 257{
3a0f472d
DL
258 struct lxc_handler *handler;
259
260 handler = malloc(sizeof(*handler));
261 if (!handler)
262 return NULL;
59eb99ba
DL
263
264 memset(handler, 0, sizeof(*handler));
265
fae349da 266 handler->conf = conf;
9123e471 267 handler->lxcpath = lxcpath;
fae349da 268
e075f5d9 269 apparmor_handler_init(handler);
3bdf52d7
DL
270 handler->name = strdup(name);
271 if (!handler->name) {
272 ERROR("failed to allocate memory");
273 goto out_free;
274 }
275
ef6e34ee 276 if (lxc_cmd_init(name, handler, lxcpath))
d2e30e99
DE
277 goto out_free_name;
278
8f2c3a70
SH
279 if (lxc_read_seccomp_config(conf) != 0) {
280 ERROR("failed loading seccomp policy");
d2e30e99 281 goto out_close_maincmd_fd;
8f2c3a70
SH
282 }
283
051151de 284 /* Begin by setting the state to STARTING */
25c2aca5 285 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 286 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
051151de 287 goto out_close_maincmd_fd;
0ad19a3f 288 }
289
f7bee6c6
MW
290 /* Start of environment variable setup for hooks */
291 if (setenv("LXC_NAME", name, 1)) {
292 SYSERROR("failed to set environment variable for container name");
293 }
294 if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
295 SYSERROR("failed to set environment variable for config path");
296 }
297 if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
298 SYSERROR("failed to set environment variable for rootfs mount");
299 }
300 if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
301 SYSERROR("failed to set environment variable for rootfs mount");
302 }
303 if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) {
304 SYSERROR("failed to set environment variable for console path");
305 }
306 if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) {
307 SYSERROR("failed to set environment variable for console log");
308 }
309 /* End of environment variable setup for hooks */
310
283678ed 311 if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) {
773fb9ca
SH
312 ERROR("failed to run pre-start hooks for container '%s'.", name);
313 goto out_aborting;
314 }
26ddeedd 315
fae349da 316 if (lxc_create_tty(name, conf)) {
36eb9bde 317 ERROR("failed to create the ttys");
59eb99ba 318 goto out_aborting;
b0a33c1e 319 }
320
321 /* the signal fd has to be created before forking otherwise
322 * if the child process exits before we setup the signal fd,
323 * the event will be lost and the command will be stuck */
83ee7875 324 handler->sigfd = setup_signal_fd(&handler->oldmask);
59eb99ba 325 if (handler->sigfd < 0) {
36eb9bde 326 ERROR("failed to set sigchild fd handler");
b5159817
DE
327 goto out_delete_tty;
328 }
329
330 /* do this after setting up signals since it might unblock SIGWINCH */
331 if (lxc_console_create(conf)) {
332 ERROR("failed to create console");
333 goto out_restore_sigmask;
b0a33c1e 334 }
335
c3e13372 336 INFO("'%s' is initialized", name);
3a0f472d 337 return handler;
59eb99ba 338
b5159817
DE
339out_restore_sigmask:
340 sigprocmask(SIG_SETMASK, &handler->oldmask, NULL);
59eb99ba 341out_delete_tty:
fae349da 342 lxc_delete_tty(&conf->tty_info);
59eb99ba 343out_aborting:
25c2aca5 344 lxc_set_state(name, handler, ABORTING);
d2e30e99
DE
345out_close_maincmd_fd:
346 close(conf->maincmd_fd);
347 conf->maincmd_fd = -1;
3bdf52d7
DL
348out_free_name:
349 free(handler->name);
350 handler->name = NULL;
3a0f472d
DL
351out_free:
352 free(handler);
c3e13372 353 return NULL;
59eb99ba
DL
354}
355
ae5c8b8e 356static void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
357{
358 /* The STOPPING state is there for future cleanup code
359 * which can take awhile
360 */
25c2aca5
MN
361 lxc_set_state(name, handler, STOPPING);
362 lxc_set_state(name, handler, STOPPED);
59eb99ba 363
283678ed 364 if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL))
773fb9ca 365 ERROR("failed to run post-stop hooks for container '%s'.", name);
26ddeedd 366
83ee7875 367 /* reset mask set by setup_signal_fd */
8f64a3f6
MN
368 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
369 WARN("failed to restore sigprocmask");
370
b5159817 371 lxc_console_delete(&handler->conf->console);
b2431939 372 lxc_delete_tty(&handler->conf->tty_info);
d2e30e99
DE
373 close(handler->conf->maincmd_fd);
374 handler->conf->maincmd_fd = -1;
3bdf52d7 375 free(handler->name);
ae5c8b8e 376 if (handler->cgroup) {
b98f7d6e 377 lxc_cgroup_destroy_desc(handler->cgroup);
ae5c8b8e
SH
378 handler->cgroup = NULL;
379 }
b2431939 380 free(handler);
59eb99ba
DL
381}
382
1bc5cc8c 383void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 384{
73e608b2
SH
385 int ret, status;
386
25c2aca5 387 lxc_set_state(name, handler, ABORTING);
7d9fb3e9
DL
388 if (handler->pid > 0)
389 kill(handler->pid, SIGKILL);
73e608b2 390 while ((ret = waitpid(-1, &status, 0)) > 0) ;
59eb99ba
DL
391}
392
828695d9
SH
393#include <sys/reboot.h>
394#include <linux/reboot.h>
395
e2fa1520
SH
396/*
397 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
398 * in a child pid namespace if container reboot support exists.
399 * Otherwise, it will either succeed or return -EPERM.
400 */
401static int container_reboot_supported(void *arg)
828695d9 402{
e2fa1520 403 int *cmd = arg;
828695d9 404 int ret;
828695d9 405
e2fa1520
SH
406 ret = reboot(*cmd);
407 if (ret == -1 && errno == EINVAL)
408 return 1;
409 return 0;
410}
411
b60ed720 412static int must_drop_cap_sys_boot(struct lxc_conf *conf)
e2fa1520
SH
413{
414 FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
b60ed720 415 int ret, cmd, v, flags;
e2fa1520 416 long stack_size = 4096;
7f145a6d 417 void *stack = alloca(stack_size);
e2fa1520
SH
418 int status;
419 pid_t pid;
420
421 if (!f) {
422 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
828695d9 423 return 1;
e2fa1520 424 }
828695d9
SH
425
426 ret = fscanf(f, "%d", &v);
427 fclose(f);
e2fa1520
SH
428 if (ret != 1) {
429 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
828695d9 430 return 1;
e2fa1520
SH
431 }
432 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
433
b60ed720
SH
434 flags = CLONE_NEWPID | SIGCHLD;
435 if (!lxc_list_empty(&conf->id_map))
436 flags |= CLONE_NEWUSER;
437
7f145a6d 438#ifdef __ia64__
b60ed720 439 pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd);
7f145a6d
DS
440#else
441 stack += stack_size;
b60ed720 442 pid = clone(container_reboot_supported, stack, flags, &cmd);
7f145a6d 443#endif
e2fa1520
SH
444 if (pid < 0) {
445 SYSERROR("failed to clone\n");
446 return -1;
447 }
448 if (wait(&status) < 0) {
449 SYSERROR("unexpected wait error: %m\n");
450 return -1;
451 }
452
453 if (WEXITSTATUS(status) != 1)
828695d9 454 return 1;
e2fa1520 455
828695d9
SH
456 return 0;
457}
458
ffe1e01a 459static int do_start(void *data)
50e98013 460{
23c53af9 461 struct lxc_handler *handler = data;
50e98013
DL
462
463 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
464 SYSERROR("failed to set sigprocmask");
9d7f9e52 465 return -1;
50e98013
DL
466 }
467
743ecd2e
DL
468 /* This prctl must be before the synchro, so if the parent
469 * dies before we set the parent death signal, we will detect
470 * its death with the synchro right after, otherwise we have
471 * a window where the parent can exit before we set the pdeath
472 * signal leading to a unsupervized container.
473 */
474 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
475 SYSERROR("failed to set pdeath signal");
476 return -1;
477 }
478
3c22086f 479 lxc_sync_fini_parent(handler);
50e98013 480
2b0e17e4 481 /* don't leak the pinfd to the container */
0d03360a
SH
482 if (handler->pinfd >= 0)
483 close(handler->pinfd);
2b0e17e4 484
3c22086f
CLG
485 /* Tell the parent task it can begin to configure the
486 * container and wait for it to finish
487 */
488 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
9d7f9e52 489 return -1;
50e98013 490
f6d3e3e4
SH
491 /*
492 * if we are in a new user namespace, become root there to have
493 * privilege over our namespace
494 */
495 if (!lxc_list_empty(&handler->conf->id_map)) {
496 NOTICE("switching to gid/uid 0 in new user namespace");
497 if (setgid(0)) {
498 SYSERROR("setgid");
499 goto out_warn_father;
500 }
501 if (setuid(0)) {
502 SYSERROR("setuid");
503 goto out_warn_father;
504 }
505 }
506
495d2046 507 #if HAVE_SYS_CAPABILITY_H
69182a31 508 if (handler->conf->need_utmp_watch) {
828695d9
SH
509 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
510 SYSERROR("failed to remove CAP_SYS_BOOT capability");
c4ea60df 511 goto out_warn_father;
828695d9 512 }
e2fa1520 513 DEBUG("Dropped cap_sys_boot\n");
e2fa1520 514 }
495d2046 515 #endif
e2fa1520
SH
516
517 /* Setup the container, ip, names, utsname, ... */
283678ed 518 if (lxc_setup(handler->name, handler->conf, handler->lxcpath)) {
e2fa1520
SH
519 ERROR("failed to setup the container");
520 goto out_warn_father;
521 }
50e98013 522
544a48a0
SH
523 /* ask father to setup cgroups and wait for him to finish */
524 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP))
525 return -1;
526
e075f5d9
SH
527 if (apparmor_load(handler) < 0)
528 goto out_warn_father;
529
8f2c3a70
SH
530 if (lxc_seccomp_load(handler->conf) != 0)
531 goto out_warn_father;
532
283678ed 533 if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) {
773fb9ca
SH
534 ERROR("failed to run start hooks for container '%s'.", handler->name);
535 goto out_warn_father;
536 }
fc25b815 537
f7bee6c6
MW
538 /* The clearenv() and putenv() calls have been moved here
539 * to allow us to use enviroment variables passed to the various
540 * hooks, such as the start hook above. Not all of the
541 * variables like CONFIG_PATH or ROOTFS are valid in this
542 * context but others are. */
543 if (clearenv()) {
544 SYSERROR("failed to clear environment");
545 /* don't error out though */
546 }
547
548 if (putenv("container=lxc")) {
549 SYSERROR("failed to set environment variable");
c4ea60df 550 goto out_warn_father;
f7bee6c6
MW
551 }
552
773fb9ca 553 close(handler->sigfd);
26ddeedd 554
e6126dbe
MN
555 /* after this call, we are in error because this
556 * ops should not return as it execs */
c4ea60df 557 handler->ops->start(handler, handler->data);
50e98013
DL
558
559out_warn_father:
544a48a0
SH
560 /* we want the parent to know something went wrong, so any
561 * value other than what it expects is ok. */
3c22086f 562 lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
50e98013
DL
563 return -1;
564}
565
7b35f3d6
SH
566int save_phys_nics(struct lxc_conf *conf)
567{
568 struct lxc_list *iterator;
569
570 lxc_list_for_each(iterator, &conf->network) {
571 struct lxc_netdev *netdev = iterator->elem;
572
573 if (netdev->type != LXC_NET_PHYS)
574 continue;
575 conf->saved_nics = realloc(conf->saved_nics,
576 (conf->num_savednics+1)*sizeof(struct saved_nic));
577 if (!conf->saved_nics) {
578 SYSERROR("failed to allocate memory");
579 return -1;
580 }
581 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
582 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
583 if (!conf->saved_nics[conf->num_savednics].orig_name) {
584 SYSERROR("failed to allocate memory");
585 return -1;
586 }
587 INFO("stored saved_nic #%d idx %d name %s\n", conf->num_savednics,
588 conf->saved_nics[conf->num_savednics].ifindex,
589 conf->saved_nics[conf->num_savednics].orig_name);
590 conf->num_savednics++;
591 }
592
593 return 0;
594}
595
b98f7d6e 596extern bool is_in_subcgroup(int pid, const char *subsystem, struct cgroup_desc *d);
23c53af9 597int lxc_spawn(struct lxc_handler *handler)
59eb99ba 598{
b98f7d6e 599 int failed_before_rename = 0;
ffe1e01a 600 const char *name = handler->name;
50e98013 601
3c22086f 602 if (lxc_sync_init(handler))
9d7f9e52 603 return -1;
0ad19a3f 604
d5088cf2 605 handler->clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
f6d3e3e4
SH
606 if (!lxc_list_empty(&handler->conf->id_map)) {
607 INFO("Cloning a new user namespace");
608 handler->clone_flags |= CLONE_NEWUSER;
609 }
fae349da 610 if (!lxc_list_empty(&handler->conf->network)) {
82d5ae15 611
d5088cf2 612 handler->clone_flags |= CLONE_NEWNET;
0ad19a3f 613
19a26f82
MK
614 /* Find gateway addresses from the link device, which is
615 * no longer accessible inside the container. Do this
616 * before creating network interfaces, since goto
617 * out_delete_net does not work before lxc_clone. */
618 if (lxc_find_gateway_addresses(handler)) {
619 ERROR("failed to find gateway addresses");
620 lxc_sync_fini(handler);
621 return -1;
622 }
623
82d5ae15
DL
624 /* that should be done before the clone because we will
625 * fill the netdev index and use them in the child
626 */
e3b4c4c4 627 if (lxc_create_network(handler)) {
82d5ae15 628 ERROR("failed to create the network");
3c22086f 629 lxc_sync_fini(handler);
32e1c760 630 return -1;
82d5ae15
DL
631 }
632 }
633
7b35f3d6
SH
634 if (save_phys_nics(handler->conf)) {
635 ERROR("failed to save physical nic info");
636 goto out_abort;
637 }
638
0c547523
SH
639 /*
640 * if the rootfs is not a blockdev, prevent the container from
641 * marking it readonly.
642 */
643
2b0e17e4
SH
644 handler->pinfd = pin_rootfs(handler->conf->rootfs.path);
645 if (handler->pinfd == -1) {
0c547523 646 ERROR("failed to pin the container's rootfs");
d71d919e 647 goto out_delete_net;
0c547523
SH
648 }
649
0ad19a3f 650 /* Create a process in a new set of namespaces */
d5088cf2 651 handler->pid = lxc_clone(do_start, handler, handler->clone_flags);
59eb99ba 652 if (handler->pid < 0) {
36eb9bde 653 SYSERROR("failed to fork into a new namespace");
7fef7a06 654 goto out_delete_net;
0ad19a3f 655 }
656
3c22086f
CLG
657 lxc_sync_fini_child(handler);
658
659 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
99a6af52 660 failed_before_rename = 1;
0ad19a3f 661
b98f7d6e 662 if ((handler->cgroup = lxc_cgroup_path_create(name)) == NULL)
ae5c8b8e 663 goto out_delete_net;
e51d4895 664
b98f7d6e 665 if (setup_cgroup(handler, &handler->conf->cgroup)) {
6031a6e5
DE
666 ERROR("failed to setup the cgroups for '%s'", name);
667 goto out_delete_net;
668 }
669
ae5c8b8e 670 if (lxc_cgroup_enter(handler->cgroup, handler->pid) < 0)
7fef7a06 671 goto out_delete_net;
218d4250 672
99a6af52
MN
673 if (failed_before_rename)
674 goto out_delete_net;
675
0ad19a3f 676 /* Create the network configuration */
d5088cf2 677 if (handler->clone_flags & CLONE_NEWNET) {
fae349da 678 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15 679 ERROR("failed to create the configured network");
7fef7a06 680 goto out_delete_net;
82d5ae15 681 }
0ad19a3f 682 }
683
f6d3e3e4
SH
684 /* map the container uids - the container became an invalid
685 * userid the moment it was cloned with CLONE_NEWUSER - this
686 * call doesn't change anything immediately, but allows the
687 * container to setuid(0) (0 being mapped to something else on
688 * the host) later to become a valid uid again */
689 if (lxc_map_ids(&handler->conf->id_map, handler->pid)) {
690 ERROR("failed to set up id mapping");
691 goto out_delete_net;
692 }
693
544a48a0
SH
694 /* Tell the child to continue its initialization. we'll get
695 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
3c22086f
CLG
696 */
697 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
544a48a0
SH
698 goto out_delete_net;
699
b98f7d6e
SH
700 if (setup_cgroup_devices(handler, &handler->conf->cgroup)) {
701 ERROR("failed to setup the devices cgroup for '%s'", name);
702 goto out_delete_net;
544a48a0
SH
703 }
704
544a48a0
SH
705 /* Tell the child to complete its initialization and wait for
706 * it to exec or return an error. (the child will never
707 * return LXC_SYNC_POST_CGROUP+1. It will either close the
708 * sync pipe, causing lxc_sync_barrier_child to return
709 * success, or return a different value, causing us to error
710 * out).
711 */
712 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP))
3c22086f 713 return -1;
0ad19a3f 714
cc28d0b0
SH
715 if (detect_shared_rootfs())
716 umount2(handler->conf->rootfs.mount, MNT_DETACH);
717
f6d3e3e4
SH
718 /* If child is in a fresh user namespace, chown his ptys for
719 * it */
720 if (uid_shift_ttys(handler->pid, handler->conf))
721 DEBUG("Failed to chown ptys.\n");
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 733
2b0e17e4
SH
734 if (handler->pinfd >= 0)
735 close(handler->pinfd);
0c547523 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 748int __lxc_start(const char *name, struct lxc_conf *conf,
13f5be62 749 struct lxc_operations* ops, void *data, const char *lxcpath)
59eb99ba 750{
3a0f472d 751 struct lxc_handler *handler;
e043236e 752 int err = -1;
59eb99ba 753 int status;
80090207 754
13f5be62 755 handler = lxc_init(name, conf, lxcpath);
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
b60ed720 763 if (must_drop_cap_sys_boot(handler->conf)) {
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:
3a0f472d 817 lxc_fini(name, handler);
0ad19a3f 818 return err;
819
59eb99ba 820out_abort:
3a0f472d 821 lxc_abort(name, handler);
9d7f9e52 822 goto out_fini;
0ad19a3f 823}
ee70bf78
CLG
824
825struct start_args {
826 char *const *argv;
827};
828
829static int start(struct lxc_handler *handler, void* data)
830{
831 struct start_args *arg = data;
832
833 NOTICE("exec'ing '%s'", arg->argv[0]);
834
835 execvp(arg->argv[0], arg->argv);
836 SYSERROR("failed to exec %s", arg->argv[0]);
837 return 0;
838}
839
840static int post_start(struct lxc_handler *handler, void* data)
841{
842 struct start_args *arg = data;
843
844 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
845 return 0;
846}
847
848static struct lxc_operations start_ops = {
849 .start = start,
850 .post_start = post_start
851};
852
13f5be62
SH
853int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf,
854 const char *lxcpath)
ee70bf78
CLG
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;
13f5be62 864 return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath);
ee70bf78 865}