Skip to content
Snippets Groups Projects
config_load_configmodule.c 13.2 KiB
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 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 411 412 413 414 415
/*
 * 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.1  (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>
#include <platform_types.h>

#define CONFIG_LOADCONFIG_MAIN
#include "config_load_configmodule.h"
#include "config_userapi.h"
#include "../utils/LOG/log.h"
#define CONFIG_SHAREDLIBFORMAT "libparams_%s.so"
#include "nfapi/oai_integration/vendor_ext.h"

int load_config_sharedlib(configmodule_interface_t *cfgptr) {
  void *lib_handle;
  char fname[128];
  char libname[FILENAME_MAX];
  int st;
  st=0;
  sprintf(libname,CONFIG_SHAREDLIBFORMAT,cfgptr->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",cfgptr->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, cfgptr->cfgmode);
    } else {
      st=cfgptr->init(cfgptr->cfgP,cfgptr->num_cfgP);
      printf("[CONFIG] function %s returned %i\n",
             fname, st);
    }

    sprintf (fname,"config_%s_get",cfgptr->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, cfgptr->cfgmode);
      st = -1;
    }

    sprintf (fname,"config_%s_getlist",cfgptr->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, cfgptr->cfgmode);
      st = -1;
    }

    if (cfgptr->rtflags & CONFIG_SAVERUNCFG) {
      sprintf(fname, "config_%s_set", cfgptr->cfgmode);
      cfgptr->set = dlsym(lib_handle, fname);

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

      if (cfgptr->write_parsedcfg == NULL) {
        printf("[CONFIG] %s %d no function %s for config mode %s\n", __FILE__, __LINE__, fname, cfgptr->cfgmode);
      }
    }

    sprintf (fname,"config_%s_end",cfgptr->cfgmode);
    cfgptr->end = dlsym(lib_handle,fname);

    if (cfgptr->end == NULL) {
      printf("[CONFIG] %s %d no function %s for config mode %s\n",
             __FILE__, __LINE__,fname, cfgptr->cfgmode);
    }
  }

  return st;
}
/*-----------------------------------------------------------------------------------*/
/* from here: interface implementtion of the configuration module */
int nooptfunc(void) {
  return 0;
};

int config_cmdlineonly_getlist(paramlist_def_t *ParamList,
                               paramdef_t *params, int numparams, char *prefix) {
  ParamList->numelt = 0;
  return 0;
}


int config_cmdlineonly_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
  int defval;
  int fatalerror=0;
  int numdefvals=0;

  for(int i=0; i<numoptions; i++) {
    defval=0;

    switch(cfgoptions[i].type) {
      case TYPE_STRING:
        defval=config_setdefault_string(&(cfgoptions[i]), prefix);
        break;

      case TYPE_STRINGLIST:
        defval=config_setdefault_stringlist(&(cfgoptions[i]), prefix);
        break;

      case TYPE_UINT8:
      case TYPE_INT8:
      case TYPE_UINT16:
      case TYPE_INT16:
      case TYPE_UINT32:
      case TYPE_INT32:
      case TYPE_MASK:
        defval=config_setdefault_int(&(cfgoptions[i]), prefix);
        break;

      case TYPE_UINT64:
      case TYPE_INT64:
        defval=config_setdefault_int64(&(cfgoptions[i]), prefix);
        break;

      case TYPE_UINTARRAY:
      case TYPE_INTARRAY:
        defval=config_setdefault_intlist(&(cfgoptions[i]), prefix);
        break;

      case TYPE_DOUBLE:
        defval=config_setdefault_double(&(cfgoptions[i]), prefix);
        break;

      case TYPE_IPV4ADDR:
        defval=config_setdefault_ipv4addr(&(cfgoptions[i]), prefix);
        break;

      default:
        fprintf(stderr,"[CONFIG] %s.%s type %i not supported\n",prefix, cfgoptions[i].optname,cfgoptions[i].type);
        fatalerror=1;
        break;
    } /* switch on param type */

    if (defval == 1) {
      numdefvals++;
      cfgoptions[i].paramflags = cfgoptions[i].paramflags |  PARAMFLAG_PARAMSETDEF;
    }
  } /* for loop on options */

  printf("[CONFIG] %s: %i/%i parameters successfully set \n",
         ((prefix == NULL)?"(root)":prefix),
         numdefvals,numoptions );

  if (fatalerror == 1) {
    fprintf(stderr,"[CONFIG] fatal errors found when assigning %s parameters \n",
            prefix);
  }

  return numdefvals;
}

configmodule_interface_t *load_configmodule(int argc,
					    char **argv,
					    uint32_t initflags)
{
  char *cfgparam=NULL;
  char *modeparams=NULL;
  char *cfgmode=NULL;
  char *strtokctx=NULL;
  char *atoken;
  uint32_t tmpflags=0;
  int i;
  int OoptIdx=-1;
  int OWoptIdx = -1;

  printf("CMDLINE: ");
  for (int i=0; i<argc; i++)
    printf("\"%s\" ", argv[i]);
  printf("\n");

  /* first parse the command line to look for the -O option */
  for (i = 0; i<argc; i++) {
    if (strlen(argv[i]) < 2)
    	continue;

    if ( argv[i][1] == 'O' && i < (argc -1)) {
      cfgparam = argv[i+1];
      OoptIdx=i;
    }

    char *OWopt = strstr(argv[i], "OW");
    if (OWopt == argv[i] + 2) {
      OWoptIdx = i;
    }
    if ( strstr(argv[i], "help_config") != NULL  ) {
      config_printhelp(Config_Params,CONFIG_PARAMLENGTH(Config_Params),CONFIG_SECTIONNAME);
      exit(0);
    }

    if ( (strcmp(argv[i]+1, "h") == 0) || (strstr(argv[i]+1, "help_") != NULL ) ) {
      tmpflags = CONFIG_HELP;
    }
  }

  /* look for the OAI_CONFIGMODULE environment variable */
  if ( cfgparam == NULL ) {
    cfgparam = getenv("OAI_CONFIGMODULE");
  }

  /* default different for UE and softmodem because UE may run without config file */
  /* and -O option is not mandatory for UE                                    */
  /* phy simulators behave as UE                                              */
  if (cfgparam == NULL) {
    tmpflags = tmpflags | CONFIG_NOOOPT;

    if ( initflags & CONFIG_ENABLECMDLINEONLY) {
      cfgparam = CONFIG_CMDLINEONLY ":dbgl0" ;
    } else {
      cfgparam = CONFIG_CMDLINEONLY ":dbgl0" ;
      cfgparam = CONFIG_LIBCONFIGFILE ":" DEFAULT_CFGFILENAME;
    }
  }

  /* 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));
    exit(-1) ;
  } else if ( i == 1 ) {
    /* -O argument doesn't contain ":" separator, assume -O <conf file> option, default cfgmode to libconfig
       with one parameter, the path to the configuration file cfgmode must not be NULL */
    modeparams=cfgmode;
    cfgmode=strdup(CONFIG_LIBCONFIGFILE);
  }
  if (cfgptr == NULL) {
    cfgptr = calloc(sizeof(configmodule_interface_t),1);
  /* argv_info is used to memorize command line options which have been recognized */
  /* and to detect unrecognized command line options which might have been specified */
    cfgptr->argv_info = calloc(sizeof(int32_t), argc+10);
  /* argv[0] is the exec name, always Ok */
    cfgptr->argv_info[0] |= CONFIG_CMDLINEOPT_PROCESSED;

    /* When reuested _(_--OW or rtflag is 5), a file with config parameters, as defined after all processing, will be created */
    if (OWoptIdx >= 0) {
      cfgptr->argv_info[OWoptIdx] |= CONFIG_CMDLINEOPT_PROCESSED;
      cfgptr->rtflags |= CONFIG_SAVERUNCFG;
    }
  /* when OoptIdx is >0, -O option has been detected at position OoptIdx 
   *  we must memorize arv[OoptIdx is Ok                                  */ 
    if (OoptIdx >= 0) {
      cfgptr->argv_info[OoptIdx] |= CONFIG_CMDLINEOPT_PROCESSED;
      cfgptr->argv_info[OoptIdx+1] |= CONFIG_CMDLINEOPT_PROCESSED;
    }

    cfgptr->rtflags = cfgptr->rtflags | tmpflags;
    cfgptr->argc   = argc;
    cfgptr->argv   = argv;
    cfgptr->cfgmode=strdup(cfgmode);
    cfgptr->num_cfgP=0;
    atoken=strtok_r(modeparams,":",&strtokctx);

    while ( cfgptr->num_cfgP< CONFIG_MAX_OOPT_PARAMS && atoken != NULL) {
    /* look for debug level in the config parameters, it is common to all config mode
       and will be removed from the parameter array passed to the shared module */
      char *aptr;
      aptr=strcasestr(atoken,"dbgl");

      if (aptr != NULL) {
        cfgptr->rtflags = cfgptr->rtflags | strtol(aptr+4,NULL,0);
      } else {
        cfgptr->cfgP[cfgptr->num_cfgP] = strdup(atoken);
        cfgptr->num_cfgP++;
      }

      atoken = strtok_r(NULL,":",&strtokctx);
    }

    printf("[CONFIG] get parameters from %s ",cfgmode);
  }
  for (i=0; i<cfgptr->num_cfgP; i++) {
    printf("%s ",cfgptr->cfgP[i]);
  }

  if (cfgptr->rtflags & CONFIG_PRINTPARAMS) {
    cfgptr->status = malloc(sizeof(configmodule_status_t));
  }
  if (strstr(cfgparam,CONFIG_CMDLINEONLY) == NULL) {
    i=load_config_sharedlib(cfgptr);

    if (i ==  0) {
      printf("[CONFIG] config module %s loaded\n",cfgmode);
      int idx = config_paramidx_fromname(Config_Params, CONFIG_PARAMLENGTH(Config_Params), CONFIGP_DEBUGFLAGS);
      Config_Params[idx].uptr = &(cfgptr->rtflags);
      idx = config_paramidx_fromname(Config_Params, CONFIG_PARAMLENGTH(Config_Params), CONFIGP_TMPDIR);
      Config_Params[idx].strptr = &(cfgptr->tmpdir);
      config_get(Config_Params,CONFIG_PARAMLENGTH(Config_Params), CONFIG_SECTIONNAME );
    } else {
      fprintf(stderr,"[CONFIG] %s %d config module \"%s\" couldn't be loaded\n", __FILE__, __LINE__,cfgmode);
      cfgptr->rtflags = cfgptr->rtflags | CONFIG_HELP | CONFIG_ABORT;
    }
  } else {
    cfgptr->init = (configmodule_initfunc_t)nooptfunc;
    cfgptr->get = config_cmdlineonly_get;
    cfgptr->getlist = config_cmdlineonly_getlist;
    cfgptr->end = (configmodule_endfunc_t)nooptfunc;
  }

  printf("[CONFIG] debug flags: 0x%08x\n", cfgptr->rtflags);

  if (modeparams != NULL) free(modeparams);

  if (cfgmode != NULL) free(cfgmode);

  if (CONFIG_ISFLAGSET(CONFIG_ABORT)) {
    config_printhelp(Config_Params,CONFIG_PARAMLENGTH(Config_Params),CONFIG_SECTIONNAME );
    //       exit(-1);
  }

  return cfgptr;
}

/* Possibly write config file, with parameters which have been read and  after command line parsing */
void write_parsedcfg(void)
{
  if (cfgptr->status && (cfgptr->rtflags & CONFIG_SAVERUNCFG)) {
    printf_params("[CONFIG] Runtime params creation status: %i null values, %i errors, %i empty list or array, %i successfull \n",
                  cfgptr->status->num_err_nullvalue,
                  cfgptr->status->num_err_write,
                  cfgptr->status->emptyla,
                  cfgptr->status->num_write);
  }
  if (cfgptr != NULL) {
    if (cfgptr->write_parsedcfg != NULL) {
      printf("[CONFIG] calling config module write_parsedcfg function...\n");
      cfgptr->write_parsedcfg();
    }
  }
}

/* free memory allocated when reading parameters */
/* config module could be initialized again after this call */
void end_configmodule(void) {
  write_parsedcfg();
  if (cfgptr != NULL) {
    if (cfgptr->end != NULL) {
      printf ("[CONFIG] calling config module end function...\n");
      cfgptr->end();
    }

    pthread_mutex_lock(&cfgptr->memBlocks_mutex);
    printf ("[CONFIG] free %u config value pointers\n",cfgptr->numptrs);

    for(int i=0; i<cfgptr->numptrs ; i++) {
      if (cfgptr->oneBlock[i].ptrs != NULL && cfgptr->oneBlock[i].ptrsAllocated== true && cfgptr->oneBlock[i].toFree) {
        free(cfgptr->oneBlock[i].ptrs);
        memset(&cfgptr->oneBlock[i], 0, sizeof(cfgptr->oneBlock[i]));
      }
    }
    
    cfgptr->numptrs=0;
    pthread_mutex_unlock(&cfgptr->memBlocks_mutex);
    if ( cfgptr->cfgmode )
      free(cfgptr->cfgmode);

    if (  cfgptr->argv_info )
      free( cfgptr->argv_info );

    free(cfgptr);
    cfgptr=NULL;
  }
}