寻找一种强制在linux中进行简短读取的方法

Cor*_*eyP 5 c linux testing io

我正在寻找一种在linux中生成短读取的方法,因此我可以对它们周围的处理代码进行单元测试.

我有许多方法,在较低级别调用pread/pread64从文件系统中的文件读取.这些设计用于处理发生短读取的情况(读取的字节数小于请求的数量).

我见过发生短读取的情况(跨网络文件系统).

理想情况下,我将能够创建一个允许读取N个字节的文件,然后将发生M字节的短读取,然后按预期进行正常读取.这将允许单元测试指向文件/文件系统.

谢谢!

And*_*nle 2

如果您知道要拦截的库调用,您可以使用通过 加载的共享对象来插入调用LD_PRELOAD

短读.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 ) );
}
Run Code Online (Sandbox Code Playgroud)

编译类似:

gcc -shared [-m32|-m64] shortread.c -o libshortread.so
Run Code Online (Sandbox Code Playgroud)

然后:

export LD_PRELOAD=/path/to/libshortread.so
Run Code Online (Sandbox Code Playgroud)

使用这样的 LD_PRELOAD 要非常小心 - 进程树中的所有进程都将被迫加载该库。如果必须加载 64 位库,32 位进程将无法运行,强制尝试加载 32 位库的 64 位进程也会失败。您可以在上面的源代码中添加一个 init 函数,该函数会删除LD_PRELOAD环境变量(或将其设置为无害的值)以进行某种程度的控制。

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

并且此代码仅处理read(). 您可能还需要处理creat(). 另外pread(),,,和readat(),(甚至可能还有其他一些我现在不记得的),尽管这确实不太可能。并注意处理大文件的 32 位进程。我已经有一段时间没有处理这些问题了,但据我回忆,这可能会变得很难看。aio_read()lio_listio()

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

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