From: Ben Harris Date: Sat, 17 Nov 2018 12:33:06 +0000 (+0000) Subject: Block SIGINT and SIGTERM while emitting a pulse. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=7a34f4ee55c5b53a20acb5183ddec6faac3a7fb4;p=clunk.git Block SIGINT and SIGTERM while emitting a pulse. 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. --- diff --git a/clunk.c b/clunk.c index 2ff3b44..70c767e 100644 --- a/clunk.c +++ b/clunk.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -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)