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