system call return values and errno

Ram picture Ram · Oct 17, 2012 · Viewed 9.3k times · Source

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

Answer

nneonneo picture nneonneo · Oct 17, 2012

recvfrom returns -1 if it is interrupted (and sets errno to EINTR). Therefore, you should just check len:

if(len == -1) {
    if(errno == EINTR) {
        syslog(LOG_NOTICE, "recvfrom interrupted");
        goto recvagain;
    } else {
        /* some other error occurred... */
    }
}