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