2011-11-23 59 views
3

我已經successfuly攔截來電read()write()open()unlink()rename()creat()但不知何故,與同樣的語義攔截stat()沒有發生。我已經使用LD_PRELOAD更改了執行環境。攔截STAT()

我錯過了什麼嗎?

該代碼是相當龐大的,哪些部分將是最有用的發佈,所以你可以幫助?

謝謝。

編輯:我保持介入的stat()包裝簡單,以檢查它是否工作。

int stat(const char *path,struct stat *buff) 
{ 
    printf("client invoke: stat %s",path); 
    return 1; 
} 
+0

嗯,我想發佈的源代碼的統計功能? –

回答

5

編譯一個函數,調用stat();看看有什麼參考文獻(nm -g stat.o)。然後,您將更好地瞭解要設置哪些功能。提示:它可能不叫stat()

+0

它的_xstat。但我該如何介入? –

+2

就像你爲其他函數所做的那樣:在你的'LD_PRELOAD'庫中聲明你自己的'_xstat'函數。但是,如果你想要捕獲很多系統調用(而不僅僅是幾個系統調用),你可以做'strace'正在做的事情,例如用'ptrace'系統調用一些晦澀的技巧。 –

+0

在系統頭文件中找到'xstat()'以獲得正確的原型,然後編寫您的插入版本,就像您做其他功能一樣。 –

3

如果您正在使用64位文件偏移編譯,然後stat()是宏或解析爲stat64()重定向函數聲明,所以你必須對這個函數干預太多。

+0

似乎沒有工作... –

3

當然,在linux中運行並不是那麼簡單。 Gnu libc做了一些技巧。你需要攔截__xstat,如果你想調用原來的保存通話。

這是我如何得到它的工作

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); 
}