From cc9d637652875020e1a26874f15ff362938aa9e0 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Sun, 16 Jul 2017 17:34:05 +0200 Subject: Signal handling, code improvements. --- src/signal.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/signal.c (limited to 'src/signal.c') diff --git a/src/signal.c b/src/signal.c new file mode 100644 index 0000000..eb9584e --- /dev/null +++ b/src/signal.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +#include "error/error.h" + +#include "pervasive.h" + +#include "./cli.h" + +static volatile char JH_GATEWAY_IS_RUNNING = (char) 1; + +static void request_termination (int const signo) +{ + if ((signo == SIGINT) || (signo == SIGTERM)) + { + JH_GATEWAY_IS_RUNNING = (char) 0; + } +} + +int JH_cli_is_running (void) +{ + return (int) JH_GATEWAY_IS_RUNNING; +} + +int JH_cli_set_signal_handlers (void) +{ + struct sigaction act; + const int old_errno = errno; + + memset((void *) &act, 0, sizeof(struct sigaction)); + + act.sa_handler = request_termination; + + errno = 0; + + if (sigaction(SIGHUP, &act, (struct sigaction * restrict) NULL) == -1) + { + JH_FATAL + ( + stderr, + "Could not set sigaction for SIGHUP (errno: %d): %s", + errno, + strerror(errno) + ); + + errno = old_errno; + + return -1; + } + + errno = 0; + + if (sigaction(SIGINT, &act, (struct sigaction * restrict) NULL) == -1) + { + JH_FATAL + ( + stderr, + "Could not set sigaction for SIGINT (errno: %d): %s", + errno, + strerror(errno) + ); + + errno = old_errno; + + return -1; + } + + act.sa_handler = SIG_IGN; + + if (sigaction(SIGPIPE, &act, (struct sigaction * restrict) NULL) == -1) + { + JH_FATAL + ( + stderr, + "Could not set sigaction for SIGPIPE (errno: %d): %s", + errno, + strerror(errno) + ); + + errno = old_errno; + + return -1; + } + + errno = old_errno; + + return 0; +} -- cgit v1.2.3-70-g09d2