From 1b02467adfef544c17d1e1606c0167777100c328 Mon Sep 17 00:00:00 2001 From: Glenn Washburn Date: Thu, 29 Aug 2019 21:53:46 -0500 Subject: Allow libtrace preload library to use for trace output a logfile specified by the environment variable FIREJAIL_TRACEFILE or as the RUN_TRACE_FILE if it exists ortherwise use the console as before. --- src/libtrace/libtrace.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'src/libtrace') diff --git a/src/libtrace/libtrace.c b/src/libtrace/libtrace.c index 745dd2260..b3f040e8f 100644 --- a/src/libtrace/libtrace.c +++ b/src/libtrace/libtrace.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include "../include/rundefs.h" #define tprintf(fp, args...) \ do { \ @@ -46,6 +46,8 @@ typedef FILE *(*orig_fopen_t)(const char *pathname, const char *mode); static orig_fopen_t orig_fopen = NULL; typedef FILE *(*orig_fopen64_t)(const char *pathname, const char *mode); static orig_fopen64_t orig_fopen64 = NULL; +typedef int (*orig_access_t)(const char *pathname, int mode); +static orig_access_t orig_access = NULL; // // library constructor/destructor @@ -65,14 +67,24 @@ void init(void) { return; orig_fopen = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen"); - - // tty + orig_access = (orig_access_t)dlsym(RTLD_NEXT, "access"); + + // allow environment variable to override defaults + char *logfile = getenv("FIREJAIL_TRACEFILE"); + if (!logfile) { + // if exists, log to trace file + logfile = RUN_TRACE_FILE; + if (orig_access(logfile, F_OK)) #ifdef PRINTF_DEVTTY - ftty = orig_fopen("/dev/tty", "w"); + // else log to associated tty + logfile = "/dev/tty"; #else - ftty = stderr; + logfile = "/proc/self/fd/2"; #endif - tprintf(ftty, "=== tracelib init() === \n"); + } + + // logfile + ftty = orig_fopen(logfile, "a"); // pid mypid = getpid(); @@ -92,6 +104,7 @@ void init(void) { if (ptr) *ptr = '\0'; + tprintf(ftty, "=== tracelib init() [%d:%s] === \n", mypid, myname); fclose(fp); free(fname); } @@ -483,8 +496,6 @@ DIR *opendir(const char *pathname) { } // access -typedef int (*orig_access_t)(const char *pathname, int mode); -static orig_access_t orig_access = NULL; int access(const char *pathname, int mode) { if (!orig_access) orig_access = (orig_access_t)dlsym(RTLD_NEXT, "access"); -- cgit v1.2.3-54-g00ecf