ac1c8307c67061af407dcd47abaf3e9fdd086847
[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       pthread_mutex_lock(&gDbusPendingRegMtx);   // block until pending received
70
71       if(pDataSize != NULL)
72       {
73          gMaxKeyValDataSize = atoi(pDataSize);
74       }
75
76       if(pBlacklistPath == NULL)
77       {
78          pBlacklistPath = "/etc/pclBackupBlacklist.txt";   // default path
79       }
80
81       if(readBlacklistConfigFile(pBlacklistPath) == -1)
82       {
83          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInitLibrary -> failed to access blacklist:"), DLT_STRING(pBlacklistPath));
84       }
85
86       if(setup_dbus_mainloop() == -1)
87       {
88          DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to setup main loop"));
89          pthread_mutex_unlock(&gDbusPendingRegMtx);
90          return EPERS_DBUS_MAINLOOP;
91       }
92
93       if(gShutdownMode != PCL_SHUTDOWN_TYPE_NONE)
94       {
95          // register for lifecycle and persistence admin service dbus messages
96          if(register_lifecycle(shutdownMode) == -1)
97          {
98             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => Failed to register to lifecycle dbus interface"));
99             pthread_mutex_unlock(&gDbusPendingRegMtx);
100             return EPERS_REGISTER_LIFECYCLE;
101          }
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          pthread_mutex_unlock(&gDbusPendingRegMtx);
108          return EPERS_REGISTER_ADMIN;
109       }
110
111       /// get custom library names to load
112       status = get_custom_libraries();
113       if(status >= 0)
114       {
115          // initialize custom library structure
116          for(i = 0; i < PersCustomLib_LastEntry; i++)
117          {
118             invalidate_custom_plugin(i);
119          }
120
121          if(pOnDemandLoad == NULL)  // load all available libraries now
122          {
123             for(i=0; i < PersCustomLib_LastEntry; i++ )
124             {
125                if(check_valid_idx(i) != -1)
126                {
127                   if(load_custom_library(i, &gPersCustomFuncs[i] ) == 1)
128                   {
129                      if( (gPersCustomFuncs[i].custom_plugin_init) != NULL)
130                      {
131                         DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => Loaded plugin: "),
132                                                            DLT_STRING(get_custom_client_lib_name(i)));
133                         gPersCustomFuncs[i].custom_plugin_init();
134                      }
135                      else
136                      {
137                         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin functions: "),
138                                                             DLT_STRING(get_custom_client_lib_name(i)));
139                      }
140                   }
141                   else
142                   {
143                      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin: "),
144                                           DLT_STRING(get_custom_client_lib_name(i)));
145                   }
146                }
147                else
148                {
149                   continue;
150                }
151             }
152          }
153       }
154       else
155       {
156          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInit => Failed to load custom library config table => error number:"), DLT_INT(status));
157       }
158
159       // assign application name
160       strncpy(gAppId, appName, MaxAppNameLen);
161       gAppId[MaxAppNameLen-1] = '\0';
162
163       // destory mutex
164       pthread_mutex_destroy(&gDbusInitializedMtx);
165       pthread_cond_destroy(&gDbusInitializedCond);
166
167       gPclInitialized++;
168    }
169    else if(gPclInitialized >= PCLinitialized)
170    {
171       gPclInitialized++; // increment init counter
172       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T  Persistence Client Library - "), DLT_STRING(gAppId),
173                            DLT_STRING("- ONLY INCREMENT init counter: "), DLT_INT(gPclInitialized) );
174    }
175
176    return rval;
177 }
178
179
180
181 int pclDeinitLibrary(void)
182 {
183    int i = 0, rval = 1;
184
185    if(gPclInitialized == PCLinitialized)
186    {
187       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
188                                          DLT_STRING("- init counter: "), DLT_INT(gPclInitialized));
189
190       // unregister for lifecycle and persistence admin service dbus messages
191       if(gShutdownMode != PCL_SHUTDOWN_TYPE_NONE)
192          rval = unregister_lifecycle(gShutdownMode);
193
194       rval = unregister_pers_admin_service();
195
196       // unload custom client libraries
197       for(i=0; i<PersCustomLib_LastEntry; i++)
198       {
199          if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
200          {
201             // deinitialize plugin
202             gPersCustomFuncs[i].custom_plugin_deinit();
203             // close library handle
204             dlclose(gPersCustomFuncs[i].handle);
205
206             invalidate_custom_plugin(i);
207          }
208       }
209
210       gPclInitialized = PCLnotInitialized;
211
212       DLT_UNREGISTER_CONTEXT(gDLTContext);
213    }
214    else if(gPclInitialized > PCLinitialized)
215    {
216       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
217                                            DLT_STRING("- ONLY DECREMENT init counter: "), DLT_INT(gPclInitialized));
218       gPclInitialized--;   // decrement init counter
219    }
220
221    // end dbus library
222    bContinue = FALSE;
223
224    pthread_mutex_destroy(&gDbusPendingRegMtx);
225    return rval;
226 }
227
228
229
230
231
232