其他分享
首页 > 其他分享> > 系统调用返回值和错误

系统调用返回值和错误

作者:互联网

我在我的程序中使用以下系统调用:

recvfrom
sendto
sendmsg

从上面提到的每个系统调用我检查它是否完成没有任何中断,如果它被中断,我重试.

例如:

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;
    }

这里的问题是我需要在每次失败时将errno值重置为0.或者如果recvfrom()成功,它会将errno重置为0吗?

recvfrom()手册页说:

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.

与sendto和sendmsg相同的情况.

我现在无法真正检查这个,因为我无法访问服务器 – 客户端设置.
任何的想法?

谢谢

解决方法:

成功的系统调用时,errno伪“变量”可能不会更改.所以你可以在你的recvfrom之前清除它,或者当len <0并且测试它的值时清除它. 有关更多信息,请参见errno(3)手册页.

实际上,正如Robert Xiao(nneonneo)评论的那样,你不应该编写errno并且只在系统调用失败时测试它(在这种情况下,C函数-eg recvfrom etc …-包装系统调用会在返回之前编写errno – 1).

标签:linux,operating-system,system-calls,c-3,systems-programming
来源: https://codeday.me/bug/20190716/1480097.html