]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
setup cgroups from parent
[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 <dlezcano at fr.ibm.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #undef _GNU_SOURCE
28 #include <string.h>
29 #include <stdlib.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <fcntl.h>
35 #include <termios.h>
36 #include <sys/param.h>
37 #include <sys/file.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/prctl.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <sys/un.h>
46 #include <sys/poll.h>
47 #include <sys/syscall.h>
48
49 #if HAVE_SYS_CAPABILITY_H
50 #include <sys/capability.h>
51 #endif
52
53 #ifdef HAVE_SYS_SIGNALFD_H
54 # include <sys/signalfd.h>
55 #else
56 /* assume kernel headers are too old */
57 #include <stdint.h>
58 struct signalfd_siginfo
59 {
60 uint32_t ssi_signo;
61 int32_t ssi_errno;
62 int32_t ssi_code;
63 uint32_t ssi_pid;
64 uint32_t ssi_uid;
65 int32_t ssi_fd;
66 uint32_t ssi_tid;
67 uint32_t ssi_band;
68 uint32_t ssi_overrun;
69 uint32_t ssi_trapno;
70 int32_t ssi_status;
71 int32_t ssi_int;
72 uint64_t ssi_ptr;
73 uint64_t ssi_utime;
74 uint64_t ssi_stime;
75 uint64_t ssi_addr;
76 uint8_t __pad[48];
77 };
78
79 # ifndef __NR_signalfd4
80 /* assume kernel headers are too old */
81 # if __i386__
82 # define __NR_signalfd4 327
83 # elif __x86_64__
84 # define __NR_signalfd4 289
85 # elif __powerpc__
86 # define __NR_signalfd4 313
87 # elif __s390x__
88 # define __NR_signalfd4 322
89 # endif
90 #endif
91
92 # ifndef __NR_signalfd
93 /* assume kernel headers are too old */
94 # if __i386__
95 # define __NR_signalfd 321
96 # elif __x86_64__
97 # define __NR_signalfd 282
98 # elif __powerpc__
99 # define __NR_signalfd 305
100 # elif __s390x__
101 # define __NR_signalfd 316
102 # endif
103 #endif
104
105 int signalfd(int fd, const sigset_t *mask, int flags)
106 {
107 int retval;
108
109 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
110 if (errno == ENOSYS && flags == 0)
111 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
112 return retval;
113 }
114 #endif
115
116 #if !HAVE_DECL_PR_CAPBSET_DROP
117 #define PR_CAPBSET_DROP 24
118 #endif
119
120 #include "start.h"
121 #include "conf.h"
122 #include "log.h"
123 #include "cgroup.h"
124 #include "error.h"
125 #include "af_unix.h"
126 #include "mainloop.h"
127 #include "utils.h"
128 #include "lxcutmp.h"
129 #include "monitor.h"
130 #include "commands.h"
131 #include "console.h"
132 #include "sync.h"
133 #include "namespace.h"
134 #include "apparmor.h"
135 #include "lxcseccomp.h"
136 #include "caps.h"
137
138 lxc_log_define(lxc_start, lxc);
139
140 static int match_fd(int fd)
141 {
142 return (fd == 0 || fd == 1 || fd == 2);
143 }
144
145 int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore)
146 {
147 struct dirent dirent, *direntp;
148 int fd, fddir;
149 DIR *dir;
150
151 restart:
152 dir = opendir("/proc/self/fd");
153 if (!dir) {
154 WARN("failed to open directory: %m");
155 return -1;
156 }
157
158 fddir = dirfd(dir);
159
160 while (!readdir_r(dir, &dirent, &direntp)) {
161 if (!direntp)
162 break;
163
164 if (!strcmp(direntp->d_name, "."))
165 continue;
166
167 if (!strcmp(direntp->d_name, ".."))
168 continue;
169
170 fd = atoi(direntp->d_name);
171
172 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
173 continue;
174
175 if (match_fd(fd))
176 continue;
177
178 if (conf->close_all_fds) {
179 close(fd);
180 closedir(dir);
181 INFO("closed inherited fd %d", fd);
182 goto restart;
183 }
184 WARN("inherited fd %d", fd);
185 }
186
187 closedir(dir); /* cannot fail */
188 return 0;
189 }
190
191 static int setup_signal_fd(sigset_t *oldmask)
192 {
193 sigset_t mask;
194 int fd;
195
196 /* Block everything except serious error signals */
197 if (sigfillset(&mask) ||
198 sigdelset(&mask, SIGILL) ||
199 sigdelset(&mask, SIGSEGV) ||
200 sigdelset(&mask, SIGBUS) ||
201 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
202 SYSERROR("failed to set signal mask");
203 return -1;
204 }
205
206 fd = signalfd(-1, &mask, 0);
207 if (fd < 0) {
208 SYSERROR("failed to create the signal fd");
209 return -1;
210 }
211
212 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
213 SYSERROR("failed to set sigfd to close-on-exec");
214 close(fd);
215 return -1;
216 }
217
218 DEBUG("sigchild handler set");
219
220 return fd;
221 }
222
223 static int signal_handler(int fd, void *data,
224 struct lxc_epoll_descr *descr)
225 {
226 struct signalfd_siginfo siginfo;
227 int ret;
228 pid_t *pid = data;
229
230 ret = read(fd, &siginfo, sizeof(siginfo));
231 if (ret < 0) {
232 ERROR("failed to read signal info");
233 return -1;
234 }
235
236 if (ret != sizeof(siginfo)) {
237 ERROR("unexpected siginfo size");
238 return -1;
239 }
240
241 if (siginfo.ssi_signo != SIGCHLD) {
242 kill(*pid, siginfo.ssi_signo);
243 INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
244 return 0;
245 }
246
247 if (siginfo.ssi_code == CLD_STOPPED ||
248 siginfo.ssi_code == CLD_CONTINUED) {
249 INFO("container init process was stopped/continued");
250 return 0;
251 }
252
253 /* more robustness, protect ourself from a SIGCHLD sent
254 * by a process different from the container init
255 */
256 if (siginfo.ssi_pid != *pid) {
257 WARN("invalid pid for SIGCHLD");
258 return 0;
259 }
260
261 DEBUG("container init process exited");
262 return 1;
263 }
264
265 int lxc_pid_callback(int fd, struct lxc_request *request,
266 struct lxc_handler *handler)
267 {
268 struct lxc_answer answer;
269 int ret;
270
271 answer.pid = handler->pid;
272 answer.ret = 0;
273
274 ret = send(fd, &answer, sizeof(answer), 0);
275 if (ret < 0) {
276 WARN("failed to send answer to the peer");
277 return -1;
278 }
279
280 if (ret != sizeof(answer)) {
281 ERROR("partial answer sent");
282 return -1;
283 }
284
285 return 0;
286 }
287
288 int lxc_clone_flags_callback(int fd, struct lxc_request *request,
289 struct lxc_handler *handler)
290 {
291 struct lxc_answer answer;
292 int ret;
293
294 answer.pid = 0;
295 answer.ret = handler->clone_flags;
296
297 ret = send(fd, &answer, sizeof(answer), 0);
298 if (ret < 0) {
299 WARN("failed to send answer to the peer");
300 return -1;
301 }
302
303 if (ret != sizeof(answer)) {
304 ERROR("partial answer sent");
305 return -1;
306 }
307
308 return 0;
309 }
310
311 int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
312 {
313 handler->state = state;
314 lxc_monitor_send_state(name, state);
315 return 0;
316 }
317
318 int lxc_poll(const char *name, struct lxc_handler *handler)
319 {
320 int sigfd = handler->sigfd;
321 int pid = handler->pid;
322 struct lxc_epoll_descr descr;
323
324 if (lxc_mainloop_open(&descr)) {
325 ERROR("failed to create mainloop");
326 goto out_sigfd;
327 }
328
329 if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
330 ERROR("failed to add handler for the signal");
331 goto out_mainloop_open;
332 }
333
334 if (lxc_console_mainloop_add(&descr, handler)) {
335 ERROR("failed to add console handler to mainloop");
336 goto out_mainloop_open;
337 }
338
339 if (lxc_command_mainloop_add(name, &descr, handler)) {
340 ERROR("failed to add command handler to mainloop");
341 goto out_mainloop_open;
342 }
343
344 if (handler->conf->need_utmp_watch) {
345 #if HAVE_SYS_CAPABILITY_H
346 if (lxc_utmp_mainloop_add(&descr, handler)) {
347 ERROR("failed to add utmp handler to mainloop");
348 goto out_mainloop_open;
349 }
350 #else
351 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support\n");
352 #endif
353 }
354
355 return lxc_mainloop(&descr);
356
357 out_mainloop_open:
358 lxc_mainloop_close(&descr);
359 out_sigfd:
360 close(sigfd);
361 return -1;
362 }
363
364 extern int lxc_caps_check(void);
365
366 struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
367 {
368 struct lxc_handler *handler;
369
370 if (!lxc_caps_check()) {
371 ERROR("Not running with sufficient privilege");
372 return NULL;
373 }
374
375 handler = malloc(sizeof(*handler));
376 if (!handler)
377 return NULL;
378
379 memset(handler, 0, sizeof(*handler));
380
381 handler->conf = conf;
382
383 apparmor_handler_init(handler);
384 handler->name = strdup(name);
385 if (!handler->name) {
386 ERROR("failed to allocate memory");
387 goto out_free;
388 }
389
390 if (lxc_command_init(name, handler))
391 goto out_free_name;
392
393 if (lxc_read_seccomp_config(conf) != 0) {
394 ERROR("failed loading seccomp policy");
395 goto out_close_maincmd_fd;
396 }
397
398 /* Begin the set the state to STARTING*/
399 if (lxc_set_state(name, handler, STARTING)) {
400 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
401 goto out_free_name;
402 }
403
404 /* Start of environment variable setup for hooks */
405 if (setenv("LXC_NAME", name, 1)) {
406 SYSERROR("failed to set environment variable for container name");
407 }
408 if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
409 SYSERROR("failed to set environment variable for config path");
410 }
411 if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
412 SYSERROR("failed to set environment variable for rootfs mount");
413 }
414 if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
415 SYSERROR("failed to set environment variable for rootfs mount");
416 }
417 if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) {
418 SYSERROR("failed to set environment variable for console path");
419 }
420 if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) {
421 SYSERROR("failed to set environment variable for console log");
422 }
423 /* End of environment variable setup for hooks */
424
425 if (run_lxc_hooks(name, "pre-start", conf)) {
426 ERROR("failed to run pre-start hooks for container '%s'.", name);
427 goto out_aborting;
428 }
429
430 if (lxc_create_tty(name, conf)) {
431 ERROR("failed to create the ttys");
432 goto out_aborting;
433 }
434
435 if (lxc_create_console(conf)) {
436 ERROR("failed to create console");
437 goto out_delete_tty;
438 }
439
440 /* the signal fd has to be created before forking otherwise
441 * if the child process exits before we setup the signal fd,
442 * the event will be lost and the command will be stuck */
443 handler->sigfd = setup_signal_fd(&handler->oldmask);
444 if (handler->sigfd < 0) {
445 ERROR("failed to set sigchild fd handler");
446 goto out_delete_console;
447 }
448
449 INFO("'%s' is initialized", name);
450 return handler;
451
452 out_delete_console:
453 lxc_delete_console(&conf->console);
454 out_delete_tty:
455 lxc_delete_tty(&conf->tty_info);
456 out_aborting:
457 lxc_set_state(name, handler, ABORTING);
458 out_close_maincmd_fd:
459 close(conf->maincmd_fd);
460 conf->maincmd_fd = -1;
461 out_free_name:
462 free(handler->name);
463 handler->name = NULL;
464 out_free:
465 free(handler);
466 return NULL;
467 }
468
469 void lxc_fini(const char *name, struct lxc_handler *handler)
470 {
471 /* The STOPPING state is there for future cleanup code
472 * which can take awhile
473 */
474 lxc_set_state(name, handler, STOPPING);
475 lxc_set_state(name, handler, STOPPED);
476
477 if (run_lxc_hooks(name, "post-stop", handler->conf))
478 ERROR("failed to run post-stop hooks for container '%s'.", name);
479
480 /* reset mask set by setup_signal_fd */
481 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
482 WARN("failed to restore sigprocmask");
483
484 lxc_delete_console(&handler->conf->console);
485 lxc_delete_tty(&handler->conf->tty_info);
486 close(handler->conf->maincmd_fd);
487 handler->conf->maincmd_fd = -1;
488 free(handler->name);
489 free(handler);
490 }
491
492 void lxc_abort(const char *name, struct lxc_handler *handler)
493 {
494 lxc_set_state(name, handler, ABORTING);
495 if (handler->pid > 0)
496 kill(handler->pid, SIGKILL);
497 }
498
499 #include <sys/reboot.h>
500 #include <linux/reboot.h>
501
502 /*
503 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
504 * in a child pid namespace if container reboot support exists.
505 * Otherwise, it will either succeed or return -EPERM.
506 */
507 static int container_reboot_supported(void *arg)
508 {
509 int *cmd = arg;
510 int ret;
511
512 ret = reboot(*cmd);
513 if (ret == -1 && errno == EINVAL)
514 return 1;
515 return 0;
516 }
517
518 static int must_drop_cap_sys_boot(void)
519 {
520 FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
521 int ret, cmd, v;
522 long stack_size = 4096;
523 void *stack = alloca(stack_size) + stack_size;
524 int status;
525 pid_t pid;
526
527 if (!f) {
528 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
529 return 1;
530 }
531
532 ret = fscanf(f, "%d", &v);
533 fclose(f);
534 if (ret != 1) {
535 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
536 return 1;
537 }
538 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
539
540 pid = clone(container_reboot_supported, stack, CLONE_NEWPID | SIGCHLD, &cmd);
541 if (pid < 0) {
542 SYSERROR("failed to clone\n");
543 return -1;
544 }
545 if (wait(&status) < 0) {
546 SYSERROR("unexpected wait error: %m\n");
547 return -1;
548 }
549
550 if (WEXITSTATUS(status) != 1)
551 return 1;
552
553 return 0;
554 }
555
556 static int do_start(void *data)
557 {
558 struct lxc_handler *handler = data;
559
560 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
561 SYSERROR("failed to set sigprocmask");
562 return -1;
563 }
564
565 /* This prctl must be before the synchro, so if the parent
566 * dies before we set the parent death signal, we will detect
567 * its death with the synchro right after, otherwise we have
568 * a window where the parent can exit before we set the pdeath
569 * signal leading to a unsupervized container.
570 */
571 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
572 SYSERROR("failed to set pdeath signal");
573 return -1;
574 }
575
576 lxc_sync_fini_parent(handler);
577
578 /* Tell the parent task it can begin to configure the
579 * container and wait for it to finish
580 */
581 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
582 return -1;
583
584 #if HAVE_SYS_CAPABILITY_H
585 if (handler->conf->need_utmp_watch) {
586 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
587 SYSERROR("failed to remove CAP_SYS_BOOT capability");
588 goto out_warn_father;
589 }
590 DEBUG("Dropped cap_sys_boot\n");
591 }
592 #endif
593
594 /* Setup the container, ip, names, utsname, ... */
595 if (lxc_setup(handler->name, handler->conf)) {
596 ERROR("failed to setup the container");
597 goto out_warn_father;
598 }
599
600 /* ask father to setup cgroups and wait for him to finish */
601 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP))
602 return -1;
603
604 if (apparmor_load(handler) < 0)
605 goto out_warn_father;
606
607 if (lxc_seccomp_load(handler->conf) != 0)
608 goto out_warn_father;
609
610 if (run_lxc_hooks(handler->name, "start", handler->conf)) {
611 ERROR("failed to run start hooks for container '%s'.", handler->name);
612 goto out_warn_father;
613 }
614
615 /* The clearenv() and putenv() calls have been moved here
616 * to allow us to use enviroment variables passed to the various
617 * hooks, such as the start hook above. Not all of the
618 * variables like CONFIG_PATH or ROOTFS are valid in this
619 * context but others are. */
620 if (clearenv()) {
621 SYSERROR("failed to clear environment");
622 /* don't error out though */
623 }
624
625 if (putenv("container=lxc")) {
626 SYSERROR("failed to set environment variable");
627 goto out_warn_father;
628 }
629
630 close(handler->sigfd);
631
632 /* after this call, we are in error because this
633 * ops should not return as it execs */
634 handler->ops->start(handler, handler->data);
635
636 out_warn_father:
637 /* we want the parent to know something went wrong, so any
638 * value other than what it expects is ok. */
639 lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
640 return -1;
641 }
642
643 int save_phys_nics(struct lxc_conf *conf)
644 {
645 struct lxc_list *iterator;
646
647 lxc_list_for_each(iterator, &conf->network) {
648 struct lxc_netdev *netdev = iterator->elem;
649
650 if (netdev->type != LXC_NET_PHYS)
651 continue;
652 conf->saved_nics = realloc(conf->saved_nics,
653 (conf->num_savednics+1)*sizeof(struct saved_nic));
654 if (!conf->saved_nics) {
655 SYSERROR("failed to allocate memory");
656 return -1;
657 }
658 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
659 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
660 if (!conf->saved_nics[conf->num_savednics].orig_name) {
661 SYSERROR("failed to allocate memory");
662 return -1;
663 }
664 INFO("stored saved_nic #%d idx %d name %s\n", conf->num_savednics,
665 conf->saved_nics[conf->num_savednics].ifindex,
666 conf->saved_nics[conf->num_savednics].orig_name);
667 conf->num_savednics++;
668 }
669
670 return 0;
671 }
672
673
674 int lxc_spawn(struct lxc_handler *handler)
675 {
676 int failed_before_rename = 0;
677 const char *name = handler->name;
678 int pinfd;
679
680 if (lxc_sync_init(handler))
681 return -1;
682
683 handler->clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
684 if (!lxc_list_empty(&handler->conf->network)) {
685
686 handler->clone_flags |= CLONE_NEWNET;
687
688 /* Find gateway addresses from the link device, which is
689 * no longer accessible inside the container. Do this
690 * before creating network interfaces, since goto
691 * out_delete_net does not work before lxc_clone. */
692 if (lxc_find_gateway_addresses(handler)) {
693 ERROR("failed to find gateway addresses");
694 lxc_sync_fini(handler);
695 return -1;
696 }
697
698 /* that should be done before the clone because we will
699 * fill the netdev index and use them in the child
700 */
701 if (lxc_create_network(handler)) {
702 ERROR("failed to create the network");
703 lxc_sync_fini(handler);
704 return -1;
705 }
706 }
707
708 if (save_phys_nics(handler->conf)) {
709 ERROR("failed to save physical nic info");
710 goto out_abort;
711 }
712
713 /*
714 * if the rootfs is not a blockdev, prevent the container from
715 * marking it readonly.
716 */
717
718 pinfd = pin_rootfs(handler->conf->rootfs.path);
719 if (pinfd == -1) {
720 ERROR("failed to pin the container's rootfs");
721 goto out_abort;
722 }
723
724 /* Create a process in a new set of namespaces */
725 handler->pid = lxc_clone(do_start, handler, handler->clone_flags);
726 if (handler->pid < 0) {
727 SYSERROR("failed to fork into a new namespace");
728 goto out_delete_net;
729 }
730
731 lxc_sync_fini_child(handler);
732
733 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
734 failed_before_rename = 1;
735
736 if (lxc_cgroup_create(name, handler->pid))
737 goto out_delete_net;
738
739 if (failed_before_rename)
740 goto out_delete_net;
741
742 /* Create the network configuration */
743 if (handler->clone_flags & CLONE_NEWNET) {
744 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
745 ERROR("failed to create the configured network");
746 goto out_delete_net;
747 }
748 }
749
750 /* Tell the child to continue its initialization. we'll get
751 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
752 */
753 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
754 goto out_delete_net;
755
756 if (setup_cgroup(name, &handler->conf->cgroup)) {
757 ERROR("failed to setup the cgroups for '%s'", name);
758 goto out_delete_net;
759 }
760
761
762 /* Tell the child to complete its initialization and wait for
763 * it to exec or return an error. (the child will never
764 * return LXC_SYNC_POST_CGROUP+1. It will either close the
765 * sync pipe, causing lxc_sync_barrier_child to return
766 * success, or return a different value, causing us to error
767 * out).
768 */
769 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP))
770 return -1;
771
772 if (detect_shared_rootfs())
773 umount2(handler->conf->rootfs.mount, MNT_DETACH);
774
775 if (handler->ops->post_start(handler, handler->data))
776 goto out_abort;
777
778 if (lxc_set_state(name, handler, RUNNING)) {
779 ERROR("failed to set state to %s",
780 lxc_state2str(RUNNING));
781 goto out_abort;
782 }
783
784 lxc_sync_fini(handler);
785
786 if (pinfd >= 0)
787 close(pinfd);
788
789 return 0;
790
791 out_delete_net:
792 if (handler->clone_flags & CLONE_NEWNET)
793 lxc_delete_network(handler);
794 out_abort:
795 lxc_abort(name, handler);
796 lxc_sync_fini(handler);
797 return -1;
798 }
799
800 int __lxc_start(const char *name, struct lxc_conf *conf,
801 struct lxc_operations* ops, void *data)
802 {
803 struct lxc_handler *handler;
804 int err = -1;
805 int status;
806
807 handler = lxc_init(name, conf);
808 if (!handler) {
809 ERROR("failed to initialize the container");
810 return -1;
811 }
812 handler->ops = ops;
813 handler->data = data;
814
815 if (must_drop_cap_sys_boot()) {
816 #if HAVE_SYS_CAPABILITY_H
817 DEBUG("Dropping cap_sys_boot\n");
818 #else
819 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported\n");
820 #endif
821 } else {
822 DEBUG("Not dropping cap_sys_boot or watching utmp\n");
823 handler->conf->need_utmp_watch = 0;
824 }
825
826 err = lxc_spawn(handler);
827 if (err) {
828 ERROR("failed to spawn '%s'", name);
829 goto out_fini_nonet;
830 }
831
832 err = lxc_poll(name, handler);
833 if (err) {
834 ERROR("mainloop exited with an error");
835 goto out_abort;
836 }
837
838 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
839 continue;
840
841 /*
842 * If the child process exited but was not signaled,
843 * it didn't call reboot. This should mean it was an
844 * lxc-execute which simply exited. In any case, treat
845 * it as a 'halt'
846 */
847 if (WIFSIGNALED(status)) {
848 switch(WTERMSIG(status)) {
849 case SIGINT: /* halt */
850 DEBUG("Container halting");
851 break;
852 case SIGHUP: /* reboot */
853 DEBUG("Container rebooting");
854 handler->conf->reboot = 1;
855 break;
856 default:
857 DEBUG("unknown exit status for init: %d\n", WTERMSIG(status));
858 break;
859 }
860 }
861
862 lxc_rename_phys_nics_on_shutdown(handler->conf);
863
864 err = lxc_error_set_and_log(handler->pid, status);
865 out_fini:
866 lxc_delete_network(handler);
867
868 out_fini_nonet:
869 lxc_cgroup_destroy(name);
870 lxc_fini(name, handler);
871 return err;
872
873 out_abort:
874 lxc_abort(name, handler);
875 goto out_fini;
876 }
877
878 struct start_args {
879 char *const *argv;
880 };
881
882 static int start(struct lxc_handler *handler, void* data)
883 {
884 struct start_args *arg = data;
885
886 NOTICE("exec'ing '%s'", arg->argv[0]);
887
888 execvp(arg->argv[0], arg->argv);
889 SYSERROR("failed to exec %s", arg->argv[0]);
890 return 0;
891 }
892
893 static int post_start(struct lxc_handler *handler, void* data)
894 {
895 struct start_args *arg = data;
896
897 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
898 return 0;
899 }
900
901 static struct lxc_operations start_ops = {
902 .start = start,
903 .post_start = post_start
904 };
905
906 int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
907 {
908 struct start_args start_arg = {
909 .argv = argv,
910 };
911
912 if (lxc_check_inherited(conf, -1))
913 return -1;
914
915 conf->need_utmp_watch = 1;
916 return __lxc_start(name, conf, &start_ops, &start_arg);
917 }