From ca637b2067ae78c9830c38bb5e9a659255b1a4a2 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 8 Jul 2014 16:14:20 +0200 Subject: [PATCH] Move signal handling to util.c. This will be useful in phc2sys and pmc. Signed-off-by: Miroslav Lichvar --- ptp4l.c | 23 ++--------------------- util.c | 32 ++++++++++++++++++++++++++++++++ util.h | 14 ++++++++++++++ 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/ptp4l.c b/ptp4l.c index 25270c6..5080a79 100644 --- a/ptp4l.c +++ b/ptp4l.c @@ -18,7 +18,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include -#include #include #include #include @@ -40,8 +39,6 @@ int assume_two_step = 0; -static int running = 1; - static struct config cfg_settings = { .dds = { .dds = { @@ -131,12 +128,6 @@ static struct config cfg_settings = { .cfg_ignore = 0, }; -static void handle_int_quit_term(int s) -{ - pr_notice("caught signal %d", s); - running = 0; -} - static void usage(char *progname) { fprintf(stderr, @@ -184,18 +175,8 @@ int main(int argc, char *argv[]) struct defaultDS *ds = &cfg_settings.dds.dds; int phc_index = -1, required_modes = 0; - if (SIG_ERR == signal(SIGINT, handle_int_quit_term)) { - fprintf(stderr, "cannot handle SIGINT\n"); + if (handle_term_signals()) return -1; - } - if (SIG_ERR == signal(SIGQUIT, handle_int_quit_term)) { - fprintf(stderr, "cannot handle SIGQUIT\n"); - return -1; - } - if (SIG_ERR == signal(SIGTERM, handle_int_quit_term)) { - fprintf(stderr, "cannot handle SIGTERM\n"); - return -1; - } /* Set fault timeouts to a default value */ for (i = 0; i < FT_CNT; i++) { @@ -404,7 +385,7 @@ int main(int argc, char *argv[]) return -1; } - while (running) { + while (is_running()) { if (clock_poll(clock)) break; } diff --git a/util.c b/util.c index 1e86040..ae66bb1 100644 --- a/util.c +++ b/util.c @@ -17,11 +17,13 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include +#include #include #include #include #include "address.h" +#include "print.h" #include "sk.h" #include "util.h" @@ -29,6 +31,8 @@ #define NS_PER_HOUR (3600 * NS_PER_SEC) #define NS_PER_DAY (24 * NS_PER_HOUR) +static int running = 1; + const char *ps_str[] = { "NONE", "INITIALIZING", @@ -311,3 +315,31 @@ int get_arg_val_d(int op, const char *optarg, double *val, } return 0; } + +static void handle_int_quit_term(int s) +{ + pr_notice("caught signal %d", s); + running = 0; +} + +int handle_term_signals(void) +{ + if (SIG_ERR == signal(SIGINT, handle_int_quit_term)) { + fprintf(stderr, "cannot handle SIGINT\n"); + return -1; + } + if (SIG_ERR == signal(SIGQUIT, handle_int_quit_term)) { + fprintf(stderr, "cannot handle SIGQUIT\n"); + return -1; + } + if (SIG_ERR == signal(SIGTERM, handle_int_quit_term)) { + fprintf(stderr, "cannot handle SIGTERM\n"); + return -1; + } + return 0; +} + +int is_running(void) +{ + return running; +} diff --git a/util.h b/util.h index 3fae51c..cf05e49 100644 --- a/util.h +++ b/util.h @@ -212,4 +212,18 @@ int get_arg_val_ui(int op, const char *optarg, unsigned int *val, int get_arg_val_d(int op, const char *optarg, double *val, double min, double max); +/** + * Setup a handler for terminating signals (SIGINT, SIGQUIT, SIGTERM). + * + * @return 0 on success, -1 on error. + */ +int handle_term_signals(void); + +/** + * Check if a terminating signal was received. + * + * @return 1 if no terminating signal was received, 0 otherwise. + */ +int is_running(void); + #endif