803fb7
From 467145adad739b0426501d2fd7ce1c0afe977e67 Mon Sep 17 00:00:00 2001
803fb7
From: Lukas Nykryn <lnykryn@redhat.com>
803fb7
Date: Thu, 16 Apr 2015 15:22:57 +0200
803fb7
Subject: [PATCH] timedated: fix enable/disable reversal
803fb7
803fb7
The state was flipped later,
803fb7
but the enable/disable routine made use of the state to decide
803fb7
what to do.
803fb7
803fb7
context_enable_ntp() and context_start_ntp() now get the desired
803fb7
state directly, so the Context parameter can be removed.
803fb7
803fb7
(based on 81b843990297ad8c813c531fccd8da30bb715bd6)
803fb7
---
de8967
 src/timedate/timedated.c | 50 +++++++++++++++-------------------------
803fb7
 1 file changed, 18 insertions(+), 32 deletions(-)
803fb7
803fb7
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
803fb7
index c3113b081..f87514936 100644
803fb7
--- a/src/timedate/timedated.c
803fb7
+++ b/src/timedate/timedated.c
803fb7
@@ -280,39 +280,26 @@ static int context_read_ntp(Context *c, sd_bus *bus) {
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
-static int context_start_ntp(Context *c, sd_bus *bus, sd_bus_error *error) {
803fb7
+static int context_start_ntp(sd_bus *bus, sd_bus_error *error, bool enabled) {
803fb7
         _cleanup_strv_free_ char **l = NULL;
803fb7
         char **i;
803fb7
         int r;
803fb7
 
803fb7
-        assert(c);
803fb7
         assert(bus);
803fb7
         assert(error);
803fb7
 
803fb7
         l = get_ntp_services();
803fb7
         STRV_FOREACH(i, l) {
803fb7
 
803fb7
-                if (c->use_ntp)
803fb7
-                        r = sd_bus_call_method(
803fb7
-                                        bus,
803fb7
-                                        "org.freedesktop.systemd1",
803fb7
-                                        "/org/freedesktop/systemd1",
803fb7
-                                        "org.freedesktop.systemd1.Manager",
803fb7
-                                        "StartUnit",
803fb7
-                                        error,
803fb7
-                                        NULL,
803fb7
-                                        "ss", *i, "replace");
803fb7
-                else
803fb7
-                        r = sd_bus_call_method(
803fb7
-                                        bus,
803fb7
-                                        "org.freedesktop.systemd1",
803fb7
-                                        "/org/freedesktop/systemd1",
803fb7
-                                        "org.freedesktop.systemd1.Manager",
803fb7
-                                        "StopUnit",
803fb7
-                                        error,
803fb7
-                                        NULL,
803fb7
-                                        "ss", *i, "replace");
803fb7
-
803fb7
+                r = sd_bus_call_method(
803fb7
+                                bus,
803fb7
+                                "org.freedesktop.systemd1",
803fb7
+                                "/org/freedesktop/systemd1",
803fb7
+                                "org.freedesktop.systemd1.Manager",
803fb7
+                                enabled ? "StartUnit" : "StopUnit",
803fb7
+                                error,
803fb7
+                                NULL,
803fb7
+                                "ss", *i, "replace");
803fb7
                 if (r < 0) {
803fb7
                         if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
803fb7
                             sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
803fb7
@@ -332,18 +319,17 @@ static int context_start_ntp(Context *c, sd_bus *bus, sd_bus_error *error) {
803fb7
         return -ENOTSUP;
803fb7
 }
803fb7
 
803fb7
-static int context_enable_ntp(Context*c, sd_bus *bus, sd_bus_error *error) {
803fb7
+static int context_enable_ntp(sd_bus *bus, sd_bus_error *error, bool enabled) {
803fb7
         _cleanup_strv_free_ char **l = NULL;
803fb7
         char **i;
803fb7
         int r;
803fb7
 
803fb7
-        assert(c);
803fb7
         assert(bus);
803fb7
         assert(error);
803fb7
 
803fb7
         l = get_ntp_services();
803fb7
         STRV_FOREACH(i, l) {
803fb7
-                if (c->use_ntp)
803fb7
+                if (enabled)
803fb7
                         r = sd_bus_call_method(
803fb7
                                         bus,
803fb7
                                         "org.freedesktop.systemd1",
803fb7
@@ -662,15 +648,15 @@ static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
803fb7
 }
803fb7
 
803fb7
 static int method_set_ntp(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
803fb7
-        int ntp, interactive;
803fb7
+        int enabled, interactive;
803fb7
         Context *c = userdata;
803fb7
         int r;
803fb7
 
803fb7
-        r = sd_bus_message_read(m, "bb", &ntp, &interactive);
803fb7
+        r = sd_bus_message_read(m, "bb", &enabled, &interactive);
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
-        if ((bool)ntp == c->use_ntp)
803fb7
+        if ((bool)enabled == c->use_ntp)
803fb7
                 return sd_bus_reply_method_return(m, NULL);
803fb7
 
803fb7
         r = bus_verify_polkit_async(m, CAP_SYS_TIME, "org.freedesktop.timedate1.set-ntp", interactive, &c->polkit_registry, error);
803fb7
@@ -679,15 +665,15 @@ static int method_set_ntp(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus
803fb7
         if (r == 0)
803fb7
                 return 1;
803fb7
 
803fb7
-        r = context_enable_ntp(c, bus, error);
803fb7
+        r = context_enable_ntp(bus, error, enabled);
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
-        r = context_start_ntp(c, bus, error);
803fb7
+        r = context_start_ntp(bus, error, enabled);
803fb7
         if (r < 0)
803fb7
                 return r;
803fb7
 
803fb7
-        c->use_ntp = ntp;
803fb7
+        c->use_ntp = enabled;
803fb7
 
803fb7
         log_info("Set NTP to %s", c->use_ntp ? "enabled" : "disabled");
803fb7