I am trying my hand on Linux Signals. Where I have created a scenario mentioned below:
- Initially block all
SIGINTsignals usingsigprocmask(). - If sender send
SIGUSR1signal then unblockSIGINTfor rest of the process life.
However first step is successfully implemented but not able to unblock (or change) process mask using sigprocmask().
What am I doing wrong?
#include<stdio.h>
#include<signal.h>
#include<stdlib.h>
sigset_t block_list, unblock_list;
void sigint_handler(int sig)
{
printf("Ouch!!\n");
}
void sigusr1_handler(int sig)
{
sigemptyset(&unblock_list);
sigprocmask(SIG_SETMASK, &unblock_list, NULL);
}
int main(int argc, char **argv)
{
int count;
signal(SIGINT, &sigint_handler);
signal(SIGUSR1, &sigusr1_handler);
sigemptyset(&block_list);
sigaddset(&block_list, SIGINT);
sigprocmask(SIG_SETMASK, &block_list, NULL);
for(count=1; ;++count)
{
printf("Process id: %ld\t%d\n", (long)getpid(), count);
sleep(4);
}
exit(EXIT_SUCCESS);
}
$kill -n SIGINT <pid>
$kill -n SIGUSER1 <pid> //This call should unblock sigint_handler() for rest of the process life, but it is only unblocking for one time. Everytime I have call $kill -n SIGUSER1 <pid> to unblock SIGINT.
Note: Error handling has been removed for simplicity.
signal(2), but alwayssigaction(2), and you should never callprintf()(or any other non signal safe function) from a signal handler.