]> git.proxmox.com Git - mirror_lxc.git/blame_incremental - src/lxc/start.c
tree-wide: remove "name" argument from lxc_{fini,abort}()
[mirror_lxc.git] / src / lxc / start.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
6#include <dirent.h>
7#include <errno.h>
8#include <fcntl.h>
9#include <grp.h>
10#include <poll.h>
11#include <pthread.h>
12#include <signal.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/file.h>
17#include <sys/mount.h>
18#include <sys/param.h>
19#include <sys/prctl.h>
20#include <sys/socket.h>
21#include <sys/stat.h>
22#include <sys/syscall.h>
23#include <sys/types.h>
24#include <sys/un.h>
25#include <sys/wait.h>
26#include <unistd.h>
27
28#include "af_unix.h"
29#include "caps.h"
30#include "cgroup.h"
31#include "commands.h"
32#include "commands_utils.h"
33#include "conf.h"
34#include "config.h"
35#include "confile_utils.h"
36#include "error.h"
37#include "file_utils.h"
38#include "list.h"
39#include "log.h"
40#include "lsm/lsm.h"
41#include "lxccontainer.h"
42#include "lxclock.h"
43#include "lxcseccomp.h"
44#include "macro.h"
45#include "mainloop.h"
46#include "memory_utils.h"
47#include "monitor.h"
48#include "namespace.h"
49#include "network.h"
50#include "raw_syscalls.h"
51#include "start.h"
52#include "storage/storage.h"
53#include "storage/storage_utils.h"
54#include "sync.h"
55#include "syscall_wrappers.h"
56#include "terminal.h"
57#include "utils.h"
58
59#if HAVE_LIBCAP
60#include <sys/capability.h>
61#endif
62
63#ifndef HAVE_STRLCPY
64#include "include/strlcpy.h"
65#endif
66
67lxc_log_define(start, lxc);
68
69extern void mod_all_rdeps(struct lxc_container *c, bool inc);
70static bool do_destroy_container(struct lxc_handler *handler);
71static int lxc_rmdir_onedev_wrapper(void *data);
72static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
73 const char *name);
74
75static void print_top_failing_dir(const char *path)
76{
77 __do_free char *copy = NULL;
78 int ret;
79 char *e, *p, saved;
80
81 copy = must_copy_string(path);
82 p = copy;
83 e = copy + strlen(path);
84
85 while (p < e) {
86 while (p < e && *p == '/')
87 p++;
88
89 while (p < e && *p != '/')
90 p++;
91
92 saved = *p;
93 *p = '\0';
94
95 ret = access(copy, X_OK);
96 if (ret != 0) {
97 SYSERROR("Could not access %s. Please grant it x access, or add an ACL for the container " "root", copy);
98 return;
99 }
100 *p = saved;
101 }
102}
103
104static void lxc_put_nsfds(struct lxc_handler *handler)
105{
106 for (int i = 0; i < LXC_NS_MAX; i++) {
107 if (handler->nsfd[i] < 0)
108 continue;
109
110 close_prot_errno_disarm(handler->nsfd[i]);
111 }
112}
113
114static int lxc_try_preserve_ns(const int pid, const char *ns)
115{
116 int fd;
117
118 fd = lxc_preserve_ns(pid, ns);
119 if (fd < 0) {
120 if (errno != ENOENT)
121 return log_error_errno(-EINVAL,
122 errno, "Failed to preserve %s namespace",
123 ns);
124
125 return log_warn_errno(-EOPNOTSUPP,
126 errno, "Kernel does not support preserving %s namespaces",
127 ns);
128 }
129
130 return fd;
131}
132
133/* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
134 * specified in ns_clone_flags.
135 * Return true on success, false on failure.
136 */
137static bool lxc_try_preserve_namespaces(struct lxc_handler *handler,
138 int ns_clone_flags, pid_t pid)
139{
140 int i;
141
142 for (i = 0; i < LXC_NS_MAX; i++)
143 handler->nsfd[i] = -EBADF;
144
145 for (i = 0; i < LXC_NS_MAX; i++) {
146 int fd;
147
148 if ((ns_clone_flags & ns_info[i].clone_flag) == 0)
149 continue;
150
151 fd = lxc_try_preserve_ns(pid, ns_info[i].proc_name);
152 if (fd < 0) {
153 /* Do not fail to start container on kernels that do
154 * not support interacting with namespaces through
155 * /proc.
156 */
157 if (fd == -EOPNOTSUPP)
158 continue;
159
160 lxc_put_nsfds(handler);
161 return false;
162 }
163
164 handler->nsfd[i] = fd;
165 DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name,
166 handler->nsfd[i]);
167 }
168
169 return true;
170}
171
172static inline bool match_stdfds(int fd)
173{
174 return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO);
175}
176
177#ifdef HAVE_DLOG
178static bool match_dlog_fds(struct dirent *direntp)
179{
180 char path[PATH_MAX] = {0};
181 char link[PATH_MAX] = {0};
182 ssize_t linklen;
183 int ret;
184
185 ret = snprintf(path, PATH_MAX, "/proc/self/fd/%s", direntp->d_name);
186 if (ret < 0 || ret >= PATH_MAX)
187 return log_error(false, "Failed to create file descriptor name");
188
189 linklen = readlink(path, link, PATH_MAX);
190 if (linklen < 0)
191 return log_error(false, "Failed to read link path - \"%s\"", path);
192 else if (linklen >= PATH_MAX)
193 return log_error(false, "The name of link path is too long - \"%s\"", path);
194
195 if (strcmp(link, "/dev/log_main") == 0 ||
196 strcmp(link, "/dev/log_system") == 0 ||
197 strcmp(link, "/dev/log_radio") == 0)
198 return true;
199
200 return false;
201}
202#endif
203
204int lxc_check_inherited(struct lxc_conf *conf, bool closeall,
205 int *fds_to_ignore, size_t len_fds)
206{
207 int fd, fddir;
208 size_t i;
209 DIR *dir;
210 struct dirent *direntp;
211
212 if (conf && conf->close_all_fds)
213 closeall = true;
214
215restart:
216 dir = opendir("/proc/self/fd");
217 if (!dir)
218 return log_warn(-1, "Failed to open directory");
219
220 fddir = dirfd(dir);
221
222 while ((direntp = readdir(dir))) {
223 int ret;
224 struct lxc_list *cur;
225 bool matched = false;
226
227 if (strcmp(direntp->d_name, ".") == 0)
228 continue;
229
230 if (strcmp(direntp->d_name, "..") == 0)
231 continue;
232
233 ret = lxc_safe_int(direntp->d_name, &fd);
234 if (ret < 0) {
235 INFO("Could not parse file descriptor for \"%s\"", direntp->d_name);
236 continue;
237 }
238
239 for (i = 0; i < len_fds; i++)
240 if (fds_to_ignore[i] == fd)
241 break;
242
243 if (fd == fddir || fd == lxc_log_fd ||
244 (i < len_fds && fd == fds_to_ignore[i]))
245 continue;
246
247 /* Keep state clients that wait on reboots. */
248 if (conf) {
249 lxc_list_for_each(cur, &conf->state_clients) {
250 struct lxc_state_client *client = cur->elem;
251
252 if (client->clientfd != fd)
253 continue;
254
255 matched = true;
256 break;
257 }
258 }
259
260 if (matched)
261 continue;
262
263 if (current_config && fd == current_config->logfd)
264 continue;
265
266 if (match_stdfds(fd))
267 continue;
268
269#ifdef HAVE_DLOG
270 if (match_dlog_fds(direntp))
271 continue;
272
273#endif
274 if (closeall) {
275 close(fd);
276 closedir(dir);
277 INFO("Closed inherited fd %d", fd);
278 goto restart;
279 }
280 WARN("Inherited fd %d", fd);
281 }
282
283 /* Only enable syslog at this point to avoid the above logging function
284 * to open a new fd and make the check_inherited function enter an
285 * infinite loop.
286 */
287 lxc_log_enable_syslog();
288
289 closedir(dir); /* cannot fail */
290 return 0;
291}
292
293static int setup_signal_fd(sigset_t *oldmask)
294{
295 int ret;
296 sigset_t mask;
297 const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH};
298
299 /* Block everything except serious error signals. */
300 ret = sigfillset(&mask);
301 if (ret < 0)
302 return -EBADF;
303
304 for (int sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) {
305 ret = sigdelset(&mask, signals[sig]);
306 if (ret < 0)
307 return -EBADF;
308 }
309
310 ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask);
311 if (ret < 0)
312 return log_error_errno(-EBADF, errno,
313 "Failed to set signal mask");
314
315 ret = signalfd(-1, &mask, SFD_CLOEXEC);
316 if (ret < 0)
317 return log_error_errno(-EBADF,
318 errno, "Failed to create signal file descriptor");
319
320 TRACE("Created signal file descriptor %d", ret);
321
322 return ret;
323}
324
325static int signal_handler(int fd, uint32_t events, void *data,
326 struct lxc_epoll_descr *descr)
327{
328 int ret;
329 siginfo_t info;
330 struct signalfd_siginfo siginfo;
331 struct lxc_handler *hdlr = data;
332
333 ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo));
334 if (ret < 0)
335 return log_error(LXC_MAINLOOP_ERROR, "Failed to read signal info from signal file descriptor %d", fd);
336
337 if (ret != sizeof(siginfo))
338 return log_error(-EINVAL, "Unexpected size for struct signalfd_siginfo");
339
340 /* Check whether init is running. */
341 info.si_pid = 0;
342 ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG);
343 if (ret == 0 && info.si_pid == hdlr->pid)
344 hdlr->init_died = true;
345
346 /* Try to figure out a reasonable exit status to report. */
347 if (hdlr->init_died) {
348 switch (info.si_code) {
349 case CLD_EXITED:
350 hdlr->exit_status = info.si_status << 8;
351 break;
352 case CLD_KILLED:
353 case CLD_DUMPED:
354 case CLD_STOPPED:
355 hdlr->exit_status = info.si_status << 8 | 0x7f;
356 break;
357 case CLD_CONTINUED:
358 /* Huh? The waitid() told us it's dead *and* continued? */
359 WARN("Init %d dead and continued?", hdlr->pid);
360 hdlr->exit_status = 1;
361 break;
362 default:
363 ERROR("Unknown si_code: %d", info.si_code);
364 hdlr->exit_status = 1;
365 }
366 }
367
368 if (siginfo.ssi_signo == SIGHUP) {
369 if (hdlr->pidfd >= 0)
370 lxc_raw_pidfd_send_signal(hdlr->pidfd, SIGTERM, NULL, 0);
371 else
372 kill(hdlr->pid, SIGTERM);
373 INFO("Killing %d since terminal hung up", hdlr->pid);
374 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
375 : LXC_MAINLOOP_CONTINUE;
376 }
377
378 if (siginfo.ssi_signo != SIGCHLD) {
379 if (hdlr->pidfd >= 0)
380 lxc_raw_pidfd_send_signal(hdlr->pidfd,
381 siginfo.ssi_signo, NULL, 0);
382 else
383 kill(hdlr->pid, siginfo.ssi_signo);
384 INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid);
385 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
386 : LXC_MAINLOOP_CONTINUE;
387 }
388
389 /* More robustness, protect ourself from a SIGCHLD sent
390 * by a process different from the container init.
391 */
392 if (siginfo.ssi_pid != hdlr->pid) {
393 NOTICE("Received %d from pid %d instead of container init %d",
394 siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid);
395 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
396 : LXC_MAINLOOP_CONTINUE;
397 }
398
399 if (siginfo.ssi_code == CLD_STOPPED) {
400 INFO("Container init process was stopped");
401 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
402 : LXC_MAINLOOP_CONTINUE;
403 }
404
405 if (siginfo.ssi_code == CLD_CONTINUED) {
406 INFO("Container init process was continued");
407 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
408 : LXC_MAINLOOP_CONTINUE;
409 }
410
411 return log_debug(LXC_MAINLOOP_CLOSE, "Container init process %d exited", hdlr->pid);
412}
413
414int lxc_serve_state_clients(const char *name, struct lxc_handler *handler,
415 lxc_state_t state)
416{
417 size_t retlen;
418 ssize_t ret;
419 struct lxc_list *cur, *next;
420 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
421
422 if (state == THAWED)
423 handler->state = RUNNING;
424 else
425 handler->state = state;
426
427 TRACE("Set container state to %s", lxc_state2str(state));
428
429 if (lxc_list_empty(&handler->conf->state_clients))
430 return log_trace(0, "No state clients registered");
431
432 retlen = strlcpy(msg.name, name, sizeof(msg.name));
433 if (retlen >= sizeof(msg.name))
434 return -E2BIG;
435
436 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
437 struct lxc_state_client *client = cur->elem;
438
439 if (client->states[state] == 0) {
440 TRACE("State %s not registered for state client %d",
441 lxc_state2str(state), client->clientfd);
442 continue;
443 }
444
445 TRACE("Sending state %s to state client %d",
446 lxc_state2str(state), client->clientfd);
447
448 ret = lxc_send_nointr(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL);
449 if (ret <= 0)
450 SYSERROR("Failed to send message to client");
451
452 /* kick client from list */
453 lxc_list_del(cur);
454 close(client->clientfd);
455 free(cur->elem);
456 free(cur);
457 }
458
459 return 0;
460}
461
462static int lxc_serve_state_socket_pair(const char *name,
463 struct lxc_handler *handler,
464 lxc_state_t state)
465{
466 ssize_t ret;
467
468 if (!handler->daemonize ||
469 handler->state_socket_pair[1] < 0 ||
470 state == STARTING)
471 return 0;
472
473 /* Close read end of the socket pair. */
474 close_prot_errno_disarm(handler->state_socket_pair[0]);
475
476again:
477 ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1],
478 &(int){state}, sizeof(int));
479 if (ret < 0) {
480 SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]);
481
482 if (errno == EINTR)
483 goto again;
484
485 return -1;
486 }
487
488 if (ret != sizeof(int))
489 return log_error(-1, "Message too long : %d", handler->state_socket_pair[1]);
490
491 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state),
492 handler->state_socket_pair[1]);
493
494 /* Close write end of the socket pair. */
495 close_prot_errno_disarm(handler->state_socket_pair[1]);
496
497 return 0;
498}
499
500int lxc_set_state(const char *name, struct lxc_handler *handler,
501 lxc_state_t state)
502{
503 int ret;
504
505 ret = lxc_serve_state_socket_pair(name, handler, state);
506 if (ret < 0)
507 return log_error(-1, "Failed to synchronize via anonymous pair of unix sockets");
508
509 ret = lxc_serve_state_clients(name, handler, state);
510 if (ret < 0)
511 return -1;
512
513 /* This function will try to connect to the legacy lxc-monitord state
514 * server and only exists for backwards compatibility.
515 */
516 lxc_monitor_send_state(name, state, handler->lxcpath);
517
518 return 0;
519}
520
521int lxc_poll(const char *name, struct lxc_handler *handler)
522{
523 int ret;
524 bool has_console = true;
525 struct lxc_epoll_descr descr, descr_console;
526
527 if (handler->conf->console.path &&
528 strcmp(handler->conf->console.path, "none") == 0)
529 has_console = false;
530
531 ret = lxc_mainloop_open(&descr);
532 if (ret < 0) {
533 ERROR("Failed to create mainloop");
534 goto out_sigfd;
535 }
536
537 if (has_console) {
538 ret = lxc_mainloop_open(&descr_console);
539 if (ret < 0) {
540 ERROR("Failed to create console mainloop");
541 goto out_mainloop;
542 }
543 }
544
545 ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler);
546 if (ret < 0) {
547 ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd);
548 goto out_mainloop_console;
549 }
550
551 ret = lxc_seccomp_setup_proxy(&handler->conf->seccomp, &descr, handler);
552 if (ret < 0) {
553 ERROR("Failed to setup seccomp proxy");
554 goto out_mainloop_console;
555 }
556
557 if (has_console) {
558 struct lxc_terminal *console = &handler->conf->console;
559
560 ret = lxc_terminal_mainloop_add(&descr, console);
561 if (ret < 0) {
562 ERROR("Failed to add console handlers to mainloop");
563 goto out_mainloop_console;
564 }
565
566 ret = lxc_terminal_mainloop_add(&descr_console, console);
567 if (ret < 0) {
568 ERROR("Failed to add console handlers to console mainloop");
569 goto out_mainloop_console;
570 }
571
572 handler->conf->console.descr = &descr;
573 }
574
575 ret = lxc_cmd_mainloop_add(name, &descr, handler);
576 if (ret < 0) {
577 ERROR("Failed to add command handler to mainloop");
578 goto out_mainloop_console;
579 }
580
581 TRACE("Mainloop is ready");
582
583 ret = lxc_mainloop(&descr, -1);
584 close_prot_errno_disarm(descr.epfd);
585 if (ret < 0 || !handler->init_died)
586 goto out_mainloop_console;
587
588 if (has_console)
589 ret = lxc_mainloop(&descr_console, 0);
590
591out_mainloop_console:
592 if (has_console) {
593 lxc_mainloop_close(&descr_console);
594 TRACE("Closed console mainloop");
595 }
596
597out_mainloop:
598 lxc_mainloop_close(&descr);
599 TRACE("Closed mainloop");
600
601out_sigfd:
602 TRACE("Closed signal file descriptor %d", handler->sigfd);
603 close_prot_errno_disarm(handler->sigfd);
604
605 return ret;
606}
607
608void lxc_zero_handler(struct lxc_handler *handler)
609{
610 memset(handler, 0, sizeof(struct lxc_handler));
611
612 handler->state = STOPPED;
613
614 handler->pinfd = -EBADF;
615
616 handler->pidfd = -EBADF;
617
618 handler->sigfd = -EBADF;
619
620 for (int i = 0; i < LXC_NS_MAX; i++)
621 handler->nsfd[i] = -EBADF;
622
623 handler->data_sock[0] = -EBADF;
624 handler->data_sock[1] = -EBADF;
625
626 handler->state_socket_pair[0] = -EBADF;
627 handler->state_socket_pair[1] = -EBADF;
628
629 handler->sync_sock[0] = -EBADF;
630 handler->sync_sock[1] = -EBADF;
631}
632
633void lxc_free_handler(struct lxc_handler *handler)
634{
635 close_prot_errno_disarm(handler->pinfd);
636 close_prot_errno_disarm(handler->pidfd);
637 close_prot_errno_disarm(handler->sigfd);
638 lxc_put_nsfds(handler);
639 if (handler->conf && handler->conf->reboot == REBOOT_NONE)
640 close_prot_errno_disarm(handler->conf->maincmd_fd);
641 close_prot_errno_disarm(handler->monitor_status_fd);
642 close_prot_errno_disarm(handler->state_socket_pair[0]);
643 close_prot_errno_disarm(handler->state_socket_pair[1]);
644 cgroup_exit(handler->cgroup_ops);
645 handler->conf = NULL;
646 free_disarm(handler);
647}
648
649struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf,
650 const char *lxcpath, bool daemonize)
651{
652 int ret;
653 struct lxc_handler *handler;
654
655 handler = malloc(sizeof(*handler));
656 if (!handler)
657 return NULL;
658
659 memset(handler, 0, sizeof(*handler));
660
661 /* Note that am_guest_unpriv() checks the effective uid. We
662 * probably don't care if we are real root only if we are running
663 * as root so this should be fine.
664 */
665 handler->am_root = !am_guest_unpriv();
666 handler->conf = conf;
667 handler->lxcpath = lxcpath;
668 handler->init_died = false;
669 handler->data_sock[0] = -EBADF;
670 handler->data_sock[1] = -EBADF;
671 handler->monitor_status_fd = -EBADF;
672 handler->pinfd = -EBADF;
673 handler->pidfd = -EBADF;
674 handler->sigfd = -EBADF;
675 handler->state_socket_pair[0] = -EBADF;
676 handler->state_socket_pair[1] = -EBADF;
677 if (handler->conf->reboot == REBOOT_NONE)
678 lxc_list_init(&handler->conf->state_clients);
679
680 for (int i = 0; i < LXC_NS_MAX; i++)
681 handler->nsfd[i] = -EBADF;
682
683 handler->name = name;
684 if (daemonize)
685 handler->transient_pid = lxc_raw_getpid();
686 else
687 handler->transient_pid = -1;
688
689 if (daemonize && handler->conf->reboot == REBOOT_NONE) {
690 /* Create socketpair() to synchronize on daemonized startup.
691 * When the container reboots we don't need to synchronize
692 * again currently so don't open another socketpair().
693 */
694 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
695 handler->state_socket_pair);
696 if (ret < 0) {
697 ERROR("Failed to create anonymous pair of unix sockets");
698 goto on_error;
699 }
700
701 TRACE("Created anonymous pair {%d,%d} of unix sockets",
702 handler->state_socket_pair[0],
703 handler->state_socket_pair[1]);
704 }
705
706 if (handler->conf->reboot == REBOOT_NONE) {
707 handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command");
708 if (handler->conf->maincmd_fd < 0) {
709 ERROR("Failed to set up command socket");
710 goto on_error;
711 }
712 }
713
714 TRACE("Unix domain socket %d for command server is ready",
715 handler->conf->maincmd_fd);
716
717 return handler;
718
719on_error:
720 lxc_free_handler(handler);
721
722 return NULL;
723}
724
725int lxc_init(const char *name, struct lxc_handler *handler)
726{
727 __do_close int status_fd = -EBADF;
728 int ret;
729 const char *loglevel;
730 struct lxc_conf *conf = handler->conf;
731
732 handler->monitor_pid = lxc_raw_getpid();
733 status_fd = open("/proc/self/status", O_RDONLY | O_CLOEXEC);
734 if (status_fd < 0)
735 return log_error_errno(-1, errno, "Failed to open monitor status fd");
736
737 lsm_init();
738 TRACE("Initialized LSM");
739
740 ret = lxc_read_seccomp_config(conf);
741 if (ret < 0)
742 return log_error(-1, "Failed loading seccomp policy");
743 TRACE("Read seccomp policy");
744
745 /* Begin by setting the state to STARTING. */
746 ret = lxc_set_state(name, handler, STARTING);
747 if (ret < 0)
748 return log_error(-1, "Failed to set state to \"%s\"", lxc_state2str(STARTING));
749 TRACE("Set container state to \"STARTING\"");
750
751 /* Start of environment variable setup for hooks. */
752 ret = setenv("LXC_NAME", name, 1);
753 if (ret < 0)
754 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name);
755
756 if (conf->rcfile) {
757 ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1);
758 if (ret < 0)
759 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s", conf->rcfile);
760 }
761
762 if (conf->rootfs.mount) {
763 ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1);
764 if (ret < 0)
765 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount);
766 }
767
768 if (conf->rootfs.path) {
769 ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1);
770 if (ret < 0)
771 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s", conf->rootfs.path);
772 }
773
774 if (conf->console.path) {
775 ret = setenv("LXC_CONSOLE", conf->console.path, 1);
776 if (ret < 0)
777 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s", conf->console.path);
778 }
779
780 if (conf->console.log_path) {
781 ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1);
782 if (ret < 0)
783 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s", conf->console.log_path);
784 }
785
786 if (cgns_supported()) {
787 ret = setenv("LXC_CGNS_AWARE", "1", 1);
788 if (ret < 0)
789 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1");
790 }
791
792 loglevel = lxc_log_priority_to_string(lxc_log_get_level());
793 ret = setenv("LXC_LOG_LEVEL", loglevel, 1);
794 if (ret < 0)
795 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", loglevel);
796
797 if (conf->hooks_version == 0)
798 ret = setenv("LXC_HOOK_VERSION", "0", 1);
799 else
800 ret = setenv("LXC_HOOK_VERSION", "1", 1);
801 if (ret < 0)
802 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version);
803 /* End of environment variable setup for hooks. */
804
805 TRACE("Set environment variables");
806
807 ret = run_lxc_hooks(name, "pre-start", conf, NULL);
808 if (ret < 0)
809 return log_error(-1, "Failed to run lxc.hook.pre-start for container \"%s\"", name);
810 TRACE("Ran pre-start hooks");
811
812 /* The signal fd has to be created before forking otherwise if the child
813 * process exits before we setup the signal fd, the event will be lost
814 * and the command will be stuck.
815 */
816 handler->sigfd = setup_signal_fd(&handler->oldmask);
817 if (handler->sigfd < 0)
818 return log_error(-1, "Failed to setup SIGCHLD fd handler.");
819 TRACE("Set up signal fd");
820
821 /* Do this after setting up signals since it might unblock SIGWINCH. */
822 ret = lxc_terminal_setup(conf);
823 if (ret < 0) {
824 ERROR("Failed to create console");
825 goto out_restore_sigmask;
826 }
827 TRACE("Created console");
828
829 ret = lxc_terminal_map_ids(conf, &conf->console);
830 if (ret < 0) {
831 ERROR("Failed to chown console");
832 goto out_delete_terminal;
833 }
834 TRACE("Chowned console");
835
836 handler->cgroup_ops = cgroup_init(handler->conf);
837 if (!handler->cgroup_ops) {
838 ERROR("Failed to initialize cgroup driver");
839 goto out_delete_terminal;
840 }
841 TRACE("Initialized cgroup driver");
842
843 ret = lsm_process_prepare(conf, handler->lxcpath);
844 if (ret < 0) {
845 ERROR("Failed to initialize LSM");
846 goto out_delete_terminal;
847 }
848 TRACE("Initialized LSM");
849
850 INFO("Container \"%s\" is initialized", name);
851 handler->monitor_status_fd = move_fd(status_fd);
852 return 0;
853
854out_delete_terminal:
855 lxc_terminal_delete(&handler->conf->console);
856
857out_restore_sigmask:
858 (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
859
860 return -1;
861}
862
863void lxc_fini(struct lxc_handler *handler)
864{
865 int ret;
866 pid_t self;
867 struct lxc_list *cur, *next;
868 char *namespaces[LXC_NS_MAX + 1];
869 size_t namespace_count = 0;
870 const char *name = handler->name;
871 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
872
873 /* The STOPPING state is there for future cleanup code which can take
874 * awhile.
875 */
876 lxc_set_state(name, handler, STOPPING);
877
878 self = lxc_raw_getpid();
879 for (int i = 0; i < LXC_NS_MAX; i++) {
880 if (handler->nsfd[i] < 0)
881 continue;
882
883 if (handler->conf->hooks_version == 0)
884 ret = asprintf(&namespaces[namespace_count],
885 "%s:/proc/%d/fd/%d", ns_info[i].proc_name,
886 self, handler->nsfd[i]);
887 else
888 ret = asprintf(&namespaces[namespace_count],
889 "/proc/%d/fd/%d", self, handler->nsfd[i]);
890 if (ret < 0) {
891 SYSERROR("Failed to allocate memory");
892 break;
893 }
894
895 if (handler->conf->hooks_version == 0) {
896 namespace_count++;
897 continue;
898 }
899
900 ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1);
901 if (ret < 0)
902 SYSERROR("Failed to set environment variable %s=%s",
903 ns_info[i].env_name, namespaces[namespace_count]);
904 else
905 TRACE("Set environment variable %s=%s",
906 ns_info[i].env_name, namespaces[namespace_count]);
907
908 namespace_count++;
909 }
910 namespaces[namespace_count] = NULL;
911
912 if (handler->conf->reboot > REBOOT_NONE) {
913 ret = setenv("LXC_TARGET", "reboot", 1);
914 if (ret < 0)
915 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot");
916 }
917
918 if (handler->conf->reboot == REBOOT_NONE) {
919 ret = setenv("LXC_TARGET", "stop", 1);
920 if (ret < 0)
921 SYSERROR("Failed to set environment variable: LXC_TARGET=stop");
922 }
923
924 if (handler->conf->hooks_version == 0)
925 ret = run_lxc_hooks(name, "stop", handler->conf, namespaces);
926 else
927 ret = run_lxc_hooks(name, "stop", handler->conf, NULL);
928 if (ret < 0)
929 ERROR("Failed to run \"lxc.hook.stop\" hook");
930
931 while (namespace_count--)
932 free(namespaces[namespace_count]);
933
934 lsm_process_cleanup(handler->conf, handler->lxcpath);
935
936 if (cgroup_ops) {
937 cgroup_ops->payload_destroy(cgroup_ops, handler);
938 cgroup_ops->monitor_destroy(cgroup_ops, handler);
939 }
940
941 if (handler->conf->reboot == REBOOT_NONE) {
942 /* For all new state clients simply close the command socket.
943 * This will inform all state clients that the container is
944 * STOPPED and also prevents a race between a open()/close() on
945 * the command socket causing a new process to get ECONNREFUSED
946 * because we haven't yet closed the command socket.
947 */
948 close_prot_errno_disarm(handler->conf->maincmd_fd);
949 TRACE("Closed command socket");
950
951 /* This function will try to connect to the legacy lxc-monitord
952 * state server and only exists for backwards compatibility.
953 */
954 lxc_monitor_send_state(name, STOPPED, handler->lxcpath);
955
956 /* The command socket is closed so no one can acces the command
957 * socket anymore so there's no need to lock it.
958 */
959 handler->state = STOPPED;
960 TRACE("Set container state to \"STOPPED\"");
961 } else {
962 lxc_set_state(name, handler, STOPPED);
963 TRACE("Set container state to \"STOPPED\"");
964 }
965
966 /* Avoid lingering namespace references. */
967 lxc_put_nsfds(handler);
968
969 ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL);
970 if (ret < 0) {
971 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name);
972 if (handler->conf->reboot > REBOOT_NONE) {
973 WARN("Container will be stopped instead of rebooted");
974 handler->conf->reboot = REBOOT_NONE;
975
976 ret = setenv("LXC_TARGET", "stop", 1);
977 if (ret < 0)
978 WARN("Failed to set environment variable: LXC_TARGET=stop");
979 }
980 }
981
982 /* Reset mask set by setup_signal_fd. */
983 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
984 if (ret < 0)
985 SYSWARN("Failed to restore signal mask");
986
987 lxc_terminal_delete(&handler->conf->console);
988 lxc_delete_tty(&handler->conf->ttys);
989
990 /* The command socket is now closed, no more state clients can register
991 * themselves from now on. So free the list of state clients.
992 */
993 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
994 struct lxc_state_client *client = cur->elem;
995
996 /* Keep state clients that want to be notified about reboots. */
997 if ((handler->conf->reboot > REBOOT_NONE) &&
998 (client->states[RUNNING] == 2))
999 continue;
1000
1001 /* close state client socket */
1002 lxc_list_del(cur);
1003 close(client->clientfd);
1004 free(cur->elem);
1005 free(cur);
1006 }
1007
1008 if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ)
1009 lxc_destroy_container_on_signal(handler, name);
1010
1011 lxc_free_handler(handler);
1012}
1013
1014void lxc_abort(struct lxc_handler *handler)
1015{
1016 int ret = 0;
1017 int status;
1018
1019 lxc_set_state(handler->name, handler, ABORTING);
1020
1021 if (handler->pidfd >= 0) {
1022 ret = lxc_raw_pidfd_send_signal(handler->pidfd, SIGKILL, NULL, 0);
1023 if (ret)
1024 SYSWARN("Failed to send SIGKILL via pidfd %d for process %d",
1025 handler->pidfd, handler->pid);
1026 }
1027
1028 if ((!ret || errno != ESRCH) && handler->pid > 0)
1029 if (kill(handler->pid, SIGKILL))
1030 SYSWARN("Failed to send SIGKILL to %d", handler->pid);
1031
1032 do {
1033 ret = waitpid(-1, &status, 0);
1034 } while (ret > 0);
1035}
1036
1037static int do_start(void *data)
1038{
1039 struct lxc_handler *handler = data;
1040 __lxc_unused __do_close int data_sock0 = handler->data_sock[0],
1041 data_sock1 = handler->data_sock[1];
1042 __do_close int status_fd = -EBADF;
1043 int ret;
1044 uid_t new_uid;
1045 gid_t new_gid;
1046 struct lxc_list *iterator;
1047 uid_t nsuid = 0;
1048 gid_t nsgid = 0;
1049 int devnull_fd = -1;
1050
1051 lxc_sync_fini_parent(handler);
1052
1053 if (lxc_abstract_unix_recv_fds(data_sock1, &status_fd, 1, NULL, 0) < 0) {
1054 ERROR("Failed to receive status file descriptor to child process");
1055 goto out_warn_father;
1056 }
1057
1058 /* This prctl must be before the synchro, so if the parent dies before
1059 * we set the parent death signal, we will detect its death with the
1060 * synchro right after, otherwise we have a window where the parent can
1061 * exit before we set the pdeath signal leading to a unsupervized
1062 * container.
1063 */
1064 ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd);
1065 if (ret < 0) {
1066 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1067 goto out_warn_father;
1068 }
1069
1070 ret = lxc_ambient_caps_up();
1071 if (ret < 0) {
1072 ERROR("Failed to raise ambient capabilities");
1073 goto out_warn_father;
1074 }
1075
1076 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
1077 if (ret < 0) {
1078 SYSERROR("Failed to set signal mask");
1079 goto out_warn_father;
1080 }
1081
1082 /* Don't leak the pinfd to the container. */
1083 close_prot_errno_disarm(handler->pinfd);
1084
1085 ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP);
1086 if (ret < 0)
1087 goto out_warn_father;
1088
1089 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1090 * https://github.com/lxc/lxd/issues/1978.
1091 */
1092 if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) ==
1093 (CLONE_NEWNET | CLONE_NEWUSER)) {
1094 ret = unshare(CLONE_NEWNET);
1095 if (ret < 0) {
1096 SYSERROR("Failed to unshare CLONE_NEWNET");
1097 goto out_warn_father;
1098 }
1099 INFO("Unshared CLONE_NEWNET");
1100 }
1101
1102 /* Tell the parent task it can begin to configure the container and wait
1103 * for it to finish.
1104 */
1105 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE);
1106 if (ret < 0)
1107 goto out_error;
1108
1109 if (handler->ns_clone_flags & CLONE_NEWNET) {
1110 ret = lxc_network_recv_from_parent(handler);
1111 if (ret < 0) {
1112 ERROR("Failed to receive veth names from parent");
1113 goto out_warn_father;
1114 }
1115 }
1116
1117 /* If we are in a new user namespace, become root there to have
1118 * privilege over our namespace.
1119 */
1120 if (!lxc_list_empty(&handler->conf->id_map)) {
1121 if (!handler->conf->root_nsuid_map)
1122 nsuid = handler->conf->init_uid;
1123
1124 if (!handler->conf->root_nsgid_map)
1125 nsgid = handler->conf->init_gid;
1126
1127 /* Drop groups only after we switched to a valid gid in the new
1128 * user namespace.
1129 */
1130 if (!lxc_setgroups(0, NULL) &&
1131 (handler->am_root || errno != EPERM))
1132 goto out_warn_father;
1133
1134 if (!lxc_switch_uid_gid(nsuid, nsgid))
1135 goto out_warn_father;
1136
1137 ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0),
1138 prctl_arg(0), prctl_arg(0));
1139 if (ret < 0)
1140 goto out_warn_father;
1141
1142 /* set{g,u}id() clears deathsignal */
1143 ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd);
1144 if (ret < 0) {
1145 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1146 goto out_warn_father;
1147 }
1148 }
1149
1150 ret = access(handler->lxcpath, X_OK);
1151 if (ret != 0) {
1152 print_top_failing_dir(handler->lxcpath);
1153 goto out_warn_father;
1154 }
1155
1156 /* In order to checkpoint restore, we need to have everything in the
1157 * same mount namespace. However, some containers may not have a
1158 * reasonable /dev (in particular, they may not have /dev/null), so we
1159 * can't set init's std fds to /dev/null by opening it from inside the
1160 * container.
1161 *
1162 * If that's the case, fall back to using the host's /dev/null. This
1163 * means that migration won't work, but at least we won't spew output
1164 * where it isn't wanted.
1165 */
1166 if (handler->daemonize && !handler->conf->autodev) {
1167 char path[PATH_MAX];
1168
1169 ret = snprintf(path, sizeof(path), "%s/dev/null",
1170 handler->conf->rootfs.mount);
1171 if (ret < 0 || ret >= sizeof(path))
1172 goto out_warn_father;
1173
1174 ret = access(path, F_OK);
1175 if (ret != 0) {
1176 devnull_fd = open_devnull();
1177
1178 if (devnull_fd < 0)
1179 goto out_warn_father;
1180 WARN("Using /dev/null from the host for container init's standard file descriptors. Migration will not work");
1181 }
1182 }
1183
1184 /* Ask father to setup cgroups and wait for him to finish. */
1185 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP);
1186 if (ret < 0)
1187 goto out_error;
1188
1189 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1190 * earlier we end up with a wrong view of /proc/self/cgroup. For
1191 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1192 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1193 * /proc/self/cgroup would show us:
1194 *
1195 * 8:cpuset:/lxc/c
1196 *
1197 * whereas it should actually show
1198 *
1199 * 8:cpuset:/
1200 */
1201 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
1202 ret = unshare(CLONE_NEWCGROUP);
1203 if (ret < 0) {
1204 if (errno != EINVAL) {
1205 SYSERROR("Failed to unshare CLONE_NEWCGROUP");
1206 goto out_warn_father;
1207 }
1208
1209 handler->ns_clone_flags &= ~CLONE_NEWCGROUP;
1210 SYSINFO("Kernel does not support CLONE_NEWCGROUP");
1211 } else {
1212 INFO("Unshared CLONE_NEWCGROUP");
1213 }
1214 }
1215
1216 /* Add the requested environment variables to the current environment to
1217 * allow them to be used by the various hooks, such as the start hook
1218 * below.
1219 */
1220 lxc_list_for_each(iterator, &handler->conf->environment) {
1221 ret = putenv((char *)iterator->elem);
1222 if (ret < 0) {
1223 SYSERROR("Failed to set environment variable: %s",
1224 (char *)iterator->elem);
1225 goto out_warn_father;
1226 }
1227 }
1228
1229 /* Setup the container, ip, names, utsname, ... */
1230 ret = lxc_setup(handler);
1231 if (ret < 0) {
1232 ERROR("Failed to setup container \"%s\"", handler->name);
1233 goto out_warn_father;
1234 }
1235
1236 /* Set the label to change to when we exec(2) the container's init. */
1237 ret = lsm_process_label_set(NULL, handler->conf, true);
1238 if (ret < 0)
1239 goto out_warn_father;
1240
1241 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1242 * before we aren't allowed anymore.
1243 */
1244 if (handler->conf->no_new_privs) {
1245 ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0),
1246 prctl_arg(0), prctl_arg(0));
1247 if (ret < 0) {
1248 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges");
1249 goto out_warn_father;
1250 }
1251 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges");
1252 }
1253
1254 /* Some init's such as busybox will set sane tty settings on stdin,
1255 * stdout, stderr which it thinks is the console. We already set them
1256 * the way we wanted on the real terminal, and we want init to do its
1257 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1258 * make sure that that pty is stdin,stdout,stderr.
1259 */
1260 if (handler->conf->console.slave >= 0) {
1261 if (handler->daemonize || !handler->conf->is_execute)
1262 ret = set_stdfds(handler->conf->console.slave);
1263 else
1264 ret = lxc_terminal_set_stdfds(handler->conf->console.slave);
1265 if (ret < 0) {
1266 ERROR("Failed to redirect std{in,out,err} to pty file descriptor %d",
1267 handler->conf->console.slave);
1268 goto out_warn_father;
1269 }
1270 }
1271
1272 /* If we mounted a temporary proc, then unmount it now. */
1273 tmp_proc_unmount(handler->conf);
1274
1275 ret = lxc_seccomp_load(handler->conf);
1276 if (ret < 0)
1277 goto out_warn_father;
1278
1279 ret = lxc_seccomp_send_notifier_fd(&handler->conf->seccomp, data_sock0);
1280 if (ret < 0) {
1281 SYSERROR("Failed to send seccomp notify fd to parent");
1282 goto out_warn_father;
1283 }
1284
1285 ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL);
1286 if (ret < 0) {
1287 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1288 handler->name);
1289 goto out_warn_father;
1290 }
1291
1292 close_prot_errno_disarm(handler->sigfd);
1293
1294 if (handler->conf->console.slave < 0 && handler->daemonize) {
1295 if (devnull_fd < 0) {
1296 devnull_fd = open_devnull();
1297 if (devnull_fd < 0)
1298 goto out_warn_father;
1299 }
1300
1301 ret = set_stdfds(devnull_fd);
1302 if (ret < 0) {
1303 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1304 goto out_warn_father;
1305 }
1306 }
1307
1308 close_prot_errno_disarm(devnull_fd);
1309
1310 setsid();
1311
1312 if (handler->conf->init_cwd) {
1313 ret = chdir(handler->conf->init_cwd);
1314 if (ret < 0) {
1315 SYSERROR("Could not change directory to \"%s\"",
1316 handler->conf->init_cwd);
1317 goto out_warn_father;
1318 }
1319 }
1320
1321 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS);
1322 if (ret < 0)
1323 goto out_warn_father;
1324
1325 /* Reset the environment variables the user requested in a clear
1326 * environment.
1327 */
1328 ret = clearenv();
1329 /* Don't error out though. */
1330 if (ret < 0)
1331 SYSERROR("Failed to clear environment.");
1332
1333 lxc_list_for_each(iterator, &handler->conf->environment) {
1334 ret = putenv((char *)iterator->elem);
1335 if (ret < 0) {
1336 SYSERROR("Failed to set environment variable: %s",
1337 (char *)iterator->elem);
1338 goto out_warn_father;
1339 }
1340 }
1341
1342 ret = putenv("container=lxc");
1343 if (ret < 0) {
1344 SYSERROR("Failed to set environment variable: container=lxc");
1345 goto out_warn_father;
1346 }
1347
1348 if (handler->conf->ttys.tty_names) {
1349 ret = putenv(handler->conf->ttys.tty_names);
1350 if (ret < 0) {
1351 SYSERROR("Failed to set environment variable for container ptys");
1352 goto out_warn_father;
1353 }
1354 }
1355
1356 /* The container has been setup. We can now switch to an unprivileged
1357 * uid/gid.
1358 */
1359 new_uid = handler->conf->init_uid;
1360 new_gid = handler->conf->init_gid;
1361
1362 /* Avoid unnecessary syscalls. */
1363 if (new_uid == nsuid)
1364 new_uid = LXC_INVALID_UID;
1365
1366 if (new_gid == nsgid)
1367 new_gid = LXC_INVALID_GID;
1368
1369 /* If we are in a new user namespace we already dropped all groups when
1370 * we switched to root in the new user namespace further above. Only
1371 * drop groups if we can, so ensure that we have necessary privilege.
1372 */
1373 if (lxc_list_empty(&handler->conf->id_map))
1374 #if HAVE_LIBCAP
1375 if (lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE))
1376 #endif
1377 if (!lxc_setgroups(0, NULL))
1378 goto out_warn_father;
1379
1380 if (!lxc_switch_uid_gid(new_uid, new_gid))
1381 goto out_warn_father;
1382
1383 ret = lxc_ambient_caps_down();
1384 if (ret < 0) {
1385 ERROR("Failed to clear ambient capabilities");
1386 goto out_warn_father;
1387 }
1388
1389 if (handler->conf->monitor_signal_pdeath != SIGKILL) {
1390 ret = lxc_set_death_signal(handler->conf->monitor_signal_pdeath,
1391 handler->monitor_pid, status_fd);
1392 if (ret < 0) {
1393 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d",
1394 handler->conf->monitor_signal_pdeath);
1395 goto out_warn_father;
1396 }
1397 }
1398
1399 /* After this call, we are in error because this ops should not return
1400 * as it execs.
1401 */
1402 handler->ops->start(handler, handler->data);
1403
1404out_warn_father:
1405 /* We want the parent to know something went wrong, so we return a
1406 * special error code.
1407 */
1408 lxc_sync_wake_parent(handler, LXC_SYNC_ERROR);
1409
1410out_error:
1411 close_prot_errno_disarm(devnull_fd);
1412
1413 return -1;
1414}
1415
1416static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
1417{
1418 int i;
1419 struct lxc_terminal_info *tty;
1420 int ret = -1;
1421 int sock = handler->data_sock[1];
1422 struct lxc_conf *conf = handler->conf;
1423 struct lxc_tty_info *ttys = &conf->ttys;
1424
1425 if (!conf->ttys.max)
1426 return 0;
1427
1428 ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
1429 if (!ttys->tty)
1430 return -1;
1431
1432 for (i = 0; i < conf->ttys.max; i++) {
1433 int ttyfds[2];
1434
1435 ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0);
1436 if (ret < 0)
1437 break;
1438
1439 tty = &ttys->tty[i];
1440 tty->busy = -1;
1441 tty->master = ttyfds[0];
1442 tty->slave = ttyfds[1];
1443 TRACE("Received pty with master fd %d and slave fd %d from child", tty->master, tty->slave);
1444 }
1445
1446 if (ret < 0)
1447 SYSERROR("Failed to receive %zu ttys from child", ttys->max);
1448 else
1449 TRACE("Received %zu ttys from child", ttys->max);
1450
1451 return ret;
1452}
1453
1454int resolve_clone_flags(struct lxc_handler *handler)
1455{
1456 int i;
1457 struct lxc_conf *conf = handler->conf;
1458
1459 for (i = 0; i < LXC_NS_MAX; i++) {
1460 if (conf->ns_keep) {
1461 if (!(conf->ns_keep & ns_info[i].clone_flag))
1462 handler->ns_clone_flags |= ns_info[i].clone_flag;
1463 } else if (conf->ns_clone) {
1464 if ((conf->ns_clone & ns_info[i].clone_flag))
1465 handler->ns_clone_flags |= ns_info[i].clone_flag;
1466 } else {
1467 if (i == LXC_NS_USER && lxc_list_empty(&handler->conf->id_map))
1468 continue;
1469
1470 if (i == LXC_NS_NET && lxc_requests_empty_network(handler))
1471 continue;
1472
1473 if (i == LXC_NS_CGROUP && !cgns_supported())
1474 continue;
1475
1476 handler->ns_clone_flags |= ns_info[i].clone_flag;
1477 }
1478
1479 if (!conf->ns_share[i])
1480 continue;
1481
1482 handler->ns_clone_flags &= ~ns_info[i].clone_flag;
1483 TRACE("Sharing %s namespace", ns_info[i].proc_name);
1484 }
1485
1486 return 0;
1487}
1488
1489/* Note that this function is used with clone(CLONE_VM). Some glibc versions
1490 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1491 * But since the memory between parent and child is shared on CLONE_VM this
1492 * would invalidate the getpid() cache that glibc used to maintain and so
1493 * getpid() in the child would return the parent's pid. This is all fixed in
1494 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1495 * not reset anymore.
1496 * However, if for whatever reason you - dear committer - somehow need to get the
1497 * pid of the dummy intermediate process for do_share_ns() you need to call
1498 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1499 * will be fine.
1500 */
1501static inline int do_share_ns(void *arg)
1502{
1503 int i, flags, ret;
1504 struct lxc_handler *handler = arg;
1505
1506 for (i = 0; i < LXC_NS_MAX; i++) {
1507 if (handler->nsfd[i] < 0)
1508 continue;
1509
1510 ret = setns(handler->nsfd[i], 0);
1511 if (ret < 0) {
1512 /*
1513 * Note that joining a user and/or mount namespace
1514 * requires the process is not multithreaded otherwise
1515 * setns() will fail here.
1516 */
1517 SYSERROR("Failed to inherit %s namespace",
1518 ns_info[i].proc_name);
1519 return -1;
1520 }
1521
1522 DEBUG("Inherited %s namespace", ns_info[i].proc_name);
1523 }
1524
1525 flags = handler->ns_on_clone_flags;
1526 flags |= CLONE_PARENT;
1527 handler->pid = lxc_raw_clone_cb(do_start, handler, CLONE_PIDFD | flags,
1528 &handler->pidfd);
1529 if (handler->pid < 0)
1530 return -1;
1531
1532 return 0;
1533}
1534
1535/* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1536 * exec()s the requested container binary.
1537 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1538 * right here should be double checked if they'd pose a security risk. (For
1539 * example, any {u}mount() operations performed here will be reflected on the
1540 * host!)
1541 */
1542static int lxc_spawn(struct lxc_handler *handler)
1543{
1544 __do_close int data_sock0 = -EBADF, data_sock1 = -EBADF;
1545 int i, ret;
1546 char pidstr[20];
1547 bool wants_to_map_ids;
1548 struct lxc_list *id_map;
1549 const char *name = handler->name;
1550 const char *lxcpath = handler->lxcpath;
1551 bool share_ns = false;
1552 struct lxc_conf *conf = handler->conf;
1553 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
1554
1555 id_map = &conf->id_map;
1556 wants_to_map_ids = !lxc_list_empty(id_map);
1557
1558 for (i = 0; i < LXC_NS_MAX; i++) {
1559 if (!conf->ns_share[i])
1560 continue;
1561
1562 handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name);
1563 if (handler->nsfd[i] < 0)
1564 return -1;
1565
1566 share_ns = true;
1567 }
1568
1569 ret = lxc_sync_init(handler);
1570 if (ret < 0)
1571 return -1;
1572
1573 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
1574 handler->data_sock);
1575 if (ret < 0)
1576 goto out_sync_fini;
1577 data_sock0 = handler->data_sock[0];
1578 data_sock1 = handler->data_sock[1];
1579
1580 ret = resolve_clone_flags(handler);
1581 if (ret < 0)
1582 goto out_sync_fini;
1583
1584 if (handler->ns_clone_flags & CLONE_NEWNET) {
1585 ret = lxc_find_gateway_addresses(handler);
1586 if (ret) {
1587 ERROR("Failed to find gateway addresses");
1588 goto out_sync_fini;
1589 }
1590 }
1591
1592 if (!cgroup_ops->payload_create(cgroup_ops, handler)) {
1593 ERROR("Failed creating cgroups");
1594 goto out_delete_net;
1595 }
1596
1597 /* If the rootfs is not a blockdev, prevent the container from marking
1598 * it readonly.
1599 * If the container is unprivileged then skip rootfs pinning.
1600 */
1601 if (!wants_to_map_ids) {
1602 handler->pinfd = pin_rootfs(conf->rootfs.path);
1603 if (handler->pinfd == -EBADF)
1604 INFO("Failed to pin the rootfs for container \"%s\"", handler->name);
1605 }
1606
1607 /* Create a process in a new set of namespaces. */
1608 handler->ns_on_clone_flags = handler->ns_clone_flags;
1609 if (handler->ns_clone_flags & CLONE_NEWUSER) {
1610 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1611 * clone a new user namespace first and only later unshare our
1612 * network namespace to ensure that network devices ownership is
1613 * set up correctly.
1614 */
1615 handler->ns_on_clone_flags &= ~CLONE_NEWNET;
1616 }
1617 /* The cgroup namespace gets unshare()ed not clone()ed. */
1618 handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP;
1619
1620 if (share_ns) {
1621 pid_t attacher_pid;
1622
1623 attacher_pid = lxc_clone(do_share_ns, handler,
1624 CLONE_VFORK | CLONE_VM | CLONE_FILES, NULL);
1625 if (attacher_pid < 0) {
1626 SYSERROR(LXC_CLONE_ERROR);
1627 goto out_delete_net;
1628 }
1629
1630 ret = wait_for_pid(attacher_pid);
1631 if (ret < 0) {
1632 SYSERROR("Intermediate process failed");
1633 goto out_delete_net;
1634 }
1635 } else {
1636 handler->pid = lxc_raw_clone_cb(do_start, handler,
1637 CLONE_PIDFD | handler->ns_on_clone_flags,
1638 &handler->pidfd);
1639 }
1640 if (handler->pid < 0) {
1641 SYSERROR(LXC_CLONE_ERROR);
1642 goto out_delete_net;
1643 }
1644 TRACE("Cloned child process %d", handler->pid);
1645
1646 /* Verify that we can actually make use of pidfds. */
1647 if (!lxc_can_use_pidfd(handler->pidfd))
1648 close_prot_errno_disarm(handler->pidfd);
1649
1650 ret = snprintf(pidstr, 20, "%d", handler->pid);
1651 if (ret < 0 || ret >= 20)
1652 goto out_delete_net;
1653
1654 ret = setenv("LXC_PID", pidstr, 1);
1655 if (ret < 0)
1656 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr);
1657
1658 for (i = 0; i < LXC_NS_MAX; i++)
1659 if (handler->ns_on_clone_flags & ns_info[i].clone_flag)
1660 INFO("Cloned %s", ns_info[i].flag_name);
1661
1662 if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) {
1663 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1664 goto out_delete_net;
1665 }
1666
1667 lxc_sync_fini_child(handler);
1668
1669 if (lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->monitor_status_fd, 1, NULL, 0) < 0) {
1670 ERROR("Failed to send status file descriptor to child process");
1671 goto out_delete_net;
1672 }
1673 close_prot_errno_disarm(handler->monitor_status_fd);
1674
1675 /* Map the container uids. The container became an invalid userid the
1676 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1677 * anything immediately, but allows the container to setuid(0) (0 being
1678 * mapped to something else on the host.) later to become a valid uid
1679 * again.
1680 */
1681 if (wants_to_map_ids) {
1682 if (!handler->conf->ns_share[LXC_NS_USER] &&
1683 (handler->conf->ns_keep & CLONE_NEWUSER) == 0) {
1684 ret = lxc_map_ids(id_map, handler->pid);
1685 if (ret < 0) {
1686 ERROR("Failed to set up id mapping.");
1687 goto out_delete_net;
1688 }
1689 }
1690 }
1691
1692 ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP);
1693 if (ret < 0)
1694 goto out_delete_net;
1695
1696 ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE);
1697 if (ret < 0)
1698 goto out_delete_net;
1699
1700 if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) {
1701 ERROR("Failed to setup cgroup limits for container \"%s\"", name);
1702 goto out_delete_net;
1703 }
1704
1705 if (!cgroup_ops->payload_enter(cgroup_ops, handler)) {
1706 goto out_delete_net;
1707 }
1708
1709 if (!cgroup_ops->payload_delegate_controllers(cgroup_ops)) {
1710 ERROR("Failed to delegate controllers to payload cgroup");
1711 goto out_delete_net;
1712 }
1713
1714 if (!cgroup_ops->setup_limits(cgroup_ops, handler)) {
1715 ERROR("Failed to setup cgroup limits for container \"%s\"", name);
1716 goto out_delete_net;
1717 }
1718
1719 if (!cgroup_ops->chown(cgroup_ops, handler->conf))
1720 goto out_delete_net;
1721
1722 /* If not done yet, we're now ready to preserve the network namespace */
1723 if (handler->nsfd[LXC_NS_NET] < 0) {
1724 ret = lxc_try_preserve_ns(handler->pid, "net");
1725 if (ret < 0) {
1726 if (ret != -EOPNOTSUPP) {
1727 SYSERROR("Failed to preserve net namespace");
1728 goto out_delete_net;
1729 }
1730 } else {
1731 handler->nsfd[LXC_NS_NET] = ret;
1732 DEBUG("Preserved net namespace via fd %d", ret);
1733 }
1734 }
1735 ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]);
1736 if (ret < 0)
1737 SYSWARN("Failed to allocate new network namespace id");
1738 else
1739 TRACE("Allocated new network namespace id");
1740
1741 /* Create the network configuration. */
1742 if (handler->ns_clone_flags & CLONE_NEWNET) {
1743 ret = lxc_create_network(handler);
1744 if (ret < 0) {
1745 ERROR("Failed to create the network");
1746 goto out_delete_net;
1747 }
1748
1749 ret = lxc_network_send_to_child(handler);
1750 if (ret < 0) {
1751 ERROR("Failed to send veth names to child");
1752 goto out_delete_net;
1753 }
1754 }
1755
1756 if (!lxc_list_empty(&conf->procs)) {
1757 ret = setup_proc_filesystem(&conf->procs, handler->pid);
1758 if (ret < 0)
1759 goto out_delete_net;
1760 }
1761
1762 /* Tell the child to continue its initialization. We'll get
1763 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1764 */
1765 ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE);
1766 if (ret < 0)
1767 goto out_delete_net;
1768
1769 if (!lxc_list_empty(&conf->limits)) {
1770 ret = setup_resource_limits(&conf->limits, handler->pid);
1771 if (ret < 0) {
1772 ERROR("Failed to setup resource limits");
1773 goto out_delete_net;
1774 }
1775 }
1776
1777 ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE);
1778 if (ret < 0)
1779 goto out_delete_net;
1780
1781 if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, true)) {
1782 ERROR("Failed to setup legacy device cgroup controller limits");
1783 goto out_delete_net;
1784 }
1785 TRACE("Set up legacy device cgroup controller limits");
1786
1787 if (!cgroup_ops->devices_activate(cgroup_ops, handler)) {
1788 ERROR("Failed to setup cgroup2 device controller limits");
1789 goto out_delete_net;
1790 }
1791 TRACE("Set up cgroup2 device controller limits");
1792
1793 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
1794 /* Now we're ready to preserve the cgroup namespace */
1795 ret = lxc_try_preserve_ns(handler->pid, "cgroup");
1796 if (ret < 0) {
1797 if (ret != -EOPNOTSUPP) {
1798 SYSERROR("Failed to preserve cgroup namespace");
1799 goto out_delete_net;
1800 }
1801 } else {
1802 handler->nsfd[LXC_NS_CGROUP] = ret;
1803 DEBUG("Preserved cgroup namespace via fd %d", ret);
1804 }
1805 }
1806
1807 cgroup_ops->payload_finalize(cgroup_ops);
1808 TRACE("Finished setting up cgroups");
1809
1810 /* Run any host-side start hooks */
1811 ret = run_lxc_hooks(name, "start-host", conf, NULL);
1812 if (ret < 0) {
1813 ERROR("Failed to run lxc.hook.start-host");
1814 goto out_delete_net;
1815 }
1816
1817 /* Tell the child to complete its initialization and wait for it to exec
1818 * or return an error. (The child will never return
1819 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1820 * lxc_sync_barrier_child to return success, or return a different
1821 * value, causing us to error out).
1822 */
1823 ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START);
1824 if (ret < 0)
1825 goto out_delete_net;
1826
1827 if (handler->ns_clone_flags & CLONE_NEWNET) {
1828 ret = lxc_network_recv_name_and_ifindex_from_child(handler);
1829 if (ret < 0) {
1830 ERROR("Failed to receive names and ifindices for network devices from child");
1831 goto out_delete_net;
1832 }
1833 }
1834
1835 /* Now all networks are created, network devices are moved into place,
1836 * and the correct names and ifindices in the respective namespaces have
1837 * been recorded. The corresponding structs have now all been filled. So
1838 * log them for debugging purposes.
1839 */
1840 lxc_log_configured_netdevs(conf);
1841
1842 /* Read tty fds allocated by child. */
1843 ret = lxc_recv_ttys_from_child(handler);
1844 if (ret < 0) {
1845 ERROR("Failed to receive tty info from child process");
1846 goto out_delete_net;
1847 }
1848
1849 ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, data_sock1);
1850 if (ret < 0) {
1851 SYSERROR("Failed to receive seccomp notify fd from child");
1852 goto out_delete_net;
1853 }
1854
1855 ret = handler->ops->post_start(handler, handler->data);
1856 if (ret < 0)
1857 goto out_abort;
1858
1859 ret = lxc_set_state(name, handler, RUNNING);
1860 if (ret < 0) {
1861 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING));
1862 goto out_abort;
1863 }
1864
1865 lxc_sync_fini(handler);
1866
1867 return 0;
1868
1869out_delete_net:
1870 if (handler->ns_clone_flags & CLONE_NEWNET)
1871 lxc_delete_network(handler);
1872
1873out_abort:
1874 lxc_abort(handler);
1875
1876out_sync_fini:
1877 lxc_sync_fini(handler);
1878 close_prot_errno_disarm(handler->pinfd);
1879
1880 return -1;
1881}
1882
1883int __lxc_start(struct lxc_handler *handler, struct lxc_operations *ops,
1884 void *data, const char *lxcpath, bool daemonize, int *error_num)
1885{
1886 int ret, status;
1887 const char *name = handler->name;
1888 struct lxc_conf *conf = handler->conf;
1889 struct cgroup_ops *cgroup_ops;
1890
1891 ret = lxc_init(name, handler);
1892 if (ret < 0) {
1893 ERROR("Failed to initialize container \"%s\"", name);
1894 goto out_abort;
1895 }
1896 handler->ops = ops;
1897 handler->data = data;
1898 handler->daemonize = daemonize;
1899 cgroup_ops = handler->cgroup_ops;
1900
1901 if (!attach_block_device(handler->conf)) {
1902 ERROR("Failed to attach block device");
1903 ret = -1;
1904 goto out_abort;
1905 }
1906
1907 if (!cgroup_ops->monitor_create(cgroup_ops, handler)) {
1908 ERROR("Failed to create monitor cgroup");
1909 ret = -1;
1910 goto out_abort;
1911 }
1912
1913 if (!cgroup_ops->monitor_enter(cgroup_ops, handler)) {
1914 ERROR("Failed to enter monitor cgroup");
1915 ret = -1;
1916 goto out_abort;
1917 }
1918
1919 if (!cgroup_ops->monitor_delegate_controllers(cgroup_ops)) {
1920 ERROR("Failed to delegate controllers to monitor cgroup");
1921 ret = -1;
1922 goto out_abort;
1923 }
1924
1925 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
1926 /* If the backing store is a device, mount it here and now. */
1927 if (rootfs_is_blockdev(conf)) {
1928 ret = unshare(CLONE_NEWNS);
1929 if (ret < 0) {
1930 ERROR("Failed to unshare CLONE_NEWNS");
1931 goto out_abort;
1932 }
1933 INFO("Unshared CLONE_NEWNS");
1934
1935 remount_all_slave();
1936 ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath);
1937 if (ret < 0) {
1938 ERROR("Error setting up rootfs mount as root before spawn");
1939 goto out_abort;
1940 }
1941 INFO("Set up container rootfs as host root");
1942 }
1943 }
1944
1945 ret = lxc_spawn(handler);
1946 if (ret < 0) {
1947 ERROR("Failed to spawn container \"%s\"", name);
1948 goto out_detach_blockdev;
1949 }
1950
1951 handler->conf->reboot = REBOOT_NONE;
1952
1953 ret = lxc_poll(name, handler);
1954 if (ret) {
1955 ERROR("LXC mainloop exited with error: %d", ret);
1956 goto out_delete_network;
1957 }
1958
1959 if (!handler->init_died && handler->pid > 0) {
1960 ERROR("Child process is not killed");
1961 ret = -1;
1962 goto out_delete_network;
1963 }
1964
1965 status = lxc_wait_for_pid_status(handler->pid);
1966 if (status < 0)
1967 SYSERROR("Failed to retrieve status for %d", handler->pid);
1968
1969 /* If the child process exited but was not signaled, it didn't call
1970 * reboot. This should mean it was an lxc-execute which simply exited.
1971 * In any case, treat it as a 'halt'.
1972 */
1973 if (WIFSIGNALED(status)) {
1974 switch(WTERMSIG(status)) {
1975 case SIGINT: /* halt */
1976 DEBUG("Container \"%s\" is halting", name);
1977 break;
1978 case SIGHUP: /* reboot */
1979 DEBUG("Container \"%s\" is rebooting", name);
1980 handler->conf->reboot = REBOOT_REQ;
1981 break;
1982 case SIGSYS: /* seccomp */
1983 DEBUG("Container \"%s\" violated its seccomp policy", name);
1984 break;
1985 default:
1986 DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status));
1987 break;
1988 }
1989 }
1990
1991 ret = lxc_restore_phys_nics_to_netns(handler);
1992 if (ret < 0)
1993 ERROR("Failed to move physical network devices back to parent network namespace");
1994
1995 close_prot_errno_disarm(handler->pinfd);
1996
1997 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
1998 lxc_error_set_and_log(handler->pid, status);
1999 if (error_num)
2000 *error_num = handler->exit_status;
2001
2002/* These are not the droids you are looking for. */
2003__private_goto1:
2004 lxc_delete_network(handler);
2005
2006__private_goto2:
2007 detach_block_device(handler->conf);
2008
2009__private_goto3:
2010 lxc_fini(handler);
2011
2012 return ret;
2013
2014/* These are the droids you are looking for. */
2015out_abort:
2016 lxc_abort(handler);
2017 goto __private_goto3;
2018
2019out_detach_blockdev:
2020 lxc_abort(handler);
2021 goto __private_goto2;
2022
2023out_delete_network:
2024 lxc_abort(handler);
2025 goto __private_goto1;
2026}
2027
2028struct start_args {
2029 char *const *argv;
2030};
2031
2032static int start(struct lxc_handler *handler, void* data)
2033{
2034 struct start_args *arg = data;
2035
2036 NOTICE("Exec'ing \"%s\"", arg->argv[0]);
2037
2038 execvp(arg->argv[0], arg->argv);
2039 SYSERROR("Failed to exec \"%s\"", arg->argv[0]);
2040 return 0;
2041}
2042
2043static int post_start(struct lxc_handler *handler, void* data)
2044{
2045 struct start_args *arg = data;
2046
2047 NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid);
2048 return 0;
2049}
2050
2051static struct lxc_operations start_ops = {
2052 .start = start,
2053 .post_start = post_start
2054};
2055
2056int lxc_start(char *const argv[], struct lxc_handler *handler,
2057 const char *lxcpath, bool daemonize, int *error_num)
2058{
2059 struct start_args start_arg = {
2060 .argv = argv,
2061 };
2062
2063 TRACE("Doing lxc_start");
2064 return __lxc_start(handler, &start_ops, &start_arg, lxcpath, daemonize, error_num);
2065}
2066
2067static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
2068 const char *name)
2069{
2070 char destroy[PATH_MAX];
2071 struct lxc_container *c;
2072 int ret = 0;
2073 bool bret = true;
2074
2075 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
2076 bret = do_destroy_container(handler);
2077 if (!bret) {
2078 ERROR("Error destroying rootfs for container \"%s\"", name);
2079 return;
2080 }
2081 }
2082 INFO("Destroyed rootfs for container \"%s\"", name);
2083
2084 ret = snprintf(destroy, PATH_MAX, "%s/%s", handler->lxcpath, name);
2085 if (ret < 0 || ret >= PATH_MAX) {
2086 ERROR("Error destroying directory for container \"%s\"", name);
2087 return;
2088 }
2089
2090 c = lxc_container_new(name, handler->lxcpath);
2091 if (c) {
2092 if (container_disk_lock(c)) {
2093 INFO("Could not update lxc_snapshots file");
2094 lxc_container_put(c);
2095 } else {
2096 mod_all_rdeps(c, false);
2097 container_disk_unlock(c);
2098 lxc_container_put(c);
2099 }
2100 }
2101
2102 if (!handler->am_root)
2103 ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper,
2104 destroy, "lxc_rmdir_onedev_wrapper");
2105 else
2106 ret = lxc_rmdir_onedev(destroy, NULL);
2107
2108 if (ret < 0) {
2109 ERROR("Error destroying directory for container \"%s\"", name);
2110 return;
2111 }
2112 INFO("Destroyed directory for container \"%s\"", name);
2113}
2114
2115static int lxc_rmdir_onedev_wrapper(void *data)
2116{
2117 char *arg = (char *) data;
2118 return lxc_rmdir_onedev(arg, NULL);
2119}
2120
2121static bool do_destroy_container(struct lxc_handler *handler)
2122{
2123 int ret;
2124
2125 if (!handler->am_root) {
2126 ret = userns_exec_full(handler->conf, storage_destroy_wrapper,
2127 handler->conf, "storage_destroy_wrapper");
2128 if (ret < 0)
2129 return false;
2130
2131 return true;
2132 }
2133
2134 return storage_destroy(handler->conf);
2135}