Move signal handling to util.c.

This will be useful in phc2sys and pmc.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
master
Miroslav Lichvar 2014-07-08 16:14:20 +02:00 committed by Richard Cochran
parent 2423357754
commit ca637b2067
3 changed files with 48 additions and 21 deletions

23
ptp4l.c
View File

@ -18,7 +18,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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;
}

32
util.c
View File

@ -17,11 +17,13 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

14
util.h
View File

@ -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