forked from busytex/busytex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_file_access.c
47 lines (37 loc) · 1.29 KB
/
log_file_access.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#ifdef LOGFILEACCESSSTATIC
FILE* orig_fopen(const char *path, const char *mode);
FILE* fopen(const char *path, const char *mode)
{
fprintf(stderr, "log_file_access: fopen(\"%s\", \"%s\")\n", path, mode);
return orig_fopen(path, mode);
}
int orig_open(const char *path, int mode);
int open(const char *path, int mode)
{
fprintf(stderr, "log_file_access: open(\"%s\", %d)\n", path, mode);
return orig_open(path, mode);
}
#endif
#ifdef LOGFILEACCESSDYNAMIC
// gcc -shared -fPIC log_file_access.c -o log_file_access.so -ldl
#include <unistd.h>
#include <errno.h>
#include <dlfcn.h>
FILE* fopen(const char *path, const char *mode)
{
fprintf(stderr, "log_file_access_preload: fopen(\"%s\", \"%s\")\n", path, mode);
typedef FILE* (*orig_fopen_func_type)(const char *path, const char *mode);
orig_fopen_func_type orig_func = (orig_fopen_func_type)dlsym(RTLD_NEXT, "fopen");
return orig_func(path, mode);
}
int open(const char *path, int flags)
{
fprintf(stderr, "log_file_access_preload: open(\"%s\", %d)\n", path, flags);
typedef int (*orig_open_func_type)(const char *pathname, int flags);
orig_open_func_type orig_func = (orig_open_func_type)dlsym(RTLD_NEXT, "open");
return orig_func(path, flags);
}
#endif