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