如果close(2)因EIO失败,是否仍将删除文件描述符? [英] If close(2) fails with EIO, will the file descriptor still be deleted?

查看:139
本文介绍了如果close(2)因EIO失败,是否仍将删除文件描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果对EIO的close(2)系统调用失败,是否仍将删除文件描述符?

If a close(2) system call fails with EIO, will the file descriptor still be deleted?

如果是,是否无法通过稍后重试来处理伪造的IO错误?如果不是,应该如何防止文件描述符泄漏?

If yes, is it not possible to handle a spurious IO error by retrying later? If no, how should one prevent a file descriptor leak?

推荐答案

这是一个棘手的问题.但是,POSIX标准确实在 close() 的描述中进行了说明. :

That's a tricky question. However, the POSIX standard does cover it in the description of close():

如果close()被要捕​​获的信号中断,则它将返回-1,并将errno设置为[EINTR],并且未指定fildes的状态.如果在close()期间从文件系统读取或写入文件系统时发生I/O错误,则它可能会返回-1,并且errno设置为[EIO].如果返回此错误,则未指定fildes的状态.

If close() is interrupted by a signal that is to be caught, it shall return -1 with errno set to [EINTR] and the state of fildes is unspecified. If an I/O error occurred while reading from or writing to the file system during close(), it may return -1 with errno set to [EIO]; if this error is returned, the state of fildes is unspecified.

因此,标准未指定文件描述符的状态.

So, the state of the file descriptor is unspecified by the standard.

出于大多数实际目的,它是封闭的;即使文件描述符正式打开,您也几乎无法做.您可以尝试执行无害的操作(例如fcntl()F_GETFL),然后查看是否返回EBADF,这表明描述符已正式关闭.但是,如果它是打开的,并且EIO错误的原因是永久的,那么您每次尝试对其进行任何操作(可能包括fcntl()调用)时,都有可能会得到EIO.您可能会也可能永远不会获得另一个类似open的操作所返回的描述符.尚不清楚如果死文件描述符已打开但无法关闭,即使dup2()能否成功指定'死'文件描述符为目标.

For most practical purposes, it is closed; there is precious little you can do with the file descriptor even if it is officially open. You could try an innocuous operation (like fcntl() and F_GETFL) and see whether you get EBADF back, indicating the descriptor is formally closed. But if it is open and the cause of the EIO error is permanent, then you're likely to get EIO every time you try to do anything with it (possibly including the fcntl() call). You might or might not ever get the same descriptor returned by another open-like operation. It is not clear that even dup2() could be successful specifying the 'dead' file descriptor as the target if the dead file descriptor is open but uncloseable.

这篇关于如果close(2)因EIO失败,是否仍将删除文件描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆