chiark / gitweb /
Make the fallback timer work.
authorBen Harris <bjh21@bjh21.me.uk>
Sun, 18 Nov 2018 12:04:59 +0000 (12:04 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sun, 18 Nov 2018 12:04:59 +0000 (12:04 +0000)
It has a null signal handler, which has the side-effect of causing
clock_nanosleep to return EINTR.

clunk.c

diff --git a/clunk.c b/clunk.c
index 32d7db78bacccd29e54662d8ca4bc620addddb85..4d62324a246a05248a22d014471d2fca19f9c204 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -124,11 +124,19 @@ init_statefile(char const *statefilename)
        }
 }
 
+static void
+alrm_handler(int signo)
+{
+
+       /* Do nothing.  The mere presence of this handler is enough. */
+}
+
 static void
 init(int argc, char **argv)
 {
        struct timespec ts;
        struct sigevent sev;
+       struct sigaction sa;
        int opt;
 
        tzset();
@@ -139,6 +147,11 @@ init(int argc, char **argv)
        sev.sigev_signo = SIGALRM;
        if (timer_create(CLOCK_MONOTONIC, &sev, &fallback_timer) != 0)
                err(1, "timer_create");
+       sa.sa_handler = alrm_handler;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_flags = 0;
+       if (sigaction(SIGALRM, &sa, NULL) != 0)
+               err(1, "sigaction");
        if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
                err(1, "clock_gettime");
        if (localtime_r(&ts.tv_sec, &displayed) == NULL)
@@ -186,6 +199,7 @@ static void
 run()
 {
        struct timespec ts;
+       struct itimerspec its;
        struct tm tm;
        int tick;
 
@@ -212,10 +226,19 @@ run()
                /* Choose when next tick will be. */
                ts.tv_nsec = 1000000000 - pulsewidth;
                ts.tv_sec += tick - 1;
+               its.it_value.tv_sec = 35;
+               its.it_value.tv_nsec = 0;
+               its.it_interval.tv_sec = 1;
+               its.it_interval.tv_nsec = 0;
+               if (timer_settime(fallback_timer, 0, &its, NULL) != 0)
+                       err(1, "timer_settime (arm)");
                errno = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME,
                                        &ts, NULL);
-               if (errno != 0)
+               its.it_value.tv_sec = 0;
+               if (errno != 0 && errno != EINTR)
                        err(1, "clock_nanosleep");
+               if (timer_settime(fallback_timer, 0, &its, NULL) != 0)
+                       err(1, "timer_settime (disarm)");
        }
 }