diff options
author | 2011-05-23 22:57:00 +0000 | |
---|---|---|
committer | 2011-05-23 22:57:00 +0000 | |
commit | 6d87551883f0a82a85210c441ba93d91f723256b (patch) | |
tree | 40bf7fb4723272f9865b679a2214ecf8bfd46d1b | |
download | autodep-6d87551883f0a82a85210c441ba93d91f723256b.tar.gz autodep-6d87551883f0a82a85210c441ba93d91f723256b.tar.bz2 autodep-6d87551883f0a82a85210c441ba93d91f723256b.zip |
Initial commit. Filestructure, one test and logger module
-rw-r--r-- | logger/src/hook_lib/Makefile | 10 | ||||
-rw-r--r-- | logger/src/hook_lib/file_hook.c | 111 | ||||
-rw-r--r-- | logger/test/1_access/Makefile | 6 | ||||
-rw-r--r-- | logger/test/1_access/accesser.c | 10 |
4 files changed, 137 insertions, 0 deletions
diff --git a/logger/src/hook_lib/Makefile b/logger/src/hook_lib/Makefile new file mode 100644 index 0000000..365ceee --- /dev/null +++ b/logger/src/hook_lib/Makefile @@ -0,0 +1,10 @@ +file_hook.so: file_hook.o + ld -shared -o file_hook.so -ldl -lc file_hook.o + +file_hook.o: file_hook.c + cc -Wall -fPIC -o file_hook.o -c file_hook.c + +all: file_hook.so + +clean: + rm -f file_hook.o file_hook diff --git a/logger/src/hook_lib/file_hook.c b/logger/src/hook_lib/file_hook.c new file mode 100644 index 0000000..3bf8faf --- /dev/null +++ b/logger/src/hook_lib/file_hook.c @@ -0,0 +1,111 @@ +#define _GNU_SOURCE + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> +#include <time.h> + +#include <dlfcn.h> + +#define _FCNTL_H +#include <bits/fcntl.h> + + +//extern int errorno; + +int (*_open)(const char * pathname, int flags, ...); +int (*_open64)(const char * pathname, int flags, ...); + +FILE *log_file_handle; + +void _init() { + _open = (int (*)(const char * pathname, int flags, ...)) dlsym(RTLD_NEXT, "open"); + _open64 = (int (*)(const char * pathname, int flags, ...)) dlsym(RTLD_NEXT, "open64"); + + if(_open==NULL || _open64==NULL) { + fprintf(stderr,"Failed to load original functions of hook\n"); + exit(1); + } + + char *log_file_name=getenv("FILE_LOG"); + if(log_file_name==NULL) { + fprintf(stderr,"Using stderr as output for logs " + "because the FILE_LOG environment variable isn't defined.\n"); + log_file_handle=stderr; + } else { + log_file_handle=fopen(log_file_name,"a+"); + if(log_file_handle==NULL) { + fprintf(stderr,"Failed to open log file %s: %s\n", log_file_name, strerror(errno)); + exit(1); + } + } +} + +void _fini() { + fclose(log_file_handle); +} + +/* + * Prints a string escaping spaces and '\' + * Does not check input variables +*/ +void __print_escaped(FILE *fh ,const char *s){ + for(;(*s)!=0; s++) { + if(*s==' ') + fprintf(fh,"\\ "); + else if(*s=='\\') + fprintf(fh,"\\\\"); + else + fprintf(fh,"%c", *s); + } +} + +/* + * Format of log string: time event file flags result parents +*/ +void __hook_log(const char *event_type, const char *filename,int flags, int result, int err) { + + fprintf(log_file_handle,"%lld ",(unsigned long long)time(NULL)); + + __print_escaped(log_file_handle, event_type); + fprintf(log_file_handle," "); + __print_escaped(log_file_handle, filename); + fprintf(log_file_handle," %d %d %d", flags, result, err); + // TODO: add a parent processes in output + + + fprintf(log_file_handle,"\n"); +} + +int open(const char * pathname, int flags, mode_t mode) { + int ret; + if(flags & O_CREAT) + ret=_open(pathname, flags, mode); + else + ret=_open(pathname, flags, 0); + + __hook_log("open",pathname,flags,ret,errno); + + return ret; +} + +int open64(const char * pathname, int flags, mode_t mode) { + int ret; + + if(flags & O_CREAT) + ret=_open64(pathname, flags, mode); + else + ret=_open64(pathname, flags, 0); + + __hook_log("open64",pathname,flags,ret,errno); + + return ret; +} + +//int execve(const char *filename, char *const argv[], +// char *const envp[]) { + //printf("FORK!!!!(canceled)"); +// return NULL; +//} diff --git a/logger/test/1_access/Makefile b/logger/test/1_access/Makefile new file mode 100644 index 0000000..b3ea345 --- /dev/null +++ b/logger/test/1_access/Makefile @@ -0,0 +1,6 @@ +accesser: accesser.c + +all: accesser + +clean: + rm -f accesser
\ No newline at end of file diff --git a/logger/test/1_access/accesser.c b/logger/test/1_access/accesser.c new file mode 100644 index 0000000..912fc70 --- /dev/null +++ b/logger/test/1_access/accesser.c @@ -0,0 +1,10 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +int main(int argc, char **argv) { + if(argc<=1) { + printf("Usage: accesser.c <file1> [file2] [file3] ...\n"); + } + return 0; +} |