Skip to content

Instantly share code, notes, and snippets.

@rwngwn
Last active September 28, 2015 12:51
Show Gist options
  • Save rwngwn/e9aa2cc63710e755b035 to your computer and use it in GitHub Desktop.
Save rwngwn/e9aa2cc63710e755b035 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <dlfcn.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
static int (*real_open)(const char* pathname, int flags, ...) = NULL;
// we need to hook __lxstat function because of some glibx stat calls magic
static int (*real_lxstat)(int ver, const char *pathname, struct stat *buf) = NULL;
static int (*real_xstat)(int ver, const char *pathname, struct stat *buf) = NULL;
static int (*real_chdir)(const char *path) = NULL;
int open(const char *pathname, int flags, ...)
{
int fd;
real_open = dlsym(RTLD_NEXT, "open");
if (strcmp(pathname, "/xxx") == 0){
FILE * tmpfile = NULL;
//FIXME - we are opening tempfile insted of real one to lets kernel give use proper file descriptor
char * tmpname;
tmpname = tmpnam(NULL);
tmpfile = fopen(tmpname, "w");
fprintf(tmpfile, "TEST TEXTaaaaaaaaaaaaaaaaaaaaaa\n");
fclose (tmpfile);
fd = real_open(tmpname, flags);
return fd;
}
return real_open(pathname,flags);
}
int __xstat(int ver, const char *pathname, struct stat *buf)
{
real_xstat = dlsym(RTLD_NEXT, "__xstat");
if (strcmp(pathname, "/xxx") == 0)
{
buf->st_mode = 0555;
return 0;
}
int rc = real_xstat(ver, pathname, buf);
return rc;
}
int __lxstat(int ver, const char *pathname, struct stat *buf)
{
real_lxstat = dlsym(RTLD_NEXT, "__lxstat");
if (strcmp(pathname, "/xxx") == 0)
{
buf->st_mode = 0555;
return 0;
}
int rc = real_lxstat(ver, pathname, buf);
return rc;
}
int chdir(const char *path) {
real_chdir = dlsym(RTLD_NEXT, "chdir");
if (strcmp(path, "/xxx") == 0)
return 0;
int rc = real_chdir(path);
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment