]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/bus-util.c
sd-bus: adjust indentation of comments
[thirdparty/systemd.git] / src / shared / bus-util.c
index ddc836451187508b4fb08373fedb83eef87632c9..e9b0b8a99d9dca6028e14afc74a722968a4641db 100644 (file)
 #include "escape.h"
 #include "fd-util.h"
 #include "missing.h"
-#include "mount-util.h"
+#include "mountpoint-util.h"
 #include "nsflags.h"
 #include "parse-util.h"
+#include "path-util.h"
 #include "proc-cmdline.h"
 #include "rlimit-util.h"
 #include "stdio-util.h"
@@ -410,14 +411,14 @@ int bus_verify_polkit_async(
                 if (sd_bus_message_is_method_error(q->reply, NULL)) {
                         const sd_bus_error *e;
 
-                        /* Copy error from polkit reply */
                         e = sd_bus_message_get_error(q->reply);
-                        sd_bus_error_copy(error, e);
 
                         /* Treat no PK available as access denied */
                         if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
                                 return -EACCES;
 
+                        /* Copy error from polkit reply */
+                        sd_bus_error_copy(error, e);
                         return -sd_bus_error_get_errno(e);
                 }
 
@@ -559,7 +560,7 @@ int bus_check_peercred(sd_bus *c) {
 }
 
 int bus_connect_system_systemd(sd_bus **_bus) {
-        _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
+        _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
         int r;
 
         assert(_bus);
@@ -592,7 +593,7 @@ int bus_connect_system_systemd(sd_bus **_bus) {
 }
 
 int bus_connect_user_systemd(sd_bus **_bus) {
-        _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
+        _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
         _cleanup_free_ char *ee = NULL;
         const char *e;
         int r;
@@ -628,7 +629,21 @@ int bus_connect_user_systemd(sd_bus **_bus) {
         return 0;
 }
 
-int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) {
+int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value) {
+        assert(name);
+
+        if (expected_value && !streq_ptr(expected_value, value))
+                return 0;
+
+        if (only_value)
+                puts(value);
+        else
+                printf("%s=%s\n", name, value);
+
+        return 0;
+}
+
+int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) {
         va_list ap;
         int r;
 
@@ -691,7 +706,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         /* This property has a single value, so we need to take
                          * care not to print a new line, everything else is OK. */
                         good = !strchr(s, '\n');
-                        bus_print_property_value(name, expected_value, value, "%s", good ? s : "[unprintable]");
+                        bus_print_property_value(name, expected_value, value, good ? s : "[unprintable]");
                 }
 
                 return 1;
@@ -707,7 +722,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (expected_value && parse_boolean(expected_value) != b)
                         return 1;
 
-                bus_print_property_value(name, NULL, value, "%s", yes_no(b));
+                bus_print_property_value(name, NULL, value, yes_no(b));
                 return 1;
         }
 
@@ -728,13 +743,13 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
 
                         t = format_timestamp(timestamp, sizeof(timestamp), u);
                         if (t || all)
-                                bus_print_property_value(name, expected_value, value, "%s", strempty(t));
+                                bus_print_property_value(name, expected_value, value, strempty(t));
 
                 } else if (strstr(name, "USec")) {
                         char timespan[FORMAT_TIMESPAN_MAX];
 
                         (void) format_timespan(timespan, sizeof(timespan), u, 0);
-                        bus_print_property_value(name, expected_value, value, "%s", timespan);
+                        bus_print_property_value(name, expected_value, value, timespan);
 
                 } else if (streq(name, "RestrictNamespaces")) {
                         _cleanup_free_ char *s = NULL;
@@ -742,7 +757,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
 
                         if ((u & NAMESPACE_FLAGS_ALL) == 0)
                                 result = "yes";
-                        else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
+                        else if (FLAGS_SET(u, NAMESPACE_FLAGS_ALL))
                                 result = "no";
                         else {
                                 r = namespace_flags_to_string(u, &s);
@@ -752,7 +767,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                                 result = s;
                         }
 
-                        bus_print_property_value(name, expected_value, value, "%s", result);
+                        bus_print_property_value(name, expected_value, value, result);
 
                 } else if (streq(name, "MountFlags")) {
                         const char *result;
@@ -761,7 +776,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         if (!result)
                                 return -EINVAL;
 
-                        bus_print_property_value(name, expected_value, value, "%s", result);
+                        bus_print_property_value(name, expected_value, value, result);
 
                 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
                         _cleanup_free_ char *s = NULL;
@@ -770,7 +785,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         if (r < 0)
                                 return r;
 
-                        bus_print_property_value(name, expected_value, value, "%s", s);
+                        bus_print_property_value(name, expected_value, value, s);
 
                 } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) ||
                            (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
@@ -778,16 +793,18 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                            (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
                            (endswith(name, "NSec") && u == (uint64_t) -1))
 
-                        bus_print_property_value(name, expected_value, value, "%s", "[not set]");
+                        bus_print_property_value(name, expected_value, value, "[not set]");
 
-                else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
+                else if ((STR_IN_SET(name, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
                          (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) ||
                          (startswith(name, "Limit") && u == (uint64_t) -1) ||
                          (startswith(name, "DefaultLimit") && u == (uint64_t) -1))
 
-                        bus_print_property_value(name, expected_value, value, "%s", "infinity");
+                        bus_print_property_value(name, expected_value, value, "infinity");
+                else if (STR_IN_SET(name, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u == (uint64_t) -1)
+                        bus_print_property_value(name, expected_value, value, "[no data]");
                 else
-                        bus_print_property_value(name, expected_value, value, "%"PRIu64, u);
+                        bus_print_property_valuef(name, expected_value, value, "%"PRIu64, u);
 
                 return 1;
         }
@@ -799,7 +816,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (r < 0)
                         return r;
 
-                bus_print_property_value(name, expected_value, value, "%"PRIi64, i);
+                bus_print_property_valuef(name, expected_value, value, "%"PRIi64, i);
                 return 1;
         }
 
@@ -811,20 +828,20 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         return r;
 
                 if (strstr(name, "UMask") || strstr(name, "Mode"))
-                        bus_print_property_value(name, expected_value, value, "%04o", u);
+                        bus_print_property_valuef(name, expected_value, value, "%04o", u);
 
                 else if (streq(name, "UID")) {
                         if (u == UID_INVALID)
-                                bus_print_property_value(name, expected_value, value, "%s", "[not set]");
+                                bus_print_property_value(name, expected_value, value, "[not set]");
                         else
-                                bus_print_property_value(name, expected_value, value, "%"PRIu32, u);
+                                bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
                 } else if (streq(name, "GID")) {
                         if (u == GID_INVALID)
-                                bus_print_property_value(name, expected_value, value, "%s", "[not set]");
+                                bus_print_property_value(name, expected_value, value, "[not set]");
                         else
-                                bus_print_property_value(name, expected_value, value, "%"PRIu32, u);
+                                bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
                 } else
-                        bus_print_property_value(name, expected_value, value, "%"PRIu32, u);
+                        bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
 
                 return 1;
         }
@@ -836,7 +853,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (r < 0)
                         return r;
 
-                bus_print_property_value(name, expected_value, value, "%"PRIi32, i);
+                bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i);
                 return 1;
         }
 
@@ -847,7 +864,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (r < 0)
                         return r;
 
-                bus_print_property_value(name, expected_value, value, "%g", d);
+                bus_print_property_valuef(name, expected_value, value, "%g", d);
                 return 1;
         }
 
@@ -897,7 +914,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                                 return r;
 
                         if (all || n > 0) {
-                                unsigned int i;
+                                unsigned i;
 
                                 if (!value)
                                         printf("%s=", name);
@@ -919,7 +936,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                                 return r;
 
                         if (all || n > 0) {
-                                unsigned int i;
+                                unsigned i;
 
                                 if (!value)
                                         printf("%s=", name);
@@ -973,7 +990,7 @@ int bus_message_print_all_properties(
                                 return log_oom();
                 }
 
-                name_with_equal = strappend(name, "=");
+                name_with_equal = strjoin(name, "=");
                 if (!name_with_equal)
                         return log_oom();
 
@@ -1237,44 +1254,6 @@ int bus_message_map_all_properties(
         return sd_bus_message_exit_container(m);
 }
 
-int bus_message_map_properties_changed(
-                sd_bus_message *m,
-                const struct bus_properties_map *map,
-                unsigned flags,
-                sd_bus_error *error,
-                void *userdata) {
-
-        const char *member;
-        int r, invalidated, i;
-
-        assert(m);
-        assert(map);
-
-        r = bus_message_map_all_properties(m, map, flags, error, userdata);
-        if (r < 0)
-                return r;
-
-        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
-        if (r < 0)
-                return r;
-
-        invalidated = 0;
-        while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
-                for (i = 0; map[i].member; i++)
-                        if (streq(map[i].member, member)) {
-                                ++invalidated;
-                                break;
-                        }
-        if (r < 0)
-                return r;
-
-        r = sd_bus_message_exit_container(m);
-        if (r < 0)
-                return r;
-
-        return invalidated;
-}
-
 int bus_map_all_properties(
                 sd_bus *bus,
                 const char *destination,
@@ -1317,7 +1296,7 @@ int bus_map_all_properties(
 }
 
 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
-        _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
+        _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
         int r;
 
         assert(transport >= 0);
@@ -1382,12 +1361,10 @@ int bus_connect_transport_systemd(BusTransport transport, const char *host, bool
                 if (user)
                         r = bus_connect_user_systemd(bus);
                 else {
-                        if (sd_booted() <= 0) {
+                        if (sd_booted() <= 0)
                                 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
-                                log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
-
-                                return -EHOSTDOWN;
-                        }
+                                return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
+                                                       "System has not been booted with systemd as init system (PID 1). Can't operate.");
                         r = bus_connect_system_systemd(bus);
                 }
                 break;
@@ -1503,14 +1480,6 @@ int bus_property_get_ulong(
 }
 #endif
 
-int bus_log_parse_error(int r) {
-        return log_error_errno(r, "Failed to parse bus message: %m");
-}
-
-int bus_log_create_error(int r) {
-        return log_error_errno(r, "Failed to create bus message: %m");
-}
-
 /**
  * bus_path_encode_unique() - encode unique object path
  * @b: bus connection or NULL
@@ -1576,7 +1545,7 @@ int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id,
         if (!external_label)
                 return -ENOMEM;
 
-        p = strjoin(prefix, "/", sender_label, "/", external_label);
+        p = path_join(prefix, sender_label, external_label);
         if (!p)
                 return -ENOMEM;
 
@@ -1706,13 +1675,14 @@ int bus_track_add_name_many(sd_bus_track *t, char **l) {
 }
 
 int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
-        _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
+        _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
         const char *e;
         int r;
 
         assert(ret);
 
-        /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */
+        /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
+         * turned on. */
 
         r = sd_bus_new(&bus);
         if (r < 0)
@@ -1736,10 +1706,6 @@ int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *descri
         if (r < 0)
                 return r;
 
-        r = sd_bus_set_trusted(bus, true);
-        if (r < 0)
-                return r;
-
         r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
         if (r < 0)
                 return r;
@@ -1761,168 +1727,6 @@ int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *descri
         return 0;
 }
 
-struct request_name_data {
-        unsigned n_ref;
-
-        const char *name;
-        uint64_t flags;
-        void *userdata;
-};
-
-static void request_name_destroy_callback(void *userdata) {
-        struct request_name_data *data = userdata;
-
-        assert(data);
-        assert(data->n_ref > 0);
-
-        log_debug("%s n_ref=%u", __func__, data->n_ref);
-
-        data->n_ref--;
-        if (data->n_ref == 0)
-                free(data);
-}
-
-static int reload_dbus_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
-        struct request_name_data *data = userdata;
-        const sd_bus_error *e;
-        int r;
-
-        assert(data);
-        assert(data->name);
-        assert(data->n_ref > 0);
-
-        e = sd_bus_message_get_error(m);
-        if (e) {
-                log_error_errno(sd_bus_error_get_errno(e), "Failed to reload DBus configuration: %s", e->message);
-                return 1;
-        }
-
-        /* Here, use the default request name handler to avoid an infinite loop of reloading and requesting. */
-        r = sd_bus_request_name_async(sd_bus_message_get_bus(m), NULL, data->name, data->flags, NULL, data->userdata);
-        if (r < 0)
-                log_error_errno(r, "Failed to request name: %m");
-
-        return 1;
-}
-
-static int request_name_handler_may_reload_dbus(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
-        struct request_name_data *data = userdata;
-        uint32_t ret;
-        int r;
-
-        assert(m);
-        assert(data);
-
-        if (sd_bus_message_is_method_error(m, NULL)) {
-                const sd_bus_error *e = sd_bus_message_get_error(m);
-                _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
-
-                if (!sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED)) {
-                        log_debug_errno(sd_bus_error_get_errno(e),
-                                        "Unable to request name, failing connection: %s",
-                                        e->message);
-
-                        bus_enter_closing(sd_bus_message_get_bus(m));
-                        return 1;
-                }
-
-                log_debug_errno(sd_bus_error_get_errno(e),
-                                "Unable to request name, will retry after reloading DBus configuration: %s",
-                                e->message);
-
-                /* If a service enables DynamicUser= and dbus.service started before the dynamic user is realized,
-                 * then the DBus policy about the service has not been enabled yet. So, let's try to reload DBus
-                 * configuration, and after that request the name again. Note that it seems that no privileges are
-                 * necessary to call the following method. */
-
-                r = sd_bus_call_method_async(
-                                sd_bus_message_get_bus(m),
-                                &slot,
-                                "org.freedesktop.DBus",
-                                "/org/freedesktop/DBus",
-                                "org.freedesktop.DBus",
-                                "ReloadConfig",
-                                reload_dbus_handler,
-                                data, NULL);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to reload DBus configuration: %m");
-                        bus_enter_closing(sd_bus_message_get_bus(m));
-                        return 1;
-                }
-
-                data->n_ref ++;
-                assert_se(sd_bus_slot_set_destroy_callback(slot, request_name_destroy_callback) >= 0);
-
-                r = sd_bus_slot_set_floating(slot, true);
-                if (r < 0)
-                        return r;
-
-                return 1;
-        }
-
-        r = sd_bus_message_read(m, "u", &ret);
-        if (r < 0)
-                return r;
-
-        switch (ret) {
-
-        case BUS_NAME_ALREADY_OWNER:
-                log_debug("Already owner of requested service name, ignoring.");
-                return 1;
-
-        case BUS_NAME_IN_QUEUE:
-                log_debug("In queue for requested service name.");
-                return 1;
-
-        case BUS_NAME_PRIMARY_OWNER:
-                log_debug("Successfully acquired requested service name.");
-                return 1;
-
-        case BUS_NAME_EXISTS:
-                log_debug("Requested service name already owned, failing connection.");
-                bus_enter_closing(sd_bus_message_get_bus(m));
-                return 1;
-        }
-
-        log_debug("Unexpected response from RequestName(), failing connection.");
-        bus_enter_closing(sd_bus_message_get_bus(m));
-        return 1;
-}
-
-int bus_request_name_async_may_reload_dbus(sd_bus *bus, sd_bus_slot **ret_slot, const char *name, uint64_t flags, void *userdata) {
-        _cleanup_free_ struct request_name_data *data = NULL;
-        _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
-        int r;
-
-        data = new(struct request_name_data, 1);
-        if (!data)
-                return -ENOMEM;
-
-        *data = (struct request_name_data) {
-                .n_ref = 1,
-                .name = name,
-                .flags = flags,
-                .userdata = userdata,
-        };
-
-        r = sd_bus_request_name_async(bus, &slot, name, flags, request_name_handler_may_reload_dbus, data);
-        if (r < 0)
-                return r;
-
-        assert_se(sd_bus_slot_set_destroy_callback(slot, request_name_destroy_callback) >= 0);
-        TAKE_PTR(data);
-
-        if (ret_slot)
-                *ret_slot = TAKE_PTR(slot);
-        else {
-                r = sd_bus_slot_set_floating(slot, true);
-                if (r < 0)
-                        return r;
-        }
-
-        return 0;
-}
-
 int bus_reply_pair_array(sd_bus_message *m, char **l) {
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         char **k, **v;
@@ -1930,8 +1734,8 @@ int bus_reply_pair_array(sd_bus_message *m, char **l) {
 
         assert(m);
 
-        /* Reply to the specified message with a message containing a dictionary put together from the specified
-         * strv */
+        /* Reply to the specified message with a message containing a dictionary put together from the
+         * specified strv */
 
         r = sd_bus_message_new_method_return(m, &reply);
         if (r < 0)
@@ -1953,3 +1757,13 @@ int bus_reply_pair_array(sd_bus_message *m, char **l) {
 
         return sd_bus_send(NULL, reply, NULL);
 }
+
+static void bus_message_unref_wrapper(void *m) {
+        sd_bus_message_unref(m);
+}
+
+const struct hash_ops bus_message_hash_ops = {
+        .hash = trivial_hash_func,
+        .compare = trivial_compare_func,
+        .free_value = bus_message_unref_wrapper,
+};