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