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