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.
master
Andre Puschmann 5 years ago
parent 4e3e537982
commit ac17ec6452

@ -25,6 +25,8 @@
#include "common.h"
#include <fstream>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
bool config_exists(std::string& filename, std::string default_name)
{

@ -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 <signal.h>
#include <stdio.h>
#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

@ -27,6 +27,7 @@
#include "srslte/common/config_file.h"
#include "srslte/common/crash_handler.h"
#include "srslte/common/signal_handler.h"
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
@ -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<enb_metrics_t> 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;

@ -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 <boost/program_options.hpp>
#include <iostream>
#include <signal.h>
@ -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*/

@ -19,27 +19,24 @@
*
*/
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <fstream>
#include <iostream>
#include <string>
#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 <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
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 = {};

Loading…
Cancel
Save