diff options
author | Lennart Poettering <lennart@poettering.net> | 2016-04-20 18:12:57 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2016-04-22 16:06:20 +0200 |
commit | 3411372e35fd199d46910b79b8017e9e4ffc8da8 (patch) | |
tree | ef9d747d66b0f4df902e4e9ab3b2d9b0379d9efe /src/basic/time-util.c | |
parent | f53d86c9295d9ac20981ca26beb6b83ad1f4296d (diff) |
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
Diffstat (limited to 'src/basic/time-util.c')
-rw-r--r-- | src/basic/time-util.c | 37 |
1 files changed, 23 insertions, 14 deletions
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) { |