From b01bace33b5c731c318fac9eccdee8a12c0a1851 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Mon, 11 May 2020 21:26:05 -0400 Subject: [PATCH 01/14] First test suitet --- .travis.yml | 19 +++++++++++++++++++ tests/setup.bash | 5 +++++ tests/tests.bats | 40 ++++++++++++++++++++++++++++++++++++++++ tests/unset.bash | 1 + 4 files changed, 65 insertions(+) create mode 100644 .travis.yml create mode 100644 tests/setup.bash create mode 100644 tests/tests.bats create mode 100644 tests/unset.bash diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..e37b20d8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +dist: xenial +language: C +os: linux +compiler: gcc + +before_install: +- sudo apt-get update -qq +- sudo apt-get install -qq bats + +install: +- make + +script: +- bats tests/tests.bats + +after_success: +- coveralls + +deploy: diff --git a/tests/setup.bash b/tests/setup.bash new file mode 100644 index 00000000..d828fed5 --- /dev/null +++ b/tests/setup.bash @@ -0,0 +1,5 @@ +\rm -Rf ${SOURCE} ${MOUNT} # Clean up from previous tests +mkdir -p ${SOURCE} ${MOUNT} # Create source and mount (defined in tests.bats) +echo ${SOURCE} > sources.txt +echo a > a.txt # This file will be used in tests +export LD_PRELOAD=passthrough.so \ No newline at end of file diff --git a/tests/tests.bats b/tests/tests.bats new file mode 100644 index 00000000..5ae56292 --- /dev/null +++ b/tests/tests.bats @@ -0,0 +1,40 @@ +#!/usr/bin/env bats + +# Source and mount are +# cleaned up in setup +SOURCE="source" +MOUNT="mount" + +@test "ls" { + load setup + ls +} + +@test "mkdir" { + load setup + mkdir ${MOUNT}/dir + load unset + test -d ${SOURCE}/dir +} + +@test "cp" { + load setup + cp a.txt ${MOUNT} + load unset + test -f ${SOURCE}/a.txt +} + +@test "mv" { + load setup + mv a.txt ${MOUNT} + load unset + test -f ${SOURCE}/a.txt + [ ! -f a.txt ] +} + +@test "dd" { + load setup + dd if=/dev/random of=${MOUNT}/file count=3 + load unset + test -f ${SOURCE}/file +} \ No newline at end of file diff --git a/tests/unset.bash b/tests/unset.bash new file mode 100644 index 00000000..4de1f9e1 --- /dev/null +++ b/tests/unset.bash @@ -0,0 +1 @@ +unset LD_PRELOAD \ No newline at end of file From e1c4c33cbd34fa2bfd792c5fe758922630dc4fe5 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Mon, 11 May 2020 21:39:03 -0400 Subject: [PATCH 02/14] Fixed LD_LIBRARY_PATH --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e37b20d8..2fb54136 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ install: - make script: -- bats tests/tests.bats +- LD_LIBRARY_PATH=$PWD bats tests/tests.bats after_success: - coveralls From fd48c366bab886c0ea84e8d91428dc1136e21373 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Mon, 11 May 2020 22:02:34 -0400 Subject: [PATCH 03/14] Updates to make tests work on Ubuntu --- Makefile | 2 +- tests/setup.bash | 3 ++- tests/tests.bats | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e46ddaae..de9abfcc 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,6 @@ all: passthrough.so passthrough.o: passthrough.cpp passthrough.h gcc -Wall -fPIC -c -ggdb passthrough.cpp passthrough.so: passthrough.o - gcc -shared -o passthrough.so passthrough.o -ldl -lpthread -ggdb + gcc -shared -o passthrough.so passthrough.o -ldl -lpthread -ggdb -lstdc++ clean: rm passthrough.o passthrough.so diff --git a/tests/setup.bash b/tests/setup.bash index d828fed5..77f011bf 100644 --- a/tests/setup.bash +++ b/tests/setup.bash @@ -1,5 +1,6 @@ +unset LD_PRELOAD # Clean up from previous tests \rm -Rf ${SOURCE} ${MOUNT} # Clean up from previous tests mkdir -p ${SOURCE} ${MOUNT} # Create source and mount (defined in tests.bats) echo ${SOURCE} > sources.txt echo a > a.txt # This file will be used in tests -export LD_PRELOAD=passthrough.so \ No newline at end of file +export LD_PRELOAD=${PWD}/passthrough.so \ No newline at end of file diff --git a/tests/tests.bats b/tests/tests.bats index 5ae56292..9fde7212 100644 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -8,6 +8,7 @@ MOUNT="mount" @test "ls" { load setup ls + load unset } @test "mkdir" { From f7286f981a912fe97f0f832540b8fb3cc94431b4 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Mon, 11 May 2020 22:04:16 -0400 Subject: [PATCH 04/14] Improved bats output --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2fb54136..df10b51b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ install: - make script: -- LD_LIBRARY_PATH=$PWD bats tests/tests.bats +- bats -p tests/tests.bats after_success: - coveralls From ef2e9f09d48b9e6d462f3ade669170fce9b0011c Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Mon, 11 May 2020 22:07:15 -0400 Subject: [PATCH 05/14] Revert bats output --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index df10b51b..e37b20d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ install: - make script: -- bats -p tests/tests.bats +- bats tests/tests.bats after_success: - coveralls From 7446c3946e001489538f4c804eb2d3f631874ef8 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Mon, 11 May 2020 22:22:25 -0400 Subject: [PATCH 06/14] Added debug option --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index de9abfcc..e1b8ad01 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: passthrough.so passthrough.o: passthrough.cpp passthrough.h - gcc -Wall -fPIC -c -ggdb passthrough.cpp + gcc -Wall -fPIC -g -c -ggdb passthrough.cpp passthrough.so: passthrough.o - gcc -shared -o passthrough.so passthrough.o -ldl -lpthread -ggdb -lstdc++ + gcc -shared -g -o passthrough.so passthrough.o -ldl -lpthread -ggdb -lstdc++ clean: rm passthrough.o passthrough.so From c68079e226b3753f20cee906f486ec8e18e5d73d Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Mon, 11 May 2020 22:47:23 -0400 Subject: [PATCH 07/14] Fixed source (now using info read from sources.txt), removed check_path_exist from get_path --- passthrough.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/passthrough.cpp b/passthrough.cpp index 68cc7b8b..f4b57e06 100644 --- a/passthrough.cpp +++ b/passthrough.cpp @@ -74,7 +74,6 @@ void* libattr_fsetxattr; static const char* relmount = "./mount"; static char mount_dir[PATH_MAX]; -static const char* source = "/dev/shm"; static const char* source_file = "./sources.txt"; static char source_mounts[MAX_MOUNTS][PATH_MAX]; @@ -156,7 +155,7 @@ static void init_sources(){ int len = strlen(source_mounts[i]); if (len > 0 && source_mounts[i][len-1] == '\n') source_mounts[i][len - 1] = 0; - log_msg(DEBUG, "sourcename %s.", source_mounts[i]); + log_msg(DEBUG, "sourcename: %s", source_mounts[i]); i++; } fclose(fhierarchy); @@ -205,7 +204,7 @@ static int get_path(const char* oldpath, char passpath[PATH_MAX]){ char actualpath [PATH_MAX + 1]; realpath(oldpath, actualpath); int len = strlen(mount_dir); - strcpy(passpath, source); + strcpy(passpath, source_mounts[0]); int match_found = 0; //log_msg(DEBUG, "actualpath: %s, mount_dir: %s", actualpath, mount_dir); @@ -215,8 +214,7 @@ static int get_path(const char* oldpath, char passpath[PATH_MAX]){ log_msg(DEBUG, "match null"); log_msg(DEBUG, "match"); *match = '\0'; - check_path_exists(match + len, passpath); - //strcat(passpath, match + len); + strcat(passpath, match + len); match_found = 1; } else{ From 2665507e32bef67cf55b122ce2c1b0803876ccfb Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Tue, 12 May 2020 00:27:07 -0400 Subject: [PATCH 08/14] Fixed error handling in readdir, commented xstat, improved ls test --- passthrough.cpp | 20 +++++++++++--------- tests/setup.bash | 1 + tests/tests.bats | 3 ++- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/passthrough.cpp b/passthrough.cpp index f4b57e06..e7349325 100644 --- a/passthrough.cpp +++ b/passthrough.cpp @@ -19,6 +19,7 @@ #include #include #include +#include //#include #define MAX_LOG 200 @@ -485,9 +486,10 @@ extern "C" { struct dirent *d; log_msg(INFO, "readdir"); + errno = 0; d = ((funcptr_readdir)libc_readdir)(dirp); - if (d != NULL) + if (d == NULL && errno) log_msg(ERROR, "reading dir %s", d->d_name); return d; } @@ -674,14 +676,14 @@ extern "C" { return ((funcptr_statvfs)libc_statvfs)(passpath, buf); } - int __xstat(int ver, const char *path, struct stat *statbuf){ - log_msg(INFO, "xstat"); - initialize_passthrough_if_necessary(); - char passpath[PATH_MAX]; - get_path(path, passpath); - log_msg(INFO, "xstat %s", passpath); - return ((funcptr___xstat)libc___xstat)(ver, passpath, statbuf); - } + // int __xstat(int ver, const char *path, struct stat *statbuf){ + // log_msg(INFO, "xstat"); + // initialize_passthrough_if_necessary(); + // char passpath[PATH_MAX]; + // get_path(path, passpath); + // log_msg(INFO, "xstat %s", passpath); + // return ((funcptr___xstat)libc___xstat)(ver, passpath, statbuf); + // } int __xstat64(int ver, const char *path, struct stat64 *statbuf){ log_msg(INFO, "xstat64"); diff --git a/tests/setup.bash b/tests/setup.bash index 77f011bf..853e307e 100644 --- a/tests/setup.bash +++ b/tests/setup.bash @@ -3,4 +3,5 @@ unset LD_PRELOAD # Clean up from previous tests mkdir -p ${SOURCE} ${MOUNT} # Create source and mount (defined in tests.bats) echo ${SOURCE} > sources.txt echo a > a.txt # This file will be used in tests +echo b > ${SOURCE}/file_in_source.txt # This file will be used in tests export LD_PRELOAD=${PWD}/passthrough.so \ No newline at end of file diff --git a/tests/tests.bats b/tests/tests.bats index 9fde7212..d3367365 100644 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -7,7 +7,8 @@ MOUNT="mount" @test "ls" { load setup - ls + a=$(ls ${MOUNT}) + [[ $a == *"file_in_source.txt" ]] # passthrough init message is in a load unset } From b131012cf4ea1bbb312dea1804244e6491ac3f74 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Tue, 12 May 2020 00:50:45 -0400 Subject: [PATCH 09/14] Added test for rm --- tests/tests.bats | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/tests.bats b/tests/tests.bats index d3367365..e0ec75db 100644 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -19,6 +19,13 @@ MOUNT="mount" test -d ${SOURCE}/dir } +@test "rm" { + load setup + rm ${MOUNT}/file_in_source.txt + load unset + [ ! -f ${SOURCE}/file_in_source.txt ] +} + @test "cp" { load setup cp a.txt ${MOUNT} From 4cb536c3c8576ad2676951a15e7931361da0aa79 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Tue, 12 May 2020 00:52:18 -0400 Subject: [PATCH 10/14] Cleaned up travis file --- .travis.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index e37b20d8..5b50c6ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,9 +11,4 @@ install: - make script: -- bats tests/tests.bats - -after_success: -- coveralls - -deploy: +- bats tests/tests.bats \ No newline at end of file From b2e40df2f23a264d50d275cbe736a16e8acff986 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Tue, 12 May 2020 14:04:02 -0400 Subject: [PATCH 11/14] Uncommented xstat, testing on more distributions --- .travis.yml | 5 ++++- passthrough.cpp | 16 ++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5b50c6ee..6a37e857 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,7 @@ -dist: xenial +dist: + - bionic + - xenial + - trusty language: C os: linux compiler: gcc diff --git a/passthrough.cpp b/passthrough.cpp index e7349325..6c57d64b 100644 --- a/passthrough.cpp +++ b/passthrough.cpp @@ -676,14 +676,14 @@ extern "C" { return ((funcptr_statvfs)libc_statvfs)(passpath, buf); } - // int __xstat(int ver, const char *path, struct stat *statbuf){ - // log_msg(INFO, "xstat"); - // initialize_passthrough_if_necessary(); - // char passpath[PATH_MAX]; - // get_path(path, passpath); - // log_msg(INFO, "xstat %s", passpath); - // return ((funcptr___xstat)libc___xstat)(ver, passpath, statbuf); - // } + int __xstat(int ver, const char *path, struct stat *statbuf){ + log_msg(INFO, "xstat"); + initialize_passthrough_if_necessary(); + char passpath[PATH_MAX]; + get_path(path, passpath); + log_msg(INFO, "xstat %s", passpath); + return ((funcptr___xstat)libc___xstat)(ver, passpath, statbuf); + } int __xstat64(int ver, const char *path, struct stat64 *statbuf){ log_msg(INFO, "xstat64"); From ec94df9b09291ff2a26d492b1ef77342dff5a399 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Tue, 12 May 2020 14:18:44 -0400 Subject: [PATCH 12/14] Another attempt at testing multiple dists --- .travis.yml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6a37e857..5c1e82b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,11 @@ -dist: - - bionic - - xenial - - trusty +jobs: + include: + - dist: bionic + name: "Bionic" + - dist: xenial + name: "Xenial" + - dist: trusty + name: "Trusty" language: C os: linux compiler: gcc From 9540bddba21bf41a4cfa98f9b85c0774c51f375d Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Tue, 12 May 2020 14:22:05 -0400 Subject: [PATCH 13/14] Removed trusty as bats isn't available there --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5c1e82b9..8faf15c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,6 @@ jobs: name: "Bionic" - dist: xenial name: "Xenial" - - dist: trusty - name: "Trusty" language: C os: linux compiler: gcc @@ -18,4 +16,4 @@ install: - make script: -- bats tests/tests.bats \ No newline at end of file +- bats tests/tests.bats From 503730264568b2118cf27a9a77f11e77389632b7 Mon Sep 17 00:00:00 2001 From: Tristan Glatard Date: Tue, 12 May 2020 14:51:54 -0400 Subject: [PATCH 14/14] Fixed segfault in logging function --- passthrough.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/passthrough.cpp b/passthrough.cpp index 6c57d64b..85214452 100644 --- a/passthrough.cpp +++ b/passthrough.cpp @@ -81,7 +81,7 @@ static char source_mounts[MAX_MOUNTS][PATH_MAX]; // maybe use tmpnam? static const char* log_fn = "passlogs.log"; -enum LogLevel { DEBUG=4, INFO=3, WARNING=2, ERROR=1, NONE=0}; +enum LogLevel { DEBUG=4, INFO=3, WARNING=2, ERROR=1, NONE=0 }; static const char* get_lvlname(int lvl){ switch(lvl){ @@ -121,12 +121,14 @@ static int log_msg(int lvl, const char* msg, ...){ else { FILE* logs = log_fopen(log_fn, "a+"); // write complete log string to file - if (logs != NULL){ - fprintf(logs, "%s: %s: %s\n", strtok(asctime(timeinfo), "\n"), get_lvlname(lvl), fmsg); + if (logs == NULL) + { + xprintf("WARNING: Cannot write to log file %s: %s (%s)\n", log_fn, msg, get_lvlname(lvl)); + return 1; } + fprintf(logs, "%s: %s: %s\n", strtok(asctime(timeinfo), "\n"), get_lvlname(lvl), fmsg); fclose(logs); } - return 0; } @@ -283,6 +285,11 @@ static void initialize_passthrough() { fdout = fdopen(stdout2, "a"); } + // Print the last error that occurred in dlsym, if any + char * error = dlerror(); + if(error) + xprintf("dlerror: %s\n",dlerror()); + init_sources(); //xprintf("initialize_passthrough(): New stdout %d\n", stdout2); @@ -305,7 +312,9 @@ void initialize_passthrough_if_necessary() { //} FILE* log_fopen(const char *path, const char *mode){ - return ((funcptr_fopen)libc_fopen)(path, mode); + if(libc_fopen) + return ((funcptr_fopen)libc_fopen)(path, mode); + return NULL; } //int orig_stat(const char *pathname, struct stat *statbuf){ // fprintf(stderr, "orig stat \n");