From: Ben Harris Date: Sun, 18 Nov 2018 10:47:36 +0000 (+0000) Subject: Create a timer to catch CLOCK_REALTIME going backwards. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=48352981d095de1592069147d4f2c9f546d313aa;p=clunk.git Create a timer to catch CLOCK_REALTIME going backwards. Not used yet: I've spotted a couple of other problems to fix first. --- diff --git a/clunk.c b/clunk.c index 70c767e..5a54f61 100644 --- a/clunk.c +++ b/clunk.c @@ -32,6 +32,13 @@ static int statefd = -1; static sigset_t signals_to_block; +/* + * The fallback timer protects us from occasions when CLOCK_REALTIME + * goes backwards so our nice absolute clock_nanosleep() end up + * sleeping far too long. + */ +static timer_t fallback_timer; + static void dummy_out(bool state) { @@ -118,12 +125,17 @@ static void init(int argc, char **argv) { struct timespec ts; + struct sigevent sev; int opt; tzset(); sigemptyset(&signals_to_block); sigaddset(&signals_to_block, SIGINT); sigaddset(&signals_to_block, SIGTERM); + sev.sigev_notify = SIGEV_SIGNAL; + sev.sigev_signo = SIGALRM; + if (timer_create(CLOCK_MONOTONIC, &sev, &fallback_timer) != 0) + err(1, "timer_create"); if (clock_gettime(CLOCK_REALTIME, &ts) != 0) err (1, "clock_gettime"); if (localtime_r(&ts.tv_sec, &displayed) == NULL)