diff --git a/pclsync/psignal.c b/pclsync/psignal.c new file mode 100644 index 0000000..0762d85 --- /dev/null +++ b/pclsync/psignal.c @@ -0,0 +1,61 @@ +#include "psignal.h" +#include +#include + +static volatile sig_atomic_t sigint_flag = 0; +static volatile sig_atomic_t sigterm_flag = 0; +static volatile sig_atomic_t sighup_flag = 0; + +static void sigint_handler(int sig) { + sigint_flag = 1; +} + +static void sigterm_handler(int sig) { + sigterm_flag = 1; +} + +static void sighup_handler(int sig) { + sighup_flag = 1; +} + +void psignal_register(int signum) { + struct sigaction sa; + void (*handler)(int) = NULL; + + if (signum == SIGINT) { + handler = sigint_handler; + } else if (signum == SIGTERM) { + handler = sigterm_handler; + } else if (signum == SIGHUP) { + handler = sighup_handler; + } else { + return; + } + + sa.sa_handler = handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + if (sigaction(signum, &sa, NULL) != 0) { + return; + } +} + +int psignal_check_pending(void) { + sig_atomic_t int_flag = sigint_flag; + sig_atomic_t term_flag = sigterm_flag; + sig_atomic_t hup_flag = sighup_flag; + + if (int_flag) { + sigint_flag = 0; + return SIGINT; + } + if (term_flag) { + sigterm_flag = 0; + return SIGTERM; + } + if (hup_flag) { + sighup_flag = 0; + return SIGHUP; + } + return 0; +} diff --git a/pclsync/psignal.h b/pclsync/psignal.h new file mode 100644 index 0000000..b40ce63 --- /dev/null +++ b/pclsync/psignal.h @@ -0,0 +1,9 @@ +#ifndef PSIGNAL_H +#define PSIGNAL_H + +#include + +void psignal_register(int signum); +int psignal_check_pending(void); + +#endif