1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
23 #include <sys/types.h>
31 #include <sys/epoll.h>
36 #include <dbus/dbus.h>
43 #include "sd-daemon.h"
45 #define SERVER_FD_MAX 16
46 #define TIMEOUT ((int) (10*MSEC_PER_SEC))
48 typedef struct Fifo Fifo;
50 typedef struct Server {
53 LIST_HEAD(Fifo, fifos);
64 struct init_request buffer;
67 LIST_FIELDS(Fifo, fifo);
70 static const char *translate_runlevel(int runlevel) {
75 { '0', SPECIAL_RUNLEVEL0_TARGET },
76 { '1', SPECIAL_RUNLEVEL1_TARGET },
77 { 's', SPECIAL_RUNLEVEL1_TARGET },
78 { 'S', SPECIAL_RUNLEVEL1_TARGET },
79 { '2', SPECIAL_RUNLEVEL2_TARGET },
80 { '3', SPECIAL_RUNLEVEL3_TARGET },
81 { '4', SPECIAL_RUNLEVEL4_TARGET },
82 { '5', SPECIAL_RUNLEVEL5_TARGET },
83 { '6', SPECIAL_RUNLEVEL6_TARGET },
88 for (i = 0; i < ELEMENTSOF(table); i++)
89 if (table[i].runlevel == runlevel)
90 return table[i].special;
95 static void change_runlevel(Server *s, int runlevel) {
97 DBusMessage *m = NULL, *reply = NULL;
99 const char *path, *replace = "isolate";
103 dbus_error_init(&error);
105 if (!(target = translate_runlevel(runlevel))) {
106 log_warning("Got request for unknown runlevel %c, ignoring.", runlevel);
110 log_debug("Running request %s", target);
112 if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "LoadUnit"))) {
113 log_error("Could not allocate message.");
117 if (!dbus_message_append_args(m,
118 DBUS_TYPE_STRING, &target,
119 DBUS_TYPE_INVALID)) {
120 log_error("Could not attach group information to signal message.");
124 if (!(reply = dbus_connection_send_with_reply_and_block(s->bus, m, -1, &error))) {
125 log_error("Failed to get unit path: %s", error.message);
129 if (!dbus_message_get_args(reply, &error,
130 DBUS_TYPE_OBJECT_PATH, &path,
131 DBUS_TYPE_INVALID)) {
132 log_error("Failed to parse unit path: %s", error.message);
136 dbus_message_unref(m);
137 if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", path, "org.freedesktop.systemd1.Unit", "Start"))) {
138 log_error("Could not allocate message.");
142 if (!dbus_message_append_args(m,
143 DBUS_TYPE_STRING, &replace,
144 DBUS_TYPE_INVALID)) {
145 log_error("Could not attach group information to signal message.");
149 dbus_message_unref(reply);
150 if (!(reply = dbus_connection_send_with_reply_and_block(s->bus, m, -1, &error))) {
151 log_error("Failed to start unit: %s", error.message);
157 dbus_message_unref(m);
160 dbus_message_unref(reply);
162 dbus_error_free(&error);
165 static void request_process(Server *s, const struct init_request *req) {
169 if (req->magic != INIT_MAGIC) {
170 log_error("Got initctl request with invalid magic. Ignoring.");
176 case INIT_CMD_RUNLVL:
177 if (!isprint(req->runlevel))
178 log_error("Got invalid runlevel. Ignoring.");
180 change_runlevel(s, req->runlevel);
183 case INIT_CMD_POWERFAIL:
184 case INIT_CMD_POWERFAILNOW:
185 case INIT_CMD_POWEROK:
186 log_warning("Received UPS/power initctl request. This is not implemented in systemd. Upgrade your UPS daemon!");
189 case INIT_CMD_CHANGECONS:
190 log_warning("Received console change initctl request. This is not implemented in systemd.");
193 case INIT_CMD_SETENV:
194 case INIT_CMD_UNSETENV:
195 log_warning("Received environment initctl request. This is not implemented in systemd.");
199 log_warning("Received unknown initctl request. Ignoring.");
204 static int fifo_process(Fifo *f) {
210 if ((l = read(f->fd, ((uint8_t*) &f->buffer) + f->bytes_read, sizeof(f->buffer) - f->bytes_read)) <= 0) {
215 log_warning("Failed to read from fifo: %s", strerror(errno));
220 assert(f->bytes_read <= sizeof(f->buffer));
222 if (f->bytes_read == sizeof(f->buffer)) {
223 request_process(f->server, &f->buffer);
230 static void fifo_free(Fifo *f) {
234 assert(f->server->n_fifos > 0);
235 f->server->n_fifos--;
236 LIST_REMOVE(Fifo, fifo, f->server->fifos, f);
241 epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL);
243 close_nointr_nofail(f->fd);
249 static void server_done(Server *s) {
255 if (s->epoll_fd >= 0)
256 close_nointr_nofail(s->epoll_fd);
259 dbus_connection_unref(s->bus);
262 static int server_init(Server *s, unsigned n_sockets) {
268 assert(n_sockets > 0);
270 dbus_error_init(&error);
274 if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
276 log_error("Failed to create epoll object: %s", strerror(errno));
280 for (i = 0; i < n_sockets; i++) {
281 struct epoll_event ev;
285 fd = SD_LISTEN_FDS_START+i;
287 if ((r = sd_is_fifo(fd, NULL)) < 0) {
288 log_error("Failed to determine file descriptor type: %s", strerror(-r));
293 log_error("Wrong file descriptor type.");
298 if (!(f = new0(Fifo, 1))) {
300 log_error("Failed to create fifo object: %s", strerror(errno));
309 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
312 log_error("Failed to add fifo fd to epoll object: %s", strerror(errno));
316 f->fd = SD_LISTEN_FDS_START+i;
317 LIST_PREPEND(Fifo, fifo, s->fifos, f);
322 if (!(s->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
323 log_error("Failed to get D-Bus connection: %s", error.message);
332 dbus_error_free(&error);
336 static int process_event(Server *s, struct epoll_event *ev) {
342 if (!(ev->events & EPOLLIN)) {
343 log_info("Got invalid event from epoll. (3)");
347 f = (Fifo*) ev->data.ptr;
349 if ((r = fifo_process(f)) < 0) {
350 log_info("Got error on fifo: %s", strerror(-r));
358 int main(int argc, char *argv[]) {
362 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
363 log_parse_environment();
365 log_info("systemd-initctl running as pid %llu", (unsigned long long) getpid());
367 if ((n = sd_listen_fds(true)) < 0) {
368 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
372 if (n <= 0 || n > SERVER_FD_MAX) {
373 log_error("No or too many file descriptors passed.");
377 if (server_init(&server, (unsigned) n) < 0)
381 struct epoll_event event;
384 if ((k = epoll_wait(server.epoll_fd,
391 log_error("epoll_wait() failed: %s", strerror(errno));
398 if ((k = process_event(&server, &event)) < 0)
404 server_done(&server);
406 log_info("systemd-initctl stopped as pid %llu", (unsigned long long) getpid());