]> git.ipfire.org Git - fireperf.git/commitdiff
Move creation of epoll() and timerfd() to main
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Feb 2021 23:14:52 +0000 (23:14 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Feb 2021 23:14:52 +0000 (23:14 +0000)
This is shared code between the client and the server and it is easier
to just maintain it once.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c
src/client.h
src/main.c
src/server.c
src/server.h

index 7a848d7c41b536083f7b29eaf768d4eb7e7bddc6..9c35faae36c629782d22b08642368dd728ae1856 100644 (file)
@@ -210,7 +210,7 @@ static int send_data_to_server(struct fireperf_config* conf, int fd,
        return 0;
 }
 
-int fireperf_client(struct fireperf_config* conf) {
+int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd) {
        struct fireperf_client_stats stats = { 0 };
        struct fireperf_random_pool* pool = NULL;
 
@@ -227,19 +227,11 @@ int fireperf_client(struct fireperf_config* conf) {
 
        int r = 1;
 
-       int epollfd = -1;
-       struct epoll_event ev;
+       struct epoll_event ev = {
+               .events = EPOLLIN,
+       };
        struct epoll_event events[EPOLL_MAX_EVENTS];
 
-       // Initialize epoll()
-       epollfd = epoll_create1(0);
-       if (epollfd < 0) {
-               ERROR(conf, "Could not initialize epoll(): %s\n", strerror(errno));
-               return 1;
-       }
-
-       ev.events = EPOLLIN;
-
        // Let us know when the socket is ready for sending data
        if (!conf->keepalive_only)
                ev.events |= EPOLLOUT;
index b1d83e565cc34c50561fb3fa719381ceb96c0ae2..a9c6f7004a529f154c6b910a7bd6e4fb1ebb6536 100644 (file)
@@ -23,6 +23,6 @@
 
 #include "main.h"
 
-int fireperf_client(struct fireperf_config* conf);
+int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd);
 
 #endif /* FIREPERF_CLIENT_H */
index 531f117ed0e3d0ca431571d5667e8cf2cc97c818..5aeeae8ffc1c345a04ab4f12cd25702b0acb03a1 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/time.h>
+#include <sys/epoll.h>
 #include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/timerfd.h>
 #include <time.h>
+#include <unistd.h>
 
 #include "client.h"
 #include "main.h"
@@ -257,12 +260,52 @@ int main(int argc, char* argv[]) {
        if (r)
                return r;
 
+       // Initialize epoll()
+       int epollfd = epoll_create1(0);
+       if (epollfd < 0) {
+               ERROR(&conf, "Could not initialize epoll(): %s\n", strerror(errno));
+               r = 1;
+               goto ERROR;
+       }
+
+       // Create timerfd() to print statistics
+       int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
+       if (timerfd < 0) {
+               ERROR(&conf, "timerfd_create() failed: %s\n", strerror(errno));
+               r = 1;
+               goto ERROR;
+       }
+
+       struct epoll_event ev = {
+               .events  = EPOLLIN,
+               .data.fd = timerfd,
+       };
+
+       if (epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &ev)) {
+               ERROR(&conf, "Could not add timerfd to epoll(): %s\n", strerror(errno));
+               r = 1;
+               goto ERROR;
+       }
+
+       // Let the timer ping us once a second
+       struct itimerspec timer = {
+               .it_interval.tv_sec = 1,
+               .it_value.tv_sec = 1,
+       };
+
+       r = timerfd_settime(timerfd, 0, &timer, NULL);
+       if (r) {
+               ERROR(&conf, "Could not set timer: %s\n", strerror(errno));
+               r = 1;
+               goto ERROR;
+       }
+
        switch (conf.mode) {
                case FIREPERF_MODE_CLIENT:
-                       return fireperf_client(&conf);
+                       return fireperf_client(&conf, epollfd, timerfd);
 
                case FIREPERF_MODE_SERVER:
-                       return fireperf_server(&conf);
+                       return fireperf_server(&conf, epollfd, timerfd);
 
                case FIREPERF_MODE_NONE:
                        fprintf(stderr, "No mode selected\n");
@@ -270,5 +313,12 @@ int main(int argc, char* argv[]) {
                        break;
        }
 
+ERROR:
+       if (epollfd > 0)
+               close(epollfd);
+
+       if (timerfd > 0)
+               close(timerfd);
+
        return r;
 }
index e481cb321bbbb5b7a66346f728443afec4744e16..b119c800ab9b02506c7482ae8082e3c3288b213e 100644 (file)
@@ -23,7 +23,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <sys/epoll.h>
-#include <sys/timerfd.h>
 #include <time.h>
 #include <unistd.h>
 
@@ -269,24 +268,16 @@ static int is_listening_socket(struct fireperf_config* conf, int* sockets, int f
        return 0;
 }
 
-int fireperf_server(struct fireperf_config* conf) {
+int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd) {
        struct fireperf_server_stats stats = { 0 };
 
        DEBUG(conf, "Launching " PACKAGE_NAME " in server mode\n");
 
        int listening_sockets[conf->listening_sockets];
 
-       int epollfd = -1;
-       struct epoll_event events[EPOLL_MAX_EVENTS];
-
        int r = 1;
-
-       // Initialize epoll()
-       epollfd = epoll_create1(0);
-       if (epollfd < 0) {
-               ERROR(conf, "Could not initialize epoll(): %s\n", strerror(errno));
-               return 1;
-       }
+       struct epoll_event ev;
+       struct epoll_event events[EPOLL_MAX_EVENTS];
 
        // Create listening sockets
        for (unsigned int i = 0; i < conf->listening_sockets; i++) {
@@ -297,10 +288,8 @@ int fireperf_server(struct fireperf_config* conf) {
                listening_sockets[i] = sockfd;
 
                // Add listening socket to epoll
-               struct epoll_event ev = {
-                       .events  = EPOLLIN,
-                       .data.fd = sockfd,
-               };
+               ev.events  = EPOLLIN;
+               ev.data.fd = sockfd;
 
                if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev)) {
                        ERROR(conf, "Could not add socket file descriptor to epoll(): %s\n",
@@ -309,36 +298,6 @@ int fireperf_server(struct fireperf_config* conf) {
                }
        }
 
-       // Create timerfd() to print statistics
-       int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
-       if (timerfd < 0) {
-               ERROR(conf, "timerfd_create() failed: %s\n", strerror(errno));
-               goto ERROR;
-       }
-
-       struct epoll_event ev = {
-               .events  = EPOLLIN,
-               .data.fd = timerfd,
-       };
-
-       if (epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &ev)) {
-               ERROR(conf, "Could not add timerfd to epoll(): %s\n",
-                       strerror(errno));
-               goto ERROR;
-       }
-
-       // Let the timer ping us once a second
-       struct itimerspec timer = {
-               .it_interval.tv_sec = 1,
-               .it_value.tv_sec = 1,
-       };
-
-       r = timerfd_settime(timerfd, 0, &timer, NULL);
-       if (r) {
-               ERROR(conf, "Could not set timer: %s\n", strerror(errno));
-               return 1;
-       }
-
        DEBUG(conf, "Entering main loop...\n");
 
        while (!conf->terminated) {
@@ -426,11 +385,5 @@ ERROR:
                        close(listening_sockets[i]);
        }
 
-       if (epollfd > 0)
-               close(epollfd);
-
-       if (timerfd > 0)
-               close(timerfd);
-
        return r;
 }
index 9dc4a00c3b6abb8a94d0b3ac7f5ac26d9c079284..b6984c0542568bd9f7eb09eddd02e388377db8f0 100644 (file)
@@ -23,6 +23,6 @@
 
 #include "main.h"
 
-int fireperf_server(struct fireperf_config* conf);
+int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd);
 
 #endif /* FIREPERF_SERVER_H */