chiark / gitweb /
Create a timer to catch CLOCK_REALTIME going backwards.
authorBen Harris <bjh21@bjh21.me.uk>
Sun, 18 Nov 2018 10:47:36 +0000 (10:47 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sun, 18 Nov 2018 10:47:36 +0000 (10:47 +0000)
Not used yet: I've spotted a couple of other problems to fix first.

clunk.c

diff --git a/clunk.c b/clunk.c
index 70c767ef8e53fc8a0463407b8605deebae4a957a..5a54f61dce387ee1b24b0df96f266fc31e74697b 100644 (file)
--- 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)