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