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