f267b26d2d4d9755da70bc807d799295ee764626
[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
49 int pclInitLibrary(const char* appName, int shutdownMode)
50 {
51    int status = 0;
52    int i = 0, rval = 1;
53
54    if(gPclInitialized == PCLnotInitialized)
55    {
56       gShutdownMode = shutdownMode;
57
58       DLT_REGISTER_CONTEXT(gDLTContext,"pers","Context for persistence client library logging");
59       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T  Persistence Client Library - "), DLT_STRING(gAppId),
60                            DLT_STRING("- init counter: "), DLT_INT(gPclInitialized) );
61
62       /// environment variable for on demand loading of custom libraries
63       const char *pOnDemandLoad = getenv("PERS_CUSTOM_LIB_LOAD_ON_DEMAND");
64       /// environment variable for max key value data
65       const char *pDataSize = getenv("PERS_MAX_KEY_VAL_DATA_SIZE");
66       /// blacklist path environment variable
67       const char *pBlacklistPath = getenv("PERS_BLACKLIST_PATH");
68
69 #if USE_PASINTERFACE == 1
70       //printf("* ADMIN INTERFACE is  - e n a b l e d - \n");
71       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("PAS interface is enabled!!"));
72 #else
73       //printf("* ADMIN INTERFACE is  - d i s a b l e d - enable with \"./configure --enable-pasinterface\"\n");
74       DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("PAS interface is not enabled, enable with \"./configure --enable-pasinterface\""));
75 #endif
76
77
78       pthread_mutex_lock(&gDbusPendingRegMtx);   // block until pending received
79
80       if(pDataSize != NULL)
81       {
82          gMaxKeyValDataSize = atoi(pDataSize);
83       }
84
85       if(pBlacklistPath == NULL)
86       {
87          pBlacklistPath = "/etc/pclBackupBlacklist.txt";   // default path
88       }
89
90       if(readBlacklistConfigFile(pBlacklistPath) == -1)
91       {
92          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInitLibrary -> failed to access blacklist:"), DLT_STRING(pBlacklistPath));
93       }
94
95       if(setup_dbus_mainloop() == -1)
96       {
97          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to setup main loop"));
98          pthread_mutex_unlock(&gDbusPendingRegMtx);
99          return EPERS_DBUS_MAINLOOP;
100       }
101
102
103       if(gShutdownMode != PCL_SHUTDOWN_TYPE_NONE)
104       {
105          // register for lifecycle and persistence admin service dbus messages
106          if(register_lifecycle(shutdownMode) == -1)
107          {
108             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to register to lifecycle dbus interface"));
109             pthread_mutex_unlock(&gDbusPendingRegMtx);
110             return EPERS_REGISTER_LIFECYCLE;
111          }
112       }
113 #if USE_PASINTERFACE == 1
114       if(register_pers_admin_service() == -1)
115       {
116          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to register to pers admin dbus interface"));
117          pthread_mutex_unlock(&gDbusPendingRegMtx);
118          return EPERS_REGISTER_ADMIN;
119       }
120 #endif
121
122       /// get custom library names to load
123       status = get_custom_libraries();
124       if(status >= 0)
125       {
126          // initialize custom library structure
127          for(i = 0; i < PersCustomLib_LastEntry; i++)
128          {
129             invalidate_custom_plugin(i);
130          }
131
132          if(pOnDemandLoad == NULL)  // load all available libraries now
133          {
134             for(i=0; i < PersCustomLib_LastEntry; i++ )
135             {
136                if(check_valid_idx(i) != -1)
137                {
138                   if(load_custom_library(i, &gPersCustomFuncs[i] ) == 1)
139                   {
140                      if( (gPersCustomFuncs[i].custom_plugin_init) != NULL)
141                      {
142                         DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => Loaded plugin: "),
143                                                            DLT_STRING(get_custom_client_lib_name(i)));
144                         gPersCustomFuncs[i].custom_plugin_init();
145                      }
146                      else
147                      {
148                         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin functions: "),
149                                                             DLT_STRING(get_custom_client_lib_name(i)));
150                      }
151                   }
152                   else
153                   {
154                      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin: "),
155                                           DLT_STRING(get_custom_client_lib_name(i)));
156                   }
157                }
158                else
159                {
160                   continue;
161                }
162             }
163          }
164       }
165       else
166       {
167          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInit => Failed to load custom library config table => error number:"), DLT_INT(status));
168       }
169
170       // assign application name
171       strncpy(gAppId, appName, MaxAppNameLen);
172       gAppId[MaxAppNameLen-1] = '\0';
173
174       // destory mutex
175       pthread_mutex_destroy(&gDbusInitializedMtx);
176       pthread_cond_destroy(&gDbusInitializedCond);
177
178       gPclInitialized++;
179    }
180    else if(gPclInitialized >= PCLinitialized)
181    {
182       gPclInitialized++; // increment init counter
183       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T  Persistence Client Library - "), DLT_STRING(gAppId),
184                            DLT_STRING("- ONLY INCREMENT init counter: "), DLT_INT(gPclInitialized) );
185    }
186
187    return rval;
188 }
189
190
191
192 int pclDeinitLibrary(void)
193 {
194    int i = 0, rval = 1;
195
196    if(gPclInitialized == PCLinitialized)
197    {
198       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
199                                          DLT_STRING("- init counter: "), DLT_INT(gPclInitialized));
200       // unregister for lifecycle and persistence admin service dbus messages
201       if(gShutdownMode != PCL_SHUTDOWN_TYPE_NONE)
202          rval = unregister_lifecycle(gShutdownMode);
203
204 #if USE_PASINTERFACE == 1
205       rval = unregister_pers_admin_service();
206 #endif
207
208       // unload custom client libraries
209       for(i=0; i<PersCustomLib_LastEntry; i++)
210       {
211          if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
212          {
213             // deinitialize plugin
214             gPersCustomFuncs[i].custom_plugin_deinit();
215             // close library handle
216             dlclose(gPersCustomFuncs[i].handle);
217
218             invalidate_custom_plugin(i);
219          }
220       }
221
222       // close all apend rct
223       pers_rct_close_all();
224
225       // close opend database
226       pers_db_close_all();
227
228       gPclInitialized = PCLnotInitialized;
229
230       DLT_UNREGISTER_CONTEXT(gDLTContext);
231    }
232    else if(gPclInitialized > PCLinitialized)
233    {
234       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
235                                            DLT_STRING("- ONLY DECREMENT init counter: "), DLT_INT(gPclInitialized));
236       gPclInitialized--;   // decrement init counter
237    }
238    else
239    {
240       rval = PCLnotInitialized;
241    }
242
243    // end dbus library
244    bContinue = FALSE;
245
246    pthread_mutex_destroy(&gDbusPendingRegMtx);
247    return rval;
248 }
249
250
251
252
253
254