Reputation: 45
I'm writing a small program in C, (running on ubuntu). The first time the program receives Ctrl+C, I want the signal to be ignored, but the second time (after 10 seconds of waiting), I want the original action to be restored (and the signal not be ignored). So I have something like this:
void (*sold)(int);
struct sigaction s;
sold = s.sa_handler;
s.sa_handler = SIG_IGN;
sigaction(SIGINT,&s,NULL);
sleep(10)
s.sa_handler = sold; //(this could be replaced by s.sa_handler = *sold and it doenst make a difference)
The program seems to ignore SIGINT just fine, but it doesn't revert back.., as in it doesn't restore the old handler. What am I doing wrong?
Upvotes: 3
Views: 3835
Reputation: 180201
If you want to change the signal disposition again, you must call sigaction()
again (or another function that serves the same purpose). Assigning a new handler to the struct sigaction
with which you previously set the signal handler has no special effect.
Upvotes: 0
Reputation: 1
If you want to restore the old signal handler, you need to actually save and restore the old handler:
struct sigaction newHandler;
struct sigaction oldHandler;
memset(&newHandler, 0, sizeof(newHandler));
sigemptyset( &newHandler.sa_mask );
newHandler.sa_handler = SIG_IGN;
sigaction(SIGINT, &newHandler, &oldHandler );
sleep( 10 );
sigaction(SIGINT, &oldHandler, NULL );
Upvotes: 4