From: octo Date: Sun, 30 Apr 2006 16:18:49 +0000 (+0000) Subject: Catch `SIGINT' and print summary after Ctrl-C has been pressed.. X-Git-Tag: liboping-0.1.0~22 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=f8ba50a957d4245db12ca70889acc08f7cbd68b5;p=collectd.git Catch `SIGINT' and print summary after Ctrl-C has been pressed.. --- diff --git a/ToDo b/ToDo index a624cbf5..6ebf1066 100644 --- a/ToDo +++ b/ToDo @@ -1 +1,2 @@ - Write manpage +- Catch SIGINT in `oping' diff --git a/configure.ac b/configure.ac index 6f74c816..5e9fc727 100644 --- a/configure.ac +++ b/configure.ac @@ -39,6 +39,7 @@ AC_CHECK_HEADERS(sys/stat.h) AC_HEADER_TIME AC_CHECK_HEADERS(sys/socket.h) AC_CHECK_HEADERS(netdb.h) +AC_CHECK_HEADERS(signal.h) # This sucks, but what can I do..? AC_CHECK_HEADERS(netinet/in_systm.h, [], [], diff --git a/src/oping.c b/src/oping.c index 457a09d2..0c799863 100644 --- a/src/oping.c +++ b/src/oping.c @@ -50,6 +50,10 @@ # include /* NI_MAXHOST */ #endif +#if HAVE_SIGNAL_H +# include +#endif + #include typedef struct ping_context @@ -69,6 +73,12 @@ static double opt_interval = 1.0; static int opt_addrfamily = PING_DEF_AF; static int opt_count = -1; +void sigint_handler (int signal) +{ + /* Exit the loop */ + opt_count = 0; +} + ping_context_t *context_create (void) { ping_context_t *ret; @@ -233,6 +243,8 @@ int main (int argc, char **argv) pingobj_t *ping; pingobj_iter_t *iter; + struct sigaction sigint_action; + struct timeval tv_begin; struct timeval tv_end; struct timespec ts_wait; @@ -241,7 +253,6 @@ int main (int argc, char **argv) int optind; int i; - optind = read_options (argc, argv); if (optind >= argc) @@ -294,12 +305,17 @@ int main (int argc, char **argv) ping_iterator_set_context (iter, (void *) context); } - while (1) + memset (&sigint_action, '\0', sizeof (sigint_action)); + sigint_action.sa_handler = sigint_handler; + if (sigaction (SIGINT, &sigint_action, NULL) < 0) { - int status; + perror ("sigaction"); + return (1); + } - if (opt_count > 0) - opt_count--; + while (opt_count != 0) + { + int status; if (gettimeofday (&tv_begin, NULL) < 0) { @@ -321,7 +337,8 @@ int main (int argc, char **argv) } fflush (stdout); - if (opt_count == 0) + /* Don't sleep in the last iteration */ + if (opt_count == 1) break; if (gettimeofday (&tv_end, NULL) < 0) @@ -340,7 +357,15 @@ int main (int argc, char **argv) perror ("nanosleep"); break; } + else if (opt_count == 0) + { + /* sigint */ + break; + } } + + if (opt_count > 0) + opt_count--; } /* while (opt_count != 0) */ for (iter = ping_iterator_get (ping);