/* signals.c -- Signal processing source This file is part of Redshift. Redshift is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Redshift is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Redshift. If not, see . Copyright (c) 2009-2015 Jon Lund Steffensen Copyright (c) 2015 Mattias Andrée */ #include "common.h" #if !defined(__WIN32__) volatile sig_atomic_t exiting = 0; volatile sig_atomic_t disable = 0; /* Signal handler for exit signals */ static void sigexit(int signo) { exiting = 1; } /* Signal handler for disable signal */ static void sigdisable(int signo) { disable = 1; } #else int disable = 0; int exiting = 0; #endif int signals_install_handlers(void) { #ifndef WINDOWS struct sigaction sigact; sigset_t sigset; /* Install signal handler for INT and TERM signals */ memset(&sigact, 0, sizeof(sigact)); sigemptyset(&sigset); sigact.sa_handler = sigexit; sigact.sa_mask = sigset; sigact.sa_flags = 0; if (sigaction(SIGINT, &sigact, NULL)) { perror("sigaction"); return -1; } if (sigaction(SIGTERM, &sigact, NULL)) { perror("sigaction"); return -1; } if (sigaction(SIGQUIT, &sigact, NULL)) { perror("sigaction"); return -1; } /* Install signal handler for USR1 signal */ sigact.sa_handler = sigdisable; sigact.sa_mask = sigset; sigact.sa_flags = 0; if (sigaction(SIGUSR1, &sigact, NULL)) { perror("sigaction"); return -1; } /* Ignore CHLD signal. This causes child processes (hooks) to be reaped automatically. */ sigact.sa_handler = SIG_IGN; sigact.sa_mask = sigset; sigact.sa_flags = 0; if (sigaction(SIGCHLD, &sigact, NULL)) { perror("sigaction"); return -1; } #endif return 0; }