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