diff options
| author | eroen | 2015-07-13 00:08:15 +0200 |
|---|---|---|
| committer | eroen | 2015-07-13 00:11:05 +0200 |
| commit | 6b97d2952aca4692c7d1464d9ac901fef8b52ea9 (patch) | |
| tree | c8415496d839b3a560c64c2b281b957e925be389 | |
| parent | 1ff4e0626696cce79347cea1f8af03ff0df5c3fd (diff) | |
| download | dynsymlink-6b97d2952aca4692c7d1464d9ac901fef8b52ea9.tar.gz dynsymlink-6b97d2952aca4692c7d1464d9ac901fef8b52ea9.tar.bz2 dynsymlink-6b97d2952aca4692c7d1464d9ac901fef8b52ea9.tar.xz | |
| -rw-r--r-- | Makefile | 23 | ||||
| -rw-r--r-- | libdynsymlink.c | 75 | ||||
| -rw-r--r-- | test3-a.c | 25 | ||||
| -rw-r--r-- | test3-b.c | 25 | ||||
| -rw-r--r-- | test3.bash | 24 |
5 files changed, 152 insertions, 20 deletions
@@ -2,28 +2,21 @@ all: libdynsymlink.so PHONY: all libdynsymlink.so: libdynsymlink.o - clang -o libdynsymlink.so --shared libdynsymlink.o -ldl - -test1: test1.o libdynsymlink.so - clang -o test1 test1.o libdynsymlink.so - -test2: test2.o - clang -o test2 test2.o + clang $(CFLAGS) -o libdynsymlink.so --shared libdynsymlink.o -ldl libdynsymlink.o: libdynsymlink.c - clang -o libdynsymlink.o -fPIC -c libdynsymlink.c - -test1.o: test1.c - clang -o test1.o -c test1.c + clang $(CFLAGS) -o libdynsymlink.o -fPIC -c libdynsymlink.c -test2.o: test2.c - clang -o test2.o -c test2.c +test1: libdynsymlink.so +test1: LDFLAGS = libdynsymlink.so -check: test1 test2 libdynsymlink.so +check: test1 test2 test3.bash test3-a test3-b libdynsymlink.so LD_LIBRARY_PATH=. ./test1 LD_PRELOAD=./libdynsymlink.so ./test2 + bash test3.bash PHONY: check clean: - +rm -f test1 test2 test1.o test2.o libdynsymlink.so libdynsymlink.o + +rm -f libdynsymlink.so libdynsymlink.o + +rm -f test1 test2 test1.o test2.o test3-a test3-b PHONY: clean diff --git a/libdynsymlink.c b/libdynsymlink.c index f1c225c..cd86478 100644 --- a/libdynsymlink.c +++ b/libdynsymlink.c @@ -2,22 +2,87 @@ #include <dlfcn.h> #include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <libgen.h> static int dynsym_initialized = 0; -static int (*real_printf) (const char *); +static int (*real_open) (const char *, int flags); void dynsym_init(void) { if(!dynsym_initialized) { - real_printf = dlsym(RTLD_NEXT, "printf"); + real_open = dlsym(RTLD_NEXT, "open"); dynsym_initialized = 1; } } -int printf(const char *__restrict __format, ...) +int open(const char *pathname, int flags, ...) { dynsym_init(); - (*real_printf)("This is libdynsymlink printf\n"); - return (*real_printf)(__format); + printf("This is libdynsymlink open\n"); + + int ret; + int errsv; + char prefix[] = "prefixdir/"; + int newpathlen = strlen(pathname) + strlen(prefix); + char *newpathname = malloc(newpathlen); + newpathname = strcpy(newpathname, prefix); + newpathname = strcat(newpathname, pathname); + + printf("Prefix: %s\n", prefix); + printf("Original pathname: %s\n", pathname); + printf("Amended pathname: %s\n", newpathname); + + // 1: prefix + path + ret = (*real_open)(newpathname, flags); + + if(ret < 0) { + errsv = errno; + perror("real_open with prefix failed: "); + + // 2: path + ret = (*real_open)(pathname, flags); + if(ret < 0) { + errsv = errno; + perror("real_open without prefix failed: "); + + // 3: create prefix + path + printf("TODO: attempt copying file to prefix\n"); + + char *newdir = strdup(newpathname); + newdir = dirname(newdir); + + // TODO: + char *mkdircmd = malloc(256); + sprintf(mkdircmd, "mkdir -p %s", newdir); + printf("command: %s\n", mkdircmd); + system(mkdircmd); + + // TODO: + char *cpcmd = malloc(256); + sprintf(cpcmd, "cp %s %s", pathname, newpathname); + printf("command: %s\n", cpcmd); + system(cpcmd); + + // TODO: + chmod(newpathname, 00600); + + ret = (*real_open)(newpathname, flags); + if(ret < 0) { + errsv = errno; + perror("real_open of copy failed: "); + } + } + } + + free(newpathname); + + errno = errsv; + return ret; } diff --git a/test3-a.c b/test3-a.c new file mode 100644 index 0000000..632967f --- /dev/null +++ b/test3-a.c @@ -0,0 +1,25 @@ +/* + * test3 + */ + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> + +int main(int argc, char *argv[]) +{ + printf("This is test3-a\n"); + int f; + f = open(argv[1], O_RDONLY); + if(f < 0) { + perror(""); + return 1; + } + if(close(f) != 0) { + perror(""); + return 1; + } + return 0; +} diff --git a/test3-b.c b/test3-b.c new file mode 100644 index 0000000..b4cf576 --- /dev/null +++ b/test3-b.c @@ -0,0 +1,25 @@ +/* + * test3 + */ + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> + +int main(int argc, char *argv[]) +{ + printf("This is test3-b\n"); + int f; + f = open(argv[1], O_WRONLY); + if(f < 0) { + perror(""); + return 1; + } + if(close(f) != 0) { + perror(""); + return 1; + } + return 0; +} diff --git a/test3.bash b/test3.bash new file mode 100644 index 0000000..d136732 --- /dev/null +++ b/test3.bash @@ -0,0 +1,24 @@ +#!/bin/bash + +TDIR=testdir-test3 +[[ -d testdir-test3 ]] || mkdir "$TDIR" +chmod +rwx "$TDIR" + +touch "$TDIR/rwfile" +chmod +rw "$TDIR/rwfile" +./test3-a "$TDIR/rwfile" || exit 1 +./test3-b "$TDIR/rwfile" || exit 1 +LD_PRELOAD=./libdynsymlink.so ./test3-a "$TDIR/rwfile" || exit 1 +LD_PRELOAD=./libdynsymlink.so ./test3-b "$TDIR/rwfile" || exit 1 +rm -f "$TDIR/rwfile" + +touch "$TDIR/rofile" +chmod +r "$TDIR/rofile" +chmod -w "$TDIR/rofile" +./test3-a "$TDIR/rofile" || exit 2 +./test3-b "$TDIR/rofile" && exit 2 +LD_PRELOAD=./libdynsymlink.so ./test3-a "$TDIR/rofile" || exit 2 +LD_PRELOAD=./libdynsymlink.so ./test3-b "$TDIR/rofile" || exit 2 +rm -f "$TDIR/rofile" + +rmdir "$TDIR" |
