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