]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
cache whether 'optional' was in mntopts
[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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 22 */
23
f549edcc
GK
24#include "config.h"
25
0ad19a3f 26#include <stdio.h>
0ad19a3f 27#include <string.h>
28#include <stdlib.h>
29#include <dirent.h>
30#include <errno.h>
31#include <unistd.h>
32#include <signal.h>
b0a33c1e 33#include <fcntl.h>
34#include <termios.h>
c476bdce 35#include <grp.h>
37515ebd 36#include <poll.h>
0ad19a3f 37#include <sys/param.h>
38#include <sys/file.h>
f4d507d5 39#include <sys/mount.h>
b4f8660e 40#include <sys/stat.h>
0ad19a3f 41#include <sys/types.h>
8173e600 42#include <sys/socket.h>
0ad19a3f 43#include <sys/prctl.h>
ddceb1f9 44#include <sys/types.h>
0ad19a3f 45#include <sys/wait.h>
b0a33c1e 46#include <sys/un.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"
0d0527a9 71#include "lxcseccomp.h"
8173e600 72#include "caps.h"
fe4de9a6 73#include "lsm/lsm.h"
36eb9bde
CLG
74
75lxc_log_define(lxc_start, lxc);
76
9f30a190
MM
77const struct ns_info ns_info[LXC_NS_MAX] = {
78 [LXC_NS_MNT] = {"mnt", CLONE_NEWNS},
79 [LXC_NS_PID] = {"pid", CLONE_NEWPID},
80 [LXC_NS_UTS] = {"uts", CLONE_NEWUTS},
81 [LXC_NS_IPC] = {"ipc", CLONE_NEWIPC},
82 [LXC_NS_USER] = {"user", CLONE_NEWUSER},
83 [LXC_NS_NET] = {"net", CLONE_NEWNET}
84};
85
c8154066
SH
86static void print_top_failing_dir(const char *path)
87{
88 size_t len = strlen(path);
89 char *copy = alloca(len+1), *p, *e, saved;
90 strcpy(copy, path);
91
92 p = copy;
93 e = copy + len;
94 while (p < e) {
95 while (p < e && *p == '/') p++;
96 while (p < e && *p != '/') p++;
97 if (p >= e)
98 return;
99 saved = *p;
100 *p = '\0';
101 if (access(copy, X_OK)) {
102 SYSERROR("could not access %s. Please grant it 'x' " \
103 "access, or add an ACL for the container root.",
104 copy);
105 return;
106 }
107 *p = saved;
108 }
109}
110
9f30a190
MM
111static void close_ns(int ns_fd[LXC_NS_MAX]) {
112 int i;
113
9f30a190
MM
114 for (i = 0; i < LXC_NS_MAX; i++) {
115 if (ns_fd[i] > -1) {
116 close(ns_fd[i]);
117 ns_fd[i] = -1;
118 }
119 }
9f30a190
MM
120}
121
122static int preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags) {
123 int i, saved_errno;
124 char path[MAXPATHLEN];
125
9f30a190
MM
126 for (i = 0; i < LXC_NS_MAX; i++)
127 ns_fd[i] = -1;
128
cd43d2d1
SH
129 if (access("/proc/self/ns", X_OK)) {
130 WARN("Kernel does not support attach; preserve_ns ignored");
131 return 0;
132 }
133
9f30a190
MM
134 for (i = 0; i < LXC_NS_MAX; i++) {
135 if ((clone_flags & ns_info[i].clone_flag) == 0)
136 continue;
137 snprintf(path, MAXPATHLEN, "/proc/self/ns/%s", ns_info[i].proc_name);
9f30a190 138 ns_fd[i] = open(path, O_RDONLY | O_CLOEXEC);
9f30a190
MM
139 if (ns_fd[i] < 0)
140 goto error;
141 }
142
143 return 0;
144
145error:
146 saved_errno = errno;
147 close_ns(ns_fd);
148 errno = saved_errno;
149 SYSERROR("failed to open '%s'", path);
150 return -1;
151}
152
153static int attach_ns(const int ns_fd[LXC_NS_MAX]) {
154 int i;
155
156 for (i = 0; i < LXC_NS_MAX; i++) {
157 if (ns_fd[i] < 0)
158 continue;
159
160 if (setns(ns_fd[i], 0) != 0)
161 goto error;
162 }
163 return 0;
164
165error:
166 SYSERROR("failed to set namespace '%s'", ns_info[i].proc_name);
167 return -1;
168}
169
80090207
CLG
170static int match_fd(int fd)
171{
172 return (fd == 0 || fd == 1 || fd == 2);
173}
174
b119f362 175int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore)
80090207
CLG
176{
177 struct dirent dirent, *direntp;
178 int fd, fddir;
179 DIR *dir;
80090207 180
b119f362 181restart:
80090207
CLG
182 dir = opendir("/proc/self/fd");
183 if (!dir) {
184 WARN("failed to open directory: %m");
185 return -1;
186 }
187
188 fddir = dirfd(dir);
189
190 while (!readdir_r(dir, &dirent, &direntp)) {
80090207
CLG
191 if (!direntp)
192 break;
193
194 if (!strcmp(direntp->d_name, "."))
195 continue;
196
197 if (!strcmp(direntp->d_name, ".."))
198 continue;
199
200 fd = atoi(direntp->d_name);
201
f2faa8fa 202 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
80090207
CLG
203 continue;
204
205 if (match_fd(fd))
206 continue;
80090207 207
b119f362
SH
208 if (conf->close_all_fds) {
209 close(fd);
210 closedir(dir);
211 INFO("closed inherited fd %d", fd);
212 goto restart;
213 }
92c7f629 214 WARN("inherited fd %d", fd);
80090207
CLG
215 }
216
92c7f629
GK
217 closedir(dir); /* cannot fail */
218 return 0;
80090207
CLG
219}
220
83ee7875 221static int setup_signal_fd(sigset_t *oldmask)
b0a33c1e 222{
223 sigset_t mask;
224 int fd;
225
f3304a29
FW
226 /* Block everything except serious error signals */
227 if (sigfillset(&mask) ||
228 sigdelset(&mask, SIGILL) ||
229 sigdelset(&mask, SIGSEGV) ||
230 sigdelset(&mask, SIGBUS) ||
b5159817 231 sigdelset(&mask, SIGWINCH) ||
f3304a29
FW
232 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
233 SYSERROR("failed to set signal mask");
b0a33c1e 234 return -1;
235 }
236
237 fd = signalfd(-1, &mask, 0);
238 if (fd < 0) {
36eb9bde 239 SYSERROR("failed to create the signal fd");
b0a33c1e 240 return -1;
241 }
242
243 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 244 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 245 close(fd);
246 return -1;
247 }
248
1ac470c0
DL
249 DEBUG("sigchild handler set");
250
b0a33c1e 251 return fd;
252}
253
84c92abd 254static int signal_handler(int fd, uint32_t events, void *data,
b0a33c1e 255 struct lxc_epoll_descr *descr)
256{
15cd25fd 257 struct signalfd_siginfo siginfo;
80507ee8 258 siginfo_t info;
15cd25fd 259 int ret;
82d89dce 260 pid_t *pid = data;
80507ee8 261 bool init_died = false;
15cd25fd
DL
262
263 ret = read(fd, &siginfo, sizeof(siginfo));
264 if (ret < 0) {
f3304a29 265 ERROR("failed to read signal info");
15cd25fd
DL
266 return -1;
267 }
268
269 if (ret != sizeof(siginfo)) {
270 ERROR("unexpected siginfo size");
271 return -1;
272 }
273
80507ee8
SH
274 // check whether init is running
275 info.si_pid = 0;
276 ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG);
277 if (ret == 0 && info.si_pid == *pid) {
278 init_died = true;
279 }
280
f3304a29
FW
281 if (siginfo.ssi_signo != SIGCHLD) {
282 kill(*pid, siginfo.ssi_signo);
283 INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
80507ee8 284 return init_died ? 1 : 0;
f3304a29
FW
285 }
286
15cd25fd
DL
287 if (siginfo.ssi_code == CLD_STOPPED ||
288 siginfo.ssi_code == CLD_CONTINUED) {
289 INFO("container init process was stopped/continued");
80507ee8 290 return init_died ? 1 : 0;
15cd25fd 291 }
1ac470c0 292
82d89dce
DL
293 /* more robustness, protect ourself from a SIGCHLD sent
294 * by a process different from the container init
295 */
296 if (siginfo.ssi_pid != *pid) {
297 WARN("invalid pid for SIGCHLD");
80507ee8 298 return init_died ? 1 : 0;
82d89dce
DL
299 }
300
15cd25fd 301 DEBUG("container init process exited");
b0a33c1e 302 return 1;
303}
304
74a3920a 305static int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
306{
307 handler->state = state;
9123e471 308 lxc_monitor_send_state(name, state, handler->lxcpath);
66aeffc7
DL
309 return 0;
310}
311
74a3920a 312static int 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
ef6e34ee 333 if (lxc_cmd_mainloop_add(name, &descr, handler)) {
563f2f2c 334 ERROR("failed to add command handler to mainloop");
96fa1ff0 335 goto out_mainloop_open;
563f2f2c
DL
336 }
337
828695d9 338 if (handler->conf->need_utmp_watch) {
495d2046 339 #if HAVE_SYS_CAPABILITY_H
828695d9
SH
340 if (lxc_utmp_mainloop_add(&descr, handler)) {
341 ERROR("failed to add utmp handler to mainloop");
342 goto out_mainloop_open;
343 }
495d2046 344 #else
959aee9c 345 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support");
495d2046 346 #endif
563f2f2c 347 }
b0a33c1e 348
e51d4895 349 return lxc_mainloop(&descr, -1);
b0a33c1e 350
351out_mainloop_open:
352 lxc_mainloop_close(&descr);
b0a33c1e 353out_sigfd:
354 close(sigfd);
c3e13372 355 return -1;
b0a33c1e 356}
357
13f5be62 358struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath)
59eb99ba 359{
3a0f472d
DL
360 struct lxc_handler *handler;
361
362 handler = malloc(sizeof(*handler));
363 if (!handler)
364 return NULL;
59eb99ba
DL
365
366 memset(handler, 0, sizeof(*handler));
367
fae349da 368 handler->conf = conf;
9123e471 369 handler->lxcpath = lxcpath;
5c068da9 370 handler->pinfd = -1;
fae349da 371
fe4de9a6
DE
372 lsm_init();
373
3bdf52d7
DL
374 handler->name = strdup(name);
375 if (!handler->name) {
376 ERROR("failed to allocate memory");
377 goto out_free;
378 }
379
ef6e34ee 380 if (lxc_cmd_init(name, handler, lxcpath))
d2e30e99
DE
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
051151de 388 /* Begin by setting the state to STARTING */
25c2aca5 389 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 390 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
051151de 391 goto out_close_maincmd_fd;
0ad19a3f 392 }
393
f7bee6c6
MW
394 /* Start of environment variable setup for hooks */
395 if (setenv("LXC_NAME", name, 1)) {
396 SYSERROR("failed to set environment variable for container name");
397 }
398 if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
399 SYSERROR("failed to set environment variable for config path");
400 }
401 if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
402 SYSERROR("failed to set environment variable for rootfs mount");
403 }
404 if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
405 SYSERROR("failed to set environment variable for rootfs mount");
406 }
407 if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) {
408 SYSERROR("failed to set environment variable for console path");
409 }
410 if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) {
411 SYSERROR("failed to set environment variable for console log");
412 }
413 /* End of environment variable setup for hooks */
414
283678ed 415 if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) {
773fb9ca
SH
416 ERROR("failed to run pre-start hooks for container '%s'.", name);
417 goto out_aborting;
418 }
26ddeedd 419
fae349da 420 if (lxc_create_tty(name, conf)) {
36eb9bde 421 ERROR("failed to create the ttys");
59eb99ba 422 goto out_aborting;
b0a33c1e 423 }
424
425 /* the signal fd has to be created before forking otherwise
426 * if the child process exits before we setup the signal fd,
427 * the event will be lost and the command will be stuck */
83ee7875 428 handler->sigfd = setup_signal_fd(&handler->oldmask);
59eb99ba 429 if (handler->sigfd < 0) {
36eb9bde 430 ERROR("failed to set sigchild fd handler");
b5159817
DE
431 goto out_delete_tty;
432 }
433
434 /* do this after setting up signals since it might unblock SIGWINCH */
435 if (lxc_console_create(conf)) {
436 ERROR("failed to create console");
437 goto out_restore_sigmask;
b0a33c1e 438 }
439
c4d10a05
SH
440 if (ttys_shift_ids(conf) < 0) {
441 ERROR("Failed to shift tty into container");
442 goto out_restore_sigmask;
443 }
444
c3e13372 445 INFO("'%s' is initialized", name);
3a0f472d 446 return handler;
59eb99ba 447
b5159817
DE
448out_restore_sigmask:
449 sigprocmask(SIG_SETMASK, &handler->oldmask, NULL);
59eb99ba 450out_delete_tty:
fae349da 451 lxc_delete_tty(&conf->tty_info);
59eb99ba 452out_aborting:
25c2aca5 453 lxc_set_state(name, handler, ABORTING);
d2e30e99
DE
454out_close_maincmd_fd:
455 close(conf->maincmd_fd);
456 conf->maincmd_fd = -1;
3bdf52d7
DL
457out_free_name:
458 free(handler->name);
459 handler->name = NULL;
3a0f472d
DL
460out_free:
461 free(handler);
c3e13372 462 return NULL;
59eb99ba
DL
463}
464
ae5c8b8e 465static void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
466{
467 /* The STOPPING state is there for future cleanup code
468 * which can take awhile
469 */
25c2aca5
MN
470 lxc_set_state(name, handler, STOPPING);
471 lxc_set_state(name, handler, STOPPED);
59eb99ba 472
283678ed 473 if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL))
773fb9ca 474 ERROR("failed to run post-stop hooks for container '%s'.", name);
26ddeedd 475
83ee7875 476 /* reset mask set by setup_signal_fd */
8f64a3f6
MN
477 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
478 WARN("failed to restore sigprocmask");
479
b5159817 480 lxc_console_delete(&handler->conf->console);
b2431939 481 lxc_delete_tty(&handler->conf->tty_info);
d2e30e99
DE
482 close(handler->conf->maincmd_fd);
483 handler->conf->maincmd_fd = -1;
3bdf52d7 484 free(handler->name);
d4ef7c50 485 cgroup_destroy(handler);
b2431939 486 free(handler);
59eb99ba
DL
487}
488
74a3920a 489static void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 490{
73e608b2
SH
491 int ret, status;
492
25c2aca5 493 lxc_set_state(name, handler, ABORTING);
7d9fb3e9
DL
494 if (handler->pid > 0)
495 kill(handler->pid, SIGKILL);
73e608b2 496 while ((ret = waitpid(-1, &status, 0)) > 0) ;
59eb99ba
DL
497}
498
828695d9
SH
499#include <sys/reboot.h>
500#include <linux/reboot.h>
501
e2fa1520
SH
502/*
503 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
504 * in a child pid namespace if container reboot support exists.
505 * Otherwise, it will either succeed or return -EPERM.
506 */
507static int container_reboot_supported(void *arg)
828695d9 508{
e2fa1520 509 int *cmd = arg;
828695d9 510 int ret;
828695d9 511
e2fa1520
SH
512 ret = reboot(*cmd);
513 if (ret == -1 && errno == EINVAL)
514 return 1;
515 return 0;
516}
517
b60ed720 518static int must_drop_cap_sys_boot(struct lxc_conf *conf)
e2fa1520 519{
025ed0f3 520 FILE *f;
b60ed720 521 int ret, cmd, v, flags;
e2fa1520 522 long stack_size = 4096;
7f145a6d 523 void *stack = alloca(stack_size);
e2fa1520
SH
524 int status;
525 pid_t pid;
526
025ed0f3 527 f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
e2fa1520
SH
528 if (!f) {
529 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
828695d9 530 return 1;
e2fa1520 531 }
828695d9
SH
532
533 ret = fscanf(f, "%d", &v);
534 fclose(f);
e2fa1520
SH
535 if (ret != 1) {
536 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
828695d9 537 return 1;
e2fa1520
SH
538 }
539 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
540
b60ed720
SH
541 flags = CLONE_NEWPID | SIGCHLD;
542 if (!lxc_list_empty(&conf->id_map))
543 flags |= CLONE_NEWUSER;
544
7f145a6d 545#ifdef __ia64__
959aee9c 546 pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd);
7f145a6d 547#else
959aee9c
SG
548 stack += stack_size;
549 pid = clone(container_reboot_supported, stack, flags, &cmd);
7f145a6d 550#endif
959aee9c
SG
551 if (pid < 0) {
552 SYSERROR("failed to clone");
553 return -1;
554 }
555 if (wait(&status) < 0) {
556 SYSERROR("unexpected wait error: %m");
557 return -1;
558 }
e2fa1520
SH
559
560 if (WEXITSTATUS(status) != 1)
828695d9 561 return 1;
e2fa1520 562
828695d9
SH
563 return 0;
564}
565
ffe1e01a 566static int do_start(void *data)
50e98013 567{
23c53af9 568 struct lxc_handler *handler = data;
e0b6898a 569 const char *lsm_label = NULL;
50e98013
DL
570
571 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
572 SYSERROR("failed to set sigprocmask");
9d7f9e52 573 return -1;
50e98013
DL
574 }
575
743ecd2e
DL
576 /* This prctl must be before the synchro, so if the parent
577 * dies before we set the parent death signal, we will detect
578 * its death with the synchro right after, otherwise we have
579 * a window where the parent can exit before we set the pdeath
580 * signal leading to a unsupervized container.
581 */
582 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
583 SYSERROR("failed to set pdeath signal");
584 return -1;
585 }
586
3c22086f 587 lxc_sync_fini_parent(handler);
50e98013 588
2b0e17e4 589 /* don't leak the pinfd to the container */
025ed0f3 590 if (handler->pinfd >= 0) {
0d03360a 591 close(handler->pinfd);
025ed0f3 592 }
2b0e17e4 593
3c22086f
CLG
594 /* Tell the parent task it can begin to configure the
595 * container and wait for it to finish
596 */
597 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
9d7f9e52 598 return -1;
50e98013 599
f6d3e3e4
SH
600 /*
601 * if we are in a new user namespace, become root there to have
602 * privilege over our namespace
603 */
604 if (!lxc_list_empty(&handler->conf->id_map)) {
605 NOTICE("switching to gid/uid 0 in new user namespace");
606 if (setgid(0)) {
607 SYSERROR("setgid");
608 goto out_warn_father;
609 }
610 if (setuid(0)) {
611 SYSERROR("setuid");
612 goto out_warn_father;
613 }
c476bdce
SH
614 if (setgroups(0, NULL)) {
615 SYSERROR("setgroups");
616 goto out_warn_father;
617 }
f6d3e3e4
SH
618 }
619
c8154066
SH
620 if (access(handler->lxcpath, R_OK)) {
621 print_top_failing_dir(handler->lxcpath);
622 goto out_warn_father;
623 }
624
495d2046 625 #if HAVE_SYS_CAPABILITY_H
69182a31 626 if (handler->conf->need_utmp_watch) {
828695d9
SH
627 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
628 SYSERROR("failed to remove CAP_SYS_BOOT capability");
c4ea60df 629 goto out_warn_father;
828695d9 630 }
959aee9c 631 DEBUG("Dropped cap_sys_boot");
e2fa1520 632 }
495d2046 633 #endif
e2fa1520
SH
634
635 /* Setup the container, ip, names, utsname, ... */
d4ef7c50 636 if (lxc_setup(handler)) {
e2fa1520
SH
637 ERROR("failed to setup the container");
638 goto out_warn_father;
639 }
50e98013 640
544a48a0
SH
641 /* ask father to setup cgroups and wait for him to finish */
642 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP))
643 return -1;
644
72863294 645 /* Set the label to change to when we exec(2) the container's init */
e0b6898a
DE
646 if (!strcmp(lsm_name(), "AppArmor"))
647 lsm_label = handler->conf->lsm_aa_profile;
648 else if (!strcmp(lsm_name(), "SELinux"))
649 lsm_label = handler->conf->lsm_se_context;
650 if (lsm_process_label_set(lsm_label, 1, 1) < 0)
e075f5d9 651 goto out_warn_father;
5112cd70 652
7da8ab18
SH
653 if (lxc_check_inherited(handler->conf, handler->sigfd))
654 return -1;
655
5112cd70
SH
656 /* If we mounted a temporary proc, then unmount it now */
657 tmp_proc_unmount(handler->conf);
e075f5d9 658
8f2c3a70
SH
659 if (lxc_seccomp_load(handler->conf) != 0)
660 goto out_warn_father;
661
283678ed 662 if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) {
773fb9ca
SH
663 ERROR("failed to run start hooks for container '%s'.", handler->name);
664 goto out_warn_father;
665 }
fc25b815 666
f7bee6c6
MW
667 /* The clearenv() and putenv() calls have been moved here
668 * to allow us to use enviroment variables passed to the various
669 * hooks, such as the start hook above. Not all of the
670 * variables like CONFIG_PATH or ROOTFS are valid in this
671 * context but others are. */
672 if (clearenv()) {
673 SYSERROR("failed to clear environment");
674 /* don't error out though */
675 }
676
677 if (putenv("container=lxc")) {
678 SYSERROR("failed to set environment variable");
c4ea60df 679 goto out_warn_father;
f7bee6c6
MW
680 }
681
773fb9ca 682 close(handler->sigfd);
26ddeedd 683
e6126dbe
MN
684 /* after this call, we are in error because this
685 * ops should not return as it execs */
c4ea60df 686 handler->ops->start(handler, handler->data);
50e98013
DL
687
688out_warn_father:
544a48a0
SH
689 /* we want the parent to know something went wrong, so any
690 * value other than what it expects is ok. */
3c22086f 691 lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
50e98013
DL
692 return -1;
693}
694
74a3920a 695static int save_phys_nics(struct lxc_conf *conf)
7b35f3d6
SH
696{
697 struct lxc_list *iterator;
698
699 lxc_list_for_each(iterator, &conf->network) {
700 struct lxc_netdev *netdev = iterator->elem;
701
702 if (netdev->type != LXC_NET_PHYS)
703 continue;
704 conf->saved_nics = realloc(conf->saved_nics,
705 (conf->num_savednics+1)*sizeof(struct saved_nic));
706 if (!conf->saved_nics) {
707 SYSERROR("failed to allocate memory");
708 return -1;
709 }
710 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
711 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
712 if (!conf->saved_nics[conf->num_savednics].orig_name) {
713 SYSERROR("failed to allocate memory");
714 return -1;
715 }
959aee9c 716 INFO("stored saved_nic #%d idx %d name %s", conf->num_savednics,
7b35f3d6
SH
717 conf->saved_nics[conf->num_savednics].ifindex,
718 conf->saved_nics[conf->num_savednics].orig_name);
719 conf->num_savednics++;
720 }
721
722 return 0;
723}
724
74a3920a 725static int lxc_spawn(struct lxc_handler *handler)
59eb99ba 726{
b98f7d6e 727 int failed_before_rename = 0;
ffe1e01a 728 const char *name = handler->name;
9f30a190
MM
729 int saved_ns_fd[LXC_NS_MAX];
730 int preserve_mask = 0, i;
731
732 for (i = 0; i < LXC_NS_MAX; i++)
6c544cb3 733 if (handler->conf->inherit_ns_fd[i] != -1)
9f30a190 734 preserve_mask |= ns_info[i].clone_flag;
50e98013 735
3c22086f 736 if (lxc_sync_init(handler))
9d7f9e52 737 return -1;
0ad19a3f 738
6c544cb3 739 handler->clone_flags = CLONE_NEWPID|CLONE_NEWNS;
f6d3e3e4
SH
740 if (!lxc_list_empty(&handler->conf->id_map)) {
741 INFO("Cloning a new user namespace");
742 handler->clone_flags |= CLONE_NEWUSER;
743 }
0ad19a3f 744
9f30a190 745 if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) {
26b797f3 746 if (!lxc_requests_empty_network(handler))
9f30a190
MM
747 handler->clone_flags |= CLONE_NEWNET;
748
26b797f3
SH
749 if (!lxc_list_empty(&handler->conf->network)) {
750
9f30a190
MM
751 /* Find gateway addresses from the link device, which is
752 * no longer accessible inside the container. Do this
753 * before creating network interfaces, since goto
754 * out_delete_net does not work before lxc_clone. */
755 if (lxc_find_gateway_addresses(handler)) {
756 ERROR("failed to find gateway addresses");
757 lxc_sync_fini(handler);
758 return -1;
759 }
760
761 /* that should be done before the clone because we will
762 * fill the netdev index and use them in the child
763 */
764 if (lxc_create_network(handler)) {
38521a3b 765 ERROR("failed to create the network");
9f30a190
MM
766 lxc_sync_fini(handler);
767 return -1;
768 }
19a26f82
MK
769 }
770
9f30a190
MM
771 if (save_phys_nics(handler->conf)) {
772 ERROR("failed to save physical nic info");
773 goto out_abort;
82d5ae15 774 }
9f30a190
MM
775 } else {
776 INFO("Inheriting a net namespace");
82d5ae15
DL
777 }
778
3c93577b
MM
779 if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) {
780 handler->clone_flags |= CLONE_NEWIPC;
781 } else {
782 INFO("Inheriting an IPC namespace");
7b35f3d6
SH
783 }
784
6c544cb3
MM
785 if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) {
786 handler->clone_flags |= CLONE_NEWUTS;
787 } else {
788 INFO("Inheriting a UTS namespace");
789 }
790
7b35f3d6 791
d4ef7c50
SH
792 if (!cgroup_init(handler)) {
793 ERROR("failed initializing cgroup support");
33ad9f1a
CS
794 goto out_delete_net;
795 }
796
d4ef7c50
SH
797 if (!cgroup_create(handler)) {
798 ERROR("failed creating cgroups");
47d8fb3b
CS
799 goto out_delete_net;
800 }
801
0c547523
SH
802 /*
803 * if the rootfs is not a blockdev, prevent the container from
804 * marking it readonly.
5e32a990
ÇO
805 *
806 * if the container is unprivileged then skip rootfs pinning
0c547523 807 */
5e32a990
ÇO
808 if (lxc_list_empty(&handler->conf->id_map)) {
809 handler->pinfd = pin_rootfs(handler->conf->rootfs.path);
810 if (handler->pinfd == -1)
811 INFO("failed to pin the container's rootfs");
812 }
0c547523 813
cd43d2d1
SH
814 if (preserve_ns(saved_ns_fd, preserve_mask) < 0)
815 goto out_delete_net;
816 if (attach_ns(handler->conf->inherit_ns_fd) < 0)
817 goto out_delete_net;
9f30a190 818
0ad19a3f 819 /* Create a process in a new set of namespaces */
d5088cf2 820 handler->pid = lxc_clone(do_start, handler, handler->clone_flags);
59eb99ba 821 if (handler->pid < 0) {
36eb9bde 822 SYSERROR("failed to fork into a new namespace");
7fef7a06 823 goto out_delete_net;
0ad19a3f 824 }
825
cd43d2d1
SH
826 if (attach_ns(saved_ns_fd))
827 WARN("failed to restore saved namespaces");
9f30a190 828
3c22086f
CLG
829 lxc_sync_fini_child(handler);
830
831 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
99a6af52 832 failed_before_rename = 1;
0ad19a3f 833
d4ef7c50
SH
834 if (!cgroup_create_legacy(handler)) {
835 ERROR("failed to setup the legacy cgroups for %s", name);
ae5c8b8e 836 goto out_delete_net;
33ad9f1a 837 }
9daf6f5d
SH
838 if (!cgroup_setup_limits(handler, false)) {
839 ERROR("failed to setup the cgroup limits for '%s'", name);
6031a6e5
DE
840 goto out_delete_net;
841 }
842
d4ef7c50 843 if (!cgroup_enter(handler))
7fef7a06 844 goto out_delete_net;
218d4250 845
0996e18a
SH
846 if (!cgroup_chown(handler))
847 goto out_delete_net;
848
99a6af52
MN
849 if (failed_before_rename)
850 goto out_delete_net;
851
0ad19a3f 852 /* Create the network configuration */
d5088cf2 853 if (handler->clone_flags & CLONE_NEWNET) {
fae349da 854 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15 855 ERROR("failed to create the configured network");
7fef7a06 856 goto out_delete_net;
82d5ae15 857 }
0ad19a3f 858 }
859
f6d3e3e4
SH
860 /* map the container uids - the container became an invalid
861 * userid the moment it was cloned with CLONE_NEWUSER - this
862 * call doesn't change anything immediately, but allows the
863 * container to setuid(0) (0 being mapped to something else on
864 * the host) later to become a valid uid again */
865 if (lxc_map_ids(&handler->conf->id_map, handler->pid)) {
866 ERROR("failed to set up id mapping");
867 goto out_delete_net;
868 }
869
544a48a0
SH
870 /* Tell the child to continue its initialization. we'll get
871 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
3c22086f
CLG
872 */
873 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
544a48a0
SH
874 goto out_delete_net;
875
9daf6f5d 876 if (!cgroup_setup_limits(handler, true)) {
b98f7d6e
SH
877 ERROR("failed to setup the devices cgroup for '%s'", name);
878 goto out_delete_net;
544a48a0
SH
879 }
880
544a48a0
SH
881 /* Tell the child to complete its initialization and wait for
882 * it to exec or return an error. (the child will never
883 * return LXC_SYNC_POST_CGROUP+1. It will either close the
884 * sync pipe, causing lxc_sync_barrier_child to return
885 * success, or return a different value, causing us to error
886 * out).
887 */
888 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP))
3c22086f 889 return -1;
0ad19a3f 890
cc28d0b0
SH
891 if (detect_shared_rootfs())
892 umount2(handler->conf->rootfs.mount, MNT_DETACH);
893
23c53af9 894 if (handler->ops->post_start(handler, handler->data))
e6126dbe
MN
895 goto out_abort;
896
25c2aca5 897 if (lxc_set_state(name, handler, RUNNING)) {
59eb99ba
DL
898 ERROR("failed to set state to %s",
899 lxc_state2str(RUNNING));
900 goto out_abort;
3f21c114 901 }
22ebac19 902
3c22086f 903 lxc_sync_fini(handler);
0c547523 904
e6126dbe 905 return 0;
1ac470c0 906
7fef7a06 907out_delete_net:
d5088cf2 908 if (handler->clone_flags & CLONE_NEWNET)
74a2b586 909 lxc_delete_network(handler);
59eb99ba
DL
910out_abort:
911 lxc_abort(name, handler);
3c22086f 912 lxc_sync_fini(handler);
5c068da9
SH
913 if (handler->pinfd >= 0) {
914 close(handler->pinfd);
915 handler->pinfd = -1;
916 }
917
b79fcd86 918 return -1;
59eb99ba 919}
0ad19a3f 920
ee70bf78 921int __lxc_start(const char *name, struct lxc_conf *conf,
13f5be62 922 struct lxc_operations* ops, void *data, const char *lxcpath)
59eb99ba 923{
3a0f472d 924 struct lxc_handler *handler;
e043236e 925 int err = -1;
59eb99ba 926 int status;
80090207 927
13f5be62 928 handler = lxc_init(name, conf, lxcpath);
3a0f472d 929 if (!handler) {
59eb99ba 930 ERROR("failed to initialize the container");
66aeffc7 931 return -1;
0ad19a3f 932 }
ee70bf78
CLG
933 handler->ops = ops;
934 handler->data = data;
e6126dbe 935
b60ed720 936 if (must_drop_cap_sys_boot(handler->conf)) {
495d2046 937 #if HAVE_SYS_CAPABILITY_H
959aee9c 938 DEBUG("Dropping cap_sys_boot");
495d2046 939 #else
959aee9c 940 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported");
495d2046 941 #endif
69182a31 942 } else {
959aee9c 943 DEBUG("Not dropping cap_sys_boot or watching utmp");
69182a31
SH
944 handler->conf->need_utmp_watch = 0;
945 }
946
23c53af9 947 err = lxc_spawn(handler);
59eb99ba 948 if (err) {
ee70bf78 949 ERROR("failed to spawn '%s'", name);
74a2b586 950 goto out_fini_nonet;
0ad19a3f 951 }
952
3a0f472d 953 err = lxc_poll(name, handler);
e043236e 954 if (err) {
59eb99ba
DL
955 ERROR("mainloop exited with an error");
956 goto out_abort;
957 }
0ad19a3f 958
3a0f472d 959 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 960 continue;
e043236e 961
8b004f07
SH
962 /*
963 * If the child process exited but was not signaled,
964 * it didn't call reboot. This should mean it was an
965 * lxc-execute which simply exited. In any case, treat
966 * it as a 'halt'
967 */
968 if (WIFSIGNALED(status)) {
969 switch(WTERMSIG(status)) {
970 case SIGINT: /* halt */
971 DEBUG("Container halting");
972 break;
973 case SIGHUP: /* reboot */
974 DEBUG("Container rebooting");
975 handler->conf->reboot = 1;
976 break;
977 default:
959aee9c 978 DEBUG("unknown exit status for init: %d", WTERMSIG(status));
8b004f07
SH
979 break;
980 }
828695d9
SH
981 }
982
7b35f3d6
SH
983 lxc_rename_phys_nics_on_shutdown(handler->conf);
984
5c068da9
SH
985 if (handler->pinfd >= 0) {
986 close(handler->pinfd);
987 handler->pinfd = -1;
988 }
989
3a0f472d 990 err = lxc_error_set_and_log(handler->pid, status);
9d7f9e52 991out_fini:
74a2b586
JK
992 lxc_delete_network(handler);
993
994out_fini_nonet:
3a0f472d 995 lxc_fini(name, handler);
0ad19a3f 996 return err;
997
59eb99ba 998out_abort:
3a0f472d 999 lxc_abort(name, handler);
9d7f9e52 1000 goto out_fini;
0ad19a3f 1001}
ee70bf78
CLG
1002
1003struct start_args {
1004 char *const *argv;
1005};
1006
1007static int start(struct lxc_handler *handler, void* data)
1008{
1009 struct start_args *arg = data;
1010
1011 NOTICE("exec'ing '%s'", arg->argv[0]);
1012
1013 execvp(arg->argv[0], arg->argv);
1014 SYSERROR("failed to exec %s", arg->argv[0]);
1015 return 0;
1016}
1017
1018static int post_start(struct lxc_handler *handler, void* data)
1019{
1020 struct start_args *arg = data;
1021
1022 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
1023 return 0;
1024}
1025
1026static struct lxc_operations start_ops = {
1027 .start = start,
1028 .post_start = post_start
1029};
1030
13f5be62
SH
1031int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf,
1032 const char *lxcpath)
ee70bf78
CLG
1033{
1034 struct start_args start_arg = {
1035 .argv = argv,
1036 };
1037
828695d9 1038 conf->need_utmp_watch = 1;
13f5be62 1039 return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath);
ee70bf78 1040}