From ac17ec6452489a0e9b7e6452f4e096ee9a78d5bb Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Mon, 16 Mar 2020 11:34:21 +0100 Subject: [PATCH] add common signal_handler for srsUE/srsENB/srsEPC the signal handler is the same for all three apps. The "running" flag as well as the file_logger object are in the common header in order to allow the signal handler to flush the file if the alarm goes off. --- lib/include/srslte/common/config_file.h | 2 + lib/include/srslte/common/signal_handler.h | 75 ++++++++++++++++++++++ srsenb/src/main.cc | 20 +----- srsepc/src/main.cc | 13 +--- srsue/src/main.cc | 46 +++---------- 5 files changed, 92 insertions(+), 64 deletions(-) create mode 100644 lib/include/srslte/common/signal_handler.h diff --git a/lib/include/srslte/common/config_file.h b/lib/include/srslte/common/config_file.h index 10709ea9b..4fb849a85 100644 --- a/lib/include/srslte/common/config_file.h +++ b/lib/include/srslte/common/config_file.h @@ -25,6 +25,8 @@ #include "common.h" #include #include +#include +#include bool config_exists(std::string& filename, std::string default_name) { diff --git a/lib/include/srslte/common/signal_handler.h b/lib/include/srslte/common/signal_handler.h new file mode 100644 index 000000000..fc7628d31 --- /dev/null +++ b/lib/include/srslte/common/signal_handler.h @@ -0,0 +1,75 @@ +/* + * Copyright 2013-2020 Software Radio Systems Limited + * + * This file is part of srsLTE. + * + * srsLTE is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * srsLTE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * A copy of the GNU Affero General Public License can be found in + * the LICENSE file in the top-level directory of this distribution + * and at http://www.gnu.org/licenses/. + * + */ + +/** + * @file signal_handler.h + * @brief Common signal handling methods for all srsLTE applications. + */ + +#ifndef SRSLTE_SIGNAL_HANDLER_H +#define SRSLTE_SIGNAL_HANDLER_H + +#include "srslte/common/logger_file.h" +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define SRSLTE_TERM_TIMEOUT_S (3) + +// static vars required by signal handling +static srslte::logger_file logger_file; +static bool running = true; + +static void srslte_signal_handler(int signal) +{ + switch (signal) { + case SIGALRM: + fprintf(stderr, "Couldn't stop after %ds. Forcing exit.\n", SRSLTE_TERM_TIMEOUT_S); + logger_file.stop(); + exit(-1); + default: + // all other registered signals try to stop the app gracefully + if (running) { + running = false; + fprintf(stdout, "Stopping ..\n"); + alarm(SRSLTE_TERM_TIMEOUT_S); + } else { + // already waiting for alarm to go off .. + } + break; + } +} + +void srslte_register_signal_handler() +{ + signal(SIGINT, srslte_signal_handler); + signal(SIGTERM, srslte_signal_handler); + signal(SIGHUP, srslte_signal_handler); + signal(SIGALRM, srslte_signal_handler); +} + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // SRSLTE_SIGNAL_HANDLER_H diff --git a/srsenb/src/main.cc b/srsenb/src/main.cc index d929e40c2..5b7f042d9 100644 --- a/srsenb/src/main.cc +++ b/srsenb/src/main.cc @@ -27,6 +27,7 @@ #include "srslte/common/config_file.h" #include "srslte/common/crash_handler.h" +#include "srslte/common/signal_handler.h" #include #include @@ -340,20 +341,8 @@ void parse_args(all_args_t* args, int argc, char* argv[]) } } -static int sigcnt = 0; -static bool running = true; static bool do_metrics = false; -void sig_int_handler(int signo) -{ - sigcnt++; - running = false; - cout << "Stopping srsENB... Press Ctrl+C " << (10 - sigcnt) << " more times to force stop" << endl; - if (sigcnt >= 10) { - exit(-1); - } -} - void* input_loop(void* m) { metrics_stdout* metrics = (metrics_stdout*)m; @@ -373,7 +362,7 @@ void* input_loop(void* m) } metrics->toggle_print(do_metrics); } else if ('q' == key) { - sig_int_handler(0); + raise(SIGTERM); } } } @@ -382,9 +371,7 @@ void* input_loop(void* m) int main(int argc, char* argv[]) { - signal(SIGINT, sig_int_handler); - signal(SIGTERM, sig_int_handler); - signal(SIGHUP, sig_int_handler); + srslte_register_signal_handler(); all_args_t args = {}; srslte::metrics_hub metricshub; metrics_stdout metrics_screen; @@ -395,7 +382,6 @@ int main(int argc, char* argv[]) parse_args(&args, argc, argv); srslte::logger_stdout logger_stdout; - srslte::logger_file logger_file; // Set logger srslte::logger* logger = nullptr; diff --git a/srsepc/src/main.cc b/srsepc/src/main.cc index 5aa75fb81..b38652656 100644 --- a/srsepc/src/main.cc +++ b/srsepc/src/main.cc @@ -26,6 +26,7 @@ #include "srslte/common/bcd_helpers.h" #include "srslte/common/config_file.h" #include "srslte/common/crash_handler.h" +#include "srslte/common/signal_handler.h" #include #include #include @@ -34,13 +35,6 @@ using namespace std; using namespace srsepc; namespace bpo = boost::program_options; -bool running = true; - -void sig_int_handler(int signo) -{ - running = false; -} - typedef struct { std::string nas_level; int nas_hex_limit; @@ -380,9 +374,7 @@ std::string get_build_string() int main(int argc, char* argv[]) { - signal(SIGINT, sig_int_handler); - signal(SIGTERM, sig_int_handler); - signal(SIGHUP, sig_int_handler); + srslte_register_signal_handler(); // print build info cout << endl << get_build_string() << endl; @@ -394,7 +386,6 @@ int main(int argc, char* argv[]) parse_args(&args, argc, argv); srslte::logger_stdout logger_stdout; - srslte::logger_file logger_file; srslte::logger* logger; /*Init logger*/ diff --git a/srsue/src/main.cc b/srsue/src/main.cc index 0a2b9ee0b..a7ac1cb0c 100644 --- a/srsue/src/main.cc +++ b/srsue/src/main.cc @@ -19,27 +19,24 @@ * */ -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - #include "srslte/common/config_file.h" #include "srslte/common/crash_handler.h" #include "srslte/common/logmap.h" #include "srslte/common/metrics_hub.h" +#include "srslte/common/signal_handler.h" #include "srslte/srslte.h" #include "srslte/version.h" #include "srsue/hdr/metrics_csv.h" #include "srsue/hdr/metrics_stdout.h" #include "srsue/hdr/ue.h" +#include +#include +#include +#include +#include +#include +#include +#include extern bool simulate_rlf; @@ -51,10 +48,6 @@ namespace bpo = boost::program_options; * Local static variables ***********************************************************************/ -#define UE_STOP_TIMEOUT_S (3) - -static srslte::logger_file logger_file; -static bool running = true; static bool do_metrics = false; static metrics_stdout* metrics_screen = nullptr; @@ -550,22 +543,6 @@ static int parse_args(all_args_t* args, int argc, char* argv[]) return SRSLTE_SUCCESS; } -static void sig_handler(int signal) -{ - switch (signal) { - case SIGALRM: - fprintf(stderr, "Couldn't stop after %ds. Forcing exit.\n", UE_STOP_TIMEOUT_S); - logger_file.stop(); - exit(-1); - default: - // all other registered signal try to stop UE gracefully - running = false; - cout << "Stopping srsUE ..." << endl; - alarm(UE_STOP_TIMEOUT_S); - break; - } -} - static void* input_loop(void*) { string key; @@ -599,10 +576,7 @@ static void* input_loop(void*) int main(int argc, char* argv[]) { - signal(SIGINT, sig_handler); - signal(SIGTERM, sig_handler); - signal(SIGHUP, sig_handler); - signal(SIGALRM, sig_handler); + srslte_register_signal_handler(); srslte_debug_handle_crash(argc, argv); all_args_t args = {};