]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
start.c:preserve_ns: added pid parameter
[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>
0ad19a3f 27#include <string.h>
28#include <stdlib.h>
29#include <dirent.h>
30#include <errno.h>
31#include <unistd.h>
32#include <signal.h>
b0a33c1e 33#include <fcntl.h>
c476bdce 34#include <grp.h>
37515ebd 35#include <poll.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>
8173e600 46#include <sys/syscall.h>
ff218c25 47
495d2046
SG
48#if HAVE_SYS_CAPABILITY_H
49#include <sys/capability.h>
50#endif
51
656994bb
MH
52#if !HAVE_DECL_PR_CAPBSET_DROP
53#define PR_CAPBSET_DROP 24
54#endif
55
63376d7d
DL
56#include "start.h"
57#include "conf.h"
58#include "log.h"
563f2f2c 59#include "cgroup.h"
e2bcd7db 60#include "error.h"
b0a33c1e 61#include "af_unix.h"
62#include "mainloop.h"
63376d7d 63#include "utils.h"
565c2d76 64#include "lxcutmp.h"
63376d7d 65#include "monitor.h"
96fa1ff0 66#include "commands.h"
63376d7d 67#include "console.h"
3c22086f 68#include "sync.h"
f549edcc 69#include "namespace.h"
0d0527a9 70#include "lxcseccomp.h"
8173e600 71#include "caps.h"
76a26f55 72#include "bdev.h"
fe4de9a6 73#include "lsm/lsm.h"
f01f7975 74#include "lxclock.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
f01f7975 87extern void mod_all_rdeps(struct lxc_container *c, bool inc);
28272964
CB
88static bool do_destroy_container(struct lxc_conf *conf);
89static int lxc_rmdir_onedev_wrapper(void *data);
90static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
91 const char *name);
92
c8154066
SH
93static void print_top_failing_dir(const char *path)
94{
95 size_t len = strlen(path);
96 char *copy = alloca(len+1), *p, *e, saved;
97 strcpy(copy, path);
98
99 p = copy;
100 e = copy + len;
101 while (p < e) {
102 while (p < e && *p == '/') p++;
103 while (p < e && *p != '/') p++;
c8154066
SH
104 saved = *p;
105 *p = '\0';
106 if (access(copy, X_OK)) {
107 SYSERROR("could not access %s. Please grant it 'x' " \
108 "access, or add an ACL for the container root.",
109 copy);
110 return;
111 }
112 *p = saved;
113 }
114}
115
9f30a190
MM
116static void close_ns(int ns_fd[LXC_NS_MAX]) {
117 int i;
118
9f30a190
MM
119 for (i = 0; i < LXC_NS_MAX; i++) {
120 if (ns_fd[i] > -1) {
121 close(ns_fd[i]);
122 ns_fd[i] = -1;
123 }
124 }
9f30a190
MM
125}
126
035a38fc 127static int preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid) {
9f30a190
MM
128 int i, saved_errno;
129 char path[MAXPATHLEN];
130
9f30a190
MM
131 for (i = 0; i < LXC_NS_MAX; i++)
132 ns_fd[i] = -1;
133
035a38fc
WB
134 snprintf(path, MAXPATHLEN, "/proc/%d/ns", pid);
135 if (access(path, X_OK)) {
cd43d2d1
SH
136 WARN("Kernel does not support attach; preserve_ns ignored");
137 return 0;
138 }
139
9f30a190
MM
140 for (i = 0; i < LXC_NS_MAX; i++) {
141 if ((clone_flags & ns_info[i].clone_flag) == 0)
142 continue;
035a38fc
WB
143 snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid,
144 ns_info[i].proc_name);
9f30a190 145 ns_fd[i] = open(path, O_RDONLY | O_CLOEXEC);
9f30a190
MM
146 if (ns_fd[i] < 0)
147 goto error;
148 }
149
150 return 0;
151
152error:
153 saved_errno = errno;
154 close_ns(ns_fd);
155 errno = saved_errno;
156 SYSERROR("failed to open '%s'", path);
157 return -1;
158}
159
160static int attach_ns(const int ns_fd[LXC_NS_MAX]) {
161 int i;
162
163 for (i = 0; i < LXC_NS_MAX; i++) {
164 if (ns_fd[i] < 0)
165 continue;
166
167 if (setns(ns_fd[i], 0) != 0)
168 goto error;
169 }
170 return 0;
171
172error:
173 SYSERROR("failed to set namespace '%s'", ns_info[i].proc_name);
174 return -1;
175}
176
80090207
CLG
177static int match_fd(int fd)
178{
179 return (fd == 0 || fd == 1 || fd == 2);
180}
181
d2cf4c37
SH
182/*
183 * Check for any fds we need to close
184 * * if fd_to_ignore != -1, then if we find that fd open we will ignore it.
185 * * By default we warn about open fds we find.
186 * * If closeall is true, we will close open fds.
187 * * If lxc-start was passed "-C", then conf->close_all_fds will be true,
188 * in which case we also close all open fds.
189 * * A daemonized container will always pass closeall=true.
190 */
191int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore)
80090207
CLG
192{
193 struct dirent dirent, *direntp;
194 int fd, fddir;
195 DIR *dir;
80090207 196
d2cf4c37
SH
197 if (conf && conf->close_all_fds)
198 closeall = true;
199
b119f362 200restart:
80090207
CLG
201 dir = opendir("/proc/self/fd");
202 if (!dir) {
203 WARN("failed to open directory: %m");
204 return -1;
205 }
206
207 fddir = dirfd(dir);
208
209 while (!readdir_r(dir, &dirent, &direntp)) {
80090207
CLG
210 if (!direntp)
211 break;
212
213 if (!strcmp(direntp->d_name, "."))
214 continue;
215
216 if (!strcmp(direntp->d_name, ".."))
217 continue;
218
219 fd = atoi(direntp->d_name);
220
f2faa8fa 221 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
80090207
CLG
222 continue;
223
858377e4
SH
224 if (current_config && fd == current_config->logfd)
225 continue;
226
80090207
CLG
227 if (match_fd(fd))
228 continue;
80090207 229
d2cf4c37 230 if (closeall) {
b119f362
SH
231 close(fd);
232 closedir(dir);
233 INFO("closed inherited fd %d", fd);
234 goto restart;
235 }
92c7f629 236 WARN("inherited fd %d", fd);
80090207
CLG
237 }
238
92c7f629
GK
239 closedir(dir); /* cannot fail */
240 return 0;
80090207
CLG
241}
242
83ee7875 243static int setup_signal_fd(sigset_t *oldmask)
b0a33c1e 244{
245 sigset_t mask;
246 int fd;
247
f3304a29
FW
248 /* Block everything except serious error signals */
249 if (sigfillset(&mask) ||
250 sigdelset(&mask, SIGILL) ||
251 sigdelset(&mask, SIGSEGV) ||
252 sigdelset(&mask, SIGBUS) ||
b5159817 253 sigdelset(&mask, SIGWINCH) ||
f3304a29
FW
254 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
255 SYSERROR("failed to set signal mask");
b0a33c1e 256 return -1;
257 }
258
259 fd = signalfd(-1, &mask, 0);
260 if (fd < 0) {
36eb9bde 261 SYSERROR("failed to create the signal fd");
b0a33c1e 262 return -1;
263 }
264
265 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 266 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 267 close(fd);
268 return -1;
269 }
270
1ac470c0
DL
271 DEBUG("sigchild handler set");
272
b0a33c1e 273 return fd;
274}
275
84c92abd 276static int signal_handler(int fd, uint32_t events, void *data,
b0a33c1e 277 struct lxc_epoll_descr *descr)
278{
15cd25fd 279 struct signalfd_siginfo siginfo;
80507ee8 280 siginfo_t info;
15cd25fd 281 int ret;
82d89dce 282 pid_t *pid = data;
80507ee8 283 bool init_died = false;
15cd25fd
DL
284
285 ret = read(fd, &siginfo, sizeof(siginfo));
286 if (ret < 0) {
f3304a29 287 ERROR("failed to read signal info");
15cd25fd
DL
288 return -1;
289 }
290
291 if (ret != sizeof(siginfo)) {
292 ERROR("unexpected siginfo size");
293 return -1;
294 }
295
80507ee8
SH
296 // check whether init is running
297 info.si_pid = 0;
298 ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG);
299 if (ret == 0 && info.si_pid == *pid) {
300 init_died = true;
301 }
302
f3304a29
FW
303 if (siginfo.ssi_signo != SIGCHLD) {
304 kill(*pid, siginfo.ssi_signo);
305 INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
80507ee8 306 return init_died ? 1 : 0;
f3304a29
FW
307 }
308
15cd25fd
DL
309 if (siginfo.ssi_code == CLD_STOPPED ||
310 siginfo.ssi_code == CLD_CONTINUED) {
311 INFO("container init process was stopped/continued");
80507ee8 312 return init_died ? 1 : 0;
15cd25fd 313 }
1ac470c0 314
82d89dce
DL
315 /* more robustness, protect ourself from a SIGCHLD sent
316 * by a process different from the container init
317 */
318 if (siginfo.ssi_pid != *pid) {
319 WARN("invalid pid for SIGCHLD");
80507ee8 320 return init_died ? 1 : 0;
82d89dce
DL
321 }
322
15cd25fd 323 DEBUG("container init process exited");
b0a33c1e 324 return 1;
325}
326
735f2c6e 327int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
328{
329 handler->state = state;
9123e471 330 lxc_monitor_send_state(name, state, handler->lxcpath);
66aeffc7
DL
331 return 0;
332}
333
735f2c6e 334int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 335{
ca5f7926
DL
336 int sigfd = handler->sigfd;
337 int pid = handler->pid;
b0a33c1e 338 struct lxc_epoll_descr descr;
339
a9e61274 340 if (lxc_mainloop_open(&descr)) {
36eb9bde 341 ERROR("failed to create mainloop");
50c8bf05 342 goto out_sigfd;
b0a33c1e 343 }
344
83ee7875 345 if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
36eb9bde 346 ERROR("failed to add handler for the signal");
b0a33c1e 347 goto out_mainloop_open;
348 }
349
63376d7d
DL
350 if (lxc_console_mainloop_add(&descr, handler)) {
351 ERROR("failed to add console handler to mainloop");
352 goto out_mainloop_open;
353 }
354
ef6e34ee 355 if (lxc_cmd_mainloop_add(name, &descr, handler)) {
563f2f2c 356 ERROR("failed to add command handler to mainloop");
96fa1ff0 357 goto out_mainloop_open;
563f2f2c
DL
358 }
359
828695d9 360 if (handler->conf->need_utmp_watch) {
495d2046 361 #if HAVE_SYS_CAPABILITY_H
828695d9
SH
362 if (lxc_utmp_mainloop_add(&descr, handler)) {
363 ERROR("failed to add utmp handler to mainloop");
364 goto out_mainloop_open;
365 }
495d2046 366 #else
959aee9c 367 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support");
495d2046 368 #endif
563f2f2c 369 }
b0a33c1e 370
e51d4895 371 return lxc_mainloop(&descr, -1);
b0a33c1e 372
373out_mainloop_open:
374 lxc_mainloop_close(&descr);
b0a33c1e 375out_sigfd:
376 close(sigfd);
c3e13372 377 return -1;
b0a33c1e 378}
379
13f5be62 380struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath)
59eb99ba 381{
3a0f472d
DL
382 struct lxc_handler *handler;
383
384 handler = malloc(sizeof(*handler));
385 if (!handler)
386 return NULL;
59eb99ba
DL
387
388 memset(handler, 0, sizeof(*handler));
389
e8bd4e43 390 handler->ttysock[0] = handler->ttysock[1] = -1;
fae349da 391 handler->conf = conf;
9123e471 392 handler->lxcpath = lxcpath;
5c068da9 393 handler->pinfd = -1;
fae349da 394
fe4de9a6
DE
395 lsm_init();
396
3bdf52d7
DL
397 handler->name = strdup(name);
398 if (!handler->name) {
399 ERROR("failed to allocate memory");
400 goto out_free;
401 }
402
ef6e34ee 403 if (lxc_cmd_init(name, handler, lxcpath))
d2e30e99
DE
404 goto out_free_name;
405
8f2c3a70
SH
406 if (lxc_read_seccomp_config(conf) != 0) {
407 ERROR("failed loading seccomp policy");
d2e30e99 408 goto out_close_maincmd_fd;
8f2c3a70
SH
409 }
410
051151de 411 /* Begin by setting the state to STARTING */
25c2aca5 412 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 413 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
051151de 414 goto out_close_maincmd_fd;
0ad19a3f 415 }
416
f7bee6c6 417 /* Start of environment variable setup for hooks */
ab7efcf5 418 if (name && setenv("LXC_NAME", name, 1)) {
f7bee6c6
MW
419 SYSERROR("failed to set environment variable for container name");
420 }
ab7efcf5 421 if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
f7bee6c6
MW
422 SYSERROR("failed to set environment variable for config path");
423 }
ab7efcf5 424 if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
f7bee6c6
MW
425 SYSERROR("failed to set environment variable for rootfs mount");
426 }
ab7efcf5 427 if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
f7bee6c6
MW
428 SYSERROR("failed to set environment variable for rootfs mount");
429 }
430 if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) {
431 SYSERROR("failed to set environment variable for console path");
432 }
433 if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) {
434 SYSERROR("failed to set environment variable for console log");
435 }
436 /* End of environment variable setup for hooks */
437
283678ed 438 if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) {
773fb9ca
SH
439 ERROR("failed to run pre-start hooks for container '%s'.", name);
440 goto out_aborting;
441 }
26ddeedd 442
b0a33c1e 443 /* the signal fd has to be created before forking otherwise
444 * if the child process exits before we setup the signal fd,
445 * the event will be lost and the command will be stuck */
83ee7875 446 handler->sigfd = setup_signal_fd(&handler->oldmask);
59eb99ba 447 if (handler->sigfd < 0) {
36eb9bde 448 ERROR("failed to set sigchild fd handler");
b5159817
DE
449 goto out_delete_tty;
450 }
451
452 /* do this after setting up signals since it might unblock SIGWINCH */
453 if (lxc_console_create(conf)) {
454 ERROR("failed to create console");
455 goto out_restore_sigmask;
b0a33c1e 456 }
457
c4d10a05
SH
458 if (ttys_shift_ids(conf) < 0) {
459 ERROR("Failed to shift tty into container");
460 goto out_restore_sigmask;
461 }
462
c3e13372 463 INFO("'%s' is initialized", name);
3a0f472d 464 return handler;
59eb99ba 465
b5159817
DE
466out_restore_sigmask:
467 sigprocmask(SIG_SETMASK, &handler->oldmask, NULL);
59eb99ba 468out_delete_tty:
fae349da 469 lxc_delete_tty(&conf->tty_info);
59eb99ba 470out_aborting:
25c2aca5 471 lxc_set_state(name, handler, ABORTING);
d2e30e99
DE
472out_close_maincmd_fd:
473 close(conf->maincmd_fd);
474 conf->maincmd_fd = -1;
3bdf52d7
DL
475out_free_name:
476 free(handler->name);
477 handler->name = NULL;
3a0f472d
DL
478out_free:
479 free(handler);
c3e13372 480 return NULL;
59eb99ba
DL
481}
482
3b72c4a0 483void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
484{
485 /* The STOPPING state is there for future cleanup code
486 * which can take awhile
487 */
25c2aca5
MN
488 lxc_set_state(name, handler, STOPPING);
489 lxc_set_state(name, handler, STOPPED);
59eb99ba 490
283678ed 491 if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL))
773fb9ca 492 ERROR("failed to run post-stop hooks for container '%s'.", name);
26ddeedd 493
83ee7875 494 /* reset mask set by setup_signal_fd */
8f64a3f6
MN
495 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
496 WARN("failed to restore sigprocmask");
497
b5159817 498 lxc_console_delete(&handler->conf->console);
b2431939 499 lxc_delete_tty(&handler->conf->tty_info);
d2e30e99
DE
500 close(handler->conf->maincmd_fd);
501 handler->conf->maincmd_fd = -1;
3bdf52d7 502 free(handler->name);
e8bd4e43
SH
503 if (handler->ttysock[0] != -1) {
504 close(handler->ttysock[0]);
505 close(handler->ttysock[1]);
506 }
776d170c 507 if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) {
28272964
CB
508 lxc_destroy_container_on_signal(handler, name);
509 }
d4ef7c50 510 cgroup_destroy(handler);
b2431939 511 free(handler);
59eb99ba
DL
512}
513
735f2c6e 514void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 515{
73e608b2
SH
516 int ret, status;
517
25c2aca5 518 lxc_set_state(name, handler, ABORTING);
7d9fb3e9
DL
519 if (handler->pid > 0)
520 kill(handler->pid, SIGKILL);
73e608b2 521 while ((ret = waitpid(-1, &status, 0)) > 0) ;
59eb99ba
DL
522}
523
828695d9
SH
524#include <sys/reboot.h>
525#include <linux/reboot.h>
526
e2fa1520
SH
527/*
528 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
529 * in a child pid namespace if container reboot support exists.
530 * Otherwise, it will either succeed or return -EPERM.
531 */
532static int container_reboot_supported(void *arg)
828695d9 533{
d028235d 534 int *cmd = arg;
828695d9 535 int ret;
828695d9 536
d028235d 537 ret = reboot(*cmd);
e2fa1520
SH
538 if (ret == -1 && errno == EINVAL)
539 return 1;
540 return 0;
541}
542
b60ed720 543static int must_drop_cap_sys_boot(struct lxc_conf *conf)
e2fa1520 544{
025ed0f3 545 FILE *f;
b60ed720 546 int ret, cmd, v, flags;
d028235d
SG
547 long stack_size = 4096;
548 void *stack = alloca(stack_size);
549 int status;
550 pid_t pid;
e2fa1520 551
025ed0f3 552 f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
e2fa1520
SH
553 if (!f) {
554 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
828695d9 555 return 1;
e2fa1520 556 }
828695d9
SH
557
558 ret = fscanf(f, "%d", &v);
559 fclose(f);
e2fa1520
SH
560 if (ret != 1) {
561 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
828695d9 562 return 1;
e2fa1520
SH
563 }
564 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
565
b60ed720
SH
566 flags = CLONE_NEWPID | SIGCHLD;
567 if (!lxc_list_empty(&conf->id_map))
568 flags |= CLONE_NEWUSER;
569
7f145a6d 570#ifdef __ia64__
959aee9c 571 pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd);
7f145a6d 572#else
959aee9c
SG
573 stack += stack_size;
574 pid = clone(container_reboot_supported, stack, flags, &cmd);
7f145a6d 575#endif
959aee9c 576 if (pid < 0) {
c08220e9 577 if (flags & CLONE_NEWUSER)
4a7e5f4f 578 ERROR("failed to clone (%#x): %s (includes CLONE_NEWUSER)", flags, strerror(errno));
c08220e9 579 else
4a7e5f4f 580 ERROR("failed to clone (%#x): %s", flags, strerror(errno));
959aee9c
SG
581 return -1;
582 }
583 if (wait(&status) < 0) {
584 SYSERROR("unexpected wait error: %m");
585 return -1;
586 }
e2fa1520
SH
587
588 if (WEXITSTATUS(status) != 1)
828695d9 589 return 1;
e2fa1520 590
828695d9
SH
591 return 0;
592}
593
658979c5
SH
594/*
595 * netpipe is used in the unprivileged case to transfer the ifindexes
596 * from parent to child
597 */
598static int netpipe = -1;
599
600static inline int count_veths(struct lxc_list *network)
601{
602 struct lxc_list *iterator;
603 struct lxc_netdev *netdev;
604 int count = 0;
605
606 lxc_list_for_each(iterator, network) {
607 netdev = iterator->elem;
608 if (netdev->type != LXC_NET_VETH)
609 continue;
610 count++;
611 }
612 return count;
613}
614
615static int read_unpriv_netifindex(struct lxc_list *network)
616{
617 struct lxc_list *iterator;
618 struct lxc_netdev *netdev;
619
620 if (netpipe == -1)
621 return 0;
622 lxc_list_for_each(iterator, network) {
623 netdev = iterator->elem;
624 if (netdev->type != LXC_NET_VETH)
625 continue;
626 if (!(netdev->name = malloc(IFNAMSIZ))) {
627 ERROR("Out of memory");
628 close(netpipe);
629 return -1;
630 }
631 if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) {
632 close(netpipe);
633 return -1;
634 }
635 }
636 close(netpipe);
637 return 0;
638}
639
ffe1e01a 640static int do_start(void *data)
50e98013 641{
7c661726 642 struct lxc_list *iterator;
23c53af9 643 struct lxc_handler *handler = data;
50e98013
DL
644
645 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
646 SYSERROR("failed to set sigprocmask");
9d7f9e52 647 return -1;
50e98013
DL
648 }
649
d028235d 650 /* This prctl must be before the synchro, so if the parent
743ecd2e
DL
651 * dies before we set the parent death signal, we will detect
652 * its death with the synchro right after, otherwise we have
653 * a window where the parent can exit before we set the pdeath
654 * signal leading to a unsupervized container.
655 */
656 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
657 SYSERROR("failed to set pdeath signal");
658 return -1;
659 }
660
3c22086f 661 lxc_sync_fini_parent(handler);
50e98013 662
2b0e17e4 663 /* don't leak the pinfd to the container */
025ed0f3 664 if (handler->pinfd >= 0) {
0d03360a 665 close(handler->pinfd);
025ed0f3 666 }
2b0e17e4 667
3c22086f
CLG
668 /* Tell the parent task it can begin to configure the
669 * container and wait for it to finish
670 */
671 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
9d7f9e52 672 return -1;
50e98013 673
658979c5
SH
674 if (read_unpriv_netifindex(&handler->conf->network) < 0)
675 goto out_warn_father;
676
f6d3e3e4
SH
677 /*
678 * if we are in a new user namespace, become root there to have
62d00539
PT
679 * privilege over our namespace. When using lxc-execute we default to root,
680 * but this can be overriden using the lxc.init_uid and lxc.init_gid
681 * configuration options.
f6d3e3e4
SH
682 */
683 if (!lxc_list_empty(&handler->conf->id_map)) {
c5cd20ce
PT
684 gid_t new_gid = 0;
685 if (handler->conf->is_execute && handler->conf->init_gid)
686 new_gid = handler->conf->init_gid;
687
688 uid_t new_uid = 0;
689 if (handler->conf->is_execute && handler->conf->init_uid)
690 new_uid = handler->conf->init_uid;
691
56f8ff00
PT
692 NOTICE("switching to gid/uid %d/%d in new user namespace", new_gid, new_uid);
693 if (setgid(new_gid)) {
f6d3e3e4
SH
694 SYSERROR("setgid");
695 goto out_warn_father;
696 }
56f8ff00 697 if (setuid(new_uid)) {
f6d3e3e4
SH
698 SYSERROR("setuid");
699 goto out_warn_father;
700 }
c476bdce
SH
701 if (setgroups(0, NULL)) {
702 SYSERROR("setgroups");
703 goto out_warn_father;
704 }
f6d3e3e4
SH
705 }
706
99b71824 707 if (access(handler->lxcpath, X_OK)) {
c8154066
SH
708 print_top_failing_dir(handler->lxcpath);
709 goto out_warn_father;
710 }
711
495d2046 712 #if HAVE_SYS_CAPABILITY_H
69182a31 713 if (handler->conf->need_utmp_watch) {
828695d9
SH
714 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
715 SYSERROR("failed to remove CAP_SYS_BOOT capability");
c4ea60df 716 goto out_warn_father;
828695d9 717 }
959aee9c 718 DEBUG("Dropped cap_sys_boot");
e2fa1520 719 }
495d2046 720 #endif
e2fa1520
SH
721
722 /* Setup the container, ip, names, utsname, ... */
d4ef7c50 723 if (lxc_setup(handler)) {
e2fa1520
SH
724 ERROR("failed to setup the container");
725 goto out_warn_father;
726 }
50e98013 727
544a48a0
SH
728 /* ask father to setup cgroups and wait for him to finish */
729 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP))
730 return -1;
731
72863294 732 /* Set the label to change to when we exec(2) the container's init */
7aff4f43 733 if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0)
e075f5d9 734 goto out_warn_father;
5112cd70 735
0d9acb99
DE
736 /* Some init's such as busybox will set sane tty settings on stdin,
737 * stdout, stderr which it thinks is the console. We already set them
738 * the way we wanted on the real terminal, and we want init to do its
739 * setup on its console ie. the pty allocated in lxc_console_create()
740 * so make sure that that pty is stdin,stdout,stderr.
741 */
742 if (lxc_console_set_stdfds(handler) < 0)
743 goto out_warn_father;
744
5112cd70
SH
745 /* If we mounted a temporary proc, then unmount it now */
746 tmp_proc_unmount(handler->conf);
e075f5d9 747
8f2c3a70
SH
748 if (lxc_seccomp_load(handler->conf) != 0)
749 goto out_warn_father;
750
283678ed 751 if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) {
773fb9ca
SH
752 ERROR("failed to run start hooks for container '%s'.", handler->name);
753 goto out_warn_father;
754 }
fc25b815 755
f7bee6c6 756 /* The clearenv() and putenv() calls have been moved here
ec64264d 757 * to allow us to use environment variables passed to the various
f7bee6c6
MW
758 * hooks, such as the start hook above. Not all of the
759 * variables like CONFIG_PATH or ROOTFS are valid in this
760 * context but others are. */
761 if (clearenv()) {
762 SYSERROR("failed to clear environment");
763 /* don't error out though */
764 }
765
7c661726
MP
766 lxc_list_for_each(iterator, &handler->conf->environment) {
767 if (putenv((char *)iterator->elem)) {
768 SYSERROR("failed to set environment variable '%s'", (char *)iterator->elem);
769 goto out_warn_father;
770 }
771 }
772
f7bee6c6 773 if (putenv("container=lxc")) {
7c661726 774 SYSERROR("failed to set environment variable 'container=lxc'");
c4ea60df 775 goto out_warn_father;
f7bee6c6
MW
776 }
777
393903d1
SH
778 if (handler->conf->pty_names) {
779 if (putenv(handler->conf->pty_names)) {
780 SYSERROR("failed to set environment variable for container ptys");
781 goto out_warn_father;
782 }
783 }
784
773fb9ca 785 close(handler->sigfd);
26ddeedd 786
69aeabac
TA
787 if (handler->backgrounded && null_stdfds() < 0)
788 goto out_warn_father;
507cee36 789
e6126dbe
MN
790 /* after this call, we are in error because this
791 * ops should not return as it execs */
c4ea60df 792 handler->ops->start(handler, handler->data);
50e98013
DL
793
794out_warn_father:
544a48a0
SH
795 /* we want the parent to know something went wrong, so any
796 * value other than what it expects is ok. */
3c22086f 797 lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
50e98013
DL
798 return -1;
799}
800
74a3920a 801static int save_phys_nics(struct lxc_conf *conf)
7b35f3d6
SH
802{
803 struct lxc_list *iterator;
804
805 lxc_list_for_each(iterator, &conf->network) {
806 struct lxc_netdev *netdev = iterator->elem;
807
808 if (netdev->type != LXC_NET_PHYS)
809 continue;
810 conf->saved_nics = realloc(conf->saved_nics,
811 (conf->num_savednics+1)*sizeof(struct saved_nic));
812 if (!conf->saved_nics) {
813 SYSERROR("failed to allocate memory");
814 return -1;
815 }
816 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
817 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
818 if (!conf->saved_nics[conf->num_savednics].orig_name) {
819 SYSERROR("failed to allocate memory");
820 return -1;
821 }
959aee9c 822 INFO("stored saved_nic #%d idx %d name %s", conf->num_savednics,
7b35f3d6
SH
823 conf->saved_nics[conf->num_savednics].ifindex,
824 conf->saved_nics[conf->num_savednics].orig_name);
825 conf->num_savednics++;
826 }
827
828 return 0;
829}
830
e8bd4e43
SH
831static int recv_fd(int sock, int *fd)
832{
833 if (lxc_abstract_unix_recv_fd(sock, fd, NULL, 0) < 0) {
834 SYSERROR("Error receiving tty fd from child");
835 return -1;
836 }
837 if (*fd == -1)
838 return -1;
839 return 0;
840}
841
842static int recv_ttys_from_child(struct lxc_handler *handler)
843{
844 struct lxc_conf *conf = handler->conf;
845 int i, sock = handler->ttysock[1];
846 struct lxc_tty_info *tty_info = &conf->tty_info;
847
848 if (!conf->tty)
849 return 0;
850
851 tty_info->pty_info = malloc(sizeof(*tty_info->pty_info)*conf->tty);
852 if (!tty_info->pty_info) {
853 SYSERROR("failed to allocate pty_info");
854 return -1;
855 }
856
857 for (i = 0; i < conf->tty; i++) {
858 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
859 pty_info->busy = 0;
860 if (recv_fd(sock, &pty_info->slave) < 0 ||
861 recv_fd(sock, &pty_info->master) < 0) {
862 ERROR("Error receiving tty info from child");
863 return -1;
864 }
865 }
866 tty_info->nbtty = conf->tty;
867
868 return 0;
869}
870
f813849c
TA
871void resolve_clone_flags(struct lxc_handler *handler)
872{
873 handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS;
874
875 if (!lxc_list_empty(&handler->conf->id_map)) {
876 INFO("Cloning a new user namespace");
877 handler->clone_flags |= CLONE_NEWUSER;
878 }
879
880 if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) {
881 if (!lxc_requests_empty_network(handler))
882 handler->clone_flags |= CLONE_NEWNET;
883 } else {
884 INFO("Inheriting a net namespace");
885 }
886
887 if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) {
888 handler->clone_flags |= CLONE_NEWIPC;
889 } else {
890 INFO("Inheriting an IPC namespace");
891 }
892
893 if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) {
894 handler->clone_flags |= CLONE_NEWUTS;
895 } else {
896 INFO("Inheriting a UTS namespace");
897 }
898}
899
74a3920a 900static int lxc_spawn(struct lxc_handler *handler)
59eb99ba 901{
b98f7d6e 902 int failed_before_rename = 0;
ffe1e01a 903 const char *name = handler->name;
7e4dfe0b 904 bool cgroups_connected = false;
9f30a190
MM
905 int saved_ns_fd[LXC_NS_MAX];
906 int preserve_mask = 0, i;
658979c5 907 int netpipepair[2], nveths;
9f30a190 908
2f2623ec
SH
909 netpipe = -1;
910
9f30a190 911 for (i = 0; i < LXC_NS_MAX; i++)
6c544cb3 912 if (handler->conf->inherit_ns_fd[i] != -1)
9f30a190 913 preserve_mask |= ns_info[i].clone_flag;
50e98013 914
3c22086f 915 if (lxc_sync_init(handler))
9d7f9e52 916 return -1;
0ad19a3f 917
e8bd4e43
SH
918 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handler->ttysock) < 0) {
919 lxc_sync_fini(handler);
920 return -1;
921 }
922
f813849c 923 resolve_clone_flags(handler);
9f30a190 924
f813849c 925 if (handler->clone_flags & CLONE_NEWNET) {
26b797f3
SH
926 if (!lxc_list_empty(&handler->conf->network)) {
927
9f30a190
MM
928 /* Find gateway addresses from the link device, which is
929 * no longer accessible inside the container. Do this
930 * before creating network interfaces, since goto
931 * out_delete_net does not work before lxc_clone. */
932 if (lxc_find_gateway_addresses(handler)) {
933 ERROR("failed to find gateway addresses");
934 lxc_sync_fini(handler);
935 return -1;
936 }
937
938 /* that should be done before the clone because we will
939 * fill the netdev index and use them in the child
940 */
941 if (lxc_create_network(handler)) {
38521a3b 942 ERROR("failed to create the network");
9f30a190
MM
943 lxc_sync_fini(handler);
944 return -1;
945 }
19a26f82
MK
946 }
947
9f30a190
MM
948 if (save_phys_nics(handler->conf)) {
949 ERROR("failed to save physical nic info");
950 goto out_abort;
82d5ae15
DL
951 }
952 }
953
d4ef7c50
SH
954 if (!cgroup_init(handler)) {
955 ERROR("failed initializing cgroup support");
33ad9f1a
CS
956 goto out_delete_net;
957 }
958
7e4dfe0b
SH
959 cgroups_connected = true;
960
d4ef7c50
SH
961 if (!cgroup_create(handler)) {
962 ERROR("failed creating cgroups");
47d8fb3b
CS
963 goto out_delete_net;
964 }
965
0c547523
SH
966 /*
967 * if the rootfs is not a blockdev, prevent the container from
968 * marking it readonly.
5e32a990
ÇO
969 *
970 * if the container is unprivileged then skip rootfs pinning
0c547523 971 */
5e32a990
ÇO
972 if (lxc_list_empty(&handler->conf->id_map)) {
973 handler->pinfd = pin_rootfs(handler->conf->rootfs.path);
974 if (handler->pinfd == -1)
975 INFO("failed to pin the container's rootfs");
976 }
0c547523 977
035a38fc 978 if (preserve_ns(saved_ns_fd, preserve_mask, getpid()) < 0)
cd43d2d1
SH
979 goto out_delete_net;
980 if (attach_ns(handler->conf->inherit_ns_fd) < 0)
981 goto out_delete_net;
9f30a190 982
658979c5
SH
983 if (am_unpriv() && (nveths = count_veths(&handler->conf->network))) {
984 if (pipe(netpipepair) < 0) {
985 SYSERROR("Error creating pipe");
986 goto out_delete_net;
987 }
988 /* store netpipe in the global var for do_start's use */
989 netpipe = netpipepair[0];
990 }
991
0ad19a3f 992 /* Create a process in a new set of namespaces */
d5088cf2 993 handler->pid = lxc_clone(do_start, handler, handler->clone_flags);
59eb99ba 994 if (handler->pid < 0) {
36eb9bde 995 SYSERROR("failed to fork into a new namespace");
7fef7a06 996 goto out_delete_net;
0ad19a3f 997 }
998
cd43d2d1
SH
999 if (attach_ns(saved_ns_fd))
1000 WARN("failed to restore saved namespaces");
9f30a190 1001
3c22086f
CLG
1002 lxc_sync_fini_child(handler);
1003
1004 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
99a6af52 1005 failed_before_rename = 1;
0ad19a3f 1006
d4ef7c50
SH
1007 if (!cgroup_create_legacy(handler)) {
1008 ERROR("failed to setup the legacy cgroups for %s", name);
ae5c8b8e 1009 goto out_delete_net;
33ad9f1a 1010 }
9daf6f5d
SH
1011 if (!cgroup_setup_limits(handler, false)) {
1012 ERROR("failed to setup the cgroup limits for '%s'", name);
6031a6e5
DE
1013 goto out_delete_net;
1014 }
1015
d4ef7c50 1016 if (!cgroup_enter(handler))
7fef7a06 1017 goto out_delete_net;
218d4250 1018
0996e18a
SH
1019 if (!cgroup_chown(handler))
1020 goto out_delete_net;
1021
99a6af52
MN
1022 if (failed_before_rename)
1023 goto out_delete_net;
1024
0ad19a3f 1025 /* Create the network configuration */
d5088cf2 1026 if (handler->clone_flags & CLONE_NEWNET) {
fae349da 1027 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15 1028 ERROR("failed to create the configured network");
7fef7a06 1029 goto out_delete_net;
82d5ae15 1030 }
0ad19a3f 1031 }
1032
658979c5
SH
1033 if (netpipe != -1) {
1034 struct lxc_list *iterator;
1035 struct lxc_netdev *netdev;
1036
1037 close(netpipe);
1038 lxc_list_for_each(iterator, &handler->conf->network) {
1039 netdev = iterator->elem;
1040 if (netdev->type != LXC_NET_VETH)
1041 continue;
1042 if (write(netpipepair[1], netdev->name, IFNAMSIZ) != IFNAMSIZ) {
1043 ERROR("Error writing veth name to container");
1044 goto out_delete_net;
1045 }
1046 }
1047 close(netpipepair[1]);
1048 }
1049
f6d3e3e4
SH
1050 /* map the container uids - the container became an invalid
1051 * userid the moment it was cloned with CLONE_NEWUSER - this
1052 * call doesn't change anything immediately, but allows the
1053 * container to setuid(0) (0 being mapped to something else on
1054 * the host) later to become a valid uid again */
1055 if (lxc_map_ids(&handler->conf->id_map, handler->pid)) {
1056 ERROR("failed to set up id mapping");
1057 goto out_delete_net;
1058 }
1059
544a48a0
SH
1060 /* Tell the child to continue its initialization. we'll get
1061 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
3c22086f
CLG
1062 */
1063 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
544a48a0
SH
1064 goto out_delete_net;
1065
9daf6f5d 1066 if (!cgroup_setup_limits(handler, true)) {
b98f7d6e
SH
1067 ERROR("failed to setup the devices cgroup for '%s'", name);
1068 goto out_delete_net;
544a48a0
SH
1069 }
1070
73d28d42 1071 cgroup_disconnect();
7e4dfe0b 1072 cgroups_connected = false;
73d28d42 1073
e8bd4e43
SH
1074 /* read tty fds allocated by child */
1075 if (recv_ttys_from_child(handler) < 0) {
1076 ERROR("failed to receive tty info from child");
1077 goto out_delete_net;
1078 }
1079
544a48a0
SH
1080 /* Tell the child to complete its initialization and wait for
1081 * it to exec or return an error. (the child will never
1082 * return LXC_SYNC_POST_CGROUP+1. It will either close the
1083 * sync pipe, causing lxc_sync_barrier_child to return
1084 * success, or return a different value, causing us to error
1085 * out).
1086 */
1087 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP))
3c22086f 1088 return -1;
0ad19a3f 1089
cc28d0b0
SH
1090 if (detect_shared_rootfs())
1091 umount2(handler->conf->rootfs.mount, MNT_DETACH);
1092
23c53af9 1093 if (handler->ops->post_start(handler, handler->data))
e6126dbe
MN
1094 goto out_abort;
1095
25c2aca5 1096 if (lxc_set_state(name, handler, RUNNING)) {
59eb99ba
DL
1097 ERROR("failed to set state to %s",
1098 lxc_state2str(RUNNING));
1099 goto out_abort;
3f21c114 1100 }
22ebac19 1101
3c22086f 1102 lxc_sync_fini(handler);
0c547523 1103
e6126dbe 1104 return 0;
1ac470c0 1105
7fef7a06 1106out_delete_net:
7e4dfe0b
SH
1107 if (cgroups_connected)
1108 cgroup_disconnect();
d5088cf2 1109 if (handler->clone_flags & CLONE_NEWNET)
74a2b586 1110 lxc_delete_network(handler);
59eb99ba
DL
1111out_abort:
1112 lxc_abort(name, handler);
3c22086f 1113 lxc_sync_fini(handler);
5c068da9
SH
1114 if (handler->pinfd >= 0) {
1115 close(handler->pinfd);
1116 handler->pinfd = -1;
1117 }
1118
b79fcd86 1119 return -1;
59eb99ba 1120}
0ad19a3f 1121
2af6bd1b
SH
1122int get_netns_fd(int pid)
1123{
1124 char path[MAXPATHLEN];
1125 int ret, fd;
1126
1127 ret = snprintf(path, MAXPATHLEN, "/proc/%d/ns/net", pid);
1128 if (ret < 0 || ret >= MAXPATHLEN) {
1129 WARN("Failed to pin netns file for pid %d", pid);
1130 return -1;
1131 }
1132
1133 fd = open(path, O_RDONLY);
1134 if (fd < 0) {
1135 WARN("Failed to pin netns file %s for pid %d: %s",
1136 path, pid, strerror(errno));
1137 return -1;
1138 }
1139 return fd;
1140}
1141
ee70bf78 1142int __lxc_start(const char *name, struct lxc_conf *conf,
507cee36
TA
1143 struct lxc_operations* ops, void *data, const char *lxcpath,
1144 bool backgrounded)
59eb99ba 1145{
3a0f472d 1146 struct lxc_handler *handler;
e043236e 1147 int err = -1;
59eb99ba 1148 int status;
2af6bd1b 1149 int netnsfd = -1;
80090207 1150
13f5be62 1151 handler = lxc_init(name, conf, lxcpath);
3a0f472d 1152 if (!handler) {
59eb99ba 1153 ERROR("failed to initialize the container");
66aeffc7 1154 return -1;
0ad19a3f 1155 }
ee70bf78
CLG
1156 handler->ops = ops;
1157 handler->data = data;
507cee36 1158 handler->backgrounded = backgrounded;
e6126dbe 1159
b60ed720 1160 if (must_drop_cap_sys_boot(handler->conf)) {
495d2046 1161 #if HAVE_SYS_CAPABILITY_H
959aee9c 1162 DEBUG("Dropping cap_sys_boot");
495d2046 1163 #else
959aee9c 1164 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported");
495d2046 1165 #endif
69182a31 1166 } else {
959aee9c 1167 DEBUG("Not dropping cap_sys_boot or watching utmp");
69182a31
SH
1168 handler->conf->need_utmp_watch = 0;
1169 }
1170
76a26f55
SH
1171 if (!attach_block_device(handler->conf)) {
1172 ERROR("Failure attaching block device");
1173 goto out_fini_nonet;
1174 }
1175
35120d9c
SH
1176 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
1177 /* if the backing store is a device, mount it here and now */
1178 if (rootfs_is_blockdev(conf)) {
1179 if (unshare(CLONE_NEWNS) < 0) {
1180 ERROR("Error unsharing mounts");
1181 goto out_fini_nonet;
1182 }
6a0c909a 1183 remount_all_slave();
35120d9c
SH
1184 if (do_rootfs_setup(conf, name, lxcpath) < 0) {
1185 ERROR("Error setting up rootfs mount as root before spawn");
1186 goto out_fini_nonet;
1187 }
1188 INFO("Set up container rootfs as host root");
1189 }
1190 }
1191
23c53af9 1192 err = lxc_spawn(handler);
59eb99ba 1193 if (err) {
ee70bf78 1194 ERROR("failed to spawn '%s'", name);
76a26f55 1195 goto out_detach_blockdev;
0ad19a3f 1196 }
1197
8bee8851
WB
1198 handler->conf->reboot = 0;
1199
2af6bd1b
SH
1200 netnsfd = get_netns_fd(handler->pid);
1201
3a0f472d 1202 err = lxc_poll(name, handler);
e043236e 1203 if (err) {
59eb99ba 1204 ERROR("mainloop exited with an error");
4b2f98f5
SH
1205 if (netnsfd >= 0)
1206 close(netnsfd);
59eb99ba
DL
1207 goto out_abort;
1208 }
0ad19a3f 1209
3a0f472d 1210 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 1211 continue;
e043236e 1212
8b004f07
SH
1213 /*
1214 * If the child process exited but was not signaled,
1215 * it didn't call reboot. This should mean it was an
1216 * lxc-execute which simply exited. In any case, treat
1217 * it as a 'halt'
1218 */
d028235d 1219 if (WIFSIGNALED(status)) {
8b004f07
SH
1220 switch(WTERMSIG(status)) {
1221 case SIGINT: /* halt */
1222 DEBUG("Container halting");
1223 break;
1224 case SIGHUP: /* reboot */
1225 DEBUG("Container rebooting");
1226 handler->conf->reboot = 1;
1227 break;
c2b9bd9e
SH
1228 case SIGSYS: /* seccomp */
1229 DEBUG("Container violated its seccomp policy");
1230 break;
8b004f07 1231 default:
959aee9c 1232 DEBUG("unknown exit status for init: %d", WTERMSIG(status));
8b004f07
SH
1233 break;
1234 }
d028235d 1235 }
828695d9 1236
ce5782df 1237 DEBUG("Pushing physical nics back to host namespace");
2af6bd1b 1238 lxc_rename_phys_nics_on_shutdown(netnsfd, handler->conf);
ce5782df
MH
1239
1240 DEBUG("Tearing down virtual network devices used by container");
1241 lxc_delete_network(handler);
1242
4b2f98f5
SH
1243 if (netnsfd >= 0)
1244 close(netnsfd);
7b35f3d6 1245
5c068da9
SH
1246 if (handler->pinfd >= 0) {
1247 close(handler->pinfd);
1248 handler->pinfd = -1;
1249 }
1250
1787abca 1251 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
3a0f472d 1252 err = lxc_error_set_and_log(handler->pid, status);
9d7f9e52 1253out_fini:
74a2b586
JK
1254 lxc_delete_network(handler);
1255
76a26f55
SH
1256out_detach_blockdev:
1257 detach_block_device(handler->conf);
1258
74a2b586 1259out_fini_nonet:
3a0f472d 1260 lxc_fini(name, handler);
0ad19a3f 1261 return err;
1262
59eb99ba 1263out_abort:
3a0f472d 1264 lxc_abort(name, handler);
9d7f9e52 1265 goto out_fini;
0ad19a3f 1266}
ee70bf78
CLG
1267
1268struct start_args {
1269 char *const *argv;
1270};
1271
1272static int start(struct lxc_handler *handler, void* data)
1273{
1274 struct start_args *arg = data;
1275
1276 NOTICE("exec'ing '%s'", arg->argv[0]);
1277
1278 execvp(arg->argv[0], arg->argv);
1279 SYSERROR("failed to exec %s", arg->argv[0]);
1280 return 0;
1281}
1282
1283static int post_start(struct lxc_handler *handler, void* data)
1284{
1285 struct start_args *arg = data;
1286
1287 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
1288 return 0;
1289}
1290
1291static struct lxc_operations start_ops = {
1292 .start = start,
1293 .post_start = post_start
1294};
1295
13f5be62 1296int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf,
507cee36 1297 const char *lxcpath, bool backgrounded)
ee70bf78
CLG
1298{
1299 struct start_args start_arg = {
1300 .argv = argv,
1301 };
1302
828695d9 1303 conf->need_utmp_watch = 1;
507cee36 1304 return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath, backgrounded);
ee70bf78 1305}
28272964
CB
1306
1307static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
1308 const char *name)
1309{
1310 char destroy[MAXPATHLEN];
1311 bool bret = true;
1312 int ret = 0;
f01f7975 1313 struct lxc_container *c;
df31363a 1314 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
28272964
CB
1315 bret = do_destroy_container(handler->conf);
1316 if (!bret) {
1317 ERROR("Error destroying rootfs for %s", name);
1318 return;
1319 }
1320 }
1321 INFO("Destroyed rootfs for %s", name);
1322
1323 ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name);
1324 if (ret < 0 || ret >= MAXPATHLEN) {
1325 ERROR("Error printing path for %s", name);
1326 ERROR("Error destroying directory for %s", name);
1327 return;
1328 }
1329
f01f7975
CB
1330 c = lxc_container_new(name, handler->lxcpath);
1331 if (c) {
1332 if (container_disk_lock(c)) {
1333 INFO("Could not update lxc_snapshots file");
1334 lxc_container_put(c);
1335 } else {
1336 mod_all_rdeps(c, false);
1337 container_disk_unlock(c);
1338 lxc_container_put(c);
1339 }
1340 }
1341
28272964
CB
1342 if (am_unpriv())
1343 ret = userns_exec_1(handler->conf, lxc_rmdir_onedev_wrapper, destroy);
1344 else
1345 ret = lxc_rmdir_onedev(destroy, NULL);
1346
1347 if (ret < 0) {
1348 ERROR("Error destroying directory for %s", name);
1349 return;
1350 }
1351 INFO("Destroyed directory for %s", name);
1352}
1353
1354static int lxc_rmdir_onedev_wrapper(void *data)
1355{
1356 char *arg = (char *) data;
1357 return lxc_rmdir_onedev(arg, NULL);
1358}
1359
1360static bool do_destroy_container(struct lxc_conf *conf) {
d028235d
SG
1361 if (am_unpriv()) {
1362 if (userns_exec_1(conf, bdev_destroy_wrapper, conf) < 0)
1363 return false;
1364 return true;
1365 }
1366 return bdev_destroy(conf);
28272964
CB
1367}
1368