14 #define LOG_NAME "/tmp/notes.log"
17 static void xlogit(const char *fmt, ...)
20 static int logfd = -1;
22 static int (*open_orig)(const char *, int, mode_t);
25 open_orig = dlsym(RTLD_NEXT, "open");
29 logfd = open_orig(LOG_NAME, O_WRONLY|O_APPEND|O_CREAT, 0666);
32 vdprintf(logfd, fmt, ap);
38 static const char *redirect_name(const char *filename)
40 static char buf[1024];
41 static int (*access_orig)(const char *, int );
42 static const char *lroot;
44 lroot = getenv("LROOT");
47 xlogit("You must set LROOT\n");
50 if (filename[0] != '/' || strncmp(filename, lroot, strlen(lroot)) == 0) {
54 access_orig = dlsym(RTLD_NEXT, "access");
56 snprintf(buf, sizeof(buf), "%s%s", lroot, filename);
57 if (access_orig(buf, F_OK) == -1) {
61 xlogit("mapped '%s' to '%s'\n", filename, buf);
66 int setenv(const char *name, const char *value, int overwrite)
68 static int (*setenv_orig)(const char *, const char *, int);
72 setenv_orig = dlsym(RTLD_NEXT, "setenv");
75 if (strcmp("LD_PRELOAD", name) == 0) {
76 xlogit("skipping LD_PRELOAD of '%s'\n", value);
81 if (strcmp("LD_LIBRARY_PATH", name) == 0) {
82 xlogit("skipping LD_LIBRARY_PATH of '%s'\n", value);
87 ret = setenv_orig(name, value, overwrite);
89 xlogit("setenv(\"%s\", \"%s\", %d) -> %d\n",
90 name, value, overwrite, ret);
95 void *dlopen(const char *filename, int flag)
97 static void *(*dlopen_orig)(const char *, int);
101 dlopen_orig = dlsym(RTLD_NEXT, "dlopen");
104 ret = dlopen_orig(redirect_name(filename), flag);
106 xlogit("dlopen(\"%s\", 0x%x) -> %p\n", filename, flag, ret);
112 int open(const char *filename, int flags, ...)
114 static int (*open_orig)(const char *, int, mode_t);
120 open_orig = dlsym(RTLD_NEXT, "open");
124 mode = va_arg(ap, mode_t);
127 ret = open_orig(redirect_name(filename), flags, mode);
129 xlogit("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret);
134 int stat(const char *filename, struct stat *st)
136 static int (*stat_orig)(const char *, struct stat *);
140 stat_orig = dlsym(RTLD_NEXT, "stat");
143 ret = stat_orig(redirect_name(filename), st);
145 xlogit("stat(\"%s\") -> %d\n", filename, ret);
150 int stat64(const char *filename, struct stat64 *st)
152 static int (*stat64_orig)(const char *, struct stat64 *);
156 stat64_orig = dlsym(RTLD_NEXT, "stat64");
159 ret = stat64_orig(redirect_name(filename), st);
161 xlogit("stat64(\"%s\") -> %d\n", filename, ret);
166 FILE *fopen(const char *filename, const char *mode)
168 static FILE *(*fopen_orig)(const char *, const char *);
172 fopen_orig = dlsym(RTLD_NEXT, "fopen");
175 ret = fopen_orig(redirect_name(filename), mode);
177 xlogit("fopen(\"%s\", \"%s\") -> %p\n", filename, mode, ret);
182 int __xstat(int x, const char *filename, struct stat *st)
184 static int (*__xstat_orig)(int x, const char *, struct stat *);
188 __xstat_orig = dlsym(RTLD_NEXT, "__xstat");
191 ret = __xstat_orig(x, redirect_name(filename), st);
193 xlogit("__xstat(\"%s\") -> %d\n", filename, ret);
198 int access(const char *filename, int x)
200 static int (*access_orig)(const char *, int );
204 access_orig = dlsym(RTLD_NEXT, "access");
207 ret = access_orig(redirect_name(filename), x);
209 xlogit("access(\"%s\") -> %d\n", filename, ret);