]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/bus-print-properties.c
Merge pull request #18863 from keszybz/cmdline-escaping
[thirdparty/systemd.git] / src / shared / bus-print-properties.c
index 4cea25073d1a78741e60547007921e1edb8c3b78..cbd5fb087e01d3008d1a856eaa900bac441b1b91 100644 (file)
 #include "time-util.h"
 #include "user-util.h"
 
-int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value) {
+int bus_print_property_value(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *value) {
         assert(name);
 
         if (expected_value && !streq_ptr(expected_value, value))
                 return 0;
 
-        if (only_value)
-                puts(value);
+        if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) && isempty(value))
+                return 0;
+
+        if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
+                puts(strempty(value));
         else
-                printf("%s=%s\n", name, value);
+                printf("%s=%s\n", name, strempty(value));
 
         return 0;
 }
 
-int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) {
+int bus_print_property_valuef(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *fmt, ...) {
+        _cleanup_free_ char *s = NULL;
         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);
+        r = vasprintf(&s, fmt, ap);
         va_end(ap);
-        puts("");
+        if (r < 0)
+                return -ENOMEM;
 
-        return 0;
+        return bus_print_property_value(name, expected_value, flags, s);
 }
 
-static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) {
+static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, BusPrintPropertyFlags flags) {
         char type;
         const char *contents;
         int r;
@@ -84,13 +68,13 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (r < 0)
                         return r;
 
-                if (all || !isempty(s)) {
+                if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) || !isempty(s)) {
                         bool good;
 
                         /* 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, good ? s : "[unprintable]");
+                        bus_print_property_value(name, expected_value, flags, good ? s : "[unprintable]");
                 }
 
                 return 1;
@@ -106,7 +90,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, yes_no(b));
+                bus_print_property_value(name, NULL, flags, yes_no(b));
                 return 1;
         }
 
@@ -126,22 +110,18 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         const char *t;
 
                         t = format_timestamp(timestamp, sizeof(timestamp), u);
-                        if (t || all)
-                                bus_print_property_value(name, expected_value, value, strempty(t));
+                        bus_print_property_value(name, expected_value, flags, 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, timespan);
-
-                } else if (streq(name, "CoredumpFilter")) {
-                        char buf[STRLEN("0xFFFFFFFF")];
+                        bus_print_property_value(name, expected_value, flags, timespan);
 
-                        xsprintf(buf, "0x%"PRIx64, u);
-                        bus_print_property_value(name, expected_value, value, buf);
+                } else if (streq(name, "CoredumpFilter"))
+                        bus_print_property_valuef(name, expected_value, flags, "0x%"PRIx64, u);
 
-                else if (streq(name, "RestrictNamespaces")) {
+                else if (streq(name, "RestrictNamespaces")) {
                         _cleanup_free_ char *s = NULL;
                         const char *result;
 
@@ -154,10 +134,10 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                                 if (r < 0)
                                         return r;
 
-                                result = strempty(s);
+                                result = s;
                         }
 
-                        bus_print_property_value(name, expected_value, value, result);
+                        bus_print_property_value(name, expected_value, flags, result);
 
                 } else if (streq(name, "MountFlags")) {
                         const char *result;
@@ -166,7 +146,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, result);
+                        bus_print_property_value(name, expected_value, flags, result);
 
                 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
                         _cleanup_free_ char *s = NULL;
@@ -175,26 +155,26 @@ 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);
+                        bus_print_property_value(name, expected_value, flags, 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) ||
                            (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
-                           (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
-                           (endswith(name, "NSec") && u == (uint64_t) -1))
+                           (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == UINT64_MAX) ||
+                           (endswith(name, "NSec") && u == UINT64_MAX))
 
-                        bus_print_property_value(name, expected_value, value, "[not set]");
+                        bus_print_property_value(name, expected_value, flags, "[not set]");
 
                 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))
+                         (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == UINT64_MAX) ||
+                         (startswith(name, "Limit") && u == UINT64_MAX) ||
+                         (startswith(name, "DefaultLimit") && u == UINT64_MAX))
 
-                        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]");
+                        bus_print_property_value(name, expected_value, flags, "infinity");
+                else if (STR_IN_SET(name, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u == UINT64_MAX)
+                        bus_print_property_value(name, expected_value, flags, "[no data]");
                 else
-                        bus_print_property_valuef(name, expected_value, value, "%"PRIu64, u);
+                        bus_print_property_valuef(name, expected_value, flags, "%"PRIu64, u);
 
                 return 1;
         }
@@ -206,7 +186,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (r < 0)
                         return r;
 
-                bus_print_property_valuef(name, expected_value, value, "%"PRIi64, i);
+                bus_print_property_valuef(name, expected_value, flags, "%"PRIi64, i);
                 return 1;
         }
 
@@ -218,20 +198,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_valuef(name, expected_value, value, "%04o", u);
+                        bus_print_property_valuef(name, expected_value, flags, "%04o", u);
 
                 else if (streq(name, "UID")) {
                         if (u == UID_INVALID)
-                                bus_print_property_value(name, expected_value, value, "[not set]");
+                                bus_print_property_value(name, expected_value, flags, "[not set]");
                         else
-                                bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
+                                bus_print_property_valuef(name, expected_value, flags, "%"PRIu32, u);
                 } else if (streq(name, "GID")) {
                         if (u == GID_INVALID)
-                                bus_print_property_value(name, expected_value, value, "[not set]");
+                                bus_print_property_value(name, expected_value, flags, "[not set]");
                         else
-                                bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
+                                bus_print_property_valuef(name, expected_value, flags, "%"PRIu32, u);
                 } else
-                        bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
+                        bus_print_property_valuef(name, expected_value, flags, "%"PRIu32, u);
 
                 return 1;
         }
@@ -243,7 +223,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (r < 0)
                         return r;
 
-                bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i);
+                bus_print_property_valuef(name, expected_value, flags, "%"PRIi32, i);
                 return 1;
         }
 
@@ -254,7 +234,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                 if (r < 0)
                         return r;
 
-                bus_print_property_valuef(name, expected_value, value, "%g", d);
+                bus_print_property_valuef(name, expected_value, flags, "%g", d);
                 return 1;
         }
 
@@ -270,12 +250,12 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) {
                                 _cleanup_free_ char *e = NULL;
 
-                                e = shell_maybe_quote(str, ESCAPE_BACKSLASH_ONELINE);
+                                e = shell_maybe_quote(str, 0);
                                 if (!e)
                                         return -ENOMEM;
 
                                 if (first) {
-                                        if (!value)
+                                        if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
                                                 printf("%s=", name);
                                         first = false;
                                 } else
@@ -286,9 +266,9 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         if (r < 0)
                                 return r;
 
-                        if (first && all && !value)
+                        if (first && FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) && !FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
                                 printf("%s=", name);
-                        if (!first || all)
+                        if (!first || FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY))
                                 puts("");
 
                         r = sd_bus_message_exit_container(m);
@@ -305,10 +285,10 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         if (r < 0)
                                 return r;
 
-                        if (all || n > 0) {
+                        if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) || n > 0) {
                                 unsigned i;
 
-                                if (!value)
+                                if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
                                         printf("%s=", name);
 
                                 for (i = 0; i < n; i++)
@@ -327,10 +307,10 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
                         if (r < 0)
                                 return r;
 
-                        if (all || n > 0) {
+                        if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) || n > 0) {
                                 unsigned i;
 
-                                if (!value)
+                                if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
                                         printf("%s=", name);
 
                                 for (i = 0; i < n; i++)
@@ -352,8 +332,7 @@ int bus_message_print_all_properties(
                 sd_bus_message *m,
                 bus_message_print_t func,
                 char **filter,
-                bool value,
-                bool all,
+                BusPrintPropertyFlags flags,
                 Set **found_properties) {
 
         int r;
@@ -393,13 +372,13 @@ int bus_message_print_all_properties(
                                 return r;
 
                         if (func)
-                                r = func(name, expected_value, m, value, all);
+                                r = func(name, expected_value, m, flags);
                         if (!func || r == 0)
-                                r = bus_print_property(name, expected_value, m, value, all);
+                                r = bus_print_property(name, expected_value, m, flags);
                         if (r < 0)
                                 return r;
                         if (r == 0) {
-                                if (all && !expected_value)
+                                if (FLAGS_SET(flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) && !expected_value)
                                         printf("%s=[unprintable]\n", name);
                                 /* skip what we didn't read */
                                 r = sd_bus_message_skip(m, contents);
@@ -436,8 +415,7 @@ int bus_print_all_properties(
                 const char *path,
                 bus_message_print_t func,
                 char **filter,
-                bool value,
-                bool all,
+                BusPrintPropertyFlags flags,
                 Set **found_properties) {
 
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
@@ -458,5 +436,5 @@ int bus_print_all_properties(
         if (r < 0)
                 return r;
 
-        return bus_message_print_all_properties(reply, func, filter, value, all, found_properties);
+        return bus_message_print_all_properties(reply, func, filter, flags, found_properties);
 }