Skip to content

Commit

Permalink
internal-logging: Fix issues with file logging (#378)
Browse files Browse the repository at this point in the history
This commit fixes the following issues if access
to the internal log file is not possible (logging_mode = DLT_LOG_TO_FILE)
* dlt_log_free tried to call fclose on a nullptr
  Added a nullcheck for this
* Access to log file might be denied but access to logs is still wanted
  Add a new CMake option WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK
  If this is set to ON and the logging moe is set to file,
  the dlt-daemon will fall back to syslog if opening the internal log
  file failed

Signed-off-by: Alexander Mohr <[email protected]>
  • Loading branch information
alexmohr authored Oct 5, 2022
1 parent 34471d8 commit 5b80a4c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ option(WITH_DLT_DBUS "Set to ON to build src/dbus binaries"
option(WITH_DLT_TESTS "Set to ON to build src/test binaries" ON)
option(WITH_DLT_UNIT_TESTS "Set to ON to build gtest framework and tests/binaries" OFF)
option(WITH_DLT_QNX_SYSTEM "Set to ON to build QNX system binary dlt-qnx-system" OFF)
option(WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK "Set to ON to enable fallback to syslog if dlt logging to file fails" OFF)

set(DLT_IPC "FIFO" CACHE STRING "UNIX_SOCKET,FIFO")
set(DLT_USER "genivi" CACHE STRING "Set user for process not run as root")
Expand Down Expand Up @@ -171,6 +172,10 @@ if(WITH_DLT_QNX_SYSTEM AND NOT "${CMAKE_C_COMPILER}" MATCHES "nto-qnx|qcc")
message(FATAL_ERROR "Can only compile for QNX with a QNX compiler.")
endif()

if (WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK)
add_definitions(-DWITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK)
endif()

if(WITH_GPROF)
add_compile_options(-pg)
endif()
Expand Down Expand Up @@ -345,6 +350,7 @@ message(STATUS "WITH_LIB_SHORT_VERSION = ${WITH_LIB_SHORT_VERSION}")
message(STATUS "WITH_LEGACY_INCLUDE_PATH = ${WITH_LEGACY_INCLUDE_PATH}")
message(STATUS "WITH_EXTENDED_FILTERING = ${WITH_EXTENDED_FILTERING}")
message(STATUS "WITH_DLT_DISABLE_MACRO = ${WITH_DLT_DISABLE_MACRO}")
message(STATUS "WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK = ${WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK}" )
message(STATUS "Change a value with: cmake -D<Variable>=<Value>")
message(STATUS "-------------------------------------------------------------------------------")
message(STATUS)
2 changes: 1 addition & 1 deletion include/dlt/dlt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ void dlt_print_with_attributes(bool state);
* Initialize (external) logging facility
* @param mode positive, 0 = log to stdout, 1 = log to syslog, 2 = log to file, 3 = log to stderr
*/
void dlt_log_init(int mode);
DltReturnValue dlt_log_init(int mode);
/**
* Print with variable arguments to specified file descriptor by DLT_LOG_MODE environment variable (like fprintf)
* @param format format string for message
Expand Down
22 changes: 20 additions & 2 deletions src/daemon/dlt-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,25 @@ int main(int argc, char *argv[])
/* Initialize internal logging facility */
dlt_log_set_filename(daemon_local.flags.loggingFilename);
dlt_log_set_level(daemon_local.flags.loggingLevel);
dlt_log_init(daemon_local.flags.loggingMode);
DltReturnValue log_init_result =
dlt_log_init(daemon_local.flags.loggingMode);

if (log_init_result != DLT_RETURN_OK) {
fprintf(stderr, "Failed to init internal logging\n");

#if WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK
if (daemon_local.flags.loggingMode == DLT_LOG_TO_FILE) {
fprintf(stderr, "Falling back to syslog mode\n");

daemon_local.flags.loggingMode = DLT_LOG_TO_SYSLOG;
log_init_result = dlt_log_init(daemon_local.flags.loggingMode);
if (log_init_result != DLT_RETURN_OK) {
fprintf(stderr, "Failed to setup syslog logging, internal logs will "
"not be available\n");
}
}
#endif
}

/* Print version information */
dlt_get_version(version, DLT_DAEMON_TEXTBUFSIZE);
Expand All @@ -955,7 +973,7 @@ int main(int argc, char *argv[])
if (dlt_mkdir_recursive(dltFifoBaseDir) != 0) {
dlt_vlog(LOG_ERR, "Base dir %s cannot be created!\n", dltFifoBaseDir);
return -1;
}
}

#else
if (dlt_mkdir_recursive(DLT_USER_IPC_PATH) != 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/daemon/dlt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ LoggingMode = 0
LoggingLevel = 6

# The logging filename if internal logging mode is log to file (Default: /tmp/dlt.log)
# If access to the file is not possible, the daemon will fall back to syslog
# if WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK is set as compile flag
LoggingFilename = /tmp/dlt.log

# Timeout on send to client (sec)
Expand Down
10 changes: 6 additions & 4 deletions src/shared/dlt_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1814,11 +1814,11 @@ void dlt_print_with_attributes(bool state)
print_with_attributes = state;
}

void dlt_log_init(int mode)
DltReturnValue dlt_log_init(int mode)
{
if ((mode < DLT_LOG_TO_CONSOLE) || (mode > DLT_LOG_DROPPED)) {
dlt_vlog(LOG_WARNING, "Wrong parameter for mode: %d\n", mode);
return;
return DLT_RETURN_WRONG_PARAMETER;
}

logging_mode = mode;
Expand All @@ -1829,14 +1829,16 @@ void dlt_log_init(int mode)

if (logging_handle == NULL) {
dlt_user_printf("Internal log file %s cannot be opened!\n", logging_filename);
return;
return DLT_RETURN_ERROR;
}
}

return DLT_RETURN_OK;
}

void dlt_log_free(void)
{
if (logging_mode == DLT_LOG_TO_FILE)
if (logging_mode == DLT_LOG_TO_FILE && logging_handle)
fclose(logging_handle);
}

Expand Down

0 comments on commit 5b80a4c

Please sign in to comment.