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