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