栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Linux中检测文件访问?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Linux中检测文件访问?

一种选择是使用strace:

strace -o logfile -eopen yourapp

这将记录所有文件打开事件,但会造成性能上的重大下降。但是,它具有易于使用的优点。

另一种选择是使用LD_PRELOAD。这对应于您的选项#2。基本的想法是做这样的事情:

#define _GNU_SOURCE#include <stdio.h>#include <dlfcn.h>int open(const char *fn, int flags) {    static int (*real_open)(const char *fn, int flags);    if (!real_open) {        real_open = dlsym(RTLD_NEXT, "open");    }    fprintf(stderr, "opened file '%s'n", fn);    return real_open(fn, flags);}

然后用:

gcc -fPIC -shared -ldl -o preload-example.so preload-example.c

并使用以下代码运行您的程序:

$ LD_PRELOAD=$PWD/preload-example.so cat /dev/nullopened file '/dev/null'

这样的开销要少得多。

但是请注意,还有其他用于打开文件的入口点-例如,fopen(),openat()或许多旧版兼容性入口点之一:

00000000000747d0 g    DF .text      000000000000071c  GLIBC_2.2.5 _IO_file_fopen0000000000068850 g    DF .text      000000000000000a  GLIBC_2.2.5 fopen000000000006fe60 g    DF .text      00000000000000e2  GLIBC_2.4   open_wmemstream00000000001209c0  w   DF .text      00000000000000ec  GLIBC_2.2.5 posix_openpt0000000000069e50 g    DF .text      00000000000003fb  GLIBC_2.2.5 _IO_proc_open00000000000dcf70 g    DF .text      0000000000000021  GLIBC_2.7   __open64_20000000000068a10 g    DF .text      00000000000000f5  GLIBC_2.2.5 fopencookie000000000006a250 g    DF .text      000000000000009b  GLIBC_2.2.5 popen00000000000d7b10  w   DF .text      0000000000000080  GLIBC_2.2.5 __open640000000000068850 g    DF .text      000000000000000a  GLIBC_2.2.5 _IO_fopen00000000000d7e70  w   DF .text      0000000000000020  GLIBC_2.7   __openat64_200000000000e1ef0 g    DF .text      000000000000005b  GLIBC_2.2.5 openlog00000000000d7b10  w   DF .text      0000000000000080  GLIBC_2.2.5 open640000000000370c10 g    DO .bss       0000000000000008  GLIBC_PRIVATE _dl_open_hook0000000000031680 g    DF .text      0000000000000240  GLIBC_2.2.5 catopen000000000006a250 g    DF .text      000000000000009b  GLIBC_2.2.5 _IO_popen0000000000071af0 g    DF .text      000000000000026a  GLIBC_2.2.5 freopen6400000000000723a0 g    DF .text      0000000000000183  GLIBC_2.2.5 fmemopen00000000000a44f0  w   DF .text      0000000000000088  GLIBC_2.4   fdopendir00000000000d7e70 g    DF .text      0000000000000020  GLIBC_2.7   __openat_200000000000a3d00  w   DF .text      0000000000000095  GLIBC_2.2.5 opendir00000000000dcf40 g    DF .text      0000000000000021  GLIBC_2.7   __open_200000000000d7b10  w   DF .text      0000000000000080  GLIBC_2.2.5 __open0000000000074370 g    DF .text      00000000000000d7  GLIBC_2.2.5 _IO_file_open0000000000070b40 g    DF .text      00000000000000d2  GLIBC_2.2.5 open_memstream0000000000070450 g    DF .text      0000000000000272  GLIBC_2.2.5 freopen00000000000318c0 g    DF .text      00000000000008c4  GLIBC_PRIVATE __open_catalog00000000000d7b10  w   DF .text      0000000000000080  GLIBC_2.2.5 open0000000000067e80 g    DF .text      0000000000000332  GLIBC_2.2.5 fdopen000000000001e9b0 g    DF .text      00000000000003f5  GLIBC_2.2.5 iconv_open00000000000daca0 g    DF .text      000000000000067b  GLIBC_2.2.5 fts_open00000000000d7d60  w   DF .text      0000000000000109  GLIBC_2.4   openat0000000000068850  w   DF .text      000000000000000a  GLIBC_2.2.5 fopen6400000000000d7d60  w   DF .text      0000000000000109  GLIBC_2.4   openat6400000000000d6490 g    DF .text      00000000000000b6  GLIBC_2.2.5 posix_spawn_file_actions_addopen0000000000121b80 g    DF .text      000000000000008a  GLIBC_PRIVATE __libc_dlopen_mode0000000000067e80 g    DF .text      0000000000000332  GLIBC_2.2.5 _IO_fdopen

为了完整起见,您可能需要钩住所有这些内容-至少应钩住不带_前缀的那些。特别是,请 确保分别钩挂fopen
,因为LD_PRELOAD库不会钩挂从fopen()到open()的libc内部调用。

类似的警告适用于strace-也有“
openat”系统调用,并且根据您的体系结构,可能还会有其他旧系统调用。但是不如LD_PRELOAD挂钩那么多,因此,如果您不介意性能下降,它可能是一个更简单的选择。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号