]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
memory_utils: adapt to new infrastructure
[mirror_lxc.git] / src / lxc / start.c
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
67 lxc_log_define(start, lxc);
68
69 extern void mod_all_rdeps(struct lxc_container *c, bool inc);
70 static bool do_destroy_container(struct lxc_handler *handler);
71 static int lxc_rmdir_onedev_wrapper(void *data);
72 static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
73 const char *name);
74
75 static 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
104 static 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
114 static 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 */
137 static 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
172 static inline bool match_stdfds(int fd)
173 {
174 return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO);
175 }
176
177 #ifdef HAVE_DLOG
178 static 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
204 int 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
215 restart:
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
293 static 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
325 static 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
414 int 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
462 static 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
476 again:
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
500 int 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
521 int 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
591 out_mainloop_console:
592 if (has_console) {
593 lxc_mainloop_close(&descr_console);
594 TRACE("Closed console mainloop");
595 }
596
597 out_mainloop:
598 lxc_mainloop_close(&descr);
599 TRACE("Closed mainloop");
600
601 out_sigfd:
602 TRACE("Closed signal file descriptor %d", handler->sigfd);
603 close_prot_errno_disarm(handler->sigfd);
604
605 return ret;
606 }
607
608 void 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
633 void 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
649 struct 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
719 on_error:
720 lxc_free_handler(handler);
721
722 return NULL;
723 }
724
725 int lxc_init(const char *name, struct lxc_handler *handler)
726 {
727 __do_close_prot_errno 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 SYSERROR("Failed to open monitor status fd");
736 goto out_close_maincmd_fd;
737 }
738
739 lsm_init();
740 TRACE("Initialized LSM");
741
742 ret = lxc_read_seccomp_config(conf);
743 if (ret < 0) {
744 ERROR("Failed loading seccomp policy");
745 goto out_close_maincmd_fd;
746 }
747 TRACE("Read seccomp policy");
748
749 /* Begin by setting the state to STARTING. */
750 ret = lxc_set_state(name, handler, STARTING);
751 if (ret < 0) {
752 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING));
753 goto out_close_maincmd_fd;
754 }
755 TRACE("Set container state to \"STARTING\"");
756
757 /* Start of environment variable setup for hooks. */
758 ret = setenv("LXC_NAME", name, 1);
759 if (ret < 0)
760 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name);
761
762 if (conf->rcfile) {
763 ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1);
764 if (ret < 0)
765 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s", conf->rcfile);
766 }
767
768 if (conf->rootfs.mount) {
769 ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1);
770 if (ret < 0)
771 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount);
772 }
773
774 if (conf->rootfs.path) {
775 ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1);
776 if (ret < 0)
777 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s", conf->rootfs.path);
778 }
779
780 if (conf->console.path) {
781 ret = setenv("LXC_CONSOLE", conf->console.path, 1);
782 if (ret < 0)
783 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s", conf->console.path);
784 }
785
786 if (conf->console.log_path) {
787 ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1);
788 if (ret < 0)
789 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s", conf->console.log_path);
790 }
791
792 if (cgns_supported()) {
793 ret = setenv("LXC_CGNS_AWARE", "1", 1);
794 if (ret < 0)
795 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1");
796 }
797
798 loglevel = lxc_log_priority_to_string(lxc_log_get_level());
799 ret = setenv("LXC_LOG_LEVEL", loglevel, 1);
800 if (ret < 0)
801 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", loglevel);
802
803 if (conf->hooks_version == 0)
804 ret = setenv("LXC_HOOK_VERSION", "0", 1);
805 else
806 ret = setenv("LXC_HOOK_VERSION", "1", 1);
807 if (ret < 0)
808 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version);
809 /* End of environment variable setup for hooks. */
810
811 TRACE("Set environment variables");
812
813 ret = run_lxc_hooks(name, "pre-start", conf, NULL);
814 if (ret < 0) {
815 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name);
816 goto out_aborting;
817 }
818 TRACE("Ran pre-start hooks");
819
820 /* The signal fd has to be created before forking otherwise if the child
821 * process exits before we setup the signal fd, the event will be lost
822 * and the command will be stuck.
823 */
824 handler->sigfd = setup_signal_fd(&handler->oldmask);
825 if (handler->sigfd < 0) {
826 ERROR("Failed to setup SIGCHLD fd handler.");
827 goto out_delete_tty;
828 }
829 TRACE("Set up signal fd");
830
831 /* Do this after setting up signals since it might unblock SIGWINCH. */
832 ret = lxc_terminal_setup(conf);
833 if (ret < 0) {
834 ERROR("Failed to create console");
835 goto out_restore_sigmask;
836 }
837 TRACE("Created console");
838
839 ret = lxc_terminal_map_ids(conf, &conf->console);
840 if (ret < 0) {
841 ERROR("Failed to chown console");
842 goto out_delete_terminal;
843 }
844 TRACE("Chowned console");
845
846 handler->cgroup_ops = cgroup_init(handler->conf);
847 if (!handler->cgroup_ops) {
848 ERROR("Failed to initialize cgroup driver");
849 goto out_delete_terminal;
850 }
851 TRACE("Initialized cgroup driver");
852
853 ret = lsm_process_prepare(conf, handler->lxcpath);
854 if (ret < 0) {
855 ERROR("Failed to initialize LSM");
856 goto out_delete_terminal;
857 }
858 TRACE("Initialized LSM");
859
860 INFO("Container \"%s\" is initialized", name);
861 handler->monitor_status_fd = move_fd(status_fd);
862 return 0;
863
864 out_delete_terminal:
865 lxc_terminal_delete(&handler->conf->console);
866
867 out_restore_sigmask:
868 (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
869
870 out_delete_tty:
871 lxc_delete_tty(&conf->ttys);
872
873 out_aborting:
874 (void)lxc_set_state(name, handler, ABORTING);
875
876 out_close_maincmd_fd:
877 close_prot_errno_disarm(conf->maincmd_fd);
878 return -1;
879 }
880
881 void lxc_fini(const char *name, struct lxc_handler *handler)
882 {
883 int ret;
884 pid_t self;
885 struct lxc_list *cur, *next;
886 char *namespaces[LXC_NS_MAX + 1];
887 size_t namespace_count = 0;
888 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
889
890 /* The STOPPING state is there for future cleanup code which can take
891 * awhile.
892 */
893 lxc_set_state(name, handler, STOPPING);
894
895 self = lxc_raw_getpid();
896 for (int i = 0; i < LXC_NS_MAX; i++) {
897 if (handler->nsfd[i] < 0)
898 continue;
899
900 if (handler->conf->hooks_version == 0)
901 ret = asprintf(&namespaces[namespace_count],
902 "%s:/proc/%d/fd/%d", ns_info[i].proc_name,
903 self, handler->nsfd[i]);
904 else
905 ret = asprintf(&namespaces[namespace_count],
906 "/proc/%d/fd/%d", self, handler->nsfd[i]);
907 if (ret < 0) {
908 SYSERROR("Failed to allocate memory");
909 break;
910 }
911
912 if (handler->conf->hooks_version == 0) {
913 namespace_count++;
914 continue;
915 }
916
917 ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1);
918 if (ret < 0)
919 SYSERROR("Failed to set environment variable %s=%s",
920 ns_info[i].env_name, namespaces[namespace_count]);
921 else
922 TRACE("Set environment variable %s=%s",
923 ns_info[i].env_name, namespaces[namespace_count]);
924
925 namespace_count++;
926 }
927 namespaces[namespace_count] = NULL;
928
929 if (handler->conf->reboot > REBOOT_NONE) {
930 ret = setenv("LXC_TARGET", "reboot", 1);
931 if (ret < 0)
932 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot");
933 }
934
935 if (handler->conf->reboot == REBOOT_NONE) {
936 ret = setenv("LXC_TARGET", "stop", 1);
937 if (ret < 0)
938 SYSERROR("Failed to set environment variable: LXC_TARGET=stop");
939 }
940
941 if (handler->conf->hooks_version == 0)
942 ret = run_lxc_hooks(name, "stop", handler->conf, namespaces);
943 else
944 ret = run_lxc_hooks(name, "stop", handler->conf, NULL);
945 if (ret < 0)
946 ERROR("Failed to run \"lxc.hook.stop\" hook");
947
948 while (namespace_count--)
949 free(namespaces[namespace_count]);
950
951 lsm_process_cleanup(handler->conf, handler->lxcpath);
952
953 if (cgroup_ops) {
954 cgroup_ops->payload_destroy(cgroup_ops, handler);
955 cgroup_ops->monitor_destroy(cgroup_ops, handler);
956 }
957
958 if (handler->conf->reboot == REBOOT_NONE) {
959 /* For all new state clients simply close the command socket.
960 * This will inform all state clients that the container is
961 * STOPPED and also prevents a race between a open()/close() on
962 * the command socket causing a new process to get ECONNREFUSED
963 * because we haven't yet closed the command socket.
964 */
965 close_prot_errno_disarm(handler->conf->maincmd_fd);
966 TRACE("Closed command socket");
967
968 /* This function will try to connect to the legacy lxc-monitord
969 * state server and only exists for backwards compatibility.
970 */
971 lxc_monitor_send_state(name, STOPPED, handler->lxcpath);
972
973 /* The command socket is closed so no one can acces the command
974 * socket anymore so there's no need to lock it.
975 */
976 handler->state = STOPPED;
977 TRACE("Set container state to \"STOPPED\"");
978 } else {
979 lxc_set_state(name, handler, STOPPED);
980 }
981
982 /* Avoid lingering namespace references. */
983 lxc_put_nsfds(handler);
984
985 ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL);
986 if (ret < 0) {
987 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name);
988 if (handler->conf->reboot > REBOOT_NONE) {
989 WARN("Container will be stopped instead of rebooted");
990 handler->conf->reboot = REBOOT_NONE;
991
992 ret = setenv("LXC_TARGET", "stop", 1);
993 if (ret < 0)
994 WARN("Failed to set environment variable: LXC_TARGET=stop");
995 }
996 }
997
998 /* Reset mask set by setup_signal_fd. */
999 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
1000 if (ret < 0)
1001 SYSWARN("Failed to restore signal mask");
1002
1003 lxc_terminal_delete(&handler->conf->console);
1004 lxc_delete_tty(&handler->conf->ttys);
1005
1006 /* The command socket is now closed, no more state clients can register
1007 * themselves from now on. So free the list of state clients.
1008 */
1009 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
1010 struct lxc_state_client *client = cur->elem;
1011
1012 /* Keep state clients that want to be notified about reboots. */
1013 if ((handler->conf->reboot > REBOOT_NONE) &&
1014 (client->states[RUNNING] == 2))
1015 continue;
1016
1017 /* close state client socket */
1018 lxc_list_del(cur);
1019 close(client->clientfd);
1020 free(cur->elem);
1021 free(cur);
1022 }
1023
1024 if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ)
1025 lxc_destroy_container_on_signal(handler, name);
1026
1027 lxc_free_handler(handler);
1028 }
1029
1030 void lxc_abort(const char *name, struct lxc_handler *handler)
1031 {
1032 int ret = 0;
1033 int status;
1034
1035 lxc_set_state(name, handler, ABORTING);
1036
1037 if (handler->pidfd >= 0) {
1038 ret = lxc_raw_pidfd_send_signal(handler->pidfd, SIGKILL, NULL, 0);
1039 if (ret)
1040 SYSWARN("Failed to send SIGKILL via pidfd %d for process %d",
1041 handler->pidfd, handler->pid);
1042 }
1043
1044 if (!ret || errno != ESRCH)
1045 if (kill(handler->pid, SIGKILL))
1046 SYSWARN("Failed to send SIGKILL to %d", handler->pid);
1047
1048 do {
1049 ret = waitpid(-1, &status, 0);
1050 } while (ret > 0);
1051 }
1052
1053 static int do_start(void *data)
1054 {
1055 struct lxc_handler *handler = data;
1056 __lxc_unused __do_close_prot_errno int data_sock0 = handler->data_sock[0],
1057 data_sock1 = handler->data_sock[1];
1058 __do_close_prot_errno int status_fd = -EBADF;
1059 int ret;
1060 uid_t new_uid;
1061 gid_t new_gid;
1062 struct lxc_list *iterator;
1063 uid_t nsuid = 0;
1064 gid_t nsgid = 0;
1065 int devnull_fd = -1;
1066
1067 lxc_sync_fini_parent(handler);
1068
1069 if (lxc_abstract_unix_recv_fds(data_sock1, &status_fd, 1, NULL, 0) < 0) {
1070 ERROR("Failed to receive status file descriptor to child process");
1071 goto out_warn_father;
1072 }
1073
1074 /* This prctl must be before the synchro, so if the parent dies before
1075 * we set the parent death signal, we will detect its death with the
1076 * synchro right after, otherwise we have a window where the parent can
1077 * exit before we set the pdeath signal leading to a unsupervized
1078 * container.
1079 */
1080 ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd);
1081 if (ret < 0) {
1082 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1083 goto out_warn_father;
1084 }
1085
1086 ret = lxc_ambient_caps_up();
1087 if (ret < 0) {
1088 ERROR("Failed to raise ambient capabilities");
1089 goto out_warn_father;
1090 }
1091
1092 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
1093 if (ret < 0) {
1094 SYSERROR("Failed to set signal mask");
1095 goto out_warn_father;
1096 }
1097
1098 /* Don't leak the pinfd to the container. */
1099 close_prot_errno_disarm(handler->pinfd);
1100
1101 ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP);
1102 if (ret < 0)
1103 goto out_warn_father;
1104
1105 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1106 * https://github.com/lxc/lxd/issues/1978.
1107 */
1108 if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) ==
1109 (CLONE_NEWNET | CLONE_NEWUSER)) {
1110 ret = unshare(CLONE_NEWNET);
1111 if (ret < 0) {
1112 SYSERROR("Failed to unshare CLONE_NEWNET");
1113 goto out_warn_father;
1114 }
1115 INFO("Unshared CLONE_NEWNET");
1116 }
1117
1118 /* Tell the parent task it can begin to configure the container and wait
1119 * for it to finish.
1120 */
1121 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE);
1122 if (ret < 0)
1123 goto out_error;
1124
1125 if (handler->ns_clone_flags & CLONE_NEWNET) {
1126 ret = lxc_network_recv_from_parent(handler);
1127 if (ret < 0) {
1128 ERROR("Failed to receive veth names from parent");
1129 goto out_warn_father;
1130 }
1131 }
1132
1133 /* If we are in a new user namespace, become root there to have
1134 * privilege over our namespace.
1135 */
1136 if (!lxc_list_empty(&handler->conf->id_map)) {
1137 if (!handler->conf->root_nsuid_map)
1138 nsuid = handler->conf->init_uid;
1139
1140 if (!handler->conf->root_nsgid_map)
1141 nsgid = handler->conf->init_gid;
1142
1143 /* Drop groups only after we switched to a valid gid in the new
1144 * user namespace.
1145 */
1146 if (!lxc_setgroups(0, NULL) &&
1147 (handler->am_root || errno != EPERM))
1148 goto out_warn_father;
1149
1150 if (!lxc_switch_uid_gid(nsuid, nsgid))
1151 goto out_warn_father;
1152
1153 ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0),
1154 prctl_arg(0), prctl_arg(0));
1155 if (ret < 0)
1156 goto out_warn_father;
1157
1158 /* set{g,u}id() clears deathsignal */
1159 ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd);
1160 if (ret < 0) {
1161 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1162 goto out_warn_father;
1163 }
1164 }
1165
1166 ret = access(handler->lxcpath, X_OK);
1167 if (ret != 0) {
1168 print_top_failing_dir(handler->lxcpath);
1169 goto out_warn_father;
1170 }
1171
1172 /* In order to checkpoint restore, we need to have everything in the
1173 * same mount namespace. However, some containers may not have a
1174 * reasonable /dev (in particular, they may not have /dev/null), so we
1175 * can't set init's std fds to /dev/null by opening it from inside the
1176 * container.
1177 *
1178 * If that's the case, fall back to using the host's /dev/null. This
1179 * means that migration won't work, but at least we won't spew output
1180 * where it isn't wanted.
1181 */
1182 if (handler->daemonize && !handler->conf->autodev) {
1183 char path[PATH_MAX];
1184
1185 ret = snprintf(path, sizeof(path), "%s/dev/null",
1186 handler->conf->rootfs.mount);
1187 if (ret < 0 || ret >= sizeof(path))
1188 goto out_warn_father;
1189
1190 ret = access(path, F_OK);
1191 if (ret != 0) {
1192 devnull_fd = open_devnull();
1193
1194 if (devnull_fd < 0)
1195 goto out_warn_father;
1196 WARN("Using /dev/null from the host for container init's standard file descriptors. Migration will not work");
1197 }
1198 }
1199
1200 /* Ask father to setup cgroups and wait for him to finish. */
1201 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP);
1202 if (ret < 0)
1203 goto out_error;
1204
1205 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1206 * earlier we end up with a wrong view of /proc/self/cgroup. For
1207 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1208 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1209 * /proc/self/cgroup would show us:
1210 *
1211 * 8:cpuset:/lxc/c
1212 *
1213 * whereas it should actually show
1214 *
1215 * 8:cpuset:/
1216 */
1217 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
1218 ret = unshare(CLONE_NEWCGROUP);
1219 if (ret < 0) {
1220 if (errno != EINVAL) {
1221 SYSERROR("Failed to unshare CLONE_NEWCGROUP");
1222 goto out_warn_father;
1223 }
1224
1225 handler->ns_clone_flags &= ~CLONE_NEWCGROUP;
1226 SYSINFO("Kernel does not support CLONE_NEWCGROUP");
1227 } else {
1228 INFO("Unshared CLONE_NEWCGROUP");
1229 }
1230 }
1231
1232 /* Add the requested environment variables to the current environment to
1233 * allow them to be used by the various hooks, such as the start hook
1234 * below.
1235 */
1236 lxc_list_for_each(iterator, &handler->conf->environment) {
1237 ret = putenv((char *)iterator->elem);
1238 if (ret < 0) {
1239 SYSERROR("Failed to set environment variable: %s",
1240 (char *)iterator->elem);
1241 goto out_warn_father;
1242 }
1243 }
1244
1245 /* Setup the container, ip, names, utsname, ... */
1246 ret = lxc_setup(handler);
1247 if (ret < 0) {
1248 ERROR("Failed to setup container \"%s\"", handler->name);
1249 goto out_warn_father;
1250 }
1251
1252 /* Set the label to change to when we exec(2) the container's init. */
1253 ret = lsm_process_label_set(NULL, handler->conf, true);
1254 if (ret < 0)
1255 goto out_warn_father;
1256
1257 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1258 * before we aren't allowed anymore.
1259 */
1260 if (handler->conf->no_new_privs) {
1261 ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0),
1262 prctl_arg(0), prctl_arg(0));
1263 if (ret < 0) {
1264 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges");
1265 goto out_warn_father;
1266 }
1267 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges");
1268 }
1269
1270 /* Some init's such as busybox will set sane tty settings on stdin,
1271 * stdout, stderr which it thinks is the console. We already set them
1272 * the way we wanted on the real terminal, and we want init to do its
1273 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1274 * make sure that that pty is stdin,stdout,stderr.
1275 */
1276 if (handler->conf->console.slave >= 0) {
1277 if (handler->daemonize || !handler->conf->is_execute)
1278 ret = set_stdfds(handler->conf->console.slave);
1279 else
1280 ret = lxc_terminal_set_stdfds(handler->conf->console.slave);
1281 if (ret < 0) {
1282 ERROR("Failed to redirect std{in,out,err} to pty file descriptor %d",
1283 handler->conf->console.slave);
1284 goto out_warn_father;
1285 }
1286 }
1287
1288 /* If we mounted a temporary proc, then unmount it now. */
1289 tmp_proc_unmount(handler->conf);
1290
1291 ret = lxc_seccomp_load(handler->conf);
1292 if (ret < 0)
1293 goto out_warn_father;
1294
1295 ret = lxc_seccomp_send_notifier_fd(&handler->conf->seccomp, data_sock0);
1296 if (ret < 0) {
1297 SYSERROR("Failed to send seccomp notify fd to parent");
1298 goto out_warn_father;
1299 }
1300
1301 ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL);
1302 if (ret < 0) {
1303 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1304 handler->name);
1305 goto out_warn_father;
1306 }
1307
1308 close_prot_errno_disarm(handler->sigfd);
1309
1310 if (handler->conf->console.slave < 0 && handler->daemonize) {
1311 if (devnull_fd < 0) {
1312 devnull_fd = open_devnull();
1313 if (devnull_fd < 0)
1314 goto out_warn_father;
1315 }
1316
1317 ret = set_stdfds(devnull_fd);
1318 if (ret < 0) {
1319 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1320 goto out_warn_father;
1321 }
1322 }
1323
1324 close_prot_errno_disarm(devnull_fd);
1325
1326 setsid();
1327
1328 if (handler->conf->init_cwd) {
1329 ret = chdir(handler->conf->init_cwd);
1330 if (ret < 0) {
1331 SYSERROR("Could not change directory to \"%s\"",
1332 handler->conf->init_cwd);
1333 goto out_warn_father;
1334 }
1335 }
1336
1337 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS);
1338 if (ret < 0)
1339 goto out_warn_father;
1340
1341 /* Reset the environment variables the user requested in a clear
1342 * environment.
1343 */
1344 ret = clearenv();
1345 /* Don't error out though. */
1346 if (ret < 0)
1347 SYSERROR("Failed to clear environment.");
1348
1349 lxc_list_for_each(iterator, &handler->conf->environment) {
1350 ret = putenv((char *)iterator->elem);
1351 if (ret < 0) {
1352 SYSERROR("Failed to set environment variable: %s",
1353 (char *)iterator->elem);
1354 goto out_warn_father;
1355 }
1356 }
1357
1358 ret = putenv("container=lxc");
1359 if (ret < 0) {
1360 SYSERROR("Failed to set environment variable: container=lxc");
1361 goto out_warn_father;
1362 }
1363
1364 if (handler->conf->ttys.tty_names) {
1365 ret = putenv(handler->conf->ttys.tty_names);
1366 if (ret < 0) {
1367 SYSERROR("Failed to set environment variable for container ptys");
1368 goto out_warn_father;
1369 }
1370 }
1371
1372 /* The container has been setup. We can now switch to an unprivileged
1373 * uid/gid.
1374 */
1375 new_uid = handler->conf->init_uid;
1376 new_gid = handler->conf->init_gid;
1377
1378 /* Avoid unnecessary syscalls. */
1379 if (new_uid == nsuid)
1380 new_uid = LXC_INVALID_UID;
1381
1382 if (new_gid == nsgid)
1383 new_gid = LXC_INVALID_GID;
1384
1385 /* If we are in a new user namespace we already dropped all groups when
1386 * we switched to root in the new user namespace further above. Only
1387 * drop groups if we can, so ensure that we have necessary privilege.
1388 */
1389 if (lxc_list_empty(&handler->conf->id_map))
1390 #if HAVE_LIBCAP
1391 if (lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE))
1392 #endif
1393 if (!lxc_setgroups(0, NULL))
1394 goto out_warn_father;
1395
1396 if (!lxc_switch_uid_gid(new_uid, new_gid))
1397 goto out_warn_father;
1398
1399 ret = lxc_ambient_caps_down();
1400 if (ret < 0) {
1401 ERROR("Failed to clear ambient capabilities");
1402 goto out_warn_father;
1403 }
1404
1405 if (handler->conf->monitor_signal_pdeath != SIGKILL) {
1406 ret = lxc_set_death_signal(handler->conf->monitor_signal_pdeath,
1407 handler->monitor_pid, status_fd);
1408 if (ret < 0) {
1409 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d",
1410 handler->conf->monitor_signal_pdeath);
1411 goto out_warn_father;
1412 }
1413 }
1414
1415 /* After this call, we are in error because this ops should not return
1416 * as it execs.
1417 */
1418 handler->ops->start(handler, handler->data);
1419
1420 out_warn_father:
1421 /* We want the parent to know something went wrong, so we return a
1422 * special error code.
1423 */
1424 lxc_sync_wake_parent(handler, LXC_SYNC_ERROR);
1425
1426 out_error:
1427 close_prot_errno_disarm(devnull_fd);
1428
1429 return -1;
1430 }
1431
1432 static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
1433 {
1434 int i;
1435 struct lxc_terminal_info *tty;
1436 int ret = -1;
1437 int sock = handler->data_sock[1];
1438 struct lxc_conf *conf = handler->conf;
1439 struct lxc_tty_info *ttys = &conf->ttys;
1440
1441 if (!conf->ttys.max)
1442 return 0;
1443
1444 ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
1445 if (!ttys->tty)
1446 return -1;
1447
1448 for (i = 0; i < conf->ttys.max; i++) {
1449 int ttyfds[2];
1450
1451 ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0);
1452 if (ret < 0)
1453 break;
1454
1455 tty = &ttys->tty[i];
1456 tty->busy = -1;
1457 tty->master = ttyfds[0];
1458 tty->slave = ttyfds[1];
1459 TRACE("Received pty with master fd %d and slave fd %d from child", tty->master, tty->slave);
1460 }
1461
1462 if (ret < 0)
1463 SYSERROR("Failed to receive %zu ttys from child", ttys->max);
1464 else
1465 TRACE("Received %zu ttys from child", ttys->max);
1466
1467 return ret;
1468 }
1469
1470 int resolve_clone_flags(struct lxc_handler *handler)
1471 {
1472 int i;
1473 struct lxc_conf *conf = handler->conf;
1474
1475 for (i = 0; i < LXC_NS_MAX; i++) {
1476 if (conf->ns_keep) {
1477 if (!(conf->ns_keep & ns_info[i].clone_flag))
1478 handler->ns_clone_flags |= ns_info[i].clone_flag;
1479 } else if (conf->ns_clone) {
1480 if ((conf->ns_clone & ns_info[i].clone_flag))
1481 handler->ns_clone_flags |= ns_info[i].clone_flag;
1482 } else {
1483 if (i == LXC_NS_USER && lxc_list_empty(&handler->conf->id_map))
1484 continue;
1485
1486 if (i == LXC_NS_NET && lxc_requests_empty_network(handler))
1487 continue;
1488
1489 if (i == LXC_NS_CGROUP && !cgns_supported())
1490 continue;
1491
1492 handler->ns_clone_flags |= ns_info[i].clone_flag;
1493 }
1494
1495 if (!conf->ns_share[i])
1496 continue;
1497
1498 handler->ns_clone_flags &= ~ns_info[i].clone_flag;
1499 TRACE("Sharing %s namespace", ns_info[i].proc_name);
1500 }
1501
1502 return 0;
1503 }
1504
1505 /* Note that this function is used with clone(CLONE_VM). Some glibc versions
1506 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1507 * But since the memory between parent and child is shared on CLONE_VM this
1508 * would invalidate the getpid() cache that glibc used to maintain and so
1509 * getpid() in the child would return the parent's pid. This is all fixed in
1510 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1511 * not reset anymore.
1512 * However, if for whatever reason you - dear committer - somehow need to get the
1513 * pid of the dummy intermediate process for do_share_ns() you need to call
1514 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1515 * will be fine.
1516 */
1517 static inline int do_share_ns(void *arg)
1518 {
1519 int i, flags, ret;
1520 struct lxc_handler *handler = arg;
1521
1522 for (i = 0; i < LXC_NS_MAX; i++) {
1523 if (handler->nsfd[i] < 0)
1524 continue;
1525
1526 ret = setns(handler->nsfd[i], 0);
1527 if (ret < 0) {
1528 /*
1529 * Note that joining a user and/or mount namespace
1530 * requires the process is not multithreaded otherwise
1531 * setns() will fail here.
1532 */
1533 SYSERROR("Failed to inherit %s namespace",
1534 ns_info[i].proc_name);
1535 return -1;
1536 }
1537
1538 DEBUG("Inherited %s namespace", ns_info[i].proc_name);
1539 }
1540
1541 flags = handler->ns_on_clone_flags;
1542 flags |= CLONE_PARENT;
1543 handler->pid = lxc_raw_clone_cb(do_start, handler, CLONE_PIDFD | flags,
1544 &handler->pidfd);
1545 if (handler->pid < 0)
1546 return -1;
1547
1548 return 0;
1549 }
1550
1551 /* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1552 * exec()s the requested container binary.
1553 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1554 * right here should be double checked if they'd pose a security risk. (For
1555 * example, any {u}mount() operations performed here will be reflected on the
1556 * host!)
1557 */
1558 static int lxc_spawn(struct lxc_handler *handler)
1559 {
1560 __do_close_prot_errno int data_sock0 = -EBADF, data_sock1 = -EBADF;
1561 int i, ret;
1562 char pidstr[20];
1563 bool wants_to_map_ids;
1564 struct lxc_list *id_map;
1565 const char *name = handler->name;
1566 const char *lxcpath = handler->lxcpath;
1567 bool share_ns = false;
1568 struct lxc_conf *conf = handler->conf;
1569 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
1570
1571 id_map = &conf->id_map;
1572 wants_to_map_ids = !lxc_list_empty(id_map);
1573
1574 for (i = 0; i < LXC_NS_MAX; i++) {
1575 if (!conf->ns_share[i])
1576 continue;
1577
1578 handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name);
1579 if (handler->nsfd[i] < 0)
1580 return -1;
1581
1582 share_ns = true;
1583 }
1584
1585 ret = lxc_sync_init(handler);
1586 if (ret < 0)
1587 return -1;
1588
1589 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
1590 handler->data_sock);
1591 if (ret < 0)
1592 goto out_sync_fini;
1593 data_sock0 = handler->data_sock[0];
1594 data_sock1 = handler->data_sock[1];
1595
1596 ret = resolve_clone_flags(handler);
1597 if (ret < 0)
1598 goto out_sync_fini;
1599
1600 if (handler->ns_clone_flags & CLONE_NEWNET) {
1601 ret = lxc_find_gateway_addresses(handler);
1602 if (ret) {
1603 ERROR("Failed to find gateway addresses");
1604 goto out_sync_fini;
1605 }
1606 }
1607
1608 if (!cgroup_ops->payload_create(cgroup_ops, handler)) {
1609 ERROR("Failed creating cgroups");
1610 goto out_delete_net;
1611 }
1612
1613 /* If the rootfs is not a blockdev, prevent the container from marking
1614 * it readonly.
1615 * If the container is unprivileged then skip rootfs pinning.
1616 */
1617 if (!wants_to_map_ids) {
1618 handler->pinfd = pin_rootfs(conf->rootfs.path);
1619 if (handler->pinfd == -EBADF)
1620 INFO("Failed to pin the rootfs for container \"%s\"", handler->name);
1621 }
1622
1623 /* Create a process in a new set of namespaces. */
1624 handler->ns_on_clone_flags = handler->ns_clone_flags;
1625 if (handler->ns_clone_flags & CLONE_NEWUSER) {
1626 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1627 * clone a new user namespace first and only later unshare our
1628 * network namespace to ensure that network devices ownership is
1629 * set up correctly.
1630 */
1631 handler->ns_on_clone_flags &= ~CLONE_NEWNET;
1632 }
1633 /* The cgroup namespace gets unshare()ed not clone()ed. */
1634 handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP;
1635
1636 if (share_ns) {
1637 pid_t attacher_pid;
1638
1639 attacher_pid = lxc_clone(do_share_ns, handler,
1640 CLONE_VFORK | CLONE_VM | CLONE_FILES, NULL);
1641 if (attacher_pid < 0) {
1642 SYSERROR(LXC_CLONE_ERROR);
1643 goto out_delete_net;
1644 }
1645
1646 ret = wait_for_pid(attacher_pid);
1647 if (ret < 0) {
1648 SYSERROR("Intermediate process failed");
1649 goto out_delete_net;
1650 }
1651 } else {
1652 handler->pid = lxc_raw_clone_cb(do_start, handler,
1653 CLONE_PIDFD | handler->ns_on_clone_flags,
1654 &handler->pidfd);
1655 }
1656 if (handler->pid < 0) {
1657 SYSERROR(LXC_CLONE_ERROR);
1658 goto out_delete_net;
1659 }
1660 TRACE("Cloned child process %d", handler->pid);
1661
1662 /* Verify that we can actually make use of pidfds. */
1663 if (!lxc_can_use_pidfd(handler->pidfd))
1664 close_prot_errno_disarm(handler->pidfd);
1665
1666 ret = snprintf(pidstr, 20, "%d", handler->pid);
1667 if (ret < 0 || ret >= 20)
1668 goto out_delete_net;
1669
1670 ret = setenv("LXC_PID", pidstr, 1);
1671 if (ret < 0)
1672 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr);
1673
1674 for (i = 0; i < LXC_NS_MAX; i++)
1675 if (handler->ns_on_clone_flags & ns_info[i].clone_flag)
1676 INFO("Cloned %s", ns_info[i].flag_name);
1677
1678 if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) {
1679 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1680 goto out_delete_net;
1681 }
1682
1683 lxc_sync_fini_child(handler);
1684
1685 if (lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->monitor_status_fd, 1, NULL, 0) < 0) {
1686 ERROR("Failed to send status file descriptor to child process");
1687 goto out_delete_net;
1688 }
1689 close_prot_errno_disarm(handler->monitor_status_fd);
1690
1691 /* Map the container uids. The container became an invalid userid the
1692 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1693 * anything immediately, but allows the container to setuid(0) (0 being
1694 * mapped to something else on the host.) later to become a valid uid
1695 * again.
1696 */
1697 if (wants_to_map_ids) {
1698 if (!handler->conf->ns_share[LXC_NS_USER] &&
1699 (handler->conf->ns_keep & CLONE_NEWUSER) == 0) {
1700 ret = lxc_map_ids(id_map, handler->pid);
1701 if (ret < 0) {
1702 ERROR("Failed to set up id mapping.");
1703 goto out_delete_net;
1704 }
1705 }
1706 }
1707
1708 ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP);
1709 if (ret < 0)
1710 goto out_delete_net;
1711
1712 ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE);
1713 if (ret < 0)
1714 goto out_delete_net;
1715
1716 if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) {
1717 ERROR("Failed to setup cgroup limits for container \"%s\"", name);
1718 goto out_delete_net;
1719 }
1720
1721 if (!cgroup_ops->payload_enter(cgroup_ops, handler)) {
1722 goto out_delete_net;
1723 }
1724
1725 if (!cgroup_ops->payload_delegate_controllers(cgroup_ops)) {
1726 ERROR("Failed to delegate controllers to payload cgroup");
1727 goto out_delete_net;
1728 }
1729
1730 if (!cgroup_ops->setup_limits(cgroup_ops, handler)) {
1731 ERROR("Failed to setup cgroup limits for container \"%s\"", name);
1732 goto out_delete_net;
1733 }
1734
1735 if (!cgroup_ops->chown(cgroup_ops, handler->conf))
1736 goto out_delete_net;
1737
1738 /* If not done yet, we're now ready to preserve the network namespace */
1739 if (handler->nsfd[LXC_NS_NET] < 0) {
1740 ret = lxc_try_preserve_ns(handler->pid, "net");
1741 if (ret < 0) {
1742 if (ret != -EOPNOTSUPP) {
1743 SYSERROR("Failed to preserve net namespace");
1744 goto out_delete_net;
1745 }
1746 } else {
1747 handler->nsfd[LXC_NS_NET] = ret;
1748 DEBUG("Preserved net namespace via fd %d", ret);
1749 }
1750 }
1751 ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]);
1752 if (ret < 0)
1753 SYSWARN("Failed to allocate new network namespace id");
1754 else
1755 TRACE("Allocated new network namespace id");
1756
1757 /* Create the network configuration. */
1758 if (handler->ns_clone_flags & CLONE_NEWNET) {
1759 ret = lxc_create_network(handler);
1760 if (ret < 0) {
1761 ERROR("Failed to create the network");
1762 goto out_delete_net;
1763 }
1764
1765 ret = lxc_network_send_to_child(handler);
1766 if (ret < 0) {
1767 ERROR("Failed to send veth names to child");
1768 goto out_delete_net;
1769 }
1770 }
1771
1772 if (!lxc_list_empty(&conf->procs)) {
1773 ret = setup_proc_filesystem(&conf->procs, handler->pid);
1774 if (ret < 0)
1775 goto out_delete_net;
1776 }
1777
1778 /* Tell the child to continue its initialization. We'll get
1779 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
1780 */
1781 ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE);
1782 if (ret < 0)
1783 goto out_delete_net;
1784
1785 if (!lxc_list_empty(&conf->limits)) {
1786 ret = setup_resource_limits(&conf->limits, handler->pid);
1787 if (ret < 0) {
1788 ERROR("Failed to setup resource limits");
1789 goto out_delete_net;
1790 }
1791 }
1792
1793 ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE);
1794 if (ret < 0)
1795 goto out_delete_net;
1796
1797 if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, true)) {
1798 ERROR("Failed to setup legacy device cgroup controller limits");
1799 goto out_delete_net;
1800 }
1801 TRACE("Set up legacy device cgroup controller limits");
1802
1803 if (!cgroup_ops->devices_activate(cgroup_ops, handler)) {
1804 ERROR("Failed to setup cgroup2 device controller limits");
1805 goto out_delete_net;
1806 }
1807 TRACE("Set up cgroup2 device controller limits");
1808
1809 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
1810 /* Now we're ready to preserve the cgroup namespace */
1811 ret = lxc_try_preserve_ns(handler->pid, "cgroup");
1812 if (ret < 0) {
1813 if (ret != -EOPNOTSUPP) {
1814 SYSERROR("Failed to preserve cgroup namespace");
1815 goto out_delete_net;
1816 }
1817 } else {
1818 handler->nsfd[LXC_NS_CGROUP] = ret;
1819 DEBUG("Preserved cgroup namespace via fd %d", ret);
1820 }
1821 }
1822
1823 cgroup_ops->payload_finalize(cgroup_ops);
1824 TRACE("Finished setting up cgroups");
1825
1826 /* Run any host-side start hooks */
1827 ret = run_lxc_hooks(name, "start-host", conf, NULL);
1828 if (ret < 0) {
1829 ERROR("Failed to run lxc.hook.start-host");
1830 goto out_delete_net;
1831 }
1832
1833 /* Tell the child to complete its initialization and wait for it to exec
1834 * or return an error. (The child will never return
1835 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
1836 * lxc_sync_barrier_child to return success, or return a different
1837 * value, causing us to error out).
1838 */
1839 ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START);
1840 if (ret < 0)
1841 goto out_delete_net;
1842
1843 if (handler->ns_clone_flags & CLONE_NEWNET) {
1844 ret = lxc_network_recv_name_and_ifindex_from_child(handler);
1845 if (ret < 0) {
1846 ERROR("Failed to receive names and ifindices for network devices from child");
1847 goto out_delete_net;
1848 }
1849 }
1850
1851 /* Now all networks are created, network devices are moved into place,
1852 * and the correct names and ifindices in the respective namespaces have
1853 * been recorded. The corresponding structs have now all been filled. So
1854 * log them for debugging purposes.
1855 */
1856 lxc_log_configured_netdevs(conf);
1857
1858 /* Read tty fds allocated by child. */
1859 ret = lxc_recv_ttys_from_child(handler);
1860 if (ret < 0) {
1861 ERROR("Failed to receive tty info from child process");
1862 goto out_delete_net;
1863 }
1864
1865 ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, data_sock1);
1866 if (ret < 0) {
1867 SYSERROR("Failed to receive seccomp notify fd from child");
1868 goto out_delete_net;
1869 }
1870
1871 ret = handler->ops->post_start(handler, handler->data);
1872 if (ret < 0)
1873 goto out_abort;
1874
1875 ret = lxc_set_state(name, handler, RUNNING);
1876 if (ret < 0) {
1877 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING));
1878 goto out_abort;
1879 }
1880
1881 lxc_sync_fini(handler);
1882
1883 return 0;
1884
1885 out_delete_net:
1886 if (handler->ns_clone_flags & CLONE_NEWNET)
1887 lxc_delete_network(handler);
1888
1889 out_abort:
1890 lxc_abort(name, handler);
1891
1892 out_sync_fini:
1893 lxc_sync_fini(handler);
1894 close_prot_errno_disarm(handler->pinfd);
1895
1896 return -1;
1897 }
1898
1899 int __lxc_start(const char *name, struct lxc_handler *handler,
1900 struct lxc_operations* ops, void *data, const char *lxcpath,
1901 bool daemonize, int *error_num)
1902 {
1903 int ret, status;
1904 struct lxc_conf *conf = handler->conf;
1905 struct cgroup_ops *cgroup_ops;
1906
1907 ret = lxc_init(name, handler);
1908 if (ret < 0) {
1909 ERROR("Failed to initialize container \"%s\"", name);
1910 goto out_fini_nonet;
1911 }
1912 handler->ops = ops;
1913 handler->data = data;
1914 handler->daemonize = daemonize;
1915 cgroup_ops = handler->cgroup_ops;
1916
1917 if (!attach_block_device(handler->conf)) {
1918 ERROR("Failed to attach block device");
1919 ret = -1;
1920 goto out_fini_nonet;
1921 }
1922
1923 if (!cgroup_ops->monitor_create(cgroup_ops, handler)) {
1924 ERROR("Failed to create monitor cgroup");
1925 ret = -1;
1926 goto out_fini_nonet;
1927 }
1928
1929 if (!cgroup_ops->monitor_enter(cgroup_ops, handler)) {
1930 ERROR("Failed to enter monitor cgroup");
1931 ret = -1;
1932 goto out_fini_nonet;
1933 }
1934
1935 if (!cgroup_ops->monitor_delegate_controllers(cgroup_ops)) {
1936 ERROR("Failed to delegate controllers to monitor cgroup");
1937 ret = -1;
1938 goto out_fini_nonet;
1939 }
1940
1941 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
1942 /* If the backing store is a device, mount it here and now. */
1943 if (rootfs_is_blockdev(conf)) {
1944 ret = unshare(CLONE_NEWNS);
1945 if (ret < 0) {
1946 ERROR("Failed to unshare CLONE_NEWNS");
1947 goto out_fini_nonet;
1948 }
1949 INFO("Unshared CLONE_NEWNS");
1950
1951 remount_all_slave();
1952 ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath);
1953 if (ret < 0) {
1954 ERROR("Error setting up rootfs mount as root before spawn");
1955 goto out_fini_nonet;
1956 }
1957 INFO("Set up container rootfs as host root");
1958 }
1959 }
1960
1961 ret = lxc_spawn(handler);
1962 if (ret < 0) {
1963 ERROR("Failed to spawn container \"%s\"", name);
1964 goto out_detach_blockdev;
1965 }
1966
1967 handler->conf->reboot = REBOOT_NONE;
1968
1969 ret = lxc_poll(name, handler);
1970 if (ret) {
1971 ERROR("LXC mainloop exited with error: %d", ret);
1972 goto out_abort;
1973 }
1974
1975 if (!handler->init_died && handler->pid > 0) {
1976 ERROR("Child process is not killed");
1977 ret = -1;
1978 goto out_abort;
1979 }
1980
1981 status = lxc_wait_for_pid_status(handler->pid);
1982 if (status < 0)
1983 SYSERROR("Failed to retrieve status for %d", handler->pid);
1984
1985 /* If the child process exited but was not signaled, it didn't call
1986 * reboot. This should mean it was an lxc-execute which simply exited.
1987 * In any case, treat it as a 'halt'.
1988 */
1989 if (WIFSIGNALED(status)) {
1990 switch(WTERMSIG(status)) {
1991 case SIGINT: /* halt */
1992 DEBUG("Container \"%s\" is halting", name);
1993 break;
1994 case SIGHUP: /* reboot */
1995 DEBUG("Container \"%s\" is rebooting", name);
1996 handler->conf->reboot = REBOOT_REQ;
1997 break;
1998 case SIGSYS: /* seccomp */
1999 DEBUG("Container \"%s\" violated its seccomp policy", name);
2000 break;
2001 default:
2002 DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status));
2003 break;
2004 }
2005 }
2006
2007 ret = lxc_restore_phys_nics_to_netns(handler);
2008 if (ret < 0)
2009 ERROR("Failed to move physical network devices back to parent network namespace");
2010
2011 close_prot_errno_disarm(handler->pinfd);
2012
2013 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
2014 lxc_error_set_and_log(handler->pid, status);
2015 if (error_num)
2016 *error_num = handler->exit_status;
2017
2018 out_fini:
2019 lxc_delete_network(handler);
2020
2021 out_detach_blockdev:
2022 detach_block_device(handler->conf);
2023
2024 out_fini_nonet:
2025 lxc_fini(name, handler);
2026 return ret;
2027
2028 out_abort:
2029 lxc_abort(name, handler);
2030 goto out_fini;
2031 }
2032
2033 struct start_args {
2034 char *const *argv;
2035 };
2036
2037 static int start(struct lxc_handler *handler, void* data)
2038 {
2039 struct start_args *arg = data;
2040
2041 NOTICE("Exec'ing \"%s\"", arg->argv[0]);
2042
2043 execvp(arg->argv[0], arg->argv);
2044 SYSERROR("Failed to exec \"%s\"", arg->argv[0]);
2045 return 0;
2046 }
2047
2048 static int post_start(struct lxc_handler *handler, void* data)
2049 {
2050 struct start_args *arg = data;
2051
2052 NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid);
2053 return 0;
2054 }
2055
2056 static struct lxc_operations start_ops = {
2057 .start = start,
2058 .post_start = post_start
2059 };
2060
2061 int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler,
2062 const char *lxcpath, bool daemonize, int *error_num)
2063 {
2064 struct start_args start_arg = {
2065 .argv = argv,
2066 };
2067
2068 TRACE("Doing lxc_start");
2069 return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, daemonize, error_num);
2070 }
2071
2072 static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
2073 const char *name)
2074 {
2075 char destroy[PATH_MAX];
2076 struct lxc_container *c;
2077 int ret = 0;
2078 bool bret = true;
2079
2080 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
2081 bret = do_destroy_container(handler);
2082 if (!bret) {
2083 ERROR("Error destroying rootfs for container \"%s\"", name);
2084 return;
2085 }
2086 }
2087 INFO("Destroyed rootfs for container \"%s\"", name);
2088
2089 ret = snprintf(destroy, PATH_MAX, "%s/%s", handler->lxcpath, name);
2090 if (ret < 0 || ret >= PATH_MAX) {
2091 ERROR("Error destroying directory for container \"%s\"", name);
2092 return;
2093 }
2094
2095 c = lxc_container_new(name, handler->lxcpath);
2096 if (c) {
2097 if (container_disk_lock(c)) {
2098 INFO("Could not update lxc_snapshots file");
2099 lxc_container_put(c);
2100 } else {
2101 mod_all_rdeps(c, false);
2102 container_disk_unlock(c);
2103 lxc_container_put(c);
2104 }
2105 }
2106
2107 if (!handler->am_root)
2108 ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper,
2109 destroy, "lxc_rmdir_onedev_wrapper");
2110 else
2111 ret = lxc_rmdir_onedev(destroy, NULL);
2112
2113 if (ret < 0) {
2114 ERROR("Error destroying directory for container \"%s\"", name);
2115 return;
2116 }
2117 INFO("Destroyed directory for container \"%s\"", name);
2118 }
2119
2120 static int lxc_rmdir_onedev_wrapper(void *data)
2121 {
2122 char *arg = (char *) data;
2123 return lxc_rmdir_onedev(arg, NULL);
2124 }
2125
2126 static bool do_destroy_container(struct lxc_handler *handler)
2127 {
2128 int ret;
2129
2130 if (!handler->am_root) {
2131 ret = userns_exec_full(handler->conf, storage_destroy_wrapper,
2132 handler->conf, "storage_destroy_wrapper");
2133 if (ret < 0)
2134 return false;
2135
2136 return true;
2137 }
2138
2139 return storage_destroy(handler->conf);
2140 }