Initial commit.
[hondza-y36pr2.git] / fadvise / fadvise.c
blobfa568fdd1c2a63bdf610c8cceb1771f5751c1e51
1 /******************************************************************************
2 * fadvise.c
3 *****************************************************************************/
5 #define _GNU_SOURCE
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdarg.h>
13 #include <dlfcn.h>
15 #include "fadvise.h"
17 static real_close_t real_close;
18 static real_write_t real_write;
19 static real_read_t real_read;
21 static real_fclose_t real_fclose;
22 static real_fwrite_t real_fwrite;
23 static real_fread_t real_fread;
24 static real_fwrite_t real_fwrite_unlocked;
25 static real_fread_t real_fread_unlocked;
27 static pid_t mypid;
29 static int fadvise_init_done=0;
30 static int fadvise_debug=0;
31 static int fadvise_advice=POSIX_FADV_DONTNEED;
33 void do_log(char *str, ...)
35 va_list arglist;
37 va_start(arglist, str);
38 vfprintf(stderr, str, arglist);
39 fflush(stderr);
40 va_end(arglist);
41 } /* do_log() */
45 static void lib_init(void)
47 char *temp;
49 if(fadvise_init_done) return;
50 fadvise_init_done=1;
52 *(void **) (&real_close) = dlsym(RTLD_NEXT, "close");
53 if(!real_close)
55 fprintf(stderr, "(fadvise) Cannot load symbol 'close' %s\n", dlerror());
56 _exit(1);
58 *(void **) (&real_write) = dlsym(RTLD_NEXT, "write");
59 if(!real_write)
61 fprintf(stderr, "(fadvise) Cannot load symbol 'write' %s\n", dlerror());
62 _exit(1);
64 *(void **) (&real_read) = dlsym(RTLD_NEXT, "read");
65 if(!real_read)
67 fprintf(stderr, "(fadvise) Cannot load symbol 'read' %s\n", dlerror());
68 _exit(1);
70 *(void **) (&real_fclose) = dlsym(RTLD_NEXT, "fclose");
71 if(!real_fclose)
73 fprintf(stderr, "(fadvise) Cannot load symbol 'fclose' %s\n", dlerror());
74 _exit(1);
76 *(void **) (&real_fwrite) = dlsym(RTLD_NEXT, "fwrite");
77 if(!real_fwrite)
79 fprintf(stderr, "(fadvise) Cannot load symbol 'fwrite' %s\n", dlerror());
80 _exit(1);
82 *(void **) (&real_fread) = dlsym(RTLD_NEXT, "fread");
83 if(!real_fread)
85 fprintf(stderr, "(fadvise) Cannot load symbol 'fread' %s\n", dlerror());
86 _exit(1);
88 *(void **) (&real_fwrite_unlocked) = dlsym(RTLD_NEXT, "fwrite_unlocked");
89 if(!real_fwrite_unlocked)
91 fprintf(stderr, "(fadvise) Cannot load symbol 'fwrite_unlocked' %s\n", dlerror());
92 _exit(1);
94 *(void **) (&real_fread_unlocked) = dlsym(RTLD_NEXT, "fread_unlocked");
95 if(!real_fread_unlocked)
97 fprintf(stderr, "(fadvise) Cannot load symbol 'fread_unlocked' %s\n", dlerror());
98 _exit(1);
101 temp = getenv("FADVISE_ADVICE"); if(!temp) temp = getenv("FADV");
102 if(temp)
104 if(strcasestr(temp, "NORM")) fadvise_advice = POSIX_FADV_NORMAL;
105 else if(strcasestr(temp, "SEQ")) fadvise_advice = POSIX_FADV_SEQUENTIAL;
106 else if(strcasestr(temp, "RAND")) fadvise_advice = POSIX_FADV_RANDOM;
107 else if(strcasestr(temp, "NORE")) fadvise_advice = POSIX_FADV_NOREUSE;
108 else if(strcasestr(temp, "WILL")) fadvise_advice = POSIX_FADV_WILLNEED;
109 else if(strcasestr(temp, "DONT")) fadvise_advice = POSIX_FADV_DONTNEED;
112 #ifndef NDEBUG
113 temp = getenv("FADVISE_DEBUG"); if(!temp) temp = getenv("FADD");
114 if(temp) fadvise_debug = strtoul(temp, NULL, 10);
115 #endif /* NDEBUG */
118 } /* lib_init() */
122 ssize_t write(int fd, const void *buf, size_t count)
124 ssize_t ret;
125 off_t pos;
127 DEBUG("write(%d, ,%u)\n", fd, count);
128 if(!fadvise_init_done) lib_init();
130 ret = real_write(fd, buf, count);
132 if(ret > 0)
134 pos = lseek(fd, 0, SEEK_CUR);
135 if((off_t)-1 == pos) pos = 0;
136 posix_fadvise(fd, 0, pos, fadvise_advice);
139 return ret;
144 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
146 size_t ret;
147 off_t pos;
149 DEBUG("fwrite(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1);
150 if(!fadvise_init_done) lib_init();
152 ret = real_fwrite(ptr, size, nmemb, stream);
154 if(ret > 0)
156 pos = (long) ftell(stream);
157 if(-1 == pos) pos = 0;
158 posix_fadvise(fileno(stream), 0, pos, fadvise_advice);
161 return ret;
166 size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *stream)
168 size_t ret;
169 off_t pos;
171 DEBUG("fwrite_unlocked(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1);
172 if(!fadvise_init_done) lib_init();
174 ret = real_fwrite_unlocked(ptr, size, nmemb, stream);
176 if(ret > 0)
178 pos = (long) ftell(stream);
179 if(-1 == pos) pos = 0;
180 posix_fadvise(fileno(stream), 0, pos, fadvise_advice);
183 return ret;
188 ssize_t read(int fd, void *buf, size_t count)
190 ssize_t ret;
191 off_t pos;
193 DEBUG("read(%d, ,%u)\n", fd, count);
194 if(!fadvise_init_done) lib_init();
196 ret = real_read(fd, buf, count);
198 if(ret > 0)
200 pos = lseek(fd, 0, SEEK_CUR);
201 if((off_t)-1 == pos) pos = 0;
202 posix_fadvise(fd, 0, pos, fadvise_advice);
205 return ret;
210 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
212 size_t ret;
213 off_t pos;
215 DEBUG("fread(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1);
216 if(!fadvise_init_done) lib_init();
218 ret = real_fread(ptr, size, nmemb, stream);
220 if(ret > 0)
222 pos = (long) ftell(stream);
223 if(-1 == pos) pos = 0;
224 posix_fadvise(fileno(stream), 0, pos, fadvise_advice);
227 return ret;
232 size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream)
234 size_t ret;
235 off_t pos;
237 DEBUG("fread_unlocked(, %u, %u, %d)\n", size, nmemb, stream ? fileno(stream) : -1);
238 if(!fadvise_init_done) lib_init();
240 ret = real_fread_unlocked(ptr, size, nmemb, stream);
242 if(ret > 0)
244 pos = (long) ftell(stream);
245 if(-1 == pos) pos = 0;
246 posix_fadvise(fileno(stream), 0, pos, fadvise_advice);
249 return ret;
255 int close(int fd)
257 off_t pos;
259 DEBUG("close(%d)\n", fd);
260 if(!fadvise_init_done) lib_init();
262 pos = lseek(fd, 0, SEEK_CUR);
263 if((off_t)-1 == pos) pos = 0;
264 posix_fadvise(fd, 0, pos, fadvise_advice);
265 return real_close(fd);
270 int fclose(FILE *fp)
272 off_t pos;
274 DEBUG("fclose(%d)\n", fp ? fileno(fp) : -1);
275 if(!fadvise_init_done) lib_init();
277 pos = (long) ftell(fp);
278 if(-1 == pos) pos = 0;
279 posix_fadvise(fileno(fp), 0, pos, fadvise_advice);
280 return real_fclose(fp);