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
This commit is contained in:
Lennart Poettering
2016-04-22 16:06:20 +02:00
parent f53d86c929
commit 3411372e35
5 changed files with 40 additions and 18 deletions
+21 -12
View File
@@ -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;
bool clock_boottime_supported(void) {
static int supported = -1;
if (clock != -1)
return clock;
/* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
if (fd < 0)
clock = CLOCK_MONOTONIC;
else {
safe_close(fd);
clock = CLOCK_BOOTTIME;
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) {
+1
View File
@@ -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) \