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