923a60
From 1a3dd33f98312421e0f3d654e8f5d56554557a8c Mon Sep 17 00:00:00 2001
923a60
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
923a60
Date: Fri, 3 Oct 2014 21:34:14 -0400
923a60
Subject: [PATCH] Revert "timedated: manage systemd-timesyncd directly instead
923a60
 of lists of alternatives"
923a60
923a60
This reverts commit b72ddf0f4f552dd53d6404b6ddbc9f17d02b8e12.
923a60
923a60
Conflicts:
923a60
	Makefile.am
923a60
	NEWS
923a60
	src/timedate/timedated.c
923a60
---
923a60
 Makefile.am              |   9 ++
923a60
 src/timedate/timedated.c | 252 +++++++++++++++++++++++++--------------
923a60
 2 files changed, 170 insertions(+), 91 deletions(-)
923a60
923a60
diff --git a/Makefile.am b/Makefile.am
923a60
index 9e64d6f988..bf65b24060 100644
923a60
--- a/Makefile.am
923a60
+++ b/Makefile.am
923a60
@@ -111,6 +111,7 @@ catalogdir=$(prefix)/lib/systemd/catalog
923a60
 kernelinstalldir = $(prefix)/lib/kernel/install.d
923a60
 factory_etcdir = $(prefix)/share/factory/etc
923a60
 factory_pamdir = $(prefix)/share/factory/etc/pam.d
923a60
+ntpunitsdir=$(prefix)/lib/systemd/ntp-units.d
923a60
 
923a60
 # And these are the special ones for /
923a60
 rootprefix=@rootprefix@
923a60
@@ -5101,6 +5102,10 @@ dist_systemunit_DATA_busnames += \
923a60
 polkitpolicy_files += \
923a60
 	src/timedate/org.freedesktop.timedate1.policy
923a60
 
923a60
+INSTALL_DIRS += \
923a60
+	$(prefix)/lib/systemd/ntp-units.d \
923a60
+	$(sysconfdir)/systemd/ntp-units.d
923a60
+
923a60
 SYSTEM_UNIT_ALIASES += \
923a60
 	systemd-timedated.service dbus-org.freedesktop.timedate1.service
923a60
 
923a60
@@ -5177,6 +5182,10 @@ EXTRA_DIST += \
923a60
 
923a60
 CLEANFILES += \
923a60
 	src/timesync/timesyncd.conf
923a60
+
923a60
+dist_ntpunits_DATA = \
923a60
+	src/timesync/90-systemd.list
923a60
+
923a60
 endif
923a60
 
923a60
 # ------------------------------------------------------------------------------
923a60
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
923a60
index 753c3d1d65..66097ef741 100644
923a60
--- a/src/timedate/timedated.c
923a60
+++ b/src/timedate/timedated.c
923a60
@@ -186,141 +186,211 @@ static int context_write_data_local_rtc(Context *c) {
923a60
         return write_string_file_atomic_label("/etc/adjtime", w);
923a60
 }
923a60
 
923a60
+static char** get_ntp_services(void) {
923a60
+        _cleanup_strv_free_ char **r = NULL, **files = NULL;
923a60
+        char **i;
923a60
+        int k;
923a60
+
923a60
+        k = conf_files_list(&files, ".list", NULL,
923a60
+                            "/etc/systemd/ntp-units.d",
923a60
+                            "/run/systemd/ntp-units.d",
923a60
+                            "/usr/local/lib/systemd/ntp-units.d",
923a60
+                            "/usr/lib/systemd/ntp-units.d",
923a60
+                            NULL);
923a60
+        if (k < 0)
923a60
+                return NULL;
923a60
+
923a60
+        STRV_FOREACH(i, files) {
923a60
+                _cleanup_fclose_ FILE *f;
923a60
+
923a60
+                f = fopen(*i, "re");
923a60
+                if (!f)
923a60
+                        continue;
923a60
+
923a60
+                for (;;) {
923a60
+                        char line[PATH_MAX], *l;
923a60
+
923a60
+                        if (!fgets(line, sizeof(line), f)) {
923a60
+                                if (ferror(f))
923a60
+                                        log_error("Failed to read NTP unit file: %m");
923a60
+
923a60
+                                break;
923a60
+                        }
923a60
+
923a60
+                        l = strstrip(line);
923a60
+                        if (l[0] == 0 || l[0] == '#')
923a60
+                                continue;
923a60
+
923a60
+                        if (strv_extend(&r, l) < 0) {
923a60
+                                log_oom();
923a60
+                                return NULL;
923a60
+                        }
923a60
+                }
923a60
+        }
923a60
+
923a60
+        i = r;
923a60
+        r = NULL; /* avoid cleanup */
923a60
+
923a60
+        return strv_uniq(i);
923a60
+}
923a60
+
923a60
 static int context_read_ntp(Context *c, sd_bus *bus) {
923a60
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
923a60
-        sd_bus_message *reply = NULL;
923a60
-        const char *s;
923a60
+        _cleanup_strv_free_ char **l;
923a60
+        char **i;
923a60
         int r;
923a60
 
923a60
         assert(c);
923a60
         assert(bus);
923a60
 
923a60
-        r = sd_bus_call_method(
923a60
-                        bus,
923a60
-                        "org.freedesktop.systemd1",
923a60
-                        "/org/freedesktop/systemd1",
923a60
-                        "org.freedesktop.systemd1.Manager",
923a60
-                        "GetUnitFileState",
923a60
-                        &error,
923a60
-                        &reply,
923a60
-                        "s",
923a60
-                        "systemd-timesyncd.service");
923a60
+        l = get_ntp_services();
923a60
+        STRV_FOREACH(i, l) {
923a60
+                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
923a60
+                sd_bus_message *reply = NULL;
923a60
+                const char *s;
923a60
 
923a60
-        if (r < 0) {
923a60
-                if (sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
923a60
-                    sd_bus_error_has_name(&error, "org.freedesktop.systemd1.LoadFailed") ||
923a60
-                    sd_bus_error_has_name(&error, "org.freedesktop.systemd1.NoSuchUnit"))
923a60
-                        return 0;
923a60
+                r = sd_bus_call_method(
923a60
+                                bus,
923a60
+                                "org.freedesktop.systemd1",
923a60
+                                "/org/freedesktop/systemd1",
923a60
+                                "org.freedesktop.systemd1.Manager",
923a60
+                                "GetUnitFileState",
923a60
+                                &error,
923a60
+                                &reply,
923a60
+                                "s",
923a60
+                                *i);
923a60
 
923a60
-                return r;
923a60
-        }
923a60
+                if (r < 0) {
923a60
+                        /* This implementation does not exist. Try the next one. */
923a60
+                        if (sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND))
923a60
+                                continue;
923a60
 
923a60
-        r = sd_bus_message_read(reply, "s", &s);
923a60
-        if (r < 0)
923a60
-                return r;
923a60
+                        return r;
923a60
+                }
923a60
+
923a60
+                r = sd_bus_message_read(reply, "s", &s);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
 
923a60
-        c->can_ntp = true;
923a60
-        c->use_ntp = STR_IN_SET(s, "enabled", "enabled-runtime");
923a60
+                c->can_ntp = true;
923a60
+                c->use_ntp = STR_IN_SET(s, "enabled", "enabled-runtime");
923a60
+
923a60
+                return 0;
923a60
+        }
923a60
 
923a60
         return 0;
923a60
 }
923a60
 
923a60
 static int context_start_ntp(Context *c, sd_bus *bus, sd_bus_error *error) {
923a60
+        _cleanup_strv_free_ char **l = NULL;
923a60
+        char **i;
923a60
         int r;
923a60
 
923a60
         assert(c);
923a60
         assert(bus);
923a60
         assert(error);
923a60
 
923a60
-        if (c->use_ntp)
923a60
-                r = sd_bus_call_method(
923a60
-                                bus,
923a60
-                                "org.freedesktop.systemd1",
923a60
-                                "/org/freedesktop/systemd1",
923a60
-                                "org.freedesktop.systemd1.Manager",
923a60
-                                "StartUnit",
923a60
-                                error,
923a60
-                                NULL,
923a60
-                                "ss",
923a60
-                                "systemd-timesyncd.service",
923a60
-                                "replace");
923a60
-        else
923a60
-                r = sd_bus_call_method(
923a60
-                                bus,
923a60
-                                "org.freedesktop.systemd1",
923a60
-                                "/org/freedesktop/systemd1",
923a60
-                                "org.freedesktop.systemd1.Manager",
923a60
-                                "StopUnit",
923a60
-                                error,
923a60
-                                NULL,
923a60
-                                "ss",
923a60
-                                "systemd-timesyncd.service",
923a60
-                                "replace");
923a60
+        l = get_ntp_services();
923a60
+        STRV_FOREACH(i, l) {
923a60
+
923a60
+                if (c->use_ntp)
923a60
+                        r = sd_bus_call_method(
923a60
+                                        bus,
923a60
+                                        "org.freedesktop.systemd1",
923a60
+                                        "/org/freedesktop/systemd1",
923a60
+                                        "org.freedesktop.systemd1.Manager",
923a60
+                                        "StartUnit",
923a60
+                                        error,
923a60
+                                        NULL,
923a60
+                                        "ss", *i, "replace");
923a60
+                else
923a60
+                        r = sd_bus_call_method(
923a60
+                                        bus,
923a60
+                                        "org.freedesktop.systemd1",
923a60
+                                        "/org/freedesktop/systemd1",
923a60
+                                        "org.freedesktop.systemd1.Manager",
923a60
+                                        "StopUnit",
923a60
+                                        error,
923a60
+                                        NULL,
923a60
+                                        "ss", *i, "replace");
923a60
+
923a60
+                if (r < 0) {
923a60
+                        if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
923a60
+                            sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
923a60
+                            sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit")) {
923a60
+                                /* This implementation does not exist. Try the next one. */
923a60
+                                sd_bus_error_free(error);
923a60
+                                continue;
923a60
+                        }
923a60
 
923a60
-        if (r < 0) {
923a60
-                if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
923a60
-                    sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
923a60
-                    sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit"))
923a60
-                        return sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
923a60
+                        return r;
923a60
+                }
923a60
 
923a60
-                return r;
923a60
+                return 1;
923a60
         }
923a60
 
923a60
-        return 0;
923a60
+        sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
923a60
+        return -ENOTSUP;
923a60
 }
923a60
 
923a60
 static int context_enable_ntp(Context*c, sd_bus *bus, sd_bus_error *error) {
923a60
+        _cleanup_strv_free_ char **l = NULL;
923a60
+        char **i;
923a60
         int r;
923a60
 
923a60
         assert(c);
923a60
         assert(bus);
923a60
         assert(error);
923a60
 
923a60
-        if (c->use_ntp)
923a60
-                r = sd_bus_call_method(
923a60
-                                bus,
923a60
-                                "org.freedesktop.systemd1",
923a60
-                                "/org/freedesktop/systemd1",
923a60
-                                "org.freedesktop.systemd1.Manager",
923a60
-                                "EnableUnitFiles",
923a60
-                                error,
923a60
-                                NULL,
923a60
-                                "asbb", 1,
923a60
-                                "systemd-timesyncd.service",
923a60
-                                false, true);
923a60
-        else
923a60
+        l = get_ntp_services();
923a60
+        STRV_FOREACH(i, l) {
923a60
+                if (c->use_ntp)
923a60
+                        r = sd_bus_call_method(
923a60
+                                        bus,
923a60
+                                        "org.freedesktop.systemd1",
923a60
+                                        "/org/freedesktop/systemd1",
923a60
+                                        "org.freedesktop.systemd1.Manager",
923a60
+                                        "EnableUnitFiles",
923a60
+                                        error,
923a60
+                                        NULL,
923a60
+                                        "asbb", 1, *i, false, true);
923a60
+                else
923a60
+                        r = sd_bus_call_method(
923a60
+                                        bus,
923a60
+                                        "org.freedesktop.systemd1",
923a60
+                                        "/org/freedesktop/systemd1",
923a60
+                                        "org.freedesktop.systemd1.Manager",
923a60
+                                        "DisableUnitFiles",
923a60
+                                        error,
923a60
+                                        NULL,
923a60
+                                        "asb", 1, *i, false);
923a60
+
923a60
+                if (r < 0) {
923a60
+                        if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND)) {
923a60
+                                /* This implementation does not exist. Try the next one. */
923a60
+                                sd_bus_error_free(error);
923a60
+                                continue;
923a60
+                        }
923a60
+
923a60
+                        return r;
923a60
+                }
923a60
+
923a60
                 r = sd_bus_call_method(
923a60
                                 bus,
923a60
                                 "org.freedesktop.systemd1",
923a60
                                 "/org/freedesktop/systemd1",
923a60
                                 "org.freedesktop.systemd1.Manager",
923a60
-                                "DisableUnitFiles",
923a60
+                                "Reload",
923a60
                                 error,
923a60
                                 NULL,
923a60
-                                "asb", 1,
923a60
-                                "systemd-timesyncd.service",
923a60
-                                false);
923a60
-
923a60
-        if (r < 0) {
923a60
-                if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND))
923a60
-                        return sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
923a60
+                                NULL);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
 
923a60
-                return r;
923a60
+                return 1;
923a60
         }
923a60
 
923a60
-        r = sd_bus_call_method(
923a60
-                        bus,
923a60
-                        "org.freedesktop.systemd1",
923a60
-                        "/org/freedesktop/systemd1",
923a60
-                        "org.freedesktop.systemd1.Manager",
923a60
-                        "Reload",
923a60
-                        error,
923a60
-                        NULL,
923a60
-                        NULL);
923a60
-        if (r < 0)
923a60
-                return r;
923a60
-
923a60
-        return 0;
923a60
+        sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
923a60
+        return -ENOTSUP;
923a60
 }
923a60
 
923a60
 static int property_get_rtc_time(