From 9c9a4b9c6b257c2dd90cfcfc792373038a808796 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 17 Sep 2010 22:10:55 +0200 Subject: [PATCH] fl_wrapper: cleaned conditional logging by use a LOG() macro --- src/tools-source/fl_wrapper/fl_wrapper.c | 53 ++++++++++-------------- src/tools-source/fl_wrapper/tpl_open.c | 5 +-- src/tools-source/fl_wrapper/tpl_rest.c | 5 +-- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/tools-source/fl_wrapper/fl_wrapper.c b/src/tools-source/fl_wrapper/fl_wrapper.c index 8fb44cf..5c2d594 100644 --- a/src/tools-source/fl_wrapper/fl_wrapper.c +++ b/src/tools-source/fl_wrapper/fl_wrapper.c @@ -28,7 +28,15 @@ #ifndef DEBUG # define DEBUG 0 +# define LOG(F, ...) +#elif DEBUG == 1 +# define LOG(F, ...) fprintf(stderr, "fl_wrapper.so debug [%d]: " F "\n", \ +# getpid(), __VA_ARGS__) +#else +# define LOG(F, ...) #endif +#define ERR(F, ...) fprintf(stderr, "fl_wrapper.so: " F "\n", __VA_ARGS__) + #define DLOPEN_LIBC 1 #ifndef FLWRAPPER_LIBC # define FLWRAPPER_LIBC "libc.so.6" @@ -67,21 +75,15 @@ static void * get_dl_symbol(char * symname) if (!libc_handle) libc_handle=dlopen(FLWRAPPER_LIBC, RTLD_LAZY); if (!libc_handle) { - fprintf(stderr, "fl_wrapper.so: Can't dlopen libc: %s\n", dlerror()); fflush(stderr); + ERR("Can't dlopen libc: %s", dlerror()); fflush(stderr); abort(); } rc = dlsym(libc_handle, symname); -# if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: Symbol '%s' in libc (%p) has been resolved to %p.\n", - getpid(), symname, libc_handle, rc); -# endif + LOG("Symbol '%s' in libc (%p) has been resolved to %p.", symname, libc_handle, rc); #else rc = dlsym(RTLD_NEXT, symname); -# if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: Symbol '%s' (RTLD_NEXT) has been resolved to %p.\n", - getpid(), symname, rc); -# endif + LOG("Symbol '%s' (RTLD_NEXT) has been resolved to %p.", symname, rc); #endif if (!rc) { fprintf(stderr, "fl_wrapper.so: Can't resolve %s: %s\n", @@ -214,7 +216,7 @@ static void check_write_access(const char * func, const char * file) if (!strcmp(file, "/dev/null") || !strncmp(file, "/tmp", 4)) { } else if (strncmp(file, FLWRAPPER_BASEDIR, sizeof(FLWRAPPER_BASEDIR)-1)) { - fprintf(stderr, "fl_wrapper.so: write outside basedir (%s): %s\n", FLWRAPPER_BASEDIR, file); + ERR("write outside basedir (%s): %s", FLWRAPPER_BASEDIR, file); fflush(stderr); exit(-1); } @@ -226,10 +228,11 @@ static void handle_file_access_before(const char * func, const char * file, struct status_t * status) { struct stat st; -#if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: begin of handle_file_access_before(\"%s\", \"%s\", xxx)\n", - getpid(), func, file); +#if DEBUG != 1 + (void) func; #endif + + LOG("begin of handle_file_access_before(\"%s\", \"%s\", xxx)", func, file); if ( lstat(file,&st) ) { status->inode=0; status->size=0; status->mtime=0; status->ctime=0; @@ -237,10 +240,7 @@ static void handle_file_access_before(const char * func, const char * file, status->inode=st.st_ino; status->size=st.st_size; status->mtime=st.st_mtime; status->ctime=st.st_ctime; } -#if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: end of handle_file_access_before(\"%s\", \"%s\", xxx)\n", - getpid(), func, file); -#endif + LOG("end of handle_file_access_before(\"%s\", \"%s\", xxx)", func, file); } /* sort of, private realpath, mostly not readlink() */ @@ -289,10 +289,8 @@ static void handle_file_access_after(const char * func, const char * file, char absfile [PATH_MAX]; int fd; struct stat st; -#if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: begin of handle_file_access_after(\"%s\", \"%s\", xxx)\n", - getpid(), func, file); -#endif + LOG("begin of handle_file_access_after(\"%s\", \"%s\", xxx)\n", func, file); + if ( !strcmp(file, wlog) ) return; if ( !strcmp(file, rlog) ) return; if ( lstat(file, &st) ) return; @@ -314,12 +312,8 @@ static void handle_file_access_after(const char * func, const char * file, for ( ; tfilterdir ; tfilterdir = strtok(NULL, ":") ) { if ( !strncmp(absfile, tfilterdir, strlen(tfilterdir)) ) { -#if DEBUG == 1 - fprintf(stderr, - "fl_wrapper.so debug [%d]: \"%s\" dropped due to filterdir \"%s\"\n", - getpid(), absfile, tfilterdir); -#endif - return; + LOG("\"%s\" dropped due to filterdir \"%s\"", absfile, tfilterdir); + return; } } @@ -338,8 +332,5 @@ static void handle_file_access_after(const char * func, const char * file, write(fd,buf,strlen(buf)); close(fd); -#if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: end of handle_file_access_after(\"%s\", \"%s\", xxx)\n", - getpid(), func, file); -#endif + LOG("end of handle_file_access_after(\"%s\", \"%s\", xxx)", func, file); } diff --git a/src/tools-source/fl_wrapper/tpl_open.c b/src/tools-source/fl_wrapper/tpl_open.c index e7de21b..6adeac6 100644 --- a/src/tools-source/fl_wrapper/tpl_open.c +++ b/src/tools-source/fl_wrapper/tpl_open.c @@ -43,10 +43,7 @@ RET_TYPE FUNCTION(P1) if (!orig_FUNCTION) orig_FUNCTION = get_dl_symbol("FUNCTION"); errno=old_errno; -#if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original FUNCTION() at %p (wrapper is at %p).\n", - getpid(), orig_FUNCTION, FUNCTION); -#endif + LOG("going to run original FUNCTION() at %p (wrapper is at %p).", orig_FUNCTION, FUNCTION); if (a & O_CREAT) { va_list ap; diff --git a/src/tools-source/fl_wrapper/tpl_rest.c b/src/tools-source/fl_wrapper/tpl_rest.c index 53ba055..5b2e7db 100644 --- a/src/tools-source/fl_wrapper/tpl_rest.c +++ b/src/tools-source/fl_wrapper/tpl_rest.c @@ -33,10 +33,7 @@ RET_TYPE FUNCTION(P1) if (!orig_FUNCTION) orig_FUNCTION = get_dl_symbol("FUNCTION"); errno=old_errno; -#if DEBUG == 1 - fprintf(stderr, "fl_wrapper.so debug [%d]: going to run original FUNCTION() at %p (wrapper is at %p).\n", - getpid(), orig_FUNCTION, FUNCTION); -#endif + LOG("going to run original FUNCTION() at %p (wrapper is at %p).", orig_FUNCTION, FUNCTION); rc = orig_FUNCTION(P2); old_errno=errno;