]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
Merge pull request #1166 from brauner/2016-09-02/no_new_privileges
[mirror_lxc.git] / src / lxc / start.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _GNU_SOURCE
25 #include "config.h"
26
27 #include <alloca.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <grp.h>
32 #include <poll.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/file.h>
39 #include <sys/mount.h>
40 #include <sys/param.h>
41 #include <sys/prctl.h>
42 #include <sys/socket.h>
43 #include <sys/stat.h>
44 #include <sys/syscall.h>
45 #include <sys/types.h>
46 #include <sys/un.h>
47 #include <sys/wait.h>
48
49 #if HAVE_SYS_CAPABILITY_H
50 #include <sys/capability.h>
51 #endif
52
53 #ifndef HAVE_DECL_PR_CAPBSET_DROP
54 #define PR_CAPBSET_DROP 24
55 #endif
56
57 #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS
58 #define PR_SET_NO_NEW_PRIVS 38
59 #endif
60
61 #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS
62 #define PR_GET_NO_NEW_PRIVS 39
63 #endif
64
65 #include "af_unix.h"
66 #include "bdev.h"
67 #include "caps.h"
68 #include "cgroup.h"
69 #include "commands.h"
70 #include "conf.h"
71 #include "console.h"
72 #include "error.h"
73 #include "log.h"
74 #include "lxclock.h"
75 #include "lxcseccomp.h"
76 #include "lxcutmp.h"
77 #include "mainloop.h"
78 #include "monitor.h"
79 #include "namespace.h"
80 #include "start.h"
81 #include "sync.h"
82 #include "utils.h"
83 #include "lsm/lsm.h"
84
85 lxc_log_define(lxc_start, lxc);
86
87 const struct ns_info ns_info[LXC_NS_MAX] = {
88 [LXC_NS_MNT] = {"mnt", CLONE_NEWNS},
89 [LXC_NS_PID] = {"pid", CLONE_NEWPID},
90 [LXC_NS_UTS] = {"uts", CLONE_NEWUTS},
91 [LXC_NS_IPC] = {"ipc", CLONE_NEWIPC},
92 [LXC_NS_USER] = {"user", CLONE_NEWUSER},
93 [LXC_NS_NET] = {"net", CLONE_NEWNET},
94 [LXC_NS_CGROUP] = {"cgroup", CLONE_NEWCGROUP}
95 };
96
97 extern void mod_all_rdeps(struct lxc_container *c, bool inc);
98 static bool do_destroy_container(struct lxc_conf *conf);
99 static int lxc_rmdir_onedev_wrapper(void *data);
100 static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
101 const char *name);
102
103 static void print_top_failing_dir(const char *path)
104 {
105 size_t len = strlen(path);
106 char *copy = alloca(len + 1), *p, *e, saved;
107 strcpy(copy, path);
108
109 p = copy;
110 e = copy + len;
111 while (p < e) {
112 while (p < e && *p == '/')
113 p++;
114 while (p < e && *p != '/')
115 p++;
116 saved = *p;
117 *p = '\0';
118 if (access(copy, X_OK)) {
119 SYSERROR("could not access %s. Please grant it 'x' " \
120 "access, or add an ACL for the container root.",
121 copy);
122 return;
123 }
124 *p = saved;
125 }
126 }
127
128 static void close_ns(int ns_fd[LXC_NS_MAX]) {
129 int i;
130
131 for (i = 0; i < LXC_NS_MAX; i++) {
132 if (ns_fd[i] > -1) {
133 close(ns_fd[i]);
134 ns_fd[i] = -1;
135 }
136 }
137 }
138
139 /*
140 * preserve_ns: open /proc/@pid/ns/@ns for each namespace specified
141 * in clone_flags.
142 * Return true on success, false on failure. On failure, leave an error
143 * message in *errmsg, which caller must free.
144 */
145 static bool preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid,
146 char **errmsg)
147 {
148 int i, ret;
149 char path[MAXPATHLEN];
150
151 for (i = 0; i < LXC_NS_MAX; i++)
152 ns_fd[i] = -1;
153
154 snprintf(path, MAXPATHLEN, "/proc/%d/ns", pid);
155 if (access(path, X_OK)) {
156 if (asprintf(errmsg, "Kernel does not support setns.") == -1)
157 *errmsg = NULL;
158 return false;
159 }
160
161 for (i = 0; i < LXC_NS_MAX; i++) {
162 if ((clone_flags & ns_info[i].clone_flag) == 0)
163 continue;
164 snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid,
165 ns_info[i].proc_name);
166 ns_fd[i] = open(path, O_RDONLY | O_CLOEXEC);
167 if (ns_fd[i] < 0)
168 goto error;
169 }
170
171 return true;
172
173 error:
174 if (errno == ENOENT) {
175 ret = asprintf(errmsg, "Kernel does not support setns for %s",
176 ns_info[i].proc_name);
177 } else {
178 ret = asprintf(errmsg, "Failed to open %s: %s",
179 path, strerror(errno));
180 }
181 if (ret == -1)
182 *errmsg = NULL;
183 close_ns(ns_fd);
184 return false;
185 }
186
187 static int attach_ns(const int ns_fd[LXC_NS_MAX]) {
188 int i;
189
190 for (i = 0; i < LXC_NS_MAX; i++) {
191 if (ns_fd[i] < 0)
192 continue;
193
194 if (setns(ns_fd[i], 0) != 0)
195 goto error;
196 }
197 return 0;
198
199 error:
200 SYSERROR("failed to set namespace '%s'", ns_info[i].proc_name);
201 return -1;
202 }
203
204 static int match_fd(int fd)
205 {
206 return (fd == 0 || fd == 1 || fd == 2);
207 }
208
209 /*
210 * Check for any fds we need to close
211 * * if fd_to_ignore != -1, then if we find that fd open we will ignore it.
212 * * By default we warn about open fds we find.
213 * * If closeall is true, we will close open fds.
214 * * If lxc-start was passed "-C", then conf->close_all_fds will be true,
215 * in which case we also close all open fds.
216 * * A daemonized container will always pass closeall=true.
217 */
218 int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore)
219 {
220 struct dirent *direntp;
221 int fd, fddir;
222 DIR *dir;
223
224 if (conf && conf->close_all_fds)
225 closeall = true;
226
227 restart:
228 dir = opendir("/proc/self/fd");
229 if (!dir) {
230 WARN("failed to open directory: %m");
231 return -1;
232 }
233
234 fddir = dirfd(dir);
235
236 while ((direntp = readdir(dir))) {
237 if (!direntp)
238 break;
239
240 if (!strcmp(direntp->d_name, "."))
241 continue;
242
243 if (!strcmp(direntp->d_name, ".."))
244 continue;
245
246 fd = atoi(direntp->d_name);
247
248 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
249 continue;
250
251 if (current_config && fd == current_config->logfd)
252 continue;
253
254 if (match_fd(fd))
255 continue;
256
257 if (closeall) {
258 close(fd);
259 closedir(dir);
260 INFO("closed inherited fd %d", fd);
261 goto restart;
262 }
263 WARN("inherited fd %d", fd);
264 }
265
266 /*
267 * only enable syslog at this point to avoid the above logging function
268 * to open a new fd and make the check_inherited function enter an
269 * infinite loop.
270 */
271 lxc_log_enable_syslog();
272
273 closedir(dir); /* cannot fail */
274 return 0;
275 }
276
277 static int setup_signal_fd(sigset_t *oldmask)
278 {
279 sigset_t mask;
280 int fd;
281
282 /* Block everything except serious error signals */
283 if (sigfillset(&mask) ||
284 sigdelset(&mask, SIGILL) ||
285 sigdelset(&mask, SIGSEGV) ||
286 sigdelset(&mask, SIGBUS) ||
287 sigdelset(&mask, SIGWINCH) ||
288 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
289 SYSERROR("failed to set signal mask");
290 return -1;
291 }
292
293 fd = signalfd(-1, &mask, 0);
294 if (fd < 0) {
295 SYSERROR("failed to create the signal fd");
296 return -1;
297 }
298
299 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
300 SYSERROR("failed to set sigfd to close-on-exec");
301 close(fd);
302 return -1;
303 }
304
305 DEBUG("sigchild handler set");
306
307 return fd;
308 }
309
310 static int signal_handler(int fd, uint32_t events, void *data,
311 struct lxc_epoll_descr *descr)
312 {
313 struct signalfd_siginfo siginfo;
314 siginfo_t info;
315 int ret;
316 pid_t *pid = data;
317 bool init_died = false;
318
319 ret = read(fd, &siginfo, sizeof(siginfo));
320 if (ret < 0) {
321 ERROR("failed to read signal info");
322 return -1;
323 }
324
325 if (ret != sizeof(siginfo)) {
326 ERROR("unexpected siginfo size");
327 return -1;
328 }
329
330 // check whether init is running
331 info.si_pid = 0;
332 ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG);
333 if (ret == 0 && info.si_pid == *pid) {
334 init_died = true;
335 }
336
337 if (siginfo.ssi_signo != SIGCHLD) {
338 kill(*pid, siginfo.ssi_signo);
339 INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
340 return init_died ? 1 : 0;
341 }
342
343 if (siginfo.ssi_code == CLD_STOPPED ||
344 siginfo.ssi_code == CLD_CONTINUED) {
345 INFO("container init process was stopped/continued");
346 return init_died ? 1 : 0;
347 }
348
349 /* more robustness, protect ourself from a SIGCHLD sent
350 * by a process different from the container init
351 */
352 if (siginfo.ssi_pid != *pid) {
353 WARN("invalid pid for SIGCHLD");
354 return init_died ? 1 : 0;
355 }
356
357 DEBUG("container init process exited");
358 return 1;
359 }
360
361 int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
362 {
363 handler->state = state;
364 lxc_monitor_send_state(name, state, handler->lxcpath);
365 return 0;
366 }
367
368 int lxc_poll(const char *name, struct lxc_handler *handler)
369 {
370 int sigfd = handler->sigfd;
371 int pid = handler->pid;
372 struct lxc_epoll_descr descr;
373
374 if (lxc_mainloop_open(&descr)) {
375 ERROR("failed to create mainloop");
376 goto out_sigfd;
377 }
378
379 if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
380 ERROR("failed to add handler for the signal");
381 goto out_mainloop_open;
382 }
383
384 if (lxc_console_mainloop_add(&descr, handler->conf)) {
385 ERROR("failed to add console handler to mainloop");
386 goto out_mainloop_open;
387 }
388
389 if (lxc_cmd_mainloop_add(name, &descr, handler)) {
390 ERROR("failed to add command handler to mainloop");
391 goto out_mainloop_open;
392 }
393
394 if (handler->conf->need_utmp_watch) {
395 #if HAVE_SYS_CAPABILITY_H
396 if (lxc_utmp_mainloop_add(&descr, handler)) {
397 ERROR("failed to add utmp handler to mainloop");
398 goto out_mainloop_open;
399 }
400 #else
401 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support");
402 #endif
403 }
404
405 return lxc_mainloop(&descr, -1);
406
407 out_mainloop_open:
408 lxc_mainloop_close(&descr);
409 out_sigfd:
410 close(sigfd);
411 return -1;
412 }
413
414 struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath)
415 {
416 int i;
417 struct lxc_handler *handler;
418
419 handler = malloc(sizeof(*handler));
420 if (!handler)
421 return NULL;
422
423 memset(handler, 0, sizeof(*handler));
424
425 handler->ttysock[0] = handler->ttysock[1] = -1;
426 handler->conf = conf;
427 handler->lxcpath = lxcpath;
428 handler->pinfd = -1;
429
430 for (i = 0; i < LXC_NS_MAX; i++)
431 handler->nsfd[i] = -1;
432
433 lsm_init();
434
435 handler->name = strdup(name);
436 if (!handler->name) {
437 ERROR("failed to allocate memory");
438 goto out_free;
439 }
440
441 if (lxc_cmd_init(name, handler, lxcpath))
442 goto out_free_name;
443
444 if (lxc_read_seccomp_config(conf) != 0) {
445 ERROR("failed loading seccomp policy");
446 goto out_close_maincmd_fd;
447 }
448
449 /* Begin by setting the state to STARTING */
450 if (lxc_set_state(name, handler, STARTING)) {
451 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
452 goto out_close_maincmd_fd;
453 }
454
455 /* Start of environment variable setup for hooks */
456 if (name && setenv("LXC_NAME", name, 1)) {
457 SYSERROR("failed to set environment variable for container name");
458 }
459 if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
460 SYSERROR("failed to set environment variable for config path");
461 }
462 if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
463 SYSERROR("failed to set environment variable for rootfs mount");
464 }
465 if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
466 SYSERROR("failed to set environment variable for rootfs mount");
467 }
468 if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) {
469 SYSERROR("failed to set environment variable for console path");
470 }
471 if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) {
472 SYSERROR("failed to set environment variable for console log");
473 }
474 if (setenv("LXC_CGNS_AWARE", "1", 1)) {
475 SYSERROR("failed to set LXC_CGNS_AWARE environment variable");
476 }
477 /* End of environment variable setup for hooks */
478
479 if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) {
480 ERROR("failed to run pre-start hooks for container '%s'.", name);
481 goto out_aborting;
482 }
483
484 /* the signal fd has to be created before forking otherwise
485 * if the child process exits before we setup the signal fd,
486 * the event will be lost and the command will be stuck */
487 handler->sigfd = setup_signal_fd(&handler->oldmask);
488 if (handler->sigfd < 0) {
489 ERROR("failed to set sigchild fd handler");
490 goto out_delete_tty;
491 }
492
493 /* do this after setting up signals since it might unblock SIGWINCH */
494 if (lxc_console_create(conf)) {
495 ERROR("failed to create console");
496 goto out_restore_sigmask;
497 }
498
499 if (ttys_shift_ids(conf) < 0) {
500 ERROR("Failed to shift tty into container");
501 goto out_restore_sigmask;
502 }
503
504 INFO("'%s' is initialized", name);
505 return handler;
506
507 out_restore_sigmask:
508 sigprocmask(SIG_SETMASK, &handler->oldmask, NULL);
509 out_delete_tty:
510 lxc_delete_tty(&conf->tty_info);
511 out_aborting:
512 lxc_set_state(name, handler, ABORTING);
513 out_close_maincmd_fd:
514 close(conf->maincmd_fd);
515 conf->maincmd_fd = -1;
516 out_free_name:
517 free(handler->name);
518 handler->name = NULL;
519 out_free:
520 free(handler);
521 return NULL;
522 }
523
524 void lxc_fini(const char *name, struct lxc_handler *handler)
525 {
526 int i, rc;
527 pid_t self = getpid();
528 char *namespaces[LXC_NS_MAX+1];
529 size_t namespace_count = 0;
530
531 /* The STOPPING state is there for future cleanup code
532 * which can take awhile
533 */
534 lxc_set_state(name, handler, STOPPING);
535
536 for (i = 0; i < LXC_NS_MAX; i++) {
537 if (handler->nsfd[i] != -1) {
538 rc = asprintf(&namespaces[namespace_count], "%s:/proc/%d/fd/%d",
539 ns_info[i].proc_name, self, handler->nsfd[i]);
540 if (rc == -1) {
541 SYSERROR("failed to allocate memory");
542 break;
543 }
544 ++namespace_count;
545 }
546 }
547 namespaces[namespace_count] = NULL;
548
549 if (handler->conf->reboot && setenv("LXC_TARGET", "reboot", 1)) {
550 SYSERROR("failed to set environment variable for stop target");
551 }
552 if (!handler->conf->reboot && setenv("LXC_TARGET", "stop", 1)) {
553 SYSERROR("failed to set environment variable for stop target");
554 }
555
556 if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath, namespaces))
557 ERROR("failed to run stop hooks for container '%s'.", name);
558
559 while (namespace_count--)
560 free(namespaces[namespace_count]);
561 for (i = 0; i < LXC_NS_MAX; i++) {
562 if (handler->nsfd[i] != -1) {
563 close(handler->nsfd[i]);
564 handler->nsfd[i] = -1;
565 }
566 }
567 lxc_set_state(name, handler, STOPPED);
568
569 if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) {
570 ERROR("failed to run post-stop hooks for container '%s'.", name);
571 if (handler->conf->reboot) {
572 WARN("Container will be stopped instead of rebooted.");
573 handler->conf->reboot = 0;
574 setenv("LXC_TARGET", "stop", 1);
575 }
576 }
577
578 /* reset mask set by setup_signal_fd */
579 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
580 WARN("failed to restore sigprocmask");
581
582 lxc_console_delete(&handler->conf->console);
583 lxc_delete_tty(&handler->conf->tty_info);
584 close(handler->conf->maincmd_fd);
585 handler->conf->maincmd_fd = -1;
586 free(handler->name);
587 if (handler->ttysock[0] != -1) {
588 close(handler->ttysock[0]);
589 close(handler->ttysock[1]);
590 }
591
592 if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1)
593 lxc_destroy_container_on_signal(handler, name);
594
595 cgroup_destroy(handler);
596 free(handler);
597 }
598
599 void lxc_abort(const char *name, struct lxc_handler *handler)
600 {
601 int ret, status;
602
603 lxc_set_state(name, handler, ABORTING);
604 if (handler->pid > 0)
605 kill(handler->pid, SIGKILL);
606 while ((ret = waitpid(-1, &status, 0)) > 0) ;
607 }
608
609 #include <sys/reboot.h>
610 #include <linux/reboot.h>
611
612 /*
613 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
614 * in a child pid namespace if container reboot support exists.
615 * Otherwise, it will either succeed or return -EPERM.
616 */
617 static int container_reboot_supported(void *arg)
618 {
619 int *cmd = arg;
620 int ret;
621
622 ret = reboot(*cmd);
623 if (ret == -1 && errno == EINVAL)
624 return 1;
625 return 0;
626 }
627
628 static int must_drop_cap_sys_boot(struct lxc_conf *conf)
629 {
630 FILE *f;
631 int ret, cmd, v, flags;
632 long stack_size = 4096;
633 void *stack = alloca(stack_size);
634 int status;
635 pid_t pid;
636
637 f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
638 if (!f) {
639 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
640 return 1;
641 }
642
643 ret = fscanf(f, "%d", &v);
644 fclose(f);
645 if (ret != 1) {
646 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
647 return 1;
648 }
649 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
650
651 flags = CLONE_NEWPID | SIGCHLD;
652 if (!lxc_list_empty(&conf->id_map))
653 flags |= CLONE_NEWUSER;
654
655 #ifdef __ia64__
656 pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd);
657 #else
658 stack += stack_size;
659 pid = clone(container_reboot_supported, stack, flags, &cmd);
660 #endif
661 if (pid < 0) {
662 if (flags & CLONE_NEWUSER)
663 ERROR("failed to clone (%#x): %s (includes CLONE_NEWUSER)", flags, strerror(errno));
664 else
665 ERROR("failed to clone (%#x): %s", flags, strerror(errno));
666 return -1;
667 }
668 if (wait(&status) < 0) {
669 SYSERROR("unexpected wait error: %m");
670 return -1;
671 }
672
673 if (WEXITSTATUS(status) != 1)
674 return 1;
675
676 return 0;
677 }
678
679 /*
680 * netpipe is used in the unprivileged case to transfer the ifindexes
681 * from parent to child
682 */
683 static int netpipe = -1;
684
685 static inline int count_veths(struct lxc_list *network)
686 {
687 struct lxc_list *iterator;
688 struct lxc_netdev *netdev;
689 int count = 0;
690
691 lxc_list_for_each(iterator, network) {
692 netdev = iterator->elem;
693 if (netdev->type != LXC_NET_VETH)
694 continue;
695 count++;
696 }
697 return count;
698 }
699
700 static int read_unpriv_netifindex(struct lxc_list *network)
701 {
702 struct lxc_list *iterator;
703 struct lxc_netdev *netdev;
704
705 if (netpipe == -1)
706 return 0;
707 lxc_list_for_each(iterator, network) {
708 netdev = iterator->elem;
709 if (netdev->type != LXC_NET_VETH)
710 continue;
711 if (!(netdev->name = malloc(IFNAMSIZ))) {
712 ERROR("Out of memory");
713 close(netpipe);
714 return -1;
715 }
716 if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) {
717 close(netpipe);
718 return -1;
719 }
720 }
721 close(netpipe);
722 return 0;
723 }
724
725 static int do_start(void *data)
726 {
727 struct lxc_list *iterator;
728 struct lxc_handler *handler = data;
729 int devnull_fd = -1, ret;
730 char path[PATH_MAX];
731
732 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
733 SYSERROR("failed to set sigprocmask");
734 return -1;
735 }
736
737 /* This prctl must be before the synchro, so if the parent
738 * dies before we set the parent death signal, we will detect
739 * its death with the synchro right after, otherwise we have
740 * a window where the parent can exit before we set the pdeath
741 * signal leading to a unsupervized container.
742 */
743 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
744 SYSERROR("failed to set pdeath signal");
745 return -1;
746 }
747
748 lxc_sync_fini_parent(handler);
749
750 /* don't leak the pinfd to the container */
751 if (handler->pinfd >= 0) {
752 close(handler->pinfd);
753 }
754
755 if (lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP))
756 return -1;
757
758 /* Unshare CLONE_NEWNET after CLONE_NEWUSER - see
759 https://github.com/lxc/lxd/issues/1978 */
760 if ((handler->clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) ==
761 (CLONE_NEWNET | CLONE_NEWUSER)) {
762 ret = unshare(CLONE_NEWNET);
763 if (ret < 0) {
764 SYSERROR("Error unsharing network namespace");
765 goto out_warn_father;
766 }
767 }
768
769 /* Tell the parent task it can begin to configure the
770 * container and wait for it to finish
771 */
772 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
773 return -1;
774
775 if (read_unpriv_netifindex(&handler->conf->network) < 0)
776 goto out_warn_father;
777
778 /*
779 * if we are in a new user namespace, become root there to have
780 * privilege over our namespace. When using lxc-execute we default to root,
781 * but this can be overriden using the lxc.init_uid and lxc.init_gid
782 * configuration options.
783 */
784 if (!lxc_list_empty(&handler->conf->id_map)) {
785 gid_t new_gid = 0;
786 if (handler->conf->is_execute && handler->conf->init_gid)
787 new_gid = handler->conf->init_gid;
788
789 uid_t new_uid = 0;
790 if (handler->conf->is_execute && handler->conf->init_uid)
791 new_uid = handler->conf->init_uid;
792
793 NOTICE("switching to gid/uid %d/%d in new user namespace", new_gid, new_uid);
794 if (setgid(new_gid)) {
795 SYSERROR("setgid");
796 goto out_warn_father;
797 }
798 if (setuid(new_uid)) {
799 SYSERROR("setuid");
800 goto out_warn_father;
801 }
802 if (setgroups(0, NULL)) {
803 SYSERROR("setgroups");
804 goto out_warn_father;
805 }
806 }
807
808 if (access(handler->lxcpath, X_OK)) {
809 print_top_failing_dir(handler->lxcpath);
810 goto out_warn_father;
811 }
812
813 #if HAVE_SYS_CAPABILITY_H
814 if (handler->conf->need_utmp_watch) {
815 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
816 SYSERROR("failed to remove CAP_SYS_BOOT capability");
817 goto out_warn_father;
818 }
819 DEBUG("Dropped cap_sys_boot");
820 }
821 #endif
822
823 ret = snprintf(path, sizeof(path), "%s/dev/null", handler->conf->rootfs.mount);
824 if (ret < 0 || ret >= sizeof(path)) {
825 SYSERROR("sprintf'd too many chars");
826 goto out_warn_father;
827 }
828
829 /* In order to checkpoint restore, we need to have everything in the
830 * same mount namespace. However, some containers may not have a
831 * reasonable /dev (in particular, they may not have /dev/null), so we
832 * can't set init's std fds to /dev/null by opening it from inside the
833 * container.
834 *
835 * If that's the case, fall back to using the host's /dev/null. This
836 * means that migration won't work, but at least we won't spew output
837 * where it isn't wanted.
838 */
839 if (handler->backgrounded && !handler->conf->autodev && access(path, F_OK) < 0) {
840 devnull_fd = open_devnull();
841
842 if (devnull_fd < 0)
843 goto out_warn_father;
844 WARN("using host's /dev/null for container init's std fds, migraiton won't work");
845 }
846
847 /* Setup the container, ip, names, utsname, ... */
848 if (lxc_setup(handler)) {
849 ERROR("failed to setup the container");
850 goto out_warn_father;
851 }
852
853 /* ask father to setup cgroups and wait for him to finish */
854 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP))
855 goto out_error;
856
857 /* Set the label to change to when we exec(2) the container's init */
858 if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0)
859 goto out_warn_father;
860
861 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
862 * before we aren't allowed anymore. */
863 if (handler->conf->no_new_privs) {
864 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
865 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
866 goto out_warn_father;
867 }
868 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges.");
869 }
870
871 /* Some init's such as busybox will set sane tty settings on stdin,
872 * stdout, stderr which it thinks is the console. We already set them
873 * the way we wanted on the real terminal, and we want init to do its
874 * setup on its console ie. the pty allocated in lxc_console_create()
875 * so make sure that that pty is stdin,stdout,stderr.
876 */
877 if (lxc_console_set_stdfds(handler->conf->console.slave) < 0)
878 goto out_warn_father;
879
880 /* If we mounted a temporary proc, then unmount it now */
881 tmp_proc_unmount(handler->conf);
882
883 if (lxc_seccomp_load(handler->conf) != 0)
884 goto out_warn_father;
885
886 if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) {
887 ERROR("failed to run start hooks for container '%s'.", handler->name);
888 goto out_warn_father;
889 }
890
891 /* The clearenv() and putenv() calls have been moved here
892 * to allow us to use environment variables passed to the various
893 * hooks, such as the start hook above. Not all of the
894 * variables like CONFIG_PATH or ROOTFS are valid in this
895 * context but others are. */
896 if (clearenv()) {
897 SYSERROR("failed to clear environment");
898 /* don't error out though */
899 }
900
901 lxc_list_for_each(iterator, &handler->conf->environment) {
902 if (putenv((char *)iterator->elem)) {
903 SYSERROR("failed to set environment variable '%s'", (char *)iterator->elem);
904 goto out_warn_father;
905 }
906 }
907
908 if (putenv("container=lxc")) {
909 SYSERROR("failed to set environment variable 'container=lxc'");
910 goto out_warn_father;
911 }
912
913 if (handler->conf->pty_names) {
914 if (putenv(handler->conf->pty_names)) {
915 SYSERROR("failed to set environment variable for container ptys");
916 goto out_warn_father;
917 }
918 }
919
920 close(handler->sigfd);
921
922 if (devnull_fd < 0) {
923 devnull_fd = open_devnull();
924
925 if (devnull_fd < 0)
926 goto out_warn_father;
927 }
928
929 if (handler->backgrounded && set_stdfds(devnull_fd))
930 goto out_warn_father;
931
932 if (devnull_fd >= 0) {
933 close(devnull_fd);
934 devnull_fd = -1;
935 }
936
937 if (cgns_supported() && unshare(CLONE_NEWCGROUP) != 0) {
938 SYSERROR("Failed to unshare cgroup namespace");
939 goto out_warn_father;
940 }
941
942 setsid();
943
944 /* after this call, we are in error because this
945 * ops should not return as it execs */
946 handler->ops->start(handler, handler->data);
947
948 out_warn_father:
949 /* we want the parent to know something went wrong, so we return a special
950 * error code. */
951 lxc_sync_wake_parent(handler, LXC_SYNC_ERROR);
952
953 out_error:
954 if (devnull_fd >= 0)
955 close(devnull_fd);
956
957 return -1;
958 }
959
960 static int save_phys_nics(struct lxc_conf *conf)
961 {
962 struct lxc_list *iterator;
963 int am_root = (getuid() == 0);
964
965 if (!am_root)
966 return 0;
967
968 lxc_list_for_each(iterator, &conf->network) {
969 struct lxc_netdev *netdev = iterator->elem;
970
971 if (netdev->type != LXC_NET_PHYS)
972 continue;
973 conf->saved_nics = realloc(conf->saved_nics,
974 (conf->num_savednics+1)*sizeof(struct saved_nic));
975 if (!conf->saved_nics) {
976 SYSERROR("failed to allocate memory");
977 return -1;
978 }
979 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
980 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
981 if (!conf->saved_nics[conf->num_savednics].orig_name) {
982 SYSERROR("failed to allocate memory");
983 return -1;
984 }
985 INFO("stored saved_nic #%d idx %d name %s", conf->num_savednics,
986 conf->saved_nics[conf->num_savednics].ifindex,
987 conf->saved_nics[conf->num_savednics].orig_name);
988 conf->num_savednics++;
989 }
990
991 return 0;
992 }
993
994 static int recv_fd(int sock, int *fd)
995 {
996 if (lxc_abstract_unix_recv_fd(sock, fd, NULL, 0) < 0) {
997 SYSERROR("Error receiving tty fd from child");
998 return -1;
999 }
1000 if (*fd == -1)
1001 return -1;
1002 return 0;
1003 }
1004
1005 static int recv_ttys_from_child(struct lxc_handler *handler)
1006 {
1007 struct lxc_conf *conf = handler->conf;
1008 int i, sock = handler->ttysock[1];
1009 struct lxc_tty_info *tty_info = &conf->tty_info;
1010
1011 if (!conf->tty)
1012 return 0;
1013
1014 tty_info->pty_info = malloc(sizeof(*tty_info->pty_info)*conf->tty);
1015 if (!tty_info->pty_info) {
1016 SYSERROR("failed to allocate pty_info");
1017 return -1;
1018 }
1019
1020 for (i = 0; i < conf->tty; i++) {
1021 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
1022 pty_info->busy = 0;
1023 if (recv_fd(sock, &pty_info->slave) < 0 ||
1024 recv_fd(sock, &pty_info->master) < 0) {
1025 ERROR("Error receiving tty info from child");
1026 return -1;
1027 }
1028 }
1029 tty_info->nbtty = conf->tty;
1030
1031 return 0;
1032 }
1033
1034 void resolve_clone_flags(struct lxc_handler *handler)
1035 {
1036 handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS;
1037
1038 if (!lxc_list_empty(&handler->conf->id_map)) {
1039 INFO("Cloning a new user namespace");
1040 handler->clone_flags |= CLONE_NEWUSER;
1041 }
1042
1043 if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) {
1044 if (!lxc_requests_empty_network(handler))
1045 handler->clone_flags |= CLONE_NEWNET;
1046 } else {
1047 INFO("Inheriting a net namespace");
1048 }
1049
1050 if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) {
1051 handler->clone_flags |= CLONE_NEWIPC;
1052 } else {
1053 INFO("Inheriting an IPC namespace");
1054 }
1055
1056 if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) {
1057 handler->clone_flags |= CLONE_NEWUTS;
1058 } else {
1059 INFO("Inheriting a UTS namespace");
1060 }
1061 }
1062
1063 static int lxc_spawn(struct lxc_handler *handler)
1064 {
1065 int failed_before_rename = 0;
1066 const char *name = handler->name;
1067 char *errmsg = NULL;
1068 bool cgroups_connected = false;
1069 int saved_ns_fd[LXC_NS_MAX];
1070 int preserve_mask = 0, i, flags;
1071 int netpipepair[2], nveths;
1072
1073 netpipe = -1;
1074
1075 for (i = 0; i < LXC_NS_MAX; i++)
1076 if (handler->conf->inherit_ns_fd[i] != -1)
1077 preserve_mask |= ns_info[i].clone_flag;
1078
1079 if (lxc_sync_init(handler))
1080 return -1;
1081
1082 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handler->ttysock) < 0) {
1083 lxc_sync_fini(handler);
1084 return -1;
1085 }
1086
1087 resolve_clone_flags(handler);
1088
1089 if (handler->clone_flags & CLONE_NEWNET) {
1090 if (!lxc_list_empty(&handler->conf->network)) {
1091
1092 /* Find gateway addresses from the link device, which is
1093 * no longer accessible inside the container. Do this
1094 * before creating network interfaces, since goto
1095 * out_delete_net does not work before lxc_clone. */
1096 if (lxc_find_gateway_addresses(handler)) {
1097 ERROR("failed to find gateway addresses");
1098 lxc_sync_fini(handler);
1099 return -1;
1100 }
1101
1102 /* that should be done before the clone because we will
1103 * fill the netdev index and use them in the child
1104 */
1105 if (lxc_create_network(handler)) {
1106 ERROR("failed to create the network");
1107 lxc_sync_fini(handler);
1108 return -1;
1109 }
1110 }
1111
1112 if (save_phys_nics(handler->conf)) {
1113 ERROR("failed to save physical nic info");
1114 goto out_abort;
1115 }
1116 }
1117
1118 if (!cgroup_init(handler)) {
1119 ERROR("failed initializing cgroup support");
1120 goto out_delete_net;
1121 }
1122
1123 cgroups_connected = true;
1124
1125 if (!cgroup_create(handler)) {
1126 ERROR("failed creating cgroups");
1127 goto out_delete_net;
1128 }
1129
1130 /*
1131 * if the rootfs is not a blockdev, prevent the container from
1132 * marking it readonly.
1133 *
1134 * if the container is unprivileged then skip rootfs pinning
1135 */
1136 if (lxc_list_empty(&handler->conf->id_map)) {
1137 handler->pinfd = pin_rootfs(handler->conf->rootfs.path);
1138 if (handler->pinfd == -1)
1139 INFO("failed to pin the container's rootfs");
1140 }
1141
1142 if (!preserve_ns(saved_ns_fd, preserve_mask, getpid(), &errmsg)) {
1143 SYSERROR("Failed to preserve requested namespaces: %s",
1144 errmsg ? errmsg : "(Out of memory)");
1145 free(errmsg);
1146 goto out_delete_net;
1147 }
1148 if (attach_ns(handler->conf->inherit_ns_fd) < 0)
1149 goto out_delete_net;
1150
1151 if (am_unpriv() && (nveths = count_veths(&handler->conf->network))) {
1152 if (pipe(netpipepair) < 0) {
1153 SYSERROR("Error creating pipe");
1154 goto out_delete_net;
1155 }
1156 /* store netpipe in the global var for do_start's use */
1157 netpipe = netpipepair[0];
1158 }
1159
1160 /* Create a process in a new set of namespaces */
1161 flags = handler->clone_flags;
1162 if (handler->clone_flags & CLONE_NEWUSER)
1163 flags &= ~CLONE_NEWNET;
1164 handler->pid = lxc_clone(do_start, handler, handler->clone_flags);
1165 if (handler->pid < 0) {
1166 SYSERROR("failed to fork into a new namespace");
1167 goto out_delete_net;
1168 }
1169
1170 if (!preserve_ns(handler->nsfd, handler->clone_flags | preserve_mask, handler->pid, &errmsg)) {
1171 INFO("Failed to store namespace references for stop hook: %s",
1172 errmsg ? errmsg : "(Out of memory)");
1173 free(errmsg);
1174 }
1175
1176 if (attach_ns(saved_ns_fd))
1177 WARN("failed to restore saved namespaces");
1178
1179 lxc_sync_fini_child(handler);
1180
1181 /* map the container uids - the container became an invalid
1182 * userid the moment it was cloned with CLONE_NEWUSER - this
1183 * call doesn't change anything immediately, but allows the
1184 * container to setuid(0) (0 being mapped to something else on
1185 * the host) later to become a valid uid again */
1186 if (lxc_map_ids(&handler->conf->id_map, handler->pid)) {
1187 ERROR("failed to set up id mapping");
1188 goto out_delete_net;
1189 }
1190
1191 if (lxc_sync_wake_child(handler, LXC_SYNC_STARTUP)) {
1192 failed_before_rename = 1;
1193 goto out_delete_net;
1194 }
1195
1196 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) {
1197 failed_before_rename = 1;
1198 goto out_delete_net;
1199 }
1200
1201 if (!cgroup_create_legacy(handler)) {
1202 ERROR("failed to setup the legacy cgroups for %s", name);
1203 goto out_delete_net;
1204 }
1205 if (!cgroup_setup_limits(handler, false)) {
1206 ERROR("failed to setup the cgroup limits for '%s'", name);
1207 goto out_delete_net;
1208 }
1209
1210 if (!cgroup_enter(handler))
1211 goto out_delete_net;
1212
1213 if (!cgroup_chown(handler))
1214 goto out_delete_net;
1215
1216 if (failed_before_rename)
1217 goto out_delete_net;
1218
1219 /* Create the network configuration */
1220 if (handler->clone_flags & CLONE_NEWNET) {
1221 if (lxc_assign_network(handler->lxcpath, handler->name,
1222 &handler->conf->network, handler->pid)) {
1223 ERROR("failed to create the configured network");
1224 goto out_delete_net;
1225 }
1226 }
1227
1228 if (netpipe != -1) {
1229 struct lxc_list *iterator;
1230 struct lxc_netdev *netdev;
1231
1232 close(netpipe);
1233 lxc_list_for_each(iterator, &handler->conf->network) {
1234 netdev = iterator->elem;
1235 if (netdev->type != LXC_NET_VETH)
1236 continue;
1237 if (write(netpipepair[1], netdev->name, IFNAMSIZ) != IFNAMSIZ) {
1238 ERROR("Error writing veth name to container");
1239 goto out_delete_net;
1240 }
1241 }
1242 close(netpipepair[1]);
1243 }
1244
1245 /* Tell the child to continue its initialization. we'll get
1246 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
1247 */
1248 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
1249 goto out_delete_net;
1250
1251 if (!cgroup_setup_limits(handler, true)) {
1252 ERROR("failed to setup the devices cgroup for '%s'", name);
1253 goto out_delete_net;
1254 }
1255
1256 cgroup_disconnect();
1257 cgroups_connected = false;
1258
1259 /* read tty fds allocated by child */
1260 if (recv_ttys_from_child(handler) < 0) {
1261 ERROR("failed to receive tty info from child");
1262 goto out_delete_net;
1263 }
1264
1265 /* Tell the child to complete its initialization and wait for
1266 * it to exec or return an error. (the child will never
1267 * return LXC_SYNC_POST_CGROUP+1. It will either close the
1268 * sync pipe, causing lxc_sync_barrier_child to return
1269 * success, or return a different value, causing us to error
1270 * out).
1271 */
1272 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP))
1273 return -1;
1274
1275 if (detect_shared_rootfs())
1276 umount2(handler->conf->rootfs.mount, MNT_DETACH);
1277
1278 if (handler->ops->post_start(handler, handler->data))
1279 goto out_abort;
1280
1281 if (lxc_set_state(name, handler, RUNNING)) {
1282 ERROR("failed to set state to %s",
1283 lxc_state2str(RUNNING));
1284 goto out_abort;
1285 }
1286
1287 lxc_sync_fini(handler);
1288
1289 return 0;
1290
1291 out_delete_net:
1292 if (cgroups_connected)
1293 cgroup_disconnect();
1294 if (handler->clone_flags & CLONE_NEWNET)
1295 lxc_delete_network(handler);
1296 out_abort:
1297 lxc_abort(name, handler);
1298 lxc_sync_fini(handler);
1299 if (handler->pinfd >= 0) {
1300 close(handler->pinfd);
1301 handler->pinfd = -1;
1302 }
1303
1304 return -1;
1305 }
1306
1307 int get_netns_fd(int pid)
1308 {
1309 char path[MAXPATHLEN];
1310 int ret, fd;
1311
1312 ret = snprintf(path, MAXPATHLEN, "/proc/%d/ns/net", pid);
1313 if (ret < 0 || ret >= MAXPATHLEN) {
1314 WARN("Failed to pin netns file for pid %d", pid);
1315 return -1;
1316 }
1317
1318 fd = open(path, O_RDONLY);
1319 if (fd < 0) {
1320 WARN("Failed to pin netns file %s for pid %d: %s",
1321 path, pid, strerror(errno));
1322 return -1;
1323 }
1324 return fd;
1325 }
1326
1327 int __lxc_start(const char *name, struct lxc_conf *conf,
1328 struct lxc_operations* ops, void *data, const char *lxcpath,
1329 bool backgrounded)
1330 {
1331 struct lxc_handler *handler;
1332 int err = -1;
1333 int status;
1334 int netnsfd = -1;
1335
1336 handler = lxc_init(name, conf, lxcpath);
1337 if (!handler) {
1338 ERROR("failed to initialize the container");
1339 return -1;
1340 }
1341 handler->ops = ops;
1342 handler->data = data;
1343 handler->backgrounded = backgrounded;
1344
1345 if (must_drop_cap_sys_boot(handler->conf)) {
1346 #if HAVE_SYS_CAPABILITY_H
1347 DEBUG("Dropping cap_sys_boot");
1348 #else
1349 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported");
1350 #endif
1351 } else {
1352 DEBUG("Not dropping cap_sys_boot or watching utmp");
1353 handler->conf->need_utmp_watch = 0;
1354 }
1355
1356 if (!attach_block_device(handler->conf)) {
1357 ERROR("Failure attaching block device");
1358 goto out_fini_nonet;
1359 }
1360
1361 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
1362 /* if the backing store is a device, mount it here and now */
1363 if (rootfs_is_blockdev(conf)) {
1364 if (unshare(CLONE_NEWNS) < 0) {
1365 ERROR("Error unsharing mounts");
1366 goto out_fini_nonet;
1367 }
1368 remount_all_slave();
1369 if (do_rootfs_setup(conf, name, lxcpath) < 0) {
1370 ERROR("Error setting up rootfs mount as root before spawn");
1371 goto out_fini_nonet;
1372 }
1373 INFO("Set up container rootfs as host root");
1374 }
1375 }
1376
1377 err = lxc_spawn(handler);
1378 if (err) {
1379 ERROR("failed to spawn '%s'", name);
1380 goto out_detach_blockdev;
1381 }
1382
1383 handler->conf->reboot = 0;
1384
1385 netnsfd = get_netns_fd(handler->pid);
1386
1387 err = lxc_poll(name, handler);
1388 if (err) {
1389 ERROR("mainloop exited with an error");
1390 if (netnsfd >= 0)
1391 close(netnsfd);
1392 goto out_abort;
1393 }
1394
1395 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1396 continue;
1397
1398 /*
1399 * If the child process exited but was not signaled,
1400 * it didn't call reboot. This should mean it was an
1401 * lxc-execute which simply exited. In any case, treat
1402 * it as a 'halt'
1403 */
1404 if (WIFSIGNALED(status)) {
1405 switch(WTERMSIG(status)) {
1406 case SIGINT: /* halt */
1407 DEBUG("Container halting");
1408 break;
1409 case SIGHUP: /* reboot */
1410 DEBUG("Container rebooting");
1411 handler->conf->reboot = 1;
1412 break;
1413 case SIGSYS: /* seccomp */
1414 DEBUG("Container violated its seccomp policy");
1415 break;
1416 default:
1417 DEBUG("unknown exit status for init: %d", WTERMSIG(status));
1418 break;
1419 }
1420 }
1421
1422 DEBUG("Pushing physical nics back to host namespace");
1423 lxc_rename_phys_nics_on_shutdown(netnsfd, handler->conf);
1424
1425 DEBUG("Tearing down virtual network devices used by container");
1426 lxc_delete_network(handler);
1427
1428 if (netnsfd >= 0)
1429 close(netnsfd);
1430
1431 if (handler->pinfd >= 0) {
1432 close(handler->pinfd);
1433 handler->pinfd = -1;
1434 }
1435
1436 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
1437 err = lxc_error_set_and_log(handler->pid, status);
1438 out_fini:
1439 lxc_delete_network(handler);
1440
1441 out_detach_blockdev:
1442 detach_block_device(handler->conf);
1443
1444 out_fini_nonet:
1445 lxc_fini(name, handler);
1446 return err;
1447
1448 out_abort:
1449 lxc_abort(name, handler);
1450 goto out_fini;
1451 }
1452
1453 struct start_args {
1454 char *const *argv;
1455 };
1456
1457 static int start(struct lxc_handler *handler, void* data)
1458 {
1459 struct start_args *arg = data;
1460
1461 NOTICE("exec'ing '%s'", arg->argv[0]);
1462
1463 execvp(arg->argv[0], arg->argv);
1464 SYSERROR("failed to exec %s", arg->argv[0]);
1465 return 0;
1466 }
1467
1468 static int post_start(struct lxc_handler *handler, void* data)
1469 {
1470 struct start_args *arg = data;
1471
1472 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
1473 return 0;
1474 }
1475
1476 static struct lxc_operations start_ops = {
1477 .start = start,
1478 .post_start = post_start
1479 };
1480
1481 int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf,
1482 const char *lxcpath, bool backgrounded)
1483 {
1484 struct start_args start_arg = {
1485 .argv = argv,
1486 };
1487
1488 conf->need_utmp_watch = 1;
1489 return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath, backgrounded);
1490 }
1491
1492 static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
1493 const char *name)
1494 {
1495 char destroy[MAXPATHLEN];
1496 bool bret = true;
1497 int ret = 0;
1498 struct lxc_container *c;
1499 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
1500 bret = do_destroy_container(handler->conf);
1501 if (!bret) {
1502 ERROR("Error destroying rootfs for %s", name);
1503 return;
1504 }
1505 }
1506 INFO("Destroyed rootfs for %s", name);
1507
1508 ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name);
1509 if (ret < 0 || ret >= MAXPATHLEN) {
1510 ERROR("Error printing path for %s", name);
1511 ERROR("Error destroying directory for %s", name);
1512 return;
1513 }
1514
1515 c = lxc_container_new(name, handler->lxcpath);
1516 if (c) {
1517 if (container_disk_lock(c)) {
1518 INFO("Could not update lxc_snapshots file");
1519 lxc_container_put(c);
1520 } else {
1521 mod_all_rdeps(c, false);
1522 container_disk_unlock(c);
1523 lxc_container_put(c);
1524 }
1525 }
1526
1527 if (am_unpriv())
1528 ret = userns_exec_1(handler->conf, lxc_rmdir_onedev_wrapper, destroy);
1529 else
1530 ret = lxc_rmdir_onedev(destroy, NULL);
1531
1532 if (ret < 0) {
1533 ERROR("Error destroying directory for %s", name);
1534 return;
1535 }
1536 INFO("Destroyed directory for %s", name);
1537 }
1538
1539 static int lxc_rmdir_onedev_wrapper(void *data)
1540 {
1541 char *arg = (char *) data;
1542 return lxc_rmdir_onedev(arg, NULL);
1543 }
1544
1545 static bool do_destroy_container(struct lxc_conf *conf) {
1546 if (am_unpriv()) {
1547 if (userns_exec_1(conf, bdev_destroy_wrapper, conf) < 0)
1548 return false;
1549 return true;
1550 }
1551 return bdev_destroy(conf);
1552 }