config_load_configmodule.c 6.3 KB
Newer Older
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
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file common/config/config_load_configmodule.c
 * \brief configuration module, load the shared library implementing the configuration module 
 * \author Francois TABURET
 * \date 2017
 * \version 0.1
 * \company NOKIA BellLabs France
 * \email: francois.taburet@nokia-bell-labs.com
 * \note
 * \warning
 */
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <dlfcn.h>

#define CONFIG_LOADCONFIG_MAIN
#include "config_load_configmodule.h"
#include "config_userapi.h"
#define CONFIG_SHAREDLIBFORMAT "libparams_%s.so"

int load_config_sharedlib(char *cfgmode, char *cfgP[], int numP, configmodule_interface_t *cfgptr)
{
void *lib_handle;
char fname[128];
char libname[FILENAME_MAX]; 
int st;

   st=0;  
   sprintf(libname,CONFIG_SHAREDLIBFORMAT,cfgmode);

   lib_handle = dlopen(libname,RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
   if (!lib_handle) {
      fprintf(stderr,"[CONFIG] %s %d Error calling dlopen(%s): %s\n",__FILE__, __LINE__, libname,dlerror());
      st = -1;
   } else { 
      sprintf (fname,"config_%s_init",cfgmode);
      cfgptr->init = dlsym(lib_handle,fname);

      if (cfgptr->init == NULL ) {
         printf("[CONFIG] %s %d no function %s for config mode %s\n",
               __FILE__, __LINE__,fname, cfgmode);
      } else {
         st=cfgptr->init(cfgP,numP); 
         printf("[CONFIG] function %s returned %i\n",
               fname, st);	 
      }

      sprintf (fname,"config_%s_get",cfgmode);
      cfgptr->get = dlsym(lib_handle,fname);
      if (cfgptr->get == NULL ) { 
         printf("[CONFIG] %s %d no function %s for config mode %s\n",
               __FILE__, __LINE__,fname, cfgmode);
	 st = -1;
      }
      
      sprintf (fname,"config_%s_getlist",cfgmode);
      cfgptr->getlist = dlsym(lib_handle,fname);
      if (cfgptr->getlist == NULL ) { 
         printf("[CONFIG] %s %d no function %s for config mode %s\n",
               __FILE__, __LINE__,fname, cfgmode);
	 st = -1;
      }

      sprintf (fname,"config_%s_end",cfgmode);
      cfgptr->end = dlsym(lib_handle,fname);
      if (cfgptr->getlist == NULL ) { 
         printf("[CONFIG] %s %d no function %s for config mode %s\n",
               __FILE__, __LINE__,fname, cfgmode);
      }      
   } 
   
   return st;	       
}
/*-----------------------------------------------------------------------------------*/
/* from here: interface implementtion of the configuration module */


configmodule_interface_t *load_configmodule(int argc, char **argv)
{
char *cfgparam=NULL;
char *modeparams=NULL;
char *cfgmode=NULL;
char *strtokctx=NULL;
char *cfgP[CONFIG_MAX_OOPT_PARAMS];

int i; 
int p;  

 
  for(i=0; i<CONFIG_MAX_OOPT_PARAMS ; i++) {
      cfgP[i]=NULL;
  }
    
/* first parse the command line to look for the -O option */
  opterr=0;
  while ((i = getopt(argc, argv, "O:")) != -1) {
       if ( i == 'O' ) {
          cfgparam = optarg; 
       }      
   }
/* look for the OAI_CONFIGMODULE environement variable */
  if ( cfgparam == NULL ) {
     cfgparam = getenv("OAI_CONFIGMODULE");
     }

/* default */
  if (cfgparam == NULL) {
     cfgparam = "libconfig:oaisoftmodem.conf";
     }
/* parse the config parameters to set the config source */
   i = sscanf(cfgparam,"%m[^':']:%ms",&cfgmode,&modeparams);
   if (i< 0) {
       fprintf(stderr,"[CONFIG] %s, %d, sscanf error parsing config source  %s: %s\n", __FILE__, __LINE__,cfgparam, strerror(errno));
       return NULL;
   }
   else if ( i == 1 ) {
       modeparams=cfgmode;
       cfgmode=strdup("libconfig");
   }

   cfgptr = malloc(sizeof(configmodule_interface_t));

   p=0;
   cfgP[p]=strtok_r(modeparams,":",&strtokctx);     
   while ( p< CONFIG_MAX_OOPT_PARAMS && cfgP[p] != NULL) {
       char *aptr;
       aptr=strcasestr(cfgP[p],"dbgl");
       if (aptr != NULL) {
           cfgptr->rtflags = strtol(aptr+4,NULL,0);
           Config_Params[CONFIGPARAM_DEBUGFLAGS_IDX].paramflags = Config_Params[CONFIGPARAM_DEBUGFLAGS_IDX].paramflags | PARAMFLAG_DONOTREAD;
           for (int j=p; j<(CONFIG_MAX_OOPT_PARAMS-1); j++) cfgP[j] = cfgP[j+1];
           p--;
       }
       p++;
       cfgP[p] = strtok_r(NULL,":",&strtokctx);
   }

   
   printf("[CONFIG] get parameters from %s ",cfgmode);
   for (i=0;i<p; i++) {
        printf("%s ",cfgP[i]); 
   }
   printf("\n");

 
   i=load_config_sharedlib(cfgmode, cfgP,p,cfgptr);
   if (i< 0) {
      fprintf(stderr,"[CONFIG] %s %d config module %s couldn't be loaded\n", __FILE__, __LINE__,cfgmode);
      return NULL;
   } else {
      printf("[CONFIG] config module %s loaded\n",cfgmode);
   }

   Config_Params[CONFIGPARAM_DEBUGFLAGS_IDX].uptr=&(cfgptr->rtflags);
   config_get(Config_Params,CONFIG_PARAMLENGTH(Config_Params), CONFIG_SECTIONNAME );   

   if (modeparams != NULL) free(modeparams);
   if (cfgmode != NULL) free(cfgmode);
   optind=1;
   cfgptr->argc = argc;
   cfgptr->argv  = argv;    
   return cfgptr;
}

void end_configmodule()
{ 
  if (cfgptr != NULL) {
      printf ("[CONFIG] free %u pointers\n",cfgptr->numptrs);  
      for(int i=0; i<cfgptr->numptrs ; i++) {
          if (cfgptr->ptrs[i] != NULL) {
             free(cfgptr->ptrs[i]);
          }
          cfgptr->ptrs[i]=NULL;
      }
      if (cfgptr->end != NULL) {
      printf ("[CONFIG] calling config module end function...\n"); 
      cfgptr->end();
      }
  free(cfgptr);
  cfgptr=NULL;
  }
}