diff --git a/configure.ac b/configure.ac index 280fb1dc1..e30438cef 100644 --- a/configure.ac +++ b/configure.ac @@ -306,5 +306,14 @@ AC_SEARCH_LIBS(clock_gettime, [rt posix4]) # Check for clock_gettime support AC_CHECK_FUNCS([clock_gettime]) + +# +# Check for systemd to support socket-based activation +# +AC_CHECK_HEADERS([systemd/sd-daemon.h], + AC_DEFINE([HAVE_SD_DAEMON_H], [1], [Have sd-daemon support.]) + AC_SEARCH_LIBS(sd_listen_fds, [systemd])) + + AC_CONFIG_FILES([Makefile src/Makefile src/version.h examples/Makefile iperf3.spec]) AC_OUTPUT diff --git a/contrib/iperf3_@.service b/contrib/iperf3_@.service new file mode 100644 index 000000000..9d8a3ed1e --- /dev/null +++ b/contrib/iperf3_@.service @@ -0,0 +1,5 @@ +[Unit] +Description=Socket-Activated iperf3 + +[Service] +ExecStart=/usr/local/bin/iperf3 -s -1 -p %i --forceflush diff --git a/contrib/iperf3_@.socket b/contrib/iperf3_@.socket new file mode 100644 index 000000000..53ca04735 --- /dev/null +++ b/contrib/iperf3_@.socket @@ -0,0 +1,6 @@ +[Socket] +ListenStream=%i + +[Install] +WantedBy=sockets.target + diff --git a/src/iperf_config.h.in b/src/iperf_config.h.in index 045126966..3b63d9b3b 100644 --- a/src/iperf_config.h.in +++ b/src/iperf_config.h.in @@ -54,6 +54,9 @@ /* Have SCTP support. */ #undef HAVE_SCTP_H +/* Have sd-daemon support. */ +#undef HAVE_SD_DAEMON_H + /* Define to 1 if you have the `sendfile' function. */ #undef HAVE_SENDFILE diff --git a/src/iperf_server_api.c b/src/iperf_server_api.c index 500ff73da..e91ad52bc 100644 --- a/src/iperf_server_api.c +++ b/src/iperf_server_api.c @@ -66,10 +66,25 @@ #endif /* TCP_CA_NAME_MAX */ #endif /* HAVE_TCP_CONGESTION */ +#if defined(HAVE_SD_DAEMON_H) +#include +#endif + int iperf_server_listen(struct iperf_test *test) { + int n = 0; retry: + #if defined(HAVE_SD_DAEMON_H) + n = sd_listen_fds(0); + if (n > 1) { + return -1; + } + if (n == 1) { + test->listener = SD_LISTEN_FDS_START + 0; + } + #endif + if (n == 0) { if((test->listener = netannounce(test->settings->domain, Ptcp, test->bind_address, test->bind_dev, test->server_port)) < 0) { if (errno == EAFNOSUPPORT && (test->settings->domain == AF_INET6 || test->settings->domain == AF_UNSPEC)) { /* If we get "Address family not supported by protocol", that @@ -85,6 +100,7 @@ iperf_server_listen(struct iperf_test *test) return -1; } } + } if (!test->json_output) { if (test->server_last_run_rc != 2)