寻找一种方法来强制在Linux中进行简短的阅读

我正在寻找在linux中产生短读取的方法,所以我可以unit testing他们周围的处理代码。

我有一些方法,在较低的层次调用pread / pread64从文件系统中的文件读取。 这些devise用于处理发生短读取的情况(读取的字节数小于请求的数量)。

我已经看到发生短读取的情况(跨networking文件系统)。

理想情况下,我将能够创build一个文件,允许读取N个字节,然后短时间读取M个字节,然后按预期正常读取。 这将允许unit testing指向文件/文件系统。

谢谢!

如果您知道要进行拦截的库调用,则可以使用通过LD_PRELOAD加载的共享对象在调用上进行干预。

shortread.c:

 #include <sys/types.h> #include <dlfcn.h> #define MAX_FDS 1024 static int short_read_array[ MAX_FDS ]; // #define these to match your system's values // (need to be really careful with header files since // getting open() declared would make things very // difficult - just try this with open( const char *, int, ...); // declared to see what I mean...) #define O_RDONLY 0 #define O_WRONLY 1 #define O_RDWR 2 // note that the mode bits for read/write are // not a bitwise-or - they are distinct values #define MODE_BITS 3 // it's much easier to *NOT* even deal with the // fact that open() is a varargs function // but that means probably having to do some // typedef's and #defines to get this to compile // typedef some function points to make things easier typedef int ( *open_ptr_t )( const char *name, int flags, mode_t mode ); typedef ssize_t ( *read_ptr_t )( int fd, void *buf, size_t bytes ); typedef int ( *close_ptr_t )( int fd ); // function points to the real IO library calls static open_ptr_t real_open = NULL; static read_ptr_t real_read = NULL; static close_ptr_t real_close = NULL; // this will return non-zero if 'filename' is a file // to cause short reads on static int shortReadsOnFd( const char *filename ) { // add logic here based on the file name to // return non-zero if you want to do // short reads on this file // // return( 1 ); return( 0 ); } // interpose on open() int open( const char *filename, int flags, mode_t mode ) { static pthread_mutex_t open_mutex = PTHREAD_MUTEX_INITIALIZER; int fd; pthread_mutex_lock( &open_mutex ); if ( NULL == real_open ) { real_open = dlsym( RTLD_NEXT, "open" ); } pthread_mutex_unlock( &open_mutex ); fd = real_open( filename, flags, mode ); if ( ( -1 == fd ) || ( fd >= MAX_FDS ) ) { return( fd ); } int mode_bits = flags & MODE_BITS; // if the file can be read from, check if this is a file // to do short reads on if ( ( O_RDONLY == mode_bits ) || ( O_RDWR == mode_bits ) ) { short_read_array[ fd ] = shortReadsOnFd( filename ); } return( fd ); } ssize_t read( int fd, void *buffer, size_t bytes ) { static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER; if ( ( fd < MAX_FDS ) && ( short_read_array[ fd ] ) ) { // read less bytes than the caller asked for bytes /= 2; if ( 0 == bytes ) { bytes = 1; } } pthread_mutex_lock( &read_mutex ); if ( NULL == real_read ) { real_read = dlsym( RTLD_NEXT, "read" ); } pthread_mutex_unlock( &read_mutex ); return( real_read( fd, buffer, bytes ) ); } int close( int fd ) { static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock( &close_mutex ); if ( NULL == real_close ) { real_close = dlsym( RTLD_NEXT, "close" ); } pthread_mutex_unlock( &close_lock ); if ( fd < MAX_FDS ) { short_read_array[ fd ] = 0; } return( real_close( fd ) ); } 

编译如下:

 gcc -shared [-m32|-m64] shortread.c -o libshortread.so 

然后:

 export LD_PRELOAD=/path/to/libshortread.so 

对这样的LD_PRELOAD非常小心 – 进程树中的所有进程将被强制加载库。 如果必须加载64位库,32位进程将无法运行,而64位进程也会被迫尝试加载32位库。 你可以在上面的源代码中添加一个init函数来删除LD_PRELOAD环境变量(或者将其设置为无害)来控制这一点。

如果任何应用程序使用open()O_DIRECT标志,您也可能需要小心。 修改正在读取的字节数可能会破坏某些Linux文件系统和/或实现的直接IO,因为可能只支持页面大小的IO操作。

而这段代码只处理read() 。 您可能还需要处理creat() 。 还有pread()readat()aio_read()lio_listio() (甚至还有其他一些我现在还不记得的东西),虽然这是不太可能的。 当心处理大文件的32位进程。 我已经处理了这些事情已经有一段时间了,但是我记得那会变得很丑。

另一个需要注意的是fopen()fread()这样的调用可能不会调用open()read()库调用,并可能直接发出相关的系统调用。 在这种情况下,您将无法轻松修改这些调用的行为。 插入到可以读取数据(如fgets()的基于STDIO的调用的整个系列可能是一件非常困难的事情,而不会破坏事情。

如果你知道你的应用程序是单线程的,你可以删除互斥锁。

最后我使用mkfifo()解决这个问题。

我创建了命名管道,然后连接一个编写器(最终将其封装在一个JNI库中,以便从Java中使用)。 然后可以让异步写入器在正确的时间写入数据,此时连接的读取器只获取可用/写入的字节,而不是请求的总数。