diff --git a/evtone.c b/evtone.c index e67d160..7f6dc19 100644 --- a/evtone.c +++ b/evtone.c @@ -132,6 +132,7 @@ /* TONE PROCESSING */ #include +#include static void cleanup_fd(int *fd) { if (*fd >= 0) { @@ -149,12 +150,35 @@ void handle_signal(int signal) { got_signal = signal; } +int setup_signals(void) { + struct sigaction action; + action.sa_handler = handle_signal; + action.sa_flags = 0; + if(sigemptyset(&action.sa_mask)) { + fprintf(stderr, "Unable to empty signal mask: %s\n", strerror(errno)); + return 1; + } + if(sigaction(SIGINT, &action, NULL) == -1) { + fprintf(stderr, "Unable to catch SIGINT: %s\n", strerror(errno)); + return 1; + } + if(sigaction(SIGTERM, &action, NULL) == -1) { + fprintf(stderr, "Unable to catch SIGTERM: %s\n", strerror(errno)); + return 1; + } + if(sigaction(SIGPIPE, &action, NULL) == -1) { + fprintf(stderr, "Unable to catch SIGPIPE: %s\n", strerror(errno)); + return 1; + } + return 0; +} + int play_tones(int dry_run, const char *device, struct tone_cmd *tones, int tone_count) { _cleanup_fd_ int dev = open_device(dry_run, device); - signal(SIGINT, handle_signal); - signal(SIGTERM, handle_signal); - signal(SIGPIPE, handle_signal); + if(setup_signals() == -1) { + return 1; + } if (dev == -1) { return 1; } @@ -183,7 +207,6 @@ #include #include #include -#include #include #include