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