Вопрос

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 errno pseudo "variable" may not change on successful syscalls. So you could clear it either before your recvfrom, or when len<0 and having tested its value.

See errno(3) man page for more.

Actually, as Robert Xiao (nneonneo) commented, you should not write errno and just test it when the syscall has failed (in that case, the C function -e.g. recvfrom etc...- wrapping that syscall would have written errno before returning -1).

Другие советы

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... */
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top