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