gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:34:50 26 4
gpt4 key购买 nike

我正在寻找一种在 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, 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 - 进程树中的所有进程都将被强制加载库。如果 32 位进程必须加载 64 位库,它将无法运行,强制尝试加载 32 位库的 64 位进程也是如此。您可以向上面的源添加一个 init 函数,删除 LD_PRELOAD 环境变量(或将其设置为无害的东西)以稍微控制它。

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

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

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

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

关于c - 寻找一种在 Linux 中强制进行简短阅读的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39585409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com