From 1388c204fa1d056c43772b2f9e4c8ddbfef949a7 Mon Sep 17 00:00:00 2001 From: fengzero Date: Tue, 14 Mar 2023 07:25:18 +0000 Subject: [PATCH] fix(api): check return value of system --- plugins/restful/file_handle.c | 5 ++-- plugins/restful/log_handle.c | 51 ++++++++++++++++++++++------------- src/utils/http.c | 2 -- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/plugins/restful/file_handle.c b/plugins/restful/file_handle.c index 9c706651..182a50c2 100644 --- a/plugins/restful/file_handle.c +++ b/plugins/restful/file_handle.c @@ -186,7 +186,6 @@ void handle_download_file(nng_aio *aio) /*handle http response*/ sprintf(http_resp, "%s%s", "attachment; filename=", file_name); rv = neu_http_response_file(aio, file_data, file_len, http_resp); - if (0 != rv) { - return; - } + free(file_data); + nlog_notice("download file %s, rv: %d", file_name, rv); } \ No newline at end of file diff --git a/plugins/restful/log_handle.c b/plugins/restful/log_handle.c index f9220d11..9f132261 100644 --- a/plugins/restful/log_handle.c +++ b/plugins/restful/log_handle.c @@ -46,11 +46,9 @@ UT_array *collect_log_files(); void handle_logs_files(nng_aio *aio) { - char path[128] = { 0 }; - char disp[128] = { 0 }; - void * data = NULL; - size_t len = 0; - int rv = 0; + void * data = NULL; + size_t len = 0; + int rv = 0; NEU_VALIDATE_JWT(aio); @@ -65,17 +63,34 @@ void handle_logs_files(nng_aio *aio) } /* tar the neuron directory */ - rv = system("tar -zcvf neuron_debug.tar.gz ../neuron"); - if (0 != rv) { - nlog_error("failed to create neuron_debug.tar.gz"); + rv = system("tar -zcvf /tmp/neuron_debug.tar.gz ../neuron"); + if (rv == -1) { + nlog_error("failed to create neuron_debug.tar.gz, rv: %d", rv); NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, { neu_http_response(aio, error_code.error, result_error); }); return; + } else { + if (WIFEXITED(rv)) { + if (WEXITSTATUS(rv) != 0) { + nlog_error("failed to create neuron_debug.tar.gz, rv: %d", + WEXITSTATUS(rv)); + NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, { + neu_http_response(aio, error_code.error, result_error); + }); + return; + } + } else { + nlog_error("failed to create neuron_debug.tar.gz, rv: %d", + WEXITSTATUS(rv)); + NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, { + neu_http_response(aio, error_code.error, result_error); + }); + return; + } } - strcpy(path, "neuron_debug.tar.gz"); - rv = read_file(path, &data, &len); + rv = read_file("/tmp/neuron_debug.tar.gz", &data, &len); if (0 != rv) { NEU_JSON_RESPONSE_ERROR( rv, { neu_http_response(aio, error_code.error, result_error); }); @@ -83,11 +98,11 @@ void handle_logs_files(nng_aio *aio) } /* handle http response */ - strcpy(disp, "attachment; filename=neuron_debug.tar.gz"); - rv = neu_http_response_file(aio, data, len, disp); - if (0 != rv) { - return; - } + rv = neu_http_response_file(aio, data, len, + "attachment; filename=neuron_debug.tar.gz"); + + free(data); + nlog_notice("download neuron_debug.tar.gz, ret: %d", rv); } void handle_log_level(nng_aio *aio) @@ -122,9 +137,9 @@ void handle_log_level(nng_aio *aio) ret = zlog_level_switch(ct, ZLOG_LEVEL_DEBUG); if (ret != 0) { - nlog_error( - "Modify node log level fail, node_name:%s, ret: %d,", - node_name, ret); + nlog_error("Modify node log level fail, node_name:%s, " + "ret: %d,", + node_name, ret); NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { neu_http_response(aio, error_code.error, result_error); }); diff --git a/src/utils/http.c b/src/utils/http.c index 1c75a128..62d378c4 100644 --- a/src/utils/http.c +++ b/src/utils/http.c @@ -397,7 +397,6 @@ int neu_http_response_file(nng_aio *aio, void *data, size_t len, "Content-Disposition")) != 0) || ((rv = nng_http_res_copy_data(res, data, len)) != 0)) { nng_http_res_free(res); - free(data); nng_aio_finish(aio, rv); return -1; @@ -407,7 +406,6 @@ int neu_http_response_file(nng_aio *aio, void *data, size_t len, nlog_notice("%s %s [%d]", nng_http_req_get_method(nng_req), nng_http_req_get_uri(nng_req), NNG_HTTP_STATUS_OK); - free(data); nng_aio_set_output(aio, 0, res); nng_aio_finish(aio, 0);