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...).
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
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.
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With