Add psignal module for centralized signal handling
- Added pclsync/psignal.h with psignal_register() and psignal_check_pending() API - Added pclsync/psignal.c with async-signal-safe handlers for SIGINT, SIGTERM, SIGHUP - Implements flag-based deferred signal handling pattern
This commit is contained in:
parent
9d4143f1f0
commit
8f951a7056
|
|
@ -0,0 +1,61 @@
|
|||
#include "psignal.h"
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef PSIGNAL_H
|
||||
#define PSIGNAL_H
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
void psignal_register(int signum);
|
||||
int psignal_check_pending(void);
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue