Skip to content

Commit 7db3b3c

Browse files
committed
internal-logging: Fix issues with file logging
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
1 parent 6a3bd90 commit 7db3b3c

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ option(WITH_DLT_DBUS "Set to ON to build src/dbus binaries"
7373
option(WITH_DLT_TESTS "Set to ON to build src/test binaries" ON)
7474
option(WITH_DLT_UNIT_TESTS "Set to ON to build gtest framework and tests/binaries" OFF)
7575
option(WITH_DLT_QNX_SYSTEM "Set to ON to build QNX system binary dlt-qnx-system" OFF)
76+
option(WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK "Set to ON to enable fallback to syslog if dlt logging to file fails" OFF)
7677

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

173+
if (WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK)
174+
add_definitions(-DWITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK)
175+
endif()
176+
172177
if(WITH_GPROF)
173178
add_compile_options(-pg)
174179
endif()
@@ -336,6 +341,7 @@ message(STATUS "WITH_LIB_SHORT_VERSION = ${WITH_LIB_SHORT_VERSION}")
336341
message(STATUS "WITH_LEGACY_INCLUDE_PATH = ${WITH_LEGACY_INCLUDE_PATH}")
337342
message(STATUS "WITH_EXTENDED_FILTERING = ${WITH_EXTENDED_FILTERING}")
338343
message(STATUS "WITH_DLT_DISABLE_MACRO = ${WITH_DLT_DISABLE_MACRO}")
344+
message(STATUS "WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK = ${WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK}" )
339345
message(STATUS "Change a value with: cmake -D<Variable>=<Value>")
340346
message(STATUS "-------------------------------------------------------------------------------")
341347
message(STATUS)

include/dlt/dlt_common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ void dlt_print_with_attributes(bool state);
11951195
* Initialize (external) logging facility
11961196
* @param mode positive, 0 = log to stdout, 1 = log to syslog, 2 = log to file, 3 = log to stderr
11971197
*/
1198-
void dlt_log_init(int mode);
1198+
DltReturnValue dlt_log_init(int mode);
11991199
/**
12001200
* Print with variable arguments to specified file descriptor by DLT_LOG_MODE environment variable (like fprintf)
12011201
* @param format format string for message

src/daemon/dlt-daemon.c

+20-2
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,25 @@ int main(int argc, char *argv[])
940940
/* Initialize internal logging facility */
941941
dlt_log_set_filename(daemon_local.flags.loggingFilename);
942942
dlt_log_set_level(daemon_local.flags.loggingLevel);
943-
dlt_log_init(daemon_local.flags.loggingMode);
943+
DltReturnValue log_init_result =
944+
dlt_log_init(daemon_local.flags.loggingMode);
945+
946+
if (log_init_result != DLT_RETURN_OK) {
947+
fprintf(stderr, "Failed to init internal logging\n");
948+
949+
#if WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK
950+
if (daemon_local.flags.loggingMode == DLT_LOG_TO_FILE) {
951+
fprintf(stderr, "Falling back to syslog mode\n");
952+
953+
daemon_local.flags.loggingMode = DLT_LOG_TO_SYSLOG;
954+
log_init_result = dlt_log_init(daemon_local.flags.loggingMode);
955+
if (log_init_result != DLT_RETURN_OK) {
956+
fprintf(stderr, "Failed to setup syslog logging, internal logs will "
957+
"not be available\n");
958+
}
959+
}
960+
#endif
961+
}
944962

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

960978
#else
961979
if (dlt_mkdir_recursive(DLT_USER_IPC_PATH) != 0) {

src/daemon/dlt.conf

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ LoggingMode = 0
4646
LoggingLevel = 6
4747

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

5153
# Timeout on send to client (sec)

src/shared/dlt_common.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1803,11 +1803,11 @@ void dlt_print_with_attributes(bool state)
18031803
print_with_attributes = state;
18041804
}
18051805

1806-
void dlt_log_init(int mode)
1806+
DltReturnValue dlt_log_init(int mode)
18071807
{
18081808
if ((mode < DLT_LOG_TO_CONSOLE) || (mode > DLT_LOG_DROPPED)) {
18091809
dlt_vlog(LOG_WARNING, "Wrong parameter for mode: %d\n", mode);
1810-
return;
1810+
return DLT_RETURN_WRONG_PARAMETER;
18111811
}
18121812

18131813
logging_mode = mode;
@@ -1818,14 +1818,16 @@ void dlt_log_init(int mode)
18181818

18191819
if (logging_handle == NULL) {
18201820
dlt_user_printf("Internal log file %s cannot be opened!\n", logging_filename);
1821-
return;
1821+
return DLT_RETURN_ERROR;
18221822
}
18231823
}
1824+
1825+
return DLT_RETURN_OK;
18241826
}
18251827

18261828
void dlt_log_free(void)
18271829
{
1828-
if (logging_mode == DLT_LOG_TO_FILE)
1830+
if (logging_mode == DLT_LOG_TO_FILE && logging_handle)
18291831
fclose(logging_handle);
18301832
}
18311833

0 commit comments

Comments
 (0)