summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_custom_loader.c
blob: 9a8bd7ffb34ffa827ead03f547cee4381bde83c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/******************************************************************************
 * Project         Persistency
 * (c) copyright   2012
 * Company         XS Embedded GmbH
 *****************************************************************************/
/******************************************************************************
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
 /**
 * @file           persistence_client_library_custom_loader.c
 * @ingroup        Persistence client library
 * @author         Ingo Huerner
 * @brief          Implementation of persistence custom loadedr
 * @see
 */

#include "persistence_client_library_custom_loader.h"
#include "../include_protected/persistence_client_library_data_organization.h"

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dlfcn.h>


/// type definition of persistence custom library information
typedef struct sPersCustomLibInfo
{
   char libname[CustLibMaxLen];
   int valid;
} PersCustomLibInfo;


/// array with custom client library names
static PersCustomLibInfo gCustomLibArray[PersCustomLib_LastEntry];


PersistenceCustomLibs_e custom_client_name_to_id(const char* lib_name, int substring)
{
   PersistenceCustomLibs_e libId = PersCustomLib_LastEntry;

   if(substring == 0)
   {
      if(0 == strncmp(lib_name, "early", PersCustomPathSize) )
      {
         libId = PersCustomLib_early;
      }
      else if (0 == strncmp(lib_name, "secure", PersCustomPathSize) )
      {
         libId = PersCustomLib_secure;
      }
      else if (0 == strncmp(lib_name, "emergency", PersCustomPathSize) )
      {
         libId = PersCustomLib_emergency;
      }
      else if (0 == strncmp(lib_name, "hwinfo", PersCustomPathSize) )
      {
         libId = PersCustomLib_HWinfo;
      }
      else if (0 == strncmp(lib_name, "custom1", PersCustomPathSize) )
      {
         libId = PersCustomLib_Custom1;
      }
      else if (0 == strncmp(lib_name, "custom2", PersCustomPathSize) )
      {
         libId = PersCustomLib_Custom2;
      }
      else if (0 == strncmp(lib_name, "custom3", PersCustomPathSize) )
      {
         libId = PersCustomLib_Custom3;
      }
      else
      {
         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("custom_libname_to_id - error - id not found for lib:"), DLT_STRING(lib_name));
      }
   }
   else
   {
      if(NULL != strstr(lib_name, "early") )
      {
         libId = PersCustomLib_early;
      }
      else if (NULL != strstr(lib_name, "secure") )
      {
         libId = PersCustomLib_secure;
      }
      else if (NULL != strstr(lib_name, "emergency") )
      {
         libId = PersCustomLib_emergency;
      }
      else if (NULL != strstr(lib_name, "hwinfo") )
      {
         libId = PersCustomLib_HWinfo;
      }
      else if (NULL != strstr(lib_name, "custom1") )
      {
         libId = PersCustomLib_Custom1;
      }
      else if (NULL != strstr(lib_name, "custom2") )
      {
         libId = PersCustomLib_Custom2;
      }
      else if (NULL != strstr(lib_name, "custom3") )
      {
         libId = PersCustomLib_Custom3;
      }
      else
      {
         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("custom_libname_to_id - error - id not found for lib:"), DLT_STRING(lib_name));
      }

   }
   return libId;
}



int get_custom_libraries()
{
   int rval = 0, fd = 0, j = 0;

   struct stat buffer;
   char* delimiters = " \n";   // search for blank and end of line
   char* configFileMap = 0;
   char* token = 0;
   const char *filename = getenv("PERS_CLIENT_LIB_CUSTOM_LOAD");

   if(filename == NULL)
   {
      filename = "/etc/pclCustomLibConfigFile.cfg";  // use default filename
   }

   for(j=0; j<PersCustomLib_LastEntry; j++)
   {
      // init pos to -1
      gCustomLibArray[j].valid = -1;
   }


   if(stat(filename, &buffer) != -1)
   {
      fd = open(filename, O_RDONLY);
      if (fd != -1)
      {
         configFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);

         if(configFileMap != MAP_FAILED)
         {
            int libId = 0;

            // get the library identifier (early, secure, emergency, ...)
            token = strtok(configFileMap, delimiters);
            libId = custom_client_name_to_id(token, 0);


            if(libId < PersCustomLib_LastEntry)
            {
               gCustomLibArray[libId].valid = 1;
            }
            else
            {
            	munmap(configFileMap, buffer.st_size); // @CB: Add
            	close(fd); // @CB: Add // close file descriptor before return
                return EPERS_OUTOFBOUNDS; // out of array bounds
            }

            // get the library name
            token  = strtok (NULL, delimiters);
            strncpy(gCustomLibArray[libId].libname, token, CustLibMaxLen);
            gCustomLibArray[libId].libname[CustLibMaxLen-1] = '\0'; // Ensures 0-Termination

            while( token != NULL )
            {
               // get the library identifier (early, secure, emergency, ...)
               token = strtok(NULL, delimiters);
               if(token != NULL)
               {
                  libId = custom_client_name_to_id(token, 0);
                  if(libId < PersCustomLib_LastEntry)
                  {
                     gCustomLibArray[libId].valid = 1;
                  }
                  else
                  {
                     rval = EPERS_OUTOFBOUNDS;
                     break;
                  }
               }
               else
               {
                  break;
               }

               // get the library name
               token  = strtok (NULL, delimiters);
               if(token != NULL)
               {
                  strncpy(gCustomLibArray[libId].libname, token, CustLibMaxLen);
                  gCustomLibArray[libId].libname[CustLibMaxLen-1] = '\0'; // Ensures 0-Termination
               }
               else
               {
                  break;
               }
            }

            munmap(configFileMap, buffer.st_size);

            #if 0 // debuging
            for(j=0; j<PersCustomLib_LastEntry; j++)
            {
               printf("Custom libraries => Name: %s | valid: %d \n", gCustomLibArray[j].libname, gCustomLibArray[j].valid);
            }
            #endif
         }
         else
         {
            rval = EPERS_CONFIGMAPFAILED;
            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error - mapping of file failed"));
         }
         close(fd);
      }
      else
      {
         rval = EPERS_CONFIGNOTAVAILABLE;
         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error - no file with plugins available:"), DLT_STRING(filename), DLT_STRING(strerror(errno)));
      }
   }
   else
   {
      rval = EPERS_CONFIGNOSTAT;
      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("lload config file error - can't stat config file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)));
   }
   return rval;
}



int load_custom_library(PersistenceCustomLibs_e customLib, Pers_custom_functs_s *customFuncts)
{
   int rval = 1;
   char *error;

   if(customLib < PersCustomLib_LastEntry)
   {
      void* handle = dlopen(gCustomLibArray[customLib].libname, RTLD_LAZY);
      customFuncts->handle = handle;

      if(handle != NULL)
      {
         dlerror();    // reset error

         // plugin_close
         *(void **) (&customFuncts->custom_plugin_handle_close) = dlsym(handle, "plugin_handle_close");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         }
         // custom_plugin_delete_data
         *(void **) (&customFuncts->custom_plugin_delete_data) = dlsym(handle, "plugin_delete_data");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
         // custom_plugin_get_data
         *(void **) (&customFuncts->custom_plugin_handle_get_data) = dlsym(handle, "plugin_handle_get_data");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
         // custom_plugin_get_data
         *(void **) (&customFuncts->custom_plugin_get_data) = dlsym(handle, "plugin_get_data");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
         // custom_plugin_init
         *(void **) (&customFuncts->custom_plugin_init) = dlsym(handle, "plugin_init");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
         // custom_plugin_deinit
         *(void **) (&customFuncts->custom_plugin_deinit) = dlsym(handle, "plugin_deinit");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
         // custom_plugin_open
         *(void **) (&customFuncts->custom_plugin_handle_open) = dlsym(handle, "plugin_handle_open");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
         // custom_plugin_set_data
         *(void **) (&customFuncts->custom_plugin_handle_set_data) = dlsym(handle, "plugin_handle_set_data");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
         // custom_plugin_set_data
         *(void **) (&customFuncts->custom_plugin_set_data) = dlsym(handle, "plugin_set_data");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         }
         // custom_plugin_get_size_handle
         *(void **) (&customFuncts->custom_plugin_handle_get_size) = dlsym(handle, "plugin_handle_get_size");
         if ((error = dlerror()) != NULL)
         {
            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         }
         // custom_plugin_get_size
         *(void **) (&customFuncts->custom_plugin_get_size) = dlsym(handle, "plugin_get_size");
         if ((error = dlerror()) != NULL)
         {
            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         }
          // create backup
         *(void **) (&customFuncts->custom_plugin_create_backup) = dlsym(handle, "plugin_create_backup");
         if ((error = dlerror()) != NULL)
         {
            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         }
         // restore backup
         *(void **) (&customFuncts->custom_plugin_restore_backup) = dlsym(handle, "plugin_restore_backup");
         if ((error = dlerror()) != NULL)
         {
             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         }
         // restore backup
         *(void **) (&customFuncts->custom_plugin_get_backup) = dlsym(handle, "plugin_get_backup");
         if ((error = dlerror()) != NULL)
         {
             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         }

         // custom_plugin_get_status_notification_clbk
         *(void **) (&customFuncts->custom_plugin_get_status_notification_clbk) = dlsym(handle, "plugin_get_status_notification_clbk");
         if ((error = dlerror()) != NULL)
         {
              DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
          }
      }
      else
      {
         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
         rval = EPERS_DLOPENERROR;
      }
   }
   else
   {
      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error: - customLib out of bounds"));
      rval = EPERS_DLOPENERROR;
   }

   return rval;
}



int load_all_custom_libraries()
{
   int rval = 0,
          i = 0;

   for(i=0; i<PersCustomLib_LastEntry; i++)
   {
      rval = load_custom_library(i, &gPersCustomFuncs[i]);
      if( rval < 0)
      {
         // printf("load_all_custom_libraries - error loading library number [%d] \n", i);
         break;
      }
   }
   return rval;
}


char* get_custom_client_lib_name(int idx)
{
   if(idx < PersCustomLib_LastEntry)
   {
      return gCustomLibArray[idx].libname;
   }
   else
   {
      return NULL;
   }
}

//int get_custom_client_position_in_array(int customLibId)
int check_valid_idx(int idx)
{
   int rval = -1;

   if(idx < PersCustomLib_LastEntry)
   {
      rval = gCustomLibArray[idx].valid;
   }

   return rval;
}