diff --git a/lib/include/srsran/common/mac_pcap_base.h b/lib/include/srsran/common/mac_pcap_base.h index 544df52dc..f9394bfbd 100644 --- a/lib/include/srsran/common/mac_pcap_base.h +++ b/lib/include/srsran/common/mac_pcap_base.h @@ -31,6 +31,8 @@ public: mac_pcap_base(const mac_pcap_base& other) = delete; mac_pcap_base& operator=(const mac_pcap_base& other) = delete; + mac_pcap_base(mac_pcap_base&& other) = delete; + mac_pcap_base& operator=(mac_pcap_base&& other) = delete; ~mac_pcap_base(); void enable(bool enable); diff --git a/lib/include/srsran/common/s1ap_pcap.h b/lib/include/srsran/common/s1ap_pcap.h index c62797a2e..0e9cae421 100644 --- a/lib/include/srsran/common/s1ap_pcap.h +++ b/lib/include/srsran/common/s1ap_pcap.h @@ -24,6 +24,8 @@ public: s1ap_pcap(); s1ap_pcap(const s1ap_pcap& other) = delete; s1ap_pcap& operator=(const s1ap_pcap& other) = delete; + s1ap_pcap(s1ap_pcap&& other) = delete; + s1ap_pcap& operator=(s1ap_pcap&& other) = delete; void enable(); void open(const char* filename_); diff --git a/lib/include/srsran/common/emergency_handlers.h b/lib/include/srsran/support/emergency_handlers.h similarity index 100% rename from lib/include/srsran/common/emergency_handlers.h rename to lib/include/srsran/support/emergency_handlers.h diff --git a/lib/include/srsran/support/signal_handler.h b/lib/include/srsran/support/signal_handler.h new file mode 100644 index 000000000..e21440bf9 --- /dev/null +++ b/lib/include/srsran/support/signal_handler.h @@ -0,0 +1,27 @@ +/** + * + * \section COPYRIGHT + * + * Copyright 2013-2021 Software Radio Systems Limited + * + * By using this file, you agree to the terms and conditions set + * forth in the LICENSE file which can be found at the top level of + * the distribution. + * + */ + +/** + * @file signal_handler.h + * @brief Common signal handling methods for all srsRAN applications. + */ + +#ifndef SRSRAN_SIGNAL_HANDLER_H +#define SRSRAN_SIGNAL_HANDLER_H + +using srsran_signal_hanlder = void (*)(); + +/// Registers the specified function to be called when the user interrupts the program execution (eg: via Ctrl+C). +/// Passing a null function pointer disables the current installed handler. +void srsran_register_signal_handler(srsran_signal_hanlder handler); + +#endif // SRSRAN_SIGNAL_HANDLER_H diff --git a/lib/src/CMakeLists.txt b/lib/src/CMakeLists.txt index fb5f62463..d6bb50635 100644 --- a/lib/src/CMakeLists.txt +++ b/lib/src/CMakeLists.txt @@ -15,4 +15,5 @@ add_subdirectory(rlc) add_subdirectory(pdcp) add_subdirectory(gtpu) add_subdirectory(srslog) +add_subdirectory(support) add_subdirectory(system) diff --git a/lib/src/common/CMakeLists.txt b/lib/src/common/CMakeLists.txt index cc8d08470..811f25b9e 100644 --- a/lib/src/common/CMakeLists.txt +++ b/lib/src/common/CMakeLists.txt @@ -28,7 +28,6 @@ set(SOURCES arch_select.cc rlc_pcap.cc s1ap_pcap.cc security.cc - emergency_handlers.cc standard_streams.cc thread_pool.cc threads.c @@ -48,7 +47,7 @@ add_dependencies(srsran_common gen_build_info) add_executable(arch_select arch_select.cc) target_include_directories(srsran_common PUBLIC ${SEC_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR} ${BACKWARD_INCLUDE_DIRS}) -target_link_libraries(srsran_common srsran_phy srslog ${SEC_LIBRARIES} ${BACKWARD_LIBRARIES} ${SCTP_LIBRARIES}) +target_link_libraries(srsran_common srsran_phy support srslog ${SEC_LIBRARIES} ${BACKWARD_LIBRARIES} ${SCTP_LIBRARIES}) target_compile_definitions(srsran_common PRIVATE ${BACKWARD_DEFINITIONS}) INSTALL(TARGETS srsran_common DESTINATION ${LIBRARY_DIR}) diff --git a/lib/src/common/mac_pcap_base.cc b/lib/src/common/mac_pcap_base.cc index 48d825199..2a12110d7 100644 --- a/lib/src/common/mac_pcap_base.cc +++ b/lib/src/common/mac_pcap_base.cc @@ -11,9 +11,9 @@ */ #include "srsran/common/mac_pcap_base.h" -#include "srsran/common/emergency_handlers.h" #include "srsran/config.h" #include "srsran/phy/common/phy_common.h" +#include "srsran/support/emergency_handlers.h" #include namespace srsran { diff --git a/lib/src/common/s1ap_pcap.cc b/lib/src/common/s1ap_pcap.cc index e87e77ad9..3909673d4 100644 --- a/lib/src/common/s1ap_pcap.cc +++ b/lib/src/common/s1ap_pcap.cc @@ -11,9 +11,9 @@ */ #include "srsran/common/s1ap_pcap.h" -#include "srsran/common/emergency_handlers.h" #include "srsran/common/pcap.h" #include "srsran/srsran.h" +#include "srsran/support/emergency_handlers.h" #include namespace srsran { diff --git a/lib/src/support/CMakeLists.txt b/lib/src/support/CMakeLists.txt new file mode 100644 index 000000000..0be443c35 --- /dev/null +++ b/lib/src/support/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright 2013-2021 Software Radio Systems Limited +# +# By using this file, you agree to the terms and conditions set +# forth in the LICENSE file which can be found at the top level of +# the distribution. +# + +set(SOURCES emergency_handlers.cc + signal_handler.cc) + +add_library(support STATIC ${SOURCES}) diff --git a/lib/src/common/emergency_handlers.cc b/lib/src/support/emergency_handlers.cc similarity index 97% rename from lib/src/common/emergency_handlers.cc rename to lib/src/support/emergency_handlers.cc index 3377153ac..b4e2cdc9a 100644 --- a/lib/src/common/emergency_handlers.cc +++ b/lib/src/support/emergency_handlers.cc @@ -10,7 +10,7 @@ * */ -#include "srsran/common/emergency_handlers.h" +#include "srsran/support/emergency_handlers.h" #include "srsran/support/srsran_assert.h" namespace { diff --git a/lib/include/srsran/common/signal_handler.h b/lib/src/support/signal_handler.cc similarity index 52% rename from lib/include/srsran/common/signal_handler.h rename to lib/src/support/signal_handler.cc index 36e48ebb2..33620d6cc 100644 --- a/lib/include/srsran/common/signal_handler.h +++ b/lib/src/support/signal_handler.cc @@ -10,28 +10,17 @@ * */ -/** - * @file signal_handler.h - * @brief Common signal handling methods for all srsRAN applications. - */ - -#ifndef SRSRAN_SIGNAL_HANDLER_H -#define SRSRAN_SIGNAL_HANDLER_H - -#include "srsran/common/emergency_handlers.h" -#include "srsran/srslog/sink.h" -#include "srsran/srslog/srslog.h" -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus +#include "srsran/support/signal_handler.h" +#include "srsran/support/emergency_handlers.h" +#include +#include +#include +#include #define SRSRAN_TERM_TIMEOUT_S (5) -// static vars required by signal handling -static std::atomic running = {true}; +/// Handler called after the user interrupts the program. +static std::atomic user_handler; static void srsran_signal_handler(int signal) { @@ -42,26 +31,24 @@ static void srsran_signal_handler(int signal) raise(SIGKILL); default: // all other registered signals try to stop the app gracefully - if (running) { - running = false; - fprintf(stdout, "Stopping ..\n"); - alarm(SRSRAN_TERM_TIMEOUT_S); + // Call the user handler if present and remove it so that further signals are treated by the default handler. + if (auto handler = user_handler.exchange(nullptr)) { + handler(); } else { - // already waiting for alarm to go off .. + return; } + fprintf(stdout, "Stopping ..\n"); + alarm(SRSRAN_TERM_TIMEOUT_S); break; } } -void srsran_register_signal_handler() +void srsran_register_signal_handler(srsran_signal_hanlder handler) { + user_handler.store(handler); + signal(SIGINT, srsran_signal_handler); signal(SIGTERM, srsran_signal_handler); signal(SIGHUP, srsran_signal_handler); signal(SIGALRM, srsran_signal_handler); } - -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // SRSRAN_SIGNAL_HANDLER_H diff --git a/srsenb/src/CMakeLists.txt b/srsenb/src/CMakeLists.txt index 67224468b..e4e26793d 100644 --- a/srsenb/src/CMakeLists.txt +++ b/srsenb/src/CMakeLists.txt @@ -27,7 +27,7 @@ target_link_libraries(enb_cfg_parser srsran_common ${LIBCONFIGPP_LIBRARIES}) add_executable(srsenb main.cc enb.cc metrics_stdout.cc metrics_csv.cc metrics_json.cc) set(SRSENB_SOURCES srsenb_phy srsenb_stack srsenb_common srsenb_s1ap srsenb_upper srsenb_mac srsenb_rrc srslog system) -set(SRSRAN_SOURCES srsran_common srsran_mac srsran_phy srsran_gtpu srsran_rlc srsran_pdcp srsran_radio rrc_asn1 s1ap_asn1 enb_cfg_parser srslog system) +set(SRSRAN_SOURCES srsran_common srsran_mac srsran_phy srsran_gtpu srsran_rlc srsran_pdcp srsran_radio rrc_asn1 s1ap_asn1 enb_cfg_parser srslog support system) set(SRSENB_SOURCES ${SRSENB_SOURCES} srsgnb_stack srsgnb_ngap srsgnb_upper srsgnb_mac srsgnb_rrc) set(SRSRAN_SOURCES ${SRSRAN_SOURCES} rrc_nr_asn1 ngap_nr_asn1) diff --git a/srsenb/src/main.cc b/srsenb/src/main.cc index d2c69a83b..118444b1d 100644 --- a/srsenb/src/main.cc +++ b/srsenb/src/main.cc @@ -20,10 +20,11 @@ #include "srsran/common/common_helper.h" #include "srsran/common/config_file.h" #include "srsran/common/crash_handler.h" -#include "srsran/common/signal_handler.h" #include "srsran/common/tsan_options.h" #include "srsran/srslog/event_trace.h" #include "srsran/srslog/srslog.h" +#include "srsran/support/emergency_handlers.h" +#include "srsran/support/signal_handler.h" #include #include @@ -45,9 +46,10 @@ namespace bpo = boost::program_options; /********************************************************************** * Program arguments processing ***********************************************************************/ -string config_file; -static bool stdout_ts_enable = false; -static srslog::sink* log_sink = nullptr; +string config_file; +static bool stdout_ts_enable = false; +static srslog::sink* log_sink = nullptr; +static std::atomic running = {true}; void parse_args(all_args_t* args, int argc, char* argv[]) { @@ -545,9 +547,14 @@ static void emergency_cleanup_handler(void* data) srsran_dft_exit(); } +static void signal_handler() +{ + running = false; +} + int main(int argc, char* argv[]) { - srsran_register_signal_handler(); + srsran_register_signal_handler(signal_handler); add_emergency_cleanup_handler(emergency_cleanup_handler, nullptr); all_args_t args = {}; diff --git a/srsepc/src/CMakeLists.txt b/srsepc/src/CMakeLists.txt index fac9a9ca5..b88e0517c 100644 --- a/srsepc/src/CMakeLists.txt +++ b/srsepc/src/CMakeLists.txt @@ -31,6 +31,7 @@ target_link_libraries( srsepc srsepc_mme srsran_asn1 srsran_common srslog + support ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${SEC_LIBRARIES} diff --git a/srsepc/src/main.cc b/srsepc/src/main.cc index 62e2fce09..c2ace4cb2 100644 --- a/srsepc/src/main.cc +++ b/srsepc/src/main.cc @@ -18,9 +18,10 @@ #include "srsran/common/common_helper.h" #include "srsran/common/config_file.h" #include "srsran/common/crash_handler.h" -#include "srsran/common/signal_handler.h" #include "srsran/srslog/srslog.h" #include "srsran/srsran.h" +#include "srsran/support/emergency_handlers.h" +#include "srsran/support/signal_handler.h" #include #include @@ -55,7 +56,8 @@ typedef struct { log_args_t log_args; } all_args_t; -static srslog::sink* log_sink = nullptr; +static srslog::sink* log_sink = nullptr; +static std::atomic running = {true}; /********************************************************************** * Program arguments processing @@ -363,9 +365,14 @@ static void emergency_cleanup_handler(void* data) } } +static void signal_handler() +{ + running = false; +} + int main(int argc, char* argv[]) { - srsran_register_signal_handler(); + srsran_register_signal_handler(signal_handler); add_emergency_cleanup_handler(emergency_cleanup_handler, nullptr); // print build info diff --git a/srsue/src/CMakeLists.txt b/srsue/src/CMakeLists.txt index 7b37cff78..8b4043623 100644 --- a/srsue/src/CMakeLists.txt +++ b/srsue/src/CMakeLists.txt @@ -22,7 +22,7 @@ endif (RPATH) add_executable(srsue main.cc ue.cc metrics_stdout.cc metrics_csv.cc metrics_json.cc) set(SRSUE_SOURCES srsue_phy srsue_stack srsue_upper srsue_mac srsue_rrc srslog system) -set(SRSRAN_SOURCES srsran_common srsran_mac srsran_phy srsran_radio srsran_gtpu srsran_rlc srsran_pdcp rrc_asn1 srslog system) +set(SRSRAN_SOURCES srsran_common srsran_mac srsran_phy srsran_radio srsran_gtpu srsran_rlc srsran_pdcp rrc_asn1 srslog support system) set(SRSUE_SOURCES ${SRSUE_SOURCES} srsue_nr_stack srsue_rrc_nr srsue_mac_nr) set(SRSRAN_SOURCES ${SRSRAN_SOURCES} rrc_nr_asn1 ngap_nr_asn1) diff --git a/srsue/src/main.cc b/srsue/src/main.cc index 7a9de3f76..b80f8a292 100644 --- a/srsue/src/main.cc +++ b/srsue/src/main.cc @@ -15,11 +15,12 @@ #include "srsran/common/crash_handler.h" #include "srsran/common/metrics_hub.h" #include "srsran/common/multiqueue.h" -#include "srsran/common/signal_handler.h" #include "srsran/common/tsan_options.h" #include "srsran/srslog/event_trace.h" #include "srsran/srslog/srslog.h" #include "srsran/srsran.h" +#include "srsran/support/emergency_handlers.h" +#include "srsran/support/signal_handler.h" #include "srsran/version.h" #include "srsue/hdr/metrics_csv.h" #include "srsue/hdr/metrics_json.h" @@ -27,6 +28,7 @@ #include "srsue/hdr/ue.h" #include #include +#include #include #include #include @@ -45,9 +47,10 @@ namespace bpo = boost::program_options; * Local static variables ***********************************************************************/ -static bool do_metrics = false; -static metrics_stdout* metrics_screen = nullptr; -static srslog::sink* log_sink = nullptr; +static bool do_metrics = false; +static metrics_stdout* metrics_screen = nullptr; +static srslog::sink* log_sink = nullptr; +static std::atomic running = {true}; /********************************************************************** * Program arguments processing @@ -667,9 +670,14 @@ static void emergency_cleanup_handler(void* data) srsran_dft_exit(); } +static void signal_handler() +{ + running = false; +} + int main(int argc, char* argv[]) { - srsran_register_signal_handler(); + srsran_register_signal_handler(signal_handler); add_emergency_cleanup_handler(emergency_cleanup_handler, nullptr); srsran_debug_handle_crash(argc, argv);