diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2018-05-13 22:27:27 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2018-05-13 22:29:05 +0200 |
commit | 749644943b3f11fca7c70414e32905136ea9c7cd (patch) | |
tree | eeb884881730fb4ba97ee2e55e1cf00defb463d6 /src/blogc-make/reloader.c | |
parent | 321459e9584e4f9b6c725f1e19effef693e3c561 (diff) | |
download | blogc-749644943b3f11fca7c70414e32905136ea9c7cd.tar.gz blogc-749644943b3f11fca7c70414e32905136ea9c7cd.tar.bz2 blogc-749644943b3f11fca7c70414e32905136ea9c7cd.zip |
make: simplified signal logic
Diffstat (limited to 'src/blogc-make/reloader.c')
-rw-r--r-- | src/blogc-make/reloader.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/blogc-make/reloader.c b/src/blogc-make/reloader.c index a010b51..7843a48 100644 --- a/src/blogc-make/reloader.c +++ b/src/blogc-make/reloader.c @@ -32,9 +32,15 @@ static pthread_mutex_t mutex_running = PTHREAD_MUTEX_INITIALIZER; static bool running = false; -static int handler_signum = 0; +static int reloader_status_code = 0; static void (*handler_func)(int) = NULL; +static void +sig_handler(int signum) +{ + bm_reloader_stop(signum + 128); +} + int bm_reloader_run(bm_ctx_t **ctx, bm_rule_exec_func_t rule_exec, @@ -46,7 +52,7 @@ bm_reloader_run(bm_ctx_t **ctx, bm_rule_exec_func_t rule_exec, fprintf(stderr, "blogc-make: failed to run reloader: %s\n", strerror(errno)); return 3; } - if (current_action.sa_handler != bm_reloader_stop) { // not installed yet + if (current_action.sa_handler != sig_handler) { // not installed yet // backup current handler pthread_mutex_lock(&mutex_running); handler_func = current_action.sa_handler; @@ -54,7 +60,7 @@ bm_reloader_run(bm_ctx_t **ctx, bm_rule_exec_func_t rule_exec, // set new handler struct sigaction new_action; - new_action.sa_handler = bm_reloader_stop; + new_action.sa_handler = sig_handler; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if (sigaction(SIGINT, &new_action, NULL) < 0) { @@ -84,23 +90,20 @@ bm_reloader_run(bm_ctx_t **ctx, bm_rule_exec_func_t rule_exec, sleep(1); } - if (handler_signum > 0 && handler_signum <= SIGRTMAX) - return 128 + handler_signum; - - return handler_signum; + return reloader_status_code; } void -bm_reloader_stop(int signum) +bm_reloader_stop(int status_code) { pthread_mutex_lock(&mutex_running); - handler_signum = signum > 128 ? signum - 128 : signum; running = false; + reloader_status_code = status_code; // reraise if SIGINT - if (handler_signum == SIGINT) { + if (status_code == SIGINT + 128) { // reinstall old ^C handler struct sigaction new_action; |