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