fixes for Power64
[tridge/junkcode.git] / preload_notes.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <pwd.h>
5 #include <sys/types.h>
6 #include <dlfcn.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdarg.h>
10 #include <errno.h>
11 #include <stdlib.h>
12
13 #if 0
14 #define LOG_NAME "/tmp/notes.log"
15 #endif
16
17 static void xlogit(const char *fmt, ...)
18 {
19 #ifdef LOG_NAME
20         static int logfd = -1;
21         va_list ap;
22         static int (*open_orig)(const char *, int, mode_t);
23
24         if (!open_orig) {
25                 open_orig = dlsym(RTLD_NEXT, "open");
26         }
27
28         if (logfd == -1) {
29                 logfd = open_orig(LOG_NAME, O_WRONLY|O_APPEND|O_CREAT, 0666);
30         }
31         va_start(ap, fmt);
32         vdprintf(logfd, fmt, ap);
33         va_end(ap);
34 #endif
35 }
36
37
38 static const char *redirect_name(const char *filename)
39 {
40         static char buf[1024];
41         static int (*access_orig)(const char *, int );
42         static const char *lroot;
43         if (lroot == NULL) {
44                 lroot = getenv("LROOT");
45         }
46         if (lroot == NULL) {
47                 xlogit("You must set LROOT\n");
48                 abort();
49         }
50         if (filename[0] != '/' || strncmp(filename, lroot, strlen(lroot)) == 0) {
51                 return filename;
52         }
53         if (!access_orig) {
54                 access_orig = dlsym(RTLD_NEXT, "access");
55         }
56         snprintf(buf, sizeof(buf), "%s%s", lroot, filename);
57         if (access_orig(buf, F_OK) == -1) {
58                 return filename;
59         }
60         
61         xlogit("mapped '%s' to '%s'\n", filename, buf);
62
63         return buf;
64 }
65
66 int setenv(const char *name, const char *value, int overwrite)
67 {
68         static int (*setenv_orig)(const char *, const char *, int);
69         int ret;
70
71         if (!setenv_orig) {
72                 setenv_orig = dlsym(RTLD_NEXT, "setenv");
73         }
74
75         if (strcmp("LD_PRELOAD", name) == 0) {
76                 xlogit("skipping LD_PRELOAD of '%s'\n", value);
77                 return 0;
78         }
79
80 #if 0
81         if (strcmp("LD_LIBRARY_PATH", name) == 0) {
82                 xlogit("skipping LD_LIBRARY_PATH of '%s'\n", value);
83                 return 0;
84         }
85 #endif
86
87         ret = setenv_orig(name, value, overwrite);
88
89         xlogit("setenv(\"%s\", \"%s\", %d) -> %d\n", 
90                 name, value, overwrite, ret);
91
92         return ret;
93 }
94
95 void *dlopen(const char *filename, int flag)
96 {
97         static void *(*dlopen_orig)(const char *, int);
98         void *ret;
99
100         if (!dlopen_orig) {
101                 dlopen_orig = dlsym(RTLD_NEXT, "dlopen");
102         }
103
104         ret = dlopen_orig(redirect_name(filename), flag);
105
106         xlogit("dlopen(\"%s\", 0x%x) -> %p\n", filename, flag, ret);
107
108         return ret;     
109 }
110
111
112 int open(const char *filename, int flags, ...)
113 {
114         static int (*open_orig)(const char *, int, mode_t);
115         int ret;
116         va_list ap;
117         mode_t mode;
118
119         if (!open_orig) {
120                 open_orig = dlsym(RTLD_NEXT, "open");
121         }
122
123         va_start(ap, flags);
124         mode = va_arg(ap, mode_t);
125         va_end(ap);
126
127         ret = open_orig(redirect_name(filename), flags, mode);
128
129         xlogit("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret);
130
131         return ret;     
132 }
133
134 int stat(const char *filename, struct stat *st)
135 {
136         static int (*stat_orig)(const char *, struct stat *);
137         int ret;
138
139         if (!stat_orig) {
140                 stat_orig = dlsym(RTLD_NEXT, "stat");
141         }
142
143         ret = stat_orig(redirect_name(filename), st);
144
145         xlogit("stat(\"%s\") -> %d\n", filename, ret);
146
147         return ret;     
148 }
149
150 int stat64(const char *filename, struct stat64 *st)
151 {
152         static int (*stat64_orig)(const char *, struct stat64 *);
153         int ret;
154
155         if (!stat64_orig) {
156                 stat64_orig = dlsym(RTLD_NEXT, "stat64");
157         }
158
159         ret = stat64_orig(redirect_name(filename), st);
160
161         xlogit("stat64(\"%s\") -> %d\n", filename, ret);
162
163         return ret;     
164 }
165
166 FILE *fopen(const char *filename, const char *mode)
167 {
168         static FILE *(*fopen_orig)(const char *, const char *);
169         FILE *ret;
170
171         if (!fopen_orig) {
172                 fopen_orig = dlsym(RTLD_NEXT, "fopen");
173         }
174
175         ret = fopen_orig(redirect_name(filename), mode);
176
177         xlogit("fopen(\"%s\", \"%s\") -> %p\n", filename, mode, ret);
178
179         return ret;     
180 }
181
182 int __xstat(int x, const char *filename, struct stat *st)
183 {
184         static int (*__xstat_orig)(int x, const char *, struct stat *);
185         int ret;
186
187         if (!__xstat_orig) {
188                 __xstat_orig = dlsym(RTLD_NEXT, "__xstat");
189         }
190
191         ret = __xstat_orig(x, redirect_name(filename), st);
192
193         xlogit("__xstat(\"%s\") -> %d\n", filename, ret);
194
195         return ret;     
196 }
197
198 int access(const char *filename, int x)
199 {
200         static int (*access_orig)(const char *, int );
201         int ret;
202
203         if (!access_orig) {
204                 access_orig = dlsym(RTLD_NEXT, "access");
205         }
206
207         ret = access_orig(redirect_name(filename), x);
208
209         xlogit("access(\"%s\") -> %d\n", filename, ret);
210
211         return ret;     
212 }
213