From ee48214209e66aa8c6f353262b175895373f76df Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Sat, 4 Jan 2020 21:31:55 +0100 Subject: ... --- src/server/interruption.c | 69 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 3 deletions(-) (limited to 'src/server/interruption.c') diff --git a/src/server/interruption.c b/src/server/interruption.c index 6eb7827..32cda53 100644 --- a/src/server/interruption.c +++ b/src/server/interruption.c @@ -1,23 +1,86 @@ +/**** POSIX *******************************************************************/ +#include +#include +#include +#include + +/**** RELABSD *****************************************************************/ +#include + +/******************************************************************************/ +/**** LOCAL FUNCTIONS *********************************************************/ +/******************************************************************************/ +static int RELABSD_INTERRUPTION_PIPES[2]; static int RELABSD_RUN = 1; -static void interrupt (int unused_mandatory_parameter) +static void interrupt (int unused_mandatory_parameter __attribute__((unused))) { RELABSD_RUN = 0; - RELABSD_S_WARNING("Interrupted, will exit at the next input device event."); + errno = 0; + + if (write(RELABSD_INTERRUPTION_PIPES[0], (void *) "!", (size_t) 1) == -1) + { + RELABSD_ERROR + ( + "Unable to signal the interruption to the conversion thread." + " Interruption should still occur following the next input from the" + " physical device. Error: %s.", + strerror(errno) + ); + } +} + +/******************************************************************************/ +/**** EXPORTED FUNCTIONS ******************************************************/ +/******************************************************************************/ +int relabsd_server_keep_running (void) +{ + return RELABSD_RUN; +} + +void relabsd_server_interrupt (void) +{ + interrupt(0); } -static int set_signal_handlers () +int relabsd_server_initialize_signal_handlers (void) { RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Setting signal handlers."); + errno = 0; + + if (pipe(RELABSD_INTERRUPTION_PIPES) == -1) + { + RELABSD_FATAL + ( + "Unable to create an unnamed pipe for the interruption handling: %s", + strerror(errno) + ); + + return -1; + } + if (signal(SIGINT, interrupt) == SIG_ERR) { RELABSD_S_FATAL("Unable to set the SIGINT signal handler."); + (void) close(RELABSD_INTERRUPTION_PIPES[0]); + (void) close(RELABSD_INTERRUPTION_PIPES[1]); + return -1; } return 0; } +void relabsd_server_finalize_signal_handlers (void) +{ + (void) close(RELABSD_INTERRUPTION_PIPES[0]); + (void) close(RELABSD_INTERRUPTION_PIPES[1]); +} + +int relabsd_server_get_interruption_file_descriptor (void) +{ + return RELABSD_INTERRUPTION_PIPES[1]; +} -- cgit v1.2.3-70-g09d2