Skip to content

Commit

Permalink
Exit process if we cannot write to outfile.
Browse files Browse the repository at this point in the history
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 <greearb@candelatech.com>
  • Loading branch information
greearb committed Dec 19, 2018
1 parent f3bb161 commit bd063e8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4035,31 +4035,41 @@ 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') {
char linebuffer[1024];
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));
l->line = strdup(linebuffer);
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);
}
}

0 comments on commit bd063e8

Please sign in to comment.