]> git.ipfire.org Git - fireperf.git/commitdiff
client: Add timeout
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Jan 2021 10:57:23 +0000 (10:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Jan 2021 10:57:23 +0000 (10:57 +0000)
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 <michael.tremer@ipfire.org>
src/client.c
src/main.c
src/main.h

index 78509b0eb5d6d85afba327e83ce1afc823dd6d27..1d4f4080674c4240ae84ee5445035afa09fe184a 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <signal.h>
 #include <sys/epoll.h>
 #include <string.h>
 #include <unistd.h>
 #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));
index cc0db573b94387bac7c07e47b1ee762ceba9c270..69f38ae2531fed9969cfca92078c1ac1ed494006 100644 (file)
@@ -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;
 
index bcf70df08a4a403f666953d96c9c602b0f199de1..62e930169194cef82ea06357038dafce828c2854 100644 (file)
@@ -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 */