summaryrefslogtreecommitdiffstats
path: root/src/systemctl
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2021-09-07 14:35:49 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-09-10 01:48:57 +0200
commita87b151ac048f5a7c77e83e748f544675c2df0cc (patch)
tree58ecfb5b585c2ccf7427ea6fc535d1d4734d519e /src/systemctl
parentMerge pull request #20693 from mcatanzaro/mcatanzaro/nss-buffers (diff)
downloadsystemd-a87b151ac048f5a7c77e83e748f544675c2df0cc.tar.xz
systemd-a87b151ac048f5a7c77e83e748f544675c2df0cc.zip
shared: Extract common LogControl CLI code to verb-log-control.h
Let's reduce duplication by sharing common logic between all log-target/log-level verbs.
Diffstat (limited to 'src/systemctl')
-rw-r--r--src/systemctl/systemctl-log-setting.c62
1 files changed, 7 insertions, 55 deletions
diff --git a/src/systemctl/systemctl-log-setting.c b/src/systemctl/systemctl-log-setting.c
index 9a95c7dab4..e3e9576471 100644
--- a/src/systemctl/systemctl-log-setting.c
+++ b/src/systemctl/systemctl-log-setting.c
@@ -7,6 +7,7 @@
#include "systemctl-log-setting.h"
#include "systemctl-util.h"
#include "systemctl.h"
+#include "verb-log-control.h"
static void give_log_control1_hint(const char *name) {
_cleanup_free_ char *link = NULL;
@@ -20,54 +21,6 @@ static void give_log_control1_hint(const char *name) {
" See the %s for details.", link ?: "org.freedesktop.LogControl1(5) man page");
}
-static int log_setting_internal(sd_bus *bus, const BusLocator* bloc, const char *verb, const char *value) {
- assert(bus);
- assert(STR_IN_SET(verb, "log-level", "log-target", "service-log-level", "service-log-target"));
-
- _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
- bool level = endswith(verb, "log-level");
- int r;
-
- if (value) {
- if (level) {
- r = log_level_from_string(value);
- if (r < 0)
- return log_error_errno(r, "\"%s\" is not a valid log level.", value);
- }
-
- r = bus_set_property(bus, bloc,
- level ? "LogLevel" : "LogTarget",
- &error, "s", value);
- if (r >= 0)
- return 0;
-
- log_error_errno(r, "Failed to set log %s of %s to %s: %s",
- level ? "level" : "target",
- bloc->destination, value, bus_error_message(&error, r));
- } else {
- _cleanup_free_ char *t = NULL;
-
- r = bus_get_property_string(bus, bloc,
- level ? "LogLevel" : "LogTarget",
- &error, &t);
- if (r >= 0) {
- puts(t);
- return 0;
- }
-
- log_error_errno(r, "Failed to get log %s of %s: %s",
- level ? "level" : "target",
- bloc->destination, bus_error_message(&error, r));
- }
-
- if (sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_METHOD,
- SD_BUS_ERROR_UNKNOWN_OBJECT,
- SD_BUS_ERROR_UNKNOWN_INTERFACE,
- SD_BUS_ERROR_UNKNOWN_PROPERTY))
- give_log_control1_hint(bloc->destination);
- return r;
-}
-
int log_setting(int argc, char *argv[], void *userdata) {
sd_bus *bus;
int r;
@@ -78,7 +31,7 @@ int log_setting(int argc, char *argv[], void *userdata) {
if (r < 0)
return r;
- return log_setting_internal(bus, bus_systemd_mgr, argv[0], argv[1]);
+ return verb_log_control_common(bus, "org.freedesktop.systemd1", argv[0], argv[1]);
}
static int service_name_to_dbus(sd_bus *bus, const char *name, char **ret_dbus_name) {
@@ -134,11 +87,10 @@ int service_log_setting(int argc, char *argv[], void *userdata) {
if (r < 0)
return r;
- const BusLocator bloc = {
- .destination = dbus_name,
- .path = "/org/freedesktop/LogControl1",
- .interface = "org.freedesktop.LogControl1",
- };
+ r = verb_log_control_common(bus, dbus_name, argv[0], argv[2]);
- return log_setting_internal(bus, &bloc, argv[0], argv[2]);
+ if (r == -EBADR)
+ give_log_control1_hint(dbus_name);
+
+ return r;
}