From: Alexander Vladimirov Date: Tue, 12 Mar 2013 09:14:11 +0000 (+0800) Subject: Add lxc.stopsignal config option X-Git-Tag: lxc-0.9.0.rc1~2^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a84b99323ab760d67cf76ef2418313bfd4f8b3ca;p=thirdparty%2Flxc.git Add lxc.stopsignal config option I remember discussion about implementing proper way to shutdown guests using different signals, so here's a patch proposal. It allows to use specific signal numbers to shutdown guests gracefully, for example SIGRTMIN+4 starts poweroff.target in systemd. Signed-off-by: Alexander Vladimirov Acked-by: Serge E. Hallyn --- diff --git a/doc/lxc.conf.sgml.in b/doc/lxc.conf.sgml.in index 60e7baa76..b78402533 100644 --- a/doc/lxc.conf.sgml.in +++ b/doc/lxc.conf.sgml.in @@ -129,6 +129,29 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Stop signal + + Allows to specify signal name or number, sent by lxc-stop to + shutdown the container. Different init systems could use + different signals to perform clean shutdown sequence. Option + allows signal to be specified in kill(1) fashion, e.g. + SIGKILL, SIGRTMIN+14, SIGRTMAX-10 or plain number. + + + + + + + + + specify the signal used to stop the container + + + + + + Network diff --git a/src/lxc/conf.h b/src/lxc/conf.h index f20fb2fa6..61456ae18 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -277,6 +277,7 @@ struct lxc_conf { #endif int maincmd_fd; int autodev; // if 1, mount and fill a /dev at start + int stopsignal; // signal used to stop container char *rcfile; // Copy of the top level rcfile we read }; diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 59cedef46..0f68fe44d 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include #include #include @@ -87,6 +89,7 @@ static int config_seccomp(const char *, const char *, struct lxc_conf *); static int config_includefile(const char *, const char *, struct lxc_conf *); static int config_network_nic(const char *, const char *, struct lxc_conf *); static int config_autodev(const char *, const char *, struct lxc_conf *); +static int config_stopsignal(const char *, const char *, struct lxc_conf *); static struct lxc_config_t config[] = { @@ -134,6 +137,34 @@ static struct lxc_config_t config[] = { { "lxc.seccomp", config_seccomp }, { "lxc.include", config_includefile }, { "lxc.autodev", config_autodev }, + { "lxc.stopsignal", config_stopsignal }, +}; + +struct signame { + int num; + char *name; +}; + +struct signame signames[] = { + { SIGHUP, "HUP" }, + { SIGINT, "INT" }, + { SIGQUIT, "QUIT" }, + { SIGILL, "ILL" }, + { SIGABRT, "ABRT" }, + { SIGFPE, "FPE" }, + { SIGKILL, "KILL" }, + { SIGSEGV, "SEGV" }, + { SIGPIPE, "PIPE" }, + { SIGALRM, "ALRM" }, + { SIGTERM, "TERM" }, + { SIGUSR1, "USR1" }, + { SIGUSR2, "USR2" }, + { SIGCHLD, "CHLD" }, + { SIGCONT, "CONT" }, + { SIGSTOP, "STOP" }, + { SIGTSTP, "TSTP" }, + { SIGTTIN, "TTIN" }, + { SIGTTOU, "TTOU" }, }; static const size_t config_size = sizeof(config)/sizeof(struct lxc_config_t); @@ -959,6 +990,65 @@ static int config_autodev(const char *key, const char *value, return 0; } +static int sig_num(const char *sig) +{ + int n; + char *endp = NULL; + + errno = 0; + n = strtol(sig, &endp, 10); + if (sig == endp || n < 0 || errno != 0) + return -1; + return n; +} + +static int rt_sig_num(const char *signame) +{ + int sig_n = 0; + int rtmax = 0; + + if (strncasecmp(signame, "max-", 4) == 0) { + rtmax = 1; + } + signame += 4; + if (!isdigit(*signame)) + return -1; + sig_n = sig_num(signame); + sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n; + if (sig_n > SIGRTMAX || sig_n < SIGRTMIN) + return -1; + return sig_n; +} + +static int sig_parse(const char *signame) { + int n; + + if (isdigit(*signame)) { + return sig_num(signame); + } else if (strncasecmp(signame, "sig", 3) == 0) { + signame += 3; + if (strncasecmp(signame, "rt", 2) == 0) + return rt_sig_num(signame + 2); + for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) { + if (strcasecmp (signames[n].name, signame) == 0) + return signames[n].num; + } + } + return -1; +} + +static int config_stopsignal(const char *key, const char *value, + struct lxc_conf *lxc_conf) +{ + int sig_n = sig_parse(value); + + if (sig_n < 0) + return -1; + lxc_conf->stopsignal = sig_n; + + return 0; +} + static int config_cgroup(const char *key, const char *value, struct lxc_conf *lxc_conf) { diff --git a/src/lxc/stop.c b/src/lxc/stop.c index 851a4bf22..7fea6b637 100644 --- a/src/lxc/stop.c +++ b/src/lxc/stop.c @@ -34,6 +34,7 @@ #include #include +#include #include "lxc.h" #include "commands.h" @@ -82,9 +83,12 @@ extern int lxc_stop_callback(int fd, struct lxc_request *request, { struct lxc_answer answer; int ret; + int stopsignal = SIGKILL; + if (handler->conf->stopsignal) + stopsignal = handler->conf->stopsignal; memset(&answer, 0, sizeof(answer)); - answer.ret = kill(handler->pid, SIGKILL); + answer.ret = kill(handler->pid, stopsignal); if (!answer.ret) { ret = lxc_unfreeze_bypath(handler->cgroup); if (!ret)