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