Skip to content

Commit

Permalink
Merge pull request #1 from glatard/master
Browse files Browse the repository at this point in the history
Several changes
  • Loading branch information
ValHayot authored May 12, 2020
2 parents c3c8ef9 + 5037302 commit 73c3e7c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 13 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
jobs:
include:
- dist: bionic
name: "Bionic"
- dist: xenial
name: "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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
gcc -shared -g -o passthrough.so passthrough.o -ldl -lpthread -ggdb -lstdc++
clean:
rm passthrough.o passthrough.so
31 changes: 20 additions & 11 deletions passthrough.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sys/types.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <errno.h>
//#include <unistd.h>

#define MAX_LOG 200
Expand Down Expand Up @@ -74,14 +75,13 @@ 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];

// 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){
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -156,7 +158,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);
Expand Down Expand Up @@ -205,7 +207,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);
Expand All @@ -215,8 +217,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{
Expand Down Expand Up @@ -284,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);
Expand All @@ -306,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");
Expand Down Expand Up @@ -487,9 +495,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;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/setup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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
echo b > ${SOURCE}/file_in_source.txt # This file will be used in tests
export LD_PRELOAD=${PWD}/passthrough.so
49 changes: 49 additions & 0 deletions tests/tests.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bats

# Source and mount are
# cleaned up in setup
SOURCE="source"
MOUNT="mount"

@test "ls" {
load setup
a=$(ls ${MOUNT})
[[ $a == *"file_in_source.txt" ]] # passthrough init message is in a
load unset
}

@test "mkdir" {
load setup
mkdir ${MOUNT}/dir
load unset
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}
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
}
1 change: 1 addition & 0 deletions tests/unset.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unset LD_PRELOAD

0 comments on commit 73c3e7c

Please sign in to comment.