【问题标题】:Looking for a way to force a short read in linux寻找一种在 linux 中强制进行短读的方法
【发布时间】:2016-09-20 03:51:04
【问题描述】:

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

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

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

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

谢谢!

【问题讨论】:

  • 最简单,或者至少是最灵活的方法,可能是使用mkfifo()mknod() 创建一个命名管道。
  • 调用“cover”函数,该函数通常只调用相关的read() 变体并返回完整大小,但可以配置为按需返回一小部分。 ssize_t tst_read(void *buffer, size_t size, int fd) { ssize_t nbytes = read(buffer, size, fd); if (…appropriate test condition…) nbytes -= 13; return nbytes; }。冲洗并重复您需要测试的每个类似读取的功能。
  • 嗯,是的,如果您可以按照@JonathanLeffler 的建议封装读取调用本身,当然是最好的:)
  • 我尝试了mkfifo(),因为这是我所追求的行为,尽管内核对我来说似乎太聪明了。当我在其他地方打开文件时,它会在打开时阻塞,它会读取我对 fifo 文件句柄的所有写调用,而不会真正阻塞等待读者读取文件。无论写入大小如何,它似乎都会这样做。我很犹豫是否只包装/覆盖read,因为它们位于离我想玩东西的地方很远的图书馆中。

标签: c linux testing io


【解决方案1】:

如果您知道要拦截的库调用,您可以使用通过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 位进程将无法运行,强制尝试加载 32 位库的 64 位进程也将无法运行。您可以在上面的源代码中添加一个 init 函数,该函数会删除 LD_PRELOAD 环境变量(或将其设置为无害的变量)以在一定程度上对其进行控制。

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

而且这段代码只处理read()。您可能还需要处理creat()。还有pread()readat()aio_read()lio_listio(),(甚至可能还有一些我现在想不起来的其他人)虽然这当然不太可能。并注意处理大文件的 32 位进程。自从我处理这些问题以来已经有一段时间了,但我记得那会变得很难看。

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

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

【讨论】:

  • 谢谢安德鲁!非常详细的描述和解决方案。
【解决方案2】:

最后我使用了mkfifo() 的解决方案。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 2011-07-19
    相关资源
    最近更新 更多