In my application, I try to catch a specific signal which is SIGUSR1, I wrote the below code to do this issue, in which the application will create a thread for "init_signal_catcher".
The problem is when I run the code ,the program goes to sleep forever and does not respond to any signal even if it is SIGUSR1.
So could you please tell me what is the problem in my below code?
static void sig_handler(const int sig_number, siginfo_t *sig_info, void *context)
{
if (sig_number == SIGSEGV)
{
error_sys("Error at address 0x%lx", (long)sig_info->si_addr);
exit(-1);
}
sig_value = sig_number;
}
int init_signal_catcher()
{
struct sigaction sig_action;
sigset_t oldmask;
sigset_t newmask;
sigset_t zeromask;
memset(&sig_action, 0, sizeof(struct sigaction));
sig_action.sa_flags = SA_SIGINFO;
sig_action.sa_sigaction = sig_handler;
sigaction(SIGHUP, &sig_action, NULL);
sigaction(SIGINT, &sig_action, NULL);
sigaction(SIGTERM, &sig_action, NULL);
sigaction(SIGSEGV, &sig_action, NULL);
sigaction(SIGUSR1, &sig_action, NULL);
Sigemptyset(&newmask);
Sigaddset(&newmask, SIGHUP);
Sigaddset(&newmask, SIGINT);
Sigaddset(&newmask, SIGTERM);
Sigaddset(&newmask, SIGSEGV);
Sigaddset(&newmask, SIGUSR1);
Sigprocmask(SIG_BLOCK, &newmask, &oldmask);
Sigemptyset(&zeromask);
sig_value = 0;
while ((sig_value != SIGINT) && (sig_value != SIGTERM))
{
sig_value = 0;
printf("Suspending on %lu mask.", zeromask);
sigsuspend(&zeromask);
switch(sig_value)
{
printf("Caught Signal %d", sig_value);
case SIGUSR1:
printf("Caught SIGUSR1");
break;
}
}
return 0;
}