加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:45:42 所属栏目:Linux 来源:网络整理
导读:我正在寻找一种在 linux中生成短读取的方法,因此我可以对它们周围的处理代码进行单元测试. 我有许多方法,在较低级别调用pread / pread64从文件系统中的文件读取.这些设计用于处理发生短读取的情况(读取的字节数小于请求的数量). 我见过发生短读取的情况(跨网
我正在寻找一种在 linux中生成短读取的方法,因此我可以对它们周围的处理代码进行单元测试.

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

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

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

谢谢!

解决方法

如果您知道要进行拦截的库调用,则可以使用通过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,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的调用系列可能是一件非常困难的事情.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读