Generate a read error

我的未来我决定 提交于 2019-12-21 11:14:04

问题


It's simple to generate a write error in a test suite by writing to /dev/full. Is there a good technique to generate a read error? I'm currently using LD_PRELOAD to override read but that seems too complicated and non-portable (not that /dev/full is portable...).


回答1:


Besides reading from a directory (as mentioned in a previous answer) you can try to read /proc/self/mem to get an error (this should get you an EIO on Linux). For an explanation, please see: https://unix.stackexchange.com/a/6302




回答2:


An approach that works on all major unices would be to implement a small FUSE filesystem. EIO is the default error code when your userspace filesystem driver does something wrong, so it's easy to achieve. Both the Perl and Python bindings come with examples to get started, you can quickly write a filesystem that mostly mirrors existing files but injects an EIO in carefully chosen places.

There's an existing such filesystem: petardfs (article), I don't know how well it works out of the box.




回答3:


According to the (OS X) read(2) manpage, read(2) will generate an error if "[a]n attempt is made to read a directory." You could therefore open(2) a directory (make sure the prot doesn't permit writing, or this'll throw an error) and then try to read from it. That looks like the only error listed there which could happen in 'normal' circumstances (ie without doing something like deliberately breaking a FILE* struct).

I'm presuming you're talking about read(2) errors in C or something like it, but even in a higher-level language, you might be able to open a directory and try to read from it (though I just tried it with Python, and it's too smart to let you open the directory...)




回答4:


You could as well pass an illegal pointer as buffer to read, which would return an -EFAULT. Something like :

read(fd, (char *)0, cout);

Thanks Suzuki



来源:https://stackoverflow.com/questions/11228509/generate-a-read-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!