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