bus-print-property: introduce BusPrintPropertyFlags
Follow-ups for 1ceaad6937.
This commit is contained in:
committed by
Lennart Poettering
parent
feb10c665f
commit
255b1fc8a3
@@ -13,57 +13,41 @@
|
||||
#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);
|
||||
bus_print_property_value(name, expected_value, flags, timespan);
|
||||
|
||||
} else if (streq(name, "CoredumpFilter")) {
|
||||
char buf[STRLEN("0xFFFFFFFF")];
|
||||
} else if (streq(name, "CoredumpFilter"))
|
||||
bus_print_property_valuef(name, expected_value, flags, "0x%"PRIx64, u);
|
||||
|
||||
xsprintf(buf, "0x%"PRIx64, u);
|
||||
bus_print_property_value(name, expected_value, value, buf);
|
||||
|
||||
} 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,8 +146,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
|
||||
if (!result)
|
||||
return -EINVAL;
|
||||
|
||||
if (all || !isempty(result))
|
||||
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;
|
||||
@@ -176,8 +155,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (all || !isempty(s))
|
||||
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) ||
|
||||
@@ -185,18 +163,18 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
|
||||
(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_MAX) ||
|
||||
(startswith(name, "Limit") && u == UINT64_MAX) ||
|
||||
(startswith(name, "DefaultLimit") && u == UINT64_MAX))
|
||||
|
||||
bus_print_property_value(name, expected_value, value, "infinity");
|
||||
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, value, "[no data]");
|
||||
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;
|
||||
}
|
||||
@@ -208,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;
|
||||
}
|
||||
|
||||
@@ -220,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;
|
||||
}
|
||||
@@ -245,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;
|
||||
}
|
||||
|
||||
@@ -256,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;
|
||||
}
|
||||
|
||||
@@ -277,7 +255,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
|
||||
return -ENOMEM;
|
||||
|
||||
if (first) {
|
||||
if (!value)
|
||||
if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
|
||||
printf("%s=", name);
|
||||
first = false;
|
||||
} else
|
||||
@@ -288,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);
|
||||
@@ -307,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++)
|
||||
@@ -329,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++)
|
||||
@@ -354,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;
|
||||
@@ -395,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);
|
||||
@@ -438,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;
|
||||
@@ -460,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);
|
||||
}
|
||||
|
||||
@@ -8,9 +8,14 @@
|
||||
#include "macro.h"
|
||||
#include "set.h"
|
||||
|
||||
typedef int (*bus_message_print_t) (const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all);
|
||||
typedef enum BusPrintPropertyFlags {
|
||||
BUS_PRINT_PROPERTY_ONLY_VALUE = 1 << 0, /* e.g. systemctl --value */
|
||||
BUS_PRINT_PROPERTY_SHOW_EMPTY = 1 << 1, /* e.g. systemctl --all */
|
||||
} BusPrintPropertyFlags;
|
||||
|
||||
int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value);
|
||||
int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) _printf_(4,5);
|
||||
int bus_message_print_all_properties(sd_bus_message *m, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties);
|
||||
int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties);
|
||||
typedef int (*bus_message_print_t) (const char *name, const char *expected_value, sd_bus_message *m, BusPrintPropertyFlags flags);
|
||||
|
||||
int bus_print_property_value(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *value);
|
||||
int bus_print_property_valuef(const char *name, const char *expected_value, BusPrintPropertyFlags flags, const char *fmt, ...) _printf_(4,5);
|
||||
int bus_message_print_all_properties(sd_bus_message *m, bus_message_print_t func, char **filter, BusPrintPropertyFlags flags, Set **found_properties);
|
||||
int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, bus_message_print_t func, char **filter, BusPrintPropertyFlags flags, Set **found_properties);
|
||||
|
||||
Reference in New Issue
Block a user