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