Add generic shared lib loader

This commit is contained in:
oai
2017-09-29 16:15:04 +02:00
parent 30abbdf826
commit 16eb450927
11 changed files with 138 additions and 97 deletions
+31 -39
View File
@@ -37,24 +37,16 @@
int processoption(paramdef_t *cfgoptions, char *value)
{
int argok=1;
char *tmpval = value;
int optisset;
int optisset=0;
char defbool[2]="1";
if (value == NULL) {
argok=0;
} else if ( value[0] == '-') {
argok = 0;
}
if ((cfgoptions->paramflags &PARAMFLAG_BOOL) == 0) { /* not a boolean, argument required */
if (argok == 0) {
if ( ((cfgoptions->paramflags &PARAMFLAG_BOOL) == 0) && value == NULL ) { /* not a boolean, argument required */
fprintf(stderr,"[CONFIG] command line, option %s requires an argument\n",cfgoptions->optname);
return 0;
}
} else { /* boolean value */
tmpval = defbool;
} else { /* boolean value option without argument, set value to true*/
tmpval = defbool;
}
switch(cfgoptions->type)
{
case TYPE_STRING:
@@ -107,7 +99,7 @@ char defbool[2]="1";
cfgoptions->paramflags = cfgoptions->paramflags | PARAMFLAG_PARAMSET;
}
return argok;
return optisset;
}
int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
@@ -115,6 +107,7 @@ int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
char **p = config_get_if()->argv;
int c = config_get_if()->argc;
int j;
char *pp;
char *cfgpath;
j = (prefix ==NULL) ? 0 : strlen(prefix);
@@ -131,32 +124,31 @@ char *cfgpath;
if (strcmp(*p, "-h") == 0 || strcmp(*p, "--help") == 0 ) {
config_printhelp(cfgoptions,numoptions);
}
for(int i=0;i<numoptions;i++) {
if ( ( cfgoptions[i].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
continue;
}
if (prefix != NULL) {
sprintf(cfgpath,"%s.%s",prefix,cfgoptions[i].optname);
} else {
sprintf(cfgpath,"%s",cfgoptions[i].optname);
}
if ( ((strlen(*p) == 2) && (strcmp(*p + 1,cfgpath) == 0)) ||
((strlen(*p) > 2) && (strcmp(*p + 2,cfgpath ) == 0 )) ) {
if (*p[0] == '-') {
for(int i=0;i<numoptions;i++) {
if ( ( cfgoptions[i].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
continue;
}
if (prefix != NULL) {
sprintf(cfgpath,"%s.%s",prefix,cfgoptions[i].optname);
} else {
sprintf(cfgpath,"%s",cfgoptions[i].optname);
}
if ( ((strlen(*p) == 2) && (strcmp(*p + 1,cfgpath) == 0)) ||
((strlen(*p) > 2) && (strcmp(*p + 2,cfgpath ) == 0 )) ) {
pp = *(p+1);
if ( ( pp != NULL ) && (pp[0]!= '-') ) {
p++;
c--;
j += processoption(&(cfgoptions[i]), pp);
} else {
j += processoption(&(cfgoptions[i]), NULL);
}
if (c > 1) {
p++;
c--;
j = processoption(&(cfgoptions[i]), *p);
if ( j== 0) {
c++;
p--;
}
} else {
j = processoption(&(cfgoptions[i]), NULL);
}
}
}
}
} /* for */
} /* if (*p[0] == '-') */
p++;
c--;
} /* fin du while */
+52
View File
@@ -0,0 +1,52 @@
/* FT NOKBLF:
* this source is to be linked with the program using the telnet server, it looks for
* the telnet server dynamic library, possibly loads it and calls the telnet server
* init functions
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <dlfcn.h>
#include "openair1/PHY/defs.h"
#define LOAD_MODULE_SHLIB_MAIN
#include "load_module_shlib.h"
int load_module_shlib(char *modname)
{
void *lib_handle;
initfunc_t fpi;
char *tmpstr;
int ret=0;
tmpstr = malloc(strlen(modname)+16);
if (tmpstr == NULL) {
fprintf(stderr,"[LOADER] %s %d malloc error loading module %s, %s\n",__FILE__, __LINE__, modname, strerror(errno));
return -1;
}
sprintf(tmpstr,"lib%s.so",modname);
lib_handle = dlopen(tmpstr, RTLD_LAZY|RTLD_NODELETE|RTLD_GLOBAL);
if (!lib_handle) {
printf("[LOADER] library %s is not loaded: %s\n", tmpstr,dlerror());
ret = -1;
} else {
sprintf(tmpstr,"init_%s",modname);
fpi = dlsym(lib_handle,tmpstr);
if (fpi != NULL )
{
fpi();
}
else
{
fprintf(stderr,"[LOADER] %s %d %s function not found %s\n",__FILE__, __LINE__, dlerror(),tmpstr);
ret = -1;
}
}
if (tmpstr != NULL) free(tmpstr);
if (lib_handle != NULL) dlclose(lib_handle);
return ret;
}
+11
View File
@@ -0,0 +1,11 @@
#ifndef LOAD_SHLIB_H
#define LOAD_SHLIB_H
typedef int(*initfunc_t)(void);
#ifdef LOAD_MODULE_SHLIB_MAIN
#else
extern int load_module_shlib(char *modname);
#endif
#endif
+2 -1
View File
@@ -8,8 +8,9 @@ ELSE()
ENDIF()
set(APPROOT . )
set(OPENAIR_DIR $ENV{OPENAIR_DIR})
set(APPROOT ${OPENAIR_DIR}/common/utils/telnetsrv )
set(OPENAIR_BUILD_DIR $ENV{OPENAIR_DIR}/cmake_targets)
set(OPENAIR1_DIR $ENV{OPENAIR1_DIR})
set(OPENAIR2_DIR $ENV{OPENAIR2_DIR})
-45
View File
@@ -1,45 +0,0 @@
/* FT NOKBLF:
* this source is to be linked with the program using the telnet server, it looks for
* the telnet server dynamic library, possibly loads it and calls the telnet server
* init functions
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <dlfcn.h>
#include "telnetsrv.h"
#include "openair1/PHY/defs.h"
int load_telnet(void)
{
void *lib_handle;
initfunc_t fpi;
lib_handle = dlopen(TELNETSRV_SHAREDLIB, RTLD_LAZY|RTLD_NODELETE|RTLD_GLOBAL);
if (!lib_handle)
{
printf("[TELNETSRV] telnet server is not loaded: %s\n", dlerror());
return -1;
}
fpi = dlsym(lib_handle,"init_telnetsrv");
if (fpi != NULL )
{
fpi(cfgfile);
}
else
{
fprintf(stderr,"[TELNETSRV] %s %d Telnet server init function not found %s\n",__FILE__, __LINE__, dlerror());
return -1;
}
dlclose(lib_handle);
return 0;
}
-5
View File
@@ -1,5 +0,0 @@
#ifndef TELNET_LOAD_H
#define TELNET_LOAD_H
#include "telnetsrv.h"
extern int load_telnet(void);
#endif
+2 -2
View File
@@ -1,7 +1,7 @@
#ifndef TELNETSRV_H
#define TELNETSRV_H
#define TELNETSRV_SHAREDLIB "libtelnetsrv.so"
#define TELNETSRV_MODNAME "telnetsrv"
#define TELNET_PORT 9090
#define TELNET_MAX_MSGLENGTH 2048
@@ -80,7 +80,7 @@ typedef struct {
typedef int(*addcmdfunc_t)(char*, telnetshell_vardef_t*, telnetshell_cmddef_t*);
typedef int(*initfunc_t)(char *cfgfile);
typedef void(*settelnetmodule_t)(char *name, void *ptr);
/*-------------------------------------------------------------------------------------------*/