小编Jas*_*eth的帖子

在 Mac 上记录“open”系统调用

我想知道open(2)bash 脚本中进行了哪些调用。

我编写了以下拦截系统调用的程序:

#include <fcntl.h>
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

#define DYLD_INTERPOSE(_replacment,_replacee) \
__attribute__((used)) static struct{ const void* replacment; const void* replacee; } _interpose_##_replacee \
__attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacment, (const void*)(unsigned long)&_replacee };

static
int
my_open(const char *filename, int oflag, mode_t mode)
{
    printf("$jason$ open: %s\n", filename);
    return open(filename, oflag, mode);
}
DYLD_INTERPOSE(my_open, open)
Run Code Online (Sandbox Code Playgroud)

然后我使用以下命令运行:

clang -dynamiclib libfile.c -o libfile.dylib
export DYLD_INSERT_LIBRARIES=libfile.dylib
touch /tmp/testingtesting
Run Code Online (Sandbox Code Playgroud)

这不起作用。

我用自己编译的程序尝试了一下,效果很好。我用brew编译的程序尝试了一下,效果很好。我阅读了touch.c的源代码。它调用open(2).

然后我禁用了 …

c macos bash dtrace system-calls

5
推荐指数
2
解决办法
938
查看次数

标签 统计

bash ×1

c ×1

dtrace ×1

macos ×1

system-calls ×1