]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/initctl.c
initctl: talk to systemd via the abstract private bus
[thirdparty/systemd.git] / src / initctl.c
CommitLineData
0b7964b8
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include <sys/socket.h>
23#include <sys/types.h>
24#include <assert.h>
25#include <time.h>
26#include <string.h>
27#include <stdio.h>
28#include <errno.h>
29#include <unistd.h>
30#include <sys/poll.h>
31#include <sys/epoll.h>
32#include <sys/un.h>
33#include <fcntl.h>
34#include <ctype.h>
35
36#include <dbus/dbus.h>
37
38#include "util.h"
39#include "log.h"
40#include "list.h"
41#include "initreq.h"
514f4ef5 42#include "special.h"
8bfcc8ea 43#include "sd-daemon.h"
0b7964b8 44
0b7964b8
LP
45#define SERVER_FD_MAX 16
46#define TIMEOUT ((int) (10*MSEC_PER_SEC))
47
48typedef struct Fifo Fifo;
49
50typedef struct Server {
51 int epoll_fd;
52
53 LIST_HEAD(Fifo, fifos);
54 unsigned n_fifos;
55
56 DBusConnection *bus;
57} Server;
58
59struct Fifo {
60 Server *server;
61
62 int fd;
63
64 struct init_request buffer;
65 size_t bytes_read;
66
67 LIST_FIELDS(Fifo, fifo);
68};
69
70static const char *translate_runlevel(int runlevel) {
6542952f
LP
71 static const struct {
72 const int runlevel;
73 const char *special;
74 } table[] = {
514f4ef5
LP
75 { '0', SPECIAL_POWEROFF_TARGET },
76 { '1', SPECIAL_RESCUE_TARGET },
77 { 's', SPECIAL_RESCUE_TARGET },
78 { 'S', SPECIAL_RESCUE_TARGET },
6542952f
LP
79 { '2', SPECIAL_RUNLEVEL2_TARGET },
80 { '3', SPECIAL_RUNLEVEL3_TARGET },
81 { '4', SPECIAL_RUNLEVEL4_TARGET },
82 { '5', SPECIAL_RUNLEVEL5_TARGET },
514f4ef5 83 { '6', SPECIAL_REBOOT_TARGET },
6542952f
LP
84 };
85
86 unsigned i;
87
88 for (i = 0; i < ELEMENTSOF(table); i++)
89 if (table[i].runlevel == runlevel)
90 return table[i].special;
91
92 return NULL;
0b7964b8
LP
93}
94
95static void change_runlevel(Server *s, int runlevel) {
96 const char *target;
97 DBusMessage *m = NULL, *reply = NULL;
98 DBusError error;
c87eba54 99 const char *replace = "replace";
0b7964b8
LP
100
101 assert(s);
102
103 dbus_error_init(&error);
104
105 if (!(target = translate_runlevel(runlevel))) {
106 log_warning("Got request for unknown runlevel %c, ignoring.", runlevel);
107 goto finish;
108 }
109
110 log_debug("Running request %s", target);
111
c87eba54 112 if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit"))) {
0b7964b8
LP
113 log_error("Could not allocate message.");
114 goto finish;
115 }
116
117 if (!dbus_message_append_args(m,
118 DBUS_TYPE_STRING, &target,
0b7964b8
LP
119 DBUS_TYPE_STRING, &replace,
120 DBUS_TYPE_INVALID)) {
c87eba54 121 log_error("Could not attach target and flag information to signal message.");
0b7964b8
LP
122 goto finish;
123 }
124
0b7964b8
LP
125 if (!(reply = dbus_connection_send_with_reply_and_block(s->bus, m, -1, &error))) {
126 log_error("Failed to start unit: %s", error.message);
127 goto finish;
128 }
129
130finish:
131 if (m)
132 dbus_message_unref(m);
133
134 if (reply)
135 dbus_message_unref(reply);
136
137 dbus_error_free(&error);
138}
139
140static void request_process(Server *s, const struct init_request *req) {
141 assert(s);
142 assert(req);
143
144 if (req->magic != INIT_MAGIC) {
145 log_error("Got initctl request with invalid magic. Ignoring.");
146 return;
147 }
148
149 switch (req->cmd) {
150
151 case INIT_CMD_RUNLVL:
152 if (!isprint(req->runlevel))
153 log_error("Got invalid runlevel. Ignoring.");
154 else
155 change_runlevel(s, req->runlevel);
156 return;
157
158 case INIT_CMD_POWERFAIL:
159 case INIT_CMD_POWERFAILNOW:
160 case INIT_CMD_POWEROK:
161 log_warning("Received UPS/power initctl request. This is not implemented in systemd. Upgrade your UPS daemon!");
162 return;
163
164 case INIT_CMD_CHANGECONS:
165 log_warning("Received console change initctl request. This is not implemented in systemd.");
166 return;
167
168 case INIT_CMD_SETENV:
169 case INIT_CMD_UNSETENV:
170 log_warning("Received environment initctl request. This is not implemented in systemd.");
171 return;
172
173 default:
174 log_warning("Received unknown initctl request. Ignoring.");
175 return;
176 }
177}
178
179static int fifo_process(Fifo *f) {
180 ssize_t l;
181
182 assert(f);
183
184 errno = EIO;
185 if ((l = read(f->fd, ((uint8_t*) &f->buffer) + f->bytes_read, sizeof(f->buffer) - f->bytes_read)) <= 0) {
186
187 if (errno == EAGAIN)
188 return 0;
189
190 log_warning("Failed to read from fifo: %s", strerror(errno));
191 return -1;
192 }
193
194 f->bytes_read += l;
195 assert(f->bytes_read <= sizeof(f->buffer));
196
197 if (f->bytes_read == sizeof(f->buffer)) {
198 request_process(f->server, &f->buffer);
199 f->bytes_read = 0;
200 }
201
202 return 0;
203}
204
205static void fifo_free(Fifo *f) {
206 assert(f);
207
208 if (f->server) {
209 assert(f->server->n_fifos > 0);
210 f->server->n_fifos--;
211 LIST_REMOVE(Fifo, fifo, f->server->fifos, f);
212 }
213
214 if (f->fd >= 0) {
215 if (f->server)
216 epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL);
217
a16e1123 218 close_nointr_nofail(f->fd);
0b7964b8
LP
219 }
220
221 free(f);
222}
223
0b7964b8
LP
224static void server_done(Server *s) {
225 assert(s);
226
227 while (s->fifos)
228 fifo_free(s->fifos);
229
230 if (s->epoll_fd >= 0)
a16e1123 231 close_nointr_nofail(s->epoll_fd);
0b7964b8 232
fb1af5b0
LP
233 if (s->bus) {
234 dbus_connection_set_exit_on_disconnect(s->bus, FALSE);
235 dbus_connection_unref(s->bus);
236 }
0b7964b8
LP
237}
238
239static int server_init(Server *s, unsigned n_sockets) {
240 int r;
241 unsigned i;
242 DBusError error;
243
244 assert(s);
245 assert(n_sockets > 0);
246
247 dbus_error_init(&error);
248
249 zero(*s);
250
251 if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
252 r = -errno;
253 log_error("Failed to create epoll object: %s", strerror(errno));
254 goto fail;
255 }
256
257 for (i = 0; i < n_sockets; i++) {
258 struct epoll_event ev;
259 Fifo *f;
7c394faa
LP
260 int fd;
261
262 fd = SD_LISTEN_FDS_START+i;
263
264 if ((r = sd_is_fifo(fd, NULL)) < 0) {
265 log_error("Failed to determine file descriptor type: %s", strerror(-r));
266 goto fail;
267 }
268
269 if (!r) {
270 log_error("Wrong file descriptor type.");
271 r = -EINVAL;
272 goto fail;
273 }
0b7964b8
LP
274
275 if (!(f = new0(Fifo, 1))) {
276 r = -ENOMEM;
277 log_error("Failed to create fifo object: %s", strerror(errno));
278 goto fail;
279 }
280
281 f->fd = -1;
282
283 zero(ev);
284 ev.events = EPOLLIN;
285 ev.data.ptr = f;
7c394faa 286 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
0b7964b8
LP
287 r = -errno;
288 fifo_free(f);
289 log_error("Failed to add fifo fd to epoll object: %s", strerror(errno));
290 goto fail;
291 }
292
8bfcc8ea 293 f->fd = SD_LISTEN_FDS_START+i;
0b7964b8
LP
294 LIST_PREPEND(Fifo, fifo, s->fifos, f);
295 f->server = s;
296 s->n_fifos ++;
297 }
298
f5a3628c 299 if (!(s->bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
0b7964b8
LP
300 log_error("Failed to get D-Bus connection: %s", error.message);
301 goto fail;
302 }
303
304 return 0;
305
306fail:
307 server_done(s);
308
309 dbus_error_free(&error);
310 return r;
311}
312
313static int process_event(Server *s, struct epoll_event *ev) {
314 int r;
315 Fifo *f;
316
317 assert(s);
318
319 if (!(ev->events & EPOLLIN)) {
320 log_info("Got invalid event from epoll. (3)");
321 return -EIO;
322 }
323
324 f = (Fifo*) ev->data.ptr;
325
326 if ((r = fifo_process(f)) < 0) {
327 log_info("Got error on fifo: %s", strerror(-r));
328 fifo_free(f);
329 return r;
330 }
331
332 return 0;
333}
334
335int main(int argc, char *argv[]) {
336 Server server;
8bfcc8ea
LP
337 int r = 3, n;
338
339 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
340 log_parse_environment();
0b7964b8 341
bb00e604 342 log_info("systemd-initctl running as pid %lu", (unsigned long) getpid());
0b7964b8 343
8bfcc8ea
LP
344 if ((n = sd_listen_fds(true)) < 0) {
345 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
0b7964b8 346 return 1;
8bfcc8ea
LP
347 }
348
349 if (n <= 0 || n > SERVER_FD_MAX) {
350 log_error("No or too many file descriptors passed.");
351 return 2;
352 }
0b7964b8 353
8bfcc8ea 354 if (server_init(&server, (unsigned) n) < 0)
0b7964b8
LP
355 return 2;
356
8c47c732
LP
357 sd_notify(false,
358 "READY=1\n"
359 "STATUS=Processing requests...");
360
0b7964b8
LP
361 for (;;) {
362 struct epoll_event event;
363 int k;
364
365 if ((k = epoll_wait(server.epoll_fd,
366 &event, 1,
367 TIMEOUT)) < 0) {
368
369 if (errno == EINTR)
370 continue;
371
372 log_error("epoll_wait() failed: %s", strerror(errno));
373 goto fail;
374 }
375
376 if (k <= 0)
377 break;
378
379 if ((k = process_event(&server, &event)) < 0)
380 goto fail;
381 }
382 r = 0;
383
384fail:
8c47c732
LP
385 sd_notify(false,
386 "STATUS=Shutting down...");
387
0b7964b8
LP
388 server_done(&server);
389
bb00e604 390 log_info("systemd-initctl stopped as pid %lu", (unsigned long) getpid());
0b7964b8
LP
391
392 dbus_shutdown();
393
394 return r;
395}