Reputation:
Consider the following program:
void handler(int signum){
printf("handling %d\n", signum);
}
int main() {
signal(SIGINT, handler);
sigset_t *ss;
sigemptyset(ss);
sigaddset(ss, SIGINT);
sigprocmask(SIG_BLOCK, ss, NULL);
for(;;);
return 0;
}
After running this program in the terminal, when I press ctrl-c, i always get "handling 2" as output, but I would expect to get no output, as SIGINT was previously blocked. Am I blocking SIGINT wrongly? Am I misunderstanding what it means for a signal to be blocked, by assuming that a blocked signal will not be handled on entering the process from kernel mode to user mode?
Upvotes: 6
Views: 128
Reputation: 9203
@Useless has rightly answered the question stating that you are invoking UB by passing an uninitialized pointer to sig*
functions.
As to why it works and doesn't crash or doesn't block SIGINT can be made clear if you add the line -
printf("%p", ss);
This will most probably print (nil)
or 0
.
Luckily the uninitialized variable is set to NULL
and from the documentation of sigprocmask
If
set
isNULL
, then the signal mask is unchanged (i.e., how is ignored), but the current value of the signal mask is nevertheless returned inoldest
(if it is notNULL
).
So your blocking of SIGINT
is in fact being ignored. But yes, you might also see crashes or some other (even correct behavior) if you are not lucky.
Upvotes: 1