Is there a way to get poll() to return an error?
Perhaps with some errors, but a number of potential errors exist. A general, not poll()
specific, approach follows.
Sometimes test code needs to inject a fake error with alternate code.
The correctness of this general approach is highly dependent code and test goals.
int poll_result = poll(&fds, nfds, timeout);
#if TEST1
if (poll_result != -1) {
// Avoid using rand()
static unsigned seed = 0;
seed = (16807 * seed) mod 2147483647; // https://stackoverflow.com/a/9492699/2410359
if (seed % 97 == 0) { // about 1% of the time
// adjust as desired, e.g. EINVAL may not make sense here.
int faults[] = { EFAULT, EINTR, EINVAL, ENOMEM };
const unsigned n = sizeof faults / sizeof faults[0];
errno = faults[seed % n];
poll_result = -1;
}
}
#endif
...
poll()
ref See notes section about EAGAIN, EINTR
.
Of course using alternate code may hide a problem that occurs only with true code.