Skip to content

Instantly share code, notes, and snippets.



Created Aug 14, 2016
What would you like to do?
Hook into libc's open
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <stdarg.h>
int open(const char* pathname, int flags, ...)
int (*libc_open)(const char*, int, ...);
printf("In our own open, opening %s\n", pathname);
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
libc_open = dlsym(RTLD_NEXT, "open");
return (*libc_open)(pathname,flags,mode);
build with: gcc -Wall -fPIC -shared -o loudopen.c -ldl
run with: LD_PRELOAD=./ cmd args
$ echo "foo">bar.txt
$ LD_PRELOAD=./ cat bar.txt
In our own open, opening bar.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.