diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2014-11-07 14:21:27 +0100 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2014-11-07 14:21:27 +0100 |
commit | a318b50bdedb370a6cf83a87720f1bd92f132eb3 (patch) | |
tree | 97931c02933788a24a46f162092d4b1389d80817 | |
parent | adbfc3e8b2fa6ab4962667624c008b36e24d7bf9 (diff) | |
download | persistence-client-library-a318b50bdedb370a6cf83a87720f1bd92f132eb3.tar.gz |
Rework of application security check; if a not trusted application tries to access persistent data, the error code EPERS_SHUTDOWN_NO_TRUSTED will be returned
-rw-r--r-- | include/persistence_client_library_error_def.h | 3 | ||||
-rw-r--r-- | src/persistence_client_library.c | 71 | ||||
-rw-r--r-- | src/persistence_client_library_file.c | 50 | ||||
-rw-r--r-- | src/persistence_client_library_key.c | 379 | ||||
-rw-r--r-- | test/persistence_client_library_test.c | 106 |
5 files changed, 408 insertions, 201 deletions
diff --git a/include/persistence_client_library_error_def.h b/include/persistence_client_library_error_def.h index f8e184b..7677e51 100644 --- a/include/persistence_client_library_error_def.h +++ b/include/persistence_client_library_error_def.h @@ -19,6 +19,7 @@ * * @par change history * Date Author Version + * 07/11/14 Ingo Huerner 1.0.1 - Added trusted application error * 29/04/14 Ingo Huerner 1.0.0 - Added cancel shutdown errors * */ @@ -116,6 +117,8 @@ extern "C" { #define EPERS_SHUTDOWN_MAX_CANCEL (-40) /// not permitted to use this function #define EPERS_SHUTDOWN_NO_PERMIT (-42) +/// not a trusted application,no access to persistence data +#define EPERS_SHUTDOWN_NO_TRUSTED (-43) /// requested handle is not valid. \since PCL v7.0.3 #define EPERS_INVALID_HANDLE (-1000) diff --git a/src/persistence_client_library.c b/src/persistence_client_library.c index e01a8f2..9bae350 100644 --- a/src/persistence_client_library.c +++ b/src/persistence_client_library.c @@ -53,6 +53,8 @@ DLT_DECLARE_CONTEXT(gPclDLTContext); static int gShutdownMode = 0; /// global shutdown cancel counter static int gCancelCounter = 0; +/// global flag +static int gAppCheckFlag = -1; int customAsyncInitClbk(int errcode) @@ -63,6 +65,55 @@ int customAsyncInitClbk(int errcode) } + +/* security check for valid application: + if the RCT table exists, the application is proven to be valid (trusted), + otherwise return EPERS_NOPRCTABLE */ +void doInitAppcheck(const char* appName) +{ +#if USE_APPCHECK + char rctFilename[DbPathMaxLen] = {0}; + snprintf(rctFilename, DbPathMaxLen, gLocalWtPathKey, appName, gResTableCfg); + + if(access(rctFilename, F_OK) == 0) + { + gAppCheckFlag = 1; // "trusted" application + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary - app check: "), DLT_STRING(appName), DLT_STRING("is a trusted application")); + } + else + { + gAppCheckFlag = 0; // currently not a "trusted" application + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary - app check: "), DLT_STRING(appName), DLT_STRING("is NOT a trusted application")); + } +#endif +} + + + +int doAppcheck(void) +{ + int trusted = 1; +#if USE_APPCHECK + if(gAppCheckFlag != 1) + { + char rctFilename[DbPathMaxLen] = {0}; + snprintf(rctFilename, DbPathMaxLen, gLocalWtPathKey, gAppId, gResTableCfg); + if(access(rctFilename, F_OK) == 0) + { + gAppCheckFlag = 1; // "trusted" application + } + else + { + gAppCheckFlag = 0; // not a "trusted" application + trusted = 0; + } + } +#endif + return trusted; +} + + + int pclInitLibrary(const char* appName, int shutdownMode) { int rval = 1; @@ -73,24 +124,15 @@ int pclInitLibrary(const char* appName, int shutdownMode) if(gPclInitialized == PCLnotInitialized) { - char rctFilename[DbPathMaxLen] = {0}; gShutdownMode = shutdownMode; DLT_REGISTER_CONTEXT(gPclDLTContext,"PCL","Context for persistence client library logging"); DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T Persistence Client Library - "), DLT_STRING(appName), DLT_STRING("- init counter: "), DLT_INT(gPclInitialized) ); -#if USE_APPCHECK - printf("SECURITY check enabled!!!!!\n"); - /* security check for valid application: - if the RCT table exists, the application is proven to be valid, - otherwise return EPERS_NOPRCTABLE */ - snprintf(rctFilename, DbPathMaxLen, gLocalWtPathKey, appName, gResTableCfg); + char blacklistPath[DbPathMaxLen] = {0}; - if(access(rctFilename, F_OK) == 0) - { -#endif - char blacklistPath[DbPathMaxLen] = {0}; + doInitAppcheck(appName); // check if we have a trusted application #if USE_FILECACHE DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using the filecache!!!")); @@ -163,13 +205,6 @@ int pclInitLibrary(const char* appName, int shutdownMode) gAppId[MaxAppNameLen-1] = '\0'; gPclInitialized++; -#if USE_APPCHECK - } - else - { - rval = EPERS_NOPRCTABLE; - } -#endif } else if(gPclInitialized >= PCLinitialized) { diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c index ecd6e23..3ea7670 100644 --- a/src/persistence_client_library_file.c +++ b/src/persistence_client_library_file.c @@ -45,6 +45,7 @@ // local function prototype int pclFileGetDefaultData(int handle, const char* resource_id, int policy); +extern int doAppcheck(void); char* get_raw_string(char* dbKey) { @@ -76,38 +77,45 @@ int pclFileClose(int fd) if(gPclInitialized >= PCLinitialized) { - int permission = get_file_permission(fd); - - if(permission != -1) // permission is here also used for range check + if(doAppcheck() == 1) { - // check if a backup and checksum file needs to be deleted - if(permission != PersistencePermission_ReadOnly || permission != PersistencePermission_LastEntry) + int permission = get_file_permission(fd); + + if(permission != -1) // permission is here also used for range check { - // remove backup file - remove(get_file_backup_path(fd)); // we don't care about return value + // check if a backup and checksum file needs to be deleted + if(permission != PersistencePermission_ReadOnly || permission != PersistencePermission_LastEntry) + { + // remove backup file + remove(get_file_backup_path(fd)); // we don't care about return value - // remove checksum file - remove(get_file_checksum_path(fd)); // we don't care about return value + // remove checksum file + remove(get_file_checksum_path(fd)); // we don't care about return value + + } + __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed); // set closed flag + #if USE_FILECACHE + if(get_file_cache_status(fd) == 1) + { + rval = pfcCloseFile(fd); + } + else + { + rval = close(fd); + } + #else + rval = close(fd); + #endif - } - __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed); // set closed flag -#if USE_FILECACHE - if(get_file_cache_status(fd) == 1) - { - rval = pfcCloseFile(fd); } else { - rval = close(fd); + rval = EPERS_MAXHANDLE; } -#else - rval = close(fd); -#endif - } else { - rval = EPERS_MAXHANDLE; + rval = EPERS_SHUTDOWN_NO_TRUSTED; } } return rval; diff --git a/src/persistence_client_library_key.c b/src/persistence_client_library_key.c index d2017a9..249c8e0 100644 --- a/src/persistence_client_library_key.c +++ b/src/persistence_client_library_key.c @@ -34,6 +34,9 @@ static int handleRegNotifyOnChange(int key_handle, pclChangeNotifyCallback_t cal static int regNotifyOnChange(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no, pclChangeNotifyCallback_t callback, PersNotifyRegPolicy_e regPolicy); + +extern int doAppcheck(void); + // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // function with handle @@ -45,34 +48,41 @@ int pclKeyHandleOpen(unsigned int ldbid, const char* resource_id, unsigned int u int rval = 0; int handle = EPERS_NOT_INITIALIZED; - if(gPclInitialized >= PCLinitialized) + if(gPclInitialized >= PCLinitialized ) { - PersistenceInfo_s dbContext; + if(doAppcheck() == 1) + { + PersistenceInfo_s dbContext; - char dbKey[DbKeyMaxLen] = {0}; // database key - char dbPath[DbPathMaxLen] = {0}; // database location + char dbKey[DbKeyMaxLen] = {0}; // database key + char dbPath[DbPathMaxLen] = {0}; // database location - dbContext.context.ldbid = ldbid; - dbContext.context.seat_no = seat_no; - dbContext.context.user_no = user_no; + dbContext.context.ldbid = ldbid; + dbContext.context.seat_no = seat_no; + dbContext.context.user_no = user_no; - // get database context: database path and database key - rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); - if((rval >= 0) && (dbContext.configKey.type == PersistenceResourceType_key)) // check if type matches - { - if(dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + // get database context: database path and database key + rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); + if((rval >= 0) && (dbContext.configKey.type == PersistenceResourceType_key)) // check if type matches { - // remember data in handle array - handle = set_key_handle_data(get_persistence_handle_idx(), resource_id, ldbid, user_no, seat_no); + if(dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + { + // remember data in handle array + handle = set_key_handle_data(get_persistence_handle_idx(), resource_id, ldbid, user_no, seat_no); + } + else + { + handle = EPERS_BADPOL; + } } else { - handle = EPERS_BADPOL; + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyHandleOpen - no database context or resource is not a key ")); } } else { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyHandleOpen - no database context or resource is not a key ")); + handle = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -87,25 +97,32 @@ int pclKeyHandleClose(int key_handle) if(gPclInitialized >= PCLinitialized) { - PersistenceKeyHandle_s persHandle; - - if(get_key_handle_data(key_handle, &persHandle) != -1) + if(doAppcheck() == 1) { - if ('\0' != persHandle.resource_id[0]) + PersistenceKeyHandle_s persHandle; + + if(get_key_handle_data(key_handle, &persHandle) != -1) { - /* Invalidate key handle data */ - set_persistence_handle_close_idx(key_handle); - clear_key_handle_array(key_handle); - rval = 1; + if ('\0' != persHandle.resource_id[0]) + { + /* Invalidate key handle data */ + set_persistence_handle_close_idx(key_handle); + clear_key_handle_array(key_handle); + rval = 1; + } + else + { + rval = EPERS_INVALID_HANDLE; + } } else { - rval = EPERS_INVALID_HANDLE; + rval = EPERS_MAXHANDLE; } } else { - rval = EPERS_MAXHANDLE; + rval = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -120,23 +137,30 @@ int pclKeyHandleGetSize(int key_handle) if(gPclInitialized >= PCLinitialized) { - PersistenceKeyHandle_s persHandle; - - if(get_key_handle_data(key_handle, &persHandle) != -1) + if(doAppcheck() == 1) { - if ('\0' != persHandle.resource_id[0]) + PersistenceKeyHandle_s persHandle; + + if(get_key_handle_data(key_handle, &persHandle) != -1) { - size = pclKeyGetSize(persHandle.ldbid, persHandle.resource_id, - persHandle.user_no, persHandle.seat_no); + if ('\0' != persHandle.resource_id[0]) + { + size = pclKeyGetSize(persHandle.ldbid, persHandle.resource_id, + persHandle.user_no, persHandle.seat_no); + } + else + { + size = EPERS_INVALID_HANDLE; + } } else { - size = EPERS_INVALID_HANDLE; + size = EPERS_MAXHANDLE; } } else { - size = EPERS_MAXHANDLE; + size = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -151,24 +175,31 @@ int pclKeyHandleReadData(int key_handle, unsigned char* buffer, int buffer_size) if(gPclInitialized >= PCLinitialized) { - PersistenceKeyHandle_s persHandle; - - if(get_key_handle_data(key_handle, &persHandle) != -1) + if(doAppcheck() == 1) { - if ('\0' != persHandle.resource_id[0]) + PersistenceKeyHandle_s persHandle; + + if(get_key_handle_data(key_handle, &persHandle) != -1) { - size = pclKeyReadData(persHandle.ldbid, persHandle.resource_id, - persHandle.user_no, persHandle.seat_no, - buffer, buffer_size); + if ('\0' != persHandle.resource_id[0]) + { + size = pclKeyReadData(persHandle.ldbid, persHandle.resource_id, + persHandle.user_no, persHandle.seat_no, + buffer, buffer_size); + } + else + { + size = EPERS_INVALID_HANDLE; + } } else { - size = EPERS_INVALID_HANDLE; + size = EPERS_MAXHANDLE; } } else { - size = EPERS_MAXHANDLE; + size = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -238,24 +269,31 @@ int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size if(gPclInitialized >= PCLinitialized) { - PersistenceKeyHandle_s persHandle; - - if(get_key_handle_data(key_handle, &persHandle) != -1) + if(doAppcheck() == 1) { - if ('\0' != persHandle.resource_id[0]) + PersistenceKeyHandle_s persHandle; + + if(get_key_handle_data(key_handle, &persHandle) != -1) { - size = pclKeyWriteData(persHandle.ldbid, persHandle.resource_id, - persHandle.user_no, persHandle.seat_no, - buffer, buffer_size); + if ('\0' != persHandle.resource_id[0]) + { + size = pclKeyWriteData(persHandle.ldbid, persHandle.resource_id, + persHandle.user_no, persHandle.seat_no, + buffer, buffer_size); + } + else + { + size = EPERS_INVALID_HANDLE; + } } else { - size = EPERS_INVALID_HANDLE; + size = EPERS_MAXHANDLE; } } else { - size = EPERS_MAXHANDLE; + size = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -278,35 +316,42 @@ int pclKeyDelete(unsigned int ldbid, const char* resource_id, unsigned int user_ if(gPclInitialized >= PCLinitialized) { - if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked + if(doAppcheck() == 1) { - PersistenceInfo_s dbContext; + if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked + { + PersistenceInfo_s dbContext; - char dbKey[DbKeyMaxLen] = {0}; // database key - char dbPath[DbPathMaxLen] = {0}; // database location + char dbKey[DbKeyMaxLen] = {0}; // database key + char dbPath[DbPathMaxLen] = {0}; // database location - dbContext.context.ldbid = ldbid; - dbContext.context.seat_no = seat_no; - dbContext.context.user_no = user_no; + dbContext.context.ldbid = ldbid; + dbContext.context.seat_no = seat_no; + dbContext.context.user_no = user_no; - // get database context: database path and database key - rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); - if( (rval >= 0) - && (dbContext.configKey.type == PersistenceResourceType_key) ) // check if type is matching - { - if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid - { - rval = persistence_delete_data(dbPath, dbKey, resource_id, &dbContext); - } - else + // get database context: database path and database key + rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); + if( (rval >= 0) + && (dbContext.configKey.type == PersistenceResourceType_key) ) // check if type is matching { - rval = EPERS_BADPOL; + if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + { + rval = persistence_delete_data(dbPath, dbKey, resource_id, &dbContext); + } + else + { + rval = EPERS_BADPOL; + } } - } + } + else + { + rval = EPERS_LOCKFS; + } } else { - rval = EPERS_LOCKFS; + rval = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -322,23 +367,30 @@ int pclKeyGetSize(unsigned int ldbid, const char* resource_id, unsigned int user if(gPclInitialized >= PCLinitialized) { - PersistenceInfo_s dbContext; + if(doAppcheck() == 1) + { + PersistenceInfo_s dbContext; - char dbKey[DbKeyMaxLen] = {0}; // database key - char dbPath[DbPathMaxLen] = {0}; // database location + char dbKey[DbKeyMaxLen] = {0}; // database key + char dbPath[DbPathMaxLen] = {0}; // database location - dbContext.context.ldbid = ldbid; - dbContext.context.seat_no = seat_no; - dbContext.context.user_no = user_no; + dbContext.context.ldbid = ldbid; + dbContext.context.seat_no = seat_no; + dbContext.context.user_no = user_no; - // get database context: database path and database key - data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); - if( (data_size >= 0) - && (dbContext.configKey.type == PersistenceResourceType_key) ) // check if type matches - { - if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + // get database context: database path and database key + data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); + if( (data_size >= 0) + && (dbContext.configKey.type == PersistenceResourceType_key) ) // check if type matches { - data_size = persistence_get_data_size(dbPath, dbKey, resource_id, &dbContext); + if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + { + data_size = persistence_get_data_size(dbPath, dbKey, resource_id, &dbContext); + } + else + { + data_size = EPERS_BADPOL; + } } else { @@ -347,7 +399,7 @@ int pclKeyGetSize(unsigned int ldbid, const char* resource_id, unsigned int user } else { - data_size = EPERS_BADPOL; + data_size = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -364,40 +416,47 @@ int pclKeyReadData(unsigned int ldbid, const char* resource_id, unsigned int use if(gPclInitialized >= PCLinitialized) { - if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked + if(doAppcheck() == 1) { - PersistenceInfo_s dbContext; - - char dbKey[DbKeyMaxLen] = {0}; // database key - char dbPath[DbPathMaxLen] = {0}; // database location + if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked + { + PersistenceInfo_s dbContext; - dbContext.context.ldbid = ldbid; - dbContext.context.seat_no = seat_no; - dbContext.context.user_no = user_no; + char dbKey[DbKeyMaxLen] = {0}; // database key + char dbPath[DbPathMaxLen] = {0}; // database location - // get database context: database path and database key - data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); - if( (data_size >= 0) - && (dbContext.configKey.type == PersistenceResourceType_key) ) - { + dbContext.context.ldbid = ldbid; + dbContext.context.seat_no = seat_no; + dbContext.context.user_no = user_no; - if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + // get database context: database path and database key + data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); + if( (data_size >= 0) + && (dbContext.configKey.type == PersistenceResourceType_key) ) { - data_size = persistence_get_data(dbPath, dbKey, resource_id, &dbContext, buffer, buffer_size); + + if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + { + data_size = persistence_get_data(dbPath, dbKey, resource_id, &dbContext, buffer, buffer_size); + } + else + { + data_size = EPERS_BADPOL; + } } else { - data_size = EPERS_BADPOL; + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyReadData - no database context or resource is not a key")); } } else { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyReadData - no database context or resource is not a key")); + data_size = EPERS_LOCKFS; } } else { - data_size = EPERS_LOCKFS; + data_size = EPERS_SHUTDOWN_NO_TRUSTED; } } @@ -413,60 +472,67 @@ int pclKeyWriteData(unsigned int ldbid, const char* resource_id, unsigned int us if(gPclInitialized >= PCLinitialized) { - if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked + if(doAppcheck() == 1) { - if(buffer_size <= gMaxKeyValDataSize) // check data size + if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked { - PersistenceInfo_s dbContext; + if(buffer_size <= gMaxKeyValDataSize) // check data size + { + PersistenceInfo_s dbContext; - unsigned int hash_val_data = 0; + unsigned int hash_val_data = 0; - char dbKey[DbKeyMaxLen] = {0}; // database key - char dbPath[DbPathMaxLen] = {0}; // database location + char dbKey[DbKeyMaxLen] = {0}; // database key + char dbPath[DbPathMaxLen] = {0}; // database location - dbContext.context.ldbid = ldbid; - dbContext.context.seat_no = seat_no; - dbContext.context.user_no = user_no; + dbContext.context.ldbid = ldbid; + dbContext.context.seat_no = seat_no; + dbContext.context.user_no = user_no; - // get database context: database path and database key - data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); - if( (data_size >= 0) - && (dbContext.configKey.type == PersistenceResourceType_key)) - { - if(dbContext.configKey.permission != PersistencePermission_ReadOnly) // don't write to a read only resource + // get database context: database path and database key + data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); + if( (data_size >= 0) + && (dbContext.configKey.type == PersistenceResourceType_key)) { - // get hash value of data to verify storing - hash_val_data = pclCrc32(hash_val_data, buffer, buffer_size); - - // store data - if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + if(dbContext.configKey.permission != PersistencePermission_ReadOnly) // don't write to a read only resource { - data_size = persistence_set_data(dbPath, dbKey, resource_id, &dbContext, buffer, buffer_size); + // get hash value of data to verify storing + hash_val_data = pclCrc32(hash_val_data, buffer, buffer_size); + + // store data + if( dbContext.configKey.storage < PersistenceStorage_LastEntry) // check if store policy is valid + { + data_size = persistence_set_data(dbPath, dbKey, resource_id, &dbContext, buffer, buffer_size); + } + else + { + data_size = EPERS_BADPOL; + } } else { - data_size = EPERS_BADPOL; + data_size = EPERS_RESOURCE_READ_ONLY; } } else { - data_size = EPERS_RESOURCE_READ_ONLY; + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyWriteData no database context or resource is not a key")); } } else { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyWriteData no database context or resource is not a key")); + data_size = EPERS_BUFLIMIT; + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyWriteData - buffer_size to big, limit is [bytes]:"), DLT_INT(gMaxKeyValDataSize)); } } else { - data_size = EPERS_BUFLIMIT; - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyWriteData - buffer_size to big, limit is [bytes]:"), DLT_INT(gMaxKeyValDataSize)); + data_size = EPERS_LOCKFS; } } else { - data_size = EPERS_LOCKFS; + data_size = EPERS_SHUTDOWN_NO_TRUSTED; } } return data_size; @@ -506,39 +572,46 @@ int regNotifyOnChange(unsigned int ldbid, const char* resource_id, unsigned int if(gPclInitialized >= PCLinitialized) { - PersistenceInfo_s dbContext; + if(doAppcheck() == 1) + { + PersistenceInfo_s dbContext; - // unsigned int hash_val_data = 0; - char dbKey[DbKeyMaxLen] = {0}; // database key - char dbPath[DbPathMaxLen] = {0}; // database location + // unsigned int hash_val_data = 0; + char dbKey[DbKeyMaxLen] = {0}; // database key + char dbPath[DbPathMaxLen] = {0}; // database location - dbContext.context.ldbid = ldbid; - dbContext.context.seat_no = seat_no; - dbContext.context.user_no = user_no; + dbContext.context.ldbid = ldbid; + dbContext.context.seat_no = seat_no; + dbContext.context.user_no = user_no; - // get database context: database path and database key - rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); + // get database context: database path and database key + rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath); - if (rval==0) // no error, key found - { - // registration is only on shared and custom keys possible - if( (dbContext.configKey.storage != PersistenceStorage_local) - && (dbContext.configKey.type == PersistenceResourceType_key) ) - { - rval = persistence_notify_on_change(resource_id, ldbid, user_no, seat_no, callback, regPolicy); + if (rval==0) // no error, key found + { + // registration is only on shared and custom keys possible + if( (dbContext.configKey.storage != PersistenceStorage_local) + && (dbContext.configKey.type == PersistenceResourceType_key) ) + { + rval = persistence_notify_on_change(resource_id, ldbid, user_no, seat_no, callback, regPolicy); + } + else + { + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("regNotifyOnChange - Not allowed! Resource is local or it is a file:"), + DLT_STRING(resource_id), DLT_STRING("LDBID:"), DLT_UINT(ldbid)); + rval = EPERS_NOTIFY_NOT_ALLOWED; + } } else { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("regNotifyOnChange - Not allowed! Resource is local or it is a file:"), - DLT_STRING(resource_id), DLT_STRING("LDBID:"), DLT_UINT(ldbid)); - rval = EPERS_NOTIFY_NOT_ALLOWED; + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, + DLT_STRING("regNotifyOnChange - Not possible! get_db_context() returned:"), + DLT_INT(rval)); } } else { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, - DLT_STRING("regNotifyOnChange - Not possible! get_db_context() returned:"), - DLT_INT(rval)); + rval = EPERS_SHUTDOWN_NO_TRUSTED; } } diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index 1c453da..980ddfe 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -1368,8 +1368,9 @@ START_TEST(test_ValidApplication) unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL; ret = pclInitLibrary("InvalidAppID", shutdownReg); - //printf("pclInitLibrary => ret: %d\n", ret); - x_fail_unless(ret == EPERS_NOPRCTABLE, "pclInitLibrary => invalid application ID not detected"); + + ret = pclKeyGetSize(0xFF, "JustTesting", 1, 1); + x_fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyGetSize => invalid application ID not detected"); pclDeinitLibrary(); } @@ -1538,6 +1539,89 @@ int main(int argc, char *argv[]) /// debug log and trace (DLT) setup DLT_REGISTER_APP("PCLt","tests the persistence client library"); +#if 0 + //Manual test of concurrent access + // start 2 instances of persistence-client_library_test + // persistence-client_library_test -w 5 + // persistence-client_library_test -r 5 + // press any key to proceed in the test + + int opt = 0; + int write = 0; + int read = 0; + int numloops = 0; + + while ((opt = getopt(argc, argv, "w:r:")) != -1) + { + switch (opt) + { + case 'w': + write = 1; + numloops = atoi(optarg); + break; + case 'r': + read = 1; + numloops = atoi(optarg); + break; + } + } + + + const char* appId_one = "lt-persistence_client_library_test"; + const char* appId_two = "concurrency_test"; + if (write) + { + int ret = 0, i = 0; + unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL; + unsigned char buffer[READ_SIZE] = { 0 }; + + (void) pclInitLibrary(appId_one, shutdownReg); + + for (i = 0; i < numloops; i++) + { + getchar(); + printf("write: [%d] \n", i); + + ret = pclKeyWriteData(0x20, "links/last_link2", 2, 1, (unsigned char*) "Test notify shared data", + strlen("Test notify shared data")); + if (ret < 0) + printf("Failed to write data: %d\n", ret); + } + + pclDeinitLibrary(); + sleep(1); + _exit(EXIT_SUCCESS); + + } + + + + if(read) + { + int ret = 0, i = 0; + unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL; + unsigned char buffer[READ_SIZE] = { 0 }; + + (void) pclInitLibrary(appId_two, shutdownReg); + + for (i = 0; i < numloops; i++) + { + getchar(); + printf("read: [%d] \n", i); + + + memset(buffer, 0, READ_SIZE); + ret = pclKeyReadData(0x20, "links/last_link2", 2, 1, buffer, READ_SIZE); + if (ret < 0) + printf("Failed to read data: %d\n", ret); + } + + pclDeinitLibrary(); + sleep(1); + _exit(EXIT_SUCCESS); + } + #endif + if(argc >= 2) { @@ -1588,9 +1672,9 @@ void do_pcl_concurrency_access(const char* applicationID, const char* resourceID (void)pclInitLibrary(applicationID, shutdownReg); - for(i=0; i< 200; i++) + for(i=0; i< 10; i++) { - printf("[%d] - i: %d", operation, i); + printf("[%d] - i: %d \n", operation, i); if(operation == 0 ) { ret = pclKeyWriteData(0x20, resourceID, 2, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data")); @@ -1600,13 +1684,13 @@ void do_pcl_concurrency_access(const char* applicationID, const char* resourceID else if(operation == 1) { memset(buffer, 0, READ_SIZE); - ret = pclKeyReadData(0x20, resourceID, 3, 2, buffer, READ_SIZE); + ret = pclKeyReadData(0x20, resourceID, 2, 1, buffer, READ_SIZE); if(ret < 0) printf("Failed to read data: %d\n", ret); } else { - printf("invalid operation - end!!"); + printf("invalid operation - end!! \n"); break; } } @@ -1618,7 +1702,7 @@ void do_pcl_concurrency_access(const char* applicationID, const char* resourceID void run_concurrency_test() { const char* appId_one = "lt-persistence_client_library_test"; - const char* appId_two = "pfs_test"; + const char* appId_two = "concurrency_test"; int pid = fork(); @@ -1626,7 +1710,9 @@ void run_concurrency_test() { /*child*/ printf("Started child process with PID: [%d] \n", pid); - do_pcl_concurrency_access(appId_one, "links/last_link2", 0); + do_pcl_concurrency_access(appId_one, "links/last_link2", 0); //write + + printf("CHILD exits! \n"); _exit(EXIT_SUCCESS); } @@ -1634,7 +1720,9 @@ void run_concurrency_test() { /*parent*/ printf("Started father process with PID: [%d] \n", pid); - do_pcl_concurrency_access(appId_one, "links/last_link3", 1); + do_pcl_concurrency_access(appId_two, "links/last_link2", 1); //read + + printf("PARENT exits! \n"); _exit(EXIT_SUCCESS); } |