chiark / gitweb /
Block SIGINT and SIGTERM while emitting a pulse.
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 17 Nov 2018 12:33:06 +0000 (12:33 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 17 Nov 2018 12:33:06 +0000 (12:33 +0000)
That should ensure that we emit a full pulse and don't either truncate
it or leave the GPIO line high because of those signals.  SIGTERM will
catch a normal shutdown by systemd etc, SIGINT the manual killing of a
foreground process.

clunk.c

diff --git a/clunk.c b/clunk.c
index 2ff3b4447b19c073e3342a8791db75c249aedbfd..70c767ef8e53fc8a0463407b8605deebae4a957a 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -1,5 +1,6 @@
 #include <err.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <time.h>
@@ -29,6 +30,8 @@ static struct tm displayed;
 
 static int statefd = -1;
 
+static sigset_t signals_to_block;
+
 static void
 dummy_out(bool state)
 {
@@ -73,11 +76,16 @@ static void
 pulse()
 {
        struct timespec const ts = { 0, pulsewidth };
+       sigset_t saved_mask;
 
+       if (sigprocmask(SIG_BLOCK, &signals_to_block, &saved_mask) != 0)
+               err(1, "sigprocmask(block)");
        dummy_out(true);
        record_tick();
        clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
        dummy_out(false);
+       if (sigprocmask(SIG_SETMASK, &saved_mask, NULL) != 0)
+               err(1, "sigprocmask(restore)");
 }
 
 static void
@@ -113,6 +121,9 @@ init(int argc, char **argv)
        int opt;
 
        tzset();
+       sigemptyset(&signals_to_block);
+       sigaddset(&signals_to_block, SIGINT);
+       sigaddset(&signals_to_block, SIGTERM);
        if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
                err (1, "clock_gettime");
        if (localtime_r(&ts.tv_sec, &displayed) == NULL)