]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/bus-util.c
coccinelle: make use of SYNTHETIC_ERRNO
[thirdparty/systemd.git] / src / shared / bus-util.c
index 10f047026e55332e8d30097c85cc06bd798adad0..760be38c85d052095f6b950dca5a1b9337cf5671 100644 (file)
@@ -1,9 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2013 Lennart Poettering
-***/
 
 #include <errno.h>
 #include <fcntl.h>
@@ -505,7 +500,7 @@ int bus_verify_polkit_async(
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_append(pk, "us", !!interactive, NULL);
+        r = sd_bus_message_append(pk, "us", interactive, NULL);
         if (r < 0)
                 return r;
 
@@ -633,15 +628,43 @@ int bus_connect_user_systemd(sd_bus **_bus) {
         return 0;
 }
 
-#define print_property(name, fmt, ...)                                  \
-        do {                                                            \
-                if (value)                                              \
-                        printf(fmt "\n", __VA_ARGS__);                  \
-                else                                                    \
-                        printf("%s=" fmt "\n", name, __VA_ARGS__);      \
-        } while (0)
+int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) {
+        va_list ap;
+        int r;
+
+        assert(name);
+        assert(fmt);
+
+        if (expected_value) {
+                _cleanup_free_ char *s = NULL;
+
+                va_start(ap, fmt);
+                r = vasprintf(&s, fmt, ap);
+                va_end(ap);
+                if (r < 0)
+                        return -ENOMEM;
+
+                if (streq_ptr(expected_value, s)) {
+                        if (only_value)
+                                puts(s);
+                        else
+                                printf("%s=%s\n", name, s);
+                }
+
+                return 0;
+        }
+
+        if (!only_value)
+                printf("%s=", name);
+        va_start(ap, fmt);
+        vprintf(fmt, ap);
+        va_end(ap);
+        puts("");
 
-int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all) {
+        return 0;
+}
+
+static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) {
         char type;
         const char *contents;
         int r;
@@ -668,7 +691,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                         /* 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');
-                        print_property(name, "%s", good ? s : "[unprintable]");
+                        bus_print_property_value(name, expected_value, value, "%s", good ? s : "[unprintable]");
                 }
 
                 return 1;
@@ -681,8 +704,10 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                 if (r < 0)
                         return r;
 
-                print_property(name, "%s", yes_no(b));
+                if (expected_value && parse_boolean(expected_value) != b)
+                        return 1;
 
+                bus_print_property_value(name, NULL, value, "%s", yes_no(b));
                 return 1;
         }
 
@@ -696,17 +721,21 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                 /* Yes, heuristics! But we can change this check
                  * should it turn out to not be sufficient */
 
-                if (endswith(name, "Timestamp") || STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec")) {
-                        char timestamp[FORMAT_TIMESTAMP_MAX], *t;
+                if (endswith(name, "Timestamp") ||
+                    STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
+                        char timestamp[FORMAT_TIMESTAMP_MAX];
+                        const char *t;
 
                         t = format_timestamp(timestamp, sizeof(timestamp), u);
                         if (t || all)
-                                print_property(name, "%s", strempty(t));
+                                bus_print_property_value(name, expected_value, value, "%s", strempty(t));
 
                 } else if (strstr(name, "USec")) {
                         char timespan[FORMAT_TIMESPAN_MAX];
 
-                        print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
+                        (void) format_timespan(timespan, sizeof(timespan), u, 0);
+                        bus_print_property_value(name, expected_value, value, "%s", timespan);
+
                 } else if (streq(name, "RestrictNamespaces")) {
                         _cleanup_free_ char *s = NULL;
                         const char *result;
@@ -716,14 +745,14 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                         else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
                                 result = "no";
                         else {
-                                r = namespace_flag_to_string_many(u, &s);
+                                r = namespace_flags_to_string(u, &s);
                                 if (r < 0)
                                         return r;
 
                                 result = s;
                         }
 
-                        print_property(name, "%s", result);
+                        bus_print_property_value(name, expected_value, value, "%s", result);
 
                 } else if (streq(name, "MountFlags")) {
                         const char *result;
@@ -732,7 +761,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                         if (!result)
                                 return -EINVAL;
 
-                        print_property(name, "%s", result);
+                        bus_print_property_value(name, expected_value, value, "%s", result);
 
                 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
                         _cleanup_free_ char *s = NULL;
@@ -741,7 +770,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                         if (r < 0)
                                 return r;
 
-                        print_property(name, "%s", s);
+                        bus_print_property_value(name, expected_value, value, "%s", 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) ||
@@ -749,16 +778,16 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                            (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
                            (endswith(name, "NSec") && u == (uint64_t) -1))
 
-                        print_property(name, "%s", "[not set]");
+                        bus_print_property_value(name, expected_value, value, "%s", "[not set]");
 
                 else if ((STR_IN_SET(name, "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))
 
-                        print_property(name, "%s", "infinity");
+                        bus_print_property_value(name, expected_value, value, "%s", "infinity");
                 else
-                        print_property(name, "%"PRIu64, u);
+                        bus_print_property_value(name, expected_value, value, "%"PRIu64, u);
 
                 return 1;
         }
@@ -770,8 +799,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                 if (r < 0)
                         return r;
 
-                print_property(name, "%"PRIi64, i);
-
+                bus_print_property_value(name, expected_value, value, "%"PRIi64, i);
                 return 1;
         }
 
@@ -783,19 +811,20 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                         return r;
 
                 if (strstr(name, "UMask") || strstr(name, "Mode"))
-                        print_property(name, "%04o", u);
+                        bus_print_property_value(name, expected_value, value, "%04o", u);
+
                 else if (streq(name, "UID")) {
                         if (u == UID_INVALID)
-                                print_property(name, "%s", "[not set]");
+                                bus_print_property_value(name, expected_value, value, "%s", "[not set]");
                         else
-                                print_property(name, "%"PRIu32, u);
+                                bus_print_property_value(name, expected_value, value, "%"PRIu32, u);
                 } else if (streq(name, "GID")) {
                         if (u == GID_INVALID)
-                                print_property(name, "%s", "[not set]");
+                                bus_print_property_value(name, expected_value, value, "%s", "[not set]");
                         else
-                                print_property(name, "%"PRIu32, u);
+                                bus_print_property_value(name, expected_value, value, "%"PRIu32, u);
                 } else
-                        print_property(name, "%"PRIu32, u);
+                        bus_print_property_value(name, expected_value, value, "%"PRIu32, u);
 
                 return 1;
         }
@@ -807,7 +836,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                 if (r < 0)
                         return r;
 
-                print_property(name, "%"PRIi32, i);
+                bus_print_property_value(name, expected_value, value, "%"PRIi32, i);
                 return 1;
         }
 
@@ -818,7 +847,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                 if (r < 0)
                         return r;
 
-                print_property(name, "%g", d);
+                bus_print_property_value(name, expected_value, value, "%g", d);
                 return 1;
         }
 
@@ -838,7 +867,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                                         printf("%s=", name);
 
                                 /* This property has multiple space-separated values, so
-                                 * neither spaces not newlines can be allowed in a value. */
+                                 * neither spaces nor newlines can be allowed in a value. */
                                 good = str[strcspn(str, " \n")] == '\0';
 
                                 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
@@ -868,7 +897,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                                 return r;
 
                         if (all || n > 0) {
-                                unsigned int i;
+                                unsigned i;
 
                                 if (!value)
                                         printf("%s=", name);
@@ -890,7 +919,7 @@ int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all
                                 return r;
 
                         if (all || n > 0) {
-                                unsigned int i;
+                                unsigned i;
 
                                 if (!value)
                                         printf("%s=", name);
@@ -927,8 +956,8 @@ int bus_message_print_all_properties(
                 return r;
 
         while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
-                const char *name;
-                const char *contents;
+                _cleanup_free_ char *name_with_equal = NULL;
+                const char *name, *contents, *expected_value = NULL;
 
                 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name);
                 if (r < 0)
@@ -940,11 +969,16 @@ int bus_message_print_all_properties(
                                 return log_oom();
 
                         r = set_put(*found_properties, name);
-                        if (r < 0 && r != EEXIST)
+                        if (r < 0 && r != -EEXIST)
                                 return log_oom();
                 }
 
-                if (!filter || strv_find(filter, name)) {
+                name_with_equal = strappend(name, "=");
+                if (!name_with_equal)
+                        return log_oom();
+
+                if (!filter || strv_find(filter, name) ||
+                    (expected_value = strv_find_startswith(filter, name_with_equal))) {
                         r = sd_bus_message_peek_type(m, NULL, &contents);
                         if (r < 0)
                                 return r;
@@ -954,13 +988,13 @@ int bus_message_print_all_properties(
                                 return r;
 
                         if (func)
-                                r = func(name, m, value, all);
+                                r = func(name, expected_value, m, value, all);
                         if (!func || r == 0)
-                                r = bus_print_property(name, m, value, all);
+                                r = bus_print_property(name, expected_value, m, value, all);
                         if (r < 0)
                                 return r;
                         if (r == 0) {
-                                if (all)
+                                if (all && !expected_value)
                                         printf("%s=[unprintable]\n", name);
                                 /* skip what we didn't read */
                                 r = sd_bus_message_skip(m, contents);
@@ -1078,9 +1112,7 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigne
                 if (r < 0)
                         return r;
 
-                strv_free(*p);
-                *p = TAKE_PTR(l);
-                return 0;
+                return strv_free_and_replace(*p, l);
         }
 
         case SD_BUS_TYPE_BOOLEAN: {
@@ -1091,9 +1123,9 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigne
                         return r;
 
                 if (flags & BUS_MAP_BOOLEAN_AS_BOOL)
-                        * (bool*) userdata = !!b;
+                        *(bool*) userdata = b;
                 else
-                        * (int*) userdata = b;
+                        *(int*) userdata = b;
 
                 return 0;
         }
@@ -1300,9 +1332,15 @@ int bus_connect_transport(BusTransport transport, const char *host, bool user, s
         case BUS_TRANSPORT_LOCAL:
                 if (user)
                         r = sd_bus_default_user(&bus);
-                else
-                        r = sd_bus_default_system(&bus);
+                else {
+                        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;
+                        }
+                        r = sd_bus_default_system(&bus);
+                }
                 break;
 
         case BUS_TRANSPORT_REMOTE:
@@ -1343,9 +1381,13 @@ int bus_connect_transport_systemd(BusTransport transport, const char *host, bool
         case BUS_TRANSPORT_LOCAL:
                 if (user)
                         r = bus_connect_user_systemd(bus);
-                else
+                else {
+                        if (sd_booted() <= 0)
+                                /* Print a friendly message when the local system is actually not running systemd as PID 1. */
+                                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;
 
         case BUS_TRANSPORT_REMOTE:
@@ -1392,7 +1434,7 @@ int bus_property_set_bool(
         if (r < 0)
                 return r;
 
-        *(bool *) userdata = !!b;
+        *(bool*) userdata = b;
         return 0;
 }
 
@@ -1603,36 +1645,40 @@ int bus_property_get_rlimit(
                 void *userdata,
                 sd_bus_error *error) {
 
+        const char *is_soft;
         struct rlimit *rl;
         uint64_t u;
         rlim_t x;
-        const char *is_soft;
 
         assert(bus);
         assert(reply);
         assert(userdata);
 
         is_soft = endswith(property, "Soft");
+
         rl = *(struct rlimit**) userdata;
         if (rl)
                 x = is_soft ? rl->rlim_cur : rl->rlim_max;
         else {
                 struct rlimit buf = {};
+                const char *s, *p;
                 int z;
-                const char *s;
 
+                /* Chop off "Soft" suffix */
                 s = is_soft ? strndupa(property, is_soft - property) : property;
 
-                z = rlimit_from_string(strstr(s, "Limit"));
+                /* Skip over any prefix, such as "Default" */
+                assert_se(p = strstr(s, "Limit"));
+
+                z = rlimit_from_string(p + 5);
                 assert(z >= 0);
 
-                getrlimit(z, &buf);
+                (void) getrlimit(z, &buf);
                 x = is_soft ? buf.rlim_cur : buf.rlim_max;
         }
 
-        /* rlim_t might have different sizes, let's map
-         * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
-         * all archs */
+        /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
+         * archs */
         u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
 
         return sd_bus_message_append(reply, "t", u);
@@ -1657,7 +1703,7 @@ int bus_track_add_name_many(sd_bus_track *t, char **l) {
         return r;
 }
 
-int bus_open_system_watch_bind(sd_bus **ret) {
+int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
         const char *e;
         int r;
@@ -1670,6 +1716,12 @@ int bus_open_system_watch_bind(sd_bus **ret) {
         if (r < 0)
                 return r;
 
+        if (description) {
+                r = sd_bus_set_description(bus, description);
+                if (r < 0)
+                        return r;
+        }
+
         e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
         if (!e)
                 e = DEFAULT_SYSTEM_BUS_ADDRESS;
@@ -1706,3 +1758,34 @@ int bus_open_system_watch_bind(sd_bus **ret) {
 
         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;
+        int r;
+
+        assert(m);
+
+        /* 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)
+                return r;
+
+        r = sd_bus_message_open_container(reply, 'a', "{ss}");
+        if (r < 0)
+                return r;
+
+        STRV_FOREACH_PAIR(k, v, l) {
+                r = sd_bus_message_append(reply, "{ss}", *k, *v);
+                if (r < 0)
+                        return r;
+        }
+
+        r = sd_bus_message_close_container(reply);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(NULL, reply, NULL);
+}