diff options
author | Ingo Huerner <ingo_huerner@mentor.com> | 2017-05-23 13:30:57 +0200 |
---|---|---|
committer | Ingo Huerner <ingo_huerner@mentor.com> | 2017-05-23 13:30:57 +0200 |
commit | 1704723d0b5f43257dd556684229ffe4b996830c (patch) | |
tree | 50dabe32d956ce316fde141b222039aab280228e | |
parent | 6ce5be34c8bbbf2ed10081950493970ebaffa87c (diff) | |
download | persistence-client-library-1704723d0b5f43257dd556684229ffe4b996830c.tar.gz |
Check return value when registering to PAS, pclInitLibrary returns now an error code if PAS register fails
-rw-r--r-- | include/persistence_client_library_error_def.h | 2 | ||||
-rw-r--r-- | src/persistence_client_library.c | 90 | ||||
-rw-r--r-- | src/persistence_client_library_data_organization.c | 3 | ||||
-rw-r--r-- | src/persistence_client_library_data_organization.h | 3 | ||||
-rw-r--r-- | test/data/PAS_data.tar.gz | bin | 6843 -> 46914 bytes | |||
-rw-r--r-- | test/persistence_client_library_test.c | 75 | ||||
-rw-r--r-- | test/persistence_client_library_test_file.c | 83 |
7 files changed, 198 insertions, 58 deletions
diff --git a/include/persistence_client_library_error_def.h b/include/persistence_client_library_error_def.h index 32f076a..23d33f9 100644 --- a/include/persistence_client_library_error_def.h +++ b/include/persistence_client_library_error_def.h @@ -126,6 +126,8 @@ extern "C" { /// plugin variable not available #define EPERS_NO_PLUGIN_VAR (-46) /// requested handle is not valid. \since PCL v7.0.3 +#define EPERS_NO_REG_TO_PAS (-47) +/// requested handle is not valid. \since PCL v7.0.3 #define EPERS_INVALID_HANDLE (-1000) #ifdef __cplusplus diff --git a/src/persistence_client_library.c b/src/persistence_client_library.c index 3d93aa8..24dec18 100644 --- a/src/persistence_client_library.c +++ b/src/persistence_client_library.c @@ -233,22 +233,27 @@ int pclInitLibrary(const char* appName, int shutdownMode) DLT_STRING("- init counter: "), DLT_UINT(gPclInitCounter) ); // do check if there are remaining shared memory and semaphores for local app + // (only when PCO key-value-store database backend is beeing used) checkLocalArtefacts("/dev/shm/", appName); //checkGroupArtefacts("/dev/shm", "group_"); rval = private_pclInitLibrary(appName, shutdownMode); + if(rval >= 0) + { + gPclInitCounter++; // increment after private init, otherwise atomic access is too early + } } else { DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary - App:"), DLT_STRING(gAppId), DLT_STRING("- ONLY INCREMENT init counter: "), DLT_UINT(gPclInitCounter) ); - } - gPclInitCounter++; // increment after private init, otherwise atomic access is too early + gPclInitCounter++; // increment after private init, otherwise atomic access is too early + } } else { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary - appName NULL")); + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary - appName invalid")); rval = EPERS_COMMON; } @@ -266,12 +271,56 @@ static int private_pclInitLibrary(const char* appName, int shutdownMode) { // no need for NULL ptr check for appName, already done in calling function - int rval = 1; + int rval = 1, pasRegStatus = -1; char blacklistPath[PERS_ORG_MAX_LENGTH_PATH_FILENAME] = {0}; gShutdownMode = shutdownMode; +#if USE_FSYNC + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using fsync version")); +#else + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using datasync version (DEFAULT)")); +#endif + +#if USE_FILECACHE + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using the filecache")); + pfcInitCache(appName); +#endif + + if(gDbusMainloopRunning == 0) // check if dbus has been already initialized + { + if(setup_dbus_mainloop() == -1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("initLibrary - Failed to setup main loop")); + return EPERS_DBUS_MAINLOOP; + } + gDbusMainloopRunning = 1; + } + +#if USE_PASINTERFACE + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("PAS interface is enabled!!")); + + pasRegStatus = register_pers_admin_service(); + + if(pasRegStatus == -1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("initLibrary - Failed reg to PAS dbus interface")); + } + else if(pasRegStatus < -1) + { + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("initLibrary - registration to PAS currently not possible.")); + return EPERS_NO_REG_TO_PAS; + } + else + { + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("initLibrary - Successfully established IPC protocol for PCL.")); + gPasRegistered = 1; // remember registration to PAS + } +#else + DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("PAS interface not enabled, enable with \"./configure --enable-pasinterface\"")); +#endif + strncpy(gAppId, appName, PERS_RCT_MAX_LENGTH_RESPONSIBLE); // assign application name gAppId[PERS_RCT_MAX_LENGTH_RESPONSIBLE-1] = '\0'; @@ -295,17 +344,6 @@ static int private_pclInitLibrary(const char* appName, int shutdownMode) gIsNodeStateManager = 1; } -#if USE_FSYNC - DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using fsync version")); -#else - DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using datasync version (DEFAULT)")); -#endif - -#if USE_FILECACHE - DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Using the filecache")); - pfcInitCache(appName); -#endif - // Assemble backup blacklist path snprintf(blacklistPath, PERS_ORG_MAX_LENGTH_PATH_FILENAME, "%s%s/%s", CACHEPREFIX, appName, gBackupFilename); @@ -314,11 +352,6 @@ static int private_pclInitLibrary(const char* appName, int shutdownMode) DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("initLibrary - Err access blacklist:"), DLT_STRING(blacklistPath)); } - if(setup_dbus_mainloop() == -1) - { - DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("initLibrary - Failed to setup main loop")); - return EPERS_DBUS_MAINLOOP; - } if(gShutdownMode != PCL_SHUTDOWN_TYPE_NONE) { @@ -327,20 +360,6 @@ static int private_pclInitLibrary(const char* appName, int shutdownMode) DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("initLibrary => Failed reg to LC dbus interface")); } } -#if USE_PASINTERFACE - DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("PAS interface is enabled!!")); - if(register_pers_admin_service() == -1) - { - DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("initLibrary - Failed reg to PAS dbus interface")); - } - else - { - DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("initLibrary - Successfully established IPC protocol for PCL.")); - gPasRegistered = 1; // remember registration to PAS - } -#else - DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("PAS interface not enabled, enable with \"./configure --enable-pasinterface\"")); -#endif if((rval = load_custom_plugins(customAsyncInitClbk)) < 0) // load custom plugins { @@ -375,7 +394,8 @@ int pclDeinitLibrary(void) DLT_STRING("- init counter: "), DLT_UINT(gPclInitCounter)); rval = private_pclDeinitLibrary(); - gPclInitCounter--; // decrement init counter + gDbusMainloopRunning = 0; + gPclInitCounter--; // decrement init counter DLT_UNREGISTER_CONTEXT(gPclDLTContext); } else if(gPclInitCounter > 1) diff --git a/src/persistence_client_library_data_organization.c b/src/persistence_client_library_data_organization.c index 7fd9ace..50f1da8 100644 --- a/src/persistence_client_library_data_organization.c +++ b/src/persistence_client_library_data_organization.c @@ -30,6 +30,9 @@ unsigned int gPclInitCounter = 0; /// flag to indicate if PCL has registered to PAS int gPasRegistered = 0; +/// flag to indicate if dbus mainloop is already running +int gDbusMainloopRunning = 0; + int gSyncFd = -1; int gIsNodeStateManager = 0; diff --git a/src/persistence_client_library_data_organization.h b/src/persistence_client_library_data_organization.h index 54f0b37..133799d 100644 --- a/src/persistence_client_library_data_organization.h +++ b/src/persistence_client_library_data_organization.h @@ -291,6 +291,9 @@ extern int gDbusPendingRvalue __attribute__ ((visibility ("hidden"))); extern int gPasRegistered; +extern int gDbusMainloopRunning; + + /** * @brief definition of change callback function * diff --git a/test/data/PAS_data.tar.gz b/test/data/PAS_data.tar.gz Binary files differindex 16a291b..e89be47 100644 --- a/test/data/PAS_data.tar.gz +++ b/test/data/PAS_data.tar.gz diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index d9d1c03..116bc5a 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -1561,6 +1561,75 @@ START_TEST(test_NoPluginFunc) END_TEST + +void* pasInstallThread(void* userData) +{ + // install data + printf("#### Start installation of data \n"); + if(system("persadmin_tool install /usr/local/var/PAS_data.tar.gz") == -1) + { + printf("#### Failed to install data\n"); + } + printf("#### Installation of data succeeded\n"); + + pthread_exit(0); +} + + + +START_TEST(test_PclInitPasNotAllowed) +{ + pthread_t installThread; + int rval = -1; + int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL; + char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD"; + + setenv(envVariable, "/etc/pclCustomLibConfigFileTest.cfg", 1); + + if(pthread_create(&installThread, NULL, pasInstallThread, NULL) == -1) + { + printf("#### Failed to create install thread\n"); + } + else + { + rval = pclInitLibrary(gTheAppId, shutdownReg); + //printf("#### 1 pclInit: %d\n\n", rval); + fail_unless(rval == 1, "Should be allowed to register"); + pclDeinitLibrary(); + + rval = pclInitLibrary(gTheAppId, shutdownReg); + printf("#### 2 pclInit: %d\n", rval); + //fail_unless(rval == EPERS_NO_REG_TO_PAS, "Should be not allowed to register"); + pclDeinitLibrary(); + + rval = pclInitLibrary(gTheAppId, shutdownReg); + printf("#### 3 pclInit: %d\n", rval); + //fail_unless(rval == EPERS_NO_REG_TO_PAS, "Should be not allowed to register"); + pclDeinitLibrary(); + + rval = pclInitLibrary(gTheAppId, shutdownReg); + printf("#### 4 pclInit: %d\n", rval); + //fail_unless(rval == EPERS_NO_REG_TO_PAS, "Should be not allowed to register"); + pclDeinitLibrary(); + } + + //printf("#### wait for install thread to end\n"); + if(pthread_join(installThread, NULL) != 0) // wait until thread has ended + printf("#### pthread_join - FAILED\n"); + + + // printf("#### Install thread ended\n"); + rval = pclInitLibrary(gTheAppId, shutdownReg); + //printf("#### 5 pclInit: %d\n\n", rval); + fail_unless(rval == 1, "Should be allowed to register"); + pclDeinitLibrary(); + + (void)unsetenv(envVariable); +} +END_TEST + + + static Suite* persistenceClientLib_suite_multi() { const char* testSuiteName = "\n\nPersistence Client Library (Key-API) - Multi"; @@ -1695,6 +1764,9 @@ static Suite * persistenceClientLib_suite() tcase_add_test(tc_SharedData, test_SharedData); tcase_set_timeout(tc_SharedData, 10); + TCase * tc_PclInitPasNotAllowed = tcase_create("PclInitPasNotAllowed"); + tcase_add_test(tc_PclInitPasNotAllowed, test_PclInitPasNotAllowed); + tcase_set_timeout(tc_PclInitPasNotAllowed, 20); #if 1 suite_add_tcase(s, tc_NoPluginFunc); @@ -1755,6 +1827,9 @@ static Suite * persistenceClientLib_suite() suite_add_tcase(s, tc_SharedData); tcase_add_checked_fixture(tc_SharedData, data_setup, data_teardown); + + + suite_add_tcase(s, tc_PclInitPasNotAllowed); // NOTE: make sure this test is run as the last test #endif #if USE_APPCHECK diff --git a/test/persistence_client_library_test_file.c b/test/persistence_client_library_test_file.c index 1f841a8..e57be0e 100644 --- a/test/persistence_client_library_test_file.c +++ b/test/persistence_client_library_test_file.c @@ -109,7 +109,6 @@ const char* gFileCsumNOKCsum = "/Data/mnt-backup/lt-persistence_client_library_ /// debug log and trace (DLT) setup DLT_DECLARE_CONTEXT(gPcltDLTContext); - // function prototype void run_concurrency_test(); int check_environment(); @@ -144,37 +143,60 @@ void setupRecoveryData(const char* originalFileName, const char* origData, ssize_t written = 0; // write data file - fd = open(originalFileName, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); - written = write(fd, origData, strlen(origData)); - if(written != strlen(origData)) - printf("Failed to write file - %s\n", originalFileName); - close(fd); - fd = -1; + fd = open(originalFileName, O_CREAT|O_RDWR|O_TRUNC, 0744); + if(fd != -1) + { + written = write(fd, origData, strlen(origData)); + if(written != strlen(origData)) + printf("Failed to write file 1 - %s - %s\n", originalFileName, strerror(errno)); + close(fd); + fd = -1; + } + else + { + printf("Failed to open file 1 - %s - %s\n", originalFileName, strerror(errno)); + } if(backupFileName != NULL) { // write backup file - fd = open(backupFileName, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); - written = write(fd, backupData, strlen(backupData)); - if(written != strlen(backupData)) - printf("Failed to write file - %s\n", backupFileName); - close(fd); - fd = -1; + fd = open(backupFileName, O_CREAT|O_RDWR|O_TRUNC, 0744); + if(fd != -1) + { + written = write(fd, backupData, strlen(backupData)); + if(written != strlen(backupData)) + printf("Failed to write file 2 - %s - %s\n", backupFileName, strerror(errno)); + close(fd); + fd = -1; + } + else + { + printf("Failed to open file 2 - %s - %s\n", originalFileName, strerror(errno)); + } } + if(csumFileName != NULL) { // write checksum file - fd = open(csumFileName, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); - written = write(fd, csumBuffer, strlen(csumBuffer)); - if(written != strlen(csumBuffer)) - printf("Failed to write file - %s\n", csumFileName); - close(fd); - fd = -1; + fd = open(csumFileName, O_CREAT|O_RDWR|O_TRUNC, 0744); + if(fd != -1) + { + written = write(fd, csumBuffer, strlen(csumBuffer)); + if(written != strlen(csumBuffer)) + printf("Failed to write file 3 - %s - %s\n", csumFileName, strerror(errno)); + close(fd); + fd = -1; + } + else + { + printf("Failed to open file 3 - %s - %s\n", originalFileName, strerror(errno)); + } } } +extern int pclCreateFile(const char* path, int chached); void data_setupBandR(void) { @@ -184,6 +206,8 @@ void data_setupBandR(void) const char* validCs2 = "2f7fb691"; // checksum for gWriteBuffer2 const char* validCs3 = "e6f52bda"; // checksum for gWriteBuffer3 + (void)pclCreateFile("/Data/mnt-backup/lt-persistence_client_library_test/user/200/seat/100/media/dummy.dum", 0); + // create test data (original files only) // @@ -1241,7 +1265,7 @@ START_TEST(test_FileBackupAndRecovery) (void)pclInitLibrary(gTheAppId, shutdownReg); -#if 1 + // // test backup and checksum creation // @@ -1355,9 +1379,7 @@ START_TEST(test_FileBackupAndRecovery) fail_unless(access(gFile2Backup, F_OK) != 0, "Backup 3 does exist, but should not\n"); fail_unless(access(gFile2Csum, F_OK) != 0, "Csum 3 does exist, but should not\n"); -#endif -#if 1 // // now the error cases // @@ -1415,7 +1437,7 @@ START_TEST(test_FileBackupAndRecovery) sizeRead = pclFileReadData(fd, readBuffer, 8192); fail_unless(sizeRead <= EPERS_COMMON, "Read succeeded, but should not => return: %dn", sizeRead); pclFileClose(fd); -#endif + // only backup file available, matches original data // expected: keep original @@ -1500,6 +1522,8 @@ static Suite * persistencyClientLib_suite() tcase_add_test(tc_FileBackupAndRecovery, test_FileBackupAndRecovery); tcase_set_timeout(tc_FileBackupAndRecovery, 30); +#if 1 + suite_add_tcase(s, tc_persDataFile); tcase_add_checked_fixture(tc_persDataFile, data_setup, data_teardown); @@ -1533,6 +1557,16 @@ static Suite * persistencyClientLib_suite() suite_add_tcase(s, tc_InitDeinit); // I M P O R T A N T: this needs to be the last test, as this tests ends NSM +#else + + printf("Do backup recovery test case now\n"); + suite_add_tcase(s, tc_FileBackupAndRecovery); + tcase_add_checked_fixture(tc_FileBackupAndRecovery, data_setupBandR, data_teardownBandR); + +#endif + + + //suite_add_tcase(s, tc_MultiFileReadWrite); //tcase_add_checked_fixture(tc_MultiFileReadWrite, data_setup, data_teardown); @@ -2279,6 +2313,9 @@ int main(int argc, char *argv[]) } + + + const char* gWriteBuffer = "Pack my box with five dozen liquor jugs. - " "Jackdaws love my big sphinx of quartz. - " "The five boxing wizards jump quickly. - " |