From 2794356649d7d40839cabffbeeee27bbadcc8b7a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 11 Jul 2016 17:22:39 +0200 Subject: Add signal handlers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/gammad.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/gammad.c b/src/gammad.c index 92fbf4d..7e981ad 100644 --- a/src/gammad.c +++ b/src/gammad.c @@ -56,6 +56,44 @@ size_t outputs_n = 0; */ int socketfd = -1; +/** + * Has the process receive a signal + * telling it to re-execute? + */ +volatile sig_atomic_t reexec = 0; + +/** + * Has the process receive a signal + * telling it to terminate? + */ +volatile sig_atomic_t terminate = 0; + + + +/** + * Called when the process receives + * a signal telling it to re-execute + * + * @param signo The received signal + */ +static void sig_reexec(int signo) +{ + reexec = 1; + (void) signo; +} + + +/** + * Called when the process receives + * a signal telling it to terminate + * + * @param signo The received signal + */ +static void sig_terminate(int signo) +{ + terminate = 1; + (void) signo; +} /** @@ -311,6 +349,9 @@ static int is_pidfile_reusable(const char* pidfile, const char* token) /** * Create PID file * + * @signal SIGUSR1 Re-execute to updated process image + * @signal SIGTERM Terminate the process gracefully + * * @param pidfile The pathname of the PID file * @return Zero on success, -1 on error, * -2 if the service is already running @@ -635,10 +676,16 @@ int main(int argc, char** argv) goto fail; } - /* Change directory to / to avoid blocking umounting. */ + /* Change directory to / to avoid blocking umounting */ if (chdir("/") < 0) perror(argv0); + /* Set up signal handlers */ + if (signal(SIGUSR1, sig_reexec) == SIG_ERR) + goto fail; + if (signal(SIGTERM, sig_terminate) == SIG_ERR) + goto fail; + /* Place in the background unless -f */ if (foreground == 0) { -- cgit v1.2.3-70-g09d2