From e266bd48aca6f5b7831eb7c44e4773e9884d4c56 Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Fri, 13 Jul 2012 14:00:21 +0200 Subject: Adding signal handler to allow clean exit of PCU The signal handler will end the main loop, so clean exit is performed. The allocated memory is dumped in order to detect memory leaks. All talloc functions use tall_pcu_ctx context instead of NULL, to track memory leaks. --- src/pcu_main.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'src/pcu_main.cpp') diff --git a/src/pcu_main.cpp b/src/pcu_main.cpp index 2a01b9b0..2c6d7638 100644 --- a/src/pcu_main.cpp +++ b/src/pcu_main.cpp @@ -25,6 +25,7 @@ #include #include #include +#include extern "C" { #include "pcu_vty.h" #include @@ -38,6 +39,7 @@ static int config_given = 0; static const char *config_file = "osmo-pcu.cfg"; extern struct vty_app_info pcu_vty_info; void *tall_pcu_ctx; +static int quit = 0; static void print_help() { @@ -93,6 +95,39 @@ static void handle_options(int argc, char **argv) } } +void sighandler(int sigset) +{ + if (sigset == SIGHUP || sigset == SIGPIPE) + return; + + fprintf(stderr, "Signal %d received.\n", sigset); + + switch (sigset) { + case SIGINT: + /* If another signal is received afterwards, the program + * is terminated without finishing shutdown process. + */ + signal(SIGINT, SIG_DFL); + signal(SIGHUP, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGPIPE, SIG_DFL); + signal(SIGABRT, SIG_DFL); + signal(SIGUSR1, SIG_DFL); + signal(SIGUSR2, SIG_DFL); + + quit = 1; + break; + case SIGABRT: + /* in case of abort, we want to obtain a talloc report + * and then return to the caller, who will abort the process + */ + case SIGUSR1: + case SIGUSR2: + talloc_report_full(tall_pcu_ctx, stderr); + break; + } +} + int main(int argc, char *argv[]) { struct gprs_rlcmac_bts *bts; @@ -118,6 +153,8 @@ int main(int argc, char *argv[]) bts->n3103 = 4; bts->n3105 = 8; + msgb_set_talloc_ctx(tall_pcu_ctx); + osmo_init_logging(&gprs_log_info); vty_init(&pcu_vty_info); @@ -151,8 +188,15 @@ int main(int argc, char *argv[]) if (rc < 0) return rc; - while (1) - { + signal(SIGINT, sighandler); + signal(SIGHUP, sighandler); + signal(SIGTERM, sighandler); + signal(SIGPIPE, sighandler); + signal(SIGABRT, sighandler); + signal(SIGUSR1, sighandler); + signal(SIGUSR2, sighandler); + + while (!quit) { osmo_gsm_timers_check(); osmo_gsm_timers_prepare(); osmo_gsm_timers_update(); @@ -160,8 +204,13 @@ int main(int argc, char *argv[]) osmo_select_main(0); } + telnet_exit(); + pcu_l1if_close(); + talloc_free(gprs_rlcmac_bts); + + talloc_report_full(tall_pcu_ctx, stderr); talloc_free(tall_pcu_ctx); return 0; -- cgit v1.2.3