]>
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> |
ca78ad1d ZJS |
7 | #include <sys/stat.h> |
8 | #include <sys/types.h> | |
cf0fbc49 | 9 | #include <unistd.h> |
0b7964b8 | 10 | |
b75b4db0 | 11 | #include "sd-bus.h" |
3ffd4af2 | 12 | #include "sd-daemon.h" |
0b7964b8 | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
b75b4db0 | 15 | #include "bus-error.h" |
3ffd4af2 | 16 | #include "bus-util.h" |
ef9eb0a8 | 17 | #include "def.h" |
3ffd4af2 | 18 | #include "fd-util.h" |
f97b34a6 | 19 | #include "format-util.h" |
3ffd4af2 LP |
20 | #include "initreq.h" |
21 | #include "list.h" | |
22 | #include "log.h" | |
0a970718 | 23 | #include "memory-util.h" |
dccca82b | 24 | #include "process-util.h" |
0a970718 | 25 | #include "special.h" |
0b7964b8 | 26 | |
0b7964b8 | 27 | #define SERVER_FD_MAX 16 |
ef9eb0a8 | 28 | #define TIMEOUT_MSEC ((int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)) |
0b7964b8 LP |
29 | |
30 | typedef struct Fifo Fifo; | |
31 | ||
32 | typedef struct Server { | |
33 | int epoll_fd; | |
34 | ||
35 | LIST_HEAD(Fifo, fifos); | |
36 | unsigned n_fifos; | |
37 | ||
b75b4db0 | 38 | sd_bus *bus; |
f632a663 LP |
39 | |
40 | bool quit; | |
0b7964b8 LP |
41 | } Server; |
42 | ||
43 | struct Fifo { | |
44 | Server *server; | |
45 | ||
46 | int fd; | |
47 | ||
48 | struct init_request buffer; | |
49 | size_t bytes_read; | |
50 | ||
51 | LIST_FIELDS(Fifo, fifo); | |
52 | }; | |
53 | ||
ac83842a | 54 | static const char *translate_runlevel(int runlevel, bool *isolate) { |
6542952f LP |
55 | static const struct { |
56 | const int runlevel; | |
57 | const char *special; | |
ac83842a | 58 | bool isolate; |
6542952f | 59 | } table[] = { |
d5d8429a LP |
60 | { '0', SPECIAL_POWEROFF_TARGET, false }, |
61 | { '1', SPECIAL_RESCUE_TARGET, true }, | |
62 | { 's', SPECIAL_RESCUE_TARGET, true }, | |
63 | { 'S', SPECIAL_RESCUE_TARGET, true }, | |
64 | { '2', SPECIAL_MULTI_USER_TARGET, true }, | |
65 | { '3', SPECIAL_MULTI_USER_TARGET, true }, | |
66 | { '4', SPECIAL_MULTI_USER_TARGET, true }, | |
67 | { '5', SPECIAL_GRAPHICAL_TARGET, true }, | |
68 | { '6', SPECIAL_REBOOT_TARGET, false }, | |
6542952f LP |
69 | }; |
70 | ||
71 | unsigned i; | |
72 | ||
ac83842a LP |
73 | assert(isolate); |
74 | ||
6542952f | 75 | for (i = 0; i < ELEMENTSOF(table); i++) |
ac83842a LP |
76 | if (table[i].runlevel == runlevel) { |
77 | *isolate = table[i].isolate; | |
65457142 FC |
78 | if (runlevel == '6' && kexec_loaded()) |
79 | return SPECIAL_KEXEC_TARGET; | |
6542952f | 80 | return table[i].special; |
ac83842a | 81 | } |
6542952f LP |
82 | |
83 | return NULL; | |
0b7964b8 LP |
84 | } |
85 | ||
73e47c42 | 86 | static int change_runlevel(Server *s, int runlevel) { |
0b7964b8 | 87 | const char *target; |
4afd3348 | 88 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
ac83842a LP |
89 | const char *mode; |
90 | bool isolate = false; | |
b75b4db0 | 91 | int r; |
0b7964b8 LP |
92 | |
93 | assert(s); | |
94 | ||
b75b4db0 | 95 | target = translate_runlevel(runlevel, &isolate); |
f168c273 | 96 | if (!target) { |
0b7964b8 | 97 | log_warning("Got request for unknown runlevel %c, ignoring.", runlevel); |
73e47c42 | 98 | return 0; |
0b7964b8 LP |
99 | } |
100 | ||
ac83842a LP |
101 | if (isolate) |
102 | mode = "isolate"; | |
103 | else | |
4c10771c | 104 | mode = "replace-irreversibly"; |
ac83842a | 105 | |
6f0d624e | 106 | log_debug("Running request %s/start/%s", target, mode); |
0b7964b8 | 107 | |
b75b4db0 TG |
108 | r = sd_bus_call_method( |
109 | s->bus, | |
110 | "org.freedesktop.systemd1", | |
111 | "/org/freedesktop/systemd1", | |
112 | "org.freedesktop.systemd1.Manager", | |
113 | "StartUnit", | |
114 | &error, | |
115 | NULL, | |
116 | "ss", target, mode); | |
73e47c42 YW |
117 | if (r < 0) |
118 | return log_error_errno(r, "Failed to change runlevel: %s", bus_error_message(&error, -r)); | |
119 | ||
120 | return 0; | |
0b7964b8 LP |
121 | } |
122 | ||
123 | static void request_process(Server *s, const struct init_request *req) { | |
124 | assert(s); | |
125 | assert(req); | |
126 | ||
127 | if (req->magic != INIT_MAGIC) { | |
128 | log_error("Got initctl request with invalid magic. Ignoring."); | |
129 | return; | |
130 | } | |
131 | ||
132 | switch (req->cmd) { | |
133 | ||
134 | case INIT_CMD_RUNLVL: | |
135 | if (!isprint(req->runlevel)) | |
136 | log_error("Got invalid runlevel. Ignoring."); | |
137 | else | |
46e964c5 TG |
138 | switch (req->runlevel) { |
139 | ||
140 | /* we are async anyway, so just use kill for reexec/reload */ | |
141 | case 'u': | |
142 | case 'U': | |
143 | if (kill(1, SIGTERM) < 0) | |
56f64d95 | 144 | log_error_errno(errno, "kill() failed: %m"); |
f632a663 LP |
145 | |
146 | /* The bus connection will be | |
147 | * terminated if PID 1 is reexecuted, | |
148 | * hence let's just exit here, and | |
149 | * rely on that we'll be restarted on | |
150 | * the next request */ | |
151 | s->quit = true; | |
46e964c5 TG |
152 | break; |
153 | ||
154 | case 'q': | |
155 | case 'Q': | |
156 | if (kill(1, SIGHUP) < 0) | |
56f64d95 | 157 | log_error_errno(errno, "kill() failed: %m"); |
46e964c5 TG |
158 | break; |
159 | ||
160 | default: | |
73e47c42 | 161 | (void) change_runlevel(s, req->runlevel); |
46e964c5 | 162 | } |
0b7964b8 LP |
163 | return; |
164 | ||
165 | case INIT_CMD_POWERFAIL: | |
166 | case INIT_CMD_POWERFAILNOW: | |
167 | case INIT_CMD_POWEROK: | |
168 | log_warning("Received UPS/power initctl request. This is not implemented in systemd. Upgrade your UPS daemon!"); | |
169 | return; | |
170 | ||
171 | case INIT_CMD_CHANGECONS: | |
172 | log_warning("Received console change initctl request. This is not implemented in systemd."); | |
173 | return; | |
174 | ||
175 | case INIT_CMD_SETENV: | |
176 | case INIT_CMD_UNSETENV: | |
177 | log_warning("Received environment initctl request. This is not implemented in systemd."); | |
178 | return; | |
179 | ||
180 | default: | |
181 | log_warning("Received unknown initctl request. Ignoring."); | |
182 | return; | |
183 | } | |
184 | } | |
185 | ||
186 | static int fifo_process(Fifo *f) { | |
187 | ssize_t l; | |
188 | ||
189 | assert(f); | |
190 | ||
191 | errno = EIO; | |
289f910e ZJS |
192 | l = read(f->fd, |
193 | ((uint8_t*) &f->buffer) + f->bytes_read, | |
194 | sizeof(f->buffer) - f->bytes_read); | |
195 | if (l <= 0) { | |
0b7964b8 LP |
196 | if (errno == EAGAIN) |
197 | return 0; | |
198 | ||
e1427b13 | 199 | return log_warning_errno(errno, "Failed to read from fifo: %m"); |
0b7964b8 LP |
200 | } |
201 | ||
202 | f->bytes_read += l; | |
203 | assert(f->bytes_read <= sizeof(f->buffer)); | |
204 | ||
205 | if (f->bytes_read == sizeof(f->buffer)) { | |
206 | request_process(f->server, &f->buffer); | |
207 | f->bytes_read = 0; | |
208 | } | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | static void fifo_free(Fifo *f) { | |
214 | assert(f); | |
215 | ||
216 | if (f->server) { | |
217 | assert(f->server->n_fifos > 0); | |
218 | f->server->n_fifos--; | |
71fda00f | 219 | LIST_REMOVE(fifo, f->server->fifos, f); |
0b7964b8 LP |
220 | } |
221 | ||
222 | if (f->fd >= 0) { | |
223 | if (f->server) | |
0e05be84 | 224 | (void) epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL); |
0b7964b8 | 225 | |
03e334a1 | 226 | safe_close(f->fd); |
0b7964b8 LP |
227 | } |
228 | ||
229 | free(f); | |
230 | } | |
231 | ||
0b7964b8 LP |
232 | static void server_done(Server *s) { |
233 | assert(s); | |
234 | ||
235 | while (s->fifos) | |
236 | fifo_free(s->fifos); | |
237 | ||
aae82d1a LP |
238 | s->epoll_fd = safe_close(s->epoll_fd); |
239 | s->bus = sd_bus_flush_close_unref(s->bus); | |
0b7964b8 LP |
240 | } |
241 | ||
242 | static int server_init(Server *s, unsigned n_sockets) { | |
243 | int r; | |
244 | unsigned i; | |
0b7964b8 LP |
245 | |
246 | assert(s); | |
247 | assert(n_sockets > 0); | |
248 | ||
41ab8c67 LP |
249 | *s = (struct Server) { |
250 | .epoll_fd = epoll_create1(EPOLL_CLOEXEC), | |
251 | }; | |
0b7964b8 | 252 | |
3143987f | 253 | if (s->epoll_fd < 0) { |
76ef789d LP |
254 | r = log_error_errno(errno, |
255 | "Failed to create epoll object: %m"); | |
0b7964b8 LP |
256 | goto fail; |
257 | } | |
258 | ||
259 | for (i = 0; i < n_sockets; i++) { | |
0b7964b8 | 260 | Fifo *f; |
7c394faa LP |
261 | int fd; |
262 | ||
263 | fd = SD_LISTEN_FDS_START+i; | |
264 | ||
3143987f ZJS |
265 | r = sd_is_fifo(fd, NULL); |
266 | if (r < 0) { | |
c33b3297 | 267 | log_error_errno(r, "Failed to determine file descriptor type: %m"); |
7c394faa LP |
268 | goto fail; |
269 | } | |
270 | ||
271 | if (!r) { | |
272 | log_error("Wrong file descriptor type."); | |
273 | r = -EINVAL; | |
274 | goto fail; | |
275 | } | |
0b7964b8 | 276 | |
3143987f ZJS |
277 | f = new0(Fifo, 1); |
278 | if (!f) { | |
0b7964b8 | 279 | r = -ENOMEM; |
56f64d95 | 280 | log_error_errno(errno, "Failed to create fifo object: %m"); |
0b7964b8 LP |
281 | goto fail; |
282 | } | |
283 | ||
284 | f->fd = -1; | |
285 | ||
41ab8c67 LP |
286 | struct epoll_event ev = { |
287 | .events = EPOLLIN, | |
288 | .data.ptr = f, | |
289 | }; | |
290 | ||
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 | } |