From 102b02146056516cb9ff21e34daebd34671ec892 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 2 Mar 2019 23:35:26 +0900 Subject: [PATCH 01/15] bus-util: drop unnecessary re-formatting --- src/login/loginctl.c | 4 +-- src/shared/bus-util.c | 54 +++++++++++++++++++------------ src/shared/bus-util.h | 3 +- src/systemctl/systemctl.c | 65 +++++++++++++++++++------------------- src/timedate/timedatectl.c | 2 +- 5 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/login/loginctl.c b/src/login/loginctl.c index ab1b56201a..69850724da 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -755,7 +755,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); if (all || !isempty(s)) - bus_print_property_value(name, expected_value, value, "%s", s); + bus_print_property_value(name, expected_value, value, s); return 1; @@ -771,7 +771,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m "Invalid user ID: " UID_FMT, uid); - bus_print_property_value(name, expected_value, value, UID_FMT, uid); + bus_print_property_valuef(name, expected_value, value, UID_FMT, uid); return 1; } break; diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index cbcf698e96..90cddd7e04 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -628,7 +628,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 +705,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 +721,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 +742,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; @@ -752,7 +766,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 +775,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 +784,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 +792,16 @@ 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) || (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 - bus_print_property_value(name, expected_value, value, "%"PRIu64, u); + bus_print_property_valuef(name, expected_value, value, "%"PRIu64, u); return 1; } @@ -799,7 +813,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 +825,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 +850,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 +861,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; } diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 71c248fe3c..2e42c45740 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -64,7 +64,8 @@ int bus_connect_transport_systemd(BusTransport transport, const char *host, bool typedef int (*bus_message_print_t) (const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all); -int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) _printf_(4,5); +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); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index fc21d318ad..c3030b9f1d 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4679,9 +4679,9 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); if (u > 0) - bus_print_property_value(name, expected_value, value, "%"PRIu32, u); + bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); else if (all) - bus_print_property_value(name, expected_value, value, "%s", ""); + bus_print_property_value(name, expected_value, value, ""); return 1; @@ -4693,7 +4693,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); if (all || !isempty(s)) - bus_print_property_value(name, expected_value, value, "%s", s); + bus_print_property_value(name, expected_value, value, s); return 1; @@ -4705,7 +4705,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); if (all || !isempty(a) || !isempty(b)) - bus_print_property_value(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); + bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); return 1; } else if (streq_ptr(name, "SystemCallFilter")) { @@ -4752,6 +4752,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m } return 1; + } else if (STR_IN_SET(name, "SELinuxContext", "AppArmorProfile", "SmackProcessLabel")) { int ignore; const char *s; @@ -4761,9 +4762,9 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); if (!isempty(s)) - bus_print_property_value(name, expected_value, value, "%s%s", ignore ? "-" : "", s); + bus_print_property_valuef(name, expected_value, value, "%s%s", ignore ? "-" : "", s); else if (all) - bus_print_property_value(name, expected_value, value, "%s", ""); + bus_print_property_value(name, expected_value, value, ""); return 1; } @@ -4781,7 +4782,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) - bus_print_property_value(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); + bus_print_property_valuef(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); if (r < 0) return bus_log_parse_error(r); @@ -4800,7 +4801,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) - bus_print_property_value(name, expected_value, value, "%s (%s)", path, type); + bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); if (r < 0) return bus_log_parse_error(r); @@ -4818,7 +4819,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) - bus_print_property_value(name, expected_value, value, "%s (%s)", path, type); + bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); if (r < 0) return bus_log_parse_error(r); @@ -4839,9 +4840,9 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; - bus_print_property_value(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, - format_timespan(timespan1, sizeof(timespan1), v, 0), - format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); + bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, + format_timespan(timespan1, sizeof(timespan1), v, 0), + format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); } if (r < 0) return bus_log_parse_error(r); @@ -4863,8 +4864,8 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { char timestamp[FORMAT_TIMESTAMP_MAX]; - bus_print_property_value(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, spec, - format_timestamp(timestamp, sizeof(timestamp), next_elapse)); + bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, spec, + format_timestamp(timestamp, sizeof(timestamp), next_elapse)); } if (r < 0) return bus_log_parse_error(r); @@ -4888,18 +4889,18 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m tt = strv_join(info.argv, " "); - bus_print_property_value(name, expected_value, value, - "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", - strna(info.path), - strna(tt), - yes_no(info.ignore), - strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), - strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), - info.pid, - sigchld_code_to_string(info.code), - info.status, - info.code == CLD_EXITED ? "" : "/", - strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); + bus_print_property_valuef(name, expected_value, value, + "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", + strna(info.path), + strna(tt), + yes_no(info.ignore), + strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), + strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), + info.pid, + sigchld_code_to_string(info.code), + info.status, + info.code == CLD_EXITED ? "" : "/", + strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); free(info.path); strv_free(info.argv); @@ -4920,7 +4921,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) - bus_print_property_value(name, expected_value, value, "%s %s", strna(path), strna(rwm)); + bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), strna(rwm)); if (r < 0) return bus_log_parse_error(r); @@ -4940,7 +4941,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) - bus_print_property_value(name, expected_value, value, "%s %"PRIu64, strna(path), weight); + bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), weight); if (r < 0) return bus_log_parse_error(r); @@ -4961,7 +4962,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) - bus_print_property_value(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); + bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); if (r < 0) return bus_log_parse_error(r); @@ -4982,8 +4983,8 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m return bus_log_parse_error(r); while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0) - bus_print_property_value(name, expected_value, value, "%s %s", strna(path), - format_timespan(ts, sizeof(ts), target, 1)); + bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), + format_timespan(ts, sizeof(ts), target, 1)); if (r < 0) return bus_log_parse_error(r); @@ -5007,7 +5008,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m if (n < 0) return log_oom(); - bus_print_property_value(name, expected_value, value, "%s", h); + bus_print_property_value(name, expected_value, value, h); return 1; } diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 1e7b26276a..406ca1df66 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -670,7 +670,7 @@ static int print_timesync_property(const char *name, const char *expected_value, return r; if (arg_all || !isempty(str)) - bus_print_property_value(name, expected_value, value, "%s", str); + bus_print_property_value(name, expected_value, value, str); return 1; } From a3ddd4571d8de6ef391704f61af4d01b93a44292 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 2 Mar 2019 23:36:51 +0900 Subject: [PATCH 02/15] systemctl: do not show negative values in {Success,Failure}ActionExitStatus= --- src/systemctl/systemctl.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index c3030b9f1d..966218149f 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4669,6 +4669,23 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m switch (bus_type) { + case SD_BUS_TYPE_INT32: + if (endswith(name, "ActionExitStatus")) { + int32_t i; + + r = sd_bus_message_read_basic(m, bus_type, &i); + if (r < 0) + return r; + + if (i >= 0 && i <= 255) + bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); + else if (all) + bus_print_property_value(name, expected_value, value, "[not set]"); + + return 1; + } + break; + case SD_BUS_TYPE_STRUCT: if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { From 8e066ef316ed1af3758b35d544c80dbca878df66 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 2 Mar 2019 23:50:59 +0900 Subject: [PATCH 03/15] systemctl: use streq() if arguments must be non-NULL --- src/systemctl/systemctl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 966218149f..984dd4deaa 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4725,7 +4725,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); return 1; - } else if (streq_ptr(name, "SystemCallFilter")) { + } else if (streq(name, "SystemCallFilter")) { _cleanup_strv_free_ char **l = NULL; int whitelist; From 4a22071201127f6da5d0913cd5a4471a64d52b96 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 00:33:28 +0900 Subject: [PATCH 04/15] systemctl: show SuccessExitStatus= and friends --- src/systemctl/systemctl.c | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 984dd4deaa..52061f36fd 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4784,6 +4784,68 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m bus_print_property_value(name, expected_value, value, ""); return 1; + + } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) { + const int32_t *status, *signal; + size_t sz_status, sz_signal, i; + + r = sd_bus_message_enter_container(m, 'r', "aiai"); + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_read_array(m, 'i', (const void **) &status, &sz_status); + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_read_array(m, 'i', (const void **) &signal, &sz_signal); + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_exit_container(m); + if (r < 0) + return bus_log_parse_error(r); + + sz_status /= sizeof(int32_t); + sz_signal /= sizeof(int32_t); + + if (all || sz_status > 0 || sz_signal > 0) { + bool first = true; + + if (!value) { + fputs(name, stdout); + fputc('=', stdout); + } + + for (i = 0; i < sz_status; i++) { + if (status[i] < 0 || status[i] > 255) + continue; + + if (first) + first = false; + else + fputc(' ', stdout); + + printf("%"PRIi32, status[i]); + } + + for (i = 0; i < sz_signal; i++) { + const char *str; + + str = signal_to_string((int) signal[i]); + if (!str) + continue; + + if (first) + first = false; + else + fputc(' ', stdout); + + fputs(str, stdout); + } + + fputc('\n', stdout); + } + return 1; } break; From a672f4fe8d41c7ab94a5b6b4056be39eb2ceb068 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 00:35:58 +0900 Subject: [PATCH 05/15] core: fix received size of signal or status size sd_bus_message_read_array() returns size of array in bytes, not number of elements. This also convert int to int32_t, as the dbus type 'i' is int32_t. --- src/core/dbus-service.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/core/dbus-service.c b/src/core/dbus-service.c index 0904cc09c0..0eb336cf03 100644 --- a/src/core/dbus-service.c +++ b/src/core/dbus-service.c @@ -55,7 +55,7 @@ static int property_get_exit_status_set( return r; SET_FOREACH(id, status_set->status, i) { - int val = PTR_TO_INT(id); + int32_t val = PTR_TO_INT(id); if (val < 0 || val > 255) continue; @@ -74,10 +74,10 @@ static int property_get_exit_status_set( return r; SET_FOREACH(id, status_set->signal, i) { - int val = PTR_TO_INT(id); + int32_t val = PTR_TO_INT(id); const char *str; - str = signal_to_string(val); + str = signal_to_string((int) val); if (!str) continue; @@ -151,7 +151,7 @@ static int bus_set_transient_exit_status( UnitWriteFlags flags, sd_bus_error *error) { - const int *status, *signal; + const int32_t *status, *signal; size_t sz_status, sz_signal, i; int r; @@ -171,6 +171,9 @@ static int bus_set_transient_exit_status( if (r < 0) return r; + sz_status /= sizeof(int32_t); + sz_signal /= sizeof(int32_t); + if (sz_status == 0 && sz_signal == 0 && !UNIT_WRITE_FLAGS_NOOP(flags)) { exit_status_set_free(status_set); unit_write_settingf(u, flags, name, "%s=", name); @@ -179,34 +182,34 @@ static int bus_set_transient_exit_status( for (i = 0; i < sz_status; i++) { if (status[i] < 0 || status[i] > 255) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid status code in %s: %i", name, status[i]); + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid status code in %s: %"PRIi32, name, status[i]); if (!UNIT_WRITE_FLAGS_NOOP(flags)) { r = set_ensure_allocated(&status_set->status, NULL); if (r < 0) return r; - r = set_put(status_set->status, INT_TO_PTR(status[i])); + r = set_put(status_set->status, INT_TO_PTR((int) status[i])); if (r < 0) return r; - unit_write_settingf(u, flags, name, "%s=%i", name, status[i]); + unit_write_settingf(u, flags, name, "%s=%"PRIi32, name, status[i]); } } for (i = 0; i < sz_signal; i++) { const char *str; - str = signal_to_string(signal[i]); + str = signal_to_string((int) signal[i]); if (!str) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal in %s: %i", name, signal[i]); + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal in %s: %"PRIi32, name, signal[i]); if (!UNIT_WRITE_FLAGS_NOOP(flags)) { r = set_ensure_allocated(&status_set->signal, NULL); if (r < 0) return r; - r = set_put(status_set->signal, INT_TO_PTR(signal[i])); + r = set_put(status_set->signal, INT_TO_PTR((int) signal[i])); if (r < 0) return r; From 81260be12e04a4e0fc929b372ee72190c1b66f52 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 01:16:50 +0900 Subject: [PATCH 06/15] util: use _cleanup_ attributes --- src/basic/in-addr-util.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index bc6b9ce44d..122bb17a95 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -294,7 +294,7 @@ int in_addr_random_prefix( } int in_addr_to_string(int family, const union in_addr_union *u, char **ret) { - char *x; + _cleanup_free_ char *x = NULL; size_t l; assert(u); @@ -312,18 +312,16 @@ int in_addr_to_string(int family, const union in_addr_union *u, char **ret) { return -ENOMEM; errno = 0; - if (!inet_ntop(family, u, x, l)) { - free(x); + if (!inet_ntop(family, u, x, l)) return errno > 0 ? -errno : -EINVAL; - } - *ret = x; + *ret = TAKE_PTR(x); return 0; } int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret) { + _cleanup_free_ char *x = NULL; size_t l; - char *x; int r; assert(u); @@ -349,14 +347,12 @@ int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifin return -ENOMEM; errno = 0; - if (!inet_ntop(family, u, x, l)) { - free(x); + if (!inet_ntop(family, u, x, l)) return errno > 0 ? -errno : -EINVAL; - } sprintf(strchr(x, 0), "%%%i", ifindex); - *ret = x; + *ret = TAKE_PTR(x); return 0; fallback: From d9143dac3eb0536a95742b1354c0989270e17a8f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 01:17:32 +0900 Subject: [PATCH 07/15] util: introduce in_addr_prefix_to_string() --- src/basic/in-addr-util.c | 35 +++++++++++++++++++++++++++++++++++ src/basic/in-addr-util.h | 1 + 2 files changed, 36 insertions(+) diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index 122bb17a95..f5cabc6534 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -12,6 +12,7 @@ #include "macro.h" #include "parse-util.h" #include "random-util.h" +#include "strxcpyx.h" #include "util.h" bool in4_addr_is_null(const struct in_addr *a) { @@ -319,6 +320,40 @@ int in_addr_to_string(int family, const union in_addr_union *u, char **ret) { return 0; } +int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned prefixlen, char **ret) { + _cleanup_free_ char *x = NULL; + char *p; + size_t l; + + assert(u); + assert(ret); + + if (family == AF_INET) + l = INET_ADDRSTRLEN + 3; + else if (family == AF_INET6) + l = INET6_ADDRSTRLEN + 4; + else + return -EAFNOSUPPORT; + + if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) + return -EINVAL; + + x = new(char, l); + if (!x) + return -ENOMEM; + + errno = 0; + if (!inet_ntop(family, u, x, l)) + return errno > 0 ? -errno : -EINVAL; + + p = x + strlen(x); + l -= strlen(x); + (void) strpcpyf(&p, l, "/%u", prefixlen); + + *ret = TAKE_PTR(x); + return 0; +} + int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret) { _cleanup_free_ char *x = NULL; size_t l; diff --git a/src/basic/in-addr-util.h b/src/basic/in-addr-util.h index 3517fe1ea3..a6a685b918 100644 --- a/src/basic/in-addr-util.h +++ b/src/basic/in-addr-util.h @@ -37,6 +37,7 @@ int in_addr_prefix_intersect(int family, const union in_addr_union *a, unsigned int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen); int in_addr_random_prefix(int family, union in_addr_union *u, unsigned prefixlen_fixed_part, unsigned prefixlen); int in_addr_to_string(int family, const union in_addr_union *u, char **ret); +int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned prefixlen, char **ret); int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret); int in_addr_from_string(int family, const char *s, union in_addr_union *ret); int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret); From 146cf7f05b3bc76545576e1f8bb4e6c3320e173f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 4 Mar 2019 22:47:10 +0900 Subject: [PATCH 08/15] test: move tests for in_addr_prefix_from_string() --- src/test/test-in-addr-util.c | 57 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/test/test-in-addr-util.c b/src/test/test-in-addr-util.c index 49ca4dc4af..4574cab604 100644 --- a/src/test/test-in-addr-util.c +++ b/src/test/test-in-addr-util.c @@ -7,7 +7,7 @@ #include "strv.h" #include "in-addr-util.h" -static void test_in_addr_prefix_from_string( +static void test_in_addr_prefix_from_string_one( const char *p, int family, int ret, @@ -57,6 +57,34 @@ static void test_in_addr_prefix_from_string( } } +static void test_in_addr_prefix_from_string(void) { + test_in_addr_prefix_from_string_one("", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); + test_in_addr_prefix_from_string_one("/", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); + test_in_addr_prefix_from_string_one("/8", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); + test_in_addr_prefix_from_string_one("1.2.3.4", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, -ENOANO, 0, 0, 8); + test_in_addr_prefix_from_string_one("1.2.3.4/0", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 0, 0, 0, 0, 0); + test_in_addr_prefix_from_string_one("1.2.3.4/1", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 1, 0, 1, 0, 1); + test_in_addr_prefix_from_string_one("1.2.3.4/2", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 2, 0, 2, 0, 2); + test_in_addr_prefix_from_string_one("1.2.3.4/32", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, 0, 32, 0, 32); + test_in_addr_prefix_from_string_one("1.2.3.4/33", AF_INET, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); + test_in_addr_prefix_from_string_one("1.2.3.4/-1", AF_INET, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); + test_in_addr_prefix_from_string_one("::1", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); + + test_in_addr_prefix_from_string_one("", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); + test_in_addr_prefix_from_string_one("/", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); + test_in_addr_prefix_from_string_one("/8", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); + test_in_addr_prefix_from_string_one("::1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, -ENOANO, 0, 0, 0); + test_in_addr_prefix_from_string_one("::1/0", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0, 0, 0, 0, 0); + test_in_addr_prefix_from_string_one("::1/1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 1, 0, 1, 0, 1); + test_in_addr_prefix_from_string_one("::1/2", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 2, 0, 2, 0, 2); + test_in_addr_prefix_from_string_one("::1/32", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 32, 0, 32, 0, 32); + test_in_addr_prefix_from_string_one("::1/33", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 33, 0, 33, 0, 33); + test_in_addr_prefix_from_string_one("::1/64", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 64, 0, 64, 0, 64); + test_in_addr_prefix_from_string_one("::1/128", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, 0, 128, 0, 128); + test_in_addr_prefix_from_string_one("::1/129", AF_INET6, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); + test_in_addr_prefix_from_string_one("::1/-1", AF_INET6, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); +} + static void test_in_addr_random_prefix(void) { _cleanup_free_ char *str = NULL; union in_addr_union a; @@ -102,32 +130,7 @@ static void test_in_addr_random_prefix(void) { } int main(int argc, char *argv[]) { - test_in_addr_prefix_from_string("", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); - test_in_addr_prefix_from_string("/", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); - test_in_addr_prefix_from_string("/8", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); - test_in_addr_prefix_from_string("1.2.3.4", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, -ENOANO, 0, 0, 8); - test_in_addr_prefix_from_string("1.2.3.4/0", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 0, 0, 0, 0, 0); - test_in_addr_prefix_from_string("1.2.3.4/1", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 1, 0, 1, 0, 1); - test_in_addr_prefix_from_string("1.2.3.4/2", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 2, 0, 2, 0, 2); - test_in_addr_prefix_from_string("1.2.3.4/32", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32, 0, 32, 0, 32); - test_in_addr_prefix_from_string("1.2.3.4/33", AF_INET, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); - test_in_addr_prefix_from_string("1.2.3.4/-1", AF_INET, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); - test_in_addr_prefix_from_string("::1", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); - - test_in_addr_prefix_from_string("", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); - test_in_addr_prefix_from_string("/", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); - test_in_addr_prefix_from_string("/8", AF_INET6, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); - test_in_addr_prefix_from_string("::1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, -ENOANO, 0, 0, 0); - test_in_addr_prefix_from_string("::1/0", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0, 0, 0, 0, 0); - test_in_addr_prefix_from_string("::1/1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 1, 0, 1, 0, 1); - test_in_addr_prefix_from_string("::1/2", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 2, 0, 2, 0, 2); - test_in_addr_prefix_from_string("::1/32", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 32, 0, 32, 0, 32); - test_in_addr_prefix_from_string("::1/33", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 33, 0, 33, 0, 33); - test_in_addr_prefix_from_string("::1/64", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 64, 0, 64, 0, 64); - test_in_addr_prefix_from_string("::1/128", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128, 0, 128, 0, 128); - test_in_addr_prefix_from_string("::1/129", AF_INET6, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); - test_in_addr_prefix_from_string("::1/-1", AF_INET6, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); - + test_in_addr_prefix_from_string(); test_in_addr_random_prefix(); return 0; From 9201eea137e5c8641bd97cde3491f84ebe8f4675 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 4 Mar 2019 23:07:35 +0900 Subject: [PATCH 09/15] test: add tests for test_in_addr_prefix_to_string() --- src/test/test-in-addr-util.c | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/test/test-in-addr-util.c b/src/test/test-in-addr-util.c index 4574cab604..916bd35f0d 100644 --- a/src/test/test-in-addr-util.c +++ b/src/test/test-in-addr-util.c @@ -85,6 +85,54 @@ static void test_in_addr_prefix_from_string(void) { test_in_addr_prefix_from_string_one("::1/-1", AF_INET6, -ERANGE, NULL, 0, -ERANGE, 0, -ERANGE, 0); } +static void test_in_addr_prefix_to_string_valid(int family, const char *p) { + _cleanup_free_ char *str = NULL; + union in_addr_union u; + unsigned char l; + + log_info("/* %s */", p); + + assert_se(in_addr_prefix_from_string(p, family, &u, &l) >= 0); + assert_se(in_addr_prefix_to_string(family, &u, l, &str) >= 0); + assert_se(streq(str, p)); +} + +static void test_in_addr_prefix_to_string_unoptimized(int family, const char *p) { + _cleanup_free_ char *str1 = NULL, *str2 = NULL; + union in_addr_union u1, u2; + unsigned char len1, len2; + + log_info("/* %s */", p); + + assert_se(in_addr_prefix_from_string(p, family, &u1, &len1) >= 0); + assert_se(in_addr_prefix_to_string(family, &u1, len1, &str1) >= 0); + assert_se(in_addr_prefix_from_string(str1, family, &u2, &len2) >= 0); + assert_se(in_addr_prefix_to_string(family, &u2, len2, &str2) >= 0); + + assert_se(streq(str1, str2)); + assert_se(len1 == len2); + assert_se(in_addr_equal(family, &u1, &u2) > 0); +} + +static void test_in_addr_prefix_to_string(void) { + test_in_addr_prefix_to_string_valid(AF_INET, "0.0.0.0/32"); + test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/0"); + test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/24"); + test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/32"); + test_in_addr_prefix_to_string_valid(AF_INET, "255.255.255.255/32"); + + test_in_addr_prefix_to_string_valid(AF_INET6, "::1/128"); + test_in_addr_prefix_to_string_valid(AF_INET6, "fd00:abcd::1/64"); + test_in_addr_prefix_to_string_valid(AF_INET6, "fd00:abcd::1234:1/64"); + test_in_addr_prefix_to_string_valid(AF_INET6, "1111:2222:3333:4444:5555:6666:7777:8888/128"); + + test_in_addr_prefix_to_string_unoptimized(AF_INET, "0.0.0.0"); + test_in_addr_prefix_to_string_unoptimized(AF_INET, "192.168.0.1"); + + test_in_addr_prefix_to_string_unoptimized(AF_INET6, "fd00:0000:0000:0000:0000:0000:0000:0001/64"); + test_in_addr_prefix_to_string_unoptimized(AF_INET6, "fd00:1111::0000:2222:3333:4444:0001/64"); +} + static void test_in_addr_random_prefix(void) { _cleanup_free_ char *str = NULL; union in_addr_union a; @@ -132,6 +180,7 @@ static void test_in_addr_random_prefix(void) { int main(int argc, char *argv[]) { test_in_addr_prefix_from_string(); test_in_addr_random_prefix(); + test_in_addr_prefix_to_string(); return 0; } From 9c1f9465665300194d6f7c753bef9e4510cab87b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 01:27:10 +0900 Subject: [PATCH 10/15] systemctl: show IPAddressAllow= and IPAddressDeny= in 'show' command --- src/systemctl/systemctl.c | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 52061f36fd..1629307f84 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -41,6 +41,7 @@ #include "hostname-util.h" #include "initreq.h" #include "install.h" +#include "in-addr-util.h" #include "io-util.h" #include "list.h" #include "locale-util.h" @@ -5090,6 +5091,67 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m bus_print_property_value(name, expected_value, value, h); return 1; + + } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) { + _cleanup_free_ char *addresses = NULL; + + r = sd_bus_message_enter_container(m, 'a', "(iayu)"); + if (r < 0) + return bus_log_parse_error(r); + + for (;;) { + _cleanup_free_ char *str = NULL; + uint32_t prefixlen; + int32_t family; + const void *ap; + size_t an; + + r = sd_bus_message_enter_container(m, 'r', "iayu"); + if (r < 0) + return bus_log_parse_error(r); + if (r == 0) + break; + + r = sd_bus_message_read(m, "i", &family); + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_read_array(m, 'y', &ap, &an); + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_read(m, "u", &prefixlen); + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_exit_container(m); + if (r < 0) + return bus_log_parse_error(r); + + if (!IN_SET(family, AF_INET, AF_INET6)) + continue; + + if (an != FAMILY_ADDRESS_SIZE(family)) + continue; + + if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) + continue; + + if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) + continue; + + if (!strextend_with_separator(&addresses, " ", str, NULL)) + return log_oom(); + } + + r = sd_bus_message_exit_container(m); + if (r < 0) + return bus_log_parse_error(r); + + if (all || !isempty(addresses)) + bus_print_property_value(name, expected_value, value, strempty(addresses)); + + return 1; } break; From 56c6b690075e01fbeec6da748c0db484b3c3bd56 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 01:35:18 +0900 Subject: [PATCH 11/15] systemctl: format IPIngressBytes= or friends nicely --- src/shared/bus-util.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 90cddd7e04..1911dd0ce6 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -800,6 +800,8 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b (startswith(name, "DefaultLimit") && u == (uint64_t) -1)) 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_valuef(name, expected_value, value, "%"PRIu64, u); From 48d3358db14205741cc94ce8fc3f36cac3359811 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 01:59:58 +0900 Subject: [PATCH 12/15] systemctl: show nothing if no LoadError= --- src/systemctl/systemctl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 1629307f84..44451c01d6 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4722,8 +4722,10 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m if (r < 0) return bus_log_parse_error(r); - if (all || !isempty(a) || !isempty(b)) + if (!isempty(a) || !isempty(b)) bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); + else if (all) + bus_print_property_value(name, expected_value, value, ""); return 1; } else if (streq(name, "SystemCallFilter")) { From 04749e40038c77d0d5d8d6f0812f2e1126d70438 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 02:06:18 +0900 Subject: [PATCH 13/15] systemctl: print RestrictAddressFamilies= in 'show' command --- src/systemctl/systemctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 44451c01d6..fa3f054673 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4728,7 +4728,8 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m bus_print_property_value(name, expected_value, value, ""); return 1; - } else if (streq(name, "SystemCallFilter")) { + + } else if (STR_IN_SET(name, "SystemCallFilter", "RestrictAddressFamilies")) { _cleanup_strv_free_ char **l = NULL; int whitelist; From 4c787df72f7dc03347f73f4f2e0efee3a3abb2ce Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 02:27:04 +0900 Subject: [PATCH 14/15] systemctl: format BindPaths= or TemporaryFileSystems= in 'show' command --- src/systemctl/systemctl.c | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index fa3f054673..e54da97b81 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -5155,6 +5156,77 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m bus_print_property_value(name, expected_value, value, strempty(addresses)); return 1; + + } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) { + _cleanup_free_ char *paths = NULL; + const char *source, *dest; + int ignore_enoent; + uint64_t rbind; + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssbt)"); + if (r < 0) + return bus_log_parse_error(r); + + while ((r = sd_bus_message_read(m, "(ssbt)", &source, &dest, &ignore_enoent, &rbind)) > 0) { + _cleanup_free_ char *str = NULL; + + if (isempty(source)) + continue; + + if (asprintf(&str, "%s%s%s%s%s", + ignore_enoent ? "-" : "", + source, + isempty(dest) ? "" : ":", + strempty(dest), + rbind == MS_REC ? ":rbind" : "") < 0) + return log_oom(); + + if (!strextend_with_separator(&paths, " ", str, NULL)) + return log_oom(); + } + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_exit_container(m); + if (r < 0) + return bus_log_parse_error(r); + + if (all || !isempty(paths)) + bus_print_property_value(name, expected_value, value, strempty(paths)); + + return 1; + + } else if (streq(name, "TemporaryFileSystem")) { + _cleanup_free_ char *paths = NULL; + const char *target, *option; + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); + if (r < 0) + return bus_log_parse_error(r); + + while ((r = sd_bus_message_read(m, "(ss)", &target, &option)) > 0) { + _cleanup_free_ char *str = NULL; + + if (isempty(target)) + continue; + + if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0) + return log_oom(); + + if (!strextend_with_separator(&paths, " ", str, NULL)) + return log_oom(); + } + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_exit_container(m); + if (r < 0) + return bus_log_parse_error(r); + + if (all || !isempty(paths)) + bus_print_property_value(name, expected_value, value, strempty(paths)); + + return 1; } break; From 305a2850924dd0138f4a5c9fcbd7a71278ef45df Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 3 Mar 2019 02:48:43 +0900 Subject: [PATCH 15/15] systemctl: format LogExtraFields= in 'show' command --- src/systemctl/systemctl.c | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index e54da97b81..444d426eb0 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -44,6 +44,7 @@ #include "install.h" #include "in-addr-util.h" #include "io-util.h" +#include "journal-util.h" #include "list.h" #include "locale-util.h" #include "log.h" @@ -75,6 +76,7 @@ #include "unit-def.h" #include "unit-name.h" #include "user-util.h" +#include "utf8.h" #include "util.h" #include "utmp-wtmp.h" #include "verbs.h" @@ -5227,6 +5229,54 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m bus_print_property_value(name, expected_value, value, strempty(paths)); return 1; + + } else if (streq(name, "LogExtraFields")) { + _cleanup_free_ char *fields = NULL; + const void *p; + size_t sz; + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "ay"); + if (r < 0) + return bus_log_parse_error(r); + + while ((r = sd_bus_message_read_array(m, 'y', &p, &sz)) > 0) { + _cleanup_free_ char *str = NULL; + const char *eq; + + if (memchr(p, 0, sz)) + continue; + + eq = memchr(p, '=', sz); + if (!eq) + continue; + + if (!journal_field_valid(p, eq - (const char*) p, false)) + continue; + + str = malloc(sz + 1); + if (!str) + return log_oom(); + + memcpy(str, p, sz); + str[sz] = '\0'; + + if (!utf8_is_valid(str)) + continue; + + if (!strextend_with_separator(&fields, " ", str, NULL)) + return log_oom(); + } + if (r < 0) + return bus_log_parse_error(r); + + r = sd_bus_message_exit_container(m); + if (r < 0) + return bus_log_parse_error(r); + + if (all || !isempty(fields)) + bus_print_property_value(name, expected_value, value, strempty(fields)); + + return 1; } break;