Skip to content

Instantly share code, notes, and snippets.

@scoates
Created August 14, 2016 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scoates/e3883858ab3ea0d6e8a21ec7df0d9dc7 to your computer and use it in GitHub Desktop.
Save scoates/e3883858ab3ea0d6e8a21ec7df0d9dc7 to your computer and use it in GitHub Desktop.
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