]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/initctl.c
relicense to LGPLv2.1 (with exceptions)
[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
5430f7f2
LP
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
0b7964b8
LP
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
5430f7f2 16 Lesser General Public License for more details.
0b7964b8 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
0b7964b8
LP
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>
81527be1 37#include <systemd/sd-daemon.h>
0b7964b8
LP
38
39#include "util.h"
40#include "log.h"
41#include "list.h"
42#include "initreq.h"
514f4ef5 43#include "special.h"
a822056b 44#include "dbus-common.h"
ef9eb0a8 45#include "def.h"
0b7964b8 46
0b7964b8 47#define SERVER_FD_MAX 16
ef9eb0a8 48#define TIMEOUT_MSEC ((int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC))
0b7964b8
LP
49
50typedef struct Fifo Fifo;
51
52typedef struct Server {
53 int epoll_fd;
54
55 LIST_HEAD(Fifo, fifos);
56 unsigned n_fifos;
57
58 DBusConnection *bus;
f632a663
LP
59
60 bool quit;
0b7964b8
LP
61} Server;
62
63struct 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
ac83842a 74static const char *translate_runlevel(int runlevel, bool *isolate) {
6542952f
LP
75 static const struct {
76 const int runlevel;
77 const char *special;
ac83842a 78 bool isolate;
6542952f 79 } table[] = {
ac83842a
LP
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 },
6542952f
LP
89 };
90
91 unsigned i;
92
ac83842a
LP
93 assert(isolate);
94
6542952f 95 for (i = 0; i < ELEMENTSOF(table); i++)
ac83842a
LP
96 if (table[i].runlevel == runlevel) {
97 *isolate = table[i].isolate;
65457142
FC
98 if (runlevel == '6' && kexec_loaded())
99 return SPECIAL_KEXEC_TARGET;
6542952f 100 return table[i].special;
ac83842a 101 }
6542952f
LP
102
103 return NULL;
0b7964b8
LP
104}
105
106static void change_runlevel(Server *s, int runlevel) {
107 const char *target;
108 DBusMessage *m = NULL, *reply = NULL;
109 DBusError error;
ac83842a
LP
110 const char *mode;
111 bool isolate = false;
0b7964b8
LP
112
113 assert(s);
114
115 dbus_error_init(&error);
116
ac83842a 117 if (!(target = translate_runlevel(runlevel, &isolate))) {
0b7964b8
LP
118 log_warning("Got request for unknown runlevel %c, ignoring.", runlevel);
119 goto finish;
120 }
121
ac83842a
LP
122 if (isolate)
123 mode = "isolate";
124 else
125 mode = "replace";
126
6f0d624e 127 log_debug("Running request %s/start/%s", target, mode);
0b7964b8 128
c87eba54 129 if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit"))) {
0b7964b8
LP
130 log_error("Could not allocate message.");
131 goto finish;
132 }
133
134 if (!dbus_message_append_args(m,
135 DBUS_TYPE_STRING, &target,
ac83842a 136 DBUS_TYPE_STRING, &mode,
0b7964b8 137 DBUS_TYPE_INVALID)) {
5192bd19 138 log_error("Could not attach target and flag information to message.");
0b7964b8
LP
139 goto finish;
140 }
141
0b7964b8 142 if (!(reply = dbus_connection_send_with_reply_and_block(s->bus, m, -1, &error))) {
4cf5d675 143 log_error("Failed to start unit: %s", bus_error_message(&error));
0b7964b8
LP
144 goto finish;
145 }
146
147finish:
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
157static 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
46e964c5
TG
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");
f632a663
LP
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;
46e964c5
TG
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 }
0b7964b8
LP
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
220static 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
246static 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
a16e1123 259 close_nointr_nofail(f->fd);
0b7964b8
LP
260 }
261
262 free(f);
263}
264
0b7964b8
LP
265static 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)
a16e1123 272 close_nointr_nofail(s->epoll_fd);
0b7964b8 273
fb1af5b0 274 if (s->bus) {
5d452f9c
LP
275 dbus_connection_flush(s->bus);
276 dbus_connection_close(s->bus);
277 dbus_connection_unref(s->bus);
fb1af5b0 278 }
0b7964b8
LP
279}
280
281static 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;
7c394faa
LP
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 }
0b7964b8
LP
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;
7c394faa 328 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
0b7964b8
LP
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
63983207 335 f->fd = fd;
0b7964b8
LP
336 LIST_PREPEND(Fifo, fifo, s->fifos, f);
337 f->server = s;
338 s->n_fifos ++;
339 }
340
b574246b 341 if (bus_connect(DBUS_BUS_SYSTEM, &s->bus, NULL, &error) < 0) {
4cf5d675 342 log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
0b7964b8
LP
343 goto fail;
344 }
345
346 return 0;
347
348fail:
349 server_done(s);
350
351 dbus_error_free(&error);
352 return r;
353}
354
355static 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
377int main(int argc, char *argv[]) {
378 Server server;
22f4096c 379 int r = EXIT_FAILURE, n;
8bfcc8ea 380
0ca3f374
LP
381 if (getppid() != 1) {
382 log_error("This program should be invoked by init only.");
22f4096c 383 return EXIT_FAILURE;
0ca3f374
LP
384 }
385
386 if (argc > 1) {
387 log_error("This program does not take arguments.");
22f4096c 388 return EXIT_FAILURE;
0ca3f374
LP
389 }
390
4cfa2c99 391 log_set_target(LOG_TARGET_AUTO);
8bfcc8ea 392 log_parse_environment();
2396fb04 393 log_open();
0b7964b8 394
4c12626c
LP
395 umask(0022);
396
8bfcc8ea
LP
397 if ((n = sd_listen_fds(true)) < 0) {
398 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
22f4096c 399 return EXIT_FAILURE;
8bfcc8ea
LP
400 }
401
402 if (n <= 0 || n > SERVER_FD_MAX) {
403 log_error("No or too many file descriptors passed.");
22f4096c 404 return EXIT_FAILURE;
8bfcc8ea 405 }
0b7964b8 406
8bfcc8ea 407 if (server_init(&server, (unsigned) n) < 0)
22f4096c 408 return EXIT_FAILURE;
0b7964b8 409
cd6d0a45
LP
410 log_debug("systemd-initctl running as pid %lu", (unsigned long) getpid());
411
8c47c732
LP
412 sd_notify(false,
413 "READY=1\n"
414 "STATUS=Processing requests...");
415
f632a663 416 while (!server.quit) {
0b7964b8
LP
417 struct epoll_event event;
418 int k;
419
420 if ((k = epoll_wait(server.epoll_fd,
421 &event, 1,
ef9eb0a8 422 TIMEOUT_MSEC)) < 0) {
0b7964b8
LP
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
e364ad06 434 if (process_event(&server, &event) < 0)
0b7964b8
LP
435 goto fail;
436 }
cd6d0a45 437
22f4096c 438 r = EXIT_SUCCESS;
0b7964b8 439
cd6d0a45
LP
440 log_debug("systemd-initctl stopped as pid %lu", (unsigned long) getpid());
441
0b7964b8 442fail:
8c47c732
LP
443 sd_notify(false,
444 "STATUS=Shutting down...");
445
0b7964b8
LP
446 server_done(&server);
447
0b7964b8
LP
448 dbus_shutdown();
449
450 return r;
451}