diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2014-07-16 12:02:47 +0200 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2014-07-16 12:02:47 +0200 |
commit | 70fba5db14cd84f8a2f38feabff6efab07699a5d (patch) | |
tree | 354acb31a7fde3d064fca0b96ca6f6a2d0b209b3 | |
parent | 28823cabacd175c09c80849e5b4468fd0a828a9f (diff) | |
download | persistence-client-library-70fba5db14cd84f8a2f38feabff6efab07699a5d.tar.gz |
Optimized config file loading; bugfix in custom plugin loading, load only if config file is not empty
-rw-r--r-- | src/persistence_client_library_backup_filelist.c | 101 | ||||
-rw-r--r-- | src/persistence_client_library_custom_loader.c | 132 |
2 files changed, 110 insertions, 123 deletions
diff --git a/src/persistence_client_library_backup_filelist.c b/src/persistence_client_library_backup_filelist.c index 0af204e..30a79fa 100644 --- a/src/persistence_client_library_backup_filelist.c +++ b/src/persistence_client_library_backup_filelist.c @@ -48,12 +48,7 @@ static void key_val_rel(void *p); static void* key_val_dup(void *p); - -static char* gpConfigFileMap = 0; static char* gpTokenArray[TOKENARRAYSIZE] = {0}; -static int gTokenCounter = 0; -static unsigned int gConfigFileSize = 0; - /// the rb tree static jsw_rbtree_t *gRb_tree_bl = NULL; @@ -63,17 +58,18 @@ static jsw_rbtree_t *gRb_tree_bl = NULL; static int need_backup_key(unsigned int key); static int key_val_cmp(const void *p1, const void *p2 ); -static void fillFileBackupCharTokenArray() +static void fillFileBackupCharTokenArray(unsigned int customConfigFileSize, char* fileMap) { unsigned int i=0; + int tokenCounter = 0; int blankCount=0; - char* tmpPointer = gpConfigFileMap; + char* tmpPointer = fileMap; // set the first pointer to the start of the file gpTokenArray[blankCount] = tmpPointer; blankCount++; - while(i < gConfigFileSize) + while(i < customConfigFileSize) { if( ((unsigned int)*tmpPointer < 127) && ((unsigned int)*tmpPointer >= 0)) @@ -89,7 +85,7 @@ static void fillFileBackupCharTokenArray() } gpTokenArray[blankCount] = tmpPointer+1; blankCount++; - gTokenCounter++; + tokenCounter++; } } tmpPointer++; @@ -141,63 +137,62 @@ static void createAndStoreFileNames() int readBlacklistConfigFile(const char* filename) { - int fd = 0, - status = 0, - rval = 0; - - struct stat buffer; + int rval = 0; if(filename != NULL) { - memset(&buffer, 0, sizeof(buffer)); - status = stat(filename, &buffer); - if(status != -1) - { - gConfigFileSize = buffer.st_size; - } + struct stat buffer; - fd = open(filename, O_RDONLY); - if (fd == -1) - { - DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error file open"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); - return -1; - } - - // check for empty file - if(gConfigFileSize == 0) + memset(&buffer, 0, sizeof(buffer)); + if(stat(filename, &buffer) != -1) { - DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error file size is 0:"), DLT_STRING(filename)); - close(fd); - return -1; - } + if(buffer.st_size > 0) // check for empty file + { + char* configFileMap = 0; + int fd = open(filename, O_RDONLY); - // map the config file into memory - gpConfigFileMap = (char*)mmap(0, gConfigFileSize, PROT_WRITE, MAP_PRIVATE, fd, 0); + if(fd == -1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error file open"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); + return EPERS_COMMON; + } - if (gpConfigFileMap == MAP_FAILED) - { - gpConfigFileMap = 0; - close(fd); - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("configReader::readConfigFile ==> Error mapping the file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); + // map the config file into memory + configFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0); - return -1; - } + if(configFileMap == MAP_FAILED) + { + close(fd); + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("configReader::readConfigFile ==> Error mapping the file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)) ); - // reset the token counter - gTokenCounter = 0; + return EPERS_COMMON; + } - fillFileBackupCharTokenArray(); + fillFileBackupCharTokenArray(buffer.st_size, configFileMap); - // create filenames and store them in the tree - createAndStoreFileNames(); + // create filenames and store them in the tree + createAndStoreFileNames(); - munmap(gpConfigFileMap, gConfigFileSize); + munmap(configFileMap, buffer.st_size); - close(fd); - } + close(fd); + } + else + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error config file size is 0:"), DLT_STRING(filename)); + return EPERS_COMMON; + } + } + else + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> failed to stat() config file:"), DLT_STRING(filename)); + return EPERS_COMMON; + } + } else { - rval = -1; + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> config file name is NULL:")); + rval = EPERS_COMMON; } return rval; @@ -209,11 +204,11 @@ int need_backup_key(unsigned int key) { int rval = CREATE_BACKUP; key_value_s* item = NULL; - key_value_s* foundItem = NULL; item = malloc(sizeof(key_value_s)); if(item != NULL && gRb_tree_bl != NULL) { + key_value_s* foundItem = NULL; item->key = key; foundItem = (key_value_s*)jsw_rbfind(gRb_tree_bl, item); if(foundItem != NULL) @@ -402,7 +397,7 @@ int pclVerifyConsistency(const char* origPath, const char* backupPath, const cha // ************************************************* // there is a backup file and a checksum // ************************************************* - if( (backupAvail == 0) && (csumAvail == 0) ) + if((backupAvail == 0) && (csumAvail == 0) ) { DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclVerifyConsistency => there is a backup file AND a checksum")); // calculate checksum form backup file diff --git a/src/persistence_client_library_custom_loader.c b/src/persistence_client_library_custom_loader.c index 8a5c1a4..f9633cd 100644 --- a/src/persistence_client_library_custom_loader.c +++ b/src/persistence_client_library_custom_loader.c @@ -42,25 +42,22 @@ typedef struct sPersCustomLibInfo /// array with custom client library names static PersCustomLibInfo gCustomLibArray[PersCustomLib_LastEntry]; - -static char* gpCustomConfigFileMap = 0; static char* gpCustomTokenArray[TOKENARRAYSIZE]; -static int gCustomTokenCounter = 0; -static unsigned int gCustomConfigFileSize = 0; int(* gPlugin_callback_async_t)(int errcode); -static void fillCustomCharTokenArray() +static void fillCustomCharTokenArray(unsigned int customConfigFileSize, char* fileMap) { unsigned int i=0; + int customTokenCounter = 0; int blankCount=0; - char* tmpPointer = gpCustomConfigFileMap; + char* tmpPointer = fileMap; // set the first pointer to the start of the file gpCustomTokenArray[blankCount] = tmpPointer; blankCount++; - while(i < gCustomConfigFileSize) + while(i < customConfigFileSize) { if(1 != gCharLookup[(int)*tmpPointer]) { @@ -73,7 +70,7 @@ static void fillCustomCharTokenArray() } gpCustomTokenArray[blankCount] = tmpPointer+1; blankCount++; - gCustomTokenCounter++; + customTokenCounter++; } tmpPointer++; @@ -210,10 +207,8 @@ PersistenceCustomLibs_e custom_client_name_to_id(const char* lib_name, int subst int get_custom_libraries() { - int rval = 0, fd = 0, j = 0, i= 0; - int status = 0; + int rval = 0, j = 0; struct stat buffer; - const char *filename = getenv("PERS_CLIENT_LIB_CUSTOM_LOAD"); if(filename == NULL) @@ -223,82 +218,79 @@ int get_custom_libraries() for(j=0; j<PersCustomLib_LastEntry; j++) { - // init pos to -1 - gCustomLibArray[j].valid = -1; + gCustomLibArray[j].valid = -1; // init pos to -1 } memset(&buffer, 0, sizeof(buffer)); - status = stat(filename, &buffer); - if(status != -1) - { - gCustomConfigFileSize = buffer.st_size; - } - - fd = open(filename, O_RDONLY); - if (fd == -1) + if(stat(filename, &buffer) != -1) { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error ==> Error file open: "), - DLT_STRING(filename), DLT_STRING("err msg: "), DLT_STRING(strerror(errno)) ); - - return EPERS_COMMON; - } - - // check for empty file - if(gCustomConfigFileSize >= 0) - { - // map the config file into memory - gpCustomConfigFileMap = (char*)mmap(0, gCustomConfigFileSize, PROT_WRITE, MAP_PRIVATE, fd, 0); - - if (gpCustomConfigFileMap == MAP_FAILED) + if(buffer.st_size > 0) // check for empty file { - gpCustomConfigFileMap = 0; - close(fd); - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error ==> Error mapping the file")); - return EPERS_COMMON; - } + char* customConfFileMap = NULL; + int i = 0; + int fd = open(filename, O_RDONLY); - // reset the token counter - gCustomTokenCounter = 0; + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("load custom library config file ==> "), DLT_STRING(filename)); - fillCustomCharTokenArray(); + if (fd == -1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load custom library config file error ==> Error file open: "), + DLT_STRING(filename), DLT_STRING("err msg: "), DLT_STRING(strerror(errno)) ); + return EPERS_COMMON; + } - while( i < TOKENARRAYSIZE ) - { - if(gpCustomTokenArray[i] != 0 && gpCustomTokenArray[i+1] != 0 && gpCustomTokenArray[i+2] != 0 &&gpCustomTokenArray[i+3] != 0 ) + // map the config file into memory + customConfFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0); + + if (customConfFileMap == MAP_FAILED) { - int libId = custom_client_name_to_id(gpCustomTokenArray[i], 0); // get the custom libID - - // assign the libraryname - strncpy(gCustomLibArray[libId].libname, gpCustomTokenArray[i+1], CustLibMaxLen); - gCustomLibArray[libId].libname[CustLibMaxLen-1] = '\0'; // Ensures 0-Termination - - gCustomLibArray[libId].loadingType = getLoadingType(gpCustomTokenArray[i+2]); - gCustomLibArray[libId].initFunction = getInitType(gpCustomTokenArray[i+3]); - gCustomLibArray[libId].valid = 1; // marks as valid; -#if 0 - // debug - printf(" 1. => %s => %d \n", gpCustomTokenArray[i], libId); - printf(" 2. => %s => %s \n", gpCustomTokenArray[i+1], gCustomLibArray[libId].libname); - printf(" 3. => %s => %d \n", gpCustomTokenArray[i+2], (int)gCustomLibArray[libId].initFunction); - printf(" 4. => %s => %d \n\n", gpCustomTokenArray[i+3], (int)gCustomLibArray[libId].loadingType); -#endif + close(fd); + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load custom library config file error ==> Error mapping the file")); + return EPERS_COMMON; } - else + + fillCustomCharTokenArray(buffer.st_size, customConfFileMap); + + while( i < TOKENARRAYSIZE ) { - break; + if(gpCustomTokenArray[i] != 0 && gpCustomTokenArray[i+1] != 0 && gpCustomTokenArray[i+2] != 0 &&gpCustomTokenArray[i+3] != 0 ) + { + int libId = custom_client_name_to_id(gpCustomTokenArray[i], 0); // get the custom libID + + // assign the libraryname + strncpy(gCustomLibArray[libId].libname, gpCustomTokenArray[i+1], CustLibMaxLen); + gCustomLibArray[libId].libname[CustLibMaxLen-1] = '\0'; // Ensures 0-Termination + + gCustomLibArray[libId].loadingType = getLoadingType(gpCustomTokenArray[i+2]); + gCustomLibArray[libId].initFunction = getInitType(gpCustomTokenArray[i+3]); + gCustomLibArray[libId].valid = 1; // marks as valid; + #if 0 + // debug + printf(" 1. => %s => %d \n", gpCustomTokenArray[i], libId); + printf(" 2. => %s => %s \n", gpCustomTokenArray[i+1], gCustomLibArray[libId].libname); + printf(" 3. => %s => %d \n", gpCustomTokenArray[i+2], (int)gCustomLibArray[libId].initFunction); + printf(" 4. => %s => %d \n\n", gpCustomTokenArray[i+3], (int)gCustomLibArray[libId].loadingType); + #endif + } + else + { + break; + } + i+=4; // move to the next configuration file entry } - i+=4; // move to the next configuration file entry + close(fd); + } + else + { + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load custom library config file error ==> Error file size is 0")); + rval = EPERS_COMMON; } - - close(fd); } else { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error ==> Error file size is 0")); - close(fd); - rval = EPERS_COMMON; + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load custom library config file error ==> failed to stat() file")); + rval = EPERS_COMMON; } - return rval; } |