From: Tycho Andersen Date: Fri, 19 Jan 2018 03:24:59 +0000 (+0000) Subject: remember the exit code from the init process X-Git-Tag: lxc-3.0.0.beta1~74^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd5177e9e36b36869c99ee8b850b9853a344b179;p=thirdparty%2Flxc.git remember the exit code from the init process error_num seems to be trying to remember the exit code of the init process, except that nothing actually keeps track of it anywhere. So, let's add a field to the handler, so that we can keep track of the process' exit status, and the propagate it to error_num in struct lxc_container so that people can use it. Note that this is a slight behavior change, essentially instead of making error_num always == the return code from start, now it contains slightly more useful information (the actual exit status). But, there is only one internal user of error_num which I'll fix in later in the series, so IMO this is ok. Signed-off-by: Tycho Andersen --- diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 2a4bb51f3..f6f07bac1 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -1027,7 +1027,7 @@ reboot: ret = lxc_execute(c->name, argv, 1, handler, c->config_path, daemonize); else ret = lxc_start(c->name, argv, handler, c->config_path, daemonize); - c->error_num = ret; + c->error_num = handler->exit_status; if (conf->reboot == 1) { INFO("Container requested reboot"); diff --git a/src/lxc/start.c b/src/lxc/start.c index 8b4144239..00e255a69 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -323,6 +323,27 @@ static int signal_handler(int fd, uint32_t events, void *data, if (ret == 0 && info.si_pid == hdlr->pid) hdlr->init_died = true; + /* Try to figure out a reasonable exit status to report. */ + if (hdlr->init_died) { + switch (info.si_code) { + case CLD_EXITED: + hdlr->exit_status = info.si_status << 8; + break; + case CLD_KILLED: + case CLD_DUMPED: + case CLD_STOPPED: + hdlr->exit_status = info.si_status << 8 | 0x7f; + break; + case CLD_CONTINUED: + /* Huh? The waitid() told us it's dead *and* continued? */ + WARN("Init %d dead and continued?", hdlr->pid); + hdlr->exit_status = 1; + break; + default: + ERROR("Unknown si_code: %d", hdlr->init_died); + } + } + /* More robustness, protect ourself from a SIGCHLD sent * by a process different from the container init. */ diff --git a/src/lxc/start.h b/src/lxc/start.h index 503154045..f2a2630e3 100644 --- a/src/lxc/start.h +++ b/src/lxc/start.h @@ -109,6 +109,11 @@ struct lxc_handler { /* Current state of the container. */ lxc_state_t state; + + /* The exit status of the container; not defined unless ->init_died == + * true. + */ + int exit_status; }; struct lxc_operations {