]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/busname.c
core,network: major per-object logging rework
[thirdparty/systemd.git] / src / core / busname.c
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
22 #include <sys/mman.h>
23
24 #include "special.h"
25 #include "bus-kernel.h"
26 #include "bus-internal.h"
27 #include "bus-util.h"
28 #include "service.h"
29 #include "kdbus.h"
30 #include "bus-policy.h"
31 #include "dbus-busname.h"
32 #include "busname.h"
33 #include "formats-util.h"
34
35 static const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
36 [BUSNAME_DEAD] = UNIT_INACTIVE,
37 [BUSNAME_MAKING] = UNIT_ACTIVATING,
38 [BUSNAME_REGISTERED] = UNIT_ACTIVE,
39 [BUSNAME_LISTENING] = UNIT_ACTIVE,
40 [BUSNAME_RUNNING] = UNIT_ACTIVE,
41 [BUSNAME_SIGTERM] = UNIT_DEACTIVATING,
42 [BUSNAME_SIGKILL] = UNIT_DEACTIVATING,
43 [BUSNAME_FAILED] = UNIT_FAILED
44 };
45
46 static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
47 static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
48
49 static 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;
56 n->accept_fd = true;
57 n->activating = true;
58
59 n->timeout_usec = u->manager->default_timeout_start_usec;
60 }
61
62 static 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
72 static 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
85 static 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);
90 }
91
92 static void busname_done(Unit *u) {
93 BusName *n = BUSNAME(u);
94
95 assert(n);
96
97 free(n->name);
98 n->name = NULL;
99
100 busname_free_policy(n);
101 busname_unwatch_control_pid(n);
102 busname_close_fd(n);
103
104 unit_ref_unset(&n->service);
105
106 n->timer_event_source = sd_event_source_unref(n->timer_event_source);
107 }
108
109 static int busname_arm_timer(BusName *n) {
110 int r;
111
112 assert(n);
113
114 if (n->timeout_usec <= 0) {
115 n->timer_event_source = sd_event_source_unref(n->timer_event_source);
116 return 0;
117 }
118
119 if (n->timer_event_source) {
120 r = sd_event_source_set_time(n->timer_event_source, now(CLOCK_MONOTONIC) + n->timeout_usec);
121 if (r < 0)
122 return r;
123
124 return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
125 }
126
127 r = sd_event_add_time(
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);
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;
139 }
140
141 static int busname_add_default_default_dependencies(BusName *n) {
142 int r;
143
144 assert(n);
145
146 r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
147 if (r < 0)
148 return r;
149
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
159 static int busname_add_extras(BusName *n) {
160 Unit *u = UNIT(n);
161 int r;
162
163 assert(n);
164
165 if (!n->name) {
166 r = unit_name_to_prefix(u->id, &n->name);
167 if (r < 0)
168 return r;
169 }
170
171 if (!u->description) {
172 r = unit_set_description(u, n->name);
173 if (r < 0)
174 return r;
175 }
176
177 if (n->activating) {
178 if (!UNIT_DEREF(n->service)) {
179 Unit *x;
180
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);
189 if (r < 0)
190 return r;
191 }
192
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
202 static int busname_verify(BusName *n) {
203 char *e;
204
205 assert(n);
206
207 if (UNIT(n)->load_state != UNIT_LOADED)
208 return 0;
209
210 if (!service_name_is_valid(n->name)) {
211 log_unit_error(UNIT(n), "Name= setting is not a valid service name Refusing.");
212 return -EINVAL;
213 }
214
215 e = strjoina(n->name, ".busname");
216 if (!unit_has_name(UNIT(n), e)) {
217 log_unit_error(UNIT(n), "Name= setting doesn't match unit name. Refusing.");
218 return -EINVAL;
219 }
220
221 return 0;
222 }
223
224 static int busname_load(Unit *u) {
225 BusName *n = BUSNAME(u);
226 int r;
227
228 assert(u);
229 assert(u->load_state == UNIT_STUB);
230
231 r = unit_load_fragment_and_dropin(u);
232 if (r < 0)
233 return r;
234
235 if (u->load_state == UNIT_LOADED) {
236 /* This is a new unit? Then let's add in some extras */
237 r = busname_add_extras(n);
238 if (r < 0)
239 return r;
240 }
241
242 return busname_verify(n);
243 }
244
245 static void busname_dump(Unit *u, FILE *f, const char *prefix) {
246 BusName *n = BUSNAME(u);
247
248 assert(n);
249 assert(f);
250
251 fprintf(f,
252 "%sBus Name State: %s\n"
253 "%sResult: %s\n"
254 "%sName: %s\n"
255 "%sActivating: %s\n"
256 "%sAccept FD: %s\n",
257 prefix, busname_state_to_string(n->state),
258 prefix, busname_result_to_string(n->result),
259 prefix, n->name,
260 prefix, yes_no(n->activating),
261 prefix, yes_no(n->accept_fd));
262
263 if (n->control_pid > 0)
264 fprintf(f,
265 "%sControl PID: "PID_FMT"\n",
266 prefix, n->control_pid);
267 }
268
269 static void busname_unwatch_fd(BusName *n) {
270 int r;
271
272 assert(n);
273
274 if (!n->starter_event_source)
275 return;
276
277 r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
278 if (r < 0)
279 log_unit_debug_errno(UNIT(n), r, "Failed to disable event source: %m");
280 }
281
282 static int busname_watch_fd(BusName *n) {
283 int r;
284
285 assert(n);
286
287 if (n->starter_fd < 0)
288 return 0;
289
290 if (n->starter_event_source)
291 r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
292 else
293 r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
294
295 if (r < 0) {
296 log_unit_warning_errno(UNIT(n), r, "Failed to watch starter fd: %m");
297 busname_unwatch_fd(n);
298 return r;
299 }
300
301 (void) sd_event_source_set_description(n->starter_event_source, "busname-starter");
302
303 return 0;
304 }
305
306 static int busname_open_fd(BusName *n) {
307 _cleanup_free_ char *path = NULL;
308 const char *mode;
309
310 assert(n);
311
312 if (n->starter_fd >= 0)
313 return 0;
314
315 mode = UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user";
316 n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
317 if (n->starter_fd < 0)
318 return log_unit_warning_errno(UNIT(n), n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");
319
320 return 0;
321 }
322
323 static 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
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
335 if (state != BUSNAME_LISTENING)
336 busname_unwatch_fd(n);
337
338 if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING))
339 busname_close_fd(n);
340
341 if (state != old_state)
342 log_unit_debug(UNIT(n), "Changed %s -> %s", busname_state_to_string(old_state), busname_state_to_string(state));
343
344 unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
345 }
346
347 static int busname_coldplug(Unit *u) {
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
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)) {
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
387 static 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();
420 log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
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
432 fail:
433 n->timer_event_source = sd_event_source_unref(n->timer_event_source);
434 return r;
435 }
436
437 static 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
446 static void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
447 KillContext kill_context = {};
448 int r;
449
450 assert(n);
451
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,
459 state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE,
460 -1,
461 n->control_pid,
462 false);
463 if (r < 0) {
464 log_unit_warning_errno(UNIT(n), r, "Failed to kill control process: %m");
465 goto fail;
466 }
467
468 if (r > 0) {
469 r = busname_arm_timer(n);
470 if (r < 0) {
471 log_unit_warning_errno(UNIT(n), r, "Failed to arm timer: %m");
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
483 fail:
484 busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
485 }
486
487 static void busname_enter_listening(BusName *n) {
488 int r;
489
490 assert(n);
491
492 if (n->activating) {
493 r = busname_watch_fd(n);
494 if (r < 0) {
495 log_unit_warning_errno(UNIT(n), r, "Failed to watch names: %m");
496 goto fail;
497 }
498
499 busname_set_state(n, BUSNAME_LISTENING);
500 } else
501 busname_set_state(n, BUSNAME_REGISTERED);
502
503 return;
504
505 fail:
506 busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
507 }
508
509 static 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) {
519 /* If there is a policy, we need to resolve user/group
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) {
526 log_unit_warning_errno(UNIT(n), r, "Failed to fork 'making' task: %m");
527 goto fail;
528 }
529
530 busname_set_state(n, BUSNAME_MAKING);
531 } else {
532 /* If there is no policy, we can do everything
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) {
537 log_unit_warning_errno(UNIT(n), r, "Failed to make starter: %m");
538 goto fail;
539 }
540
541 busname_enter_listening(n);
542 }
543
544 return;
545
546 fail:
547 busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
548 }
549
550 static 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
559 if (!n->activating)
560 return;
561
562 /* We don't take connections anymore if we are supposed to
563 * shut down anyway */
564
565 if (unit_stop_pending(UNIT(n))) {
566 log_unit_debug(UNIT(n), "Suppressing activation request since unit stop is scheduled.");
567
568 /* Flush all queued activation reqeuest by closing and reopening the connection */
569 bus_kernel_drop_one(n->starter_fd);
570
571 busname_enter_listening(n);
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
592 fail:
593 log_unit_warning(UNIT(n), "Failed to queue service startup job: %s", bus_error_message(&error, r));
594 busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
595 }
596
597 static int busname_start(Unit *u) {
598 BusName *n = BUSNAME(u);
599
600 assert(n);
601
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
611 if (n->activating && UNIT_ISSET(n->service)) {
612 Service *service;
613
614 service = SERVICE(UNIT_DEREF(n->service));
615
616 if (UNIT(service)->load_state != UNIT_LOADED) {
617 log_unit_error(u, "Bus service %s not loaded, refusing.", UNIT(service)->id);
618 return -ENOENT;
619 }
620 }
621
622 assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
623
624 n->result = BUSNAME_SUCCESS;
625 busname_enter_making(n);
626
627 return 1;
628 }
629
630 static int busname_stop(Unit *u) {
631 BusName *n = BUSNAME(u);
632
633 assert(n);
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
647 assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
648
649 busname_enter_dead(n, BUSNAME_SUCCESS);
650 return 1;
651 }
652
653 static 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
663 if (n->control_pid > 0)
664 unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid);
665
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
679 static 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)
691 log_unit_debug(u, "Failed to parse state value: %s", value);
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)
700 log_unit_debug(u, "Failed to parse result value: %s", value);
701 else if (f != BUSNAME_SUCCESS)
702 n->result = f;
703
704 } else if (streq(key, "control-pid")) {
705 pid_t pid;
706
707 if (parse_pid(value, &pid) < 0)
708 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
709 else
710 n->control_pid = pid;
711 } else if (streq(key, "starter-fd")) {
712 int fd;
713
714 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
715 log_unit_debug(u, "Failed to parse starter fd value: %s", value);
716 else {
717 safe_close(n->starter_fd);
718 n->starter_fd = fdset_remove(fds, fd);
719 }
720 } else
721 log_unit_debug(u, "Unknown serialization key: %s", key);
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
738 static int busname_peek_message(BusName *n) {
739 struct kdbus_cmd_recv cmd_recv = {
740 .size = sizeof(cmd_recv),
741 .flags = KDBUS_RECV_PEEK,
742 };
743 struct kdbus_cmd_free cmd_free = {
744 .size = sizeof(cmd_free),
745 };
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
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
758 assert(n);
759
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;
765
766 r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv);
767 if (r < 0) {
768 if (errno == EINTR || errno == EAGAIN)
769 return 0;
770
771 return log_unit_error_errno(UNIT(n), errno, "Failed to query activation message: %m");
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();
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);
784
785 p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
786 if (p == MAP_FAILED) {
787 r = log_unit_error_errno(UNIT(n), errno, "Failed to map activation message: %m");
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)
806 log_unit_debug(UNIT(n), "Activation triggered by process " PID_FMT " (%s)", pid, strna(comm));
807
808 r = 0;
809
810 finish:
811 if (p)
812 (void) munmap(p, sz);
813
814 cmd_free.offset = cmd_recv.msg.offset;
815 if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
816 log_unit_warning(UNIT(n), "Failed to free peeked message, ignoring: %m");
817
818 return r;
819 }
820
821 static 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
830 log_unit_debug(UNIT(n), "Activation request");
831
832 if (revents != EPOLLIN) {
833 log_unit_error(UNIT(n), "Got unexpected poll event (0x%x) on starter fd.", revents);
834 goto fail;
835 }
836
837 busname_peek_message(n);
838 busname_enter_running(n);
839 return 0;
840 fail:
841
842 busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
843 return 0;
844 }
845
846 static 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;
864 else if (code == CLD_DUMPED)
865 f = BUSNAME_FAILURE_CORE_DUMP;
866 else
867 assert_not_reached("Unknown sigchld code");
868
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);
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
897 static 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:
906 log_unit_warning(UNIT(n), "Making timed out. Terminating.");
907 busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
908 break;
909
910 case BUSNAME_SIGTERM:
911 log_unit_warning(UNIT(n), "Stopping timed out. Killing.");
912 busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
913 break;
914
915 case BUSNAME_SIGKILL:
916 log_unit_warning(UNIT(n), "Processes still around after SIGKILL. Ignoring.");
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
927 static 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
938 static 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
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))
960 busname_enter_listening(n);
961 }
962
963 static 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
967 static 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
981 static bool busname_supported(void) {
982 static int supported = -1;
983
984 if (supported < 0)
985 supported = is_kdbus_available();
986
987 return supported;
988 }
989
990 static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
991 [BUSNAME_DEAD] = "dead",
992 [BUSNAME_MAKING] = "making",
993 [BUSNAME_REGISTERED] = "registered",
994 [BUSNAME_LISTENING] = "listening",
995 [BUSNAME_RUNNING] = "running",
996 [BUSNAME_SIGTERM] = "sigterm",
997 [BUSNAME_SIGKILL] = "sigkill",
998 [BUSNAME_FAILED] = "failed",
999 };
1000
1001 DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
1002
1003 static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
1004 [BUSNAME_SUCCESS] = "success",
1005 [BUSNAME_FAILURE_RESOURCES] = "resources",
1006 [BUSNAME_FAILURE_TIMEOUT] = "timeout",
1007 [BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
1008 [BUSNAME_FAILURE_SIGNAL] = "signal",
1009 [BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
1010 [BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
1011 };
1012
1013 DEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
1014
1015 const 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
1024 .no_alias = true,
1025 .no_instances = true,
1026
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
1038 .kill = busname_kill,
1039
1040 .get_timeout = busname_get_timeout,
1041
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
1048 .sigchld_event = busname_sigchld_event,
1049
1050 .trigger_notify = busname_trigger_notify,
1051
1052 .reset_failed = busname_reset_failed,
1053
1054 .supported = busname_supported,
1055
1056 .bus_interface = "org.freedesktop.systemd1.BusName",
1057 .bus_vtable = bus_busname_vtable,
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 };