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