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