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