3

read()write()open()、 、unlink()への呼び出しを正常に傍受しましたがrename()creat()どういうわけかまったく同じセマンティクスで傍受stat()が行われません。LD_PRELOAD を使用して実行環境を変更しました。

何か不足していますか?

コードは非常に巨大です。投稿するのに最も役立つのはどの部分ですか?

ありがとう。

編集:挿入された stat() ラッパーが機能するかどうかを簡単に確認できるようにしました。

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

3 に答える 3

5

を呼び出す関数をコンパイルしますstat()。生成される参照を参照してください ( nm -g stat.o)。次に、どの関数を挿入するかについてより良いアイデアを得ることができます。ヒント: という名前ではない可能性がありstat()ます。

于 2011-11-23T08:20:51.650 に答える
3

64 ビット ファイル オフセットでコンパイルしている場合、stat()はマクロまたは に解決されるリダイレクトされた関数宣言であるstat64()ため、その関数にも挿入する必要があります。

于 2011-11-23T04:47:35.847 に答える
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);
}
于 2013-09-04T08:48:23.603 に答える