errno (3) is set to EIO only for
EIO Input/output error (POSIX.1)
also according to read (2) for:
EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling terminal, and either it is ignoring or blocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape.
and in accordance with write (2) for:
EIO A low-level I/O error occurred while modifying the inode.
Thus, simulating this particular error code may be difficult; note that there are other system calls for I / O, especially writev (2) and (indirectly) mmap (2) , but read(2) and write(2) are the most common.
Note that file systems and the Linux kernel (for example, its VFS level ) cache data. You can get EIO much later or never. See sync (2) and fsync (2)
However, as a rule, most programs do not specifically handle EIO wrt other error codes; you are probably testing enough by getting another error code, like for example.
EDQUOT The user quota of disk blocks on the filesystem containing the file referred to by fd has been exhausted.
So, you will probably experience enough by limiting disk quotas (see quotactl (2) , setquota (8) , etc.) and file space (see setrlimit (2) with RLIMIT_FSIZE , prlimit (1) , ulimit built-in bash ( 1) etc.)
If you really want to fake EIO , you can physically damage the device (or maybe just unplug the USB drive at the wrong time) or write your own File System in User Space (FUSE) that simulates it. I donโt think itโs worth the effort (because when something gets EIO , the whole computer becomes unusable very quickly, and the user will notice it anyway ... and because most software tools handle all error codes in the same way - except EINTR )
In part C of your code, you can use strerror (3) (with syslog (3) ) and / or perror (3) . I'm not sure it is worth trying to handle EIO completely different way from most other errors.
NB: many critical domains have standards that determine how errors should be handled, and code must be developed and tested, for example. ISO26262 in automotive or DO-178B in avionics. Follow your domain standards.