From bd063e88331014fcc19285a7e7b3c174ab8f8a9a Mon Sep 17 00:00:00 2001 From: Ben Greear Date: Fri, 16 Nov 2018 16:09:26 -0800 Subject: [PATCH] Exit process if we cannot write to outfile. This lets process exit when pipe is closed, for instance when piping output of iperf3 to a script for post processing. Signed-off-by: Ben Greear --- src/iperf_api.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 6cfb5d0c1..11b9e2a38 100755 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -4035,11 +4035,13 @@ iperf_printf(struct iperf_test *test, const char* format, ...) * easily exceed the size of the line buffer, but which don't need * to be buffered up anyway. */ + int rv; if (test->role == 'c') { if (test->title) fprintf(test->outfile, "%s: ", test->title); va_start(argp, format); r = vfprintf(test->outfile, format, argp); + rv = r; va_end(argp); } else if (test->role == 's') { @@ -4047,7 +4049,7 @@ iperf_printf(struct iperf_test *test, const char* format, ...) va_start(argp, format); r = vsnprintf(linebuffer, sizeof(linebuffer), format, argp); va_end(argp); - fprintf(test->outfile, "%s", linebuffer); + rv = fprintf(test->outfile, "%s", linebuffer); if (test->role == 's' && iperf_get_test_get_server_output(test)) { struct iperf_textline *l = (struct iperf_textline *) malloc(sizeof(struct iperf_textline)); @@ -4055,11 +4057,19 @@ iperf_printf(struct iperf_test *test, const char* format, ...) TAILQ_INSERT_TAIL(&(test->server_output_list), l, textlineentries); } } + + if (rv < 0) { + /* If printing output fails, go ahead and exit, probably broken pipe */ + exit(1); + } return r; } int iflush(struct iperf_test *test) { - return fflush(test->outfile); + int rv = fflush(test->outfile); + if (rv < 0) { + exit(2); + } }