make multiple config instead of one implicit global

This commit is contained in:
Laurent THOMAS
2023-11-10 10:23:11 +01:00
parent 82597e7e8f
commit f50d5f5a9b
74 changed files with 1316 additions and 1118 deletions
@@ -11,7 +11,7 @@ paramdef_t someoptions[] = {
{"opt1", "<help opt1>", 0, uptr:&varopt1, defuintval:0, TYPE_UINT, 0 },
};
config_get( someoptions,sizeof(someoptions)/sizeof(paramdef_t),"somesection");
config_get( someoptions,sizeofArray(someoptions),"somesection");
```
new code:
@@ -31,7 +31,7 @@ paramdef_t someoptions[] = {
{"opt2", "<help opt2>", 0, strptr:&varopt2,defstrval:"",TYPE_STRING,0 },
};
config_get( someoptions,sizeof(someoptions)/sizeof(paramdef_t),"somesection");
config_get( someoptions,sizeofArray(someoptions),"somesection");
```
@@ -105,7 +105,7 @@ character string, used as a prefix for the section name. It has to be specified
list to be read is under another section.
*/
config_getlist( &NbIoT_MacRLC_ParamList,NbIoT_MacRLC_Params,
sizeof(NbIoT_MacRLC_Params)/sizeof(paramdef_t),
sizeofArray(NbIoT_MacRLC_Params),
NULL);
/*
+44 -36
View File
@@ -37,9 +37,11 @@
#include <errno.h>
#include <platform_types.h>
#include "config_userapi.h"
#include "config_common.h"
#include "../utils/LOG/log.h"
int parse_stringlist(paramdef_t *cfgoptions, char *val) {
static int parse_stringlist(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *val)
{
char *atoken;
char *tokctx = NULL;
char *tmpval=strdup(val);
@@ -57,21 +59,22 @@ int parse_stringlist(paramdef_t *cfgoptions, char *val) {
AssertFatal(MAX_LIST_SIZE > numelt,
"This piece of code use fixed size arry of constant #define MAX_LIST_SIZE %d\n",
MAX_LIST_SIZE );
config_check_valptr(cfgoptions, sizeof(char*), numelt);
config_check_valptr(cfg, cfgoptions, sizeof(char *), numelt);
cfgoptions->numelt=numelt;
atoken=strtok_r(val, ",",&tokctx);
for( int i=0; i<cfgoptions->numelt && atoken != NULL ; i++) {
snprintf(cfgoptions->strlistptr[i],DEFAULT_EXTRA_SZ,"%s",atoken);
printf_params("[LIBCONFIG] %s[%i]: %s\n", cfgoptions->optname,i,cfgoptions->strlistptr[i]);
printf_params(cfg, "[LIBCONFIG] %s[%i]: %s\n", cfgoptions->optname, i, cfgoptions->strlistptr[i]);
atoken=strtok_r(NULL, ",",&tokctx);
}
return (cfgoptions->numelt > 0);
}
int processoption(paramdef_t *cfgoptions, char *value) {
static int processoption(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *value)
{
char *tmpval = value;
int optisset=0;
char defbool[2]="1";
@@ -89,19 +92,18 @@ int processoption(paramdef_t *cfgoptions, char *value) {
case TYPE_STRING:
if (cfgoptions->numelt == 0 )
config_check_valptr(cfgoptions, 1,
strlen(tmpval)+1);
config_check_valptr(cfg, cfgoptions, 1, strlen(tmpval) + 1);
if (cfgoptions->numelt < (strlen(tmpval)+1)) {
CONFIG_PRINTF_ERROR("[CONFIG] command line option %s value too long\n",
cfgoptions->optname);
}
snprintf(*cfgoptions->strptr, cfgoptions->numelt ,"%s",tmpval);
printf_cmdl("[CONFIG] %s set to %s from command line\n", cfgoptions->optname, tmpval);
printf_cmdl(cfg, "[CONFIG] %s set to %s from command line\n", cfgoptions->optname, tmpval);
optisset=1;
break;
case TYPE_STRINGLIST:
optisset=parse_stringlist(cfgoptions,tmpval);
optisset = parse_stringlist(cfg, cfgoptions, tmpval);
break;
case TYPE_UINT32:
@@ -110,8 +112,8 @@ int processoption(paramdef_t *cfgoptions, char *value) {
case TYPE_INT16:
case TYPE_UINT8:
case TYPE_INT8:
config_check_valptr(cfgoptions, sizeof(*cfgoptions->iptr), 1);
config_assign_int(cfgoptions,cfgoptions->optname,(int32_t)strtol(tmpval,&charptr,0));
config_check_valptr(cfg, cfgoptions, sizeof(*cfgoptions->iptr), 1);
config_assign_int(cfg, cfgoptions, cfgoptions->optname, (int32_t)strtol(tmpval, &charptr, 0));
if( *charptr != 0) {
CONFIG_PRINTF_ERROR("[CONFIG] command line, option %s requires an integer argument\n",cfgoptions->optname);
@@ -122,14 +124,14 @@ int processoption(paramdef_t *cfgoptions, char *value) {
case TYPE_UINT64:
case TYPE_INT64:
config_check_valptr(cfgoptions, sizeof(*cfgoptions->i64ptr), 1);
config_check_valptr(cfg, cfgoptions, sizeof(*cfgoptions->i64ptr), 1);
*(cfgoptions->i64ptr)=strtoll(tmpval,&charptr,0);
if( *charptr != 0) {
CONFIG_PRINTF_ERROR("[CONFIG] command line, option %s requires an integer argument\n",cfgoptions->optname);
}
printf_cmdl("[CONFIG] %s set to %lli from command line\n", cfgoptions->optname, (long long)*cfgoptions->i64ptr);
printf_cmdl(cfg, "[CONFIG] %s set to %lli from command line\n", cfgoptions->optname, (long long)*cfgoptions->i64ptr);
optisset=1;
break;
@@ -138,14 +140,14 @@ int processoption(paramdef_t *cfgoptions, char *value) {
break;
case TYPE_DOUBLE:
config_check_valptr(cfgoptions, sizeof(*cfgoptions->dblptr), 1);
config_check_valptr(cfg, cfgoptions, sizeof(*cfgoptions->dblptr), 1);
*cfgoptions->dblptr = strtof(tmpval,&charptr);
if( *charptr != 0) {
CONFIG_PRINTF_ERROR("[CONFIG] command line, option %s requires a double argument\n",cfgoptions->optname);
}
printf_cmdl("[CONFIG] %s set to %lf from command line\n", cfgoptions->optname, *(cfgoptions->dblptr));
printf_cmdl(cfg, "[CONFIG] %s set to %lf from command line\n", cfgoptions->optname, *(cfgoptions->dblptr));
optisset=1;
break;
@@ -167,7 +169,8 @@ int processoption(paramdef_t *cfgoptions, char *value) {
/*--------------------------------------------------------------------*/
/* check unknown options in the command line
*/
int config_check_unknown_cmdlineopt(char *prefix) {
int config_check_unknown_cmdlineopt(configmodule_interface_t *cfg, char *prefix)
{
int unknowndetected=0;
char testprefix[CONFIG_MAXOPTLENGTH];
int finalcheck = 0;
@@ -182,18 +185,21 @@ int config_check_unknown_cmdlineopt(char *prefix) {
}
}
for (int i=1; i<config_get_if()->argc ; i++) {
if ( !finalcheck && strstr(config_get_if()->argv[i],testprefix) == NULL ) continue;
for (int i = 1; i < cfg->argc; i++) {
if (!finalcheck && strstr(cfg->argv[i], testprefix) == NULL)
continue;
if ( !finalcheck && testprefix[0]==0 && index(config_get_if()->argv[i],'.') != NULL) continue;
if (!finalcheck && testprefix[0] == 0 && index(cfg->argv[i], '.') != NULL)
continue;
if ( !finalcheck && isdigit(config_get_if()->argv[i][0])) continue;
if (!finalcheck && isdigit(cfg->argv[i][0]))
continue;
if ( !finalcheck && config_get_if()->argv[i][0] == '-' && isdigit(config_get_if()->argv[i][1])) continue;
if (!finalcheck && cfg->argv[i][0] == '-' && isdigit(cfg->argv[i][1]))
continue;
if ( (config_get_if()->argv_info[i] & CONFIG_CMDLINEOPT_PROCESSED) == 0 ) {
fprintf(stderr,"[CONFIG] unknown option: %s\n",
config_get_if()->argv[i] );
if ((cfg->argv_info[i] & CONFIG_CMDLINEOPT_PROCESSED) == 0) {
fprintf(stderr, "[CONFIG] unknown option: %s\n", cfg->argv[i]);
unknowndetected++;
}
}
@@ -204,10 +210,11 @@ int config_check_unknown_cmdlineopt(char *prefix) {
}
return unknowndetected;
} /* config_check_unknown_cmdlineopt */
} /* config_check_unknown_cmdlineopt */
int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix) {
int c = config_get_if()->argc;
int config_process_cmdline(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int numoptions, char *prefix)
{
int c = cfg->argc;
int i,j;
char *pp;
char cfgpath[CONFIG_MAXOPTLENGTH];
@@ -215,15 +222,16 @@ int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
i = 0;
while (c > 0 ) {
char *oneargv = strdup(config_get_if()->argv[i]); /* we use strtok_r which modifies its string paramater, and we don't want argv to be modified */
char *oneargv =
strdup(cfg->argv[i]); /* we use strtok_r which modifies its string paramater, and we don't want argv to be modified */
if(!oneargv) abort();
/* first check help options, either --help, -h or --help_<section> */
if (strncmp(oneargv, "-h",2) == 0 || strncmp(oneargv, "--help",6) == 0 ) {
char *tokctx = NULL;
pp=strtok_r(oneargv, "_",&tokctx);
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
cfg->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
if (pp == NULL || strcasecmp(pp,config_get_if()->argv[i] ) == 0 ) {
if (pp == NULL || strcasecmp(pp, cfg->argv[i]) == 0) {
if( prefix == NULL) {
config_printhelp(cfgoptions,numoptions,"(root section)");
@@ -263,10 +271,10 @@ int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
((strlen(oneargv) > 2) && (strcmp(oneargv + 2, cfgoptions[n].optname) == 0 ) && (cfgoptions[n].paramflags & PARAMFLAG_CMDLINE_NOPREFIXENABLED )) ) {
char *valptr=NULL;
int ret;
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
cfg->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
if (c > 0) {
pp = config_get_if()->argv[i+1];
pp = cfg->argv[i + 1];
if (pp != NULL ) {
ret = strlen(pp);
@@ -280,11 +288,11 @@ int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
}
}
j += processoption(&(cfgoptions[n]), valptr);
j += processoption(cfg, &(cfgoptions[n]), valptr);
if ( valptr != NULL ) {
i++;
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
cfg->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
c--;
}
@@ -298,6 +306,6 @@ int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
c--;
} /* fin du while */
printf_cmdl("[CONFIG] %s %i options set from command line\n",((prefix == NULL) ? "(root)":prefix),j);
printf_cmdl(cfg, "[CONFIG] %s %i options set from command line\n", ((prefix == NULL) ? "(root)" : prefix), j);
return j;
} /* parse_cmdline*/
} /* parse_cmdline*/
+313
View File
@@ -0,0 +1,313 @@
/*
* 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
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <dlfcn.h>
#include <arpa/inet.h>
#include <platform_types.h>
#include "config_common.h"
#include "../utils/LOG/log.h"
static int managed_ptr_sz(configmodule_interface_t *cfg, void *ptr)
{
AssertFatal(cfg->numptrs < CONFIG_MAX_ALLOCATEDPTRS,
"This code use fixed size array as #define CONFIG_MAX_ALLOCATEDPTRS %d\n",
CONFIG_MAX_ALLOCATEDPTRS);
int i;
pthread_mutex_lock(&cfg->memBlocks_mutex);
int numptrs=cfg->numptrs;
for (i=0; i<numptrs; i++ )
if (cfg->oneBlock[i].ptrs == ptr)
break;
pthread_mutex_unlock(&cfg->memBlocks_mutex);
if ( i == numptrs )
return -1;
else
return cfg->oneBlock[i].sz;
}
void config_check_valptr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int elt_sz, int nb_elt)
{
const bool toFree=!(cfgoptions->paramflags & PARAMFLAG_NOFREE);
// let's see if the value has been read
// the datamodel is more difficult than it should be
// and it is not thread safe
if (cfgoptions->voidptr) {
int sz;
if ( cfgoptions-> type == TYPE_STRING)
sz = managed_ptr_sz(cfg, *cfgoptions->strptr);
else
sz = managed_ptr_sz(cfg, cfgoptions->voidptr);
if ( sz != -1 ) // the same variable pointer has been used!
cfgoptions->numelt=sz;
}
if (cfgoptions->voidptr != NULL
&& (cfgoptions->type == TYPE_INTARRAY
|| cfgoptions->type == TYPE_UINTARRAY
|| cfgoptions->type == TYPE_STRINGLIST )) {
int sz;
if (cfgoptions->type == TYPE_STRING || cfgoptions->type == TYPE_STRINGLIST)
sz = managed_ptr_sz(cfg, *cfgoptions->strptr);
else
sz = managed_ptr_sz(cfg, cfgoptions->voidptr);
if ( sz == -1) {
fprintf(stderr,"[CONFIG] %s NOT SUPPORTED not NULL pointer with array types", cfgoptions->optname);
return ;
}
}
if (cfgoptions->voidptr == NULL ) {
if (cfgoptions->type == TYPE_STRING) {
// difficult datamodel
cfgoptions->strptr = config_allocate_new(cfg, sizeof(*cfgoptions->strptr), toFree);
} else if ( cfgoptions->type == TYPE_STRINGLIST) {
AssertFatal(nb_elt<MAX_LIST_SIZE,
"This piece of code use fixed size arry of constant #define MAX_LIST_SIZE %d\n",
MAX_LIST_SIZE );
cfgoptions->strlistptr = config_allocate_new(cfg, sizeof(char *) * MAX_LIST_SIZE, toFree);
for (int i=0; i<MAX_LIST_SIZE; i++)
cfgoptions->strlistptr[i] = config_allocate_new(cfg, DEFAULT_EXTRA_SZ, toFree);
} else {
if ( cfgoptions->type == TYPE_INTARRAY || cfgoptions->type == TYPE_UINTARRAY )
nb_elt=max(nb_elt, MAX_LIST_SIZE); // make room if the list is larger than the default one
cfgoptions->voidptr = config_allocate_new(cfg, elt_sz * nb_elt, toFree);
}
}
if (cfgoptions->type == TYPE_STRING && *cfgoptions->strptr == NULL) {
*cfgoptions->strptr = config_allocate_new(cfg, nb_elt + DEFAULT_EXTRA_SZ, toFree);
cfgoptions->numelt=nb_elt+DEFAULT_EXTRA_SZ;
}
}
void *config_allocate_new(configmodule_interface_t *cfg, int sz, bool autoFree)
{
void *ptr = calloc(sz, 1);
AssertFatal(ptr, "calloc fails\n");
// add the memory piece in the managed memory pieces list
pthread_mutex_lock(&cfg->memBlocks_mutex);
int newBlockIdx=cfg->numptrs++;
oneBlock_t* tmp=&cfg->oneBlock[newBlockIdx];
tmp->ptrs = (char *)ptr;
tmp->ptrsAllocated = true;
tmp->sz=sz;
tmp->toFree=autoFree;
pthread_mutex_unlock(&cfg->memBlocks_mutex);
return ptr;
}
int config_setdefault_int(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix)
{
int status = 0;
config_check_valptr(cfg, cfgoptions, sizeof(*cfgoptions->iptr), 1);
if (((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
config_assign_int(cfg, cfgoptions, cfgoptions->optname, cfgoptions->defintval);
status=1;
printf_params(cfg, "[CONFIG] %s.%s set to default value\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname);
}
return status;
}
int config_setdefault_int64(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix)
{
int status = 0;
config_check_valptr(cfg, cfgoptions, sizeof(*cfgoptions->i64ptr), 1);
if (((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
*(cfgoptions->u64ptr) = cfgoptions->defuintval;
status=1;
printf_params(cfg,
"[CONFIG] %s.%s set to default value %llu\n",
((prefix == NULL) ? "" : prefix),
cfgoptions->optname,
(long long unsigned)(*(cfgoptions->u64ptr)));
}
return status;
}
int config_setdefault_intlist(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix)
{
int status = 0;
if (cfgoptions->defintarrayval != NULL) {
config_check_valptr(cfg, cfgoptions, sizeof(cfgoptions->iptr), cfgoptions->numelt);
cfgoptions->iptr = cfgoptions->defintarrayval;
status=1;
for (int j = 0; j < cfgoptions->numelt; j++) {
printf_params(cfg, "[CONFIG] %s[%i] set to default value %i\n", cfgoptions->optname, j, (int)cfgoptions->iptr[j]);
}
}
return status;
}
int config_setdefault_string(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix)
{
int status = 0;
if (cfgoptions->defstrval != NULL) {
status=1;
if (cfgoptions->numelt == 0)
config_check_valptr(cfg, cfgoptions, 1, strlen(cfgoptions->defstrval) + 1);
if (cfgoptions->numelt < strlen(cfgoptions->defstrval) + 1) {
fprintf(stderr,"[CONFIG] %s size too small\n", cfgoptions->optname);
}
snprintf(*cfgoptions->strptr, cfgoptions->numelt, "%s", cfgoptions->defstrval);
printf_params(cfg,
"[CONFIG] %s.%s set to default value \"%s\"\n",
((prefix == NULL) ? "" : prefix),
cfgoptions->optname,
*cfgoptions->strptr);
}
return status;
}
int config_setdefault_stringlist(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix)
{
int status = 0;
if (cfgoptions->defstrlistval != NULL) {
cfgoptions->strlistptr = cfgoptions->defstrlistval;
status=1;
for (int j = 0; j < cfgoptions->numelt; j++)
printf_params(cfg,
"[CONFIG] %s.%s[%i] set to default value %s\n",
((prefix == NULL) ? "" : prefix),
cfgoptions->optname,
j,
cfgoptions->strlistptr[j]);
}
return status;
}
int config_setdefault_double(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix)
{
int status = 0;
config_check_valptr(cfg, cfgoptions, sizeof(*cfgoptions->dblptr), 1);
if( ((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
*(cfgoptions->dblptr)=cfgoptions->defdblval;
status=1;
printf_params(cfg, "[CONFIG] %s set to default value %lf\n", cfgoptions->optname, *(cfgoptions->dblptr));
}
return status;
}
int config_assign_ipv4addr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *ipv4addr)
{
config_check_valptr(cfg, cfgoptions, sizeof(*cfgoptions->uptr), 1);
int rst=inet_pton(AF_INET, ipv4addr,cfgoptions->uptr );
if (rst == 1 && *(cfgoptions->uptr) > 0) {
printf_params(cfg, "[CONFIG] %s: %s\n", cfgoptions->optname, ipv4addr);
return 1;
} else {
if ( strncmp(ipv4addr,ANY_IPV4ADDR_STRING,sizeof(ANY_IPV4ADDR_STRING)) == 0) {
printf_params(cfg, "[CONFIG] %s:%s (INADDR_ANY) \n", cfgoptions->optname, ipv4addr);
*cfgoptions->uptr=INADDR_ANY;
return 1;
} else {
fprintf(stderr,"[CONFIG] %s not valid for %s \n", ipv4addr, cfgoptions->optname);
return -1;
}
}
return 0;
}
int config_setdefault_ipv4addr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix)
{
int status = 0;
if (cfgoptions->defstrval != NULL) {
status = config_assign_ipv4addr(cfg, cfgoptions, cfgoptions->defstrval);
}
return status;
}
void config_assign_int(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *fullname, int val)
{
int tmpval=val;
if ( ((cfgoptions->paramflags &PARAMFLAG_BOOL) != 0) && tmpval >0) {
tmpval =1;
}
switch (cfgoptions->type) {
case TYPE_UINT8:
*(cfgoptions->u8ptr) = (uint8_t)tmpval;
printf_params(cfg, "[CONFIG] %s: %u\n", fullname, (uint8_t)tmpval);
break;
case TYPE_INT8:
*(cfgoptions->i8ptr) = (int8_t)tmpval;
printf_params(cfg, "[CONFIG] %s: %i\n", fullname, (int8_t)tmpval);
break;
case TYPE_UINT16:
*(cfgoptions->u16ptr) = (uint16_t)tmpval;
printf_params(cfg, "[CONFIG] %s: %hu\n", fullname, (uint16_t)tmpval);
break;
case TYPE_INT16:
*(cfgoptions->i16ptr) = (int16_t)tmpval;
printf_params(cfg, "[CONFIG] %s: %hi\n", fullname, (int16_t)tmpval);
break;
case TYPE_UINT32:
*(cfgoptions->uptr) = (uint32_t)tmpval;
printf_params(cfg, "[CONFIG] %s: %u\n", fullname, (uint32_t)tmpval);
break;
case TYPE_MASK:
*(cfgoptions->uptr) = *(cfgoptions->uptr) | (uint32_t)tmpval;
printf_params(cfg, "[CONFIG] %s: 0x%08x\n", fullname, (uint32_t)tmpval);
break;
case TYPE_INT32:
*(cfgoptions->iptr) = (int32_t)tmpval;
printf_params(cfg, "[CONFIG] %s: %i\n", fullname, (int32_t)tmpval);
break;
default:
fprintf (stderr,"[CONFIG] %s %i type %i non integer parameter %s not assigned\n",__FILE__, __LINE__,cfgoptions->type,fullname);
break;
}
}
+58
View File
@@ -0,0 +1,58 @@
/*
* 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_userapi.h
* \brief: configuration module, include file to be used by the source code calling the
* configuration module to access configuration parameters
* \author Francois TABURET
* \date 2017
* \version 0.1
* \company NOKIA BellLabs France
* \email: francois.taburet@nokia-bell-labs.com
* \note
* \warning
*/
#ifndef INCLUDE_CONFIG_COMMON_H
#define INCLUDE_CONFIG_COMMON_H
#include "config_load_configmodule.h"
#include "common/utils/utils.h"
#ifdef __cplusplus
extern "C"
{
#endif
void config_check_valptr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int elt_sz, int nb_elt);
/* functions to set a parameter to its default value */
int config_setdefault_int(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix);
int config_setdefault_int64(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix);
int config_setdefault_intlist(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix);
int config_setdefault_string(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix);
int config_setdefault_stringlist(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix);
int config_setdefault_double(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix);
int config_setdefault_ipv4addr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *prefix);
void *config_allocate_new(configmodule_interface_t *cfg, int sz, bool autoFree);
void config_assign_int(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *fullname, int val);
#ifdef __cplusplus
}
#endif
#endif
+59 -36
View File
@@ -44,6 +44,25 @@
#include "../utils/LOG/log.h"
#define CONFIG_SHAREDLIBFORMAT "libparams_%s.so"
#include "nfapi/oai_integration/vendor_ext.h"
#include "config_common.h"
// clang-format off
static char config_helpstr [] = "\n lte-softmodem -O [config mode]<:dbgl[debugflags]><:incp[path]>\n \
debugflags can also be defined in the config section of the config file\n \
debugflags: mask, 1->print parameters, 2->print memory allocations debug messages\n \
4->print command line processing debug messages\n \
incp parameter can be used to define the include path used for config files (@include directive)\n \
defaults is set to the path of the main config file.\n";
static paramdef_t Config_Params[] = {
/*--------------------------------------------------------------------------------------------------------------------------*/
/* config parameters for config module */
/* optname helpstr paramflags XXXptr defXXXval type numelt */
/*--------------------------------------------------------------------------------------------------------------------------*/
{CONFIGP_DEBUGFLAGS, config_helpstr, 0, .uptr = NULL, .defintval = 0, TYPE_MASK, 0},
{CONFIGP_TMPDIR, CONFIG_HELP_TMPDIR, PARAMFLAG_NOFREE, .strptr = NULL, .defstrval = "/tmp", TYPE_STRING, 0},
};
// clang-format on
int load_config_sharedlib(configmodule_interface_t *cfgptr) {
void *lib_handle;
@@ -52,7 +71,7 @@ int load_config_sharedlib(configmodule_interface_t *cfgptr) {
int st;
st=0;
sprintf(libname,CONFIG_SHAREDLIBFORMAT,cfgptr->cfgmode);
lib_handle = dlopen(libname,RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
lib_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL | RTLD_NODELETE);
if (!lib_handle) {
fprintf(stderr,"[CONFIG] %s %d Error calling dlopen(%s): %s\n",__FILE__, __LINE__, libname,dlerror());
@@ -65,7 +84,7 @@ int load_config_sharedlib(configmodule_interface_t *cfgptr) {
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);
st = cfgptr->init(cfgptr);
printf("[CONFIG] function %s returned %i\n",
fname, st);
}
@@ -121,14 +140,18 @@ int nooptfunc(void) {
return 0;
};
int config_cmdlineonly_getlist(paramlist_def_t *ParamList,
paramdef_t *params, int numparams, char *prefix) {
int config_cmdlineonly_getlist(configmodule_interface_t *cfg,
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 config_cmdlineonly_get(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int numoptions, char *prefix)
{
int defval;
int fatalerror=0;
int numdefvals=0;
@@ -138,11 +161,11 @@ int config_cmdlineonly_get(paramdef_t *cfgoptions,int numoptions, char *prefix )
switch(cfgoptions[i].type) {
case TYPE_STRING:
defval=config_setdefault_string(&(cfgoptions[i]), prefix);
defval = config_setdefault_string(cfg, &cfgoptions[i], prefix);
break;
case TYPE_STRINGLIST:
defval=config_setdefault_stringlist(&(cfgoptions[i]), prefix);
defval = config_setdefault_stringlist(cfg, &cfgoptions[i], prefix);
break;
case TYPE_UINT8:
@@ -152,25 +175,25 @@ int config_cmdlineonly_get(paramdef_t *cfgoptions,int numoptions, char *prefix )
case TYPE_UINT32:
case TYPE_INT32:
case TYPE_MASK:
defval=config_setdefault_int(&(cfgoptions[i]), prefix);
defval = config_setdefault_int(cfg, &cfgoptions[i], prefix);
break;
case TYPE_UINT64:
case TYPE_INT64:
defval=config_setdefault_int64(&(cfgoptions[i]), prefix);
defval = config_setdefault_int64(cfg, &cfgoptions[i], prefix);
break;
case TYPE_UINTARRAY:
case TYPE_INTARRAY:
defval=config_setdefault_intlist(&(cfgoptions[i]), prefix);
defval = config_setdefault_intlist(cfg, &cfgoptions[i], prefix);
break;
case TYPE_DOUBLE:
defval=config_setdefault_double(&(cfgoptions[i]), prefix);
defval = config_setdefault_double(cfg, &cfgoptions[i], prefix);
break;
case TYPE_IPV4ADDR:
defval=config_setdefault_ipv4addr(&(cfgoptions[i]), prefix);
defval = config_setdefault_ipv4addr(cfg, &cfgoptions[i], prefix);
break;
default:
@@ -231,7 +254,7 @@ configmodule_interface_t *load_configmodule(int argc,
OWoptIdx = i;
}
if ( strstr(argv[i], "help_config") != NULL ) {
config_printhelp(Config_Params,CONFIG_PARAMLENGTH(Config_Params),CONFIG_SECTIONNAME);
config_printhelp(Config_Params, sizeofArray(Config_Params), CONFIG_SECTIONNAME);
exit(0);
}
@@ -271,8 +294,12 @@ configmodule_interface_t *load_configmodule(int argc,
modeparams=cfgmode;
cfgmode=strdup(CONFIG_LIBCONFIGFILE);
}
if (cfgptr == NULL) {
cfgptr = calloc(sizeof(configmodule_interface_t),1);
static configmodule_interface_t *cfgptr;
if (cfgptr)
printf("ERROR: Call load_configmodule more than one time\n");
// The macros are not thread safe print_params and similar
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);
@@ -314,11 +341,10 @@ configmodule_interface_t *load_configmodule(int argc,
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]);
}
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));
@@ -328,11 +354,11 @@ configmodule_interface_t *load_configmodule(int argc,
if (i == 0) {
printf("[CONFIG] config module %s loaded\n",cfgmode);
int idx = config_paramidx_fromname(Config_Params, CONFIG_PARAMLENGTH(Config_Params), CONFIGP_DEBUGFLAGS);
int idx = config_paramidx_fromname(Config_Params, sizeofArray(Config_Params), CONFIGP_DEBUGFLAGS);
Config_Params[idx].uptr = &(cfgptr->rtflags);
idx = config_paramidx_fromname(Config_Params, CONFIG_PARAMLENGTH(Config_Params), CONFIGP_TMPDIR);
idx = config_paramidx_fromname(Config_Params, sizeofArray(Config_Params), CONFIGP_TMPDIR);
Config_Params[idx].strptr = &(cfgptr->tmpdir);
config_get(Config_Params,CONFIG_PARAMLENGTH(Config_Params), CONFIG_SECTIONNAME );
config_get(cfgptr, Config_Params, sizeofArray(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;
@@ -351,7 +377,7 @@ configmodule_interface_t *load_configmodule(int argc,
if (cfgmode != NULL) free(cfgmode);
if (CONFIG_ISFLAGSET(CONFIG_ABORT)) {
config_printhelp(Config_Params,CONFIG_PARAMLENGTH(Config_Params),CONFIG_SECTIONNAME );
config_printhelp(Config_Params, sizeofArray(Config_Params), CONFIG_SECTIONNAME);
// exit(-1);
}
@@ -359,10 +385,11 @@ configmodule_interface_t *load_configmodule(int argc,
}
/* Possibly write config file, with parameters which have been read and after command line parsing */
void write_parsedcfg(void)
void write_parsedcfg(configmodule_interface_t *cfgptr)
{
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",
printf_params(cfgptr,
"[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,
@@ -371,19 +398,20 @@ void write_parsedcfg(void)
if (cfgptr != NULL) {
if (cfgptr->write_parsedcfg != NULL) {
printf("[CONFIG] calling config module write_parsedcfg function...\n");
cfgptr->write_parsedcfg();
cfgptr->write_parsedcfg(cfgptr);
}
}
}
/* free memory allocated when reading parameters */
/* config module could be initialized again after this call */
void end_configmodule(void) {
write_parsedcfg();
void end_configmodule(configmodule_interface_t *cfgptr)
{
write_parsedcfg(cfgptr);
if (cfgptr != NULL) {
if (cfgptr->end != NULL) {
printf ("[CONFIG] calling config module end function...\n");
cfgptr->end();
cfgptr->end(cfgptr);
}
pthread_mutex_lock(&cfgptr->memBlocks_mutex);
@@ -408,8 +436,3 @@ void end_configmodule(void) {
cfgptr=NULL;
}
}
+18 -33
View File
@@ -59,11 +59,14 @@
#define CONFIG_HELP (1<<20) // print help message
#define CONFIG_ABORT (1<<21) // config failed,abort execution
#define CONFIG_NOOOPT (1<<22) // no -O option found when parsing command line
typedef int(*configmodule_initfunc_t)(char *cfgP[],int numP);
typedef int(*configmodule_getfunc_t)(paramdef_t *,int numparams, char *prefix);
typedef int(*configmodule_getlistfunc_t)(paramlist_def_t *, paramdef_t *,int numparams, char *prefix);
struct configmodule_interface;
typedef int (*configmodule_getfunc_t)(struct configmodule_interface *, paramdef_t *, int numparams, char *prefix);
typedef int (
*configmodule_getlistfunc_t)(struct configmodule_interface *, paramlist_def_t *, paramdef_t *, int numparams, char *prefix);
typedef int (*configmodule_setfunc_t)(paramdef_t *cfgoptions, int numoptions, char *prefix);
typedef void(*configmodule_endfunc_t)(void);
typedef void (*configmodule_endfunc_t)(struct configmodule_interface *cfg);
typedef int (*configmodule_initfunc_t)(struct configmodule_interface *cfg);
typedef struct configmodule_status {
int num_paramgroups;
@@ -105,41 +108,23 @@ typedef struct configmodule_interface {
configmodule_status_t *status; // allocated in debug mode only
} configmodule_interface_t;
#ifdef CONFIG_LOADCONFIG_MAIN
configmodule_interface_t *cfgptr=NULL;
static char config_helpstr [] = "\n lte-softmodem -O [config mode]<:dbgl[debugflags]><:incp[path]>\n \
debugflags can also be defined in the config section of the config file\n \
debugflags: mask, 1->print parameters, 2->print memory allocations debug messages\n \
4->print command line processing debug messages\n \
incp parameter can be used to define the include path used for config files (@include directive)\n \
defaults is set to the path of the main config file.\n";
#define CONFIG_HELP_TMPDIR "<Repository to create temporary file>"
#define CONFIG_SECTIONNAME "config"
#define CONFIGP_DEBUGFLAGS "debugflags"
#define CONFIGP_TMPDIR "tmpdir"
// clang-format off
static paramdef_t Config_Params[] = {
/*--------------------------------------------------------------------------------------------------------------------------*/
/* config parameters for config module */
/* optname helpstr paramflags XXXptr defXXXval type numelt */
/*--------------------------------------------------------------------------------------------------------------------------*/
{CONFIGP_DEBUGFLAGS, config_helpstr, 0, .uptr = NULL, .defintval = 0, TYPE_MASK, 0},
{CONFIGP_TMPDIR, CONFIG_HELP_TMPDIR, PARAMFLAG_NOFREE, .strptr = NULL, .defstrval = "/tmp", TYPE_STRING, 0},
};
// clang-format on
// extern configmodule_interface_t *cfgptr;
#else
extern configmodule_interface_t *cfgptr;
#endif
#define printf_params(cfg, ...) \
if ((cfg->rtflags & (CONFIG_PRINTPARAMS)) != 0) { \
printf(__VA_ARGS__); \
}
#define printf_params(...) if ( (cfgptr->rtflags & (CONFIG_PRINTPARAMS)) != 0 ) { printf ( __VA_ARGS__ ); }
#define printf_ptrs(...) if ( (cfgptr->rtflags & (CONFIG_DEBUGPTR)) != 0 ) { printf ( __VA_ARGS__ ); }
#define printf_cmdl(...) if ( (cfgptr->rtflags & (CONFIG_DEBUGCMDLINE)) != 0 ) { printf ( __VA_ARGS__ ); }
#define printf_cmdl(cfg, ...) \
if ((cfg->rtflags & (CONFIG_DEBUGCMDLINE)) != 0) { \
printf(__VA_ARGS__); \
}
#define CONFIG_ENABLECMDLINEONLY (1<<1)
extern configmodule_interface_t *load_configmodule(int argc, char **argv, uint32_t initflags);
@@ -148,8 +133,8 @@ extern configmodule_interface_t *load_configmodule(int argc, char **argv, uint32
* should be used as soon as there is no need to read parameters but doesn't prevent
* a new config module init
*/
extern void end_configmodule(void);
extern void write_parsedcfg(void);
void end_configmodule(configmodule_interface_t *cfg);
void write_parsedcfg(configmodule_interface_t *cfg);
/* free all config module memory, to be used at end of program as
* it will free parameters values even those specified with the PARAMFLAG_NOFREE flag */
+12 -11
View File
@@ -33,7 +33,6 @@
#ifndef INCLUDE_CONFIG_PARAMDESC_H
#define INCLUDE_CONFIG_PARAMDESC_H
#define MAX_OPTNAME_SIZE 64
#define CONFIG_MAXOPTLENGTH 512 /* max full option length, full option name exemple: (prefix1.[<index>].prefix2.optname */
@@ -56,42 +55,47 @@
/* checkedparam_t is possibly used in paramdef_t for specific parameter value validation */
#define CONFIG_MAX_NUMCHECKVAL 20
typedef struct paramdef paramdef_t;
typedef struct configmodule_interface configmodule_interface_t;
typedef union checkedparam {
struct {
int (*f1)(paramdef_t *param); /* check an integer against a list of authorized values */
int (*f1)(configmodule_interface_t *cfg, paramdef_t *param); /* check an integer against a list of authorized values */
int okintval[CONFIG_MAX_NUMCHECKVAL]; /* integer array, store possible values */
int num_okintval; /* number of valid values in the checkingval array */
} s1;
struct {
int (*f1a)(paramdef_t *param); /* check an integer against a list of authorized values and set param value */
int (*f1a)(configmodule_interface_t *cfg,
paramdef_t *param); /* check an integer against a list of authorized values and set param value */
/* to the corresponding item in setintval array (mainly for RRC params) */
int okintval[CONFIG_MAX_NUMCHECKVAL]; /* integer array, store possible values in config file */
int setintval[CONFIG_MAX_NUMCHECKVAL]; /* integer array, values set in the paramdef structure */
int num_okintval; /* number of valid values in the checkingval array */
} s1a;
struct {
int (*f2)(paramdef_t *param); /* check an integer against an authorized range, defined by its min and max value */
int (*f2)(configmodule_interface_t *cfg,
paramdef_t *param); /* check an integer against an authorized range, defined by its min and max value */
int okintrange[CONFIG_MAX_NUMCHECKVAL]; /* integer array, store min and max values */
} s2;
struct {
int (*f3)(paramdef_t *param); /* check a string against a list of authorized values */
int (*f3)(configmodule_interface_t *cfg, paramdef_t *param); /* check a string against a list of authorized values */
char *okstrval[CONFIG_MAX_NUMCHECKVAL]; /* string array, store possible values */
int num_okstrval; /* number of valid values in the checkingval array */
} s3;
struct {
int (*f3a)(paramdef_t *param); /* check a string against a list of authorized values and set param value */
int (*f3a)(configmodule_interface_t *cfg,
paramdef_t *param); /* check a string against a list of authorized values and set param value */
/* to the corresponding item in setintval array (mainly for RRC params) */
char *okstrval[CONFIG_MAX_NUMCHECKVAL]; /* string array, store possible values */
int setintval[CONFIG_MAX_NUMCHECKVAL]; /* integer array, values set in the paramdef structure */
int num_okstrval; /* number of valid values in the checkingval array */
} s3a;
struct {
int (*f4)(paramdef_t *param); /* generic check function, no arguments but the param description */
int (*f4)(configmodule_interface_t *cfg,
paramdef_t *param); /* generic check function, no arguments but the param description */
} s4;
struct {
void (*checkfunc)(void) ;
void (*checkfunc)(configmodule_interface_t *cfg);
} s5;
} checkedparam_t;
@@ -157,13 +161,10 @@ typedef struct paramdef {
#define TYPE_LIST 55
#define ANY_IPV4ADDR_STRING "0.0.0.0"
typedef struct paramlist_def {
char listname[MAX_OPTNAME_SIZE];
paramdef_t **paramarray;
int numelt ;
} paramlist_def_t;
/* macro helpers for module users */
#define CONFIG_PARAMLENGTH(A) (sizeof(A)/sizeof(paramdef_t))
#endif /* INCLUDE_CONFIG_PARAMDESC_H */
+39 -299
View File
@@ -41,160 +41,12 @@
#include <arpa/inet.h>
#include <platform_types.h>
#include "config_userapi.h"
#include "config_common.h"
#include "../utils/LOG/log.h"
configmodule_interface_t *config_get_if(void) {
if (cfgptr == NULL) {
if (isLogInitDone())
LOG_W(ENB_APP,"[CONFIG] %s %d config module not initialized\n",__FILE__,__LINE__);
}
return cfgptr;
}
static int managed_ptr_sz(void* ptr) {
configmodule_interface_t * cfg=config_get_if();
AssertFatal(cfg->numptrs < CONFIG_MAX_ALLOCATEDPTRS,
"This code use fixed size array as #define CONFIG_MAX_ALLOCATEDPTRS %d\n",
CONFIG_MAX_ALLOCATEDPTRS);
int i;
pthread_mutex_lock(&cfg->memBlocks_mutex);
int numptrs=cfg->numptrs;
for (i=0; i<numptrs; i++ )
if (cfg->oneBlock[i].ptrs == ptr)
break;
pthread_mutex_unlock(&cfg->memBlocks_mutex);
if ( i == numptrs )
return -1;
else
return cfg->oneBlock[i].sz;
}
void *config_allocate_new(int sz, bool autoFree) {
void *ptr = calloc(sz,1);
AssertFatal(ptr, "calloc fails\n");
configmodule_interface_t * cfg=config_get_if();
// add the memory piece in the managed memory pieces list
pthread_mutex_lock(&cfg->memBlocks_mutex);
int newBlockIdx=cfg->numptrs++;
oneBlock_t* tmp=&cfg->oneBlock[newBlockIdx];
tmp->ptrs = (char *)ptr;
tmp->ptrsAllocated = true;
tmp->sz=sz;
tmp->toFree=autoFree;
pthread_mutex_unlock(&cfg->memBlocks_mutex);
return ptr;
}
void config_check_valptr(paramdef_t *cfgoptions, int elt_sz, int nb_elt) {
const bool toFree=!(cfgoptions->paramflags & PARAMFLAG_NOFREE);
// let's see if the value has been read
// the datamodel is more difficult than it should be
// and it is not thread safe
if (cfgoptions->voidptr) {
int sz;
if ( cfgoptions-> type == TYPE_STRING)
sz=managed_ptr_sz(*cfgoptions->strptr);
else
sz=managed_ptr_sz(cfgoptions->voidptr);
if ( sz != -1 ) // the same variable pointer has been used!
cfgoptions->numelt=sz;
}
if (cfgoptions->voidptr != NULL
&& (cfgoptions->type == TYPE_INTARRAY
|| cfgoptions->type == TYPE_UINTARRAY
|| cfgoptions->type == TYPE_STRINGLIST )) {
int sz;
if ( cfgoptions->type == TYPE_STRING ||
cfgoptions->type == TYPE_STRINGLIST )
sz=managed_ptr_sz(*cfgoptions->strptr);
else
sz=managed_ptr_sz(cfgoptions->voidptr);
if ( sz == -1) {
CONFIG_PRINTF_ERROR("[CONFIG] %s NOT SUPPORTED not NULL pointer with array types", cfgoptions->optname);
return ;
}
}
if (cfgoptions->voidptr == NULL ) {
if (cfgoptions->type == TYPE_STRING) {
// difficult datamodel
cfgoptions->strptr = config_allocate_new(sizeof(*cfgoptions->strptr), toFree);
} else if ( cfgoptions->type == TYPE_STRINGLIST) {
AssertFatal(nb_elt<MAX_LIST_SIZE,
"This piece of code use fixed size arry of constant #define MAX_LIST_SIZE %d\n",
MAX_LIST_SIZE );
cfgoptions->strlistptr= config_allocate_new(sizeof(char*)*MAX_LIST_SIZE, toFree);
for (int i=0; i<MAX_LIST_SIZE; i++)
cfgoptions->strlistptr[i]= config_allocate_new(DEFAULT_EXTRA_SZ, toFree);
} else {
if ( cfgoptions->type == TYPE_INTARRAY || cfgoptions->type == TYPE_UINTARRAY )
nb_elt=max(nb_elt, MAX_LIST_SIZE); // make room if the list is larger than the default one
cfgoptions->voidptr = config_allocate_new(elt_sz*nb_elt, toFree);
}
}
if (cfgoptions->type == TYPE_STRING && *cfgoptions->strptr == NULL) {
*cfgoptions->strptr = config_allocate_new(nb_elt+DEFAULT_EXTRA_SZ, toFree);
cfgoptions->numelt=nb_elt+DEFAULT_EXTRA_SZ;
}
printf_ptrs("[CONFIG] %s ptr: %p requested size: elt_sz: %d, nb_elt: %d\n",cfgoptions->optname, cfgoptions->voidptr, elt_sz, nb_elt);
}
void config_assign_int(paramdef_t *cfgoptions, char *fullname, int val) {
int tmpval=val;
if ( ((cfgoptions->paramflags &PARAMFLAG_BOOL) != 0) && tmpval >0) {
tmpval =1;
}
switch (cfgoptions->type) {
case TYPE_UINT8:
*(cfgoptions->u8ptr) = (uint8_t)tmpval;
printf_params("[CONFIG] %s: %u\n", fullname, (uint8_t)tmpval);
break;
case TYPE_INT8:
*(cfgoptions->i8ptr) = (int8_t)tmpval;
printf_params("[CONFIG] %s: %i\n", fullname, (int8_t)tmpval);
break;
case TYPE_UINT16:
*(cfgoptions->u16ptr) = (uint16_t)tmpval;
printf_params("[CONFIG] %s: %hu\n", fullname, (uint16_t)tmpval);
break;
case TYPE_INT16:
*(cfgoptions->i16ptr) = (int16_t)tmpval;
printf_params("[CONFIG] %s: %hi\n", fullname, (int16_t)tmpval);
break;
case TYPE_UINT32:
*(cfgoptions->uptr) = (uint32_t)tmpval;
printf_params("[CONFIG] %s: %u\n", fullname, (uint32_t)tmpval);
break;
case TYPE_MASK:
*(cfgoptions->uptr) = *(cfgoptions->uptr) | (uint32_t)tmpval;
printf_params("[CONFIG] %s: 0x%08x\n", fullname, (uint32_t)tmpval);
break;
case TYPE_INT32:
*(cfgoptions->iptr) = (int32_t)tmpval;
printf_params("[CONFIG] %s: %i\n", fullname, (int32_t)tmpval);
break;
default:
fprintf (stderr,"[CONFIG] %s %i type %i non integer parameter %s not assigned\n",__FILE__, __LINE__,cfgoptions->type,fullname);
break;
}
}
void config_assign_processedint(paramdef_t *cfgoption, int val) {
cfgoption->processedvalue = config_allocate_new(sizeof(int),!(cfgoption->paramflags & PARAMFLAG_NOFREE));
static void config_assign_processedint(configmodule_interface_t *cfg, paramdef_t *cfgoption, int val)
{
cfgoption->processedvalue = config_allocate_new(cfg, sizeof(int), !(cfgoption->paramflags & PARAMFLAG_NOFREE));
if ( cfgoption->processedvalue != NULL) {
*(cfgoption->processedvalue) = val;
@@ -203,14 +55,15 @@ void config_assign_processedint(paramdef_t *cfgoption, int val) {
}
}
int config_get_processedint(paramdef_t *cfgoption) {
int config_get_processedint(configmodule_interface_t *cfg, paramdef_t *cfgoption)
{
int ret;
if ( cfgoption->processedvalue != NULL) {
ret=*(cfgoption->processedvalue);
free( cfgoption->processedvalue);
cfgoption->processedvalue=NULL;
printf_params("[CONFIG] %s: set from %s to %i\n",cfgoption->optname, *cfgoption->strptr, ret);
printf_params(cfg, "[CONFIG] %s: set from %s to %i\n", cfgoption->optname, *cfgoption->strptr, ret);
} else {
fprintf (stderr,"[CONFIG] %s %d %s has no processed integer availablle\n",__FILE__, __LINE__, cfgoption->optname);
ret=0;
@@ -231,7 +84,8 @@ void config_printhelp(paramdef_t *params,int numparams, char *prefix) {
printf("--------------------------------------------------------------------\n\n");
}
int config_execcheck(paramdef_t *params, int numparams, char *prefix) {
int config_execcheck(configmodule_interface_t *cfg, paramdef_t *params, int numparams, char *prefix)
{
int st=0;
for (int i=0 ; i<numparams ; i++) {
@@ -240,7 +94,7 @@ int config_execcheck(paramdef_t *params, int numparams, char *prefix) {
}
if (params[i].chkPptr->s4.f4 != NULL) {
st += params[i].chkPptr->s4.f4(&(params[i]));
st += params[i].chkPptr->s4.f4(cfg, &(params[i]));
}
}
@@ -261,7 +115,8 @@ int config_paramidx_fromname(paramdef_t *params, int numparams, char *name) {
return -1;
}
int config_get(paramdef_t *params, int numparams, char *prefix) {
int config_get(configmodule_interface_t *cfgif, paramdef_t *params, int numparams, char *prefix)
{
int ret= -1;
if (CONFIG_ISFLAGSET(CONFIG_ABORT)) {
@@ -269,16 +124,15 @@ int config_get(paramdef_t *params, int numparams, char *prefix) {
return ret;
}
configmodule_interface_t *cfgif = config_get_if();
if (cfgif != NULL) {
ret = config_get_if()->get(params, numparams, prefix);
ret = cfgif->get(cfgif, params, numparams, prefix);
if (ret >= 0) {
config_process_cmdline(params, numparams, prefix);
config_process_cmdline(cfgif, params, numparams, prefix);
if (cfgif->rtflags & CONFIG_SAVERUNCFG) {
config_get_if()->set(params, numparams, prefix);
cfgif->set(params, numparams, prefix);
}
config_execcheck(params, numparams, prefix);
config_execcheck(cfgif, params, numparams, prefix);
}
return ret;
@@ -287,16 +141,13 @@ int config_get(paramdef_t *params, int numparams, char *prefix) {
return ret;
}
int config_getlist(paramlist_def_t *ParamList, paramdef_t *params, int numparams, char *prefix) {
int config_getlist(configmodule_interface_t *cfg, paramlist_def_t *ParamList, paramdef_t *params, int numparams, char *prefix)
{
if (CONFIG_ISFLAGSET(CONFIG_ABORT)) {
fprintf(stderr,"[CONFIG] config_get skipped, config module not properly initialized\n");
return -1;
}
if (!config_get_if())
return -1;
const int ret = config_get_if()->getlist(ParamList, params, numparams, prefix);
const int ret = cfg->getlist(cfg, ParamList, params, numparams, prefix);
if (ret >= 0 && params) {
char *newprefix;
@@ -314,11 +165,11 @@ int config_getlist(paramlist_def_t *ParamList, paramdef_t *params, int numparams
for (int i = 0; i < ParamList->numelt; ++i) {
// TODO config_process_cmdline?
sprintf(cfgpath, "%s.[%i]", newprefix, i);
config_process_cmdline(ParamList->paramarray[i],numparams,cfgpath);
if (config_get_if()->rtflags & CONFIG_SAVERUNCFG) {
config_get_if()->set(ParamList->paramarray[i], numparams, cfgpath);
config_process_cmdline(cfg, ParamList->paramarray[i], numparams, cfgpath);
if (cfg->rtflags & CONFIG_SAVERUNCFG) {
cfg->set(ParamList->paramarray[i], numparams, cfgpath);
}
config_execcheck(ParamList->paramarray[i], numparams, cfgpath);
config_execcheck(cfg, ParamList->paramarray[i], numparams, cfgpath);
}
if (prefix)
@@ -347,7 +198,8 @@ void print_intvalueerror(paramdef_t *param, char *fname, int *okval, int numokva
fprintf(stderr, " \n");
}
int config_check_intval(paramdef_t *param) {
int config_check_intval(configmodule_interface_t *cfg, paramdef_t *param)
{
if ( param == NULL ) {
fprintf(stderr,"[CONFIG] config_check_intval: NULL param argument\n");
return -1;
@@ -363,10 +215,15 @@ int config_check_intval(paramdef_t *param) {
return -1;
}
int config_check_modify_integer(paramdef_t *param) {
int config_check_modify_integer(configmodule_interface_t *cfg, paramdef_t *param)
{
for (int i=0; i < param->chkPptr->s1a.num_okintval ; i++) {
if (*(param->uptr) == param->chkPptr->s1a.okintval[i] ) {
printf_params("[CONFIG] %s: read value %i, set to %i\n",param->optname,*(param->uptr),param->chkPptr->s1a.setintval [i]);
printf_params(cfg,
"[CONFIG] %s: read value %i, set to %i\n",
param->optname,
*(param->uptr),
param->chkPptr->s1a.setintval[i]);
*(param->uptr) = param->chkPptr->s1a.setintval [i];
return 0;
}
@@ -376,7 +233,8 @@ int config_check_modify_integer(paramdef_t *param) {
return -1;
}
int config_check_intrange(paramdef_t *param) {
int config_check_intrange(configmodule_interface_t *cfg, paramdef_t *param)
{
if( *(param->iptr) >= param->chkPptr->s2.okintrange[0] && *(param->iptr) <= param->chkPptr->s2.okintrange[1] ) {
return 0;
}
@@ -397,7 +255,8 @@ void print_strvalueerror(paramdef_t *param, char *fname, char **okval, int numok
fprintf(stderr, " \n");
}
int config_check_strval(paramdef_t *param) {
int config_check_strval(configmodule_interface_t *cfg, paramdef_t *param)
{
if ( param == NULL ) {
fprintf(stderr,"[CONFIG] config_check_strval: NULL param argument\n");
return -1;
@@ -413,10 +272,11 @@ int config_check_strval(paramdef_t *param) {
return -1;
}
int config_checkstr_assign_integer(paramdef_t *param) {
int config_checkstr_assign_integer(configmodule_interface_t *cfg, paramdef_t *param)
{
for (int i=0; i < param->chkPptr->s3a.num_okstrval ; i++) {
if (strcasecmp(*param->strptr,param->chkPptr->s3a.okstrval[i] ) == 0) {
config_assign_processedint(param, param->chkPptr->s3a.setintval[i]);
config_assign_processedint(cfg, param, param->chkPptr->s3a.setintval[i]);
return 0;
}
}
@@ -430,123 +290,3 @@ void config_set_checkfunctions(paramdef_t *params, checkedparam_t *checkfunction
params[i].chkPptr = &(checkfunctions[i]);
}
}
int config_setdefault_string(paramdef_t *cfgoptions, char *prefix) {
int status = 0;
if( cfgoptions->defstrval != NULL) {
status=1;
if (cfgoptions->numelt == 0 )
config_check_valptr(cfgoptions, 1, strlen(cfgoptions->defstrval)+1);
if ( cfgoptions->numelt < strlen(cfgoptions->defstrval)+1 ) {
CONFIG_PRINTF_ERROR("[CONFIG] %s size too small\n", cfgoptions->optname);
}
snprintf(*cfgoptions->strptr, cfgoptions->numelt, "%s",cfgoptions->defstrval);
printf_params("[CONFIG] %s.%s set to default value \"%s\"\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname, *cfgoptions->strptr);
}
return status;
}
int config_setdefault_stringlist(paramdef_t *cfgoptions, char *prefix) {
int status = 0;
if( cfgoptions->defstrlistval != NULL) {
cfgoptions->strlistptr=cfgoptions->defstrlistval;
status=1;
for(int j=0; j<cfgoptions->numelt; j++)
printf_params("[CONFIG] %s.%s[%i] set to default value %s\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname,j, cfgoptions->strlistptr[j]);
}
return status;
}
int config_setdefault_int(paramdef_t *cfgoptions, char *prefix) {
int status = 0;
config_check_valptr(cfgoptions, sizeof(*cfgoptions->iptr), 1);
if( ((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
config_assign_int(cfgoptions,cfgoptions->optname,cfgoptions->defintval);
status=1;
printf_params("[CONFIG] %s.%s set to default value\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname);
}
return status;
}
int config_setdefault_int64(paramdef_t *cfgoptions, char *prefix) {
int status = 0;
config_check_valptr(cfgoptions, sizeof(*cfgoptions->i64ptr), 1);
if( ((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
*(cfgoptions->u64ptr)=cfgoptions->defuintval;
status=1;
printf_params("[CONFIG] %s.%s set to default value %llu\n", ((prefix == NULL) ? "" : prefix), cfgoptions->optname, (long long unsigned)(*(cfgoptions->u64ptr)));
}
return status;
}
int config_setdefault_intlist(paramdef_t *cfgoptions, char *prefix) {
int status = 0;
if( cfgoptions->defintarrayval != NULL) {
config_check_valptr(cfgoptions, sizeof(cfgoptions->iptr), cfgoptions->numelt);
cfgoptions->iptr=cfgoptions->defintarrayval;
status=1;
for (int j=0; j<cfgoptions->numelt ; j++) {
printf_params("[CONFIG] %s[%i] set to default value %i\n",cfgoptions->optname,j,(int)cfgoptions->iptr[j]);
}
}
return status;
}
int config_setdefault_double(paramdef_t *cfgoptions, char *prefix) {
int status = 0;
config_check_valptr(cfgoptions, sizeof(*cfgoptions->dblptr), 1);
if( ((cfgoptions->paramflags & PARAMFLAG_MANDATORY) == 0)) {
*(cfgoptions->dblptr)=cfgoptions->defdblval;
status=1;
printf_params("[CONFIG] %s set to default value %lf\n",cfgoptions->optname, *(cfgoptions->dblptr));
}
return status;
}
int config_assign_ipv4addr(paramdef_t *cfgoptions, char *ipv4addr) {
config_check_valptr(cfgoptions, sizeof(*cfgoptions->uptr), 1);
int rst=inet_pton(AF_INET, ipv4addr,cfgoptions->uptr );
if (rst == 1 && *(cfgoptions->uptr) > 0) {
printf_params("[CONFIG] %s: %s\n",cfgoptions->optname, ipv4addr);
return 1;
} else {
if ( strncmp(ipv4addr,ANY_IPV4ADDR_STRING,sizeof(ANY_IPV4ADDR_STRING)) == 0) {
printf_params("[CONFIG] %s:%s (INADDR_ANY) \n",cfgoptions->optname,ipv4addr);
*cfgoptions->uptr=INADDR_ANY;
return 1;
} else {
fprintf(stderr,"[CONFIG] %s not valid for %s \n", ipv4addr, cfgoptions->optname);
return -1;
}
}
return 0;
}
int config_setdefault_ipv4addr(paramdef_t *cfgoptions, char *prefix) {
int status = 0;
if (cfgoptions->defstrval != NULL) {
status = config_assign_ipv4addr(cfgoptions, cfgoptions->defstrval);
}
return status;
}
+21 -32
View File
@@ -33,60 +33,49 @@
#ifndef INCLUDE_CONFIG_USERAPI_H
#define INCLUDE_CONFIG_USERAPI_H
#include "config_load_configmodule.h"
#include "common/utils/utils.h"
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef NOT_uniqCfg
extern configmodule_interface_t *uniqCfg;
#define config_get_if() uniqCfg // Existing code implicit uniq configuration context
#endif
/* utility functions to ease usage of config module structures */
#define CONFIG_GETSOURCE ( (config_get_if()==NULL) ? NULL : config_get_if()->cfgmode )
#define CONFIG_GETNUMP ( (config_get_if()==NULL) ? 0 : config_get_if()->num_cfgP )
#define CONFIG_GETP(P) ( (config_get_if()==NULL) ? NULL : config_get_if()->cfgP[P] )
#define CONFIG_GETSOURCE ((config_get_if() == NULL) ? NULL : config_get_if()->cfgmode)
#define CONFIG_ISFLAGSET(P) ( (config_get_if()==NULL) ? 0 : !!(config_get_if()->rtflags & P))
#define CONFIG_SETRTFLAG(P) if (config_get_if()) { config_get_if()->rtflags |= P; }
#define CONFIG_CLEARRTFLAG(P) if (config_get_if()) { config_get_if()->rtflags &= (~P); }
#define CONFIG_ISPARAMFLAGSET(P,F) ( !!(P.paramflags & F))
extern int config_paramidx_fromname(paramdef_t *params,int numparams, char *name);
int config_paramidx_fromname(paramdef_t *params, int numparams, char *name);
/* utility functions, to be used by configuration module and/or configuration libraries */
extern configmodule_interface_t *config_get_if(void);
void config_check_valptr(paramdef_t *cfgoptions, int elt_sz, int nb_elt);
extern void config_printhelp(paramdef_t *,int numparams, char *prefix);
extern int config_process_cmdline(paramdef_t *params,int numparams, char *prefix);
extern void config_assign_processedint(paramdef_t *cfgoption, int val);
extern void config_assign_int(paramdef_t *cfgoptions, char *fullname, int val);
extern int config_assign_ipv4addr(paramdef_t *cfgoptions, char *ipv4addr);
void config_printhelp(paramdef_t *, int numparams, char *prefix);
int config_process_cmdline(configmodule_interface_t *cfg, paramdef_t *params, int numparams, char *prefix);
int config_assign_ipv4addr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, char *ipv4addr);
/* apis to get/check parameters, to be used by oai modules, at configuration time */
#define CONFIG_CHECKALLSECTIONS "ALLSECTIONS"
extern int config_check_unknown_cmdlineopt(char *prefix);
extern int config_get(paramdef_t *params,int numparams, char *prefix);
extern int config_getlist(paramlist_def_t *ParamList, paramdef_t *params, int numparams, char *prefix);
int config_check_unknown_cmdlineopt(configmodule_interface_t *cfg, char *prefix);
int config_get(configmodule_interface_t *cfg, paramdef_t *params, int numparams, char *prefix);
int config_getlist(configmodule_interface_t *cfg, paramlist_def_t *ParamList, paramdef_t *params, int numparams, char *prefix);
/* apis to set some of the paramdef_t fields before using the get/getlist api's */
extern void config_set_checkfunctions(paramdef_t *params, checkedparam_t *checkfunctions, int numparams);
void config_set_checkfunctions(paramdef_t *params, checkedparam_t *checkfunctions, int numparams);
/* apis to retrieve parameters info after calling get or getlist functions */
extern int config_isparamset(paramdef_t *params,int paramidx);
extern int config_get_processedint(paramdef_t *cfgoption);
int config_isparamset(paramdef_t *params, int paramidx);
int config_get_processedint(configmodule_interface_t *cfg, paramdef_t *cfgoption);
/* functions to be used in parameters definition, to check parameters values */
extern int config_check_intval(paramdef_t *param);
extern int config_check_modify_integer(paramdef_t *param);
extern int config_check_intrange(paramdef_t *param);
extern int config_check_strval(paramdef_t *param);
extern int config_checkstr_assign_integer(paramdef_t *param);
int config_check_intval(configmodule_interface_t *cfg, paramdef_t *param);
int config_check_modify_integer(configmodule_interface_t *cfg, paramdef_t *param);
int config_check_intrange(configmodule_interface_t *cfg, paramdef_t *param);
int config_check_strval(configmodule_interface_t *cfg, paramdef_t *param);
int config_checkstr_assign_integer(configmodule_interface_t *cfg, paramdef_t *param);
/* functions to set a parameter to its default value */
extern int config_setdefault_string(paramdef_t *cfgoptions, char *prefix);
extern int config_setdefault_stringlist(paramdef_t *cfgoptions, char *prefix);
extern int config_setdefault_int(paramdef_t *cfgoptions, char *prefix);
extern int config_setdefault_int64(paramdef_t *cfgoptions, char *prefix);
extern int config_setdefault_intlist(paramdef_t *cfgoptions, char *prefix);
extern int config_setdefault_double(paramdef_t *cfgoptions, char *prefix);
extern int config_setdefault_ipv4addr(paramdef_t *cfgoptions, char *prefix);
void *config_allocate_new(int sz, bool autoFree);
#define CONFIG_GETCONFFILE (config_get_if()->cfgP[0])
#ifdef __cplusplus
+102 -74
View File
@@ -43,6 +43,8 @@
#include "config_libconfig.h"
#include "config_libconfig_private.h"
#define NOT_uniqCfg
#include "../config_common.h"
#include "../config_userapi.h"
#include "errno.h"
#include "assertions.h"
@@ -51,14 +53,15 @@
#define config_setting_lookup config_lookup_from
#endif
void config_libconfig_end(void );
void config_libconfig_end(configmodule_interface_t *cfg);
int read_strlist(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath) {
int read_strlist(configmodule_interface_t *cfg, 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, sizeof(char *), numelt);
config_check_valptr(cfg, cfgoptions, sizeof(char *), numelt);
st=0;
AssertFatal(MAX_LIST_SIZE > numelt,
"This piece of code use fixed size arry of constant #define MAX_LIST_SIZE %d\n",
@@ -71,7 +74,7 @@ int read_strlist(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath
} else {
snprintf(cfgoptions->strlistptr[i], DEFAULT_EXTRA_SZ, "%s",str);
st++;
printf_params("[LIBCONFIG] %s%i: %s\n", cfgpath,i,cfgoptions->strlistptr[i]);
printf_params(cfg, "[LIBCONFIG] %s%i: %s\n", cfgpath, i, cfgoptions->strlistptr[i]);
}
}
@@ -79,22 +82,27 @@ int read_strlist(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath
return st;
}
int read_intarray(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpath) {
static int read_intarray(configmodule_interface_t *cfg, paramdef_t *cfgoptions, config_setting_t *setting, char *cfgpath)
{
cfgoptions->numelt=config_setting_length(setting);
if (cfgoptions->numelt > 0) {
cfgoptions->iptr=config_allocate_new(cfgoptions->numelt*sizeof(*cfgoptions->iptr),
!(cfgoptions->paramflags & PARAMFLAG_NOFREE) );
cfgoptions->iptr =
config_allocate_new(cfg, cfgoptions->numelt * sizeof(*cfgoptions->iptr), !(cfgoptions->paramflags & PARAMFLAG_NOFREE));
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]);
printf_params(cfg, "[LIBCONFIG] %s[%i]: %i\n", cfgpath, i, cfgoptions->iptr[i]);
} /* for loop on array element... */
}
return cfgoptions->numelt;
}
int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_setting_t *asetting, char *prefix)
int config_libconfig_setparams(configmodule_interface_t *cfg,
paramdef_t *cfgoptions,
int numoptions,
config_setting_t *asetting,
char *prefix)
{
int status;
int errors = 0;
@@ -106,7 +114,7 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
for (int i = 0; i < numoptions; i++) {
if (cfgoptions[i].paramflags & PARAMFLAG_CMDLINEONLY) {
continue;
printf_params("[LIBCONFIG] setting %s.%s skipped (command line only) \n", secprefix, cfgoptions[i].optname);
printf_params(cfg, "[LIBCONFIG] setting %s.%s skipped (command line only) \n", secprefix, cfgoptions[i].optname);
}
status = CONFIG_FALSE;
config_setting_t *psetting;
@@ -115,7 +123,7 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
psetting = config_lookup(&(libconfig_privdata.runtcfg), spath);
free(spath);
if (psetting != NULL) {
printf_params("[LIBCONFIG] setting %s.%s already created \n", secprefix, cfgoptions[i].optname);
printf_params(cfg, "[LIBCONFIG] setting %s.%s already created \n", secprefix, cfgoptions[i].optname);
continue;
}
switch (cfgoptions[i].type) {
@@ -137,7 +145,7 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
if (psetting != NULL) {
if (cfgoptions[i].numelt <= 0) {
emptyla++;
printf_params("[LIBCONFIG], no element in list %s\n", cfgoptions[i].optname);
printf_params(cfg, "[LIBCONFIG], no element in list %s\n", cfgoptions[i].optname);
status = CONFIG_TRUE;
}
for (int j = 0; j < cfgoptions[i].numelt; j++) {
@@ -176,7 +184,7 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
if (psetting != NULL) {
if (cfgoptions[i].numelt <= 0) {
emptyla++;
printf_params("[LIBCONFIG], no element in array %s.%s\n", secprefix, cfgoptions[i].optname);
printf_params(cfg, "[LIBCONFIG], no element in array %s.%s\n", secprefix, cfgoptions[i].optname);
status = CONFIG_TRUE;
}
for (int j = 0; j < cfgoptions[i].numelt; j++) {
@@ -222,24 +230,24 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
fprintf(stderr, "[LIBCONFIG] Error creating setting %i: %s.%s type %i\n", i, secprefix, cfgoptions[i].optname, cfgoptions[i].type);
}
}
printf_params("[LIBCONFIG], in group \"%s\" %i settings \n", secprefix, numoptions);
printf_params(cfg, "[LIBCONFIG], in group \"%s\" %i settings \n", secprefix, numoptions);
if (notused > 0)
printf_params("[LIBCONFIG], ..... %i settings with NULL value pointer\n", notused);
printf_params(cfg, "[LIBCONFIG], ..... %i settings with NULL value pointer\n", notused);
if (errors > 0)
fprintf(stderr, "[LIBCONFIG] .....%i settings creation errors \n", errors);
if (emptyla > 0)
fprintf(stderr, "[LIBCONFIG] .....%i empty lists or arrays settings \n", emptyla);
if (cfgptr->status) {
cfgptr->status->num_err_nullvalue += notused;
cfgptr->status->num_err_write += errors;
cfgptr->status->num_write += numoptions;
cfgptr->status->emptyla += emptyla;
if (cfg->status) {
cfg->status->num_err_nullvalue += notused;
cfg->status->num_err_write += errors;
cfg->status->num_write += numoptions;
cfg->status->emptyla += emptyla;
}
return errors;
}
int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix)
int config_libconfig_set(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int numoptions, char *prefix)
{
char *tokctx1 = NULL;
int listidx = -1;
@@ -250,13 +258,13 @@ int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix)
prefixbck = strdup(prefix);
prefix_elem1 = strtok_r(prefixbck, ".", &tokctx1);
}
printf_params("[LIBCONFIG], processing prefix %s\n", (prefix == NULL) ? "NULL" : prefix);
printf_params(cfg, "[LIBCONFIG], processing prefix %s\n", (prefix == NULL) ? "NULL" : prefix);
/* parse the prefix , possibly creating groups, lists and list elements */
while (prefix_elem1 != NULL) {
int n1 = strlen(prefix_elem1);
char *prefix_elem2 = prefix_elem1 + n1 + 1;
printf_params("[LIBCONFIG], processing elem1 %s elem2 %s\n", prefix_elem1, prefix_elem2);
printf_params(cfg, "[LIBCONFIG], processing elem1 %s elem2 %s\n", prefix_elem1, prefix_elem2);
/* prefix (the path to the parameter name) may contain groups and elements from a list, which are specified with [] */
if (prefix_elem2[0] != '[') { // not a list
@@ -265,11 +273,11 @@ int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix)
asetting = config_setting_add(asetting, prefix_elem1, CONFIG_TYPE_GROUP);
else
asetting = tmpset;
printf_params("[LIBCONFIG], creating or looking for group %s: %s\n", prefix_elem1, (asetting == NULL) ? "NOK" : "OK");
printf_params(cfg, "[LIBCONFIG], creating or looking for group %s: %s\n", prefix_elem1, (asetting == NULL) ? "NOK" : "OK");
} else { // a list
listidx = (int)strtol(prefix_elem2 + 1, NULL, 10);
if (errno == EINVAL || errno == ERANGE) {
printf_params("[LIBCONFIG], Error %s looking for list index in %s \n", strerror(errno), prefix_elem2);
printf_params(cfg, "[LIBCONFIG], Error %s looking for list index in %s \n", strerror(errno), prefix_elem2);
break;
}
config_setting_t *tmpset = config_setting_lookup(asetting, prefix_elem1);
@@ -277,14 +285,17 @@ int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix)
asetting = config_setting_add(asetting, prefix_elem1, CONFIG_TYPE_LIST);
else
asetting = tmpset;
printf_params("[LIBCONFIG], creating or looking for list %s: %s\n", prefix_elem1, (asetting == NULL) ? "NOK" : "OK");
printf_params(cfg, "[LIBCONFIG], creating or looking for list %s: %s\n", prefix_elem1, (asetting == NULL) ? "NOK" : "OK");
if (asetting != NULL) {
tmpset = config_setting_get_elem(asetting, listidx);
if (tmpset == NULL)
asetting = config_setting_add(asetting, NULL, CONFIG_TYPE_GROUP);
else
asetting = tmpset;
printf_params("[LIBCONFIG], creating or looking for list element %i: %s\n", listidx, (asetting == NULL) ? "NOK" : "OK");
printf_params(cfg,
"[LIBCONFIG], creating or looking for list element %i: %s\n",
listidx,
(asetting == NULL) ? "NOK" : "OK");
}
prefix_elem1 = strtok_r(NULL, ".", &tokctx1); // skip the [x] elements we already took care of it
if (prefix_elem1 == NULL) {
@@ -299,8 +310,8 @@ int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix)
}
free(prefixbck);
if (asetting != NULL) {
config_libconfig_setparams(cfgoptions, numoptions, asetting, prefix);
printf_params("[LIBCONFIG] %i settings added in group %s\n", numoptions, (prefix == NULL) ? "" : prefix);
config_libconfig_setparams(cfg, cfgoptions, numoptions, asetting, prefix);
printf_params(cfg, "[LIBCONFIG] %i settings added in group %s\n", numoptions, (prefix == NULL) ? "" : prefix);
return 0;
} else {
fprintf(stderr, "[LIBCONFIG] Error parsing %s params, skipped...\n", (prefix == NULL) ? "" : prefix);
@@ -308,7 +319,8 @@ int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix)
}
}
int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
int config_libconfig_get(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int numoptions, char *prefix)
{
config_setting_t *setting;
char *str;
int i,u;
@@ -330,7 +342,7 @@ int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
}
if( (cfgoptions->paramflags & PARAMFLAG_DONOTREAD) != 0) {
printf_params("[LIBCONFIG] %s.%s ignored\n", cfgpath,cfgoptions[i].optname );
printf_params(cfg, "[LIBCONFIG] %s.%s ignored\n", cfgpath, cfgoptions[i].optname);
continue;
}
@@ -340,14 +352,14 @@ int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
switch(cfgoptions[i].type) {
case TYPE_STRING:
if ( config_lookup_string(&(libconfig_privdata.cfg), cfgpath, (const char**)&str)) {
config_check_valptr(&(cfgoptions[i]), 1, strlen(str)+1);
config_check_valptr(cfg, &cfgoptions[i], 1, strlen(str) + 1);
if ( strlen(str)+1 > cfgoptions[i].numelt )
fprintf(stderr,"[LIBCONFIG] %s: %s exceeds maximum length of %i bytes, value truncated\n",
cfgpath,str,cfgoptions[i].numelt);
snprintf( *cfgoptions[i].strptr , cfgoptions[i].numelt, "%s", str);
printf_params("[LIBCONFIG] %s: \"%s\"\n", cfgpath, *cfgoptions[i].strptr);
printf_params(cfg, "[LIBCONFIG] %s: \"%s\"\n", cfgpath, *cfgoptions[i].strptr);
} else {
defval=config_setdefault_string(&cfgoptions[i],prefix);
defval = config_setdefault_string(cfg, &cfgoptions[i], prefix);
}
break;
@@ -356,9 +368,9 @@ int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );
if ( setting != NULL) {
read_strlist(&cfgoptions[i],setting,cfgpath);
read_strlist(cfg, &cfgoptions[i], setting, cfgpath);
} else {
defval=config_setdefault_stringlist(&(cfgoptions[i]),prefix);
defval = config_setdefault_stringlist(cfg, &(cfgoptions[i]), prefix);
}
break;
@@ -371,10 +383,10 @@ int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
case TYPE_INT32:
case TYPE_MASK:
if ( config_lookup_int(&(libconfig_privdata.cfg),cfgpath, &u)) {
config_check_valptr(&(cfgoptions[i]), sizeof(*cfgoptions[i].iptr), 1);
config_assign_int(&(cfgoptions[i]),cfgpath,u);
config_check_valptr(cfg, &(cfgoptions[i]), sizeof(*cfgoptions[i].iptr), 1);
config_assign_int(cfg, &(cfgoptions[i]), cfgpath, u);
} else {
defval=config_setdefault_int(&(cfgoptions[i]),prefix);
defval = config_setdefault_int(cfg, &(cfgoptions[i]), prefix);
}
break;
@@ -382,17 +394,17 @@ int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
case TYPE_UINT64:
case TYPE_INT64:
if ( config_lookup_int64(&(libconfig_privdata.cfg),cfgpath, &llu)) {
config_check_valptr(&cfgoptions[i], sizeof(*cfgoptions[i].i64ptr), 1);
config_check_valptr(cfg, &cfgoptions[i], sizeof(*cfgoptions[i].i64ptr), 1);
if(cfgoptions[i].type==TYPE_UINT64) {
*(cfgoptions[i].u64ptr) = (uint64_t)llu;
printf_params("[LIBCONFIG] %s: %lu\n", cfgpath,*cfgoptions[i].u64ptr);
printf_params(cfg, "[LIBCONFIG] %s: %lu\n", cfgpath, *cfgoptions[i].u64ptr);
} else {
*(cfgoptions[i].i64ptr) = llu;
printf_params("[LIBCONFIG] %s: %ld\n", cfgpath,*cfgoptions[i].i64ptr);
printf_params(cfg, "[LIBCONFIG] %s: %ld\n", cfgpath, *cfgoptions[i].i64ptr);
}
} else {
defval=config_setdefault_int64(&(cfgoptions[i]),prefix);
defval = config_setdefault_int64(cfg, &(cfgoptions[i]), prefix);
}
break;
@@ -402,29 +414,29 @@ int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
setting = config_setting_lookup (config_root_setting(&(libconfig_privdata.cfg)),cfgpath );
if ( setting != NULL) {
read_intarray(&cfgoptions[i],setting,cfgpath);
read_intarray(cfg, &cfgoptions[i], setting, cfgpath);
} else {
defval=config_setdefault_intlist(&(cfgoptions[i]),prefix);
defval = config_setdefault_intlist(cfg, &(cfgoptions[i]), prefix);
}
break;
case TYPE_DOUBLE:
if ( config_lookup_float(&(libconfig_privdata.cfg),cfgpath, &dbl)) {
config_check_valptr(&cfgoptions[i],sizeof(*cfgoptions[i].dblptr), 1);
config_check_valptr(cfg, &cfgoptions[i], sizeof(*cfgoptions[i].dblptr), 1);
*cfgoptions[i].dblptr = dbl;
printf_params("[LIBCONFIG] %s: %lf\n", cfgpath,*(cfgoptions[i].dblptr) );
printf_params(cfg, "[LIBCONFIG] %s: %lf\n", cfgpath, *(cfgoptions[i].dblptr));
} else {
defval=config_setdefault_double(&(cfgoptions[i]),prefix);
defval = config_setdefault_double(cfg, &(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);
defval = config_setdefault_ipv4addr(cfg, &(cfgoptions[i]), prefix);
} else {
rst=config_assign_ipv4addr(cfgoptions, str);
rst = config_assign_ipv4addr(cfg, cfgoptions, str);
if (rst < 0) {
fprintf(stderr,"[LIBCONFIG] %s not valid for %s \n", str, cfgpath);
@@ -478,15 +490,19 @@ int config_libconfig_get(paramdef_t *cfgoptions,int numoptions, char *prefix ) {
if (fatalerror == 1) {
fprintf(stderr,"[LIBCONFIG] fatal errors found when processing %s \n", libconfig_privdata.configfile );
config_libconfig_end();
end_configmodule();
config_libconfig_end(cfg);
end_configmodule(cfg);
}
return status;
}
int config_libconfig_getlist(paramlist_def_t *ParamList,
paramdef_t *params, int numparams, char *prefix) {
int config_libconfig_getlist(configmodule_interface_t *cfg,
paramlist_def_t *ParamList,
paramdef_t *params,
int numparams,
char *prefix)
{
config_setting_t *setting;
int i,j,status;
char *listpath=NULL;
@@ -502,8 +518,7 @@ int config_libconfig_getlist(paramlist_def_t *ParamList,
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 );
printf_params(cfg, "[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 );
@@ -512,10 +527,10 @@ int config_libconfig_getlist(paramlist_def_t *ParamList,
}
if (ParamList->numelt > 0 && params != NULL) {
ParamList->paramarray = config_allocate_new(ParamList->numelt * sizeof(paramdef_t *), true);
ParamList->paramarray = config_allocate_new(cfg, ParamList->numelt * sizeof(paramdef_t *), true);
for (i=0 ; i < ParamList->numelt ; i++) {
ParamList->paramarray[i] = config_allocate_new(numparams * sizeof(paramdef_t), true);
ParamList->paramarray[i] = config_allocate_new(cfg, numparams * sizeof(paramdef_t), true);
memcpy(ParamList->paramarray[i], params, sizeof(paramdef_t)*numparams);
for (j=0; j<numparams; j++) {
@@ -523,7 +538,7 @@ int config_libconfig_getlist(paramlist_def_t *ParamList,
}
sprintf(cfgpath,"%s.[%i]",listpath,i);
config_libconfig_get(ParamList->paramarray[i], numparams, cfgpath );
config_libconfig_get(cfg, ParamList->paramarray[i], numparams, cfgpath);
} /* for i... */
} /* ParamList->numelt > 0 && params != NULL */
@@ -533,12 +548,15 @@ int config_libconfig_getlist(paramlist_def_t *ParamList,
return status;
}
int config_libconfig_init(char *cfgP[], int numP) {
int config_libconfig_init(configmodule_interface_t *cfg)
{
char **cfgP = cfg->cfgP;
int numP = cfg->num_cfgP;
config_init(&(libconfig_privdata.cfg));
libconfig_privdata.configfile = strdup((char *)cfgP[0]);
config_get_if()->numptrs=0;
pthread_mutex_init(&config_get_if()->memBlocks_mutex, NULL);
memset(config_get_if()->oneBlock,0,sizeof(config_get_if()->oneBlock));
cfg->numptrs = 0;
pthread_mutex_init(&cfg->memBlocks_mutex, NULL);
memset(cfg->oneBlock, 0, sizeof(cfg->oneBlock));
/* search for include path parameter and set config file include path accordingly */
for (int i=0; i<numP; i++) {
if (strncmp(cfgP[i],"incp",4) == 0) {
@@ -569,7 +587,7 @@ int config_libconfig_init(char *cfgP[], int numP) {
}
/* possibly init a libconfig struct for saving really used params */
if (cfgptr->rtflags & CONFIG_SAVERUNCFG) {
if (cfg->rtflags & CONFIG_SAVERUNCFG) {
config_init(&(libconfig_privdata.runtcfg));
// config_set_options (&(libconfig_privdata.runtcfg), CONFIG_OPTION_ALLOW_OVERRIDES, CONFIG_TRUE);
}
@@ -577,25 +595,34 @@ int config_libconfig_init(char *cfgP[], int numP) {
return 0;
}
void config_libconfig_write_parsedcfg(void)
void config_libconfig_write_parsedcfg(configmodule_interface_t *cfg)
{
if (cfgptr->rtflags & CONFIG_SAVERUNCFG) {
if (cfg->rtflags & CONFIG_SAVERUNCFG) {
char *fname = strdup(libconfig_privdata.configfile);
int newcfgflen = strlen(libconfig_privdata.configfile) + strlen(cfgptr->tmpdir) + 20;
cfgptr->status->debug_cfgname = realloc(cfgptr->status->debug_cfgname, newcfgflen);
int newcfgflen = strlen(libconfig_privdata.configfile) + strlen(cfg->tmpdir) + 20;
cfg->status->debug_cfgname = realloc(cfg->status->debug_cfgname, newcfgflen);
time_t t = time(NULL);
struct tm tm = *localtime(&t);
snprintf(cfgptr->status->debug_cfgname, newcfgflen, "%s/%s-run%d_%02d_%02d_%02d%02d", cfgptr->tmpdir, basename(fname), tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);
if (config_write_file(&(libconfig_privdata.runtcfg), cfgptr->status->debug_cfgname) != CONFIG_TRUE) {
snprintf(cfg->status->debug_cfgname,
newcfgflen,
"%s/%s-run%d_%02d_%02d_%02d%02d",
cfg->tmpdir,
basename(fname),
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min);
if (config_write_file(&(libconfig_privdata.runtcfg), cfg->status->debug_cfgname) != CONFIG_TRUE) {
fprintf(stderr,
"[LIBCONFIG] %s %d file %s - line %d: %s\n",
__FILE__,
__LINE__,
cfgptr->status->debug_cfgname,
cfg->status->debug_cfgname,
config_error_line(&(libconfig_privdata.runtcfg)),
config_error_text(&(libconfig_privdata.runtcfg)));
} else {
printf("[LIBCONFIG] file %s created successfully\n", cfgptr->status->debug_cfgname);
printf("[LIBCONFIG] file %s created successfully\n", cfg->status->debug_cfgname);
}
free(fname);
} else {
@@ -603,9 +630,10 @@ void config_libconfig_write_parsedcfg(void)
}
}
void config_libconfig_end(void ) {
void config_libconfig_end(configmodule_interface_t *cfg)
{
config_destroy(&(libconfig_privdata.cfg));
if (cfgptr->rtflags & CONFIG_SAVERUNCFG) {
if (cfg->rtflags & CONFIG_SAVERUNCFG) {
config_destroy(&(libconfig_privdata.runtcfg));
}
if ( libconfig_privdata.configfile != NULL ) {