From 05350cb9dc58ec8442a7ebb07961ea665ff354a2 Mon Sep 17 00:00:00 2001 From: Jin JinRu Date: Thu, 16 Nov 2017 01:57:46 -0500 Subject: [PATCH 1/2] Added RETRACE_CONFIG_VAR environment for retrace configuration --- src/common.c | 61 +++++++++++++++++++++++++++++++----------------- src/file.c | 28 ++++++++++++++++++++++ src/file.h | 2 ++ test/runtests.sh | 5 ++++ 4 files changed, 75 insertions(+), 21 deletions(-) diff --git a/src/common.c b/src/common.c index 84611f59..6a52911d 100644 --- a/src/common.c +++ b/src/common.c @@ -1575,40 +1575,59 @@ get_config_file() FILE *config_file = NULL; char *file_path; int olderrno; + char *config_env; + char *file_path_user; + char *file_name_user = ".retrace.conf"; + + int config_fd; + + /* save latest errno */ olderrno = errno; /* If we have a RETRACE_CONFIG env var, try to open the config file from there. */ file_path = real_getenv(RETRACE_ENV_CONFIG_FILE); - if (file_path) config_file = real_fopen(file_path, "r"); + if (config_file) + return config_file; + } /* If we couldn't open the file from the env var try to home it from ~/.retrace.conf */ - if (!config_file) { - file_path = real_getenv("HOME"); - - if (file_path) { - char *file_path_user; - char *file_name_user = ".retrace.conf"; - - file_path_user = (char *)real_malloc(real_strlen(file_path) + real_strlen(file_name_user) + 2); - - if (file_path_user) { - real_strcpy(file_path_user, file_path); - real_strcat(file_path_user, "/"); - real_strcat(file_path_user, file_name_user); - - config_file = real_fopen(file_path_user, "r"); + file_path = real_getenv("HOME"); + config_env = real_getenv("RETRACE_CONFIG_VAR"); + if (!file_path || !config_env) + return NULL; + + /* build file path */ + file_path_user = (char *)real_malloc(real_strlen(file_path) + real_strlen(file_name_user) + 2); + if (!file_path_user) + return NULL; + + real_strcpy(file_path_user, file_path); + real_strcat(file_path_user, "/"); + real_strcat(file_path_user, file_name_user); + + /* create configuration file by configuration environment */ + config_fd = real_open(file_path_user, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); + if (config_fd > 0) { + config_file = real_fdopen(config_fd, "w"); + if (!config_file) + real_close(config_fd); + else { + /* write configuration lines */ + real_fwrite(config_env, 1, strlen(config_env), config_file); - real_free(file_path_user); - } + /* close file */ + real_fclose(config_file); } + + /* open configuration file */ + config_file = real_fopen(file_path_user, "r"); } - /* Finally if the above failed try to open /etc/retrace.conf */ - if (!config_file) - config_file = real_fopen("/etc/retrace.conf", "r"); + /* free file path for user */ + real_free(file_path_user); errno = olderrno; diff --git a/src/file.c b/src/file.c index ac7d9339..92f43994 100644 --- a/src/file.c +++ b/src/file.c @@ -301,6 +301,34 @@ FILE *RETRACE_IMPLEMENTATION(fopen)(const char *file, const char *mode) RETRACE_REPLACE(fopen, FILE *, (const char *file, const char *mode), (file, mode)) +FILE *RETRACE_IMPLEMENTATION(fdopen)(int fd, const char *mode) +{ + FILE *ret; + struct rtr_event_info event_info; + unsigned int parameter_types[] = {PARAMETER_TYPE_INT, PARAMETER_TYPE_STRING, PARAMETER_TYPE_END}; + void const *parameter_values[] = {&fd, &mode}; + + memset(&event_info, 0, sizeof(event_info)); + event_info.function_name = "fdopen"; + event_info.function_group = RTR_FUNC_GRP_FILE; + event_info.parameter_types = parameter_types; + event_info.parameter_values = (void **) parameter_values; + event_info.return_value_type = PARAMETER_TYPE_FILE_STREAM; + event_info.return_value = &ret; + event_info.logging_level = RTR_LOG_LEVEL_NOR; + retrace_log_and_redirect_before(&event_info); + + ret = real_fdopen(fd, mode); + if (!ret) + event_info.logging_level |= RTR_LOG_LEVEL_ERR; + + retrace_log_and_redirect_after(&event_info); + + return ret; +} + +RETRACE_REPLACE(fdopen, FILE *, (int fd, const char *mode), (fd, mode)) + int RETRACE_IMPLEMENTATION(close)(int fd) { struct rtr_event_info event_info; diff --git a/src/file.h b/src/file.h index b2790a58..a01b2eb2 100644 --- a/src/file.h +++ b/src/file.h @@ -2,6 +2,7 @@ #define __RETRACE_FILE_H__ typedef FILE *(*rtr_fopen_t)(const char *filename, const char *mode); +typedef FILE *(*rtr_fdopen_t)(int fd, const char *mode); typedef int (*rtr_fclose_t)(FILE *stream); typedef int (*rtr_fseek_t)(FILE *stream, long offset, int whence); typedef int (*rtr_fileno_t)(FILE *stream); @@ -31,6 +32,7 @@ typedef int (*rtr_feof_t)(FILE *stream); RETRACE_DECL(fopen); +RETRACE_DECL(fdopen); RETRACE_DECL(fclose); RETRACE_DECL(fseek); RETRACE_DECL(fileno); diff --git a/test/runtests.sh b/test/runtests.sh index c22409a3..d5001060 100755 --- a/test/runtests.sh +++ b/test/runtests.sh @@ -3,6 +3,9 @@ # make sure ../retrace itself can fail ../retrace -f nonexistent ls || ../retrace nonexistent || echo ok +# retrace configuration var +RTR_CONF_VAR=`cat ../retrace.conf.example` + ./strinject.sh LC_ALL="POSIX" ../retrace ./setlocale ../retrace ./exec @@ -31,4 +34,6 @@ LC_ALL="POSIX" ../retrace ./setlocale ../retrace ./log ../retrace ./writev ../retrace ./readv +export RETRACE_CONFIG_VAR="${RTR_CONF_VAR}" +../retrace ./config ../retrace -f ../retrace.conf.example ./config From 0a2def34b30048542e62c45dea54900ea2a621f1 Mon Sep 17 00:00:00 2001 From: Jin JinRu Date: Sun, 19 Nov 2017 08:58:26 -0500 Subject: [PATCH 2/2] Fixed compilation issue --- src/common.c | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/common.c b/src/common.c index 6a52911d..3cdecd23 100644 --- a/src/common.c +++ b/src/common.c @@ -1575,19 +1575,16 @@ get_config_file() FILE *config_file = NULL; char *file_path; int olderrno; - char *config_env; char *file_path_user; char *file_name_user = ".retrace.conf"; - int config_fd; - /* save latest errno */ olderrno = errno; /* If we have a RETRACE_CONFIG env var, try to open the config file from there. */ file_path = real_getenv(RETRACE_ENV_CONFIG_FILE); - if (file_path) + if (file_path) { config_file = real_fopen(file_path, "r"); if (config_file) return config_file; @@ -1595,8 +1592,7 @@ get_config_file() /* If we couldn't open the file from the env var try to home it from ~/.retrace.conf */ file_path = real_getenv("HOME"); - config_env = real_getenv("RETRACE_CONFIG_VAR"); - if (!file_path || !config_env) + if (!file_path) return NULL; /* build file path */ @@ -1609,22 +1605,9 @@ get_config_file() real_strcat(file_path_user, file_name_user); /* create configuration file by configuration environment */ - config_fd = real_open(file_path_user, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); - if (config_fd > 0) { - config_file = real_fdopen(config_fd, "w"); - if (!config_file) - real_close(config_fd); - else { - /* write configuration lines */ - real_fwrite(config_env, 1, strlen(config_env), config_file); - - /* close file */ - real_fclose(config_file); - } - - /* open configuration file */ - config_file = real_fopen(file_path_user, "r"); - } + config_file = real_fopen(file_path_user, "r"); + if (config_file) + return config_file; /* free file path for user */ real_free(file_path_user);