gpt4 book ai didi

linux - 我如何(可重现)从 close(2) 触发 EIO 错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:07:43 27 4
gpt4 key购买 nike

close(2) 的 Linux 手册页指出:

NOTES

Not checking the return value of close() is a common but nevertheless serious programming error. It is quite possible that error on a previous write(2) operation are first reported at the final close(). Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and with disk quota. [...]

现在我想知道这是否真的是这样:许多软件项目检查close() 的返回值,但这是一个问题吗?我试图通过在文件中使用小型 ext2 文件系统并写入磁盘容量边界附近的文件来生成这样的错误,但我唯一得到的是 ENOSPC 来自 write(2) 系统调用。

那么:有没有一种方法可以在 close(2) 上使用引用 file 的有效文件描述符重复触发 I/O 错误,最好是在 Linux 上,但 *BSD 也可以。

最佳答案

如联机帮助页所述,这主要是 NFS 共享的问题。引用 nfs.sourceforge.net:

So, when an application opens a file stored in NFS, the NFS client checks that it still exists on the server, and is permitted to the opener, by sending a GETATTR or ACCESS operation. When the application closes the file, the NFS client writes back any pending changes to the file so that the next opener can view the changes. This also gives the NFS client an opportunity to report any server write errors to the application via the return code from close().

这就是检查 close() 的 ret 值变得重要的地方。当您关闭 (fd) 时,磁盘文件系统在正常情况下不会提供 EIO。但是您可以尝试一些操作,例如在您的程序关闭文件时拔出 U 盘。

关于linux - 我如何(可重现)从 close(2) 触发 EIO 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com