Skip to content

Commit

Permalink
Merge pull request #1523 from matt9j/fix-exit-code-when-doing-json-ou…
Browse files Browse the repository at this point in the history
…tput

Fix exit code when using json output
  • Loading branch information
bmah888 authored Jul 3, 2023
2 parents c2fe76c + 060cde6 commit cc10897
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
55 changes: 31 additions & 24 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4726,31 +4726,38 @@ iperf_json_start(struct iperf_test *test)
int
iperf_json_finish(struct iperf_test *test)
{
if (test->title)
cJSON_AddStringToObject(test->json_top, "title", test->title);
if (test->extra_data)
cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data);
/* Include server output */
if (test->json_server_output) {
cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output);
}
if (test->server_output_text) {
cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text);
if (test->json_top) {
if (test->title) {
cJSON_AddStringToObject(test->json_top, "title", test->title);
}
if (test->extra_data) {
cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data);
}
/* Include server output */
if (test->json_server_output) {
cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output);
}
if (test->server_output_text) {
cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text);
}
// Get ASCII rendering of JSON structure. Then make our
// own copy of it and return the storage that cJSON allocated
// on our behalf. We keep our own copy around.
char *str = cJSON_Print(test->json_top);
if (str == NULL) {
return -1;
}
test->json_output_string = strdup(str);
cJSON_free(str);
if (test->json_output_string == NULL) {
return -1;
}
fprintf(test->outfile, "%s\n", test->json_output_string);
iflush(test);
cJSON_Delete(test->json_top);
test->json_top = NULL;
}
// Get ASCII rendering of JSON structure. Then make our
// own copy of it and return the storage that cJSON allocated
// on our behalf. We keep our own copy around.
char *str = cJSON_Print(test->json_top);
if (str == NULL)
return -1;
test->json_output_string = strdup(str);
cJSON_free(str);
if (test->json_output_string == NULL)
return -1;
fprintf(test->outfile, "%s\n", test->json_output_string);
iflush(test);
cJSON_Delete(test->json_top);
test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL;
test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL;
return 0;
}

Expand Down
4 changes: 0 additions & 4 deletions src/iperf_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,6 @@ iperf_run_client(struct iperf_test * test)
if (test->json_output) {
cJSON_AddStringToObject(test->json_top, "error", iperf_strerror(i_errno));
iperf_json_finish(test);
iflush(test);
// Return 0 and not -1 since all terminating function were done here.
// Also prevents error message logging outside the already closed JSON output.
return 0;
}
iflush(test);
return -1;
Expand Down
6 changes: 4 additions & 2 deletions src/iperf_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ iperf_errexit(struct iperf_test *test, const char *format, ...)

va_start(argp, format);
vsnprintf(str, sizeof(str), format, argp);
if (test != NULL && test->json_output && test->json_top != NULL) {
cJSON_AddStringToObject(test->json_top, "error", str);
if (test != NULL && test->json_output) {
if (test->json_top != NULL) {
cJSON_AddStringToObject(test->json_top, "error", str);
}
iperf_json_finish(test);
} else
if (test && test->outfile && test->outfile != stdout) {
Expand Down

0 comments on commit cc10897

Please sign in to comment.