]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/initctl.c
build-sys: move source files to subdirectory
[thirdparty/systemd.git] / src / initctl.c
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"
42 #include "manager.h"
43 #include "sd-daemon.h"
44
45 #define SERVER_FD_MAX 16
46 #define TIMEOUT ((int) (10*MSEC_PER_SEC))
47
48 typedef struct Fifo Fifo;
49
50 typedef struct Server {
51 int epoll_fd;
52
53 LIST_HEAD(Fifo, fifos);
54 unsigned n_fifos;
55
56 DBusConnection *bus;
57 } Server;
58
59 struct 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
70 static const char *translate_runlevel(int runlevel) {
71 static const struct {
72 const int runlevel;
73 const char *special;
74 } table[] = {
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 },
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;
93 }
94
95 static void change_runlevel(Server *s, int runlevel) {
96 const char *target;
97 DBusMessage *m = NULL, *reply = NULL;
98 DBusError error;
99 const char *path, *replace = "isolate";
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
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.");
114 goto finish;
115 }
116
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.");
121 goto finish;
122 }
123
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);
126 goto finish;
127 }
128
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);
133 goto finish;
134 }
135
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.");
139 goto finish;
140 }
141
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.");
146 goto finish;
147 }
148
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);
152 goto finish;
153 }
154
155 finish:
156 if (m)
157 dbus_message_unref(m);
158
159 if (reply)
160 dbus_message_unref(reply);
161
162 dbus_error_free(&error);
163 }
164
165 static void request_process(Server *s, const struct init_request *req) {
166 assert(s);
167 assert(req);
168
169 if (req->magic != INIT_MAGIC) {
170 log_error("Got initctl request with invalid magic. Ignoring.");
171 return;
172 }
173
174 switch (req->cmd) {
175
176 case INIT_CMD_RUNLVL:
177 if (!isprint(req->runlevel))
178 log_error("Got invalid runlevel. Ignoring.");
179 else
180 change_runlevel(s, req->runlevel);
181 return;
182
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!");
187 return;
188
189 case INIT_CMD_CHANGECONS:
190 log_warning("Received console change initctl request. This is not implemented in systemd.");
191 return;
192
193 case INIT_CMD_SETENV:
194 case INIT_CMD_UNSETENV:
195 log_warning("Received environment initctl request. This is not implemented in systemd.");
196 return;
197
198 default:
199 log_warning("Received unknown initctl request. Ignoring.");
200 return;
201 }
202 }
203
204 static int fifo_process(Fifo *f) {
205 ssize_t l;
206
207 assert(f);
208
209 errno = EIO;
210 if ((l = read(f->fd, ((uint8_t*) &f->buffer) + f->bytes_read, sizeof(f->buffer) - f->bytes_read)) <= 0) {
211
212 if (errno == EAGAIN)
213 return 0;
214
215 log_warning("Failed to read from fifo: %s", strerror(errno));
216 return -1;
217 }
218
219 f->bytes_read += l;
220 assert(f->bytes_read <= sizeof(f->buffer));
221
222 if (f->bytes_read == sizeof(f->buffer)) {
223 request_process(f->server, &f->buffer);
224 f->bytes_read = 0;
225 }
226
227 return 0;
228 }
229
230 static void fifo_free(Fifo *f) {
231 assert(f);
232
233 if (f->server) {
234 assert(f->server->n_fifos > 0);
235 f->server->n_fifos--;
236 LIST_REMOVE(Fifo, fifo, f->server->fifos, f);
237 }
238
239 if (f->fd >= 0) {
240 if (f->server)
241 epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL);
242
243 close_nointr_nofail(f->fd);
244 }
245
246 free(f);
247 }
248
249 static void server_done(Server *s) {
250 assert(s);
251
252 while (s->fifos)
253 fifo_free(s->fifos);
254
255 if (s->epoll_fd >= 0)
256 close_nointr_nofail(s->epoll_fd);
257
258 if (s->bus)
259 dbus_connection_unref(s->bus);
260 }
261
262 static int server_init(Server *s, unsigned n_sockets) {
263 int r;
264 unsigned i;
265 DBusError error;
266
267 assert(s);
268 assert(n_sockets > 0);
269
270 dbus_error_init(&error);
271
272 zero(*s);
273
274 if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
275 r = -errno;
276 log_error("Failed to create epoll object: %s", strerror(errno));
277 goto fail;
278 }
279
280 for (i = 0; i < n_sockets; i++) {
281 struct epoll_event ev;
282 Fifo *f;
283
284 if (!(f = new0(Fifo, 1))) {
285 r = -ENOMEM;
286 log_error("Failed to create fifo object: %s", strerror(errno));
287 goto fail;
288 }
289
290 f->fd = -1;
291
292 zero(ev);
293 ev.events = EPOLLIN;
294 ev.data.ptr = f;
295 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SD_LISTEN_FDS_START+i, &ev) < 0) {
296 r = -errno;
297 fifo_free(f);
298 log_error("Failed to add fifo fd to epoll object: %s", strerror(errno));
299 goto fail;
300 }
301
302 f->fd = SD_LISTEN_FDS_START+i;
303 LIST_PREPEND(Fifo, fifo, s->fifos, f);
304 f->server = s;
305 s->n_fifos ++;
306 }
307
308 if (!(s->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
309 log_error("Failed to get D-Bus connection: %s", error.message);
310 goto fail;
311 }
312
313 return 0;
314
315 fail:
316 server_done(s);
317
318 dbus_error_free(&error);
319 return r;
320 }
321
322 static int process_event(Server *s, struct epoll_event *ev) {
323 int r;
324 Fifo *f;
325
326 assert(s);
327
328 if (!(ev->events & EPOLLIN)) {
329 log_info("Got invalid event from epoll. (3)");
330 return -EIO;
331 }
332
333 f = (Fifo*) ev->data.ptr;
334
335 if ((r = fifo_process(f)) < 0) {
336 log_info("Got error on fifo: %s", strerror(-r));
337 fifo_free(f);
338 return r;
339 }
340
341 return 0;
342 }
343
344 int main(int argc, char *argv[]) {
345 Server server;
346 int r = 3, n;
347
348 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
349 log_parse_environment();
350
351 log_info("systemd-initctl running as pid %llu", (unsigned long long) getpid());
352
353 if ((n = sd_listen_fds(true)) < 0) {
354 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
355 return 1;
356 }
357
358 if (n <= 0 || n > SERVER_FD_MAX) {
359 log_error("No or too many file descriptors passed.");
360 return 2;
361 }
362
363 if (server_init(&server, (unsigned) n) < 0)
364 return 2;
365
366 for (;;) {
367 struct epoll_event event;
368 int k;
369
370 if ((k = epoll_wait(server.epoll_fd,
371 &event, 1,
372 TIMEOUT)) < 0) {
373
374 if (errno == EINTR)
375 continue;
376
377 log_error("epoll_wait() failed: %s", strerror(errno));
378 goto fail;
379 }
380
381 if (k <= 0)
382 break;
383
384 if ((k = process_event(&server, &event)) < 0)
385 goto fail;
386 }
387 r = 0;
388
389 fail:
390 server_done(&server);
391
392 log_info("systemd-initctl stopped as pid %llu", (unsigned long long) getpid());
393
394 dbus_shutdown();
395
396 return r;
397 }