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