From ebf30a086dfa526ca048cf14ae16bb403f3dcd12 Mon Sep 17 00:00:00 2001 From: Alexander Kuleshov Date: Tue, 16 Feb 2016 00:01:44 +0600 Subject: time-util: introduce deserialize_timestamp_value() The time-util.c provides dual_timestamp_deserialize() function to convert value to usec_t and set it as value of ts->monotonic and ts->realtime. There are some places in code which do the same but only for one clockid_t (realtime or monotonic), when dual_timestamp_deserialize() sets value of both. This patch introduces the deserialize_timestamp_value() which converts a given value to usec_t and write it to a given timestamp. --- src/basic/time-util.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/basic/time-util.h') diff --git a/src/basic/time-util.h b/src/basic/time-util.h index 9894e626c5..a826ad75ec 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -99,6 +99,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy); void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t); int dual_timestamp_deserialize(const char *value, dual_timestamp *t); +int deserialize_timestamp_value(const char *value, usec_t *timestamp); int parse_timestamp(const char *t, usec_t *usec); -- cgit v1.2.3-54-g00ecf From b895a7353b739e7186f6f51f3a415485b5afd80f Mon Sep 17 00:00:00 2001 From: Benjamin Robin Date: Mon, 15 Feb 2016 23:26:34 +0100 Subject: time-util: Rename and fix call of deserialize_timestamp_value() The deserialize_timestamp_value() is renamed timestamp_deserialize() to be more consistent with dual_timestamp_deserialize() And add the NULL check back on realtime and monotonic --- src/basic/time-util.c | 7 +++---- src/basic/time-util.h | 2 +- src/login/logind-session.c | 6 ++++-- src/login/logind-user.c | 6 ++++-- src/machine/machine.c | 6 ++++-- 5 files changed, 16 insertions(+), 11 deletions(-) (limited to 'src/basic/time-util.h') diff --git a/src/basic/time-util.c b/src/basic/time-util.c index ac5988fdf9..0b4f5ab5b9 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -449,7 +449,7 @@ int dual_timestamp_deserialize(const char *value, dual_timestamp *t) { assert(t); if (sscanf(value, "%llu %llu", &a, &b) != 2) { - log_debug("Failed to parse finish timestamp value %s.", value); + log_debug("Failed to parse dual timestamp value \"%s\": %m", value); return -EINVAL; } @@ -459,15 +459,14 @@ int dual_timestamp_deserialize(const char *value, dual_timestamp *t) { return 0; } -int deserialize_timestamp_value(const char *value, usec_t *timestamp) { +int timestamp_deserialize(const char *value, usec_t *timestamp) { int r; assert(value); r = safe_atou64(value, timestamp); - if (r < 0) - return log_debug_errno(r, "Failed to parse finish timestamp value \"%s\": %m", value); + return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value); return r; } diff --git a/src/basic/time-util.h b/src/basic/time-util.h index a826ad75ec..77e3cd08d4 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -99,7 +99,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy); void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t); int dual_timestamp_deserialize(const char *value, dual_timestamp *t); -int deserialize_timestamp_value(const char *value, usec_t *timestamp); +int timestamp_deserialize(const char *value, usec_t *timestamp); int parse_timestamp(const char *t, usec_t *usec); diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 9874cdae5e..e088225beb 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -446,8 +446,10 @@ int session_load(Session *s) { safe_close(fd); } - deserialize_timestamp_value(realtime, &s->timestamp.realtime); - deserialize_timestamp_value(monotonic, &s->timestamp.monotonic); + if (realtime) + timestamp_deserialize(realtime, &s->timestamp.realtime); + if (monotonic) + timestamp_deserialize(monotonic, &s->timestamp.monotonic); if (controller) { if (bus_name_has_owner(s->manager->bus, controller, NULL) > 0) diff --git a/src/login/logind-user.c b/src/login/logind-user.c index aa27f73a87..a826321bf0 100644 --- a/src/login/logind-user.c +++ b/src/login/logind-user.c @@ -321,8 +321,10 @@ int user_load(User *u) { if (s && s->display && display_is_local(s->display)) u->display = s; - deserialize_timestamp_value(realtime, &u->timestamp.realtime); - deserialize_timestamp_value(monotonic, &u->timestamp.monotonic); + if (realtime) + timestamp_deserialize(realtime, &u->timestamp.realtime); + if (monotonic) + timestamp_deserialize(monotonic, &u->timestamp.monotonic); return r; } diff --git a/src/machine/machine.c b/src/machine/machine.c index 7e92ffc474..7a7a1bb42b 100644 --- a/src/machine/machine.c +++ b/src/machine/machine.c @@ -299,8 +299,10 @@ int machine_load(Machine *m) { m->class = c; } - deserialize_timestamp_value(realtime, &m->timestamp.realtime); - deserialize_timestamp_value(monotonic, &m->timestamp.monotonic); + if (realtime) + timestamp_deserialize(realtime, &m->timestamp.realtime); + if (monotonic) + timestamp_deserialize(monotonic, &m->timestamp.monotonic); if (netif) { size_t allocated = 0, nr = 0; -- cgit v1.2.3-54-g00ecf From 3411372e35fd199d46910b79b8017e9e4ffc8da8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 20 Apr 2016 18:12:57 +0200 Subject: tree-wide: don't assume CLOCK_BOOTIME is generally available Before we invoke now(CLOCK_BOOTTIME), let's make sure we actually have that clock, since now() will otherwise hit an assert. Specifically, let's refuse CLOCK_BOOTTIME early in sd-event if the kernel doesn't actually support it. This is a follow-up for #3037, and specifically: https://github.com/systemd/systemd/pull/3037#issuecomment-210199167 --- src/basic/time-util.c | 37 ++++++++++++++++++++++-------------- src/basic/time-util.h | 1 + src/core/timer.c | 2 +- src/libsystemd/sd-event/sd-event.c | 10 +++++++++- src/libsystemd/sd-event/test-event.c | 12 ++++++++---- 5 files changed, 42 insertions(+), 20 deletions(-) (limited to 'src/basic/time-util.h') diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 7ca764abeb..edd9179cb8 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -1080,22 +1080,31 @@ bool timezone_is_valid(const char *name) { return true; } -clockid_t clock_boottime_or_monotonic(void) { - static clockid_t clock = -1; - int fd; - - if (clock != -1) - return clock; - - fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC); - if (fd < 0) - clock = CLOCK_MONOTONIC; - else { - safe_close(fd); - clock = CLOCK_BOOTTIME; +bool clock_boottime_supported(void) { + static int supported = -1; + + /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */ + + if (supported < 0) { + int fd; + + fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC); + if (fd < 0) + supported = false; + else { + safe_close(fd); + supported = true; + } } - return clock; + return supported; +} + +clockid_t clock_boottime_or_monotonic(void) { + if (clock_boottime_supported()) + return CLOCK_BOOTTIME; + else + return CLOCK_MONOTONIC; } int get_timezone(char **tz) { diff --git a/src/basic/time-util.h b/src/basic/time-util.h index 77e3cd08d4..a5e3f567ec 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -112,6 +112,7 @@ bool ntp_synced(void); int get_timezones(char ***l); bool timezone_is_valid(const char *name); +bool clock_boottime_supported(void); clockid_t clock_boottime_or_monotonic(void); #define xstrftime(buf, fmt, tm) \ diff --git a/src/core/timer.c b/src/core/timer.c index b286b714fa..f8f5f4b2e4 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -373,7 +373,7 @@ static void timer_enter_waiting(Timer *t, bool initial) { * rather than the monotonic clock. */ ts_realtime = now(CLOCK_REALTIME); - ts_monotonic = now(t->wake_system ? CLOCK_BOOTTIME : CLOCK_MONOTONIC); + ts_monotonic = now(t->wake_system ? clock_boottime_or_monotonic() : CLOCK_MONOTONIC); t->next_elapse_monotonic_or_boottime = t->next_elapse_realtime = 0; LIST_FOREACH(value, v, t->values) { diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 79b98c293c..7ba6527f63 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -1072,6 +1072,10 @@ _public_ int sd_event_add_time( assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(!event_pid_changed(e), -ECHILD); + if (IN_SET(clock, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM) && + !clock_boottime_supported()) + return -EOPNOTSUPP; + if (!callback) callback = time_exit_callback; @@ -2527,7 +2531,8 @@ _public_ int sd_event_wait(sd_event *e, uint64_t timeout) { } dual_timestamp_get(&e->timestamp); - e->timestamp_boottime = now(clock_boottime_or_monotonic()); + if (clock_boottime_supported()) + e->timestamp_boottime = now(CLOCK_BOOTTIME); for (i = 0; i < m; i++) { @@ -2761,6 +2766,9 @@ _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) { CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM), -EOPNOTSUPP); + if (IN_SET(clock, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM) && !clock_boottime_supported()) + return -EOPNOTSUPP; + if (!dual_timestamp_is_set(&e->timestamp)) { /* Implicitly fall back to now() if we never ran * before and thus have no cached time. */ diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c index fd31588b8f..289114490c 100644 --- a/src/libsystemd/sd-event/test-event.c +++ b/src/libsystemd/sd-event/test-event.c @@ -270,8 +270,10 @@ static void test_sd_event_now(void) { assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0); assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0); assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0); - assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0); - assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0); + if (clock_boottime_supported()) { + assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0); + assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0); + } assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP); assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP); @@ -280,8 +282,10 @@ static void test_sd_event_now(void) { assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0); assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0); assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0); - assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0); - assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0); + if (clock_boottime_supported()) { + assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0); + assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0); + } assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP); assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP); } -- cgit v1.2.3-54-g00ecf