funfail/src/read.c

26 lines
552 B
C

#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
typedef ssize_t (*f)(int fd, void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count)
{
static f og_read = NULL;
static int calls = 0;
const int MAX_CALLS = getenv("MAX_READ_CALLS") ? atoi(getenv("MAX_READ_CALLS")) : -1;
if (!og_read)
{
og_read = (f)dlsym(RTLD_NEXT, "read");
}
calls++;
if (MAX_CALLS < 0 || calls <= MAX_CALLS)
{
return og_read(fd, buf, count);
}
errno = EBADF;
return -1;
}