Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Last active August 29, 2015 14:23
Show Gist options
  • Save NicolasT/4304932455f94d7b64b9 to your computer and use it in GitHub Desktop.
Save NicolasT/4304932455f94d7b64b9 to your computer and use it in GitHub Desktop.
preload-statfs.so
test-preload-statfs
CFLAGS := -Wall -Wextra -O2 -pipe
BINS := preload-statfs.so test-preload-statfs
default: all
all: $(BINS)
preload-statfs.so: preload-statfs.c
$(CC) -o $@ $(CFLAGS) -shared -fPIC -ldl $<
test-preload-statfs: test-preload-statfs.c
$(CC) -o $@ $(CFLAGS) $<
clean:
rm -f preload-statfs.so test-preload-statfs
.PHONY: clean
check: $(BINS)
./test-preload-statfs /
./test-preload-statfs /tmp
if ./test-preload-statfs /thisshouldnotexist; then false; else true; fi
if LD_PRELOAD=./preload-statfs.so ./test-preload-statfs /; then false; else true; fi
LD_PRELOAD=./preload-statfs.so ./test-preload-statfs /tmp
if LD_PRELOAD=./preload-statfs.so ./test-preload-statfs /thisshouldnotexist; then false; else true; fi
.PHONY: check
#define _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include <dlfcn.h>
#include <string.h>
#include <sys/vfs.h>
typedef int (*statfs_proto)(const char *path, struct statfs *buf);
static statfs_proto real_statfs = NULL;
int statfs(const char *path, struct statfs *buf) {
if(real_statfs == NULL) {
real_statfs = (statfs_proto)dlsym(RTLD_NEXT, "statfs");
}
if(strlen(path) == 1 && path[0] == '/') {
/* Emulate error: "The filesystem does not support this call." */
errno = ENOSYS;
return -1;
}
else {
return real_statfs(path, buf);
}
}
#include <stdio.h>
#include <sys/vfs.h>
int main(int argc, char **argv) {
struct statfs buf = { 0 };
int res = 0;
if(argc != 2) {
fprintf(stderr, "Required argument\n");
return 1;
}
res = statfs(argv[1], &buf);
if(res < 0) {
perror("statfs");
return 1;
}
printf("Success!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment