]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dbus-kill: simplify bus_kill_context_set_transient_property()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 29 Dec 2017 08:07:44 +0000 (17:07 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 1 Jan 2018 17:23:36 +0000 (02:23 +0900)
src/core/dbus-kill.c

index bf3bbb2047567f936d2f285d1fa03314761dc449..53d0aad63b153c9fc52f1ae4293ae10af3ab4ab0 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "bus-util.h"
 #include "dbus-kill.h"
+#include "dbus-util.h"
 #include "kill.h"
 #include "signal-util.h"
 
@@ -34,6 +35,9 @@ const sd_bus_vtable bus_kill_vtable[] = {
         SD_BUS_VTABLE_END
 };
 
+static BUS_DEFINE_SET_TRANSIENT_PARSE(kill_mode, KillMode, kill_mode_from_string);
+static BUS_DEFINE_SET_TRANSIENT_TO_STRING(kill_signal, "i", int32_t, int, "%" PRIi32, signal_to_string_with_check);
+
 int bus_kill_context_set_transient_property(
                 Unit *u,
                 KillContext *c,
@@ -42,8 +46,6 @@ int bus_kill_context_set_transient_property(
                 UnitWriteFlags flags,
                 sd_bus_error *error) {
 
-        int r;
-
         assert(u);
         assert(c);
         assert(name);
@@ -51,75 +53,17 @@ int bus_kill_context_set_transient_property(
 
         flags |= UNIT_PRIVATE;
 
-        if (streq(name, "KillMode")) {
-                const char *m;
-                KillMode k;
-
-                r = sd_bus_message_read(message, "s", &m);
-                if (r < 0)
-                        return r;
-
-                k = kill_mode_from_string(m);
-                if (k < 0)
-                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Kill mode '%s' not known.", m);
-
-                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
-                        c->kill_mode = k;
-
-                        unit_write_settingf(u, flags, name, "KillMode=%s", kill_mode_to_string(k));
-                }
-
-                return 1;
-
-        } else if (streq(name, "KillSignal")) {
-                int sig;
-
-                r = sd_bus_message_read(message, "i", &sig);
-                if (r < 0)
-                        return r;
-
-                if (!SIGNAL_VALID(sig))
-                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal %i out of range", sig);
-
-                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
-                        c->kill_signal = sig;
-
-                        unit_write_settingf(u, flags, name, "KillSignal=%s", signal_to_string(sig));
-                }
-
-                return 1;
-
-        } else if (streq(name, "SendSIGHUP")) {
-                int b;
-
-                r = sd_bus_message_read(message, "b", &b);
-                if (r < 0)
-                        return r;
-
-                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
-                        c->send_sighup = b;
-
-                        unit_write_settingf(u, flags, name, "SendSIGHUP=%s", yes_no(b));
-                }
-
-                return 1;
-
-        } else if (streq(name, "SendSIGKILL")) {
-                int b;
-
-                r = sd_bus_message_read(message, "b", &b);
-                if (r < 0)
-                        return r;
-
-                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
-                        c->send_sigkill = b;
+        if (streq(name, "KillMode"))
+                return bus_set_transient_kill_mode(u, name, &c->kill_mode, message, flags, error);
 
-                        unit_write_settingf(u, flags, name, "SendSIGKILL=%s", yes_no(b));
-                }
+        if (streq(name, "SendSIGHUP"))
+                return bus_set_transient_bool(u, name, &c->send_sighup, message, flags, error);
 
-                return 1;
+        if (streq(name, "SendSIGKILL"))
+                return bus_set_transient_bool(u, name, &c->send_sigkill, message, flags, error);
 
-        }
+        if (streq(name, "KillSignal"))
+                return bus_set_transient_kill_signal(u, name, &c->kill_signal, message, flags, error);
 
         return 0;
 }