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