From abbffef0151fab4f15eed63876154c8c40aaf978 Mon Sep 17 00:00:00 2001
From: laurent <laurent.thomas@open-cells.com>
Date: Wed, 5 Sep 2018 17:03:19 +0200
Subject: [PATCH] fix some exit situations, that was badly done by SIGUSR1
 handler

---
 common/config/config_cmdline.c                   | 1 +
 common/config/config_userapi.c                   | 2 +-
 common/config/config_userapi.h                   | 2 --
 common/utils/ocp_itti/intertask_interface.cpp    | 2 +-
 common/utils/telnetsrv/telnetsrv_proccmd.c       | 2 --
 openair1/PHY/defs_common.h                       | 1 -
 openair1/SIMULATION/LTE_PHY/unitary_defs.h       | 6 +++++-
 openair2/COMMON/platform_types.h                 | 3 +++
 openair2/ENB_APP/flexran_agent_common_internal.c | 2 +-
 targets/RT/USER/lte-enb.c                        | 1 -
 targets/RT/USER/lte-softmodem.c                  | 8 ++++----
 targets/RT/USER/lte-softmodem.h                  | 1 -
 targets/RT/USER/lte-uesoftmodem.c                | 7 ++++---
 13 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/common/config/config_cmdline.c b/common/config/config_cmdline.c
index 8b3026c22b..47371ea568 100644
--- a/common/config/config_cmdline.c
+++ b/common/config/config_cmdline.c
@@ -35,6 +35,7 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <errno.h>
+#include <platform_types.h>
 #include "config_userapi.h"
 
 
diff --git a/common/config/config_userapi.c b/common/config/config_userapi.c
index 3f3651aa01..57a680044b 100644
--- a/common/config/config_userapi.c
+++ b/common/config/config_userapi.c
@@ -37,8 +37,8 @@
 #include <dlfcn.h>
 #include <arpa/inet.h>
 
+#include <platform_types.h>
 #include "config_userapi.h"
-extern void exit_fun(const char* s);  // lte-softmodem clean exit function
 
 
 configmodule_interface_t *config_get_if(void)
diff --git a/common/config/config_userapi.h b/common/config/config_userapi.h
index 217310db36..25306b8778 100644
--- a/common/config/config_userapi.h
+++ b/common/config/config_userapi.h
@@ -38,8 +38,6 @@
 extern "C"
 {
 #endif
-/* get rid of "exit_fun undeclared" warning */
-extern void exit_fun(const char* s);
 
 #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      )
diff --git a/common/utils/ocp_itti/intertask_interface.cpp b/common/utils/ocp_itti/intertask_interface.cpp
index a4b280f86e..7222161a7e 100644
--- a/common/utils/ocp_itti/intertask_interface.cpp
+++ b/common/utils/ocp_itti/intertask_interface.cpp
@@ -288,7 +288,7 @@ extern "C" {
   void itti_terminate_tasks(task_id_t task_id) {
     // Sends Terminate signals to all tasks.
     itti_send_terminate_message (task_id);
-    pthread_exit (NULL);
+    usleep(100*1000); // Allow the tasks to receive the message before going returning to main thread
   }
 
   int itti_init(task_id_t task_max, thread_id_t thread_max, MessagesIds messages_id_max, const task_info_t *tasks_info,
diff --git a/common/utils/telnetsrv/telnetsrv_proccmd.c b/common/utils/telnetsrv/telnetsrv_proccmd.c
index 519c222114..cd82749057 100644
--- a/common/utils/telnetsrv/telnetsrv_proccmd.c
+++ b/common/utils/telnetsrv/telnetsrv_proccmd.c
@@ -302,8 +302,6 @@ char sv1[64];
 } 
 int proccmd_exit(char *buf, int debug, telnet_printfunc_t prnt)
 {
-extern void exit_fun(const char* s);   
-   
    if (debug > 0)
        prnt("process module received %s\n",buf);
 
diff --git a/openair1/PHY/defs_common.h b/openair1/PHY/defs_common.h
index d0cc7e9ca4..75e6d3fce3 100644
--- a/openair1/PHY/defs_common.h
+++ b/openair1/PHY/defs_common.h
@@ -930,7 +930,6 @@ typedef enum {
 #endif
 
 
-void exit_fun(const char* s);
 
 #include "common/utils/LOG/log_extern.h"
 extern pthread_cond_t sync_cond;
diff --git a/openair1/SIMULATION/LTE_PHY/unitary_defs.h b/openair1/SIMULATION/LTE_PHY/unitary_defs.h
index 133656a562..eea8e89fbe 100644
--- a/openair1/SIMULATION/LTE_PHY/unitary_defs.h
+++ b/openair1/SIMULATION/LTE_PHY/unitary_defs.h
@@ -22,7 +22,11 @@
 openair0_device openair0;
 volatile int oai_exit=0;
 
-void exit_fun(const char *s) { exit(-1); }
+void exit_function(const char* file, const char* function, const int line,const char *s) { 
+   const char * msg= s==NULL ? "no comment": s;
+   printf("Exiting at: %s:%d %s(), %s\n", file, line, function, msg); 
+   exit(-1); 
+}
 
 extern unsigned int dlsch_tbs25[27][25],TBStable[27][110];
 extern unsigned char offset_mumimo_llr_drange_fix;
diff --git a/openair2/COMMON/platform_types.h b/openair2/COMMON/platform_types.h
index 281092c2ca..0f9fc34f83 100644
--- a/openair2/COMMON/platform_types.h
+++ b/openair2/COMMON/platform_types.h
@@ -285,4 +285,7 @@ typedef struct protocol_ctxt_s {
     (CTXT_Pp)->rnti
 
 #define CHECK_CTXT_ARGS(CTXT_Pp)
+
+#define exit_fun(msg) exit_function(__FILE__,__FUNCTION__,__LINE__,msg)
+void exit_function(const char* file, const char* function, const int line, const char* s);
 #endif
diff --git a/openair2/ENB_APP/flexran_agent_common_internal.c b/openair2/ENB_APP/flexran_agent_common_internal.c
index 8874a0f648..e9fc39044d 100644
--- a/openair2/ENB_APP/flexran_agent_common_internal.c
+++ b/openair2/ENB_APP/flexran_agent_common_internal.c
@@ -68,7 +68,7 @@ void handle_reconfiguration(mid_t mod_id)
 
   if (restart_L1L2(mod_id) < 0) {
     LOG_F(ENB_APP, "can not restart, killing lte-softmodem\n");
-    itti_terminate_tasks(TASK_PHY_ENB);
+    exit_fun("can not restart L1L2, killing lte-softmodem");
     return;
   }
 
diff --git a/targets/RT/USER/lte-enb.c b/targets/RT/USER/lte-enb.c
index 594ce2d3c7..57f6f9f569 100644
--- a/targets/RT/USER/lte-enb.c
+++ b/targets/RT/USER/lte-enb.c
@@ -141,7 +141,6 @@ static struct {
 
 extern double cpuf;
 
-void exit_fun(const char* s);
 
 void init_eNB(int,int);
 void stop_eNB(int nb_inst);
diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c
index a7f1cfd1c7..3cb9f4f0b8 100644
--- a/targets/RT/USER/lte-softmodem.c
+++ b/targets/RT/USER/lte-softmodem.c
@@ -319,13 +319,13 @@ void signal_handler(int sig) {
 
 
 
-void exit_fun(const char* s)
+void exit_function(const char* file, const char* function, const int line, const char* s)
 {
 
   int ru_id;
 
   if (s != NULL) {
-    printf("%s %s() Exiting OAI softmodem: %s\n",__FILE__, __FUNCTION__, s);
+    printf("%s:%d %s() Exiting OAI softmodem: %s\n",file,line, function, s);
   }
 
   oai_exit = 1;
@@ -345,11 +345,11 @@ void exit_fun(const char* s)
     }
 
 
-#if defined(ENABLE_ITTI)
     sleep(1); //allow lte-softmodem threads to exit first
+#if defined(ENABLE_ITTI)
     itti_terminate_tasks (TASK_UNKNOWN);
 #endif
-
+   exit(1);
 
 }
 
diff --git a/targets/RT/USER/lte-softmodem.h b/targets/RT/USER/lte-softmodem.h
index 68f24a1f0c..d8689b9ffb 100644
--- a/targets/RT/USER/lte-softmodem.h
+++ b/targets/RT/USER/lte-softmodem.h
@@ -240,7 +240,6 @@ extern volatile int             start_UE;
 #include "threads_t.h"
 extern threads_t threads;
 
-extern void exit_fun(const char* s);
 // In lte-enb.c
 extern void init_eNB(int single_thread_flag,int wait_for_sync);
 extern void stop_eNB(int);
diff --git a/targets/RT/USER/lte-uesoftmodem.c b/targets/RT/USER/lte-uesoftmodem.c
index 6a1199bf1a..9050340918 100644
--- a/targets/RT/USER/lte-uesoftmodem.c
+++ b/targets/RT/USER/lte-uesoftmodem.c
@@ -322,13 +322,13 @@ void signal_handler(int sig) {
 
 
 
-void exit_fun(const char* s)
+void exit_function(const char* file, const char* function, const int line, const char* s)
 {
   int CC_id;
 
   logClean();
   if (s != NULL) {
-    printf("%s %s() Exiting OAI softmodem: %s\n",__FILE__, __FUNCTION__, s);
+    printf("%s:%d %s() Exiting OAI softmodem: %s\n",file,line, function, s);
   }
 
   oai_exit = 1;
@@ -341,10 +341,11 @@ void exit_fun(const char* s)
 	        PHY_vars_UE_g[0][CC_id]->rfdevice.trx_end_func(&PHY_vars_UE_g[0][CC_id]->rfdevice);
     }
 
-#if defined(ENABLE_ITTI)
     sleep(1); //allow lte-softmodem threads to exit first
+#if defined(ENABLE_ITTI)
     itti_terminate_tasks (TASK_UNKNOWN);
 #endif
+  exit(1);
 }
 
 #ifdef XFORMS
-- 
GitLab