Recvfrom () timeout with alarm ()

I am debugging the following code:

signal(SIGALRM, testt); alarm(1); result = recvfrom( listening_socket, buf, maxlen, 0, &from, &fromlen ); printf("stoped\n"); 

As described in man 3 siginterrupt , the alarm should interrupt the system call, but in my case it is not. An alarm handler is called, but recvfrom not interrupted.

However, when a new signal handler is specified by the signal function (2), 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 is wrong with my code?

NOTES: sigaction signal with sigaction not what I'm looking for.

+7
source share
1 answer

Invalid siginterrupt(3) page ( version 3.33 of the Linux installation file fixes the error). The default behavior of glibc signal is to set a signal that does not interrupt system calls. You can see this yourself strace :

 void handler(int unused) {} int main(void) { signal(SIGALRM, handler); } 

& Longrightarrow;

 $ strace -e trace=rt_sigaction ./a.out rt_sigaction(SIGALRM, {0x4005b4, [ALRM], SA_RESTORER|SA_RESTART, 0x7fe732d3d490}, {SIG_DFL, [], 0}, 8) = 0 

Pay attention to SA_RESTART. You can continue to do what you do - call siginterrupt(SIGALRM, 1) after installing the handler - or you can switch to using sigaction , which allows you to set the flags the way you want in the first place. You said you didn’t want to do this (why?), But nevertheless this is what I would recommend.

+9
source

All Articles