* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include "../config.h"
+#include "config.h"
+
#include <stdio.h>
#undef _GNU_SOURCE
#include <string.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/mount.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <sys/prctl.h>
+#include <sys/types.h>
#include <sys/capability.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/poll.h>
-#ifdef HAVE_SYS_SIGNALFD_H
+#ifdef HAVE_SYS_SIGNALFD_H
# include <sys/signalfd.h>
#else
+/* assume kernel headers are too old */
+#include <stdint.h>
+struct signalfd_siginfo
+{
+ uint32_t ssi_signo;
+ int32_t ssi_errno;
+ int32_t ssi_code;
+ uint32_t ssi_pid;
+ uint32_t ssi_uid;
+ int32_t ssi_fd;
+ uint32_t ssi_tid;
+ uint32_t ssi_band;
+ uint32_t ssi_overrun;
+ uint32_t ssi_trapno;
+ int32_t ssi_status;
+ int32_t ssi_int;
+ uint64_t ssi_ptr;
+ uint64_t ssi_utime;
+ uint64_t ssi_stime;
+ uint64_t ssi_addr;
+ uint8_t __pad[48];
+};
+
# ifndef __NR_signalfd4
/* assume kernel headers are too old */
# if __i386__
#define PR_CAPBSET_DROP 24
#endif
+#include "start.h"
+#include "conf.h"
+#include "log.h"
+#include "cgroup.h"
#include "error.h"
#include "af_unix.h"
#include "mainloop.h"
-
-#include <lxc/lxc.h>
-#include <lxc/log.h>
+#include "utils.h"
+#include "utmp.h"
+#include "monitor.h"
+#include "commands.h"
+#include "console.h"
+#include "sync.h"
+#include "namespace.h"
lxc_log_define(lxc_start, lxc);
-LXC_TTY_HANDLER(SIGINT);
-LXC_TTY_HANDLER(SIGQUIT);
+static int match_fd(int fd)
+{
+ return (fd == 0 || fd == 1 || fd == 2);
+}
-static int setup_sigchld_fd(sigset_t *oldmask)
+int lxc_check_inherited(int fd_to_ignore)
{
- sigset_t mask;
- int fd;
+ struct dirent dirent, *direntp;
+ int fd, fddir;
+ DIR *dir;
- if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
- SYSERROR("failed to get mask signal");
+ dir = opendir("/proc/self/fd");
+ if (!dir) {
+ WARN("failed to open directory: %m");
return -1;
}
- if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
- SYSERROR("failed to set mask signal");
+ fddir = dirfd(dir);
+
+ while (!readdir_r(dir, &dirent, &direntp)) {
+ if (!direntp)
+ break;
+
+ if (!strcmp(direntp->d_name, "."))
+ continue;
+
+ if (!strcmp(direntp->d_name, ".."))
+ continue;
+
+ fd = atoi(direntp->d_name);
+
+ if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
+ continue;
+
+ if (match_fd(fd))
+ continue;
+
+ WARN("inherited fd %d", fd);
+ }
+
+ closedir(dir); /* cannot fail */
+ return 0;
+}
+
+static int setup_signal_fd(sigset_t *oldmask)
+{
+ sigset_t mask;
+ int fd;
+
+ /* Block everything except serious error signals */
+ if (sigfillset(&mask) ||
+ sigdelset(&mask, SIGILL) ||
+ sigdelset(&mask, SIGSEGV) ||
+ sigdelset(&mask, SIGBUS) ||
+ sigprocmask(SIG_BLOCK, &mask, oldmask)) {
+ SYSERROR("failed to set signal mask");
return -1;
}
return -1;
}
+ DEBUG("sigchild handler set");
+
return fd;
}
-static int setup_tty_service(const char *name, int *ttyfd)
+static int signal_handler(int fd, void *data,
+ struct lxc_epoll_descr *descr)
{
- int fd;
- struct sockaddr_un addr = { 0 };
- char *offset = &addr.sun_path[1];
-
- strcpy(offset, name);
- addr.sun_path[0] = '\0';
-
- fd = lxc_af_unix_open(addr.sun_path, SOCK_STREAM, 0);
- if (fd < 0)
+ struct signalfd_siginfo siginfo;
+ int ret;
+ pid_t *pid = data;
+
+ ret = read(fd, &siginfo, sizeof(siginfo));
+ if (ret < 0) {
+ ERROR("failed to read signal info");
return -1;
+ }
- if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
- SYSERROR("failed to close-on-exec flag");
- close(fd);
+ if (ret != sizeof(siginfo)) {
+ ERROR("unexpected siginfo size");
return -1;
}
- *ttyfd = fd;
+ if (siginfo.ssi_signo != SIGCHLD) {
+ kill(*pid, siginfo.ssi_signo);
+ INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
+ return 0;
+ }
- return 0;
-}
+ if (siginfo.ssi_code == CLD_STOPPED ||
+ siginfo.ssi_code == CLD_CONTINUED) {
+ INFO("container init process was stopped/continued");
+ return 0;
+ }
-static int sigchld_handler(int fd, void *data,
- struct lxc_epoll_descr *descr)
-{
+ /* more robustness, protect ourself from a SIGCHLD sent
+ * by a process different from the container init
+ */
+ if (siginfo.ssi_pid != *pid) {
+ WARN("invalid pid for SIGCHLD");
+ return 0;
+ }
+
+ DEBUG("container init process exited");
return 1;
}
-static int ttyclient_handler(int fd, void *data,
- struct lxc_epoll_descr *descr)
+int lxc_pid_callback(int fd, struct lxc_request *request,
+ struct lxc_handler *handler)
{
- int i;
- struct lxc_tty_info *tty_info = data;
+ struct lxc_answer answer;
+ int ret;
- for (i = 0; i < tty_info->nbtty; i++) {
+ answer.pid = handler->pid;
+ answer.ret = 0;
- if (tty_info->pty_info[i].busy != fd)
- continue;
+ ret = send(fd, &answer, sizeof(answer), 0);
+ if (ret < 0) {
+ WARN("failed to send answer to the peer");
+ return -1;
+ }
- lxc_mainloop_del_handler(descr, fd);
- tty_info->pty_info[i].busy = 0;
- close(fd);
+ if (ret != sizeof(answer)) {
+ ERROR("partial answer sent");
+ return -1;
}
return 0;
}
-static int ttyservice_handler(int fd, void *data,
- struct lxc_epoll_descr *descr)
+int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
{
- int conn, ttynum, val = 1, ret = -1;
- struct lxc_tty_info *tty_info = data;
-
- conn = accept(fd, NULL, 0);
- if (conn < 0) {
- SYSERROR("failed to accept tty client");
- return -1;
- }
-
- if (setsockopt(conn, SOL_SOCKET, SO_PASSCRED, &val, sizeof(val))) {
- SYSERROR("failed to enable credential on socket");
- goto out_close;
- }
-
- if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum)))
- goto out_close;
-
- if (ttynum > 0) {
- if (ttynum > tty_info->nbtty)
- goto out_close;
-
- if (tty_info->pty_info[ttynum - 1].busy)
- goto out_close;
-
- goto out_send;
- }
-
- /* fixup index tty1 => [0] */
- for (ttynum = 1;
- ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy;
- ttynum++);
-
- /* we didn't find any available slot for tty */
- if (ttynum > tty_info->nbtty)
- goto out_close;
-
-out_send:
- if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum - 1].master,
- &ttynum, sizeof(ttynum)) < 0) {
- ERROR("failed to send tty to client");
- goto out_close;
- }
-
- if (lxc_mainloop_add_handler(descr, conn,
- ttyclient_handler, tty_info)) {
- ERROR("failed to add tty client handler");
- goto out_close;
- }
-
- tty_info->pty_info[ttynum - 1].busy = conn;
-
- ret = 0;
-out:
- return ret;
-out_close:
- close(conn);
- goto out;
+ handler->state = state;
+ lxc_monitor_send_state(name, state);
+ return 0;
}
int lxc_poll(const char *name, struct lxc_handler *handler)
{
int sigfd = handler->sigfd;
int pid = handler->pid;
- const struct lxc_tty_info *tty_info = &handler->tty_info;
-
- int nfds, ttyfd = -1, ret = -1;
struct lxc_epoll_descr descr;
- if (tty_info->nbtty && setup_tty_service(name, &ttyfd)) {
- ERROR("failed to create the tty service point");
+ if (lxc_mainloop_open(&descr)) {
+ ERROR("failed to create mainloop");
goto out_sigfd;
}
- /* sigfd + nb tty + tty service
- * if tty is enabled */
- nfds = tty_info->nbtty + 1 + tty_info->nbtty ? 1 : 0;
+ if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
+ ERROR("failed to add handler for the signal");
+ goto out_mainloop_open;
+ }
- if (lxc_mainloop_open(nfds, &descr)) {
- ERROR("failed to create mainloop");
- goto out_ttyfd;
+ if (lxc_console_mainloop_add(&descr, handler)) {
+ ERROR("failed to add console handler to mainloop");
+ goto out_mainloop_open;
}
- if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
- ERROR("failed to add handler for the signal");
+ if (lxc_command_mainloop_add(name, &descr, handler)) {
+ ERROR("failed to add command handler to mainloop");
goto out_mainloop_open;
}
- if (tty_info->nbtty) {
- if (lxc_mainloop_add_handler(&descr, ttyfd,
- ttyservice_handler,
- (void *)tty_info)) {
- ERROR("failed to add handler for the tty");
+ if (handler->conf->need_utmp_watch) {
+ if (lxc_utmp_mainloop_add(&descr, handler)) {
+ ERROR("failed to add utmp handler to mainloop");
goto out_mainloop_open;
}
}
- ret = lxc_mainloop(&descr);
-
-out:
- return ret;
+ return lxc_mainloop(&descr);
out_mainloop_open:
lxc_mainloop_close(&descr);
-out_ttyfd:
- close(ttyfd);
out_sigfd:
close(sigfd);
- goto out;
+ return -1;
}
-static int save_init_pid(const char *name, pid_t pid)
-{
- char init[MAXPATHLEN];
- char *val;
- int fd, err = -1;
-
- snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
-
- if (!asprintf(&val, "%d\n", pid)) {
- SYSERROR("failed to allocate memory");
- goto out;
- }
-
- fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
- if (fd < 0) {
- SYSERROR("failed to open '%s'", init);
- goto out_free;
- }
-
- if (write(fd, val, strlen(val)) < 0) {
- SYSERROR("failed to write the init pid");
- goto out_close;
- }
-
- err = 0;
-
-out_close:
- close(fd);
-out_free:
- free(val);
-out:
- return err;
-}
+extern int lxc_caps_check(void);
-static void remove_init_pid(const char *name, pid_t pid)
+struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
{
- char init[MAXPATHLEN];
+ struct lxc_handler *handler;
- snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
- unlink(init);
-}
+ if (!lxc_caps_check()) {
+ ERROR("Not running with sufficient privilege");
+ return NULL;
+ }
-int lxc_init(const char *name, struct lxc_handler *handler)
-{
- int err = -1;
+ handler = malloc(sizeof(*handler));
+ if (!handler)
+ return NULL;
memset(handler, 0, sizeof(*handler));
- handler->lock = lxc_get_lock(name);
- if (handler->lock < 0)
- goto out;
+ handler->conf = conf;
+
+ handler->name = strdup(name);
+ if (!handler->name) {
+ ERROR("failed to allocate memory");
+ goto out_free;
+ }
/* Begin the set the state to STARTING*/
- if (lxc_setstate(name, STARTING)) {
+ if (lxc_set_state(name, handler, STARTING)) {
ERROR("failed to set state '%s'", lxc_state2str(STARTING));
- goto out_put_lock;
+ goto out_free_name;
}
- /* If we are not attached to a tty, disable it */
- if (ttyname_r(0, handler->tty, sizeof(handler->tty)))
- handler->tty[0] = '\0';
-
- if (lxc_create_tty(name, &handler->tty_info)) {
+ if (lxc_create_tty(name, conf)) {
ERROR("failed to create the ttys");
goto out_aborting;
}
+ if (lxc_create_console(conf)) {
+ ERROR("failed to create console");
+ goto out_delete_tty;
+ }
+
/* the signal fd has to be created before forking otherwise
* if the child process exits before we setup the signal fd,
* the event will be lost and the command will be stuck */
- handler->sigfd = setup_sigchld_fd(&handler->oldmask);
+ handler->sigfd = setup_signal_fd(&handler->oldmask);
if (handler->sigfd < 0) {
ERROR("failed to set sigchild fd handler");
- goto out_delete_tty;
+ goto out_delete_console;
}
- /* Avoid signals from terminal */
- LXC_TTY_ADD_HANDLER(SIGINT);
- LXC_TTY_ADD_HANDLER(SIGQUIT);
-
- err = 0;
-out:
- return err;
+ INFO("'%s' is initialized", name);
+ return handler;
+out_delete_console:
+ lxc_delete_console(&conf->console);
out_delete_tty:
- lxc_delete_tty(&handler->tty_info);
+ lxc_delete_tty(&conf->tty_info);
out_aborting:
- lxc_setstate(name, ABORTING);
-out_put_lock:
- lxc_put_lock(handler->lock);
- goto out;
+ lxc_set_state(name, handler, ABORTING);
+out_free_name:
+ free(handler->name);
+ handler->name = NULL;
+out_free:
+ free(handler);
+ return NULL;
}
void lxc_fini(const char *name, struct lxc_handler *handler)
/* The STOPPING state is there for future cleanup code
* which can take awhile
*/
- lxc_setstate(name, STOPPING);
-
- lxc_setstate(name, STOPPED);
-
- remove_init_pid(name, handler->pid);
-
- lxc_delete_tty(&handler->tty_info);
+ lxc_set_state(name, handler, STOPPING);
+ lxc_set_state(name, handler, STOPPED);
- lxc_unlink_nsgroup(name);
+ /* reset mask set by setup_signal_fd */
+ if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
+ WARN("failed to restore sigprocmask");
- lxc_put_lock(handler->lock);
-
- LXC_TTY_DEL_HANDLER(SIGQUIT);
- LXC_TTY_DEL_HANDLER(SIGINT);
+ lxc_delete_console(&handler->conf->console);
+ lxc_delete_tty(&handler->conf->tty_info);
+ free(handler->name);
+ free(handler);
}
void lxc_abort(const char *name, struct lxc_handler *handler)
{
- lxc_setstate(name, ABORTING);
- kill(handler->pid, SIGKILL);
+ lxc_set_state(name, handler, ABORTING);
+ if (handler->pid > 0)
+ kill(handler->pid, SIGKILL);
}
-int lxc_spawn(const char *name, struct lxc_handler *handler, char *argv[])
+#include <sys/reboot.h>
+#include <linux/reboot.h>
+
+static int must_drop_cap_sys_boot(void)
{
- int sv[2];
- int clone_flags;
- int err = -1, sync;
+ FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
+ int ret;
+ int v;
+
+ if (!f)
+ return 1;
+
+ ret = fscanf(f, "%d", &v);
+ fclose(f);
+ if (ret != 1)
+ return 1;
+ ret = reboot(v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF);
+ if (ret != -1)
+ return 1;
+ return 0;
+}
- /* Synchro socketpair */
- if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
- SYSERROR("failed to create communication socketpair");
- goto out;
- }
+static int do_start(void *data)
+{
+ struct lxc_handler *handler = data;
- clone_flags = CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
- if (conf_has_utsname(name))
- clone_flags |= CLONE_NEWUTS;
- if (conf_has_network(name))
- clone_flags |= CLONE_NEWNET;
+ if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
+ SYSERROR("failed to set sigprocmask");
+ return -1;
+ }
- /* Create a process in a new set of namespaces */
- handler->pid = fork_ns(clone_flags);
- if (handler->pid < 0) {
- SYSERROR("failed to fork into a new namespace");
- goto out_close;
+ /* This prctl must be before the synchro, so if the parent
+ * dies before we set the parent death signal, we will detect
+ * its death with the synchro right after, otherwise we have
+ * a window where the parent can exit before we set the pdeath
+ * signal leading to a unsupervized container.
+ */
+ if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
+ SYSERROR("failed to set pdeath signal");
+ return -1;
}
- if (!handler->pid) {
+ lxc_sync_fini_parent(handler);
+
+ /* Tell the parent task it can begin to configure the
+ * container and wait for it to finish
+ */
+ if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
+ return -1;
+
+ /* Setup the container, ip, names, utsname, ... */
+ if (lxc_setup(handler->name, handler->conf)) {
+ ERROR("failed to setup the container");
+ goto out_warn_father;
+ }
- if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
- SYSERROR("failed to set sigprocmask");
+ if (must_drop_cap_sys_boot()) {
+ if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
+ SYSERROR("failed to remove CAP_SYS_BOOT capability");
return -1;
}
+ handler->conf->need_utmp_watch = 1;
+ } else
+ handler->conf->need_utmp_watch = 0;
- close(sv[1]);
+ close(handler->sigfd);
- /* Be sure we don't inherit this after the exec */
- fcntl(sv[0], F_SETFD, FD_CLOEXEC);
-
- /* Tell our father he can begin to configure the container */
- if (write(sv[0], &sync, sizeof(sync)) < 0) {
- SYSERROR("failed to write socket");
- goto out_child;
- }
+ /* after this call, we are in error because this
+ * ops should not return as it execs */
+ if (handler->ops->start(handler, handler->data))
+ return -1;
- /* Wait for the father to finish the configuration */
- if (read(sv[0], &sync, sizeof(sync)) < 0) {
- SYSERROR("failed to read socket");
- goto out_child;
- }
+out_warn_father:
+ lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
+ return -1;
+}
- /* Setup the container, ip, names, utsname, ... */
- err = lxc_setup(name, handler->tty, &handler->tty_info);
- if (err) {
- ERROR("failed to setup the container");
- if (write(sv[0], &err, sizeof(err)) < 0)
- SYSERROR("failed to write the socket");
- goto out_child;
- }
+int lxc_spawn(struct lxc_handler *handler)
+{
+ int clone_flags;
+ int failed_before_rename = 0;
+ const char *name = handler->name;
- if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
- SYSERROR("failed to remove CAP_SYS_BOOT capability");
- goto out_child;
- }
+ if (lxc_sync_init(handler))
+ return -1;
- execvp(argv[0], argv);
- SYSERROR("failed to exec %s", argv[0]);
+ clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
+ if (!lxc_list_empty(&handler->conf->network)) {
- err = LXC_ERROR_WRONG_COMMAND;
- /* If the exec fails, tell that to our father */
- if (write(sv[0], &err, sizeof(err)) < 0)
- SYSERROR("failed to write the socket");
-
- out_child:
- exit(err);
+ clone_flags |= CLONE_NEWNET;
+
+ /* Find gateway addresses from the link device, which is
+ * no longer accessible inside the container. Do this
+ * before creating network interfaces, since goto
+ * out_delete_net does not work before lxc_clone. */
+ if (lxc_find_gateway_addresses(handler)) {
+ ERROR("failed to find gateway addresses");
+ lxc_sync_fini(handler);
+ return -1;
+ }
+
+ /* that should be done before the clone because we will
+ * fill the netdev index and use them in the child
+ */
+ if (lxc_create_network(handler)) {
+ ERROR("failed to create the network");
+ lxc_sync_fini(handler);
+ return -1;
+ }
}
- close(sv[0]);
-
- /* Wait for the child to be ready */
- if (read(sv[1], &sync, sizeof(sync)) < 0) {
- SYSERROR("failed to read the socket");
- goto out_abort;
+ /* Create a process in a new set of namespaces */
+ handler->pid = lxc_clone(do_start, handler, clone_flags);
+ if (handler->pid < 0) {
+ SYSERROR("failed to fork into a new namespace");
+ goto out_delete_net;
}
- if (lxc_link_nsgroup(name, handler->pid))
- WARN("cgroupfs not found: cgroup disabled");
+ lxc_sync_fini_child(handler);
- /* Create the network configuration */
- if (clone_flags & CLONE_NEWNET && conf_create_network(name, handler->pid)) {
- ERROR("failed to create the configured network");
- goto out_abort;
- }
+ if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
+ failed_before_rename = 1;
- /* Tell the child to continue its initialization */
- if (write(sv[1], &sync, sizeof(sync)) < 0) {
- SYSERROR("failed to write the socket");
- goto out_abort;
- }
+ if (lxc_cgroup_create(name, handler->pid))
+ goto out_delete_net;
- /* Wait for the child to exec or returning an error */
- err = read(sv[1], &sync, sizeof(sync));
- if (err < 0) {
- ERROR("failed to read the socket");
- goto out_abort;
+ if (failed_before_rename)
+ goto out_delete_net;
+
+ /* Create the network configuration */
+ if (clone_flags & CLONE_NEWNET) {
+ if (lxc_assign_network(&handler->conf->network, handler->pid)) {
+ ERROR("failed to create the configured network");
+ goto out_delete_net;
+ }
}
- if (save_init_pid(name, handler->pid)) {
- ERROR("failed to save the init pid info");
+ /* Tell the child to continue its initialization and wait for
+ * it to exec or return an error
+ */
+ if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
+ return -1;
+
+ if (handler->ops->post_start(handler, handler->data))
goto out_abort;
- }
- if (lxc_setstate(name, RUNNING)) {
+ if (lxc_set_state(name, handler, RUNNING)) {
ERROR("failed to set state to %s",
lxc_state2str(RUNNING));
goto out_abort;
}
- err = 0;
-
-out_close:
- close(sv[0]);
- close(sv[1]);
-out:
- return err;
+ lxc_sync_fini(handler);
+ return 0;
+out_delete_net:
+ if (clone_flags & CLONE_NEWNET)
+ lxc_delete_network(&handler->conf->network);
out_abort:
lxc_abort(name, handler);
- goto out_close;
+ lxc_sync_fini(handler);
+ return -1;
}
-int lxc_start(const char *name, char *argv[])
+int __lxc_start(const char *name, struct lxc_conf *conf,
+ struct lxc_operations* ops, void *data)
{
- struct lxc_handler handler = { 0 };
- int err = -LXC_ERROR_INTERNAL;
+ struct lxc_handler *handler;
+ int err = -1;
int status;
- if (lxc_init(name, &handler)) {
+ handler = lxc_init(name, conf);
+ if (!handler) {
ERROR("failed to initialize the container");
- goto out;
+ return -1;
}
+ handler->ops = ops;
+ handler->data = data;
- err = lxc_spawn(name, &handler, argv);
+ err = lxc_spawn(handler);
if (err) {
- ERROR("failed to spawn '%s'", argv[0]);
- goto out;
+ ERROR("failed to spawn '%s'", name);
+ goto out_fini;
}
- if (lxc_poll(name, &handler)) {
+ err = lxc_poll(name, handler);
+ if (err) {
ERROR("mainloop exited with an error");
goto out_abort;
}
- while (waitpid(handler.pid, &status, 0) < 0 && errno == EINTR)
+ while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
continue;
- err = 0;
-out:
- lxc_fini(name, &handler);
+
+ if (!WIFSIGNALED(status)) {
+ printf("child process exited but was not signaled\n");
+ return -1;
+ }
+
+ switch(WTERMSIG(status)) {
+ case SIGINT: /* halt */
+ DEBUG("Container halting");
+ break;
+ case SIGHUP: /* reboot */
+ DEBUG("Container rebooting");
+ handler->conf->reboot = 1;
+ break;
+ default:
+ DEBUG("unknown exit status for init: %d\n", WTERMSIG(status));
+ break;
+ }
+
+ err = lxc_error_set_and_log(handler->pid, status);
+out_fini:
+ lxc_cgroup_destroy(name);
+ lxc_fini(name, handler);
return err;
out_abort:
- lxc_abort(name, &handler);
- goto out;
+ lxc_abort(name, handler);
+ goto out_fini;
+}
+
+struct start_args {
+ char *const *argv;
+};
+
+static int start(struct lxc_handler *handler, void* data)
+{
+ struct start_args *arg = data;
+
+ NOTICE("exec'ing '%s'", arg->argv[0]);
+
+ execvp(arg->argv[0], arg->argv);
+ SYSERROR("failed to exec %s", arg->argv[0]);
+ return 0;
+}
+
+static int post_start(struct lxc_handler *handler, void* data)
+{
+ struct start_args *arg = data;
+
+ NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
+ return 0;
+}
+
+static struct lxc_operations start_ops = {
+ .start = start,
+ .post_start = post_start
+};
+
+int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
+{
+ struct start_args start_arg = {
+ .argv = argv,
+ };
+
+ if (lxc_check_inherited(-1))
+ return -1;
+
+ conf->need_utmp_watch = 1;
+ return __lxc_start(name, conf, &start_ops, &start_arg);
}