X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Finitctl.c;h=e2189e9ee171e8ccf0f997f823badff2c55d5116;hp=10db2dda188e95eae7d6ae814a0ea8c35fb95381;hb=57020a3abff20f176e9f0cbb982d7977119d6f08;hpb=5d452f9ceceabf2bf69acb58e69a41fb41011e37 diff --git a/src/initctl.c b/src/initctl.c index 10db2dda1..e2189e9ee 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -34,17 +34,18 @@ #include #include +#include #include "util.h" #include "log.h" #include "list.h" #include "initreq.h" #include "special.h" -#include "sd-daemon.h" #include "dbus-common.h" +#include "def.h" #define SERVER_FD_MAX 16 -#define TIMEOUT ((int) (10*MSEC_PER_SEC)) +#define TIMEOUT_MSEC ((int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)) typedef struct Fifo Fifo; @@ -55,6 +56,8 @@ typedef struct Server { unsigned n_fifos; DBusConnection *bus; + + bool quit; } Server; struct Fifo { @@ -68,27 +71,34 @@ struct Fifo { LIST_FIELDS(Fifo, fifo); }; -static const char *translate_runlevel(int runlevel) { +static const char *translate_runlevel(int runlevel, bool *isolate) { static const struct { const int runlevel; const char *special; + bool isolate; } table[] = { - { '0', SPECIAL_POWEROFF_TARGET }, - { '1', SPECIAL_RESCUE_TARGET }, - { 's', SPECIAL_RESCUE_TARGET }, - { 'S', SPECIAL_RESCUE_TARGET }, - { '2', SPECIAL_RUNLEVEL2_TARGET }, - { '3', SPECIAL_RUNLEVEL3_TARGET }, - { '4', SPECIAL_RUNLEVEL4_TARGET }, - { '5', SPECIAL_RUNLEVEL5_TARGET }, - { '6', SPECIAL_REBOOT_TARGET }, + { '0', SPECIAL_POWEROFF_TARGET, false }, + { '1', SPECIAL_RESCUE_TARGET, true }, + { 's', SPECIAL_RESCUE_TARGET, true }, + { 'S', SPECIAL_RESCUE_TARGET, true }, + { '2', SPECIAL_RUNLEVEL2_TARGET, true }, + { '3', SPECIAL_RUNLEVEL3_TARGET, true }, + { '4', SPECIAL_RUNLEVEL4_TARGET, true }, + { '5', SPECIAL_RUNLEVEL5_TARGET, true }, + { '6', SPECIAL_REBOOT_TARGET, false }, }; unsigned i; + assert(isolate); + for (i = 0; i < ELEMENTSOF(table); i++) - if (table[i].runlevel == runlevel) + if (table[i].runlevel == runlevel) { + *isolate = table[i].isolate; + if (runlevel == '6' && kexec_loaded()) + return SPECIAL_KEXEC_TARGET; return table[i].special; + } return NULL; } @@ -97,18 +107,24 @@ static void change_runlevel(Server *s, int runlevel) { const char *target; DBusMessage *m = NULL, *reply = NULL; DBusError error; - const char *replace = "replace"; + const char *mode; + bool isolate = false; assert(s); dbus_error_init(&error); - if (!(target = translate_runlevel(runlevel))) { + if (!(target = translate_runlevel(runlevel, &isolate))) { log_warning("Got request for unknown runlevel %c, ignoring.", runlevel); goto finish; } - log_debug("Running request %s", target); + if (isolate) + mode = "isolate"; + else + mode = "replace"; + + log_debug("Running request %s/start/%s", target, mode); if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit"))) { log_error("Could not allocate message."); @@ -117,7 +133,7 @@ static void change_runlevel(Server *s, int runlevel) { if (!dbus_message_append_args(m, DBUS_TYPE_STRING, &target, - DBUS_TYPE_STRING, &replace, + DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID)) { log_error("Could not attach target and flag information to message."); goto finish; @@ -153,7 +169,31 @@ static void request_process(Server *s, const struct init_request *req) { if (!isprint(req->runlevel)) log_error("Got invalid runlevel. Ignoring."); else - change_runlevel(s, req->runlevel); + switch (req->runlevel) { + + /* we are async anyway, so just use kill for reexec/reload */ + case 'u': + case 'U': + if (kill(1, SIGTERM) < 0) + log_error("kill() failed: %m"); + + /* The bus connection will be + * terminated if PID 1 is reexecuted, + * hence let's just exit here, and + * rely on that we'll be restarted on + * the next request */ + s->quit = true; + break; + + case 'q': + case 'Q': + if (kill(1, SIGHUP) < 0) + log_error("kill() failed: %m"); + break; + + default: + change_runlevel(s, req->runlevel); + } return; case INIT_CMD_POWERFAIL: @@ -352,6 +392,8 @@ int main(int argc, char *argv[]) { log_parse_environment(); log_open(); + umask(0022); + if ((n = sd_listen_fds(true)) < 0) { log_error("Failed to read listening file descriptors from environment: %s", strerror(-r)); return EXIT_FAILURE; @@ -371,13 +413,13 @@ int main(int argc, char *argv[]) { "READY=1\n" "STATUS=Processing requests..."); - for (;;) { + while (!server.quit) { struct epoll_event event; int k; if ((k = epoll_wait(server.epoll_fd, &event, 1, - TIMEOUT)) < 0) { + TIMEOUT_MSEC)) < 0) { if (errno == EINTR) continue;