From fe4a13ab4dd6ca089168e01bea1747046fd3b0ae Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Tue, 26 Nov 2013 15:40:19 -0800 Subject: [PATCH 1/2] Support daemonizing with -D flag. Possible related work still under consideration: o Use syslog in daemon mode for output that would normally go to stdout / stderr. o Write a PID file. This is basically the gist of Issue 105. --- src/iperf_api.c | 4 ++-- src/iperf_api.h | 3 ++- src/iperf_error.c | 6 +++++- src/locale.c | 2 +- src/main.c | 9 ++++++++- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index e92fc9e..ac27d39 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2011, The Regents of the University of California, + * Copyright (c) 2009-2013, The Regents of the University of California, * through Lawrence Berkeley National Laboratory (subject to receipt of any * required approvals from the U.S. Dept. of Energy). All rights reserved. * @@ -536,7 +536,7 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) blksize = 0; server_flag = client_flag = rate_flag = 0; - while ((flag = getopt_long(argc, argv, "p:f:i:DVJdvsc:ub:t:n:l:P:Rw:B:M:N46S:L:ZO:F:A:T:h", longopts, NULL)) != -1) { + while ((flag = getopt_long(argc, argv, "p:f:i:DVJvsc:ub:t:n:l:P:Rw:B:M:N46S:L:ZO:F:A:T:h", longopts, NULL)) != -1) { switch (flag) { case 'p': test->server_port = atoi(optarg); diff --git a/src/iperf_api.h b/src/iperf_api.h index bb26508..da1c441 100644 --- a/src/iperf_api.h +++ b/src/iperf_api.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2011, The Regents of the University of California, + * Copyright (c) 2009-2013, The Regents of the University of California, * through Lawrence Berkeley National Laboratory (subject to receipt of any * required approvals from the U.S. Dept. of Energy). All rights reserved. * @@ -273,6 +273,7 @@ enum { IESETWINDOWSIZE = 130, // Unable to set socket window size (check perror) IEPROTOCOL = 131, // Protocol does not exist IEAFFINITY = 132, // Unable to set CPU affinity (check perror) + IEDAEMON = 133, // Unable to become a daemon process /* Stream errors */ IECREATESTREAM = 200, // Unable to create a new stream (check herror/perror) IEINITSTREAM = 201, // Unable to initialize stream (check herror/perror) diff --git a/src/iperf_error.c b/src/iperf_error.c index caff3cf..3cf4fc6 100644 --- a/src/iperf_error.c +++ b/src/iperf_error.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2011, The Regents of the University of California, + * Copyright (c) 2009-2013, The Regents of the University of California, * through Lawrence Berkeley National Laboratory (subject to receipt of any * required approvals from the U.S. Dept. of Energy). All rights reserved. * @@ -235,6 +235,10 @@ iperf_strerror(int i_errno) snprintf(errstr, len, "unable to set CPU affinity"); perr = 1; break; + case IEDAEMON: + snprintf(errstr, len, "unable to become a daemon"); + perr = 1; + break; case IECREATESTREAM: snprintf(errstr, len, "unable to create a new stream"); herr = 1; diff --git a/src/locale.c b/src/locale.c index 26ede80..3b80b16 100644 --- a/src/locale.c +++ b/src/locale.c @@ -80,6 +80,7 @@ const char usage_longstr[] = "Usage: iperf [-s|-c host] [options]\n" " -h, --help show this message and quit\n" "Server specific:\n" " -s, --server run in server mode\n" + " -D, --daemon run the server as a daemon\n" "Client specific:\n" " -c, --client run in client mode, connecting to \n" " -u, --udp use UDP rather than TCP\n" @@ -104,7 +105,6 @@ const char usage_longstr[] = "Usage: iperf [-s|-c host] [options]\n" " -T, --title str prefix every output line with this string\n" #ifdef NOT_YET_SUPPORTED /* still working on these */ - " -D, --daemon run the server as a daemon\n" #endif "\n" diff --git a/src/main.c b/src/main.c index b5b14e4..fea782f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2011, The Regents of the University of California, + * Copyright (c) 2009-2013, The Regents of the University of California, * through Lawrence Berkeley National Laboratory (subject to receipt of any * required approvals from the U.S. Dept. of Energy). All rights reserved. * @@ -104,6 +104,13 @@ run(struct iperf_test *test) switch (test->role) { case 's': + if (test->daemon) { + int rc = daemon(0, 0); + if (rc < 0) { + i_errno = IEDAEMON; + iperf_errexit(test, "error - %s", iperf_strerror(i_errno)); + } + } consecutive_errors = 0; for (;;) { if (iperf_run_server(test) < 0) { From adbc1f48608a64cb43c6b050a4025bcb0ea32640 Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Mon, 2 Dec 2013 09:38:13 -0800 Subject: [PATCH 2/2] Document the recently-added -D / --daemon flag from changeset 4a9b814c16e6. --- src/iperf3.1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/iperf3.1 b/src/iperf3.1 index 06f1239..1c9c7b5 100644 --- a/src/iperf3.1 +++ b/src/iperf3.1 @@ -57,6 +57,9 @@ show a help synopsis .TP .BR -s ", " --server " " run in server mode +.TP +.BR -D ", " --daemon " " +run the server in background as a daemon .SH "CLIENT SPECIFIC OPTIONS" .TP