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