chiark / gitweb /
busname: fix CMD_FREE ioctl
[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 "dbus-busname.h"
30 #include "busname.h"
31 #include "kdbus.h"
32
33 static const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
34         [BUSNAME_DEAD] = UNIT_INACTIVE,
35         [BUSNAME_MAKING] = UNIT_ACTIVATING,
36         [BUSNAME_REGISTERED] = UNIT_ACTIVE,
37         [BUSNAME_LISTENING] = UNIT_ACTIVE,
38         [BUSNAME_RUNNING] = UNIT_ACTIVE,
39         [BUSNAME_SIGTERM] = UNIT_DEACTIVATING,
40         [BUSNAME_SIGKILL] = UNIT_DEACTIVATING,
41         [BUSNAME_FAILED] = UNIT_FAILED
42 };
43
44 static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
45 static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
46
47 static void busname_init(Unit *u) {
48         BusName *n = BUSNAME(u);
49
50         assert(u);
51         assert(u->load_state == UNIT_STUB);
52
53         n->starter_fd = -1;
54         n->accept_fd = true;
55         n->activating = true;
56
57         n->timeout_usec = u->manager->default_timeout_start_usec;
58 }
59
60 static void busname_unwatch_control_pid(BusName *n) {
61         assert(n);
62
63         if (n->control_pid <= 0)
64                 return;
65
66         unit_unwatch_pid(UNIT(n), n->control_pid);
67         n->control_pid = 0;
68 }
69
70 static void busname_free_policy(BusName *n) {
71         BusNamePolicy *p;
72
73         assert(n);
74
75         while ((p = n->policy)) {
76                 LIST_REMOVE(policy, n->policy, p);
77
78                 free(p->name);
79                 free(p);
80         }
81 }
82
83 static void busname_close_fd(BusName *n) {
84         assert(n);
85
86         n->starter_event_source = sd_event_source_unref(n->starter_event_source);
87         n->starter_fd = safe_close(n->starter_fd);
88 }
89
90 static void busname_done(Unit *u) {
91         BusName *n = BUSNAME(u);
92
93         assert(n);
94
95         free(n->name);
96         n->name = NULL;
97
98         busname_free_policy(n);
99         busname_unwatch_control_pid(n);
100         busname_close_fd(n);
101
102         unit_ref_unset(&n->service);
103
104         n->timer_event_source = sd_event_source_unref(n->timer_event_source);
105 }
106
107 static int busname_arm_timer(BusName *n) {
108         int r;
109
110         assert(n);
111
112         if (n->timeout_usec <= 0) {
113                 n->timer_event_source = sd_event_source_unref(n->timer_event_source);
114                 return 0;
115         }
116
117         if (n->timer_event_source) {
118                 r = sd_event_source_set_time(n->timer_event_source, now(CLOCK_MONOTONIC) + n->timeout_usec);
119                 if (r < 0)
120                         return r;
121
122                 return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
123         }
124
125         return sd_event_add_time(
126                         UNIT(n)->manager->event,
127                         &n->timer_event_source,
128                         CLOCK_MONOTONIC,
129                         now(CLOCK_MONOTONIC) + n->timeout_usec, 0,
130                         busname_dispatch_timer, n);
131 }
132
133 static int busname_add_default_default_dependencies(BusName *n) {
134         int r;
135
136         assert(n);
137
138         r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, NULL, true);
139         if (r < 0)
140                 return r;
141
142         if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
143                 r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
144                 if (r < 0)
145                         return r;
146         }
147
148         return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
149 }
150
151 static int busname_add_extras(BusName *n) {
152         Unit *u = UNIT(n);
153         int r;
154
155         assert(n);
156
157         if (!n->name) {
158                 n->name = unit_name_to_prefix(u->id);
159                 if (!n->name)
160                         return -ENOMEM;
161         }
162
163         if (!u->description) {
164                 r = unit_set_description(u, n->name);
165                 if (r < 0)
166                         return r;
167         }
168
169         if (n->activating) {
170                 if (!UNIT_DEREF(n->service)) {
171                         Unit *x;
172
173                         r = unit_load_related_unit(u, ".service", &x);
174                         if (r < 0)
175                                 return r;
176
177                         unit_ref_set(&n->service, x);
178                 }
179
180                 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
181                 if (r < 0)
182                         return r;
183         }
184
185         if (u->default_dependencies) {
186                 r = busname_add_default_default_dependencies(n);
187                 if (r < 0)
188                         return r;
189         }
190
191         return 0;
192 }
193
194 static int busname_verify(BusName *n) {
195         char *e;
196
197         assert(n);
198
199         if (UNIT(n)->load_state != UNIT_LOADED)
200                 return 0;
201
202         if (!service_name_is_valid(n->name)) {
203                 log_unit_error(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
204                 return -EINVAL;
205         }
206
207         e = strappenda(n->name, ".busname");
208         if (!unit_has_name(UNIT(n), e)) {
209                 log_unit_error(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
210                 return -EINVAL;
211         }
212
213         return 0;
214 }
215
216 static int busname_load(Unit *u) {
217         BusName *n = BUSNAME(u);
218         int r;
219
220         assert(u);
221         assert(u->load_state == UNIT_STUB);
222
223         r = unit_load_fragment_and_dropin(u);
224         if (r < 0)
225                 return r;
226
227         if (u->load_state == UNIT_LOADED) {
228                 /* This is a new unit? Then let's add in some extras */
229                 r = busname_add_extras(n);
230                 if (r < 0)
231                         return r;
232         }
233
234         return busname_verify(n);
235 }
236
237 static void busname_dump(Unit *u, FILE *f, const char *prefix) {
238         BusName *n = BUSNAME(u);
239
240         assert(n);
241         assert(f);
242
243         fprintf(f,
244                 "%sBus Name State: %s\n"
245                 "%sResult: %s\n"
246                 "%sName: %s\n"
247                 "%sActivating: %s\n"
248                 "%sAccept FD: %s\n",
249                 prefix, busname_state_to_string(n->state),
250                 prefix, busname_result_to_string(n->result),
251                 prefix, n->name,
252                 prefix, yes_no(n->activating),
253                 prefix, yes_no(n->accept_fd));
254
255         if (n->control_pid > 0)
256                 fprintf(f,
257                         "%sControl PID: "PID_FMT"\n",
258                         prefix, n->control_pid);
259 }
260
261 static void busname_unwatch_fd(BusName *n) {
262         int r;
263
264         assert(n);
265
266         if (!n->starter_event_source)
267                 return;
268
269         r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
270         if (r < 0)
271                 log_unit_debug(UNIT(n)->id, "Failed to disable event source.");
272 }
273
274 static int busname_watch_fd(BusName *n) {
275         int r;
276
277         assert(n);
278
279         if (n->starter_fd < 0)
280                 return 0;
281
282         if (n->starter_event_source)
283                 r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
284         else
285                 r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
286         if (r < 0) {
287                 log_unit_warning_errno(UNIT(n)->id, r, "Failed to watch starter fd: %m");
288                 busname_unwatch_fd(n);
289                 return r;
290         }
291
292         return 0;
293 }
294
295 static int busname_open_fd(BusName *n) {
296         _cleanup_free_ char *path = NULL;
297         const char *mode;
298
299         assert(n);
300
301         if (n->starter_fd >= 0)
302                 return 0;
303
304         mode = UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user";
305         n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
306         if (n->starter_fd < 0)
307                 return log_unit_warning_errno(UNIT(n)->id, n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");
308
309         return 0;
310 }
311
312 static 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
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
324         if (state != BUSNAME_LISTENING)
325                 busname_unwatch_fd(n);
326
327         if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING))
328                 busname_close_fd(n);
329
330         if (state != old_state)
331                 log_unit_debug(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
337 static 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
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)) {
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
377 static 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_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
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
422 fail:
423         n->timer_event_source = sd_event_source_unref(n->timer_event_source);
424         return r;
425 }
426
427 static 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
436 static void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
437         KillContext kill_context = {};
438         int r;
439
440         assert(n);
441
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 ? KILL_KILL : KILL_TERMINATE,
450                               -1,
451                               n->control_pid,
452                               false);
453         if (r < 0) {
454                 log_unit_warning_errno(UNIT(n)->id, r, "%s failed to kill control process: %m", UNIT(n)->id);
455                 goto fail;
456         }
457
458         if (r > 0) {
459                 r = busname_arm_timer(n);
460                 if (r < 0) {
461                         log_unit_warning_errno(UNIT(n)->id, r, "%s failed to arm timer: %m", UNIT(n)->id);
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
473 fail:
474         busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
475 }
476
477 static void busname_enter_listening(BusName *n) {
478         int r;
479
480         assert(n);
481
482         if (n->activating) {
483                 r = busname_watch_fd(n);
484                 if (r < 0) {
485                         log_unit_warning_errno(UNIT(n)->id, r, "%s failed to watch names: %m", UNIT(n)->id);
486                         goto fail;
487                 }
488
489                 busname_set_state(n, BUSNAME_LISTENING);
490         } else
491                 busname_set_state(n, BUSNAME_REGISTERED);
492
493         return;
494
495 fail:
496         busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
497 }
498
499 static 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) {
509                 /* If there is a policy, we need to resolve user/group
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_unit_warning_errno(UNIT(n)->id, r, "%s failed to fork 'making' task: %m", UNIT(n)->id);
517                         goto fail;
518                 }
519
520                 busname_set_state(n, BUSNAME_MAKING);
521         } else {
522                 /* If there is no policy, we can do everything
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_unit_warning_errno(UNIT(n)->id, r, "%s failed to make starter: %m", UNIT(n)->id);
528                         goto fail;
529                 }
530
531                 busname_enter_listening(n);
532         }
533
534         return;
535
536 fail:
537         busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
538 }
539
540 static 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
549         if (!n->activating)
550                 return;
551
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_unit_debug(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
557
558                 /* Flush all queued activation reqeuest by closing and reopening the connection */
559                 bus_kernel_drop_one(n->starter_fd);
560
561                 busname_enter_listening(n);
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
582 fail:
583         log_unit_warning(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
587 static int busname_start(Unit *u) {
588         BusName *n = BUSNAME(u);
589
590         assert(n);
591
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
601         if (n->activating && UNIT_ISSET(n->service)) {
602                 Service *service;
603
604                 service = SERVICE(UNIT_DEREF(n->service));
605
606                 if (UNIT(service)->load_state != UNIT_LOADED) {
607                         log_unit_error(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;
615         busname_enter_making(n);
616
617         return 0;
618 }
619
620 static int busname_stop(Unit *u) {
621         BusName *n = BUSNAME(u);
622
623         assert(n);
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
637         assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
638
639         busname_enter_dead(n, BUSNAME_SUCCESS);
640         return 0;
641 }
642
643 static 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
653         if (n->control_pid > 0)
654                 unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid);
655
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
669 static 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_unit_debug(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_unit_debug(u->id, "Failed to parse result value %s", value);
691                 else if (f != BUSNAME_SUCCESS)
692                         n->result = f;
693
694         } else if (streq(key, "control-pid")) {
695                 pid_t pid;
696
697                 if (parse_pid(value, &pid) < 0)
698                         log_unit_debug(u->id, "Failed to parse control-pid value %s", value);
699                 else
700                         n->control_pid = pid;
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_unit_debug(u->id, "Failed to parse starter fd value %s", value);
706                 else {
707                         safe_close(n->starter_fd);
708                         n->starter_fd = fdset_remove(fds, fd);
709                 }
710         } else
711                 log_unit_debug(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
728 static int busname_peek_message(BusName *n) {
729         struct kdbus_cmd_recv cmd_recv = {
730                 .size = sizeof(cmd_recv),
731                 .flags = KDBUS_RECV_PEEK,
732         };
733         struct kdbus_cmd_free cmd_free = {
734                 .size = sizeof(cmd_free),
735         };
736         const char *comm = NULL;
737         struct kdbus_item *d;
738         struct kdbus_msg *k;
739         size_t start, ps, sz, delta;
740         void *p = NULL;
741         pid_t pid = 0;
742         int r;
743
744         /* Generate a friendly debug log message about which process
745          * caused triggering of this bus name. This simply peeks the
746          * metadata of the first queued message and logs it. */
747
748         assert(n);
749
750         /* Let's shortcut things a bit, if debug logging is turned off
751          * anyway. */
752
753         if (log_get_max_level() < LOG_DEBUG)
754                 return 0;
755
756         r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv);
757         if (r < 0) {
758                 if (errno == EINTR || errno == EAGAIN)
759                         return 0;
760
761                 log_unit_error(UNIT(n)->id, "%s: Failed to query activation message: %m", UNIT(n)->id);
762                 return -errno;
763         }
764
765         /* We map as late as possible, and unmap imemdiately after
766          * use. On 32bit address space is scarce and we want to be
767          * able to handle a lot of activator connections at the same
768          * time, and hence shouldn't keep the mmap()s around for
769          * longer than necessary. */
770
771         ps = page_size();
772         start = (cmd_recv.reply.offset / ps) * ps;
773         delta = cmd_recv.reply.offset - start;
774         sz = PAGE_ALIGN(delta + cmd_recv.reply.msg_size);
775
776         p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
777         if (p == MAP_FAILED) {
778                 log_unit_error(UNIT(n)->id, "%s: Failed to map activation message: %m", UNIT(n)->id);
779                 r = -errno;
780                 goto finish;
781         }
782
783         k = (struct kdbus_msg *) ((uint8_t *) p + delta);
784         KDBUS_ITEM_FOREACH(d, k, items) {
785                 switch (d->type) {
786
787                 case KDBUS_ITEM_PIDS:
788                         pid = d->pids.pid;
789                         break;
790
791                 case KDBUS_ITEM_PID_COMM:
792                         comm = d->str;
793                         break;
794                 }
795         }
796
797         if (pid > 0)
798                 log_unit_debug(UNIT(n)->id, "%s: Activation triggered by process " PID_FMT " (%s)", UNIT(n)->id, pid, strna(comm));
799
800         r = 0;
801
802 finish:
803         if (p)
804                 (void) munmap(p, sz);
805
806         cmd_free.offset = cmd_recv.reply.offset;
807         if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
808                 log_unit_warning(UNIT(n)->id, "Failed to free peeked message, ignoring: %m");
809
810         return r;
811 }
812
813 static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
814         BusName *n = userdata;
815
816         assert(n);
817         assert(fd >= 0);
818
819         if (n->state != BUSNAME_LISTENING)
820                 return 0;
821
822         log_unit_debug(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
823
824         if (revents != EPOLLIN) {
825                 log_unit_error(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
826                                UNIT(n)->id, revents);
827                 goto fail;
828         }
829
830         busname_peek_message(n);
831         busname_enter_running(n);
832         return 0;
833 fail:
834
835         busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
836         return 0;
837 }
838
839 static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
840         BusName *n = BUSNAME(u);
841         BusNameResult f;
842
843         assert(n);
844         assert(pid >= 0);
845
846         if (pid != n->control_pid)
847                 return;
848
849         n->control_pid = 0;
850
851         if (is_clean_exit(code, status, NULL))
852                 f = BUSNAME_SUCCESS;
853         else if (code == CLD_EXITED)
854                 f = BUSNAME_FAILURE_EXIT_CODE;
855         else if (code == CLD_KILLED)
856                 f = BUSNAME_FAILURE_SIGNAL;
857         else if (code == CLD_DUMPED)
858                 f = BUSNAME_FAILURE_CORE_DUMP;
859         else
860                 assert_not_reached("Unknown sigchld code");
861
862         log_unit_full(u->id,
863                       f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
864                       "%s control process exited, code=%s status=%i",
865                       u->id, sigchld_code_to_string(code), status);
866
867         if (f != BUSNAME_SUCCESS)
868                 n->result = f;
869
870         switch (n->state) {
871
872         case BUSNAME_MAKING:
873                 if (f == BUSNAME_SUCCESS)
874                         busname_enter_listening(n);
875                 else
876                         busname_enter_signal(n, BUSNAME_SIGTERM, f);
877                 break;
878
879         case BUSNAME_SIGTERM:
880         case BUSNAME_SIGKILL:
881                 busname_enter_dead(n, f);
882                 break;
883
884         default:
885                 assert_not_reached("Uh, control process died at wrong time.");
886         }
887
888         /* Notify clients about changed exit status */
889         unit_add_to_dbus_queue(u);
890 }
891
892 static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
893         BusName *n = BUSNAME(userdata);
894
895         assert(n);
896         assert(n->timer_event_source == source);
897
898         switch (n->state) {
899
900         case BUSNAME_MAKING:
901                 log_unit_warning(UNIT(n)->id, "%s making timed out. Terminating.", UNIT(n)->id);
902                 busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
903                 break;
904
905         case BUSNAME_SIGTERM:
906                 log_unit_warning(UNIT(n)->id, "%s stopping timed out. Killing.", UNIT(n)->id);
907                 busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
908                 break;
909
910         case BUSNAME_SIGKILL:
911                 log_unit_warning(UNIT(n)->id, "%s still around after SIGKILL. Ignoring.", UNIT(n)->id);
912                 busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT);
913                 break;
914
915         default:
916                 assert_not_reached("Timeout at wrong time.");
917         }
918
919         return 0;
920 }
921
922 static void busname_reset_failed(Unit *u) {
923         BusName *n = BUSNAME(u);
924
925         assert(n);
926
927         if (n->state == BUSNAME_FAILED)
928                 busname_set_state(n, BUSNAME_DEAD);
929
930         n->result = BUSNAME_SUCCESS;
931 }
932
933 static void busname_trigger_notify(Unit *u, Unit *other) {
934         BusName *n = BUSNAME(u);
935         Service *s;
936
937         assert(n);
938         assert(other);
939
940         if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
941                 return;
942
943         if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
944                 return;
945
946         s = SERVICE(other);
947
948         if (s->state == SERVICE_FAILED && s->result == SERVICE_FAILURE_START_LIMIT)
949                 busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT);
950         else if (IN_SET(s->state,
951                         SERVICE_DEAD, SERVICE_FAILED,
952                         SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
953                         SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
954                         SERVICE_AUTO_RESTART))
955                 busname_enter_listening(n);
956 }
957
958 static int busname_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
959         return unit_kill_common(u, who, signo, -1, BUSNAME(u)->control_pid, error);
960 }
961
962 static int busname_get_timeout(Unit *u, uint64_t *timeout) {
963         BusName *n = BUSNAME(u);
964         int r;
965
966         if (!n->timer_event_source)
967                 return 0;
968
969         r = sd_event_source_get_time(n->timer_event_source, timeout);
970         if (r < 0)
971                 return r;
972
973         return 1;
974 }
975
976 static bool busname_supported(Manager *m) {
977         int supported = -1;
978         assert(m);
979
980         if (supported < 0)
981                 supported = access("/sys/fs/kdbus", F_OK) >= 0;
982
983         return supported;
984 }
985
986 static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
987         [BUSNAME_DEAD] = "dead",
988         [BUSNAME_MAKING] = "making",
989         [BUSNAME_REGISTERED] = "registered",
990         [BUSNAME_LISTENING] = "listening",
991         [BUSNAME_RUNNING] = "running",
992         [BUSNAME_SIGTERM] = "sigterm",
993         [BUSNAME_SIGKILL] = "sigkill",
994         [BUSNAME_FAILED] = "failed",
995 };
996
997 DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
998
999 static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
1000         [BUSNAME_SUCCESS] = "success",
1001         [BUSNAME_FAILURE_RESOURCES] = "resources",
1002         [BUSNAME_FAILURE_TIMEOUT] = "timeout",
1003         [BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
1004         [BUSNAME_FAILURE_SIGNAL] = "signal",
1005         [BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
1006         [BUSNAME_FAILURE_SERVICE_FAILED_PERMANENT] = "service-failed-permanent",
1007 };
1008
1009 DEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
1010
1011 const UnitVTable busname_vtable = {
1012         .object_size = sizeof(BusName),
1013
1014         .sections =
1015                 "Unit\0"
1016                 "BusName\0"
1017                 "Install\0",
1018         .private_section = "BusName",
1019
1020         .init = busname_init,
1021         .done = busname_done,
1022         .load = busname_load,
1023
1024         .coldplug = busname_coldplug,
1025
1026         .dump = busname_dump,
1027
1028         .start = busname_start,
1029         .stop = busname_stop,
1030
1031         .kill = busname_kill,
1032
1033         .get_timeout = busname_get_timeout,
1034
1035         .serialize = busname_serialize,
1036         .deserialize_item = busname_deserialize_item,
1037
1038         .active_state = busname_active_state,
1039         .sub_state_to_string = busname_sub_state_to_string,
1040
1041         .sigchld_event = busname_sigchld_event,
1042
1043         .trigger_notify = busname_trigger_notify,
1044
1045         .reset_failed = busname_reset_failed,
1046
1047         .supported = busname_supported,
1048
1049         .bus_interface = "org.freedesktop.systemd1.BusName",
1050         .bus_vtable = bus_busname_vtable,
1051
1052         .status_message_formats = {
1053                 .finished_start_job = {
1054                         [JOB_DONE]       = "Listening on %s.",
1055                         [JOB_FAILED]     = "Failed to listen on %s.",
1056                         [JOB_DEPENDENCY] = "Dependency failed for %s.",
1057                         [JOB_TIMEOUT]    = "Timed out starting %s.",
1058                 },
1059                 .finished_stop_job = {
1060                         [JOB_DONE]       = "Closed %s.",
1061                         [JOB_FAILED]     = "Failed stopping %s.",
1062                         [JOB_TIMEOUT]    = "Timed out stopping %s.",
1063                 },
1064         },
1065 };