From 2a4abc62b24f0af31e171508dd648690a9b0667a Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sun, 13 Nov 2016 06:37:56 +0100 Subject: [PATCH] Move realtime scheduling and signal handling to common code --- src/amps/main.c | 26 -------------------------- src/anetz/main.c | 26 -------------------------- src/bnetz/main.c | 26 -------------------------- src/cnetz/main.c | 26 -------------------------- src/common/main_common.c | 37 ++++++++++++++++++++++++++++++++++++- src/nmt/main.c | 26 -------------------------- 6 files changed, 36 insertions(+), 131 deletions(-) diff --git a/src/amps/main.c b/src/amps/main.c index 7561a8f..a32fdc8 100644 --- a/src/amps/main.c +++ b/src/amps/main.c @@ -22,8 +22,6 @@ #include #include #include -#include -#include #include "../common/main.h" #include "../common/debug.h" #include "../common/timer.h" @@ -357,32 +355,8 @@ int main(int argc, char *argv[]) printf("Base station on channel %d ready (%s), please tune transmitter to %.3f MHz and receiver to %.3f MHz.\n", kanal[i], chan_type_long_name(chan_type[i]), amps_channel2freq(kanal[i], 0), amps_channel2freq(kanal[i], 1)); } - signal(SIGINT,sighandler); - signal(SIGHUP,sighandler); - signal(SIGTERM,sighandler); - signal(SIGPIPE,sighandler); - - if (rt_prio > 0) { - struct sched_param schedp; - int rc; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = rt_prio; - rc = sched_setscheduler(0, SCHED_RR, &schedp); - if (rc) - fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio); - } - main_loop(&quit, latency, interval, NULL); - if (rt_prio > 0) { - struct sched_param schedp; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = 0; - sched_setscheduler(0, SCHED_OTHER, &schedp); - } - fail: /* cleanup functions */ call_cleanup(); diff --git a/src/anetz/main.c b/src/anetz/main.c index 681196c..ce616cf 100644 --- a/src/anetz/main.c +++ b/src/anetz/main.c @@ -22,8 +22,6 @@ #include #include #include -#include -#include #include "../common/main.h" #include "../common/debug.h" #include "../common/timer.h" @@ -177,32 +175,8 @@ int main(int argc, char *argv[]) printf("Base station on channel %d ready, please tune transmitter to %.3f MHz and receiver to %.3f MHz.\n", kanal[i], anetz_kanal2freq(kanal[i], 0), anetz_kanal2freq(kanal[i], 1)); } - signal(SIGINT,sighandler); - signal(SIGHUP,sighandler); - signal(SIGTERM,sighandler); - signal(SIGPIPE,sighandler); - - if (rt_prio > 0) { - struct sched_param schedp; - int rc; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = rt_prio; - rc = sched_setscheduler(0, SCHED_RR, &schedp); - if (rc) - fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio); - } - main_loop(&quit, latency, interval, NULL); - if (rt_prio > 0) { - struct sched_param schedp; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = 0; - sched_setscheduler(0, SCHED_OTHER, &schedp); - } - fail: /* cleanup functions */ call_cleanup(); diff --git a/src/bnetz/main.c b/src/bnetz/main.c index 0ddfde9..d0abedc 100644 --- a/src/bnetz/main.c +++ b/src/bnetz/main.c @@ -22,8 +22,6 @@ #include #include #include -#include -#include #include "../common/debug.h" #include "../common/timer.h" #include "../common/call.h" @@ -197,32 +195,8 @@ int main(int argc, char *argv[]) printf("To call phone, switch transmitter (using pilot signal) to %.3f MHz.\n", bnetz_kanal2freq(19, 0)); } - signal(SIGINT,sighandler); - signal(SIGHUP,sighandler); - signal(SIGTERM,sighandler); - signal(SIGPIPE,sighandler); - - if (rt_prio > 0) { - struct sched_param schedp; - int rc; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = rt_prio; - rc = sched_setscheduler(0, SCHED_RR, &schedp); - if (rc) - fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio); - } - main_loop(&quit, latency, interval, NULL); - if (rt_prio > 0) { - struct sched_param schedp; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = 0; - sched_setscheduler(0, SCHED_OTHER, &schedp); - } - fail: /* cleanup functions */ call_cleanup(); diff --git a/src/cnetz/main.c b/src/cnetz/main.c index fa64476..97a9551 100644 --- a/src/cnetz/main.c +++ b/src/cnetz/main.c @@ -22,8 +22,6 @@ #include #include #include -#include -#include #include "../common/main.h" #include "../common/debug.h" #include "../common/timer.h" @@ -311,32 +309,8 @@ int main(int argc, char *argv[]) printf("Base station on channel %d ready, please tune transmitter to %.3f MHz and receiver to %.3f MHz.\n", kanal[i], cnetz_kanal2freq(kanal[i], 0), cnetz_kanal2freq(kanal[i], 1)); } - signal(SIGINT,sighandler); - signal(SIGHUP,sighandler); - signal(SIGTERM,sighandler); - signal(SIGPIPE,sighandler); - - if (rt_prio > 0) { - struct sched_param schedp; - int rc; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = rt_prio; - rc = sched_setscheduler(0, SCHED_RR, &schedp); - if (rc) - fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio); - } - main_loop(&quit, latency, interval, NULL); - if (rt_prio > 0) { - struct sched_param schedp; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = 0; - sched_setscheduler(0, SCHED_OTHER, &schedp); - } - fail: /* cleanup functions */ call_cleanup(); diff --git a/src/common/main_common.c b/src/common/main_common.c index 749d228..2706b71 100644 --- a/src/common/main_common.c +++ b/src/common/main_common.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -256,7 +257,7 @@ void sighandler(int sigset) if (sigset == SIGPIPE) return; - fprintf(stderr, "Signal received: %d\n", sigset); + fprintf(stderr, "\nSignal received: %d\n", sigset); quit = 1; } @@ -287,6 +288,24 @@ void main_loop(int *quit, int latency, int interval, void (*myhandler)(void)) struct termios term, term_orig; int c; + /* catch signals */ + signal(SIGINT, sighandler); + signal(SIGHUP, sighandler); + signal(SIGTERM, sighandler); + signal(SIGPIPE, sighandler); + + /* real time priority */ + if (rt_prio > 0) { + struct sched_param schedp; + int rc; + + memset(&schedp, 0, sizeof(schedp)); + schedp.sched_priority = rt_prio; + rc = sched_setscheduler(0, SCHED_RR, &schedp); + if (rc) + fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio); + } + /* prepare terminal */ tcgetattr(0, &term_orig); term = term_orig; @@ -323,6 +342,7 @@ next_char: switch (c) { case 3: /* quit */ + fprintf(stderr, "\nCTRL+c received, quitting!\n"); *quit = 1; goto next_char; case 'w': @@ -349,5 +369,20 @@ next_char: /* reset terminal */ tcsetattr(0, TCSANOW, &term_orig); + + /* reset real time prio */ + if (rt_prio > 0) { + struct sched_param schedp; + + memset(&schedp, 0, sizeof(schedp)); + schedp.sched_priority = 0; + sched_setscheduler(0, SCHED_OTHER, &schedp); + } + + /* reset signals */ + signal(SIGINT, SIG_DFL); + signal(SIGHUP, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGPIPE, SIG_DFL); } diff --git a/src/nmt/main.c b/src/nmt/main.c index 6de4c97..ca9a1cb 100644 --- a/src/nmt/main.c +++ b/src/nmt/main.c @@ -22,8 +22,6 @@ #include #include #include -#include -#include #include #include #include @@ -358,32 +356,8 @@ int main(int argc, char *argv[]) nmt_check_channels(); - signal(SIGINT,sighandler); - signal(SIGHUP,sighandler); - signal(SIGTERM,sighandler); - signal(SIGPIPE,sighandler); - - if (rt_prio > 0) { - struct sched_param schedp; - int rc; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = rt_prio; - rc = sched_setscheduler(0, SCHED_RR, &schedp); - if (rc) - fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio); - } - main_loop(&quit, latency, interval, myhandler); - if (rt_prio > 0) { - struct sched_param schedp; - - memset(&schedp, 0, sizeof(schedp)); - schedp.sched_priority = 0; - sched_setscheduler(0, SCHED_OTHER, &schedp); - } - fail: /* fifo */ if (sms_deliver_fd > 0)