]>
Commit | Line | Data |
---|---|---|
e821075a LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 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 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
bd5f920f LP |
22 | #include <sys/mman.h> |
23 | ||
e821075a LP |
24 | #include "special.h" |
25 | #include "bus-kernel.h" | |
26 | #include "bus-internal.h" | |
27 | #include "bus-util.h" | |
28 | #include "service.h" | |
3c70e3bb LP |
29 | #include "kdbus.h" |
30 | #include "bus-policy.h" | |
e821075a LP |
31 | #include "dbus-busname.h" |
32 | #include "busname.h" | |
6482f626 | 33 | #include "formats-util.h" |
e821075a LP |
34 | |
35 | static const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = { | |
36 | [BUSNAME_DEAD] = UNIT_INACTIVE, | |
a4152e3f | 37 | [BUSNAME_MAKING] = UNIT_ACTIVATING, |
5892a914 | 38 | [BUSNAME_REGISTERED] = UNIT_ACTIVE, |
e821075a LP |
39 | [BUSNAME_LISTENING] = UNIT_ACTIVE, |
40 | [BUSNAME_RUNNING] = UNIT_ACTIVE, | |
a4152e3f LP |
41 | [BUSNAME_SIGTERM] = UNIT_DEACTIVATING, |
42 | [BUSNAME_SIGKILL] = UNIT_DEACTIVATING, | |
e821075a LP |
43 | [BUSNAME_FAILED] = UNIT_FAILED |
44 | }; | |
45 | ||
46 | static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
a4152e3f | 47 | static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
e821075a LP |
48 | |
49 | static void busname_init(Unit *u) { | |
50 | BusName *n = BUSNAME(u); | |
51 | ||
52 | assert(u); | |
53 | assert(u->load_state == UNIT_STUB); | |
54 | ||
55 | n->starter_fd = -1; | |
3f9da416 | 56 | n->accept_fd = true; |
4f101180 | 57 | n->activating = true; |
a4152e3f LP |
58 | |
59 | n->timeout_usec = u->manager->default_timeout_start_usec; | |
60 | } | |
61 | ||
62 | static void busname_unwatch_control_pid(BusName *n) { | |
63 | assert(n); | |
64 | ||
65 | if (n->control_pid <= 0) | |
66 | return; | |
67 | ||
68 | unit_unwatch_pid(UNIT(n), n->control_pid); | |
69 | n->control_pid = 0; | |
70 | } | |
71 | ||
72 | static void busname_free_policy(BusName *n) { | |
73 | BusNamePolicy *p; | |
74 | ||
75 | assert(n); | |
76 | ||
77 | while ((p = n->policy)) { | |
78 | LIST_REMOVE(policy, n->policy, p); | |
79 | ||
80 | free(p->name); | |
81 | free(p); | |
82 | } | |
83 | } | |
84 | ||
85 | static void busname_close_fd(BusName *n) { | |
86 | assert(n); | |
87 | ||
88 | n->starter_event_source = sd_event_source_unref(n->starter_event_source); | |
89 | n->starter_fd = safe_close(n->starter_fd); | |
e821075a LP |
90 | } |
91 | ||
92 | static void busname_done(Unit *u) { | |
93 | BusName *n = BUSNAME(u); | |
94 | ||
a4152e3f | 95 | assert(n); |
e821075a LP |
96 | |
97 | free(n->name); | |
98 | n->name = NULL; | |
99 | ||
a4152e3f LP |
100 | busname_free_policy(n); |
101 | busname_unwatch_control_pid(n); | |
102 | busname_close_fd(n); | |
103 | ||
e821075a LP |
104 | unit_ref_unset(&n->service); |
105 | ||
a4152e3f LP |
106 | n->timer_event_source = sd_event_source_unref(n->timer_event_source); |
107 | } | |
108 | ||
109 | static int busname_arm_timer(BusName *n) { | |
110 | int r; | |
111 | ||
112 | assert(n); | |
113 | ||
114 | if (n->timeout_usec <= 0) { | |
115 | n->timer_event_source = sd_event_source_unref(n->timer_event_source); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | if (n->timer_event_source) { | |
120 | r = sd_event_source_set_time(n->timer_event_source, now(CLOCK_MONOTONIC) + n->timeout_usec); | |
121 | if (r < 0) | |
122 | return r; | |
123 | ||
124 | return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT); | |
125 | } | |
126 | ||
127 | return sd_event_add_time( | |
128 | UNIT(n)->manager->event, | |
129 | &n->timer_event_source, | |
130 | CLOCK_MONOTONIC, | |
131 | now(CLOCK_MONOTONIC) + n->timeout_usec, 0, | |
132 | busname_dispatch_timer, n); | |
e821075a LP |
133 | } |
134 | ||
135 | static int busname_add_default_default_dependencies(BusName *n) { | |
136 | int r; | |
137 | ||
138 | assert(n); | |
139 | ||
140 | r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true); | |
141 | if (r < 0) | |
142 | return r; | |
143 | ||
144 | if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) { | |
145 | r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true); | |
146 | if (r < 0) | |
147 | return r; | |
148 | } | |
149 | ||
150 | return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); | |
151 | } | |
152 | ||
153 | static int busname_add_extras(BusName *n) { | |
154 | Unit *u = UNIT(n); | |
155 | int r; | |
156 | ||
157 | assert(n); | |
158 | ||
159 | if (!n->name) { | |
160 | n->name = unit_name_to_prefix(u->id); | |
161 | if (!n->name) | |
162 | return -ENOMEM; | |
163 | } | |
164 | ||
165 | if (!u->description) { | |
166 | r = unit_set_description(u, n->name); | |
167 | if (r < 0) | |
168 | return r; | |
169 | } | |
170 | ||
5892a914 DM |
171 | if (n->activating) { |
172 | if (!UNIT_DEREF(n->service)) { | |
173 | Unit *x; | |
e821075a | 174 | |
5892a914 DM |
175 | r = unit_load_related_unit(u, ".service", &x); |
176 | if (r < 0) | |
177 | return r; | |
178 | ||
179 | unit_ref_set(&n->service, x); | |
180 | } | |
181 | ||
182 | r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true); | |
e821075a LP |
183 | if (r < 0) |
184 | return r; | |
e821075a LP |
185 | } |
186 | ||
e821075a LP |
187 | if (u->default_dependencies) { |
188 | r = busname_add_default_default_dependencies(n); | |
189 | if (r < 0) | |
190 | return r; | |
191 | } | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
e821075a LP |
196 | static int busname_verify(BusName *n) { |
197 | char *e; | |
198 | ||
199 | assert(n); | |
200 | ||
201 | if (UNIT(n)->load_state != UNIT_LOADED) | |
202 | return 0; | |
203 | ||
204 | if (!service_name_is_valid(n->name)) { | |
79008bdd | 205 | log_unit_error(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id); |
e821075a LP |
206 | return -EINVAL; |
207 | } | |
208 | ||
63c372cb | 209 | e = strjoina(n->name, ".busname"); |
e821075a | 210 | if (!unit_has_name(UNIT(n), e)) { |
79008bdd | 211 | log_unit_error(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id); |
e821075a LP |
212 | return -EINVAL; |
213 | } | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | static int busname_load(Unit *u) { | |
219 | BusName *n = BUSNAME(u); | |
220 | int r; | |
221 | ||
222 | assert(u); | |
223 | assert(u->load_state == UNIT_STUB); | |
224 | ||
225 | r = unit_load_fragment_and_dropin(u); | |
226 | if (r < 0) | |
227 | return r; | |
228 | ||
229 | if (u->load_state == UNIT_LOADED) { | |
230 | /* This is a new unit? Then let's add in some extras */ | |
231 | r = busname_add_extras(n); | |
232 | if (r < 0) | |
233 | return r; | |
234 | } | |
235 | ||
236 | return busname_verify(n); | |
237 | } | |
238 | ||
239 | static void busname_dump(Unit *u, FILE *f, const char *prefix) { | |
240 | BusName *n = BUSNAME(u); | |
241 | ||
242 | assert(n); | |
243 | assert(f); | |
244 | ||
245 | fprintf(f, | |
246 | "%sBus Name State: %s\n" | |
247 | "%sResult: %s\n" | |
3f9da416 | 248 | "%sName: %s\n" |
5892a914 | 249 | "%sActivating: %s\n" |
3f9da416 | 250 | "%sAccept FD: %s\n", |
e821075a LP |
251 | prefix, busname_state_to_string(n->state), |
252 | prefix, busname_result_to_string(n->result), | |
3f9da416 | 253 | prefix, n->name, |
5892a914 | 254 | prefix, yes_no(n->activating), |
3f9da416 | 255 | prefix, yes_no(n->accept_fd)); |
a4152e3f LP |
256 | |
257 | if (n->control_pid > 0) | |
258 | fprintf(f, | |
259 | "%sControl PID: "PID_FMT"\n", | |
260 | prefix, n->control_pid); | |
e821075a LP |
261 | } |
262 | ||
263 | static void busname_unwatch_fd(BusName *n) { | |
264 | int r; | |
265 | ||
266 | assert(n); | |
267 | ||
a4152e3f | 268 | if (!n->starter_event_source) |
e821075a LP |
269 | return; |
270 | ||
a4152e3f LP |
271 | r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF); |
272 | if (r < 0) | |
79008bdd | 273 | log_unit_debug(UNIT(n)->id, "Failed to disable event source."); |
e821075a LP |
274 | } |
275 | ||
276 | static int busname_watch_fd(BusName *n) { | |
277 | int r; | |
278 | ||
279 | assert(n); | |
280 | ||
281 | if (n->starter_fd < 0) | |
282 | return 0; | |
283 | ||
a4152e3f LP |
284 | if (n->starter_event_source) |
285 | r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON); | |
e821075a | 286 | else |
a4152e3f | 287 | r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n); |
e821075a | 288 | if (r < 0) { |
31938a85 | 289 | log_unit_warning_errno(UNIT(n)->id, r, "Failed to watch starter fd: %m"); |
e821075a LP |
290 | busname_unwatch_fd(n); |
291 | return r; | |
292 | } | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static int busname_open_fd(BusName *n) { | |
8f077bf9 ZJS |
298 | _cleanup_free_ char *path = NULL; |
299 | const char *mode; | |
300 | ||
e821075a LP |
301 | assert(n); |
302 | ||
303 | if (n->starter_fd >= 0) | |
304 | return 0; | |
305 | ||
8f077bf9 ZJS |
306 | mode = UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user"; |
307 | n->starter_fd = bus_kernel_open_bus_fd(mode, &path); | |
23bbb0de MS |
308 | if (n->starter_fd < 0) |
309 | return log_unit_warning_errno(UNIT(n)->id, n->starter_fd, "Failed to open %s: %m", path ?: "kdbus"); | |
e821075a LP |
310 | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static void busname_set_state(BusName *n, BusNameState state) { | |
315 | BusNameState old_state; | |
316 | assert(n); | |
317 | ||
318 | old_state = n->state; | |
319 | n->state = state; | |
320 | ||
a4152e3f LP |
321 | if (!IN_SET(state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) { |
322 | n->timer_event_source = sd_event_source_unref(n->timer_event_source); | |
323 | busname_unwatch_control_pid(n); | |
324 | } | |
325 | ||
e821075a LP |
326 | if (state != BUSNAME_LISTENING) |
327 | busname_unwatch_fd(n); | |
328 | ||
a4152e3f | 329 | if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) |
e821075a LP |
330 | busname_close_fd(n); |
331 | ||
332 | if (state != old_state) | |
79008bdd | 333 | log_unit_debug(UNIT(n)->id, "%s changed %s -> %s", |
e821075a LP |
334 | UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state)); |
335 | ||
336 | unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true); | |
337 | } | |
338 | ||
be847e82 | 339 | static int busname_coldplug(Unit *u) { |
e821075a LP |
340 | BusName *n = BUSNAME(u); |
341 | int r; | |
342 | ||
343 | assert(n); | |
344 | assert(n->state == BUSNAME_DEAD); | |
345 | ||
346 | if (n->deserialized_state == n->state) | |
347 | return 0; | |
348 | ||
a4152e3f LP |
349 | if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) { |
350 | ||
351 | if (n->control_pid <= 0) | |
352 | return -EBADMSG; | |
353 | ||
354 | r = unit_watch_pid(UNIT(n), n->control_pid); | |
355 | if (r < 0) | |
356 | return r; | |
357 | ||
358 | r = busname_arm_timer(n); | |
359 | if (r < 0) | |
360 | return r; | |
361 | } | |
362 | ||
363 | if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) { | |
e821075a LP |
364 | r = busname_open_fd(n); |
365 | if (r < 0) | |
366 | return r; | |
367 | } | |
368 | ||
369 | if (n->deserialized_state == BUSNAME_LISTENING) { | |
370 | r = busname_watch_fd(n); | |
371 | if (r < 0) | |
372 | return r; | |
373 | } | |
374 | ||
375 | busname_set_state(n, n->deserialized_state); | |
376 | return 0; | |
377 | } | |
378 | ||
a4152e3f LP |
379 | static int busname_make_starter(BusName *n, pid_t *_pid) { |
380 | pid_t pid; | |
381 | int r; | |
382 | ||
383 | r = busname_arm_timer(n); | |
384 | if (r < 0) | |
385 | goto fail; | |
386 | ||
387 | /* We have to resolve the user/group names out-of-process, | |
388 | * hence let's fork here. It's messy, but well, what can we | |
389 | * do? */ | |
390 | ||
391 | pid = fork(); | |
392 | if (pid < 0) | |
393 | return -errno; | |
394 | ||
395 | if (pid == 0) { | |
396 | int ret; | |
397 | ||
398 | default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1); | |
399 | ignore_signals(SIGPIPE, -1); | |
400 | log_forget_fds(); | |
401 | ||
402 | r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world); | |
403 | if (r < 0) { | |
404 | ret = EXIT_MAKE_STARTER; | |
405 | goto fail_child; | |
406 | } | |
407 | ||
408 | _exit(0); | |
409 | ||
410 | fail_child: | |
411 | log_open(); | |
da927ba9 | 412 | log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD)); |
a4152e3f LP |
413 | |
414 | _exit(ret); | |
415 | } | |
416 | ||
417 | r = unit_watch_pid(UNIT(n), pid); | |
418 | if (r < 0) | |
419 | goto fail; | |
420 | ||
421 | *_pid = pid; | |
422 | return 0; | |
423 | ||
424 | fail: | |
425 | n->timer_event_source = sd_event_source_unref(n->timer_event_source); | |
426 | return r; | |
427 | } | |
428 | ||
e821075a LP |
429 | static void busname_enter_dead(BusName *n, BusNameResult f) { |
430 | assert(n); | |
431 | ||
432 | if (f != BUSNAME_SUCCESS) | |
433 | n->result = f; | |
434 | ||
435 | busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD); | |
436 | } | |
437 | ||
a4152e3f LP |
438 | static void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) { |
439 | KillContext kill_context = {}; | |
e821075a LP |
440 | int r; |
441 | ||
442 | assert(n); | |
443 | ||
a4152e3f LP |
444 | if (f != BUSNAME_SUCCESS) |
445 | n->result = f; | |
446 | ||
447 | kill_context_init(&kill_context); | |
448 | ||
449 | r = unit_kill_context(UNIT(n), | |
450 | &kill_context, | |
db2cb23b | 451 | state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE, |
a4152e3f LP |
452 | -1, |
453 | n->control_pid, | |
454 | false); | |
e821075a | 455 | if (r < 0) { |
31938a85 | 456 | log_unit_warning_errno(UNIT(n)->id, r, "%s failed to kill control process: %m", UNIT(n)->id); |
e821075a LP |
457 | goto fail; |
458 | } | |
459 | ||
a4152e3f LP |
460 | if (r > 0) { |
461 | r = busname_arm_timer(n); | |
462 | if (r < 0) { | |
31938a85 | 463 | log_unit_warning_errno(UNIT(n)->id, r, "%s failed to arm timer: %m", UNIT(n)->id); |
a4152e3f LP |
464 | goto fail; |
465 | } | |
466 | ||
467 | busname_set_state(n, state); | |
468 | } else if (state == BUSNAME_SIGTERM) | |
469 | busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_SUCCESS); | |
470 | else | |
471 | busname_enter_dead(n, BUSNAME_SUCCESS); | |
472 | ||
473 | return; | |
474 | ||
475 | fail: | |
476 | busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES); | |
477 | } | |
478 | ||
479 | static void busname_enter_listening(BusName *n) { | |
480 | int r; | |
481 | ||
482 | assert(n); | |
483 | ||
5892a914 DM |
484 | if (n->activating) { |
485 | r = busname_watch_fd(n); | |
486 | if (r < 0) { | |
31938a85 | 487 | log_unit_warning_errno(UNIT(n)->id, r, "%s failed to watch names: %m", UNIT(n)->id); |
5892a914 DM |
488 | goto fail; |
489 | } | |
490 | ||
491 | busname_set_state(n, BUSNAME_LISTENING); | |
492 | } else | |
493 | busname_set_state(n, BUSNAME_REGISTERED); | |
e821075a | 494 | |
e821075a LP |
495 | return; |
496 | ||
a4152e3f LP |
497 | fail: |
498 | busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES); | |
499 | } | |
500 | ||
501 | static void busname_enter_making(BusName *n) { | |
502 | int r; | |
503 | ||
504 | assert(n); | |
505 | ||
506 | r = busname_open_fd(n); | |
507 | if (r < 0) | |
508 | goto fail; | |
509 | ||
510 | if (n->policy) { | |
8d0e0ddd | 511 | /* If there is a policy, we need to resolve user/group |
a4152e3f LP |
512 | * names, which we can't do from PID1, hence let's |
513 | * fork. */ | |
514 | busname_unwatch_control_pid(n); | |
515 | ||
516 | r = busname_make_starter(n, &n->control_pid); | |
517 | if (r < 0) { | |
31938a85 | 518 | log_unit_warning_errno(UNIT(n)->id, r, "%s failed to fork 'making' task: %m", UNIT(n)->id); |
a4152e3f LP |
519 | goto fail; |
520 | } | |
521 | ||
522 | busname_set_state(n, BUSNAME_MAKING); | |
523 | } else { | |
8d0e0ddd | 524 | /* If there is no policy, we can do everything |
a4152e3f LP |
525 | * directly from PID 1, hence do so. */ |
526 | ||
527 | r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world); | |
528 | if (r < 0) { | |
31938a85 | 529 | log_unit_warning_errno(UNIT(n)->id, r, "%s failed to make starter: %m", UNIT(n)->id); |
a4152e3f LP |
530 | goto fail; |
531 | } | |
532 | ||
533 | busname_enter_listening(n); | |
534 | } | |
535 | ||
536 | return; | |
537 | ||
e821075a LP |
538 | fail: |
539 | busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES); | |
540 | } | |
541 | ||
542 | static void busname_enter_running(BusName *n) { | |
543 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; | |
544 | bool pending = false; | |
545 | Unit *other; | |
546 | Iterator i; | |
547 | int r; | |
548 | ||
549 | assert(n); | |
550 | ||
5892a914 DM |
551 | if (!n->activating) |
552 | return; | |
553 | ||
05a08cb6 | 554 | /* We don't take connections anymore if we are supposed to |
e821075a LP |
555 | * shut down anyway */ |
556 | ||
557 | if (unit_stop_pending(UNIT(n))) { | |
79008bdd | 558 | log_unit_debug(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id); |
16ac4014 LP |
559 | |
560 | /* Flush all queued activation reqeuest by closing and reopening the connection */ | |
ff975efb | 561 | bus_kernel_drop_one(n->starter_fd); |
16ac4014 | 562 | |
16ac4014 | 563 | busname_enter_listening(n); |
e821075a LP |
564 | return; |
565 | } | |
566 | ||
567 | /* If there's already a start pending don't bother to do | |
568 | * anything */ | |
569 | SET_FOREACH(other, UNIT(n)->dependencies[UNIT_TRIGGERS], i) | |
570 | if (unit_active_or_pending(other)) { | |
571 | pending = true; | |
572 | break; | |
573 | } | |
574 | ||
575 | if (!pending) { | |
576 | r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL); | |
577 | if (r < 0) | |
578 | goto fail; | |
579 | } | |
580 | ||
581 | busname_set_state(n, BUSNAME_RUNNING); | |
582 | return; | |
583 | ||
584 | fail: | |
79008bdd | 585 | log_unit_warning(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r)); |
e821075a LP |
586 | busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES); |
587 | } | |
588 | ||
589 | static int busname_start(Unit *u) { | |
590 | BusName *n = BUSNAME(u); | |
591 | ||
592 | assert(n); | |
593 | ||
a4152e3f LP |
594 | /* We cannot fulfill this request right now, try again later |
595 | * please! */ | |
596 | if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) | |
597 | return -EAGAIN; | |
598 | ||
599 | /* Already on it! */ | |
600 | if (n->state == BUSNAME_MAKING) | |
601 | return 0; | |
602 | ||
5892a914 | 603 | if (n->activating && UNIT_ISSET(n->service)) { |
e821075a LP |
604 | Service *service; |
605 | ||
606 | service = SERVICE(UNIT_DEREF(n->service)); | |
607 | ||
608 | if (UNIT(service)->load_state != UNIT_LOADED) { | |
79008bdd | 609 | log_unit_error(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id); |
e821075a LP |
610 | return -ENOENT; |
611 | } | |
612 | } | |
613 | ||
614 | assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED)); | |
615 | ||
616 | n->result = BUSNAME_SUCCESS; | |
a4152e3f | 617 | busname_enter_making(n); |
e821075a | 618 | |
82a2b6bb | 619 | return 1; |
e821075a LP |
620 | } |
621 | ||
622 | static int busname_stop(Unit *u) { | |
623 | BusName *n = BUSNAME(u); | |
624 | ||
625 | assert(n); | |
a4152e3f LP |
626 | |
627 | /* Already on it */ | |
628 | if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) | |
629 | return 0; | |
630 | ||
631 | /* If there's already something running, we go directly into | |
632 | * kill mode. */ | |
633 | ||
634 | if (n->state == BUSNAME_MAKING) { | |
635 | busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_SUCCESS); | |
636 | return -EAGAIN; | |
637 | } | |
638 | ||
5892a914 | 639 | assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING)); |
e821075a LP |
640 | |
641 | busname_enter_dead(n, BUSNAME_SUCCESS); | |
82a2b6bb | 642 | return 1; |
e821075a LP |
643 | } |
644 | ||
645 | static int busname_serialize(Unit *u, FILE *f, FDSet *fds) { | |
646 | BusName *n = BUSNAME(u); | |
647 | ||
648 | assert(n); | |
649 | assert(f); | |
650 | assert(fds); | |
651 | ||
652 | unit_serialize_item(u, f, "state", busname_state_to_string(n->state)); | |
653 | unit_serialize_item(u, f, "result", busname_result_to_string(n->result)); | |
654 | ||
a4152e3f LP |
655 | if (n->control_pid > 0) |
656 | unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid); | |
657 | ||
e821075a LP |
658 | if (n->starter_fd >= 0) { |
659 | int copy; | |
660 | ||
661 | copy = fdset_put_dup(fds, n->starter_fd); | |
662 | if (copy < 0) | |
663 | return copy; | |
664 | ||
665 | unit_serialize_item_format(u, f, "starter-fd", "%i", copy); | |
666 | } | |
667 | ||
668 | return 0; | |
669 | } | |
670 | ||
671 | static int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
672 | BusName *n = BUSNAME(u); | |
673 | ||
674 | assert(n); | |
675 | assert(key); | |
676 | assert(value); | |
677 | ||
678 | if (streq(key, "state")) { | |
679 | BusNameState state; | |
680 | ||
681 | state = busname_state_from_string(value); | |
682 | if (state < 0) | |
79008bdd | 683 | log_unit_debug(u->id, "Failed to parse state value %s", value); |
e821075a LP |
684 | else |
685 | n->deserialized_state = state; | |
686 | ||
687 | } else if (streq(key, "result")) { | |
688 | BusNameResult f; | |
689 | ||
690 | f = busname_result_from_string(value); | |
691 | if (f < 0) | |
79008bdd | 692 | log_unit_debug(u->id, "Failed to parse result value %s", value); |
e821075a LP |
693 | else if (f != BUSNAME_SUCCESS) |
694 | n->result = f; | |
695 | ||
a4152e3f LP |
696 | } else if (streq(key, "control-pid")) { |
697 | pid_t pid; | |
698 | ||
699 | if (parse_pid(value, &pid) < 0) | |
79008bdd | 700 | log_unit_debug(u->id, "Failed to parse control-pid value %s", value); |
a4152e3f LP |
701 | else |
702 | n->control_pid = pid; | |
e821075a LP |
703 | } else if (streq(key, "starter-fd")) { |
704 | int fd; | |
705 | ||
706 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
79008bdd | 707 | log_unit_debug(u->id, "Failed to parse starter fd value %s", value); |
e821075a | 708 | else { |
03e334a1 | 709 | safe_close(n->starter_fd); |
e821075a LP |
710 | n->starter_fd = fdset_remove(fds, fd); |
711 | } | |
712 | } else | |
79008bdd | 713 | log_unit_debug(u->id, "Unknown serialization key '%s'", key); |
e821075a LP |
714 | |
715 | return 0; | |
716 | } | |
717 | ||
718 | _pure_ static UnitActiveState busname_active_state(Unit *u) { | |
719 | assert(u); | |
720 | ||
721 | return state_translation_table[BUSNAME(u)->state]; | |
722 | } | |
723 | ||
724 | _pure_ static const char *busname_sub_state_to_string(Unit *u) { | |
725 | assert(u); | |
726 | ||
727 | return busname_state_to_string(BUSNAME(u)->state); | |
728 | } | |
729 | ||
bd5f920f LP |
730 | static int busname_peek_message(BusName *n) { |
731 | struct kdbus_cmd_recv cmd_recv = { | |
94e15fdc | 732 | .size = sizeof(cmd_recv), |
bd5f920f LP |
733 | .flags = KDBUS_RECV_PEEK, |
734 | }; | |
e24e415e DM |
735 | struct kdbus_cmd_free cmd_free = { |
736 | .size = sizeof(cmd_free), | |
737 | }; | |
bd5f920f LP |
738 | const char *comm = NULL; |
739 | struct kdbus_item *d; | |
740 | struct kdbus_msg *k; | |
741 | size_t start, ps, sz, delta; | |
742 | void *p = NULL; | |
743 | pid_t pid = 0; | |
744 | int r; | |
745 | ||
dcc2fc01 LP |
746 | /* Generate a friendly debug log message about which process |
747 | * caused triggering of this bus name. This simply peeks the | |
748 | * metadata of the first queued message and logs it. */ | |
749 | ||
bd5f920f LP |
750 | assert(n); |
751 | ||
dcc2fc01 LP |
752 | /* Let's shortcut things a bit, if debug logging is turned off |
753 | * anyway. */ | |
754 | ||
755 | if (log_get_max_level() < LOG_DEBUG) | |
756 | return 0; | |
bd5f920f | 757 | |
94e15fdc | 758 | r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv); |
bd5f920f LP |
759 | if (r < 0) { |
760 | if (errno == EINTR || errno == EAGAIN) | |
761 | return 0; | |
762 | ||
79008bdd | 763 | log_unit_error(UNIT(n)->id, "%s: Failed to query activation message: %m", UNIT(n)->id); |
bd5f920f LP |
764 | return -errno; |
765 | } | |
766 | ||
767 | /* We map as late as possible, and unmap imemdiately after | |
768 | * use. On 32bit address space is scarce and we want to be | |
769 | * able to handle a lot of activator connections at the same | |
770 | * time, and hence shouldn't keep the mmap()s around for | |
771 | * longer than necessary. */ | |
772 | ||
773 | ps = page_size(); | |
a9c8343e DM |
774 | start = (cmd_recv.msg.offset / ps) * ps; |
775 | delta = cmd_recv.msg.offset - start; | |
776 | sz = PAGE_ALIGN(delta + cmd_recv.msg.msg_size); | |
bd5f920f LP |
777 | |
778 | p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start); | |
779 | if (p == MAP_FAILED) { | |
79008bdd | 780 | log_unit_error(UNIT(n)->id, "%s: Failed to map activation message: %m", UNIT(n)->id); |
bd5f920f LP |
781 | r = -errno; |
782 | goto finish; | |
783 | } | |
784 | ||
785 | k = (struct kdbus_msg *) ((uint8_t *) p + delta); | |
786 | KDBUS_ITEM_FOREACH(d, k, items) { | |
787 | switch (d->type) { | |
788 | ||
789 | case KDBUS_ITEM_PIDS: | |
790 | pid = d->pids.pid; | |
791 | break; | |
792 | ||
793 | case KDBUS_ITEM_PID_COMM: | |
794 | comm = d->str; | |
795 | break; | |
796 | } | |
797 | } | |
798 | ||
799 | if (pid > 0) | |
79008bdd | 800 | log_unit_debug(UNIT(n)->id, "%s: Activation triggered by process " PID_FMT " (%s)", UNIT(n)->id, pid, strna(comm)); |
bd5f920f LP |
801 | |
802 | r = 0; | |
803 | ||
804 | finish: | |
805 | if (p) | |
806 | (void) munmap(p, sz); | |
807 | ||
a9c8343e | 808 | cmd_free.offset = cmd_recv.msg.offset; |
dcc2fc01 | 809 | if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0) |
79008bdd | 810 | log_unit_warning(UNIT(n)->id, "Failed to free peeked message, ignoring: %m"); |
bd5f920f LP |
811 | |
812 | return r; | |
813 | } | |
814 | ||
e821075a LP |
815 | static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
816 | BusName *n = userdata; | |
817 | ||
818 | assert(n); | |
819 | assert(fd >= 0); | |
820 | ||
821 | if (n->state != BUSNAME_LISTENING) | |
822 | return 0; | |
823 | ||
79008bdd | 824 | log_unit_debug(UNIT(n)->id, "Activation request on %s", UNIT(n)->id); |
e821075a LP |
825 | |
826 | if (revents != EPOLLIN) { | |
79008bdd | 827 | log_unit_error(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.", |
e821075a LP |
828 | UNIT(n)->id, revents); |
829 | goto fail; | |
830 | } | |
831 | ||
bd5f920f | 832 | busname_peek_message(n); |
e821075a LP |
833 | busname_enter_running(n); |
834 | return 0; | |
835 | fail: | |
836 | ||
837 | busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES); | |
838 | return 0; | |
839 | } | |
840 | ||
a4152e3f LP |
841 | static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
842 | BusName *n = BUSNAME(u); | |
843 | BusNameResult f; | |
844 | ||
845 | assert(n); | |
846 | assert(pid >= 0); | |
847 | ||
848 | if (pid != n->control_pid) | |
849 | return; | |
850 | ||
851 | n->control_pid = 0; | |
852 | ||
853 | if (is_clean_exit(code, status, NULL)) | |
854 | f = BUSNAME_SUCCESS; | |
855 | else if (code == CLD_EXITED) | |
856 | f = BUSNAME_FAILURE_EXIT_CODE; | |
857 | else if (code == CLD_KILLED) | |
858 | f = BUSNAME_FAILURE_SIGNAL; | |
deffddf1 | 859 | else if (code == CLD_DUMPED) |
a4152e3f LP |
860 | f = BUSNAME_FAILURE_CORE_DUMP; |
861 | else | |
862 | assert_not_reached("Unknown sigchld code"); | |
863 | ||
79008bdd LP |
864 | log_unit_full(u->id, |
865 | f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE, | |
866 | "%s control process exited, code=%s status=%i", | |
a4152e3f LP |
867 | u->id, sigchld_code_to_string(code), status); |
868 | ||
869 | if (f != BUSNAME_SUCCESS) | |
870 | n->result = f; | |
871 | ||
872 | switch (n->state) { | |
873 | ||
874 | case BUSNAME_MAKING: | |
875 | if (f == BUSNAME_SUCCESS) | |
876 | busname_enter_listening(n); | |
877 | else | |
878 | busname_enter_signal(n, BUSNAME_SIGTERM, f); | |
879 | break; | |
880 | ||
881 | case BUSNAME_SIGTERM: | |
882 | case BUSNAME_SIGKILL: | |
883 | busname_enter_dead(n, f); | |
884 | break; | |
885 | ||
886 | default: | |
887 | assert_not_reached("Uh, control process died at wrong time."); | |
888 | } | |
889 | ||
890 | /* Notify clients about changed exit status */ | |
891 | unit_add_to_dbus_queue(u); | |
892 | } | |
893 | ||
894 | static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { | |
895 | BusName *n = BUSNAME(userdata); | |
896 | ||
897 | assert(n); | |
898 | assert(n->timer_event_source == source); | |
899 | ||
900 | switch (n->state) { | |
901 | ||
902 | case BUSNAME_MAKING: | |
79008bdd | 903 | log_unit_warning(UNIT(n)->id, "%s making timed out. Terminating.", UNIT(n)->id); |
a4152e3f LP |
904 | busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT); |
905 | break; | |
906 | ||
907 | case BUSNAME_SIGTERM: | |
79008bdd | 908 | log_unit_warning(UNIT(n)->id, "%s stopping timed out. Killing.", UNIT(n)->id); |
a4152e3f LP |
909 | busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT); |
910 | break; | |
911 | ||
912 | case BUSNAME_SIGKILL: | |
79008bdd | 913 | log_unit_warning(UNIT(n)->id, "%s still around after SIGKILL. Ignoring.", UNIT(n)->id); |
a4152e3f LP |
914 | busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT); |
915 | break; | |
916 | ||
917 | default: | |
918 | assert_not_reached("Timeout at wrong time."); | |
919 | } | |
920 | ||
921 | return 0; | |
922 | } | |
923 | ||
e821075a LP |
924 | static void busname_reset_failed(Unit *u) { |
925 | BusName *n = BUSNAME(u); | |
926 | ||
927 | assert(n); | |
928 | ||
929 | if (n->state == BUSNAME_FAILED) | |
930 | busname_set_state(n, BUSNAME_DEAD); | |
931 | ||
932 | n->result = BUSNAME_SUCCESS; | |
933 | } | |
934 | ||
935 | static void busname_trigger_notify(Unit *u, Unit *other) { | |
936 | BusName *n = BUSNAME(u); | |
937 | Service *s; | |
938 | ||
939 | assert(n); | |
940 | assert(other); | |
941 | ||
942 | if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING)) | |
943 | return; | |
944 | ||
945 | if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE) | |
946 | return; | |
947 | ||
948 | s = SERVICE(other); | |
949 | ||
2f671520 LP |
950 | if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT) |
951 | busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT); | |
952 | else if (IN_SET(s->state, | |
953 | SERVICE_DEAD, SERVICE_FAILED, | |
954 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, | |
955 | SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, | |
956 | SERVICE_AUTO_RESTART)) | |
e821075a LP |
957 | busname_enter_listening(n); |
958 | } | |
959 | ||
a4152e3f LP |
960 | static int busname_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
961 | return unit_kill_common(u, who, signo, -1, BUSNAME(u)->control_pid, error); | |
962 | } | |
963 | ||
964 | static int busname_get_timeout(Unit *u, uint64_t *timeout) { | |
965 | BusName *n = BUSNAME(u); | |
966 | int r; | |
967 | ||
968 | if (!n->timer_event_source) | |
969 | return 0; | |
970 | ||
971 | r = sd_event_source_get_time(n->timer_event_source, timeout); | |
972 | if (r < 0) | |
973 | return r; | |
974 | ||
975 | return 1; | |
976 | } | |
977 | ||
0faacd47 | 978 | static bool busname_supported(Manager *m) { |
d69a7cea | 979 | static int supported = -1; |
0faacd47 LP |
980 | assert(m); |
981 | ||
982 | if (supported < 0) | |
983 | supported = access("/sys/fs/kdbus", F_OK) >= 0; | |
984 | ||
985 | return supported; | |
986 | } | |
987 | ||
e821075a LP |
988 | static const char* const busname_state_table[_BUSNAME_STATE_MAX] = { |
989 | [BUSNAME_DEAD] = "dead", | |
a4152e3f | 990 | [BUSNAME_MAKING] = "making", |
5892a914 | 991 | [BUSNAME_REGISTERED] = "registered", |
e821075a LP |
992 | [BUSNAME_LISTENING] = "listening", |
993 | [BUSNAME_RUNNING] = "running", | |
a4152e3f LP |
994 | [BUSNAME_SIGTERM] = "sigterm", |
995 | [BUSNAME_SIGKILL] = "sigkill", | |
996 | [BUSNAME_FAILED] = "failed", | |
e821075a LP |
997 | }; |
998 | ||
999 | DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState); | |
1000 | ||
1001 | static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = { | |
1002 | [BUSNAME_SUCCESS] = "success", | |
1003 | [BUSNAME_FAILURE_RESOURCES] = "resources", | |
a4152e3f LP |
1004 | [BUSNAME_FAILURE_TIMEOUT] = "timeout", |
1005 | [BUSNAME_FAILURE_EXIT_CODE] = "exit-code", | |
1006 | [BUSNAME_FAILURE_SIGNAL] = "signal", | |
1007 | [BUSNAME_FAILURE_CORE_DUMP] = "core-dump", | |
700ff4d9 | 1008 | [BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent", |
e821075a LP |
1009 | }; |
1010 | ||
1011 | DEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult); | |
1012 | ||
1013 | const UnitVTable busname_vtable = { | |
1014 | .object_size = sizeof(BusName), | |
1015 | ||
1016 | .sections = | |
1017 | "Unit\0" | |
1018 | "BusName\0" | |
1019 | "Install\0", | |
1020 | .private_section = "BusName", | |
1021 | ||
1022 | .init = busname_init, | |
1023 | .done = busname_done, | |
1024 | .load = busname_load, | |
1025 | ||
1026 | .coldplug = busname_coldplug, | |
1027 | ||
1028 | .dump = busname_dump, | |
1029 | ||
1030 | .start = busname_start, | |
1031 | .stop = busname_stop, | |
1032 | ||
a4152e3f LP |
1033 | .kill = busname_kill, |
1034 | ||
1035 | .get_timeout = busname_get_timeout, | |
1036 | ||
e821075a LP |
1037 | .serialize = busname_serialize, |
1038 | .deserialize_item = busname_deserialize_item, | |
1039 | ||
1040 | .active_state = busname_active_state, | |
1041 | .sub_state_to_string = busname_sub_state_to_string, | |
1042 | ||
a4152e3f LP |
1043 | .sigchld_event = busname_sigchld_event, |
1044 | ||
e821075a LP |
1045 | .trigger_notify = busname_trigger_notify, |
1046 | ||
1047 | .reset_failed = busname_reset_failed, | |
1048 | ||
0faacd47 LP |
1049 | .supported = busname_supported, |
1050 | ||
e821075a LP |
1051 | .bus_interface = "org.freedesktop.systemd1.BusName", |
1052 | .bus_vtable = bus_busname_vtable, | |
e821075a LP |
1053 | |
1054 | .status_message_formats = { | |
1055 | .finished_start_job = { | |
1056 | [JOB_DONE] = "Listening on %s.", | |
1057 | [JOB_FAILED] = "Failed to listen on %s.", | |
1058 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
1059 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
1060 | }, | |
1061 | .finished_stop_job = { | |
1062 | [JOB_DONE] = "Closed %s.", | |
1063 | [JOB_FAILED] = "Failed stopping %s.", | |
1064 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
1065 | }, | |
1066 | }, | |
1067 | }; |