[英]LD_PRELOAD with boost fileystem library
我試圖攔截Linux中的開放系統調用。 它適用於其他庫,但不使用boost libboost_fileystem。 這是我的代碼(為了便於閱讀而刪除)。
#include <boost/filesystem/fstream.hpp>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <stdio.h>
using namespace std;
using namespace boost::filesystem;
typedef int (*open_func_type)(const char * pathname, int flags, ...);
int open(const char *path, int flags, ...)
{
va_list arg;
mode_t mode = 0;
if (flags & O_CREAT)
{
va_start(arg, flags);
mode = va_arg(arg, mode_t);
va_end(arg);
}
//some stuff here
open_func_type open_func = (open_func_type) dlsym(RTLD_NEXT, "open");
return open_func(path, flags, mode);
}
int main()
{
boost::filesystem::fstream build_path;
build_path.open("/tmp/test.txt", ios::in);
//other stuff
return 0;
}
我使用gdb逐步執行代碼,我的open實現不會被調用。 但是做strace會顯示被調用的開放系統調用。 如果我調用其他調用open的庫函數,我會看到我的實現被調用。 我在這里做錯了什么? 我正在動態鏈接boost庫。
我花了一些時間弄明白,並發現內部提升調用std :: basic_filebuf打開,然后調用fopen。 fopen似乎在沒有調用開放庫調用的情況下調用內核中的開放系統調用(如果有人能指出它為什么會很好)。 我攔截了fopen電話,現在這個工作正常。 如果使用大文件支持,還需要實現fopen64。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.