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