Skip to content

Commit

Permalink
Merge pull request #335 from riboseinc/retrace_conf_env
Browse files Browse the repository at this point in the history
Load configuration via environment variable 'RETRACE_CONFIG_VAR'
  • Loading branch information
erikbor authored Dec 19, 2017
2 parents d7232e8 + 0a2def3 commit 0142e8a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1576,39 +1576,41 @@ get_config_file()
char *file_path;
int olderrno;

char *file_path_user;
char *file_name_user = ".retrace.conf";

/* 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 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");

real_free(file_path_user);
}
}
if (config_file)
return config_file;
}

/* Finally if the above failed try to open /etc/retrace.conf */
if (!config_file)
config_file = real_fopen("/etc/retrace.conf", "r");
/* If we couldn't open the file from the env var try to home it from ~/.retrace.conf */
file_path = real_getenv("HOME");
if (!file_path)
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_file = real_fopen(file_path_user, "r");
if (config_file)
return config_file;

/* free file path for user */
real_free(file_path_user);

errno = olderrno;

Expand Down
28 changes: 28 additions & 0 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 0142e8a

Please sign in to comment.