LCOV - code coverage report
Current view: top level - core - busname.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 6 511 1.2 %
Date: 2015-07-29 18:47:03 Functions: 5 41 12.2 %

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

Generated by: LCOV version 1.11