Generate read error

Easily create a write error in a test case by writing to /dev/full . Is there a good technique for creating a read error? I am currently using LD_PRELOAD to override read , but this seems too complicated and not portable (not that / dev / full is portable ...).

+6
source share
4 answers

Besides reading from the directory (as mentioned in the previous answer), you can try reading /proc/self/mem to get an error message (this should lead you to EIO on Linux). See Section https://unix.stackexchange.com/a/6302 for clarification.

+5
source

The approach that works in all major organizations will be to implement a small FUSE file system. EIO is the default error code when your userpace file system driver does something wrong, so it is easy to achieve. Both the Perl and Python bindings come with examples to get you started, you can quickly write a file system that mainly reflects existing files, but enters EIO in carefully selected places.

There exists such a file system: petardfs ( article ), I do not know how well this works out of the box.

+4
source

According to the control page (OS X) read (2), read (2) generates an error if "[a] n an attempt was made to read the directory". Therefore, you can open the (2) directory (make sure prot does not allow writing, otherwise it will cause an error), and then try to read it. This is similar to the only error listed there that could occur in β€œnormal” circumstances (i.e., not to do something like intentionally violating the FILE * structure).

I assume you are talking about reading errors (2) in C or something like that, but even in a higher level language, you can open the directory and try to read it (although I just tried it with Python and it is too smart so you can open the directory ...)

+2
source

You can also pass an illegal pointer as a read buffer, which will return -EFAULT. Sort of:

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

Thanks Suzuki

+1
source

Source: https://habr.com/ru/post/919136/


All Articles