Adjusted library version information; added define for non shutdown notifications...
[profile/ivi/persistence-client-library.git] / src / persistence_client_library.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
4  * Company         XS Embedded GmbH
5  *****************************************************************************/
6 /******************************************************************************
7  * This Source Code Form is subject to the terms of the
8  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
9  * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
11  /**
12  * @file           persistence_client_library.c
13  * @ingroup        Persistence client library
14  * @author         Ingo Huerner
15  * @brief          Implementation of the persistence client library.
16  *                 Library provides an API to access persistent data
17  * @see            
18  */
19
20
21 #include "persistence_client_library_lc_interface.h"
22 #include "persistence_client_library_pas_interface.h"
23 #include "persistence_client_library_dbus_service.h"
24 #include "persistence_client_library_handle.h"
25 #include "persistence_client_library_custom_loader.h"
26 #include "persistence_client_library.h"
27 #include "persistence_client_library_backup_filelist.h"
28
29 #include <string.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <dlfcn.h>
33
34 #include <dlt/dlt.h>
35 #include <dlt/dlt_common.h>
36
37 #include <dbus/dbus.h>
38
39 /// debug log and trace (DLT) setup
40 DLT_DECLARE_CONTEXT(gDLTContext);
41
42 // declared in persistence_client_library_dbus_service.c
43 // used to end dbus library
44 extern int bContinue;
45
46 static int gShutdownMode = 0;
47
48 /// loical function declaration
49 void invalidateCustomPlugin(int idx);
50
51 int pclInitLibrary(const char* appName, int shutdownMode)
52 {
53    int status = 0;
54    int i = 0, rval = 1;
55
56    if(gPclInitialized == PCLnotInitialized)
57    {
58       gShutdownMode = shutdownMode;
59
60       DLT_REGISTER_CONTEXT(gDLTContext,"pers","Context for persistence client library logging");
61       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T  Persistence Client Library - "), DLT_STRING(gAppId),
62                            DLT_STRING("- init counter: "), DLT_INT(gPclInitialized) );
63
64       /// environment variable for on demand loading of custom libraries
65       const char *pOnDemandLoad = getenv("PERS_CUSTOM_LIB_LOAD_ON_DEMAND");
66
67       /// environment variable for max key value data
68       const char *pDataSize = getenv("PERS_MAX_KEY_VAL_DATA_SIZE");
69
70       /// blacklist path environment variable
71       const char *pBlacklistPath = getenv("PERS_BLACKLIST_PATH");
72
73       if(pDataSize != NULL)
74       {
75          gMaxKeyValDataSize = atoi(pDataSize);
76       }
77
78       if(pBlacklistPath == NULL)
79       {
80          pBlacklistPath = "/etc/pclBackupBlacklist.txt";   // default path
81       }
82
83       if(readBlacklistConfigFile(pBlacklistPath) == -1)
84       {
85          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInitLibrary -> failed to access blacklist:"), DLT_STRING(pBlacklistPath));
86       }
87
88
89       if(setup_dbus_mainloop() == -1)
90       {
91          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to setup main loop"));
92          return EPERS_DBUS_MAINLOOP;
93       }
94
95       if(gShutdownMode != PCL_SHUTDOWN_TYPE_NONE)
96       {
97          // register for lifecycle and persistence admin service dbus messages
98          if(register_lifecycle(shutdownMode) == -1)
99          {
100             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to register to lifecycle dbus interface"));
101             return EPERS_REGISTER_LIFECYCLE;
102          }
103       }
104       if(register_pers_admin_service() == -1)
105       {
106          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to register to pers admin dbus interface"));
107          return EPERS_REGISTER_ADMIN;
108       }
109
110       /// get custom library names to load
111       status = get_custom_libraries();
112       if(status >= 0)
113       {
114          // initialize custom library structure
115          for(i = 0; i < PersCustomLib_LastEntry; i++)
116          {
117             invalidateCustomPlugin(i);
118          }
119
120          if(pOnDemandLoad == NULL)  // load all available libraries now
121          {
122             for(i=0; i < PersCustomLib_LastEntry; i++ )
123             {
124                if(check_valid_idx(i) != -1)
125                {
126                   if(load_custom_library(i, &gPersCustomFuncs[i] ) == 1)
127                   {
128                      if( (gPersCustomFuncs[i].custom_plugin_init) != NULL)
129                      {
130                         DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => Loaded plugin: "),
131                                                            DLT_STRING(get_custom_client_lib_name(i)));
132                         gPersCustomFuncs[i].custom_plugin_init();
133                      }
134                      else
135                      {
136                         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin functions: "),
137                                                             DLT_STRING(get_custom_client_lib_name(i)));
138                      }
139                   }
140                   else
141                   {
142                      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin: "),
143                                           DLT_STRING(get_custom_client_lib_name(i)));
144                   }
145                }
146                else
147                {
148                   continue;
149                }
150             }
151          }
152       }
153       else
154       {
155          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInit => Failed to load custom library config table => error number:"), DLT_INT(status));
156       }
157
158       // assign application name
159       strncpy(gAppId, appName, MaxAppNameLen);
160       gAppId[MaxAppNameLen-1] = '\0';
161
162       // destory mutex
163       pthread_mutex_destroy(&gDbusInitializedMtx);
164       pthread_cond_destroy(&gDbusInitializedCond);
165
166       gPclInitialized++;
167    }
168    else if(gPclInitialized >= PCLinitialized)
169    {
170       gPclInitialized++; // increment init counter
171       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T  Persistence Client Library - "), DLT_STRING(gAppId),
172                            DLT_STRING("- ONLY INCREMENT init counter: "), DLT_INT(gPclInitialized) );
173    }
174
175    return rval;
176 }
177
178
179
180 int pclDeinitLibrary(void)
181 {
182    int i = 0, rval = 1;
183
184    if(gPclInitialized == PCLinitialized)
185    {
186       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
187                                            DLT_STRING("- init counter: "), DLT_INT(gPclInitialized));
188
189       // unregister for lifecycle and persistence admin service dbus messages
190       rval = unregister_lifecycle(gShutdownMode);
191       rval = unregister_pers_admin_service();
192
193       // unload custom client libraries
194       for(i=0; i<PersCustomLib_LastEntry; i++)
195       {
196          if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
197          {
198             // deinitialize plugin
199             gPersCustomFuncs[i].custom_plugin_deinit();
200             // close library handle
201             dlclose(gPersCustomFuncs[i].handle);
202
203             invalidateCustomPlugin(i);
204          }
205       }
206
207       gPclInitialized = PCLnotInitialized;
208
209       DLT_UNREGISTER_CONTEXT(gDLTContext);
210    }
211    else if(gPclInitialized > PCLinitialized)
212    {
213       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
214                                            DLT_STRING("- ONLY DECREMENT init counter: "), DLT_INT(gPclInitialized));
215       gPclInitialized--;   // decrement init counter
216    }
217
218    // end dbus library
219    bContinue = FALSE;
220
221    return rval;
222 }
223
224
225
226 void invalidateCustomPlugin(int idx)
227 {
228    gPersCustomFuncs[idx].handle  = NULL;
229    gPersCustomFuncs[idx].custom_plugin_init = NULL;
230    gPersCustomFuncs[idx].custom_plugin_deinit = NULL;
231    gPersCustomFuncs[idx].custom_plugin_handle_open = NULL;
232    gPersCustomFuncs[idx].custom_plugin_handle_close = NULL;
233    gPersCustomFuncs[idx].custom_plugin_handle_get_data = NULL;
234    gPersCustomFuncs[idx].custom_plugin_handle_set_data  = NULL;
235    gPersCustomFuncs[idx].custom_plugin_get_data = NULL;
236    gPersCustomFuncs[idx].custom_plugin_set_data = NULL;
237    gPersCustomFuncs[idx].custom_plugin_delete_data = NULL;
238    gPersCustomFuncs[idx].custom_plugin_get_status_notification_clbk = NULL;
239    gPersCustomFuncs[idx].custom_plugin_handle_get_size = NULL;
240    gPersCustomFuncs[idx].custom_plugin_get_size = NULL;
241    gPersCustomFuncs[idx].custom_plugin_create_backup = NULL;
242    gPersCustomFuncs[idx].custom_plugin_get_backup = NULL;
243    gPersCustomFuncs[idx].custom_plugin_restore_backup = NULL;
244 }
245
246
247
248
249