我成功地拦截了对read()、write()、open()、unlink()、rename()、creat()的调用,但不知何故,以完全相同的语义拦截stat()却没有发生。我已经使用LD_PRELOAD更改了执行环境。
我是不是遗漏了什么?
这段代码相当庞大,它的哪一部分最有帮助呢?
谢谢。
编辑:我让interposed stat()包装器保持简单,以检查它是否正常工作。
int stat(const char *path,struct stat *buff)
{
printf("client invoke: stat %s",path);
return 1;
}发布于 2011-11-23 16:20:52
编译一个调用stat()的函数;查看生成了什么引用(nm -g stat.o)。然后,您将更好地了解要插入哪些函数。提示:它可能不叫stat()。
发布于 2011-11-23 12:47:36
如果您正在使用64位文件偏移量进行编译,那么stat()要么是一个宏,要么是一个解析为stat64()的重定向函数声明,因此您也必须插入该函数。
发布于 2013-09-04 16:48:24
当在linux中运行时,这并不是很简单。Gnu libc做了一些技巧。您需要截取__xstat,如果您想调用原始的and,请保存调用。
下面是我是如何让它工作的
gcc -fPIC -shared -o stat.so stat.c -ldl
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
static int (*old_xstat)(int ver, const char *path, struct stat *buf) = NULL;
static int (*old_xstat64)(int ver, const char *path, struct stat64 *buf) = NULL;
int __xstat(int ver, const char *path, struct stat *buf)
{
if ( old_xstat == NULL ) {
old_xstat = dlsym(RTLD_NEXT, "__xstat");
}
printf("xstat %s\n",path);
return old_xstat(ver,path, buf);
}
int __xstat64(int ver, const char *path, struct stat64 *buf)
{
if ( old_xstat64 == NULL ) {
old_xstat64 = dlsym(RTLD_NEXT, "__xstat64");
}
printf("xstat64 %s\n",path);
return old_xstat64(ver,path, buf);
}https://stackoverflow.com/questions/8237294
复制相似问题