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