I am trying to use LD_PRELOAD to intercept calls to open/close. My test implementation of close() is just
int close(int fd) {
fprintf(stderr, "TEST\n");
return syscall(SYS_close, fd);
}
and I have a test program that does
int handle=open("test.txt", O_WRONLY|O_CREAT, 0644);
close(handle);
FILE *f = fopen("test.txt", "w");
fclose(f);
The problem is: my LD_PRELOAD code successfully intercepted the close() call, but could not intercept the close() syscall made from fclose(), although from strace output the two syscall's look exactly the same.
I know I can just write my interception of fclose(), and it also worked for C++'s ofstream::close(). However, my interceptions for open()/fopen() still don't work for C++'s ofstream::ofstream(). How can I make sure I intercept all calls to open or close a file, no matter which wrapper function calls them? Thanks!