]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0b7964b8 | 2 | |
cf0fbc49 | 3 | #include <ctype.h> |
0b7964b8 | 4 | #include <errno.h> |
cf0fbc49 | 5 | #include <stdio.h> |
0b7964b8 | 6 | #include <sys/epoll.h> |
cf0fbc49 | 7 | #include <unistd.h> |
0b7964b8 | 8 | |
b75b4db0 | 9 | #include "sd-bus.h" |
3ffd4af2 | 10 | #include "sd-daemon.h" |
0b7964b8 | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
b75b4db0 | 13 | #include "bus-error.h" |
3ffd4af2 | 14 | #include "bus-util.h" |
ef9eb0a8 | 15 | #include "def.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
f97b34a6 | 17 | #include "format-util.h" |
3ffd4af2 LP |
18 | #include "initreq.h" |
19 | #include "list.h" | |
20 | #include "log.h" | |
21 | #include "special.h" | |
22 | #include "util.h" | |
dccca82b | 23 | #include "process-util.h" |
0b7964b8 | 24 | |
0b7964b8 | 25 | #define SERVER_FD_MAX 16 |
ef9eb0a8 | 26 | #define TIMEOUT_MSEC ((int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)) |
0b7964b8 LP |
27 | |
28 | typedef struct Fifo Fifo; | |
29 | ||
30 | typedef struct Server { | |
31 | int epoll_fd; | |
32 | ||
33 | LIST_HEAD(Fifo, fifos); | |
34 | unsigned n_fifos; | |
35 | ||
b75b4db0 | 36 | sd_bus *bus; |
f632a663 LP |
37 | |
38 | bool quit; | |
0b7964b8 LP |
39 | } Server; |
40 | ||
41 | struct Fifo { | |
42 | Server *server; | |
43 | ||
44 | int fd; | |
45 | ||
46 | struct init_request buffer; | |
47 | size_t bytes_read; | |
48 | ||
49 | LIST_FIELDS(Fifo, fifo); | |
50 | }; | |
51 | ||
ac83842a | 52 | static const char *translate_runlevel(int runlevel, bool *isolate) { |
6542952f LP |
53 | static const struct { |
54 | const int runlevel; | |
55 | const char *special; | |
ac83842a | 56 | bool isolate; |
6542952f | 57 | } table[] = { |
d5d8429a LP |
58 | { '0', SPECIAL_POWEROFF_TARGET, false }, |
59 | { '1', SPECIAL_RESCUE_TARGET, true }, | |
60 | { 's', SPECIAL_RESCUE_TARGET, true }, | |
61 | { 'S', SPECIAL_RESCUE_TARGET, true }, | |
62 | { '2', SPECIAL_MULTI_USER_TARGET, true }, | |
63 | { '3', SPECIAL_MULTI_USER_TARGET, true }, | |
64 | { '4', SPECIAL_MULTI_USER_TARGET, true }, | |
65 | { '5', SPECIAL_GRAPHICAL_TARGET, true }, | |
66 | { '6', SPECIAL_REBOOT_TARGET, false }, | |
6542952f LP |
67 | }; |
68 | ||
69 | unsigned i; | |
70 | ||
ac83842a LP |
71 | assert(isolate); |
72 | ||
6542952f | 73 | for (i = 0; i < ELEMENTSOF(table); i++) |
ac83842a LP |
74 | if (table[i].runlevel == runlevel) { |
75 | *isolate = table[i].isolate; | |
65457142 FC |
76 | if (runlevel == '6' && kexec_loaded()) |
77 | return SPECIAL_KEXEC_TARGET; | |
6542952f | 78 | return table[i].special; |
ac83842a | 79 | } |
6542952f LP |
80 | |
81 | return NULL; | |
0b7964b8 LP |
82 | } |
83 | ||
73e47c42 | 84 | static int change_runlevel(Server *s, int runlevel) { |
0b7964b8 | 85 | const char *target; |
4afd3348 | 86 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
ac83842a LP |
87 | const char *mode; |
88 | bool isolate = false; | |
b75b4db0 | 89 | int r; |
0b7964b8 LP |
90 | |
91 | assert(s); | |
92 | ||
b75b4db0 | 93 | target = translate_runlevel(runlevel, &isolate); |
f168c273 | 94 | if (!target) { |
0b7964b8 | 95 | log_warning("Got request for unknown runlevel %c, ignoring.", runlevel); |
73e47c42 | 96 | return 0; |
0b7964b8 LP |
97 | } |
98 | ||
ac83842a LP |
99 | if (isolate) |
100 | mode = "isolate"; | |
101 | else | |
4c10771c | 102 | mode = "replace-irreversibly"; |
ac83842a | 103 | |
6f0d624e | 104 | log_debug("Running request %s/start/%s", target, mode); |
0b7964b8 | 105 | |
b75b4db0 TG |
106 | r = sd_bus_call_method( |
107 | s->bus, | |
108 | "org.freedesktop.systemd1", | |
109 | "/org/freedesktop/systemd1", | |
110 | "org.freedesktop.systemd1.Manager", | |
111 | "StartUnit", | |
112 | &error, | |
113 | NULL, | |
114 | "ss", target, mode); | |
73e47c42 YW |
115 | if (r < 0) |
116 | return log_error_errno(r, "Failed to change runlevel: %s", bus_error_message(&error, -r)); | |
117 | ||
118 | return 0; | |
0b7964b8 LP |
119 | } |
120 | ||
121 | static void request_process(Server *s, const struct init_request *req) { | |
122 | assert(s); | |
123 | assert(req); | |
124 | ||
125 | if (req->magic != INIT_MAGIC) { | |
126 | log_error("Got initctl request with invalid magic. Ignoring."); | |
127 | return; | |
128 | } | |
129 | ||
130 | switch (req->cmd) { | |
131 | ||
132 | case INIT_CMD_RUNLVL: | |
133 | if (!isprint(req->runlevel)) | |
134 | log_error("Got invalid runlevel. Ignoring."); | |
135 | else | |
46e964c5 TG |
136 | switch (req->runlevel) { |
137 | ||
138 | /* we are async anyway, so just use kill for reexec/reload */ | |
139 | case 'u': | |
140 | case 'U': | |
141 | if (kill(1, SIGTERM) < 0) | |
56f64d95 | 142 | log_error_errno(errno, "kill() failed: %m"); |
f632a663 LP |
143 | |
144 | /* The bus connection will be | |
145 | * terminated if PID 1 is reexecuted, | |
146 | * hence let's just exit here, and | |
147 | * rely on that we'll be restarted on | |
148 | * the next request */ | |
149 | s->quit = true; | |
46e964c5 TG |
150 | break; |
151 | ||
152 | case 'q': | |
153 | case 'Q': | |
154 | if (kill(1, SIGHUP) < 0) | |
56f64d95 | 155 | log_error_errno(errno, "kill() failed: %m"); |
46e964c5 TG |
156 | break; |
157 | ||
158 | default: | |
73e47c42 | 159 | (void) change_runlevel(s, req->runlevel); |
46e964c5 | 160 | } |
0b7964b8 LP |
161 | return; |
162 | ||
163 | case INIT_CMD_POWERFAIL: | |
164 | case INIT_CMD_POWERFAILNOW: | |
165 | case INIT_CMD_POWEROK: | |
166 | log_warning("Received UPS/power initctl request. This is not implemented in systemd. Upgrade your UPS daemon!"); | |
167 | return; | |
168 | ||
169 | case INIT_CMD_CHANGECONS: | |
170 | log_warning("Received console change initctl request. This is not implemented in systemd."); | |
171 | return; | |
172 | ||
173 | case INIT_CMD_SETENV: | |
174 | case INIT_CMD_UNSETENV: | |
175 | log_warning("Received environment initctl request. This is not implemented in systemd."); | |
176 | return; | |
177 | ||
178 | default: | |
179 | log_warning("Received unknown initctl request. Ignoring."); | |
180 | return; | |
181 | } | |
182 | } | |
183 | ||
184 | static int fifo_process(Fifo *f) { | |
185 | ssize_t l; | |
186 | ||
187 | assert(f); | |
188 | ||
189 | errno = EIO; | |
289f910e ZJS |
190 | l = read(f->fd, |
191 | ((uint8_t*) &f->buffer) + f->bytes_read, | |
192 | sizeof(f->buffer) - f->bytes_read); | |
193 | if (l <= 0) { | |
0b7964b8 LP |
194 | if (errno == EAGAIN) |
195 | return 0; | |
196 | ||
e1427b13 | 197 | return log_warning_errno(errno, "Failed to read from fifo: %m"); |
0b7964b8 LP |
198 | } |
199 | ||
200 | f->bytes_read += l; | |
201 | assert(f->bytes_read <= sizeof(f->buffer)); | |
202 | ||
203 | if (f->bytes_read == sizeof(f->buffer)) { | |
204 | request_process(f->server, &f->buffer); | |
205 | f->bytes_read = 0; | |
206 | } | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | static void fifo_free(Fifo *f) { | |
212 | assert(f); | |
213 | ||
214 | if (f->server) { | |
215 | assert(f->server->n_fifos > 0); | |
216 | f->server->n_fifos--; | |
71fda00f | 217 | LIST_REMOVE(fifo, f->server->fifos, f); |
0b7964b8 LP |
218 | } |
219 | ||
220 | if (f->fd >= 0) { | |
221 | if (f->server) | |
222 | epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL); | |
223 | ||
03e334a1 | 224 | safe_close(f->fd); |
0b7964b8 LP |
225 | } |
226 | ||
227 | free(f); | |
228 | } | |
229 | ||
0b7964b8 LP |
230 | static void server_done(Server *s) { |
231 | assert(s); | |
232 | ||
233 | while (s->fifos) | |
234 | fifo_free(s->fifos); | |
235 | ||
03e334a1 | 236 | safe_close(s->epoll_fd); |
0b7964b8 | 237 | |
fb1af5b0 | 238 | if (s->bus) { |
b75b4db0 TG |
239 | sd_bus_flush(s->bus); |
240 | sd_bus_unref(s->bus); | |
fb1af5b0 | 241 | } |
0b7964b8 LP |
242 | } |
243 | ||
244 | static int server_init(Server *s, unsigned n_sockets) { | |
245 | int r; | |
246 | unsigned i; | |
0b7964b8 LP |
247 | |
248 | assert(s); | |
249 | assert(n_sockets > 0); | |
250 | ||
0b7964b8 LP |
251 | zero(*s); |
252 | ||
3143987f ZJS |
253 | s->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
254 | if (s->epoll_fd < 0) { | |
76ef789d LP |
255 | r = log_error_errno(errno, |
256 | "Failed to create epoll object: %m"); | |
0b7964b8 LP |
257 | goto fail; |
258 | } | |
259 | ||
260 | for (i = 0; i < n_sockets; i++) { | |
261 | struct epoll_event ev; | |
262 | Fifo *f; | |
7c394faa LP |
263 | int fd; |
264 | ||
265 | fd = SD_LISTEN_FDS_START+i; | |
266 | ||
3143987f ZJS |
267 | r = sd_is_fifo(fd, NULL); |
268 | if (r < 0) { | |
c33b3297 | 269 | log_error_errno(r, "Failed to determine file descriptor type: %m"); |
7c394faa LP |
270 | goto fail; |
271 | } | |
272 | ||
273 | if (!r) { | |
274 | log_error("Wrong file descriptor type."); | |
275 | r = -EINVAL; | |
276 | goto fail; | |
277 | } | |
0b7964b8 | 278 | |
3143987f ZJS |
279 | f = new0(Fifo, 1); |
280 | if (!f) { | |
0b7964b8 | 281 | r = -ENOMEM; |
56f64d95 | 282 | log_error_errno(errno, "Failed to create fifo object: %m"); |
0b7964b8 LP |
283 | goto fail; |
284 | } | |
285 | ||
286 | f->fd = -1; | |
287 | ||
288 | zero(ev); | |
289 | ev.events = EPOLLIN; | |
290 | ev.data.ptr = f; | |
7c394faa | 291 | if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
0b7964b8 LP |
292 | r = -errno; |
293 | fifo_free(f); | |
56f64d95 | 294 | log_error_errno(errno, "Failed to add fifo fd to epoll object: %m"); |
0b7964b8 LP |
295 | goto fail; |
296 | } | |
297 | ||
63983207 | 298 | f->fd = fd; |
71fda00f | 299 | LIST_PREPEND(fifo, s->fifos, f); |
0b7964b8 | 300 | f->server = s; |
313cefa1 | 301 | s->n_fifos++; |
0b7964b8 LP |
302 | } |
303 | ||
266f3e26 | 304 | r = bus_connect_system_systemd(&s->bus); |
b75b4db0 | 305 | if (r < 0) { |
da927ba9 | 306 | log_error_errno(r, "Failed to get D-Bus connection: %m"); |
3143987f | 307 | r = -EIO; |
0b7964b8 LP |
308 | goto fail; |
309 | } | |
310 | ||
311 | return 0; | |
312 | ||
313 | fail: | |
314 | server_done(s); | |
315 | ||
0b7964b8 LP |
316 | return r; |
317 | } | |
318 | ||
319 | static int process_event(Server *s, struct epoll_event *ev) { | |
320 | int r; | |
321 | Fifo *f; | |
322 | ||
323 | assert(s); | |
324 | ||
baaa35ad ZJS |
325 | if (!(ev->events & EPOLLIN)) |
326 | return log_info_errno(SYNTHETIC_ERRNO(EIO), | |
327 | "Got invalid event from epoll. (3)"); | |
0b7964b8 LP |
328 | |
329 | f = (Fifo*) ev->data.ptr; | |
289f910e ZJS |
330 | r = fifo_process(f); |
331 | if (r < 0) { | |
da927ba9 | 332 | log_info_errno(r, "Got error on fifo: %m"); |
0b7964b8 LP |
333 | fifo_free(f); |
334 | return r; | |
335 | } | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
340 | int main(int argc, char *argv[]) { | |
341 | Server server; | |
22f4096c | 342 | int r = EXIT_FAILURE, n; |
8bfcc8ea | 343 | |
0ca3f374 LP |
344 | if (getppid() != 1) { |
345 | log_error("This program should be invoked by init only."); | |
22f4096c | 346 | return EXIT_FAILURE; |
0ca3f374 LP |
347 | } |
348 | ||
349 | if (argc > 1) { | |
350 | log_error("This program does not take arguments."); | |
22f4096c | 351 | return EXIT_FAILURE; |
0ca3f374 LP |
352 | } |
353 | ||
6bf3c61c | 354 | log_setup_service(); |
0b7964b8 | 355 | |
4c12626c LP |
356 | umask(0022); |
357 | ||
82d25240 LP |
358 | n = sd_listen_fds(true); |
359 | if (n < 0) { | |
da927ba9 | 360 | log_error_errno(r, "Failed to read listening file descriptors from environment: %m"); |
22f4096c | 361 | return EXIT_FAILURE; |
8bfcc8ea LP |
362 | } |
363 | ||
364 | if (n <= 0 || n > SERVER_FD_MAX) { | |
365 | log_error("No or too many file descriptors passed."); | |
22f4096c | 366 | return EXIT_FAILURE; |
8bfcc8ea | 367 | } |
0b7964b8 | 368 | |
8bfcc8ea | 369 | if (server_init(&server, (unsigned) n) < 0) |
22f4096c | 370 | return EXIT_FAILURE; |
0b7964b8 | 371 | |
df0ff127 | 372 | log_debug("systemd-initctl running as pid "PID_FMT, getpid_cached()); |
cd6d0a45 | 373 | |
8c47c732 LP |
374 | sd_notify(false, |
375 | "READY=1\n" | |
376 | "STATUS=Processing requests..."); | |
377 | ||
f632a663 | 378 | while (!server.quit) { |
0b7964b8 LP |
379 | struct epoll_event event; |
380 | int k; | |
381 | ||
3cc2aff1 LP |
382 | k = epoll_wait(server.epoll_fd, &event, 1, TIMEOUT_MSEC); |
383 | if (k < 0) { | |
0b7964b8 LP |
384 | if (errno == EINTR) |
385 | continue; | |
56f64d95 | 386 | log_error_errno(errno, "epoll_wait() failed: %m"); |
0b7964b8 LP |
387 | goto fail; |
388 | } | |
389 | ||
390 | if (k <= 0) | |
391 | break; | |
392 | ||
e364ad06 | 393 | if (process_event(&server, &event) < 0) |
0b7964b8 LP |
394 | goto fail; |
395 | } | |
cd6d0a45 | 396 | |
22f4096c | 397 | r = EXIT_SUCCESS; |
0b7964b8 | 398 | |
df0ff127 | 399 | log_debug("systemd-initctl stopped as pid "PID_FMT, getpid_cached()); |
cd6d0a45 | 400 | |
0b7964b8 | 401 | fail: |
8c47c732 | 402 | sd_notify(false, |
af4ec430 | 403 | "STOPPING=1\n" |
8c47c732 LP |
404 | "STATUS=Shutting down..."); |
405 | ||
0b7964b8 LP |
406 | server_done(&server); |
407 | ||
0b7964b8 LP |
408 | return r; |
409 | } |