Skip to content

Instantly share code, notes, and snippets.

@banthar
Created January 28, 2011 12:24
Show Gist options
  • Save banthar/800189 to your computer and use it in GitHub Desktop.
Save banthar/800189 to your computer and use it in GitHub Desktop.
lazy read
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
{
static ssize_t (*orig_read)(int, void *, size_t)=NULL;
if(orig_read==NULL)
{
orig_read = dlsym (RTLD_NEXT, "read");
char * error;
if ((error = dlerror()) != NULL)
{
fprintf (stderr, "lazy_read: %s\n", error);
exit (EXIT_FAILURE);
}
}
size_t ret = (*orig_read)(__fd, __buf, __nbytes);
if(ret==0)
{
//TODO fuser /proc/*/fd/* /proc/*/fdinfo/*
usleep(1000000);
ret = (*orig_read)(__fd, __buf, __nbytes);
}
return ret;
}
@banthar
Copy link
Author

banthar commented Jan 28, 2011

gcc -Wall -fPIC -D_GNU_SOURCE -shared -o lazy_read.so lazy_read.c -ldl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment