ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
Zbigniew Jędrzejewski-Szmek 2b6823
From d15dd311752fcafa92d2e6a00c582020dc0f6ac3 Mon Sep 17 00:00:00 2001
Jan Synacek 92af61
From: Lennart Poettering <lennart@poettering.net>
Jan Synacek 92af61
Date: Fri, 12 Feb 2016 21:29:01 +0100
Jan Synacek 92af61
Subject: [PATCH] time-util: map ALARM clockids to non-ALARM clockids in now()
Jan Synacek 92af61
Jan Synacek 92af61
Fixes: #2597
Jan Synacek 92af61
(cherry picked from commit 32c1f5a57998f2a9e1992af006b83e39e3155830)
Jan Synacek 92af61
---
Jan Synacek 92af61
 src/basic/time-util.c              | 24 ++++++++++++++++++++++--
Jan Synacek 92af61
 src/libsystemd/sd-event/sd-event.c |  6 +++++-
Jan Synacek 92af61
 2 files changed, 27 insertions(+), 3 deletions(-)
Jan Synacek 92af61
Jan Synacek 92af61
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
Zbigniew Jędrzejewski-Szmek 2b6823
index 3973850b44..510f018d9b 100644
Jan Synacek 92af61
--- a/src/basic/time-util.c
Jan Synacek 92af61
+++ b/src/basic/time-util.c
Jan Synacek 92af61
@@ -42,10 +42,30 @@
Jan Synacek 92af61
 
Jan Synacek 92af61
 static nsec_t timespec_load_nsec(const struct timespec *ts);
Jan Synacek 92af61
 
Jan Synacek 92af61
+static clockid_t map_clock_id(clockid_t c) {
Jan Synacek 92af61
+
Jan Synacek 92af61
+        /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
Jan Synacek 92af61
+         * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
Jan Synacek 92af61
+         * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
Jan Synacek 92af61
+         * those archs. */
Jan Synacek 92af61
+
Jan Synacek 92af61
+        switch (c) {
Jan Synacek 92af61
+
Jan Synacek 92af61
+        case CLOCK_BOOTTIME_ALARM:
Jan Synacek 92af61
+                return CLOCK_BOOTTIME;
Jan Synacek 92af61
+
Jan Synacek 92af61
+        case CLOCK_REALTIME_ALARM:
Jan Synacek 92af61
+                return CLOCK_REALTIME;
Jan Synacek 92af61
+
Jan Synacek 92af61
+        default:
Jan Synacek 92af61
+                return c;
Jan Synacek 92af61
+        }
Jan Synacek 92af61
+}
Jan Synacek 92af61
+
Jan Synacek 92af61
 usec_t now(clockid_t clock_id) {
Jan Synacek 92af61
         struct timespec ts;
Jan Synacek 92af61
 
Jan Synacek 92af61
-        assert_se(clock_gettime(clock_id, &ts) == 0);
Jan Synacek 92af61
+        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
Jan Synacek 92af61
 
Jan Synacek 92af61
         return timespec_load(&ts);
Jan Synacek 92af61
 }
Jan Synacek 92af61
@@ -53,7 +73,7 @@ usec_t now(clockid_t clock_id) {
Jan Synacek 92af61
 nsec_t now_nsec(clockid_t clock_id) {
Jan Synacek 92af61
         struct timespec ts;
Jan Synacek 92af61
 
Jan Synacek 92af61
-        assert_se(clock_gettime(clock_id, &ts) == 0);
Jan Synacek 92af61
+        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
Jan Synacek 92af61
 
Jan Synacek 92af61
         return timespec_load_nsec(&ts);
Jan Synacek 92af61
 }
Jan Synacek 92af61
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
Zbigniew Jędrzejewski-Szmek 2b6823
index deef6ba9d3..2b46a1ff06 100644
Jan Synacek 92af61
--- a/src/libsystemd/sd-event/sd-event.c
Jan Synacek 92af61
+++ b/src/libsystemd/sd-event/sd-event.c
Jan Synacek 92af61
@@ -2780,9 +2780,13 @@ _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
Jan Synacek 92af61
                 *usec = e->timestamp.monotonic;
Jan Synacek 92af61
                 break;
Jan Synacek 92af61
 
Jan Synacek 92af61
-        default:
Jan Synacek 92af61
+        case CLOCK_BOOTTIME:
Jan Synacek 92af61
+        case CLOCK_BOOTTIME_ALARM:
Jan Synacek 92af61
                 *usec = e->timestamp_boottime;
Jan Synacek 92af61
                 break;
Jan Synacek 92af61
+
Jan Synacek 92af61
+        default:
Jan Synacek 92af61
+                assert_not_reached("Unknown clock?");
Jan Synacek 92af61
         }
Jan Synacek 92af61
 
Jan Synacek 92af61
         return 0;