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