srsLTE/lib/include/srsran/common/signal_handler.h

72 lines
1.6 KiB
C
Raw Normal View History

/**
*
* \section COPYRIGHT
*
2021-03-19 03:45:56 -07:00
* 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
2021-03-19 03:45:56 -07:00
* @brief Common signal handling methods for all srsRAN applications.
*/
2021-03-19 03:45:56 -07:00
#ifndef SRSRAN_SIGNAL_HANDLER_H
#define SRSRAN_SIGNAL_HANDLER_H
2021-03-19 03:45:56 -07:00
#include "srsran/srslog/sink.h"
#include "srsran/srslog/srslog.h"
#include <signal.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
2021-03-19 03:45:56 -07:00
#define SRSRAN_TERM_TIMEOUT_S (5)
// static vars required by signal handling
2021-01-28 03:17:18 -08:00
static srslog::sink* log_sink = nullptr;
static bool running = true;
2021-03-19 03:45:56 -07:00
static void srsran_signal_handler(int signal)
{
switch (signal) {
case SIGALRM:
2021-03-19 03:45:56 -07:00
fprintf(stderr, "Couldn't stop after %ds. Forcing exit.\n", SRSRAN_TERM_TIMEOUT_S);
2021-01-28 03:17:18 -08:00
srslog::flush();
//:TODO: refactor the sighandler, should not depend on log utilities
if (log_sink) {
log_sink->flush();
}
raise(SIGKILL);
default:
// all other registered signals try to stop the app gracefully
if (running) {
running = false;
fprintf(stdout, "Stopping ..\n");
2021-03-19 03:45:56 -07:00
alarm(SRSRAN_TERM_TIMEOUT_S);
} else {
// already waiting for alarm to go off ..
}
break;
}
}
2021-03-19 03:45:56 -07:00
void srsran_register_signal_handler()
{
2021-03-19 03:45:56 -07:00
signal(SIGINT, srsran_signal_handler);
signal(SIGTERM, srsran_signal_handler);
signal(SIGHUP, srsran_signal_handler);
signal(SIGALRM, srsran_signal_handler);
}
#ifdef __cplusplus
}
#endif // __cplusplus
2021-03-19 03:45:56 -07:00
#endif // SRSRAN_SIGNAL_HANDLER_H