From 3b824c9067bd2a456f5fa0eecd33c65f4b2c2ef2 Mon Sep 17 00:00:00 2001
From: frtabu <francois.taburet@nokia-bell-labs.com>
Date: Sat, 7 Oct 2017 00:35:23 +0200
Subject: [PATCH] telnet server and shared library loader enhancement

---
 common/utils/load_module_shlib.c           | 91 ++++++++++++++++++----
 common/utils/load_module_shlib.h           | 56 ++++++++++++-
 common/utils/telnetsrv/telnetsrv.c         |  2 +-
 common/utils/telnetsrv/telnetsrv.h         | 31 ++++++++
 common/utils/telnetsrv/telnetsrv_phycmd.c  | 33 +++++++-
 common/utils/telnetsrv/telnetsrv_phycmd.h  | 30 +++++++
 common/utils/telnetsrv/telnetsrv_proccmd.c | 31 ++++++++
 common/utils/telnetsrv/telnetsrv_proccmd.h |  2 +-
 targets/RT/USER/lte-softmodem.c            |  2 +-
 9 files changed, 257 insertions(+), 21 deletions(-)

diff --git a/common/utils/load_module_shlib.c b/common/utils/load_module_shlib.c
index 27e7ba8b099..b21f39e69df 100644
--- a/common/utils/load_module_shlib.c
+++ b/common/utils/load_module_shlib.c
@@ -1,9 +1,34 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */
 
-/* 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
-*/
+/*! \file common/utils/load_module_shlib.c
+ * \brief shared library loader implementation
+ * \author Francois TABURET
+ * \date 2017
+ * \version 0.1
+ * \company NOKIA BellLabs France
+ * \email: francois.taburet@nokia-bell-labs.com
+ * \note
+ * \warning
+ */
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
@@ -13,37 +38,71 @@
 #include <dlfcn.h>
 #include "openair1/PHY/defs.h"
 #define LOAD_MODULE_SHLIB_MAIN
+
+#include "common/config/config_userapi.h"
 #include "load_module_shlib.h"
-int load_module_shlib(char *modname)
+void loader_init(void) {
+  paramdef_t LoaderParams[] = LOADER_PARAMS_DESC;
+
+
+  int ret = config_get( LoaderParams,sizeof(LoaderParams)/sizeof(paramdef_t),LOADER_CONFIG_PREFIX);
+  if (ret <0) {
+       fprintf(stderr,"[LOADER] configuration couldn't be performed");
+       if (loader_data.shlibpath == NULL) {
+         loader_data.shlibpath=DEFAULT_PATH;
+        }
+       return;
+  }   
+}
+
+int load_module_shlib(char *modname,loader_shlibfunc_t *farray, int numf)
 {
    void *lib_handle;
    initfunc_t fpi;
    char *tmpstr;
    int ret=0;
- 
-   tmpstr = malloc(strlen(modname)+16);
+
+   if (loader_data.shlibpath  == NULL) {
+      loader_init();
+   }
+   tmpstr = malloc(strlen(loader_data.shlibpath)+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);
+
+   if(loader_data.shlibpath[0] != 0) {
+       ret=sprintf(tmpstr,"%s/",loader_data.shlibpath);
+   }
+   if(strstr(modname,".so") == NULL) {
+      sprintf(tmpstr+ret,"lib%s.so",modname);
+   } else {
+      sprintf(tmpstr+ret,"%s",modname);   
+   } 
+   ret = 0;
    lib_handle = dlopen(tmpstr, RTLD_LAZY|RTLD_NODELETE|RTLD_GLOBAL);
    if (!lib_handle) {
-      printf("[LOADER] library %s is not loaded: %s\n", tmpstr,dlerror());
+      fprintf(stderr,"[LOADER] library %s is not loaded: %s\n", tmpstr,dlerror());
       ret = -1;
    } else {
-      sprintf(tmpstr,"init_%s",modname);
+      printf("[LOADER] library %s uccessfully loaded loaded\n", tmpstr);
+      sprintf(tmpstr,"%s_autoinit",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 (farray != NULL) {
+          for (int i=0; i<numf; i++) {
+	      farray[i].fptr = dlsym(lib_handle,farray[i].fname);
+	      if (farray[i].fptr == NULL ) {
+	          fprintf(stderr,"[LOADER] %s %d %s function not found %s\n",__FILE__, __LINE__, dlerror(),farray[i].fname);
+               ret= -1;
+	      }
+	  } /* for int i... */
+      }	 /* farray ! NULL */
     } 
 	  	 
    if (tmpstr != NULL) free(tmpstr);
diff --git a/common/utils/load_module_shlib.h b/common/utils/load_module_shlib.h
index 2db1d17ec0c..1f991dddd2c 100644
--- a/common/utils/load_module_shlib.h
+++ b/common/utils/load_module_shlib.h
@@ -1,11 +1,65 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */
+
+/*! \file common/utils/load_module_shlib.h
+ * \brief include file for users of the shared lib loader
+ * \author Francois TABURET
+ * \date 2017
+ * \version 0.1
+ * \company NOKIA BellLabs France
+ * \email: francois.taburet@nokia-bell-labs.com
+ * \note
+ * \warning
+ */
 #ifndef LOAD_SHLIB_H
 #define LOAD_SHLIB_H
 
 
 typedef int(*initfunc_t)(void);
+
+typedef struct {
+   char *shlibpath;
+}loader_data_t;
+
+typedef struct {
+   char *fname;
+   int (*fptr)(void);
+}loader_shlibfunc_t;
 #ifdef LOAD_MODULE_SHLIB_MAIN
+#define LOADER_CONFIG_PREFIX  "loader"
+#define DEFAULT_PATH ""
+loader_data_t loader_data;
+
+/*--------------------------------------------------------------------------------------------------------------------------------------*/
+/*                                       LOADER parameters                                                                              */
+/*   optname               helpstr   paramflags    XXXptr	                           defXXXval	            type       numelt   */
+/*--------------------------------------------------------------------------------------------------------------------------------------*/
+#define LOADER_PARAMS_DESC { \
+{"shlibpath",                NULL,    0,          strptr:(char **)&(loader_data.shlibpath), defstrval:DEFAULT_PATH, TYPE_STRING, 0} \
+}
+
+/*-------------------------------------------------------------------------------------------------------------*/
 #else
-extern int load_module_shlib(char *modname);
+extern int load_module_shlib(char *modname, loader_shlibfunc_t *farray, int numf);
 #endif
 
 #endif
+
diff --git a/common/utils/telnetsrv/telnetsrv.c b/common/utils/telnetsrv/telnetsrv.c
index 6d6b84f5684..f99a83f3218 100644
--- a/common/utils/telnetsrv/telnetsrv.c
+++ b/common/utils/telnetsrv/telnetsrv.c
@@ -19,7 +19,7 @@
  *      contact@openairinterface.org
  */
 
-/*! \file common/utils/telnetsrv.c
+/*! \file common/utils/telnetsrv/telnetsrv.c
  * \brief: implementation of a telnet server
  * \author Francois TABURET
  * \date 2017
diff --git a/common/utils/telnetsrv/telnetsrv.h b/common/utils/telnetsrv/telnetsrv.h
index f54557e751d..c8cad58784d 100644
--- a/common/utils/telnetsrv/telnetsrv.h
+++ b/common/utils/telnetsrv/telnetsrv.h
@@ -1,3 +1,34 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */
+
+/*! \file common/utils/telnetsrv/telnetsrv.h
+ * \brief: include file for telnet server implementation
+ * \author Francois TABURET
+ * \date 2017
+ * \version 0.1
+ * \company NOKIA BellLabs France
+ * \email: francois.taburet@nokia-bell-labs.com
+ * \note
+ * \warning
+ */
 #ifndef TELNETSRV_H
 #define TELNETSRV_H
 
diff --git a/common/utils/telnetsrv/telnetsrv_phycmd.c b/common/utils/telnetsrv/telnetsrv_phycmd.c
index 1e267f68901..6867d5deeef 100644
--- a/common/utils/telnetsrv/telnetsrv_phycmd.c
+++ b/common/utils/telnetsrv/telnetsrv_phycmd.c
@@ -1,3 +1,34 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */
+
+/*! \file common/utils/telnetsrv/telnetsrv_phycmd.c
+ * \brief: implementation of telnet commands related to softmodem linux process
+ * \author Francois TABURET
+ * \date 2017
+ * \version 0.1
+ * \company NOKIA BellLabs France
+ * \email: francois.taburet@nokia-bell-labs.com
+ * \note
+ * \warning
+ */
 #define _GNU_SOURCE 
 #include <string.h>
 #include <pthread.h>
@@ -89,7 +120,7 @@ int dump_phyvars(char *buf, int debug, telnet_printfunc_t prnt)
    if (strcasestr(buf,"uedump") != NULL)
        {
        dump_uestats(debug, prnt,1);
-       }      
+       }           
    return 0;
 }
 
diff --git a/common/utils/telnetsrv/telnetsrv_phycmd.h b/common/utils/telnetsrv/telnetsrv_phycmd.h
index 7289810c8a9..3922bda727c 100644
--- a/common/utils/telnetsrv/telnetsrv_phycmd.h
+++ b/common/utils/telnetsrv/telnetsrv_phycmd.h
@@ -1,4 +1,34 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */
 
+/*! \file common/utils/telnetsrv_proccmd.h
+ * \brief: Include file defining telnet commands related to softmodem linux process
+ * \author Francois TABURET
+ * \date 2017
+ * \version 0.1
+ * \company NOKIA BellLabs France
+ * \email: francois.taburet@nokia-bell-labs.com
+ * \note
+ * \warning
+ */
 
 #ifdef TELNETSRV_PHYCMD_MAIN
 
diff --git a/common/utils/telnetsrv/telnetsrv_proccmd.c b/common/utils/telnetsrv/telnetsrv_proccmd.c
index bd409ab27cd..82c4549faea 100644
--- a/common/utils/telnetsrv/telnetsrv_proccmd.c
+++ b/common/utils/telnetsrv/telnetsrv_proccmd.c
@@ -1,3 +1,34 @@
+/*
+ * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The OpenAirInterface Software Alliance licenses this file to You under
+ * the OAI Public License, Version 1.0  (the "License"); you may not use this file
+ * except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.openairinterface.org/?page_id=698
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *-------------------------------------------------------------------------------
+ * For more information about the OpenAirInterface (OAI) Software Alliance:
+ *      contact@openairinterface.org
+ */
+
+/*! \file common/utils/telnetsrv/telnetsrv_proccmd.c
+ * \brief: implementation of telnet commands related to this linux process
+ * \author Francois TABURET
+ * \date 2017
+ * \version 0.1
+ * \company NOKIA BellLabs France
+ * \email: francois.taburet@nokia-bell-labs.com
+ * \note
+ * \warning
+ */
 #define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/common/utils/telnetsrv/telnetsrv_proccmd.h b/common/utils/telnetsrv/telnetsrv_proccmd.h
index 60eaa2e4062..d7fd0a6e249 100644
--- a/common/utils/telnetsrv/telnetsrv_proccmd.h
+++ b/common/utils/telnetsrv/telnetsrv_proccmd.h
@@ -19,7 +19,7 @@
  *      contact@openairinterface.org
  */
 
-/*! \file common/utils/telnetsrv_proccmd.h
+/*! \file common/utils/telnetsrv/telnetsrv_proccmd.h
  * \brief: Include file defining telnet commands related to this linux process
  * \author Francois TABURET
  * \date 2017
diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c
index 58a1a18af2d..a8cc3e84d94 100644
--- a/targets/RT/USER/lte-softmodem.c
+++ b/targets/RT/USER/lte-softmodem.c
@@ -588,7 +588,7 @@ static void get_options(void) {
       set_glog(-1, glog_verbosity);
   }
   if (start_telnetsrv) {
-     load_module_shlib("telnetsrv");
+     load_module_shlib("telnetsrv",NULL,0);
   }
 
   
-- 
GitLab