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