]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
17806704259d8fee404dae8f13178475ca5e783a
[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 <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 <sys/file.h>
41 #include <sys/mount.h>
42 #include <sys/param.h>
43 #include <sys/prctl.h>
44 #include <sys/socket.h>
45 #include <sys/stat.h>
46 #include <sys/syscall.h>
47 #include <sys/types.h>
48 #include <sys/un.h>
49 #include <sys/wait.h>
50 #include <unistd.h>
51
52 #include "af_unix.h"
53 #include "caps.h"
54 #include "cgroup.h"
55 #include "commands.h"
56 #include "commands_utils.h"
57 #include "conf.h"
58 #include "config.h"
59 #include "confile_utils.h"
60 #include "error.h"
61 #include "file_utils.h"
62 #include "list.h"
63 #include "log.h"
64 #include "lsm/lsm.h"
65 #include "lxccontainer.h"
66 #include "lxclock.h"
67 #include "lxcseccomp.h"
68 #include "macro.h"
69 #include "mainloop.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 int ret;
101 size_t len;
102 char *copy, *e, *p, saved;
103
104 len = strlen(path);
105 copy = alloca(len + 1);
106 (void)strlcpy(copy, path, len + 1);
107
108 p = copy;
109 e = copy + len;
110
111 while (p < e) {
112 while (p < e && *p == '/')
113 p++;
114
115 while (p < e && *p != '/')
116 p++;
117
118 saved = *p;
119 *p = '\0';
120
121 ret = access(copy, X_OK);
122 if (ret != 0) {
123 SYSERROR("Could not access %s. Please grant it x "
124 "access, or add an ACL for the container "
125 "root", copy);
126 return;
127 }
128 *p = saved;
129 }
130 }
131
132 static void lxc_put_nsfds(struct lxc_handler *handler)
133 {
134 int i;
135
136 for (i = 0; i < LXC_NS_MAX; i++) {
137 if (handler->nsfd[i] < 0)
138 continue;
139
140 close(handler->nsfd[i]);
141 handler->nsfd[i] = -EBADF;
142 }
143 }
144
145 static int lxc_try_preserve_ns(const int pid, const char *ns)
146 {
147 int fd;
148
149 fd = lxc_preserve_ns(pid, ns);
150 if (fd < 0) {
151 if (errno != ENOENT) {
152 SYSERROR("Failed to preserve %s namespace", ns);
153 return -EINVAL;
154 }
155
156 SYSWARN("Kernel does not support preserving %s namespaces", ns);
157 return -EOPNOTSUPP;
158 }
159
160 return fd;
161 }
162
163 /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
164 * specified in ns_clone_flags.
165 * Return true on success, false on failure.
166 */
167 static bool lxc_try_preserve_namespaces(struct lxc_handler *handler,
168 int ns_clone_flags, pid_t pid)
169 {
170 int i;
171
172 for (i = 0; i < LXC_NS_MAX; i++)
173 handler->nsfd[i] = -EBADF;
174
175 for (i = 0; i < LXC_NS_MAX; i++) {
176 int fd;
177
178 if ((ns_clone_flags & ns_info[i].clone_flag) == 0)
179 continue;
180
181 fd = lxc_try_preserve_ns(pid, ns_info[i].proc_name);
182 if (fd < 0) {
183 handler->nsfd[i] = -EBADF;
184
185 /* Do not fail to start container on kernels that do
186 * not support interacting with namespaces through
187 * /proc.
188 */
189 if (fd == -EOPNOTSUPP)
190 continue;
191
192 lxc_put_nsfds(handler);
193 return false;
194 }
195
196 handler->nsfd[i] = fd;
197 DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name,
198 handler->nsfd[i]);
199 }
200
201 return true;
202 }
203
204 static inline bool match_stdfds(int fd)
205 {
206 return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO);
207 }
208
209 int lxc_check_inherited(struct lxc_conf *conf, bool closeall,
210 int *fds_to_ignore, size_t len_fds)
211 {
212 int fd, fddir;
213 size_t i;
214 DIR *dir;
215 struct dirent *direntp;
216
217 if (conf && conf->close_all_fds)
218 closeall = true;
219
220 restart:
221 dir = opendir("/proc/self/fd");
222 if (!dir) {
223 SYSWARN("Failed to open directory");
224 return -1;
225 }
226
227 fddir = dirfd(dir);
228
229 while ((direntp = readdir(dir))) {
230 int ret;
231 struct lxc_list *cur;
232 bool matched = false;
233
234 if (strcmp(direntp->d_name, ".") == 0)
235 continue;
236
237 if (strcmp(direntp->d_name, "..") == 0)
238 continue;
239
240 ret = lxc_safe_int(direntp->d_name, &fd);
241 if (ret < 0) {
242 INFO("Could not parse file descriptor for \"%s\"", direntp->d_name);
243 continue;
244 }
245
246 for (i = 0; i < len_fds; i++)
247 if (fds_to_ignore[i] == fd)
248 break;
249
250 if (fd == fddir || fd == lxc_log_fd ||
251 (i < len_fds && fd == fds_to_ignore[i]))
252 continue;
253
254 /* Keep state clients that wait on reboots. */
255 if (conf) {
256 lxc_list_for_each(cur, &conf->state_clients) {
257 struct lxc_state_client *client = cur->elem;
258
259 if (client->clientfd != fd)
260 continue;
261
262 matched = true;
263 break;
264 }
265 }
266
267 if (matched)
268 continue;
269
270 if (current_config && fd == current_config->logfd)
271 continue;
272
273 if (match_stdfds(fd))
274 continue;
275
276 if (closeall) {
277 close(fd);
278 closedir(dir);
279 INFO("Closed inherited fd %d", fd);
280 goto restart;
281 }
282 WARN("Inherited fd %d", fd);
283 }
284
285 /* Only enable syslog at this point to avoid the above logging function
286 * to open a new fd and make the check_inherited function enter an
287 * infinite loop.
288 */
289 lxc_log_enable_syslog();
290
291 closedir(dir); /* cannot fail */
292 return 0;
293 }
294
295 static int setup_signal_fd(sigset_t *oldmask)
296 {
297 int ret;
298 int sig;
299 sigset_t mask;
300 const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH};
301
302 /* Block everything except serious error signals. */
303 ret = sigfillset(&mask);
304 if (ret < 0)
305 return -EBADF;
306
307 for (sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) {
308 ret = sigdelset(&mask, signals[sig]);
309 if (ret < 0)
310 return -EBADF;
311 }
312
313 ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask);
314 if (ret < 0) {
315 SYSERROR("Failed to set signal mask");
316 return -EBADF;
317 }
318
319 ret = signalfd(-1, &mask, SFD_CLOEXEC);
320 if (ret < 0) {
321 SYSERROR("Failed to create signal file descriptor");
322 return -EBADF;
323 }
324
325 TRACE("Created signal file descriptor %d", ret);
326
327 return ret;
328 }
329
330 static int signal_handler(int fd, uint32_t events, void *data,
331 struct lxc_epoll_descr *descr)
332 {
333 int ret;
334 siginfo_t info;
335 struct signalfd_siginfo siginfo;
336 struct lxc_handler *hdlr = data;
337
338 ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo));
339 if (ret < 0) {
340 ERROR("Failed to read signal info from signal file descriptor %d", fd);
341 return LXC_MAINLOOP_ERROR;
342 }
343
344 if (ret != sizeof(siginfo)) {
345 ERROR("Unexpected size for struct signalfd_siginfo");
346 return -EINVAL;
347 }
348
349 /* Check whether init is running. */
350 info.si_pid = 0;
351 ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG);
352 if (ret == 0 && info.si_pid == hdlr->pid)
353 hdlr->init_died = true;
354
355 /* Try to figure out a reasonable exit status to report. */
356 if (hdlr->init_died) {
357 switch (info.si_code) {
358 case CLD_EXITED:
359 hdlr->exit_status = info.si_status << 8;
360 break;
361 case CLD_KILLED:
362 case CLD_DUMPED:
363 case CLD_STOPPED:
364 hdlr->exit_status = info.si_status << 8 | 0x7f;
365 break;
366 case CLD_CONTINUED:
367 /* Huh? The waitid() told us it's dead *and* continued? */
368 WARN("Init %d dead and continued?", hdlr->pid);
369 hdlr->exit_status = 1;
370 break;
371 default:
372 ERROR("Unknown si_code: %d", info.si_code);
373 hdlr->exit_status = 1;
374 }
375 }
376
377 if (siginfo.ssi_signo == SIGHUP) {
378 kill(hdlr->pid, SIGTERM);
379 INFO("Killing %d since terminal hung up", hdlr->pid);
380 return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE;
381 }
382
383 if (siginfo.ssi_signo != SIGCHLD) {
384 kill(hdlr->pid, siginfo.ssi_signo);
385 INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid);
386 return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE;
387 }
388
389 /* More robustness, protect ourself from a SIGCHLD sent
390 * by a process different from the container init.
391 */
392 if (siginfo.ssi_pid != hdlr->pid) {
393 NOTICE("Received %d from pid %d instead of container init %d",
394 siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid);
395 return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE;
396 }
397
398 if (siginfo.ssi_code == CLD_STOPPED) {
399 INFO("Container init process was stopped");
400 return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE;
401 } else if (siginfo.ssi_code == CLD_CONTINUED) {
402 INFO("Container init process was continued");
403 return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE;
404 }
405
406 DEBUG("Container init process %d exited", hdlr->pid);
407 return LXC_MAINLOOP_CLOSE;
408 }
409
410 int lxc_serve_state_clients(const char *name, struct lxc_handler *handler,
411 lxc_state_t state)
412 {
413 size_t retlen;
414 ssize_t ret;
415 struct lxc_list *cur, *next;
416 struct lxc_state_client *client;
417 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
418
419 if (state == THAWED)
420 handler->state = RUNNING;
421 else
422 handler->state = state;
423
424 TRACE("Set container state to %s", lxc_state2str(state));
425
426 if (lxc_list_empty(&handler->conf->state_clients)) {
427 TRACE("No state clients registered");
428 return 0;
429 }
430
431 retlen = strlcpy(msg.name, name, sizeof(msg.name));
432 if (retlen >= sizeof(msg.name))
433 return -E2BIG;
434
435 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
436 client = cur->elem;
437
438 if (client->states[state] == 0) {
439 TRACE("State %s not registered for state client %d",
440 lxc_state2str(state), client->clientfd);
441 continue;
442 }
443
444 TRACE("Sending state %s to state client %d",
445 lxc_state2str(state), client->clientfd);
446
447 ret = lxc_send_nointr(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL);
448 if (ret <= 0)
449 SYSERROR("Failed to send message to client");
450
451 /* kick client from list */
452 lxc_list_del(cur);
453 close(client->clientfd);
454 free(cur->elem);
455 free(cur);
456 }
457
458 return 0;
459 }
460
461 static int lxc_serve_state_socket_pair(const char *name,
462 struct lxc_handler *handler,
463 lxc_state_t state)
464 {
465 ssize_t ret;
466
467 if (!handler->daemonize ||
468 handler->state_socket_pair[1] < 0 ||
469 state == STARTING)
470 return 0;
471
472 /* Close read end of the socket pair. */
473 close(handler->state_socket_pair[0]);
474 handler->state_socket_pair[0] = -1;
475
476 again:
477 ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1],
478 &(int){state}, sizeof(int));
479 if (ret < 0) {
480 SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]);
481
482 if (errno == EINTR)
483 goto again;
484
485 return -1;
486 }
487
488 if (ret != sizeof(int)) {
489 ERROR("Message too long : %d", handler->state_socket_pair[1]);
490 return -1;
491 }
492
493 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state),
494 handler->state_socket_pair[1]);
495
496 /* Close write end of the socket pair. */
497 close(handler->state_socket_pair[1]);
498 handler->state_socket_pair[1] = -1;
499
500 return 0;
501 }
502
503 int lxc_set_state(const char *name, struct lxc_handler *handler,
504 lxc_state_t state)
505 {
506 int ret;
507
508 ret = lxc_serve_state_socket_pair(name, handler, state);
509 if (ret < 0) {
510 ERROR("Failed to synchronize via anonymous pair of unix sockets");
511 return -1;
512 }
513
514 ret = lxc_serve_state_clients(name, handler, state);
515 if (ret < 0)
516 return -1;
517
518 /* This function will try to connect to the legacy lxc-monitord state
519 * server and only exists for backwards compatibility.
520 */
521 lxc_monitor_send_state(name, state, handler->lxcpath);
522
523 return 0;
524 }
525
526 int lxc_poll(const char *name, struct lxc_handler *handler)
527 {
528 int ret;
529 bool has_console = true;
530 struct lxc_epoll_descr descr, descr_console;
531
532 if (handler->conf->console.path &&
533 strcmp(handler->conf->console.path, "none") == 0)
534 has_console = false;
535
536 ret = lxc_mainloop_open(&descr);
537 if (ret < 0) {
538 ERROR("Failed to create mainloop");
539 goto out_sigfd;
540 }
541
542 if (has_console) {
543 ret = lxc_mainloop_open(&descr_console);
544 if (ret < 0) {
545 ERROR("Failed to create console mainloop");
546 goto out_mainloop;
547 }
548 }
549
550 ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler);
551 if (ret < 0) {
552 ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd);
553 goto out_mainloop_console;
554 }
555
556 if (has_console) {
557 struct lxc_terminal *console = &handler->conf->console;
558
559 ret = lxc_terminal_mainloop_add(&descr, console);
560 if (ret < 0) {
561 ERROR("Failed to add console handlers to mainloop");
562 goto out_mainloop_console;
563 }
564
565 ret = lxc_terminal_mainloop_add(&descr_console, console);
566 if (ret < 0) {
567 ERROR("Failed to add console handlers to console mainloop");
568 goto out_mainloop_console;
569 }
570
571 handler->conf->console.descr = &descr;
572 }
573
574 ret = lxc_cmd_mainloop_add(name, &descr, handler);
575 if (ret < 0) {
576 ERROR("Failed to add command handler to mainloop");
577 goto out_mainloop_console;
578 }
579
580 TRACE("Mainloop is ready");
581
582 ret = lxc_mainloop(&descr, -1);
583 close(descr.epfd);
584 descr.epfd = -EBADF;
585 if (ret < 0 || !handler->init_died)
586 goto out_mainloop_console;
587
588 if (has_console)
589 ret = lxc_mainloop(&descr_console, 0);
590
591 out_mainloop_console:
592 if (has_console) {
593 lxc_mainloop_close(&descr_console);
594 TRACE("Closed console mainloop");
595 }
596
597 out_mainloop:
598 lxc_mainloop_close(&descr);
599 TRACE("Closed mainloop");
600
601 out_sigfd:
602 close(handler->sigfd);
603 TRACE("Closed signal file descriptor %d", handler->sigfd);
604 handler->sigfd = -EBADF;
605
606 return ret;
607 }
608
609 void lxc_zero_handler(struct lxc_handler *handler)
610 {
611 int i;
612
613 memset(handler, 0, sizeof(struct lxc_handler));
614
615 handler->pinfd = -1;
616
617 handler->sigfd = -1;
618
619 for (i = 0; i < LXC_NS_MAX; i++)
620 handler->nsfd[i] = -1;
621
622 handler->data_sock[0] = -1;
623 handler->data_sock[1] = -1;
624
625 handler->state_socket_pair[0] = -1;
626 handler->state_socket_pair[1] = -1;
627
628 handler->sync_sock[0] = -1;
629 handler->sync_sock[1] = -1;
630 }
631
632 void lxc_free_handler(struct lxc_handler *handler)
633 {
634 if (handler->pinfd >= 0)
635 close(handler->pinfd);
636
637 if (handler->sigfd >= 0)
638 close(handler->sigfd);
639
640 lxc_put_nsfds(handler);
641
642 if (handler->conf && handler->conf->reboot == REBOOT_NONE)
643 if (handler->conf->maincmd_fd >= 0)
644 lxc_abstract_unix_close(handler->conf->maincmd_fd);
645
646 if (handler->state_socket_pair[0] >= 0)
647 close(handler->state_socket_pair[0]);
648
649 if (handler->state_socket_pair[1] >= 0)
650 close(handler->state_socket_pair[1]);
651
652 handler->conf = NULL;
653 free(handler);
654 handler = NULL;
655 }
656
657 struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf,
658 const char *lxcpath, bool daemonize)
659 {
660 int i, ret;
661 struct lxc_handler *handler;
662
663 handler = malloc(sizeof(*handler));
664 if (!handler)
665 return NULL;
666
667 memset(handler, 0, sizeof(*handler));
668
669 /* Note that am_guest_unpriv() checks the effective uid. We
670 * probably don't care if we are real root only if we are running
671 * as root so this should be fine.
672 */
673 handler->am_root = !am_guest_unpriv();
674 handler->data_sock[0] = handler->data_sock[1] = -1;
675 handler->conf = conf;
676 handler->lxcpath = lxcpath;
677 handler->pinfd = -1;
678 handler->sigfd = -EBADF;
679 handler->init_died = false;
680 handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1;
681 if (handler->conf->reboot == REBOOT_NONE)
682 lxc_list_init(&handler->conf->state_clients);
683
684 for (i = 0; i < LXC_NS_MAX; i++)
685 handler->nsfd[i] = -1;
686
687 handler->name = name;
688
689 if (daemonize && handler->conf->reboot == REBOOT_NONE) {
690 /* Create socketpair() to synchronize on daemonized startup.
691 * When the container reboots we don't need to synchronize
692 * again currently so don't open another socketpair().
693 */
694 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
695 handler->state_socket_pair);
696 if (ret < 0) {
697 ERROR("Failed to create anonymous pair of unix sockets");
698 goto on_error;
699 }
700
701 TRACE("Created anonymous pair {%d,%d} of unix sockets",
702 handler->state_socket_pair[0],
703 handler->state_socket_pair[1]);
704 }
705
706 if (handler->conf->reboot == REBOOT_NONE) {
707 handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command");
708 if (handler->conf->maincmd_fd < 0) {
709 ERROR("Failed to set up command socket");
710 goto on_error;
711 }
712 }
713
714 TRACE("Unix domain socket %d for command server is ready",
715 handler->conf->maincmd_fd);
716
717 return handler;
718
719 on_error:
720 lxc_free_handler(handler);
721
722 return NULL;
723 }
724
725 int lxc_init(const char *name, struct lxc_handler *handler)
726 {
727 int ret;
728 const char *loglevel;
729 struct lxc_conf *conf = handler->conf;
730
731 handler->monitor_pid = lxc_raw_getpid();
732
733 lsm_init();
734 TRACE("Initialized LSM");
735
736 ret = lxc_read_seccomp_config(conf);
737 if (ret < 0) {
738 ERROR("Failed loading seccomp policy");
739 goto out_close_maincmd_fd;
740 }
741 TRACE("Read seccomp policy");
742
743 /* Begin by setting the state to STARTING. */
744 ret = lxc_set_state(name, handler, STARTING);
745 if (ret < 0) {
746 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING));
747 goto out_close_maincmd_fd;
748 }
749 TRACE("Set container state to \"STARTING\"");
750
751 /* Start of environment variable setup for hooks. */
752 ret = setenv("LXC_NAME", name, 1);
753 if (ret < 0)
754 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name);
755
756 if (conf->rcfile) {
757 ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1);
758 if (ret < 0)
759 SYSERROR("Failed to set environment variable: "
760 "LXC_CONFIG_FILE=%s", conf->rcfile);
761 }
762
763 if (conf->rootfs.mount) {
764 ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1);
765 if (ret < 0)
766 SYSERROR("Failed to set environment variable: "
767 "LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount);
768 }
769
770 if (conf->rootfs.path) {
771 ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1);
772 if (ret < 0)
773 SYSERROR("Failed to set environment variable: "
774 "LXC_ROOTFS_PATH=%s", conf->rootfs.path);
775 }
776
777 if (conf->console.path) {
778 ret = setenv("LXC_CONSOLE", conf->console.path, 1);
779 if (ret < 0)
780 SYSERROR("Failed to set environment variable: "
781 "LXC_CONSOLE=%s", conf->console.path);
782 }
783
784 if (conf->console.log_path) {
785 ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1);
786 if (ret < 0)
787 SYSERROR("Failed to set environment variable: "
788 "LXC_CONSOLE_LOGPATH=%s", conf->console.log_path);
789 }
790
791 if (cgns_supported()) {
792 ret = setenv("LXC_CGNS_AWARE", "1", 1);
793 if (ret < 0)
794 SYSERROR("Failed to set environment variable "
795 "LXC_CGNS_AWARE=1");
796 }
797
798 loglevel = lxc_log_priority_to_string(lxc_log_get_level());
799 ret = setenv("LXC_LOG_LEVEL", loglevel, 1);
800 if (ret < 0)
801 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
802 loglevel);
803
804 if (conf->hooks_version == 0)
805 ret = setenv("LXC_HOOK_VERSION", "0", 1);
806 else
807 ret = setenv("LXC_HOOK_VERSION", "1", 1);
808 if (ret < 0)
809 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version);
810 /* End of environment variable setup for hooks. */
811
812 TRACE("Set environment variables");
813
814 ret = run_lxc_hooks(name, "pre-start", conf, NULL);
815 if (ret < 0) {
816 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name);
817 goto out_aborting;
818 }
819 TRACE("Ran pre-start hooks");
820
821 /* The signal fd has to be created before forking otherwise if the child
822 * process exits before we setup the signal fd, the event will be lost
823 * and the command will be stuck.
824 */
825 handler->sigfd = setup_signal_fd(&handler->oldmask);
826 if (handler->sigfd < 0) {
827 ERROR("Failed to setup SIGCHLD fd handler.");
828 goto out_delete_tty;
829 }
830 TRACE("Set up signal fd");
831
832 /* Do this after setting up signals since it might unblock SIGWINCH. */
833 ret = lxc_terminal_setup(conf);
834 if (ret < 0) {
835 ERROR("Failed to create console");
836 goto out_restore_sigmask;
837 }
838 TRACE("Created console");
839
840 ret = lxc_terminal_map_ids(conf, &conf->console);
841 if (ret < 0) {
842 ERROR("Failed to chown console");
843 goto out_delete_terminal;
844 }
845 TRACE("Chowned console");
846
847 handler->cgroup_ops = cgroup_init(handler->conf);
848 if (!handler->cgroup_ops) {
849 ERROR("Failed to initialize cgroup driver");
850 goto out_delete_terminal;
851 }
852 TRACE("Initialized cgroup driver");
853
854 ret = lsm_process_prepare(conf, handler->lxcpath);
855 if (ret < 0) {
856 ERROR("Failed to initialize LSM");
857 goto out_destroy_cgroups;
858 }
859 TRACE("Initialized LSM");
860
861 INFO("Container \"%s\" is initialized", name);
862 return 0;
863
864 out_destroy_cgroups:
865 handler->cgroup_ops->payload_destroy(handler->cgroup_ops, handler);
866 handler->cgroup_ops->monitor_destroy(handler->cgroup_ops, handler);
867
868 out_delete_terminal:
869 lxc_terminal_delete(&handler->conf->console);
870
871 out_restore_sigmask:
872 (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
873
874 out_delete_tty:
875 lxc_delete_tty(&conf->ttys);
876
877 out_aborting:
878 (void)lxc_set_state(name, handler, ABORTING);
879
880 out_close_maincmd_fd:
881 lxc_abstract_unix_close(conf->maincmd_fd);
882 conf->maincmd_fd = -1;
883 return -1;
884 }
885
886 void lxc_fini(const char *name, struct lxc_handler *handler)
887 {
888 int i, ret;
889 pid_t self;
890 struct lxc_list *cur, *next;
891 char *namespaces[LXC_NS_MAX + 1];
892 size_t namespace_count = 0;
893 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
894
895 /* The STOPPING state is there for future cleanup code which can take
896 * awhile.
897 */
898 lxc_set_state(name, handler, STOPPING);
899
900 self = lxc_raw_getpid();
901 for (i = 0; i < LXC_NS_MAX; i++) {
902 if (handler->nsfd[i] < 0)
903 continue;
904
905 if (handler->conf->hooks_version == 0)
906 ret = asprintf(&namespaces[namespace_count],
907 "%s:/proc/%d/fd/%d", ns_info[i].proc_name,
908 self, handler->nsfd[i]);
909 else
910 ret = asprintf(&namespaces[namespace_count],
911 "/proc/%d/fd/%d", self, handler->nsfd[i]);
912 if (ret == -1) {
913 SYSERROR("Failed to allocate memory");
914 break;
915 }
916
917 if (handler->conf->hooks_version == 0) {
918 namespace_count++;
919 continue;
920 }
921
922 ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1);
923 if (ret < 0)
924 SYSERROR("Failed to set environment variable %s=%s",
925 ns_info[i].env_name, namespaces[namespace_count]);
926 else
927 TRACE("Set environment variable %s=%s",
928 ns_info[i].env_name, namespaces[namespace_count]);
929
930 namespace_count++;
931 }
932 namespaces[namespace_count] = NULL;
933
934 if (handler->conf->reboot > REBOOT_NONE) {
935 ret = setenv("LXC_TARGET", "reboot", 1);
936 if (ret < 0)
937 SYSERROR("Failed to set environment variable: "
938 "LXC_TARGET=reboot");
939 }
940
941 if (handler->conf->reboot == REBOOT_NONE) {
942 ret = setenv("LXC_TARGET", "stop", 1);
943 if (ret < 0)
944 SYSERROR("Failed to set environment variable: "
945 "LXC_TARGET=stop");
946 }
947
948 if (handler->conf->hooks_version == 0)
949 ret = run_lxc_hooks(name, "stop", handler->conf, namespaces);
950 else
951 ret = run_lxc_hooks(name, "stop", handler->conf, NULL);
952 if (ret < 0)
953 ERROR("Failed to run \"lxc.hook.stop\" hook");
954
955 while (namespace_count--)
956 free(namespaces[namespace_count]);
957
958 lsm_process_cleanup(handler->conf, handler->lxcpath);
959
960 cgroup_ops->payload_destroy(cgroup_ops, handler);
961 cgroup_ops->monitor_destroy(cgroup_ops, handler);
962 cgroup_exit(cgroup_ops);
963
964 if (handler->conf->reboot == REBOOT_NONE) {
965 /* For all new state clients simply close the command socket.
966 * This will inform all state clients that the container is
967 * STOPPED and also prevents a race between a open()/close() on
968 * the command socket causing a new process to get ECONNREFUSED
969 * because we haven't yet closed the command socket.
970 */
971 lxc_abstract_unix_close(handler->conf->maincmd_fd);
972 handler->conf->maincmd_fd = -1;
973 TRACE("Closed command socket");
974
975 /* This function will try to connect to the legacy lxc-monitord
976 * state server and only exists for backwards compatibility.
977 */
978 lxc_monitor_send_state(name, STOPPED, handler->lxcpath);
979
980 /* The command socket is closed so no one can acces the command
981 * socket anymore so there's no need to lock it.
982 */
983 handler->state = STOPPED;
984 TRACE("Set container state to \"STOPPED\"");
985 } else {
986 lxc_set_state(name, handler, STOPPED);
987 }
988
989 ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL);
990 if (ret < 0) {
991 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name);
992 if (handler->conf->reboot > REBOOT_NONE) {
993 WARN("Container will be stopped instead of rebooted");
994 handler->conf->reboot = REBOOT_NONE;
995
996 ret = setenv("LXC_TARGET", "stop", 1);
997 if (ret < 0)
998 WARN("Failed to set environment variable: "
999 "LXC_TARGET=stop");
1000 }
1001 }
1002
1003 /* Reset mask set by setup_signal_fd. */
1004 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
1005 if (ret < 0)
1006 SYSWARN("Failed to restore signal mask");
1007
1008 lxc_terminal_delete(&handler->conf->console);
1009 lxc_delete_tty(&handler->conf->ttys);
1010
1011 /* The command socket is now closed, no more state clients can register
1012 * themselves from now on. So free the list of state clients.
1013 */
1014 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
1015 struct lxc_state_client *client = cur->elem;
1016
1017 /* Keep state clients that want to be notified about reboots. */
1018 if ((handler->conf->reboot > REBOOT_NONE) &&
1019 (client->states[RUNNING] == 2))
1020 continue;
1021
1022 /* close state client socket */
1023 lxc_list_del(cur);
1024 close(client->clientfd);
1025 free(cur->elem);
1026 free(cur);
1027 }
1028
1029 if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ)
1030 lxc_destroy_container_on_signal(handler, name);
1031
1032 lxc_free_handler(handler);
1033 }
1034
1035 void lxc_abort(const char *name, struct lxc_handler *handler)
1036 {
1037 int ret, status;
1038
1039 lxc_set_state(name, handler, ABORTING);
1040
1041 if (handler->pid > 0) {
1042 ret = kill(handler->pid, SIGKILL);
1043 if (ret < 0)
1044 SYSERROR("Failed to send SIGKILL to %d", handler->pid);
1045 }
1046
1047 while ((ret = waitpid(-1, &status, 0)) > 0) {
1048 ;
1049 }
1050 }
1051
1052 static int do_start(void *data)
1053 {
1054 int ret;
1055 char path[PATH_MAX];
1056 uid_t new_uid;
1057 gid_t new_gid;
1058 struct lxc_list *iterator;
1059 uid_t nsuid = 0;
1060 gid_t nsgid = 0;
1061 int devnull_fd = -1;
1062 struct lxc_handler *handler = data;
1063
1064 lxc_sync_fini_parent(handler);
1065
1066 /* This prctl must be before the synchro, so if the parent dies before
1067 * we set the parent death signal, we will detect its death with the
1068 * synchro right after, otherwise we have a window where the parent can
1069 * exit before we set the pdeath signal leading to a unsupervized
1070 * container.
1071 */
1072 ret = lxc_set_death_signal(SIGKILL, 0);
1073 if (ret < 0) {
1074 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1075 goto out_warn_father;
1076 }
1077
1078 ret = lxc_ambient_caps_up();
1079 if (ret < 0) {
1080 ERROR("Failed to raise ambient capabilities");
1081 goto out_warn_father;
1082 }
1083
1084 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
1085 if (ret < 0) {
1086 SYSERROR("Failed to set signal mask");
1087 goto out_warn_father;
1088 }
1089
1090 /* Don't leak the pinfd to the container. */
1091 if (handler->pinfd >= 0)
1092 close(handler->pinfd);
1093
1094 ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP);
1095 if (ret < 0)
1096 goto out_warn_father;
1097
1098 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1099 * https://github.com/lxc/lxd/issues/1978.
1100 */
1101 if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) ==
1102 (CLONE_NEWNET | CLONE_NEWUSER)) {
1103 ret = unshare(CLONE_NEWNET);
1104 if (ret < 0) {
1105 SYSERROR("Failed to unshare CLONE_NEWNET");
1106 goto out_warn_father;
1107 }
1108 INFO("Unshared CLONE_NEWNET");
1109 }
1110
1111 /* Tell the parent task it can begin to configure the container and wait
1112 * for it to finish.
1113 */
1114 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE);
1115 if (ret < 0)
1116 goto out_error;
1117
1118 ret = lxc_network_recv_veth_names_from_parent(handler);
1119 if (ret < 0) {
1120 ERROR("Failed to receive veth names from parent");
1121 goto out_warn_father;
1122 }
1123
1124 /* If we are in a new user namespace, become root there to have
1125 * privilege over our namespace.
1126 */
1127 if (!lxc_list_empty(&handler->conf->id_map)) {
1128 if (!handler->conf->root_nsuid_map)
1129 nsuid = handler->conf->init_uid;
1130
1131 if (!handler->conf->root_nsgid_map)
1132 nsgid = handler->conf->init_gid;
1133
1134 if (!lxc_switch_uid_gid(nsuid, nsgid))
1135 goto out_warn_father;
1136
1137 /* Drop groups only after we switched to a valid gid in the new
1138 * user namespace.
1139 */
1140 if (!lxc_setgroups(0, NULL) &&
1141 (handler->am_root || errno != EPERM))
1142 goto out_warn_father;
1143
1144 ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0),
1145 prctl_arg(0), prctl_arg(0));
1146 if (ret < 0)
1147 goto out_warn_father;
1148
1149 /* set{g,u}id() clears deathsignal */
1150 ret = lxc_set_death_signal(SIGKILL, 0);
1151 if (ret < 0) {
1152 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1153 goto out_warn_father;
1154 }
1155 }
1156
1157 ret = access(handler->lxcpath, X_OK);
1158 if (ret != 0) {
1159 print_top_failing_dir(handler->lxcpath);
1160 goto out_warn_father;
1161 }
1162
1163 ret = snprintf(path, sizeof(path), "%s/dev/null",
1164 handler->conf->rootfs.mount);
1165 if (ret < 0 || ret >= sizeof(path))
1166 goto out_warn_father;
1167
1168 /* In order to checkpoint restore, we need to have everything in the
1169 * same mount namespace. However, some containers may not have a
1170 * reasonable /dev (in particular, they may not have /dev/null), so we
1171 * can't set init's std fds to /dev/null by opening it from inside the
1172 * container.
1173 *
1174 * If that's the case, fall back to using the host's /dev/null. This
1175 * means that migration won't work, but at least we won't spew output
1176 * where it isn't wanted.
1177 */
1178 if (handler->daemonize && !handler->conf->autodev) {
1179 ret = access(path, F_OK);
1180 if (ret != 0) {
1181 devnull_fd = open_devnull();
1182
1183 if (devnull_fd < 0)
1184 goto out_warn_father;
1185 WARN("Using /dev/null from the host for container "
1186 "init's standard file descriptors. Migration will "
1187 "not work");
1188 }
1189 }
1190
1191 /* Ask father to setup cgroups and wait for him to finish. */
1192 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP);
1193 if (ret < 0)
1194 goto out_error;
1195
1196 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1197 * earlier we end up with a wrong view of /proc/self/cgroup. For
1198 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1199 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1200 * /proc/self/cgroup would show us:
1201 *
1202 * 8:cpuset:/lxc/c
1203 *
1204 * whereas it should actually show
1205 *
1206 * 8:cpuset:/
1207 */
1208 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
1209 ret = unshare(CLONE_NEWCGROUP);
1210 if (ret < 0) {
1211 INFO("Failed to unshare CLONE_NEWCGROUP");
1212 goto out_warn_father;
1213 }
1214 INFO("Unshared CLONE_NEWCGROUP");
1215 }
1216
1217 /* Add the requested environment variables to the current environment to
1218 * allow them to be used by the various hooks, such as the start hook
1219 * above.
1220 */
1221 lxc_list_for_each(iterator, &handler->conf->environment) {
1222 ret = putenv((char *)iterator->elem);
1223 if (ret < 0) {
1224 SYSERROR("Failed to set environment variable: %s",
1225 (char *)iterator->elem);
1226 goto out_warn_father;
1227 }
1228 }
1229
1230 /* Setup the container, ip, names, utsname, ... */
1231 ret = lxc_setup(handler);
1232 close(handler->data_sock[1]);
1233 close(handler->data_sock[0]);
1234 if (ret < 0) {
1235 ERROR("Failed to setup container \"%s\"", handler->name);
1236 goto out_warn_father;
1237 }
1238
1239 /* Set the label to change to when we exec(2) the container's init. */
1240 ret = lsm_process_label_set(NULL, handler->conf, true);
1241 if (ret < 0)
1242 goto out_warn_father;
1243
1244 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
1245 * before we aren't allowed anymore.
1246 */
1247 if (handler->conf->no_new_privs) {
1248 ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0),
1249 prctl_arg(0), prctl_arg(0));
1250 if (ret < 0) {
1251 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1252 "execve() gainable privileges");
1253 goto out_warn_father;
1254 }
1255 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1256 "privileges");
1257 }
1258
1259 /* Some init's such as busybox will set sane tty settings on stdin,
1260 * stdout, stderr which it thinks is the console. We already set them
1261 * the way we wanted on the real terminal, and we want init to do its
1262 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
1263 * make sure that that pty is stdin,stdout,stderr.
1264 */
1265 if (handler->conf->console.slave >= 0) {
1266 if (handler->daemonize || !handler->conf->is_execute)
1267 ret = set_stdfds(handler->conf->console.slave);
1268 else
1269 ret = lxc_terminal_set_stdfds(handler->conf->console.slave);
1270 if (ret < 0) {
1271 ERROR("Failed to redirect std{in,out,err} to pty file "
1272 "descriptor %d", handler->conf->console.slave);
1273 goto out_warn_father;
1274 }
1275 }
1276
1277 /* If we mounted a temporary proc, then unmount it now. */
1278 tmp_proc_unmount(handler->conf);
1279
1280 ret = lxc_seccomp_load(handler->conf);
1281 if (ret < 0)
1282 goto out_warn_father;
1283
1284 ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL);
1285 if (ret < 0) {
1286 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1287 handler->name);
1288 goto out_warn_father;
1289 }
1290
1291 close(handler->sigfd);
1292
1293 if (handler->conf->console.slave < 0 && handler->daemonize) {
1294 if (devnull_fd < 0) {
1295 devnull_fd = open_devnull();
1296 if (devnull_fd < 0)
1297 goto out_warn_father;
1298 }
1299
1300 ret = set_stdfds(devnull_fd);
1301 if (ret < 0) {
1302 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
1303 goto out_warn_father;
1304 }
1305 }
1306
1307 if (devnull_fd >= 0) {
1308 close(devnull_fd);
1309 devnull_fd = -1;
1310 }
1311
1312 setsid();
1313
1314 if (handler->conf->init_cwd) {
1315 ret = chdir(handler->conf->init_cwd);
1316 if (ret < 0) {
1317 SYSERROR("Could not change directory to \"%s\"",
1318 handler->conf->init_cwd);
1319 goto out_warn_father;
1320 }
1321 }
1322
1323 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS);
1324 if (ret < 0)
1325 goto out_warn_father;
1326
1327 /* Reset the environment variables the user requested in a clear
1328 * environment.
1329 */
1330 ret = clearenv();
1331 /* Don't error out though. */
1332 if (ret < 0)
1333 SYSERROR("Failed to clear environment.");
1334
1335 lxc_list_for_each(iterator, &handler->conf->environment) {
1336 ret = putenv((char *)iterator->elem);
1337 if (ret < 0) {
1338 SYSERROR("Failed to set environment variable: %s",
1339 (char *)iterator->elem);
1340 goto out_warn_father;
1341 }
1342 }
1343
1344 ret = putenv("container=lxc");
1345 if (ret < 0) {
1346 SYSERROR("Failed to set environment variable: container=lxc");
1347 goto out_warn_father;
1348 }
1349
1350 if (handler->conf->ttys.tty_names) {
1351 ret = putenv(handler->conf->ttys.tty_names);
1352 if (ret < 0) {
1353 SYSERROR("Failed to set environment variable for container ptys");
1354 goto out_warn_father;
1355 }
1356 }
1357
1358 /* The container has been setup. We can now switch to an unprivileged
1359 * uid/gid.
1360 */
1361 new_uid = handler->conf->init_uid;
1362 new_gid = handler->conf->init_gid;
1363
1364 /* Avoid unnecessary syscalls. */
1365 if (new_uid == nsuid)
1366 new_uid = LXC_INVALID_UID;
1367
1368 if (new_gid == nsgid)
1369 new_gid = LXC_INVALID_GID;
1370
1371 if (!lxc_switch_uid_gid(new_uid, new_gid))
1372 goto out_warn_father;
1373
1374 /* If we are in a new user namespace we already dropped all groups when
1375 * we switched to root in the new user namespace further above. Only
1376 * drop groups if we can, so ensure that we have necessary privilege.
1377 */
1378 if (lxc_list_empty(&handler->conf->id_map))
1379 #if HAVE_LIBCAP
1380 if (lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE))
1381 #endif
1382 if (!lxc_setgroups(0, NULL))
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, 0);
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->payload_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->payload_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 daemonize, int *error_num)
1956 {
1957 int ret, status;
1958 struct lxc_conf *conf = handler->conf;
1959 struct cgroup_ops *cgroup_ops;
1960
1961 ret = lxc_init(name, handler);
1962 if (ret < 0) {
1963 ERROR("Failed to initialize container \"%s\"", name);
1964 return -1;
1965 }
1966 handler->ops = ops;
1967 handler->data = data;
1968 handler->daemonize = daemonize;
1969 cgroup_ops = handler->cgroup_ops;
1970
1971 if (!attach_block_device(handler->conf)) {
1972 ERROR("Failed to attach block device");
1973 goto out_fini_nonet;
1974 }
1975
1976 if (!cgroup_ops->monitor_create(cgroup_ops, handler)) {
1977 ERROR("Failed to create monitor cgroup");
1978 goto out_fini_nonet;
1979 }
1980
1981 if (!cgroup_ops->monitor_enter(cgroup_ops, handler->monitor_pid)) {
1982 ERROR("Failed to enter monitor cgroup");
1983 goto out_fini_nonet;
1984 }
1985
1986 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
1987 /* If the backing store is a device, mount it here and now. */
1988 if (rootfs_is_blockdev(conf)) {
1989 ret = unshare(CLONE_NEWNS);
1990 if (ret < 0) {
1991 ERROR("Failed to unshare CLONE_NEWNS");
1992 goto out_fini_nonet;
1993 }
1994 INFO("Unshared CLONE_NEWNS");
1995
1996 remount_all_slave();
1997 ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath);
1998 if (ret < 0) {
1999 ERROR("Error setting up rootfs mount as root before spawn");
2000 goto out_fini_nonet;
2001 }
2002 INFO("Set up container rootfs as host root");
2003 }
2004 }
2005
2006 ret = lxc_spawn(handler);
2007 if (ret < 0) {
2008 ERROR("Failed to spawn container \"%s\"", name);
2009 goto out_detach_blockdev;
2010 }
2011 /* close parent side of data socket */
2012 close(handler->data_sock[0]);
2013 handler->data_sock[0] = -1;
2014 close(handler->data_sock[1]);
2015 handler->data_sock[1] = -1;
2016
2017 handler->conf->reboot = REBOOT_NONE;
2018
2019 ret = lxc_poll(name, handler);
2020 if (ret) {
2021 ERROR("LXC mainloop exited with error: %d", ret);
2022 goto out_abort;
2023 }
2024
2025 if (!handler->init_died && handler->pid > 0) {
2026 ERROR("Child process is not killed");
2027 goto out_abort;
2028 }
2029
2030 status = lxc_wait_for_pid_status(handler->pid);
2031 if (status < 0)
2032 SYSERROR("Failed to retrieve status for %d", handler->pid);
2033
2034 /* If the child process exited but was not signaled, it didn't call
2035 * reboot. This should mean it was an lxc-execute which simply exited.
2036 * In any case, treat it as a 'halt'.
2037 */
2038 if (WIFSIGNALED(status)) {
2039 switch(WTERMSIG(status)) {
2040 case SIGINT: /* halt */
2041 DEBUG("Container \"%s\" is halting", name);
2042 break;
2043 case SIGHUP: /* reboot */
2044 DEBUG("Container \"%s\" is rebooting", name);
2045 handler->conf->reboot = REBOOT_REQ;
2046 break;
2047 case SIGSYS: /* seccomp */
2048 DEBUG("Container \"%s\" violated its seccomp policy", name);
2049 break;
2050 default:
2051 DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status));
2052 break;
2053 }
2054 }
2055
2056 ret = lxc_restore_phys_nics_to_netns(handler);
2057 if (ret < 0)
2058 ERROR("Failed to move physical network devices back to parent "
2059 "network namespace");
2060
2061 if (handler->pinfd >= 0) {
2062 close(handler->pinfd);
2063 handler->pinfd = -1;
2064 }
2065
2066 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
2067 lxc_error_set_and_log(handler->pid, status);
2068 if (error_num)
2069 *error_num = handler->exit_status;
2070
2071 out_fini:
2072 lxc_delete_network(handler);
2073
2074 out_detach_blockdev:
2075 detach_block_device(handler->conf);
2076
2077 out_fini_nonet:
2078 lxc_fini(name, handler);
2079 return ret;
2080
2081 out_abort:
2082 lxc_abort(name, handler);
2083 goto out_fini;
2084 }
2085
2086 struct start_args {
2087 char *const *argv;
2088 };
2089
2090 static int start(struct lxc_handler *handler, void* data)
2091 {
2092 struct start_args *arg = data;
2093
2094 NOTICE("Exec'ing \"%s\"", arg->argv[0]);
2095
2096 execvp(arg->argv[0], arg->argv);
2097 SYSERROR("Failed to exec \"%s\"", arg->argv[0]);
2098 return 0;
2099 }
2100
2101 static int post_start(struct lxc_handler *handler, void* data)
2102 {
2103 struct start_args *arg = data;
2104
2105 NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid);
2106 return 0;
2107 }
2108
2109 static struct lxc_operations start_ops = {
2110 .start = start,
2111 .post_start = post_start
2112 };
2113
2114 int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler,
2115 const char *lxcpath, bool daemonize, int *error_num)
2116 {
2117 struct start_args start_arg = {
2118 .argv = argv,
2119 };
2120
2121 TRACE("Doing lxc_start");
2122 return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, daemonize, error_num);
2123 }
2124
2125 static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
2126 const char *name)
2127 {
2128 char destroy[MAXPATHLEN];
2129 struct lxc_container *c;
2130 int ret = 0;
2131 bool bret = true;
2132
2133 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
2134 bret = do_destroy_container(handler);
2135 if (!bret) {
2136 ERROR("Error destroying rootfs for container \"%s\"", name);
2137 return;
2138 }
2139 }
2140 INFO("Destroyed rootfs for container \"%s\"", name);
2141
2142 ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name);
2143 if (ret < 0 || ret >= MAXPATHLEN) {
2144 ERROR("Error destroying directory for container \"%s\"", name);
2145 return;
2146 }
2147
2148 c = lxc_container_new(name, handler->lxcpath);
2149 if (c) {
2150 if (container_disk_lock(c)) {
2151 INFO("Could not update lxc_snapshots file");
2152 lxc_container_put(c);
2153 } else {
2154 mod_all_rdeps(c, false);
2155 container_disk_unlock(c);
2156 lxc_container_put(c);
2157 }
2158 }
2159
2160 if (!handler->am_root)
2161 ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper,
2162 destroy, "lxc_rmdir_onedev_wrapper");
2163 else
2164 ret = lxc_rmdir_onedev(destroy, NULL);
2165
2166 if (ret < 0) {
2167 ERROR("Error destroying directory for container \"%s\"", name);
2168 return;
2169 }
2170 INFO("Destroyed directory for container \"%s\"", name);
2171 }
2172
2173 static int lxc_rmdir_onedev_wrapper(void *data)
2174 {
2175 char *arg = (char *) data;
2176 return lxc_rmdir_onedev(arg, NULL);
2177 }
2178
2179 static bool do_destroy_container(struct lxc_handler *handler)
2180 {
2181 int ret;
2182
2183 if (!handler->am_root) {
2184 ret = userns_exec_full(handler->conf, storage_destroy_wrapper,
2185 handler->conf, "storage_destroy_wrapper");
2186 if (ret < 0)
2187 return false;
2188
2189 return true;
2190 }
2191
2192 return storage_destroy(handler->conf);
2193 }