From 6b3d01948443f143af5497d4a8addf870d95b1f3 Mon Sep 17 00:00:00 2001 From: Ingo Huerner Date: Fri, 3 May 2013 11:57:58 +0200 Subject: Added init/deinit library functions; removed library constructor/destructor --- configure.ac | 2 +- include/persistence_client_library_key.h | 26 ++++++-- .../persistence_client_library_data_organization.h | 2 +- src/persistence_client_library.c | 64 +++--------------- src/persistence_client_library_file.c | 21 ++---- src/persistence_client_library_prct_access.c | 5 +- test/persistence_client_library_dbus_test.c | 15 ++++- test/persistence_client_library_test.c | 78 +++++++++++++++++++++- 8 files changed, 132 insertions(+), 81 deletions(-) diff --git a/configure.ac b/configure.ac index a93eeb3..f50ced4 100644 --- a/configure.ac +++ b/configure.ac @@ -16,7 +16,7 @@ AC_GNU_SOURCE() # create library version information -m4_define([pers_client_library_version_current], [4]) +m4_define([pers_client_library_version_current], [5]) m4_define([pers_client_library_version_revision], [0]) m4_define([pers_client_library_version_age], [0]) m4_define([pers_client_library_version], [pers_client_library_version_current():pers_client_library_version_revision():pers_client_library_version_age()]) diff --git a/include/persistence_client_library_key.h b/include/persistence_client_library_key.h index 0afd071..ea0b7af 100644 --- a/include/persistence_client_library_key.h +++ b/include/persistence_client_library_key.h @@ -32,7 +32,7 @@ extern "C" { #endif -#define PERSIST_KEYVALUEAPI_INTERFACE_VERSION (0x04100000U) +#define PERSIST_KEYVALUEAPI_INTERFACE_VERSION (0x05000000U) /** * status returned in notification structure @@ -61,6 +61,10 @@ typedef struct _pclNotification_s } pclNotification_s; +/** + * shutdown notification type definitions + * according to Node State Manager component + */ enum pclShutdownTypeNotification { NSM_SHUTDOWN_TYPE_FAST = 2, /// Client registered for fast lifecycle shutdown @@ -71,11 +75,23 @@ enum pclShutdownTypeNotification /// defiinition of the change callback typedef int(* pclChangeNotifyCallback_t)(pclNotification_s * notifyStruct); -/// library constructor -void pclLibraryConstructor(void) __attribute__((constructor)); +/** + * @brief itialize client library + * + * @param application name + * @param shutdown mode NSM_SHUTDOWN_TYPE_FAST or NSM_SHUTDOWN_TYPE_NORMAL + * + */ +void pclInitLibrary(const char* appname, int shutdownMode); -/// library deconstructor -void pclLibraryDestructor(void) __attribute__((destructor)); + + +/** + * @brief deinitialize client library + * + * @param shutdown mode NSM_SHUTDOWN_TYPE_FAST or NSM_SHUTDOWN_TYPE_NORMAL + */ +void pclDeinitLibrary(int shutdownMode); /** diff --git a/include_protected/persistence_client_library_data_organization.h b/include_protected/persistence_client_library_data_organization.h index 4bec5bf..56c86d7 100644 --- a/include_protected/persistence_client_library_data_organization.h +++ b/include_protected/persistence_client_library_data_organization.h @@ -24,7 +24,7 @@ extern "C" { #endif -#define PERSIST_CLIENT_LIBRARY_DATA_ORGANIZATION_INTERFACE_VERSION (0x01020000U) +#define PERSIST_CLIENT_LIBRARY_DATA_ORGANIZATION_INTERFACE_VERSION (0x01030000U) #include "../include/persistence_client_library_error_def.h" #include "../include/persistence_client_library_key.h" diff --git a/src/persistence_client_library.c b/src/persistence_client_library.c index eb4a9c2..d11fba4 100644 --- a/src/persistence_client_library.c +++ b/src/persistence_client_library.c @@ -28,65 +28,25 @@ #include #include #include + #include #include #include -#define ENABLE_DBUS_INTERFACE 1 - -extern char* __progname; - /// debug log and trace (DLT) setup DLT_DECLARE_CONTEXT(gDLTContext); -/** - * @brief itialize client library - * - * @param shutdown mode NSM_SHUTDOWN_TYPE_FAST or NSM_SHUTDOWN_TYPE_NORMAL - * - */ -void pclInit(const char* appname, int shutdownMode); - - - -/** - * @brief deinitialize client library - * - * @param shutdown mode NSM_SHUTDOWN_TYPE_FAST or NSM_SHUTDOWN_TYPE_NORMAL - */ -void pclDeinit(int shutdownMode); - -void pclLibraryConstructor(void) -{ - int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; - - DLT_REGISTER_APP("test","tests the persistence client library"); - /// debug log and trace (DLT) setup - - printf("A p p l i c a t i o n n a m e => %s \n", __progname /*program_invocation_short_name*/); - pclInit(__progname, shutdownReg); -} - - -void pclLibraryDestructor(void) -{ - int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; - pclDeinit(shutdownReg); -} - - - -void pclInit(const char* appName, int shutdownMode) +void pclInitLibrary(const char* appName, int shutdownMode) { int status = 0; int i = 0; DLT_REGISTER_CONTEXT(gDLTContext,"pers","Context for persistence client library logging"); - DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("Initialize Persistence Client Library!!!!")); + DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInit => Initialize Persistence Client Library!!!!")); /// environment variable for on demand loading of custom libraries const char *pOnDemandLoad = getenv("PERS_CUSTOM_LIB_LOAD_ON_DEMAND"); @@ -99,13 +59,11 @@ void pclInit(const char* appName, int shutdownMode) gMaxKeyValDataSize = atoi(pDataSize); } -#if ENABLE_DBUS_INTERFACE == 1 setup_dbus_mainloop(); // register for lifecycle and persistence admin service dbus messages register_lifecycle(shutdownMode); register_pers_admin_service(); -#endif // clear the open file descriptor array memset(gOpenFdArray, 0, MaxPersHandle * sizeof(int)); @@ -113,8 +71,8 @@ void pclInit(const char* appName, int shutdownMode) /// get custom library names to load status = get_custom_libraries(); if(status < 0) - { - printf("Failed to load custom library config table => error number %d\n", status ); + { + DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInit => Failed to load custom library config table => error number:"), DLT_INT(status)); } // initialize custom library structure @@ -144,7 +102,7 @@ void pclInit(const char* appName, int shutdownMode) { if(load_custom_library(get_custom_client_position_in_array(i), &gPersCustomFuncs[i] ) == -1) { - printf("E r r o r could not load plugin: %s \n", get_custom_client_lib_name(get_custom_client_position_in_array(i))); + DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInit => E r r o r could not load plugin: "), DLT_STRING(get_custom_client_lib_name(get_custom_client_position_in_array(i)))); break; } gPersCustomFuncs[i].custom_plugin_init(); @@ -155,8 +113,6 @@ void pclInit(const char* appName, int shutdownMode) strncpy(gAppId, appName, MaxAppNameLen); gAppId[MaxAppNameLen-1] = '\0'; - DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInit -> initialized client library:"), DLT_STRING(gAppId) ); - // destory mutex pthread_mutex_destroy(&gDbusInitializedMtx); pthread_cond_destroy(&gDbusInitializedCond); @@ -164,19 +120,15 @@ void pclInit(const char* appName, int shutdownMode) -void pclDeinit(int shutdownMode) +void pclDeinitLibrary(int shutdownMode) { + DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinit -> Deinit client library:"), DLT_STRING(gAppId)); -#if ENABLE_DBUS_INTERFACE == 1 // unregister for lifecycle and persistence admin service dbus messages unregister_lifecycle(shutdownMode); unregister_pers_admin_service(); -#endif - - DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinit -> deinit client library:"), DLT_STRING(gAppId)); DLT_UNREGISTER_CONTEXT(gDLTContext); - DLT_UNREGISTER_APP(); dlt_free(); } diff --git a/src/persistence_client_library_file.c b/src/persistence_client_library_file.c index 617e817..4469e56 100644 --- a/src/persistence_client_library_file.c +++ b/src/persistence_client_library_file.c @@ -64,19 +64,12 @@ int pclFileClose(int fd) // check if a backup and checksum file needs to bel deleted if( gFileHandleArray[fd].permission != PersistencePermission_ReadOnly) { - // remove bakup file - if(remove(gFileHandleArray[fd].backupPath ) == -1) - { - printf("pclFileClose ==> failed to remove backup file\n"); - DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileClose ==> failed to remove backup file"), DLT_STRING(gFileHandleArray[fd].backupPath)); - } + // remove backup file + remove(gFileHandleArray[fd].backupPath); // we don't care about return value // remove checksum file - if(remove(gFileHandleArray[fd].csumPath) == -1) - { - printf("pclFileClose ==> failed to remove checksum file\n"); - DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclFileClose ==> failed to remove checksum file"), DLT_STRING(gFileHandleArray[fd].csumPath)); - } + remove(gFileHandleArray[fd].csumPath); // we don't care about return value + } __sync_fetch_and_sub(&gOpenFdArray[fd], FileClosed); // set closed flag rval = close(fd); @@ -94,7 +87,7 @@ int pclFileGetSize(int fd) int ret = 0; ret = fstat(fd, &buf); - DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileGetSize fd: "), DLT_INT(fd)); + //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileGetSize fd: "), DLT_INT(fd)); if(ret != -1) { @@ -246,7 +239,7 @@ int pclFileRemove(unsigned int ldbid, const char* resource_id, unsigned int user { int rval = 0; - DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileReadData "), DLT_INT(ldbid), DLT_STRING(resource_id)); + //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileReadData "), DLT_INT(ldbid), DLT_STRING(resource_id)); if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked { @@ -317,7 +310,7 @@ int pclFileUnmapData(void* address, long size) { int rval = 0; - DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileUnmapData")); + //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclFileUnmapData")); if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked { diff --git a/src/persistence_client_library_prct_access.c b/src/persistence_client_library_prct_access.c index 1624c29..0aae822 100644 --- a/src/persistence_client_library_prct_access.c +++ b/src/persistence_client_library_prct_access.c @@ -169,7 +169,7 @@ int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsign else { printf("get_db_context - resource_table: no value for key: %s \n", resource_id); - DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_db_context => itzam_btree_open => resource_table: no value for key"), DLT_STRING(resource_id) ); + DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("get_db_context => itzam_btree_open => resource_table: no value for key:"), DLT_STRING(resource_id) ); rval = EPERS_NOKEYDATA; } } // resource table @@ -201,7 +201,8 @@ int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsign memcpy(dbContext->configKey.customID, "A_CUSTOM_ID", strlen("A_CUSTOM_ID")); memcpy(dbContext->configKey.reponsible, "default", strlen("default")); memcpy(dbContext->configKey.custom_name, "default", strlen("default")); - //printf("get_db_context ==> R E S O U R C E N O T found: %s \n", resource_id); + + DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context => create resource not in PRCT => key:"), DLT_STRING(resource_id) ); // send create notification rval = pers_send_Notification_Signal(dbKey, &dbContext->context, pclNotifyStatus_created); diff --git a/test/persistence_client_library_dbus_test.c b/test/persistence_client_library_dbus_test.c index a726b51..b619cac 100644 --- a/test/persistence_client_library_dbus_test.c +++ b/test/persistence_client_library_dbus_test.c @@ -22,7 +22,8 @@ #include - +#include +#include int myChangeCallback(pclNotification_s * notifyStruct) @@ -42,9 +43,15 @@ int myChangeCallback(pclNotification_s * notifyStruct) int main(int argc, char *argv[]) { int ret = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; printf("Dbus interface test application\n"); + /// debug log and trace (DLT) setup + DLT_REGISTER_APP("noty","tests the persistence client library"); + pclInitLibrary("lt-persistence_client_library_dbus_test", shutdownReg); + + printf("Press a key to end application\n"); ret = pclKeyHandleOpen(0xFF, "posHandle/last_position", 0, 0); @@ -55,6 +62,12 @@ int main(int argc, char *argv[]) getchar(); + pclDeinitLibrary(shutdownReg); + + + // unregister debug log and trace + DLT_UNREGISTER_APP(); + printf("By\n"); return ret; } diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index 863be2d..75b7a43 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -43,6 +43,9 @@ #define NUM_OF_FILES 3 #define READ_SIZE 1024 +/// application id +char gTheAppId[MaxAppNameLen]; + // definition of weekday char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; @@ -56,7 +59,12 @@ char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "F START_TEST (test_GetData) { int ret = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + unsigned char buffer[READ_SIZE]; + + pclInitLibrary(gTheAppId, shutdownReg); + memset(buffer, 0, READ_SIZE); /** @@ -132,6 +140,8 @@ START_TEST (test_GetData) */ ret = pclKeyReadData(0x84, "links/last_link", 2, 1, buffer, READ_SIZE); fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/queens", strlen((char*)buffer)) == 0, "Buffer not correctly read"); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -145,11 +155,16 @@ END_TEST START_TEST (test_GetDataHandle) { int ret = 0, handle = 0, handle2 = 0, handle3 = 0, handle4 = 0, size = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + unsigned char buffer[READ_SIZE]; struct tm *locTime; - time_t t = time(0); char sysTimeBuffer[128]; + + pclInitLibrary(gTheAppId, shutdownReg); + + time_t t = time(0); memset(buffer, 0, READ_SIZE); locTime = localtime(&t); @@ -228,6 +243,8 @@ START_TEST (test_GetDataHandle) ret = pclKeyHandleClose(handle); ret = pclKeyHandleClose(handle3); ret = pclKeyHandleClose(handle4); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -241,12 +258,16 @@ END_TEST START_TEST(test_SetData) { int ret = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; unsigned char buffer[READ_SIZE]; char write1[READ_SIZE]; char write2[READ_SIZE]; char sysTimeBuffer[256]; struct tm *locTime; + + pclInitLibrary(gTheAppId, shutdownReg); + time_t t = time(0); locTime = localtime(&t); @@ -340,6 +361,7 @@ START_TEST(test_SetData) fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read"); fail_unless(ret == strlen(write2), "Wrong read size"); + pclDeinitLibrary(shutdownReg); } END_TEST @@ -353,8 +375,11 @@ END_TEST START_TEST(test_SetDataNoPRCT) { int ret = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; unsigned char buffer[READ_SIZE]; struct tm *locTime; + + pclInitLibrary(gTheAppId, shutdownReg); time_t t = time(0); char sysTimeBuffer[128]; @@ -381,6 +406,7 @@ START_TEST(test_SetDataNoPRCT) fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size"); printf("read buffer : %s\n", buffer); + pclDeinitLibrary(shutdownReg); } END_TEST @@ -394,6 +420,10 @@ START_TEST(test_GetDataSize) { int size = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + + pclInitLibrary(gTheAppId, shutdownReg); + /** * Logical DB ID: 0xFF with user 3 and seat 2 * ==> local USER value (user 3, seat 2) @@ -408,6 +438,8 @@ START_TEST(test_GetDataSize) */ size = pclKeyGetSize(0x84, "links/last_link", 2, 1); fail_unless(size == strlen("CACHE_ /last_exit/queens"), "Invalid size"); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -421,6 +453,9 @@ START_TEST(test_DeleteData) { int rval = 0; unsigned char buffer[READ_SIZE]; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + + pclInitLibrary(gTheAppId, shutdownReg); // read data from key rval = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE); @@ -447,6 +482,8 @@ START_TEST(test_DeleteData) // after deleting the key, reading from key must fail now! rval = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE); fail_unless(rval == EPERS_NOKEY, "Read form key 70 works, but should fail"); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -465,10 +502,15 @@ START_TEST(test_DataFile) int fd = 0, i = 0, idx = 0; int size = 0, ret = 0; int writeSize = 16*1024; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + unsigned char buffer[READ_SIZE]; const char* refBuffer = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media"; char* writeBuffer; char* fileMap = NULL; + + pclInitLibrary(gTheAppId, shutdownReg); + writeBuffer = malloc(writeSize); @@ -544,6 +586,8 @@ START_TEST(test_DataFile) fail_unless(ret == 0, "Failed to close file"); free(writeBuffer); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -555,6 +599,9 @@ START_TEST(test_DataFileRecovery) int fd_RW = 0, fd_RO = 0; int ret = 0; char* wBuffer = "This is a buffer to write"; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + + pclInitLibrary(gTheAppId, shutdownReg); // test backup creation -------------------------------------------- fd_RO = pclFileOpen(0xFF, "media/mediaDB_ReadOnly.db", 1, 1); @@ -567,6 +614,7 @@ START_TEST(test_DataFileRecovery) ret = pclFileClose(fd_RW); ret = pclFileClose(fd_RO); + pclDeinitLibrary(shutdownReg); } END_TEST @@ -577,6 +625,9 @@ START_TEST(test_DataHandle) { int handle1 = 0, handle2 = 0; int ret = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + + pclInitLibrary(gTheAppId, shutdownReg); // test file handles handle1 = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1); @@ -602,6 +653,8 @@ START_TEST(test_DataHandle) ret = pclKeyHandleClose(1024); fail_unless(ret == -1, "Could close, but should not!!"); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -614,6 +667,9 @@ END_TEST START_TEST(test_DataHandleOpen) { int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + + pclInitLibrary(gTheAppId, shutdownReg); // open handles ---------------------------------------------------- hd1 = pclKeyHandleOpen(0xFF, "posHandle/last_position1", 0, 0); @@ -672,6 +728,8 @@ START_TEST(test_DataHandleOpen) ret = pclKeyHandleClose(hd9); fail_unless(ret != -1, "Failed to close handle!!"); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -688,6 +746,9 @@ START_TEST(test_Cursor) char bufferDataSrc[READ_SIZE]; char bufferKeyDst[READ_SIZE]; char bufferDataDst[READ_SIZE]; + int shutdownReg = NSM_SHUTDOWN_TYPE_FAST | NSM_SHUTDOWN_TYPE_NORMAL; + + pclInitLibrary(gTheAppId, shutdownReg); memset(bufferKeySrc, 0, READ_SIZE); memset(bufferDataSrc, 0, READ_SIZE); @@ -742,6 +803,8 @@ START_TEST(test_Cursor) rval = pers_db_cursor_destroy(handle1); fail_unless(rval != -1, "Failed to destroy cursor!!"); + + pclDeinitLibrary(shutdownReg); } END_TEST @@ -806,12 +869,25 @@ int main(int argc, char *argv[]) { int nr_failed = 0; + // assign application name + strncpy(gTheAppId, "lt-persistence_client_library_test", MaxAppNameLen); + gTheAppId[MaxAppNameLen-1] = '\0'; + + printf("A p p l i c a t i o n n a m e => %s \n", gTheAppId /*program_invocation_short_name*/); + + /// debug log and trace (DLT) setup + DLT_REGISTER_APP("test","tests the persistence client library"); + Suite * s = persistencyClientLib_suite(); SRunner * sr = srunner_create(s); srunner_run_all(sr, CK_VERBOSE); nr_failed = srunner_ntests_failed(sr); srunner_free(sr); + + // unregister debug log and trace + DLT_UNREGISTER_APP(); + return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE; } -- cgit v1.2.1