#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;
unsigned n_fifos;
DBusConnection *bus;
+
+ bool quit;
} Server;
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;
}
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.");
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 signal message.");
+ log_error("Could not attach target and flag information to message.");
goto finish;
}
if (!(reply = dbus_connection_send_with_reply_and_block(s->bus, m, -1, &error))) {
- log_error("Failed to start unit: %s", error.message);
+ log_error("Failed to start unit: %s", bus_error_message(&error));
goto finish;
}
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:
close_nointr_nofail(s->epoll_fd);
if (s->bus) {
- dbus_connection_close(s->bus);
- dbus_connection_unref(s->bus);
+ dbus_connection_flush(s->bus);
+ dbus_connection_close(s->bus);
+ dbus_connection_unref(s->bus);
}
}
}
if (bus_connect(DBUS_BUS_SYSTEM, &s->bus, NULL, &error) < 0) {
- log_error("Failed to get D-Bus connection: %s", error.message);
+ log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
goto fail;
}
int main(int argc, char *argv[]) {
Server server;
- int r = 3, n;
+ int r = EXIT_FAILURE, n;
if (getppid() != 1) {
log_error("This program should be invoked by init only.");
- return 1;
+ return EXIT_FAILURE;
}
if (argc > 1) {
log_error("This program does not take arguments.");
- return 1;
+ return EXIT_FAILURE;
}
log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
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 1;
+ return EXIT_FAILURE;
}
if (n <= 0 || n > SERVER_FD_MAX) {
log_error("No or too many file descriptors passed.");
- return 2;
+ return EXIT_FAILURE;
}
if (server_init(&server, (unsigned) n) < 0)
- return 2;
+ return EXIT_FAILURE;
log_debug("systemd-initctl running as pid %lu", (unsigned long) getpid());
"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;
goto fail;
}
- r = 0;
+ r = EXIT_SUCCESS;
log_debug("systemd-initctl stopped as pid %lu", (unsigned long) getpid());