From bbdf20178f9ff78f5fb99f3c9f06499d2f42ff07 Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Fri, 11 Oct 2019 21:05:56 -0400 Subject: [PATCH] Fix #373: Correct mutex around log file write The mutex for the log file write during the WriteLogFileCmd excution was held longer than it should have been. This lock must *NOT* be held during EVS_SendEvent, as this will cause deadlock. This moves the mutex to protect only the area that actually accesses the log data, and it moves all send event calls to be AFTER the mutex is released. --- fsw/cfe-core/src/evs/cfe_evs_log.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/fsw/cfe-core/src/evs/cfe_evs_log.c b/fsw/cfe-core/src/evs/cfe_evs_log.c index af5960414..146a69cd6 100644 --- a/fsw/cfe-core/src/evs/cfe_evs_log.c +++ b/fsw/cfe-core/src/evs/cfe_evs_log.c @@ -167,9 +167,6 @@ int32 CFE_EVS_WriteLogDataFileCmd(const CFE_EVS_WriteLogDataFile_t *data) } else { - /* Serialize access to event log control variables */ - OS_MutSemTake(CFE_EVS_GlobalData.EVS_SharedDataMutexID); - /* Copy the commanded filename into local buffer to ensure size limitation and to allow for modification */ CFE_SB_MessageStringGet(LogFilename, (const char *)CmdPtr->LogFilename, CFE_PLATFORM_EVS_DEFAULT_LOG_FILE, OS_MAX_PATH_LEN, sizeof(CmdPtr->LogFilename)); @@ -198,6 +195,9 @@ int32 CFE_EVS_WriteLogDataFileCmd(const CFE_EVS_WriteLogDataFile_t *data) if (BytesWritten == sizeof(LogFileHdr)) { + /* Serialize access to event log control variables */ + OS_MutSemTake(CFE_EVS_GlobalData.EVS_SharedDataMutexID); + /* Is the log full? -- Doesn't matter if wrap mode is enabled */ if (CFE_EVS_GlobalData.EVS_LogPtr->LogCount == CFE_PLATFORM_EVS_LOG_MAX) { @@ -228,13 +228,12 @@ int32 CFE_EVS_WriteLogDataFileCmd(const CFE_EVS_WriteLogDataFile_t *data) } else { - EVS_SendEvent(CFE_EVS_ERR_WRLOGFILE_EID, CFE_EVS_EventType_ERROR, - "Write Log File Command Error: OS_write = 0x%08X, filename = %s", - (unsigned int)BytesWritten, LogFilename); break; } } + OS_MutSemGive(CFE_EVS_GlobalData.EVS_SharedDataMutexID); + /* Process command handler success result */ if (i == CFE_EVS_GlobalData.EVS_LogPtr->LogCount) { @@ -243,13 +242,17 @@ int32 CFE_EVS_WriteLogDataFileCmd(const CFE_EVS_WriteLogDataFile_t *data) (int)CFE_EVS_GlobalData.EVS_LogPtr->LogCount, LogFilename); Result = CFE_SUCCESS; } + else + { + EVS_SendEvent(CFE_EVS_ERR_WRLOGFILE_EID, CFE_EVS_EventType_ERROR, + "Write Log File Command Error: OS_write = 0x%08X, filename = %s", + (unsigned int)BytesWritten, LogFilename); + } } OS_close(LogFileHandle); } - OS_MutSemGive(CFE_EVS_GlobalData.EVS_SharedDataMutexID); - } return(Result);