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