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