From: Ben Harris Date: Sun, 18 Nov 2018 12:04:59 +0000 (+0000) Subject: Make the fallback timer work. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=b1a7cffc39aedb4a0298e294b8d022424c287d8f;p=clunk.git Make the fallback timer work. It has a null signal handler, which has the side-effect of causing clock_nanosleep to return EINTR. --- diff --git a/clunk.c b/clunk.c index 32d7db7..4d62324 100644 --- 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)"); } }