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;
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;
#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 */
#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"
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");
break;
}
+ERROR:
+ if (epollfd > 0)
+ close(epollfd);
+
+ if (timerfd > 0)
+ close(timerfd);
+
return r;
}
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
-#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
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++) {
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",
}
}
- // 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) {
close(listening_sockets[i]);
}
- if (epollfd > 0)
- close(epollfd);
-
- if (timerfd > 0)
- close(timerfd);
-
return r;
}
#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 */