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