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