I am using following system calls in my program:
recvfrom
sendto
sendmsg
And from each system call mentioned above I check if it completes with out any interruption and in case if it is interrupted, I retry.
Ex:
recvagain:
len = recvfrom(fd, response, MSGSIZE, MSG_WAITALL, (struct sockaddr *)&from, &fromlen);
if (errno == EINTR) {
syslog(LOG_NOTICE, "recvfrom interrupted: %s", strerror(errno));
goto recvagain;
}
Problem here is that do I need to reset errno value to 0 each and every time it fails. Or if recvfrom() is successful, does it reset errno to 0?
recvfrom() man page says:
Upon successful completion, recvfrom() returns the length of the message in bytes. If no messages are available to be received and the
peer has performed an orderly shutdown, recvfrom() returns 0.
Otherwise the function returns -1 and sets errno to indicate the
error.
same case with sendto and sendmsg.
I can n’t really check this now as I don’t have access to server-client setup.
Any idea?
Thanks
The
errnopseudo “variable” may not change on successful syscalls. So you could clear it either before yourrecvfrom, or whenlen<0and having tested its value.See errno(3) man page for more.
Actually, as Robert Xiao (nneonneo) commented, you should not write
errnoand just test it when the syscall has failed (in that case, the C function -e.g.recvfrometc…- wrapping that syscall would have writtenerrnobefore returning -1).