diff --git a/tests/server/test_server/test_server.c b/tests/server/test_server/test_server.c index 649f7888..4aef7531 100644 --- a/tests/server/test_server/test_server.c +++ b/tests/server/test_server/test_server.c @@ -40,7 +40,7 @@ void free_server_state(struct server_state_st *state) { if (state == NULL) { - goto end; + return; } SAFE_FREE(state->address); @@ -55,9 +55,7 @@ void free_server_state(struct server_state_st *state) SAFE_FREE(state->expected_username); SAFE_FREE(state->expected_password); SAFE_FREE(state->config_file); - -end: - return; + SAFE_FREE(state->log_file); } /* SIGCHLD handler for cleaning up dead children. */ @@ -94,6 +92,30 @@ int run_server(struct server_state_st *state) return SSH_ERROR; } + /* Redirect all the output and errors to the file to avoid mixing up with + * the output from the client */ + if (state->log_file != NULL) { + int fd; + FILE *f = fopen(state->log_file, "a"); + if (f == NULL) { + fprintf(stderr, "Failed to open the log file: %s\n", strerror(errno)); + return SSH_ERROR; + } + fd = dup2(fileno(f), STDERR_FILENO); + if (fd == -1) { + fprintf(stderr, "dup2 of log file to stderr failed: %s\n", + strerror(errno)); + return SSH_ERROR; + } + fd = dup2(fileno(f), STDOUT_FILENO); + if (fd == -1) { + fprintf(stderr, "dup2 of log file to stdout failed: %s\n", + strerror(errno)); + return SSH_ERROR; + } + fclose(f); + } + if (state->address == NULL) { fprintf(stderr, "Missing bind address\n"); return SSH_ERROR; diff --git a/tests/server/test_server/test_server.h b/tests/server/test_server/test_server.h index 0854504f..858dd75a 100644 --- a/tests/server/test_server/test_server.h +++ b/tests/server/test_server/test_server.h @@ -52,6 +52,8 @@ struct server_state_st { char *config_file; bool parse_global_config; + char *log_file; + /* State */ int max_tries; int error;