]>
Commit | Line | Data |
---|---|---|
0b7964b8 LP |
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" | |
6542952f | 42 | #include "manager.h" |
8bfcc8ea | 43 | #include "sd-daemon.h" |
0b7964b8 | 44 | |
0b7964b8 LP |
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) { | |
6542952f LP |
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; | |
0b7964b8 LP |
93 | } |
94 | ||
95 | static void change_runlevel(Server *s, int runlevel) { | |
96 | const char *target; | |
97 | DBusMessage *m = NULL, *reply = NULL; | |
98 | DBusError error; | |
c87eba54 | 99 | const char *replace = "replace"; |
0b7964b8 LP |
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 | ||
c87eba54 | 112 | if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit"))) { |
0b7964b8 LP |
113 | log_error("Could not allocate message."); |
114 | goto finish; | |
115 | } | |
116 | ||
117 | if (!dbus_message_append_args(m, | |
118 | DBUS_TYPE_STRING, &target, | |
0b7964b8 LP |
119 | DBUS_TYPE_STRING, &replace, |
120 | DBUS_TYPE_INVALID)) { | |
c87eba54 | 121 | log_error("Could not attach target and flag information to signal message."); |
0b7964b8 LP |
122 | goto finish; |
123 | } | |
124 | ||
0b7964b8 LP |
125 | if (!(reply = dbus_connection_send_with_reply_and_block(s->bus, m, -1, &error))) { |
126 | log_error("Failed to start unit: %s", error.message); | |
127 | goto finish; | |
128 | } | |
129 | ||
130 | finish: | |
131 | if (m) | |
132 | dbus_message_unref(m); | |
133 | ||
134 | if (reply) | |
135 | dbus_message_unref(reply); | |
136 | ||
137 | dbus_error_free(&error); | |
138 | } | |
139 | ||
140 | static void request_process(Server *s, const struct init_request *req) { | |
141 | assert(s); | |
142 | assert(req); | |
143 | ||
144 | if (req->magic != INIT_MAGIC) { | |
145 | log_error("Got initctl request with invalid magic. Ignoring."); | |
146 | return; | |
147 | } | |
148 | ||
149 | switch (req->cmd) { | |
150 | ||
151 | case INIT_CMD_RUNLVL: | |
152 | if (!isprint(req->runlevel)) | |
153 | log_error("Got invalid runlevel. Ignoring."); | |
154 | else | |
155 | change_runlevel(s, req->runlevel); | |
156 | return; | |
157 | ||
158 | case INIT_CMD_POWERFAIL: | |
159 | case INIT_CMD_POWERFAILNOW: | |
160 | case INIT_CMD_POWEROK: | |
161 | log_warning("Received UPS/power initctl request. This is not implemented in systemd. Upgrade your UPS daemon!"); | |
162 | return; | |
163 | ||
164 | case INIT_CMD_CHANGECONS: | |
165 | log_warning("Received console change initctl request. This is not implemented in systemd."); | |
166 | return; | |
167 | ||
168 | case INIT_CMD_SETENV: | |
169 | case INIT_CMD_UNSETENV: | |
170 | log_warning("Received environment initctl request. This is not implemented in systemd."); | |
171 | return; | |
172 | ||
173 | default: | |
174 | log_warning("Received unknown initctl request. Ignoring."); | |
175 | return; | |
176 | } | |
177 | } | |
178 | ||
179 | static int fifo_process(Fifo *f) { | |
180 | ssize_t l; | |
181 | ||
182 | assert(f); | |
183 | ||
184 | errno = EIO; | |
185 | if ((l = read(f->fd, ((uint8_t*) &f->buffer) + f->bytes_read, sizeof(f->buffer) - f->bytes_read)) <= 0) { | |
186 | ||
187 | if (errno == EAGAIN) | |
188 | return 0; | |
189 | ||
190 | log_warning("Failed to read from fifo: %s", strerror(errno)); | |
191 | return -1; | |
192 | } | |
193 | ||
194 | f->bytes_read += l; | |
195 | assert(f->bytes_read <= sizeof(f->buffer)); | |
196 | ||
197 | if (f->bytes_read == sizeof(f->buffer)) { | |
198 | request_process(f->server, &f->buffer); | |
199 | f->bytes_read = 0; | |
200 | } | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
205 | static void fifo_free(Fifo *f) { | |
206 | assert(f); | |
207 | ||
208 | if (f->server) { | |
209 | assert(f->server->n_fifos > 0); | |
210 | f->server->n_fifos--; | |
211 | LIST_REMOVE(Fifo, fifo, f->server->fifos, f); | |
212 | } | |
213 | ||
214 | if (f->fd >= 0) { | |
215 | if (f->server) | |
216 | epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL); | |
217 | ||
a16e1123 | 218 | close_nointr_nofail(f->fd); |
0b7964b8 LP |
219 | } |
220 | ||
221 | free(f); | |
222 | } | |
223 | ||
0b7964b8 LP |
224 | static void server_done(Server *s) { |
225 | assert(s); | |
226 | ||
227 | while (s->fifos) | |
228 | fifo_free(s->fifos); | |
229 | ||
230 | if (s->epoll_fd >= 0) | |
a16e1123 | 231 | close_nointr_nofail(s->epoll_fd); |
0b7964b8 LP |
232 | |
233 | if (s->bus) | |
234 | dbus_connection_unref(s->bus); | |
235 | } | |
236 | ||
237 | static int server_init(Server *s, unsigned n_sockets) { | |
238 | int r; | |
239 | unsigned i; | |
240 | DBusError error; | |
241 | ||
242 | assert(s); | |
243 | assert(n_sockets > 0); | |
244 | ||
245 | dbus_error_init(&error); | |
246 | ||
247 | zero(*s); | |
248 | ||
249 | if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) { | |
250 | r = -errno; | |
251 | log_error("Failed to create epoll object: %s", strerror(errno)); | |
252 | goto fail; | |
253 | } | |
254 | ||
255 | for (i = 0; i < n_sockets; i++) { | |
256 | struct epoll_event ev; | |
257 | Fifo *f; | |
7c394faa LP |
258 | int fd; |
259 | ||
260 | fd = SD_LISTEN_FDS_START+i; | |
261 | ||
262 | if ((r = sd_is_fifo(fd, NULL)) < 0) { | |
263 | log_error("Failed to determine file descriptor type: %s", strerror(-r)); | |
264 | goto fail; | |
265 | } | |
266 | ||
267 | if (!r) { | |
268 | log_error("Wrong file descriptor type."); | |
269 | r = -EINVAL; | |
270 | goto fail; | |
271 | } | |
0b7964b8 LP |
272 | |
273 | if (!(f = new0(Fifo, 1))) { | |
274 | r = -ENOMEM; | |
275 | log_error("Failed to create fifo object: %s", strerror(errno)); | |
276 | goto fail; | |
277 | } | |
278 | ||
279 | f->fd = -1; | |
280 | ||
281 | zero(ev); | |
282 | ev.events = EPOLLIN; | |
283 | ev.data.ptr = f; | |
7c394faa | 284 | if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
0b7964b8 LP |
285 | r = -errno; |
286 | fifo_free(f); | |
287 | log_error("Failed to add fifo fd to epoll object: %s", strerror(errno)); | |
288 | goto fail; | |
289 | } | |
290 | ||
8bfcc8ea | 291 | f->fd = SD_LISTEN_FDS_START+i; |
0b7964b8 LP |
292 | LIST_PREPEND(Fifo, fifo, s->fifos, f); |
293 | f->server = s; | |
294 | s->n_fifos ++; | |
295 | } | |
296 | ||
297 | if (!(s->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { | |
298 | log_error("Failed to get D-Bus connection: %s", error.message); | |
299 | goto fail; | |
300 | } | |
301 | ||
302 | return 0; | |
303 | ||
304 | fail: | |
305 | server_done(s); | |
306 | ||
307 | dbus_error_free(&error); | |
308 | return r; | |
309 | } | |
310 | ||
311 | static int process_event(Server *s, struct epoll_event *ev) { | |
312 | int r; | |
313 | Fifo *f; | |
314 | ||
315 | assert(s); | |
316 | ||
317 | if (!(ev->events & EPOLLIN)) { | |
318 | log_info("Got invalid event from epoll. (3)"); | |
319 | return -EIO; | |
320 | } | |
321 | ||
322 | f = (Fifo*) ev->data.ptr; | |
323 | ||
324 | if ((r = fifo_process(f)) < 0) { | |
325 | log_info("Got error on fifo: %s", strerror(-r)); | |
326 | fifo_free(f); | |
327 | return r; | |
328 | } | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | int main(int argc, char *argv[]) { | |
334 | Server server; | |
8bfcc8ea LP |
335 | int r = 3, n; |
336 | ||
337 | log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); | |
338 | log_parse_environment(); | |
0b7964b8 LP |
339 | |
340 | log_info("systemd-initctl running as pid %llu", (unsigned long long) getpid()); | |
341 | ||
8bfcc8ea LP |
342 | if ((n = sd_listen_fds(true)) < 0) { |
343 | log_error("Failed to read listening file descriptors from environment: %s", strerror(-r)); | |
0b7964b8 | 344 | return 1; |
8bfcc8ea LP |
345 | } |
346 | ||
347 | if (n <= 0 || n > SERVER_FD_MAX) { | |
348 | log_error("No or too many file descriptors passed."); | |
349 | return 2; | |
350 | } | |
0b7964b8 | 351 | |
8bfcc8ea | 352 | if (server_init(&server, (unsigned) n) < 0) |
0b7964b8 LP |
353 | return 2; |
354 | ||
355 | for (;;) { | |
356 | struct epoll_event event; | |
357 | int k; | |
358 | ||
359 | if ((k = epoll_wait(server.epoll_fd, | |
360 | &event, 1, | |
361 | TIMEOUT)) < 0) { | |
362 | ||
363 | if (errno == EINTR) | |
364 | continue; | |
365 | ||
366 | log_error("epoll_wait() failed: %s", strerror(errno)); | |
367 | goto fail; | |
368 | } | |
369 | ||
370 | if (k <= 0) | |
371 | break; | |
372 | ||
373 | if ((k = process_event(&server, &event)) < 0) | |
374 | goto fail; | |
375 | } | |
376 | r = 0; | |
377 | ||
378 | fail: | |
379 | server_done(&server); | |
380 | ||
381 | log_info("systemd-initctl stopped as pid %llu", (unsigned long long) getpid()); | |
382 | ||
383 | dbus_shutdown(); | |
384 | ||
385 | return r; | |
386 | } |