Skip to content

Instantly share code, notes, and snippets.

@scoates scoates/loudopen.c
Created Aug 14, 2016

Embed
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);
va_end(ap);
libc_open = dlsym(RTLD_NEXT, "open");
return (*libc_open)(pathname,flags,mode);
}
/*
build with: gcc -Wall -fPIC -shared -o loudopen.so loudopen.c -ldl
run with: LD_PRELOAD=./loudopen.so cmd args
$ echo "foo">bar.txt
$ LD_PRELOAD=./loudopen.so cat bar.txt
In our own open, opening bar.txt
foo
*/
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.