From: Michael Tremer Date: Thu, 28 Jan 2021 10:57:23 +0000 (+0000) Subject: client: Add timeout X-Git-Tag: 0.1.0~48 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2560af5f96c6af639225a25d56cf7f839e8b9b23;p=fireperf.git client: Add timeout This is a safety precausion which will terminate the client automatically after a certain time just in case the network becomes unresponsive. Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index 78509b0..1d4f408 100644 --- a/src/client.c +++ b/src/client.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -27,6 +28,18 @@ #include "logging.h" #include "main.h" +// Set to one when the timeout has expired +static int timeout_expired = 0; + +static void handle_SIGALRM(int signal) { + switch (signal) { + // Terminate after timeout has expired + case SIGALRM: + timeout_expired = 1; + break; + } +} + static int connect_socket(struct fireperf_config* conf, int fd) { DEBUG(conf, "(Re-)connecting socket %d...\n", fd); @@ -100,9 +113,17 @@ int fireperf_client(struct fireperf_config* conf) { } } + // Configure timeout if set + if (conf->timeout) { + // Register signal handler + signal(SIGALRM, handle_SIGALRM); + + alarm(conf->timeout); + } + DEBUG(conf, "Entering main loop...\n"); - while (!conf->terminated) { + while (!conf->terminated && !timeout_expired) { int fds = epoll_wait(epollfd, events, EPOLL_MAX_EVENTS, -1); if (fds < 1) { ERROR(conf, "epoll_wait() failed: %s\n", strerror(errno)); diff --git a/src/main.c b/src/main.c index cc0db57..69f38ae 100644 --- a/src/main.c +++ b/src/main.c @@ -61,6 +61,7 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) { {"parallel", required_argument, 0, 'P'}, {"port", required_argument, 0, 'p'}, {"server", no_argument, 0, 's'}, + {"timeout", required_argument, 0, 't'}, {0, 0, 0, 0}, }; @@ -68,7 +69,7 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) { int done = 0; while (!done) { - int c = getopt_long(argc, argv, "c:dp:sP:", long_options, &option_index); + int c = getopt_long(argc, argv, "c:dp:st:P:", long_options, &option_index); // End if (c == -1) @@ -130,6 +131,10 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) { conf->mode = FIREPERF_MODE_SERVER; break; + case 't': + conf->timeout = strtoul(optarg, NULL, 10); + break; + default: done = 1; break; @@ -145,6 +150,7 @@ int main(int argc, char* argv[]) { .mode = FIREPERF_MODE_NONE, .port = DEFAULT_PORT, .parallel = DEFAULT_PARALLEL, + .timeout = DEFAULT_TIMEOUT, }; int r; diff --git a/src/main.h b/src/main.h index bcf70df..62e9301 100644 --- a/src/main.h +++ b/src/main.h @@ -26,6 +26,7 @@ #define DEFAULT_LOG_LEVEL LOG_INFO #define DEFAULT_PARALLEL 1 #define DEFAULT_PORT 5001 +#define DEFAULT_TIMEOUT 300 #define MAX_PARALLEL (1 << 20) @@ -45,6 +46,7 @@ struct fireperf_config { struct in6_addr address; int port; unsigned long parallel; + unsigned int timeout; }; #endif /* FIREPERF_MAIN_H */