config_libconfig.c 11.9 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
/*
 * 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/libconfig/config_libconfig.c
 * \brief: implementation libconfig configuration library
 * \author Francois TABURET
 * \date 2017
 * \version 0.1
 * \company NOKIA BellLabs France
 * \email: francois.taburet@nokia-bell-labs.com
 * \note
 * \warning
 */
31 32 33 34 35 36 37 38 39 40 41 42 43
#define _GNU_SOURCE
#include <libconfig.h>

#include <string.h>
#include <stdlib.h>

#include <arpa/inet.h>

#include "config_libconfig.h"
#include "config_libconfig_private.h"
#include "../config_userapi.h"
#include "errno.h"

44
#if ( LIBCONFIG_VER_MAJOR == 1 && LIBCONFIG_VER_MINOR < 5)
45
  #define config_setting_lookup config_lookup_from
46 47
#endif

48 49
void config_libconfig_end(void );

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
int read_strlist(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath) {
  const char *str;
  int st;
  int numelt;
  numelt=config_setting_length(setting);
  config_check_valptr(cfgoptions,(char **)&(cfgoptions->strlistptr), sizeof(char *) * numelt);
  st=0;

  for (int i=0; i< numelt ; i++) {
    str=config_setting_get_string_elem(setting,i);

    if (str==NULL) {
      printf("[LIBCONFIG] %s%i  not found in config file\n", cfgoptions->optname,i);
    } else {
      config_check_valptr(cfgoptions,&(cfgoptions->strlistptr[i]),strlen(str)+1);
      sprintf(cfgoptions->strlistptr[i],"%s",str);
      st++;
      printf_params("[LIBCONFIG] %s%i: %s\n", cfgpath,i,cfgoptions->strlistptr[i]);
    }
  }

  cfgoptions->numelt=numelt;
  return st;
73 74
}

75 76 77 78 79 80 81 82 83 84 85 86 87
int read_intarray(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath) {
  cfgoptions->numelt=config_setting_length(setting);

  if (cfgoptions->numelt > 0) {
    cfgoptions->iptr=malloc(sizeof(int) * (cfgoptions->numelt));

    for (int i=0; i< cfgoptions->numelt && cfgoptions->iptr != NULL; i++) {
      cfgoptions->iptr[i]=config_setting_get_int_elem(setting,i);
      printf_params("[LIBCONFIG] %s[%i]: %i\n", cfgpath,i,cfgoptions->iptr[i]);
    } /* for loop on array element... */
  }

  return cfgoptions->numelt;
88 89 90 91 92
}




93
int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
94
  config_setting_t *setting;
95
  char *str;
96 97
  int i,u;
  long long int llu;
98
  double dbl;
99 100 101
  int rst;
  int status=0;
  int notfound;
102
  int defval;
103
  int fatalerror=0;
104
  int numdefvals=0;
Cedric Roux's avatar
Cedric Roux committed
105 106
  char cfgpath[512];  /* 512 should be enough for the sprintf below */

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
  for(i=0; i<numoptions; i++) {
    if (prefix != NULL) {
      sprintf(cfgpath,"%s.%s",prefix,cfgoptions[i].optname);
    } else {
      sprintf(cfgpath,"%s",cfgoptions[i].optname);
    }

    if( (cfgoptions->paramflags & PARAMFLAG_DONOTREAD) != 0) {
      printf_params("[LIBCONFIG] %s.%s ignored\n", cfgpath,cfgoptions[i].optname );
      continue;
    }

    notfound=0;
    defval=0;

    switch(cfgoptions[i].type) {
      case TYPE_STRING:
        if ( config_lookup_string(&(libconfig_privdata.cfg),cfgpath, (const char **)&str)) {
          if ( cfgoptions[i].numelt > 0  && str != NULL && strlen(str) >= cfgoptions[i].numelt ) {
            fprintf(stderr,"[LIBCONFIG] %s:  %s exceeds maximum length of %i bytes, value truncated\n",
                    cfgpath,str,cfgoptions[i].numelt);
            str[strlen(str)-1] = 0;
          }

          if (cfgoptions[i].numelt == 0 ) {
            //          config_check_valptr(&(cfgoptions[i]), (char **)(&(cfgoptions[i].strptr)), sizeof(char *));
            config_check_valptr(&(cfgoptions[i]), cfgoptions[i].strptr, strlen(str)+1);
            sprintf( *(cfgoptions[i].strptr) , "%s", str);
            printf_params("[LIBCONFIG] %s: \"%s\"\n", cfgpath,*(cfgoptions[i].strptr) );
          } else {
            sprintf( (char *)(cfgoptions[i].strptr) , "%s", str);
            printf_params("[LIBCONFIG] %s: \"%s\"\n", cfgpath,(char *)cfgoptions[i].strptr );
          }
        } else {
          defval=config_setdefault_string(&(cfgoptions[i]),prefix);
        }

144
        break;
145 146 147 148 149 150 151 152 153 154

      case TYPE_STRINGLIST:
        setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );

        if ( setting != NULL) {
          read_strlist(&cfgoptions[i],setting,cfgpath);
        } else {
          defval=config_setdefault_stringlist(&(cfgoptions[i]),prefix);
        }

155
        break;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

      case TYPE_UINT8:
      case TYPE_INT8:
      case TYPE_UINT16:
      case TYPE_INT16:
      case TYPE_UINT32:
      case TYPE_INT32:
      case TYPE_MASK:
        if ( config_lookup_int(&(libconfig_privdata.cfg),cfgpath, &u)) {
          config_check_valptr(&(cfgoptions[i]), (char **)(&(cfgoptions[i].iptr)),sizeof(int32_t));
          config_assign_int(&(cfgoptions[i]),cfgpath,u);
        } else {
          defval=config_setdefault_int(&(cfgoptions[i]),prefix);
        }

171
        break;
172 173 174 175 176 177 178 179 180 181

      case TYPE_UINT64:
      case TYPE_INT64:
        if ( config_lookup_int64(&(libconfig_privdata.cfg),cfgpath, &llu)) {
          config_check_valptr(&(cfgoptions[i]), (char **)&(cfgoptions[i].i64ptr),sizeof(long long));

          if(cfgoptions[i].type==TYPE_UINT64) {
            *(cfgoptions[i].u64ptr) = (uint64_t)llu;
            printf_params("[LIBCONFIG] %s: %llu\n", cfgpath,(long long unsigned)(*(cfgoptions[i].u64ptr)) );
          } else {
182 183
            *(cfgoptions[i].i64ptr) = llu;
            printf_params("[LIBCONFIG] %s: %lld\n", cfgpath,(long long)(*(cfgoptions[i].i64ptr)) );
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
          }
        } else {
          defval=config_setdefault_int64(&(cfgoptions[i]),prefix);
        }

        break;

      case TYPE_UINTARRAY:
      case TYPE_INTARRAY:
        setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );

        if ( setting != NULL) {
          read_intarray(&cfgoptions[i],setting,cfgpath);
        } else {
          defval=config_setdefault_intlist(&(cfgoptions[i]),prefix);
        }

        break;

      case TYPE_DOUBLE:
        if ( config_lookup_float(&(libconfig_privdata.cfg),cfgpath, &dbl)) {
          config_check_valptr(&(cfgoptions[i]), (char **)&(cfgoptions[i].dblptr),sizeof(double));
          *(cfgoptions[i].dblptr) = dbl;
          printf_params("[LIBCONFIG] %s: %lf\n", cfgpath,*(cfgoptions[i].dblptr) );
        } else {
          defval=config_setdefault_double(&(cfgoptions[i]),prefix);
        }

        break;

      case TYPE_IPV4ADDR:
        if ( !config_lookup_string(&(libconfig_privdata.cfg),cfgpath, (const char **)&str)) {
          defval=config_setdefault_ipv4addr(&(cfgoptions[i]),prefix);
        } else {
          rst=config_assign_ipv4addr(cfgoptions, str);

          if (rst < 0) {
            fprintf(stderr,"[LIBCONFIG] %s not valid for %s \n", str, cfgpath);
222
            fatalerror=1;
223 224 225 226 227 228 229 230 231 232
          }
        }

        break;

      case TYPE_LIST:
        setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );

        if ( setting) {
          cfgoptions[i].numelt=config_setting_length(setting);
233
        } else {
234
          notfound=1;
235
        }
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

        break;

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

    if( notfound == 1) {
      printf("[LIBCONFIG] %s not found in %s ", cfgpath,libconfig_privdata.configfile );

      if ( (cfgoptions[i].paramflags & PARAMFLAG_MANDATORY) != 0) {
        fatalerror=1;
        printf("  mandatory parameter missing\n");
      } else {
        printf("\n");
      }
254
    } else {
255
      if (defval == 1) {
256 257
        numdefvals++;
        cfgoptions[i].paramflags = cfgoptions[i].paramflags |  PARAMFLAG_PARAMSETDEF;
258
      } else {
259
        cfgoptions[i].paramflags = cfgoptions[i].paramflags |  PARAMFLAG_PARAMSET;
260
      }
261

262 263 264
      status++;
    }
  } /* for loop on options */
265

266
  printf("[LIBCONFIG] %s: %i/%i parameters successfully set, (%i to default value)\n",
267
         ((prefix == NULL)?"(root)":prefix),
268
         status,numoptions,numdefvals );
269

270
  if (fatalerror == 1) {
271 272 273
    fprintf(stderr,"[LIBCONFIG] fatal errors found when processing  %s \n", libconfig_privdata.configfile );
    config_libconfig_end();
    end_configmodule();
274
  }
275

276 277 278
  return status;
}

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
int config_libconfig_getlist(paramlist_def_t *ParamList,
                             paramdef_t *params, int numparams, char *prefix) {
  config_setting_t *setting;
  int i,j,status;
  char *listpath=NULL;
  char cfgpath[MAX_OPTNAME_SIZE*2 + 6]; /* prefix.listname.[listindex] */

  if (prefix != NULL) {
    i=asprintf(&listpath ,"%s.%s",prefix,ParamList->listname);
  } else {
    i=asprintf(&listpath ,"%s",ParamList->listname);
  }

  setting = config_lookup(&(libconfig_privdata.cfg), listpath);

  if ( setting) {
    status = ParamList->numelt = config_setting_length(setting);
    printf_params("[LIBCONFIG] %i %s in config file %s \n",
                  ParamList->numelt,listpath,libconfig_privdata.configfile );
  } else {
    printf("[LIBCONFIG] list %s not found in config file %s \n",
           listpath,libconfig_privdata.configfile );
    ParamList->numelt= 0;
    status = -1;
  }

  if (ParamList->numelt > 0 && params != NULL) {
    ParamList->paramarray = malloc(ParamList->numelt * sizeof(paramdef_t *));

    if ( ParamList->paramarray != NULL) {
      config_get_if()->ptrs[config_get_if()->numptrs] = (char *)(ParamList->paramarray);
      config_get_if()->numptrs++;
311
    } else {
312 313
      fprintf (stderr,"[LIBCONFIG] %s %d malloc error\n",__FILE__, __LINE__);
      exit(-1);
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

    for (i=0 ; i < ParamList->numelt ; i++) {
      ParamList->paramarray[i] = malloc(numparams * sizeof(paramdef_t));

      if ( ParamList->paramarray[i] != NULL) {
        config_get_if()->ptrs[config_get_if()->numptrs] = (char *)(ParamList->paramarray[i]);
        config_get_if()->numptrs++;
      } else {
        fprintf (stderr,"[LIBCONFIG] %s %d malloc error\n",__FILE__, __LINE__);
        exit(-1);
      }

      memcpy(ParamList->paramarray[i], params, sizeof(paramdef_t)*numparams);

      for (j=0; j<numparams; j++) {
        ParamList->paramarray[i][j].strptr = NULL ;
      }

      sprintf(cfgpath,"%s.[%i]",listpath,i);
      config_libconfig_get(ParamList->paramarray[i], numparams, cfgpath );
    } /* for i... */
  } /* ParamList->numelt > 0 && params != NULL */

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

  return status;
342 343
}

344
int config_libconfig_init(char *cfgP[], int numP) {
345 346 347 348 349 350 351
  config_init(&(libconfig_privdata.cfg));
  libconfig_privdata.configfile = strdup((char *)cfgP[0]);
  config_get_if()->numptrs=0;
  memset(config_get_if()->ptrs,0,sizeof(void *) * CONFIG_MAX_ALLOCATEDPTRS);

  /* Read the file. If there is an error, report it and exit. */
  if(! config_read_file(&(libconfig_privdata.cfg), libconfig_privdata.configfile)) {
352
    fprintf(stderr,"[LIBCONFIG] %s %d file %s - %d - %s\n",__FILE__, __LINE__,
353 354
            libconfig_privdata.configfile, config_error_line(&(libconfig_privdata.cfg)),
            config_error_text(&(libconfig_privdata.cfg)));
355 356 357 358 359 360 361 362 363
    config_destroy(&(libconfig_privdata.cfg));
    printf( "\n");
    return -1;
  }

  return 0;
}


364
void config_libconfig_end(void ) {
365
  config_destroy(&(libconfig_privdata.cfg));
366

367
  if ( libconfig_privdata.configfile != NULL ) {
368 369 370
    free(libconfig_privdata.configfile);
    libconfig_privdata.configfile=NULL;
  }
371
}