I’m debugging the following code:
signal(SIGALRM, testt);
alarm(1);
result = recvfrom( listening_socket, buf, maxlen, 0, &from, &fromlen );
printf("stoped\n");
As described by man 3 siginterrupt the alarm should interrupt the system call, but in my case it doesn’t. Alarm handler is invoked, but recvfrom is not interrupted.
However, when a new signal handler is specified with the signal(2) function, the system call is interrupted by default.
If I add siginterrupt(SIGALRM, 1); after setting the alarm handler then recvfrom is interrupted as expected.
What am I missing? What’s wrong with my code?
NOTES: Replacing signal with sigaction is not what I’m looking for.
The
siginterrupt(3)manpage was incorrect (version 3.33 of the Linux manpage set corrects the error). The default behavior of glibc’ssignalis to establish a signal that does not interrupt system calls. You can see this for yourself withstrace:⟶
Note the SA_RESTART in there. You can either keep doing what you’re doing — call
siginterrupt(SIGALRM, 1)after establishing the handler — or you can switch to usingsigaction, which will let you set the flags the way you want in the first place. You said you didn’t want to do that (why?) but nonetheless that is what I would recommend.