]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/busname.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / core / busname.c
index eaf5b3c2ba735cbd75a69777ebb0207ce0b3612b..df4a8bef52bb2e521acbf61e23331c9820b8a39c 100644 (file)
 
 #include <sys/mman.h>
 
-#include "special.h"
-#include "bus-kernel.h"
 #include "bus-internal.h"
+#include "bus-kernel.h"
+#include "bus-policy.h"
 #include "bus-util.h"
-#include "service.h"
-#include "dbus-busname.h"
 #include "busname.h"
+#include "dbus-busname.h"
+#include "fd-util.h"
+#include "formats-util.h"
 #include "kdbus.h"
+#include "parse-util.h"
+#include "service.h"
+#include "signal-util.h"
+#include "special.h"
+#include "string-util.h"
 
 static const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
         [BUSNAME_DEAD] = UNIT_INACTIVE,
@@ -92,8 +98,7 @@ static void busname_done(Unit *u) {
 
         assert(n);
 
-        free(n->name);
-        n->name = NULL;
+        n->name = mfree(n->name);
 
         busname_free_policy(n);
         busname_unwatch_control_pid(n);
@@ -122,12 +127,18 @@ static int busname_arm_timer(BusName *n) {
                 return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
         }
 
-        return sd_event_add_time(
+        r = sd_event_add_time(
                         UNIT(n)->manager->event,
                         &n->timer_event_source,
                         CLOCK_MONOTONIC,
                         now(CLOCK_MONOTONIC) + n->timeout_usec, 0,
                         busname_dispatch_timer, n);
+        if (r < 0)
+                return r;
+
+        (void) sd_event_source_set_description(n->timer_event_source, "busname-timer");
+
+        return 0;
 }
 
 static int busname_add_default_default_dependencies(BusName *n) {
@@ -139,7 +150,7 @@ static int busname_add_default_default_dependencies(BusName *n) {
         if (r < 0)
                 return r;
 
-        if (UNIT(n)->manager->running_as == SYSTEMD_SYSTEM) {
+        if (UNIT(n)->manager->running_as == MANAGER_SYSTEM) {
                 r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
                 if (r < 0)
                         return r;
@@ -155,9 +166,9 @@ static int busname_add_extras(BusName *n) {
         assert(n);
 
         if (!n->name) {
-                n->name = unit_name_to_prefix(u->id);
-                if (!n->name)
-                        return -ENOMEM;
+                r = unit_name_to_prefix(u->id, &n->name);
+                if (r < 0)
+                        return r;
         }
 
         if (!u->description) {
@@ -200,13 +211,13 @@ static int busname_verify(BusName *n) {
                 return 0;
 
         if (!service_name_is_valid(n->name)) {
-                log_unit_error(UNIT(n)->id, "%s's Name= setting is not a valid service name Refusing.", UNIT(n)->id);
+                log_unit_error(UNIT(n), "Name= setting is not a valid service name Refusing.");
                 return -EINVAL;
         }
 
-        e = strappenda(n->name, ".busname");
+        e = strjoina(n->name, ".busname");
         if (!unit_has_name(UNIT(n), e)) {
-                log_unit_error(UNIT(n)->id, "%s's Name= setting doesn't match unit name. Refusing.", UNIT(n)->id);
+                log_unit_error(UNIT(n), "Name= setting doesn't match unit name. Refusing.");
                 return -EINVAL;
         }
 
@@ -268,7 +279,7 @@ static void busname_unwatch_fd(BusName *n) {
 
         r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
         if (r < 0)
-                log_unit_debug(UNIT(n)->id, "Failed to disable event source.");
+                log_unit_debug_errno(UNIT(n), r, "Failed to disable event source: %m");
 }
 
 static int busname_watch_fd(BusName *n) {
@@ -279,17 +290,24 @@ static int busname_watch_fd(BusName *n) {
         if (n->starter_fd < 0)
                 return 0;
 
-        if (n->starter_event_source)
+        if (n->starter_event_source) {
                 r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
-        else
+                if (r < 0)
+                        goto fail;
+        } else {
                 r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
-        if (r < 0) {
-                log_unit_warning(UNIT(n)->id, "Failed to watch starter fd: %s", strerror(-r));
-                busname_unwatch_fd(n);
-                return r;
+                if (r < 0)
+                        goto fail;
+
+                (void) sd_event_source_set_description(n->starter_event_source, "busname-starter");
         }
 
         return 0;
+
+fail:
+        log_unit_warning_errno(UNIT(n), r, "Failed to watch starter fd: %m");
+        busname_unwatch_fd(n);
+        return r;
 }
 
 static int busname_open_fd(BusName *n) {
@@ -301,12 +319,10 @@ static int busname_open_fd(BusName *n) {
         if (n->starter_fd >= 0)
                 return 0;
 
-        mode = UNIT(n)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user";
+        mode = UNIT(n)->manager->running_as == MANAGER_SYSTEM ? "system" : "user";
         n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
-        if (n->starter_fd < 0) {
-                log_unit_warning(UNIT(n)->id, "Failed to open %s: %s", path ?: "kdbus", strerror(-n->starter_fd));
-                return n->starter_fd;
-        }
+        if (n->starter_fd < 0)
+                return log_unit_warning_errno(UNIT(n), n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");
 
         return 0;
 }
@@ -330,8 +346,7 @@ static void busname_set_state(BusName *n, BusNameState state) {
                 busname_close_fd(n);
 
         if (state != old_state)
-                log_unit_debug(UNIT(n)->id, "%s changed %s -> %s",
-                               UNIT(n)->id, busname_state_to_string(old_state), busname_state_to_string(state));
+                log_unit_debug(UNIT(n), "Changed %s -> %s", busname_state_to_string(old_state), busname_state_to_string(state));
 
         unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
 }
@@ -395,8 +410,8 @@ static int busname_make_starter(BusName *n, pid_t *_pid) {
         if (pid == 0) {
                 int ret;
 
-                default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
-                ignore_signals(SIGPIPE, -1);
+                (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
+                (void) ignore_signals(SIGPIPE, -1);
                 log_forget_fds();
 
                 r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
@@ -409,7 +424,7 @@ static int busname_make_starter(BusName *n, pid_t *_pid) {
 
         fail_child:
                 log_open();
-                log_error_errno(-r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
+                log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
 
                 _exit(ret);
         }
@@ -453,14 +468,14 @@ static void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f
                               n->control_pid,
                               false);
         if (r < 0) {
-                log_unit_warning(UNIT(n)->id, "%s failed to kill control process: %s", UNIT(n)->id, strerror(-r));
+                log_unit_warning_errno(UNIT(n), r, "Failed to kill control process: %m");
                 goto fail;
         }
 
         if (r > 0) {
                 r = busname_arm_timer(n);
                 if (r < 0) {
-                        log_unit_warning(UNIT(n)->id, "%s failed to arm timer: %s", UNIT(n)->id, strerror(-r));
+                        log_unit_warning_errno(UNIT(n), r, "Failed to arm timer: %m");
                         goto fail;
                 }
 
@@ -484,7 +499,7 @@ static void busname_enter_listening(BusName *n) {
         if (n->activating) {
                 r = busname_watch_fd(n);
                 if (r < 0) {
-                        log_unit_warning(UNIT(n)->id, "%s failed to watch names: %s", UNIT(n)->id, strerror(-r));
+                        log_unit_warning_errno(UNIT(n), r, "Failed to watch names: %m");
                         goto fail;
                 }
 
@@ -515,7 +530,7 @@ static void busname_enter_making(BusName *n) {
 
                 r = busname_make_starter(n, &n->control_pid);
                 if (r < 0) {
-                        log_unit_warning(UNIT(n)->id, "%s failed to fork 'making' task: %s", UNIT(n)->id, strerror(-r));
+                        log_unit_warning_errno(UNIT(n), r, "Failed to fork 'making' task: %m");
                         goto fail;
                 }
 
@@ -526,7 +541,7 @@ static void busname_enter_making(BusName *n) {
 
                 r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
                 if (r < 0) {
-                        log_unit_warning(UNIT(n)->id, "%s failed to make starter: %s", UNIT(n)->id, strerror(-r));
+                        log_unit_warning_errno(UNIT(n), r, "Failed to make starter: %m");
                         goto fail;
                 }
 
@@ -551,11 +566,11 @@ static void busname_enter_running(BusName *n) {
         if (!n->activating)
                 return;
 
-        /* We don't take conenctions anymore if we are supposed to
+        /* We don't take connections anymore if we are supposed to
          * shut down anyway */
 
         if (unit_stop_pending(UNIT(n))) {
-                log_unit_debug(UNIT(n)->id, "Suppressing activation request on %s since unit stop is scheduled.", UNIT(n)->id);
+                log_unit_debug(UNIT(n), "Suppressing activation request since unit stop is scheduled.");
 
                 /* Flush all queued activation reqeuest by closing and reopening the connection */
                 bus_kernel_drop_one(n->starter_fd);
@@ -573,6 +588,12 @@ static void busname_enter_running(BusName *n) {
                 }
 
         if (!pending) {
+                if (!UNIT_ISSET(n->service)) {
+                        log_unit_error(UNIT(n), "Service to activate vanished, refusing activation.");
+                        r = -ENOENT;
+                        goto fail;
+                }
+
                 r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, true, &error, NULL);
                 if (r < 0)
                         goto fail;
@@ -582,7 +603,7 @@ static void busname_enter_running(BusName *n) {
         return;
 
 fail:
-        log_unit_warning(UNIT(n)->id, "%s failed to queue service startup job: %s", UNIT(n)->id, bus_error_message(&error, r));
+        log_unit_warning(UNIT(n), "Failed to queue service startup job: %s", bus_error_message(&error, r));
         busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
 }
 
@@ -606,7 +627,7 @@ static int busname_start(Unit *u) {
                 service = SERVICE(UNIT_DEREF(n->service));
 
                 if (UNIT(service)->load_state != UNIT_LOADED) {
-                        log_unit_error(u->id, "Bus service %s not loaded, refusing.", UNIT(service)->id);
+                        log_unit_error(u, "Bus service %s not loaded, refusing.", UNIT(service)->id);
                         return -ENOENT;
                 }
         }
@@ -616,7 +637,7 @@ static int busname_start(Unit *u) {
         n->result = BUSNAME_SUCCESS;
         busname_enter_making(n);
 
-        return 0;
+        return 1;
 }
 
 static int busname_stop(Unit *u) {
@@ -639,11 +660,12 @@ static int busname_stop(Unit *u) {
         assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
 
         busname_enter_dead(n, BUSNAME_SUCCESS);
-        return 0;
+        return 1;
 }
 
 static int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
         BusName *n = BUSNAME(u);
+        int r;
 
         assert(n);
         assert(f);
@@ -655,15 +677,9 @@ static int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
         if (n->control_pid > 0)
                 unit_serialize_item_format(u, f, "control-pid", PID_FMT, n->control_pid);
 
-        if (n->starter_fd >= 0) {
-                int copy;
-
-                copy = fdset_put_dup(fds, n->starter_fd);
-                if (copy < 0)
-                        return copy;
-
-                unit_serialize_item_format(u, f, "starter-fd", "%i", copy);
-        }
+        r = unit_serialize_item_fd(u, f, fds, "starter-fd", n->starter_fd);
+        if (r < 0)
+                return r;
 
         return 0;
 }
@@ -680,7 +696,7 @@ static int busname_deserialize_item(Unit *u, const char *key, const char *value,
 
                 state = busname_state_from_string(value);
                 if (state < 0)
-                        log_unit_debug(u->id, "Failed to parse state value %s", value);
+                        log_unit_debug(u, "Failed to parse state value: %s", value);
                 else
                         n->deserialized_state = state;
 
@@ -689,7 +705,7 @@ static int busname_deserialize_item(Unit *u, const char *key, const char *value,
 
                 f = busname_result_from_string(value);
                 if (f < 0)
-                        log_unit_debug(u->id, "Failed to parse result value %s", value);
+                        log_unit_debug(u, "Failed to parse result value: %s", value);
                 else if (f != BUSNAME_SUCCESS)
                         n->result = f;
 
@@ -697,20 +713,20 @@ static int busname_deserialize_item(Unit *u, const char *key, const char *value,
                 pid_t pid;
 
                 if (parse_pid(value, &pid) < 0)
-                        log_unit_debug(u->id, "Failed to parse control-pid value %s", value);
+                        log_unit_debug(u, "Failed to parse control-pid value: %s", value);
                 else
                         n->control_pid = pid;
         } else if (streq(key, "starter-fd")) {
                 int fd;
 
                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
-                        log_unit_debug(u->id, "Failed to parse starter fd value %s", value);
+                        log_unit_debug(u, "Failed to parse starter fd value: %s", value);
                 else {
                         safe_close(n->starter_fd);
                         n->starter_fd = fdset_remove(fds, fd);
                 }
         } else
-                log_unit_debug(u->id, "Unknown serialization key '%s'", key);
+                log_unit_debug(u, "Unknown serialization key: %s", key);
 
         return 0;
 }
@@ -729,9 +745,12 @@ _pure_ static const char *busname_sub_state_to_string(Unit *u) {
 
 static int busname_peek_message(BusName *n) {
         struct kdbus_cmd_recv cmd_recv = {
+                .size = sizeof(cmd_recv),
                 .flags = KDBUS_RECV_PEEK,
         };
-        struct kdbus_cmd_free cmd_free = {};
+        struct kdbus_cmd_free cmd_free = {
+                .size = sizeof(cmd_free),
+        };
         const char *comm = NULL;
         struct kdbus_item *d;
         struct kdbus_msg *k;
@@ -752,13 +771,12 @@ static int busname_peek_message(BusName *n) {
         if (log_get_max_level() < LOG_DEBUG)
                 return 0;
 
-        r = ioctl(n->starter_fd, KDBUS_CMD_MSG_RECV, &cmd_recv);
+        r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv);
         if (r < 0) {
                 if (errno == EINTR || errno == EAGAIN)
                         return 0;
 
-                log_unit_error(UNIT(n)->id, "%s: Failed to query activation message: %m", UNIT(n)->id);
-                return -errno;
+                return log_unit_error_errno(UNIT(n), errno, "Failed to query activation message: %m");
         }
 
         /* We map as late as possible, and unmap imemdiately after
@@ -768,14 +786,13 @@ static int busname_peek_message(BusName *n) {
          * longer than necessary. */
 
         ps = page_size();
-        start = (cmd_recv.offset / ps) * ps;
-        delta = cmd_recv.offset - start;
-        sz = PAGE_ALIGN(delta + cmd_recv.msg_size);
+        start = (cmd_recv.msg.offset / ps) * ps;
+        delta = cmd_recv.msg.offset - start;
+        sz = PAGE_ALIGN(delta + cmd_recv.msg.msg_size);
 
         p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
         if (p == MAP_FAILED) {
-                log_unit_error(UNIT(n)->id, "%s: Failed to map activation message: %m", UNIT(n)->id);
-                r = -errno;
+                r = log_unit_error_errno(UNIT(n), errno, "Failed to map activation message: %m");
                 goto finish;
         }
 
@@ -794,7 +811,7 @@ static int busname_peek_message(BusName *n) {
         }
 
         if (pid > 0)
-                log_unit_debug(UNIT(n)->id, "%s: Activation triggered by process " PID_FMT " (%s)", UNIT(n)->id, pid, strna(comm));
+                log_unit_debug(UNIT(n), "Activation triggered by process " PID_FMT " (%s)", pid, strna(comm));
 
         r = 0;
 
@@ -802,9 +819,9 @@ finish:
         if (p)
                 (void) munmap(p, sz);
 
-        cmd_free.offset = cmd_recv.offset;
+        cmd_free.offset = cmd_recv.msg.offset;
         if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
-                log_unit_warning(UNIT(n)->id, "Failed to free peeked message, ignoring: %m");
+                log_unit_warning(UNIT(n), "Failed to free peeked message, ignoring: %m");
 
         return r;
 }
@@ -818,11 +835,10 @@ static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents
         if (n->state != BUSNAME_LISTENING)
                 return 0;
 
-        log_unit_debug(UNIT(n)->id, "Activation request on %s", UNIT(n)->id);
+        log_unit_debug(UNIT(n), "Activation request");
 
         if (revents != EPOLLIN) {
-                log_unit_error(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.",
-                               UNIT(n)->id, revents);
+                log_unit_error(UNIT(n), "Got unexpected poll event (0x%x) on starter fd.", revents);
                 goto fail;
         }
 
@@ -858,10 +874,8 @@ static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         else
                 assert_not_reached("Unknown sigchld code");
 
-        log_unit_full(u->id,
-                      f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
-                      "%s control process exited, code=%s status=%i",
-                      u->id, sigchld_code_to_string(code), status);
+        log_unit_full(u, f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
+                      "Control process exited, code=%s status=%i", sigchld_code_to_string(code), status);
 
         if (f != BUSNAME_SUCCESS)
                 n->result = f;
@@ -897,17 +911,17 @@ static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *us
         switch (n->state) {
 
         case BUSNAME_MAKING:
-                log_unit_warning(UNIT(n)->id, "%s making timed out. Terminating.", UNIT(n)->id);
+                log_unit_warning(UNIT(n), "Making timed out. Terminating.");
                 busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
                 break;
 
         case BUSNAME_SIGTERM:
-                log_unit_warning(UNIT(n)->id, "%s stopping timed out. Killing.", UNIT(n)->id);
+                log_unit_warning(UNIT(n), "Stopping timed out. Killing.");
                 busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
                 break;
 
         case BUSNAME_SIGKILL:
-                log_unit_warning(UNIT(n)->id, "%s still around after SIGKILL. Ignoring.", UNIT(n)->id);
+                log_unit_warning(UNIT(n), "Processes still around after SIGKILL. Ignoring.");
                 busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT);
                 break;
 
@@ -972,18 +986,14 @@ static int busname_get_timeout(Unit *u, uint64_t *timeout) {
         return 1;
 }
 
-static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
-        [BUSNAME_DEAD] = "dead",
-        [BUSNAME_MAKING] = "making",
-        [BUSNAME_REGISTERED] = "registered",
-        [BUSNAME_LISTENING] = "listening",
-        [BUSNAME_RUNNING] = "running",
-        [BUSNAME_SIGTERM] = "sigterm",
-        [BUSNAME_SIGKILL] = "sigkill",
-        [BUSNAME_FAILED] = "failed",
-};
+static bool busname_supported(void) {
+        static int supported = -1;
+
+        if (supported < 0)
+                supported = is_kdbus_available();
 
-DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
+        return supported;
+}
 
 static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
         [BUSNAME_SUCCESS] = "success",
@@ -1006,6 +1016,9 @@ const UnitVTable busname_vtable = {
                 "Install\0",
         .private_section = "BusName",
 
+        .no_alias = true,
+        .no_instances = true,
+
         .init = busname_init,
         .done = busname_done,
         .load = busname_load,
@@ -1033,20 +1046,18 @@ const UnitVTable busname_vtable = {
 
         .reset_failed = busname_reset_failed,
 
-        .bus_interface = "org.freedesktop.systemd1.BusName",
+        .supported = busname_supported,
+
         .bus_vtable = bus_busname_vtable,
 
         .status_message_formats = {
                 .finished_start_job = {
                         [JOB_DONE]       = "Listening on %s.",
                         [JOB_FAILED]     = "Failed to listen on %s.",
-                        [JOB_DEPENDENCY] = "Dependency failed for %s.",
-                        [JOB_TIMEOUT]    = "Timed out starting %s.",
                 },
                 .finished_stop_job = {
                         [JOB_DONE]       = "Closed %s.",
                         [JOB_FAILED]     = "Failed stopping %s.",
-                        [JOB_TIMEOUT]    = "Timed out stopping %s.",
                 },
         },
 };