From e1a0e6a83d8ac5756f1b3cb1f10b5c994216f2e5 Mon Sep 17 00:00:00 2001 From: mtirum011 Date: Fri, 2 Jan 2026 08:37:54 +0000 Subject: [PATCH] RDK-60291 [telemetry] RDK Coverity Defect Resolution for Device Management --- source/bulkdata/datamodel.c | 17 +++++- source/bulkdata/profile.c | 27 +++++++-- source/bulkdata/reportprofiles.c | 23 +++++++- source/bulkdata/t2eventreceiver.c | 43 +++++++++----- .../commonlib/telemetry_busmessage_sender.c | 14 ++--- source/dcautil/dca.c | 4 ++ .../protocol/rbusMethod/rbusmethodinterface.c | 1 + source/reportgen/reportgen.c | 6 +- source/scheduler/scheduler.c | 26 +++++++-- source/utils/persistence.c | 14 ++++- source/utils/t2collection.c | 8 ++- source/xconf-client/xconfclient.c | 56 +++++++++++++++---- 12 files changed, 188 insertions(+), 51 deletions(-) diff --git a/source/bulkdata/datamodel.c b/source/bulkdata/datamodel.c index bf290bc0..7b29ec6d 100644 --- a/source/bulkdata/datamodel.c +++ b/source/bulkdata/datamodel.c @@ -57,7 +57,10 @@ static void *process_rp_thread(void *data) { pthread_mutex_lock(&rpMutex); T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__); - pthread_cond_wait(&rpCond, &rpMutex); + while(t2_queue_count(rpQueue) == 0 && !stopProcessing) + { + pthread_cond_wait(&rpCond, &rpMutex); + } T2Debug("%s: Received wake up signal \n", __FUNCTION__); if(t2_queue_count(rpQueue) > 0) @@ -87,7 +90,10 @@ static void *process_tmprp_thread(void *data) { pthread_mutex_lock(&tmpRpMutex); T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__); - pthread_cond_wait(&tmpRpCond, &tmpRpMutex); + while(t2_queue_count(tmpRpQueue) == 0 && !stopProcessing) + { + pthread_cond_wait(&tmpRpCond, &tmpRpMutex); + } T2Debug("%s: Received wake up signal \n", __FUNCTION__); if(t2_queue_count(tmpRpQueue) > 0) @@ -113,7 +119,10 @@ static void *process_msg_thread(void *data) while(!stopProcessing) { pthread_mutex_lock(&rpMsgMutex); - pthread_cond_wait(&msg_Cond, &rpMsgMutex); + while(t2_queue_count(rpMsgPkgQueue) == 0 && !stopProcessing) + { + pthread_cond_wait(&msg_Cond, &rpMsgMutex); + } if(t2_queue_count(rpMsgPkgQueue) > 0) { msgpack = (struct __msgpack__ *)t2_queue_pop(rpMsgPkgQueue); @@ -274,6 +283,7 @@ T2ERROR datamodel_MsgpackProcessProfile(char *str, int strSize) msgpack->msgpack_blob = str; msgpack->msgpack_blob_size = strSize; + pthread_mutex_lock(&rpMutex); pthread_mutex_lock(&rpMsgMutex); if (!stopProcessing) { @@ -287,6 +297,7 @@ T2ERROR datamodel_MsgpackProcessProfile(char *str, int strSize) T2Error("Datamodel not initialized, dropping request \n"); } pthread_mutex_unlock(&rpMsgMutex); + pthread_mutex_unlock(&rpMutex); return T2ERROR_SUCCESS; } diff --git a/source/bulkdata/profile.c b/source/bulkdata/profile.c index f5d9bb17..4a3f9bc3 100644 --- a/source/bulkdata/profile.c +++ b/source/bulkdata/profile.c @@ -331,10 +331,13 @@ static void* CollectAndReport(void* data) do { T2Info("%s while Loop -- START \n", __FUNCTION__); + // Release reuseThreadMutex before acquiring reportInProgressMutex to maintain lock order + pthread_mutex_unlock(&profile->reuseThreadMutex); pthread_mutex_lock(&profile->reportInProgressMutex); profile->reportInProgress = true; pthread_cond_signal(&profile->reportInProgressCond); pthread_mutex_unlock(&profile->reportInProgressMutex); + pthread_mutex_lock(&profile->reuseThreadMutex); int count = profile->grepSeekProfile->execCounter; @@ -549,8 +552,18 @@ static void* CollectAndReport(void* data) pthread_cond_init(&profile->reportcond, NULL); clock_gettime(CLOCK_REALTIME, &profile->currentTime); profile->maxlatencyTime.tv_sec = profile->currentTime.tv_sec; - srand(time(0)); // Initialise the random number generator - maxuploadinmilliSec = rand() % (profile->maxUploadLatency - 1); + unsigned int random_value = 0; + FILE *urandom = fopen("/dev/urandom", "r"); + if(urandom != NULL && fread(&random_value, sizeof(random_value), 1, urandom) == 1) + { + maxuploadinmilliSec = random_value % (profile->maxUploadLatency - 1); + fclose(urandom); + } + else + { + if(urandom != NULL) fclose(urandom); + maxuploadinmilliSec = (unsigned int)(time(0) % (profile->maxUploadLatency - 1)); + } maxuploadinSec = (maxuploadinmilliSec + 1) / 1000; } if( strcmp(profile->protocol, "HTTP") == 0 || strcmp(profile->protocol, "RBUS_METHOD") == 0 ) @@ -564,7 +577,9 @@ static void* CollectAndReport(void* data) pthread_mutex_lock(&profile->reportMutex); T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec); profile->maxlatencyTime.tv_sec += maxuploadinSec; - n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime); + do { + n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime); + } while(n != ETIMEDOUT && n != 0); if(n == ETIMEDOUT) { T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name); @@ -615,7 +630,9 @@ static void* CollectAndReport(void* data) pthread_mutex_lock(&profile->reportMutex); T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec); profile->maxlatencyTime.tv_sec += maxuploadinSec; - n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime); + do { + n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime); + } while(n != ETIMEDOUT && n != 0); if(n == ETIMEDOUT) { T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name); @@ -782,9 +799,11 @@ reportThreadEnd : } while(profile->enable); T2Info("%s --out Exiting collect and report Thread\n", __FUNCTION__); + pthread_mutex_unlock(&profile->reuseThreadMutex); pthread_mutex_lock(&profile->reportInProgressMutex); profile->reportInProgress = false; pthread_mutex_unlock(&profile->reportInProgressMutex); + pthread_mutex_lock(&profile->reuseThreadMutex); profile->threadExists = false; pthread_mutex_unlock(&profile->reuseThreadMutex); pthread_mutex_destroy(&profile->reuseThreadMutex); diff --git a/source/bulkdata/reportprofiles.c b/source/bulkdata/reportprofiles.c index dbe48be5..558bf9a9 100644 --- a/source/bulkdata/reportprofiles.c +++ b/source/bulkdata/reportprofiles.c @@ -946,7 +946,17 @@ void ReportProfiles_ProcessReportProfilesBlob(cJSON *profiles_root, bool rprofil // Delete profiles not present in the new profile list char *profileNameKey = NULL; - int count = hash_map_count(profileHashMap) - 1; + uint32_t hashmap_count = hash_map_count(profileHashMap); + int count; + if (hashmap_count == 0 || hashmap_count == UINT32_MAX) + { + count = -1; + } + else + { + count = (int)(hashmap_count - 1); + } + const char *DirPath = NULL; if (rprofiletypes == T2_RP) @@ -1309,6 +1319,7 @@ int __ReportProfiles_ProcessReportProfilesMsgPackBlob(void *msgpack, bool checkP return T2ERROR_PROFILE_NOT_FOUND; } hash_map_t *profileHashMap; + uint32_t hashmap_count; int count; char *profileNameKey = NULL; int profileIndex; @@ -1325,7 +1336,15 @@ int __ReportProfiles_ProcessReportProfilesMsgPackBlob(void *msgpack, bool checkP } /* Delete profiles not present in the new profile list */ - count = hash_map_count(profileHashMap) - 1; + hashmap_count = hash_map_count(profileHashMap); + if (hashmap_count == 0 || hashmap_count == UINT32_MAX) + { + count = -1; + } + else + { + count = (int)(hashmap_count - 1); + } while(count >= 0) { profile_found_flag = false; diff --git a/source/bulkdata/t2eventreceiver.c b/source/bulkdata/t2eventreceiver.c index 5099e0e0..77f3c0a8 100644 --- a/source/bulkdata/t2eventreceiver.c +++ b/source/bulkdata/t2eventreceiver.c @@ -243,7 +243,18 @@ void* T2ER_EventDispatchThread(void *arg) return NULL; } T2Debug("Checking for events in event queue , event count = %d\n", t2_queue_count(eQueue)); - if(t2_queue_count(eQueue) > 0) + while(t2_queue_count(eQueue) == 0 && !stopDispatchThread) + { + T2Debug("Event Queue size is 0, Waiting events from T2ER_Push\n"); + int ret = pthread_cond_wait(&erCond, &erMutex); + if(ret != 0) // pthread cond wait failed return after unlock + { + T2Error("%s pthread_cond_wait failed with error code: %d\n", __FUNCTION__, ret); + } + T2Debug("Received signal from T2ER_Push\n"); + } + + if(t2_queue_count(eQueue) > 0) { T2Event *event = (T2Event *)t2_queue_pop(eQueue); if(event == NULL) @@ -280,18 +291,11 @@ void* T2ER_EventDispatchThread(void *arg) } else { - T2Debug("Event Queue size is 0, Waiting events from T2ER_Push\n"); - int ret = pthread_cond_wait(&erCond, &erMutex); - if(ret != 0) // pthread cond wait failed return after unlock - { - T2Error("%s pthread_cond_wait failed with error code: %d\n", __FUNCTION__, ret); - } if(pthread_mutex_unlock(&erMutex) != 0) { T2Error("%s pthread_mutex_unlock for erMutex failed\n", __FUNCTION__); return NULL; } - T2Debug("Received signal from T2ER_Push\n"); } } T2Debug("%s --out\n", __FUNCTION__); @@ -345,7 +349,10 @@ T2ERROR T2ER_Init() registerForTelemetryEvents(T2ER_PushDataWithDelim); } - system("touch /tmp/.t2ReadyToReceiveEvents"); + if (system("touch /tmp/.t2ReadyToReceiveEvents") != 0) + { + T2Error("Failed to create /tmp/.t2ReadyToReceiveEvents flag file \n"); + } setT2EventReceiveState(T2_STATE_COMPONENT_READY); T2Info("T2 is now Ready to Recieve Events\n"); @@ -500,13 +507,13 @@ void T2ER_Uninit() } EREnabled = false; + if(pthread_mutex_lock(&sTDMutex) != 0) // mutex lock failed so return from T2ER_Uninit + { + T2Error("%s pthread_mutex_lock for sTDMutex failed\n", __FUNCTION__); + return; + } if(!stopDispatchThread) { - if(pthread_mutex_lock(&sTDMutex) != 0) // mutex lock failed so return from T2ER_Uninit - { - T2Error("%s pthread_mutex_lock for sTDMutex failed\n", __FUNCTION__); - return; - } stopDispatchThread = true; if(pthread_mutex_unlock(&sTDMutex) != 0) //mutex unlock failed so return from T2ER_Uninit { @@ -551,6 +558,14 @@ void T2ER_Uninit() return; } } + else + { + if(pthread_mutex_unlock(&sTDMutex) != 0) + { + T2Error("%s pthread_mutex_unlock for sTDMutex failed\n", __FUNCTION__); + return; + } + } T2Debug("T2ER Event Dispatch Thread successfully terminated\n"); t2_queue_destroy(eQueue, freeT2Event); eQueue = NULL; diff --git a/source/commonlib/telemetry_busmessage_sender.c b/source/commonlib/telemetry_busmessage_sender.c index c5d43127..6905ade5 100644 --- a/source/commonlib/telemetry_busmessage_sender.c +++ b/source/commonlib/telemetry_busmessage_sender.c @@ -755,7 +755,7 @@ void t2_uninit(void) T2ERROR t2_event_s(const char* marker, const char* value) { - int ret; + int ret = -1; T2ERROR retStatus = T2ERROR_FAILURE ; EVENT_DEBUG("%s ++in\n", __FUNCTION__); char* strvalue = NULL; @@ -782,13 +782,13 @@ T2ERROR t2_event_s(const char* marker, const char* value) return T2ERROR_SUCCESS; } strvalue = strdup(value); - if( strvalue[strlen(strvalue) - 1] == '\n' ) - { - strvalue[strlen(strvalue) - 1] = '\0'; - } - ret = report_or_cache_data(strvalue, marker); if(strvalue != NULL) - { + { + if( strvalue[strlen(strvalue) - 1] == '\n' ) + { + strvalue[strlen(strvalue) - 1] = '\0'; + } + ret = report_or_cache_data(strvalue, marker); free(strvalue); } if(ret != -1) diff --git a/source/dcautil/dca.c b/source/dcautil/dca.c index abbc8c78..08f98ec5 100644 --- a/source/dcautil/dca.c +++ b/source/dcautil/dca.c @@ -1017,7 +1017,9 @@ static FileDescriptor* getFileDeltaInMemMapAndSearch(const int fd, const off_t s //create a tmp file for main file fd char tmp_fdmain[] = "/tmp/dca_tmpfile_fdmainXXXXXX"; + mode_t old_umask = umask(0077); // Set secure umask (owner read/write only) int tmp_fd = mkstemp(tmp_fdmain); + umask(old_umask); // Restore original umask if (tmp_fd == -1) { T2Error("Failed to create temp file: %s\n", strerror(errno)); @@ -1044,7 +1046,9 @@ static FileDescriptor* getFileDeltaInMemMapAndSearch(const int fd, const off_t s if (rd != -1 && fstat(rd, &rb) == 0 && rb.st_size > 0) { char tmp_fdrotated[] = "/tmp/dca_tmpfile_fdrotatedXXXXXX"; + mode_t old_umask = umask(0077); // Set secure umask (owner read/write only) int tmp_rd = mkstemp(tmp_fdrotated); + umask(old_umask); // Restore original umask if (tmp_rd == -1) { T2Error("Failed to create temp file: %s\n", strerror(errno)); diff --git a/source/protocol/rbusMethod/rbusmethodinterface.c b/source/protocol/rbusMethod/rbusmethodinterface.c index 02673931..8feacfe4 100644 --- a/source/protocol/rbusMethod/rbusmethodinterface.c +++ b/source/protocol/rbusMethod/rbusmethodinterface.c @@ -50,6 +50,7 @@ static void asyncMethodHandler(rbusHandle_t handle, char const* methodName, rbus (void) params; T2Info("T2 asyncMethodHandler called: %s with return error code = %s \n", methodName, rbusError_ToString(retStatus)); + pthread_mutex_lock(&rbusMethodMutex); if(retStatus == RBUS_ERROR_SUCCESS) { isRbusMethod = true ; diff --git a/source/reportgen/reportgen.c b/source/reportgen/reportgen.c index 88b47051..60bfac88 100644 --- a/source/reportgen/reportgen.c +++ b/source/reportgen/reportgen.c @@ -1210,6 +1210,7 @@ char *prepareHttpUrl(T2HTTP *http) unsigned int index = 0; int params_len = 0; char *url_params = NULL; + char *temp_params = NULL; for(; index < http->RequestURIparamList->count; index++) { @@ -1250,13 +1251,14 @@ char *prepareHttpUrl(T2HTTP *http) { new_params_len += strlen(httpParamVal); } - url_params = realloc(url_params, new_params_len); - if(url_params == NULL) + temp_params = realloc(url_params, new_params_len); + if(temp_params == NULL) { T2Error("Unable to allocate %d bytes of memory at Line %d on %s \n", new_params_len, __LINE__, __FILE__); curl_free(httpParamVal); continue; } + url_params = temp_params; params_len += snprintf(url_params + params_len, new_params_len - params_len, "%s=%s&", httpParam->HttpName, httpParamVal); curl_free(httpParamVal); diff --git a/source/scheduler/scheduler.c b/source/scheduler/scheduler.c index bab6fa6a..31f9d433 100644 --- a/source/scheduler/scheduler.c +++ b/source/scheduler/scheduler.c @@ -258,19 +258,25 @@ void* TimeoutThread(void *arg) if(tProfile->firstreportint > 0 && tProfile->firstexecution == true ) { T2Info("Waiting for %d sec for next TIMEOUT for profile as firstreporting interval is given - %s\n", tProfile->firstreportint, tProfile->name); - n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts); + do { + n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts); + } while(n != ETIMEDOUT && n != 0); } else { if(tProfile->timeOutDuration == UINT_MAX && tProfile->timeRefinSec == 0) { T2Info("Waiting for condition as reporting interval is not configured for profile - %s\n", tProfile->name); - n = pthread_cond_wait(&tProfile->tCond, &tProfile->tMutex); + do { + n = pthread_cond_wait(&tProfile->tCond, &tProfile->tMutex); + } while(n != 0 && !tProfile->terminated); } else { T2Info("Waiting for timeref or reporting interval for the profile - %s is started\n", tProfile->name); - n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts); + do { + n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts); + } while(n != ETIMEDOUT && n != 0); } } if(n == ETIMEDOUT) @@ -648,8 +654,20 @@ T2ERROR unregisterProfileFromScheduler(const char* profileName) // pthread_join(tProfile->tId, NULL); // pthread_detach in freeSchedulerProfile will detach the thread sched_yield(); // This will give chance for the signal receiving thread to start int count = 0; - while(signalrecived_and_executing && !is_activation_time_out) + bool signal_executing = true; + while(signal_executing && !is_activation_time_out) { + if(pthread_mutex_lock(&tProfile->tMutex) != 0) + { + T2Error("tProfile Mutex lock failed\n"); + return T2ERROR_FAILURE; + } + signal_executing = signalrecived_and_executing; + if(pthread_mutex_unlock(&tProfile->tMutex) != 0) + { + T2Error("tProfile Mutex unlock failed\n"); + return T2ERROR_FAILURE; + } if(count++ > 10) { break; diff --git a/source/utils/persistence.c b/source/utils/persistence.c index 253ec412..df60fef9 100644 --- a/source/utils/persistence.c +++ b/source/utils/persistence.c @@ -464,8 +464,18 @@ T2ERROR getPrivacyModeFromPersistentFolder(char **privMode) T2Error("Unable to open the file : %s\n", filePath); return T2ERROR_FAILURE; } - stat(filePath, &filestat); - fread(data, sizeof(char), filestat.st_size, fp); + if (stat(filePath, &filestat) != 0) + { + T2Error("Unable to stat file : %s\n", filePath); + fclose(fp); + return T2ERROR_FAILURE; + } + if (fread(data, sizeof(char), filestat.st_size, fp) != (size_t)filestat.st_size) + { + T2Error("Failed to read complete data from file : %s\n", filePath); + fclose(fp); + return T2ERROR_FAILURE; + } *privMode = strdup(data); if(fclose(fp) != 0) { diff --git a/source/utils/t2collection.c b/source/utils/t2collection.c index 901b3bfc..73075294 100644 --- a/source/utils/t2collection.c +++ b/source/utils/t2collection.c @@ -105,12 +105,14 @@ void *t2_queue_remove(queue_t *q, uint32_t index) element_t *e, *tmp = NULL; void *data; uint32_t i = 0; + uint32_t count; if(q == NULL) { return NULL; } - if (index > (t2_queue_count(q) - 1)) + count = t2_queue_count(q); + if (count == 0 || index >= count) { return NULL; } @@ -146,12 +148,14 @@ void *t2_queue_peek(queue_t *q, uint32_t index) { element_t *e; uint32_t i = 0; + uint32_t count; if(q == NULL) { return NULL; } - if (index > (t2_queue_count(q) - 1)) + count = t2_queue_count(q); + if (count == 0 || index >= count) { return NULL; } diff --git a/source/xconf-client/xconfclient.c b/source/xconf-client/xconfclient.c index 36577ad5..61a608bf 100644 --- a/source/xconf-client/xconfclient.c +++ b/source/xconf-client/xconfclient.c @@ -222,7 +222,6 @@ static char *getTimezone () { fclose(file); free(jsonDoc); - jsonDoc = NULL; T2Debug("Failed to read Timezone value from %s file...\n", jsonpath); continue; } @@ -252,7 +251,6 @@ static char *getTimezone () } } free(jsonDoc); - jsonDoc = NULL; cJSON_Delete(root); } count++; @@ -281,7 +279,6 @@ static char *getTimezone () if(zoneValue) { free(zoneValue); - zoneValue = NULL ; } zoneValue = strdup(zone); } @@ -818,7 +815,10 @@ T2ERROR doHttpGet(char* httpsUrl, char **data) // Share data with parent close(sharedPipeFdDataLen[0]); - write(sharedPipeFdDataLen[1], &len, sizeof(size_t)); + if (write(sharedPipeFdDataLen[1], &len, sizeof(size_t)) != sizeof(size_t)) + { + T2Error("Failed to write data length to pipe\n"); + } close(sharedPipeFdDataLen[1]); FILE *httpOutput = fopen(HTTP_RESPONSE_FILE, "w+"); @@ -905,7 +905,10 @@ T2ERROR doHttpGet(char* httpsUrl, char **data) status_return : close(sharedPipeFdStatus[0]); - write(sharedPipeFdStatus[1], &ret, sizeof(T2ERROR)); + if (write(sharedPipeFdStatus[1], &ret, sizeof(T2ERROR)) != sizeof(T2ERROR)) + { + T2Error("Failed to write status to pipe\n"); + } close(sharedPipeFdStatus[1]); exit(0); @@ -1296,12 +1299,21 @@ static void* getUpdatedConfigurationThread(void *data) #endif pthread_mutex_lock(&xcThreadMutex); stopFetchRemoteConfiguration = false ; + pthread_mutex_unlock(&xcThreadMutex); do { T2Debug("%s while Loop -- START \n", __FUNCTION__); - while(!stopFetchRemoteConfiguration && (urlFetchStatus = getRemoteConfigURL(&configURL)) != T2ERROR_SUCCESS) + pthread_mutex_lock(&xcThreadMutex); + while(!stopFetchRemoteConfiguration) { + pthread_mutex_unlock(&xcThreadMutex); + urlFetchStatus = getRemoteConfigURL(&configURL); + pthread_mutex_lock(&xcThreadMutex); + if(urlFetchStatus == T2ERROR_SUCCESS) + { + break; + } if (urlFetchStatus == T2ERROR_INVALID_RESPONSE) { T2Info("Config URL is not set to valid value. Xconfclient shall not proceed for T1.0 settings fetch attempts \n"); @@ -1315,7 +1327,9 @@ static void* getUpdatedConfigurationThread(void *data) _ts.tv_sec = _now.tv_sec + RFC_RETRY_TIMEOUT; T2Info("Waiting for %d sec before trying getRemoteConfigURL\n", RFC_RETRY_TIMEOUT); - n = pthread_cond_timedwait(&xcCond, &xcMutex, &_ts); + do { + n = pthread_cond_timedwait(&xcCond, &xcMutex, &_ts); + } while(n != ETIMEDOUT && n != 0); if(n == ETIMEDOUT) { T2Info("TIMEDOUT -- trying fetchConfigURLs again\n"); @@ -1330,9 +1344,12 @@ static void* getUpdatedConfigurationThread(void *data) } pthread_mutex_unlock(&xcMutex); } + pthread_mutex_unlock(&xcThreadMutex); + pthread_mutex_lock(&xcThreadMutex); while(!stopFetchRemoteConfiguration) { + pthread_mutex_unlock(&xcThreadMutex); T2ERROR ret = T2ERROR_FAILURE ; if ( urlFetchStatus == T2ERROR_INVALID_RESPONSE ) { @@ -1392,6 +1409,7 @@ static void* getUpdatedConfigurationThread(void *data) free(configData); configData = NULL ; } + pthread_mutex_lock(&xcThreadMutex); break; } else if(ret == T2ERROR_PROFILE_NOT_SET) @@ -1402,6 +1420,7 @@ static void* getUpdatedConfigurationThread(void *data) free(configData); configData = NULL ; } + pthread_mutex_lock(&xcThreadMutex); break; } else @@ -1416,6 +1435,7 @@ static void* getUpdatedConfigurationThread(void *data) { T2Error("Reached max xconf retry counts : %d, Using saved profile if exists until next reboot\n", MAX_XCONF_RETRY_COUNT); xConfRetryCount = 0; + pthread_mutex_lock(&xcThreadMutex); break; } T2Info("Waiting for %d sec before trying fetchRemoteConfiguration, No.of tries : %d\n", XCONF_RETRY_TIMEOUT, xConfRetryCount); @@ -1427,7 +1447,9 @@ static void* getUpdatedConfigurationThread(void *data) clock_gettime(CLOCK_REALTIME, &_now); _ts.tv_sec = _now.tv_sec + XCONF_RETRY_TIMEOUT; - n = pthread_cond_timedwait(&xcCond, &xcMutex, &_ts); + do { + n = pthread_cond_timedwait(&xcCond, &xcMutex, &_ts); + } while(n != ETIMEDOUT && n != 0); if(n == ETIMEDOUT) { T2Info("TIMEDOUT -- trying fetchConfigurations again\n"); @@ -1442,7 +1464,9 @@ static void* getUpdatedConfigurationThread(void *data) } pthread_mutex_unlock(&xcMutex); } + pthread_mutex_lock(&xcThreadMutex); } // End of config fetch while + pthread_mutex_unlock(&xcThreadMutex); if(configFetch == T2ERROR_FAILURE && !ProfileXConf_isSet()) { T2Error("Failed to fetch updated configuration and no saved configurations on disk for XCONF, uninitializing the process\n"); @@ -1467,13 +1491,14 @@ static void* getUpdatedConfigurationThread(void *data) } } #endif + pthread_mutex_lock(&xcThreadMutex); stopFetchRemoteConfiguration = true; T2Debug("%s while Loop -- END; wait for restart event\n", __FUNCTION__); pthread_cond_wait(&xcThreadCond, &xcThreadMutex); + pthread_mutex_unlock(&xcThreadMutex); } while(isXconfInit); //End of do while loop - pthread_mutex_unlock(&xcThreadMutex); // pthread_detach(pthread_self()); commenting this line as thread will detached by stopXConfClient T2Debug("%s --out\n", __FUNCTION__); return NULL; @@ -1495,9 +1520,9 @@ void uninitXConfClient() { T2Debug("XConfClientThread is stopped already\n"); } + pthread_mutex_lock(&xcThreadMutex); if(isXconfInit) { - pthread_mutex_lock(&xcThreadMutex); isXconfInit = false; pthread_cond_signal(&xcThreadCond); pthread_mutex_unlock(&xcThreadMutex); @@ -1514,6 +1539,10 @@ void uninitXConfClient() xcCertSelectorFree(); #endif } + else + { + pthread_mutex_unlock(&xcThreadMutex); + } T2Debug("%s --out\n", __FUNCTION__); T2Info("Uninit XConf Client Successful\n"); } @@ -1591,8 +1620,10 @@ T2ERROR stopXConfClient() { T2Debug("%s ++in\n", __FUNCTION__); //pthread_detach(xcrThread); - pthread_mutex_lock(&xcMutex); + pthread_mutex_lock(&xcThreadMutex); stopFetchRemoteConfiguration = true; + pthread_mutex_unlock(&xcThreadMutex); + pthread_mutex_lock(&xcMutex); pthread_cond_signal(&xcCond); pthread_mutex_unlock(&xcMutex); T2Debug("%s --out\n", __FUNCTION__); @@ -1602,8 +1633,10 @@ T2ERROR stopXConfClient() T2ERROR startXConfClient() { T2Debug("%s ++in\n", __FUNCTION__); + pthread_mutex_lock(&xcThreadMutex); if (isXconfInit) { + pthread_mutex_unlock(&xcThreadMutex); //pthread_create(&xcrThread, NULL, getUpdatedConfigurationThread, NULL); pthread_mutex_lock(&xcMutex); pthread_cond_signal(&xcCond); @@ -1616,6 +1649,7 @@ T2ERROR startXConfClient() else { T2Info("getUpdatedConfigurationThread is still active ... Ignore xconf reload \n"); + pthread_mutex_unlock(&xcThreadMutex); } T2Debug("%s --out\n", __FUNCTION__); return T2ERROR_SUCCESS;