From 5d195d5cb16c9c2241c61051fe0d515ba4b4ad81 Mon Sep 17 00:00:00 2001 From: liyuxi Date: Wed, 1 Feb 2023 09:26:11 +0000 Subject: [PATCH] fix download log coredump --- include/neuron/errcodes.h | 3 + plugins/restful/http.c | 2 + plugins/restful/log_handle.c | 107 +---------------------------------- 3 files changed, 8 insertions(+), 104 deletions(-) diff --git a/include/neuron/errcodes.h b/include/neuron/errcodes.h index d7ea6dc0..23f1c84a 100644 --- a/include/neuron/errcodes.h +++ b/include/neuron/errcodes.h @@ -106,6 +106,9 @@ typedef enum { NEU_ERR_PLUGIN_TAG_NOT_READY = 3011, NEU_ERR_PLUGIN_PACKET_OUT_OF_ORDER = 3012, + NEU_ERR_FILE_OPEN_FAILURE = 4101, + NEU_ERR_FILE_READ_FAILURE = 4102, + NEU_ERR_MQTT_FAILURE = 4000, NEU_ERR_MQTT_NO_CERTFILESET = 4001, NEU_ERR_MQTT_CERTFILE_LOAD_FAILURE = 4002, diff --git a/plugins/restful/http.c b/plugins/restful/http.c index 37b9deef..2f005c2a 100644 --- a/plugins/restful/http.c +++ b/plugins/restful/http.c @@ -327,6 +327,8 @@ int http_response(nng_aio *aio, neu_err_code_e code, char *content) status = NNG_HTTP_STATUS_BAD_REQUEST; break; case NEU_ERR_FILE_NOT_EXIST: + case NEU_ERR_FILE_OPEN_FAILURE: + case NEU_ERR_FILE_READ_FAILURE: case NEU_ERR_LIBRARY_NOT_FOUND: case NEU_ERR_NODE_NOT_EXIST: case NEU_ERR_TAG_NOT_EXIST: diff --git a/plugins/restful/log_handle.c b/plugins/restful/log_handle.c index 71a60108..1dd9bf50 100644 --- a/plugins/restful/log_handle.c +++ b/plugins/restful/log_handle.c @@ -44,7 +44,6 @@ UT_array *collect_log_files(); -// void handle_get_log_files(nng_aio *aio); int read_file(const char *file_name, void **datap, size_t *lenp); static int http_resp_files(nng_aio *aio, void *data, size_t len, const char *disposition); @@ -185,106 +184,6 @@ void handle_log_level(nng_aio *aio) }) } -// void handle_get_log(nng_aio *aio) -// { -// int rv = 0; -// char log_file[NEU_NODE_NAME_LEN + 32] = { 0 }; - -// VALIDATE_JWT(aio); - -// // optional params -// rv = http_get_param_str(aio, "log_file", log_file, sizeof(log_file)); -// if (-1 == rv) { -// nlog_error("parse query param `log_file` fail"); -// NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { -// http_response(aio, error_code.error, result_error); -// }); -// return; -// } else if (-2 == rv) { -// // handle no param -// handle_get_log_files(aio); -// return; -// } else if ((size_t) rv >= sizeof(log_file)) { -// nlog_error("query param overflow, `log_file`:%s", log_file); -// NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { -// http_response(aio, error_code.error, result_error); -// }); -// return; -// } - -// void * data = NULL; -// size_t len = 0; -// nng_http_res *res = NULL; -// rv = read_log_file(log_file, &data, &len); -// if (0 != rv) { -// NEU_JSON_RESPONSE_ERROR( -// rv, { http_response(aio, error_code.error, result_error); }); -// return; -// } - -// if (((rv = nng_http_res_alloc(&res)) != 0) || -// ((rv = nng_http_res_set_status(res, NNG_HTTP_STATUS_OK)) != 0) || -// ((rv = nng_http_res_set_header(res, "Content-Type", -// "application/octet-stream")) != 0) || -// ((rv = nng_http_res_set_header(res, "Access-Control-Allow-Origin", -// "*")) != 0) || -// ((rv = nng_http_res_set_header(res, "Access-Control-Allow-Methods", -// "POST,GET,PUT,DELETE,OPTIONS")) != 0) -// || -// ((rv = nng_http_res_set_header(res, "Access-Control-Allow-Headers", -// "*")) != 0) || -// ((rv = nng_http_res_copy_data(res, data, len)) != 0)) { -// nng_http_res_free(res); -// free(data); -// nng_aio_finish(aio, rv); -// return; -// } - -// free(data); -// nng_aio_set_output(aio, 0, res); -// nng_aio_finish(aio, 0); -// } - -// void handle_get_log_files(nng_aio *aio) -// { -// UT_array *log_files = collect_log_files(); -// if (NULL == log_files) { -// NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { -// http_response(aio, error_code.error, result_error); -// }); -// return; -// } - -// size_t len = 1 + strlen("{log_files:[ ]}"); -// utarray_foreach(log_files, char **, log_file) -// { -// // one byte for the comma, -// // the last one will be for the terminating NULL byte -// len += strlen(*log_file) + 1; -// } - -// char *res = malloc(len); -// if (NULL == res) { -// NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { -// http_response(aio, error_code.error, result_error); -// }); -// utarray_free(log_files); -// return; -// } - -// // with the space char to simplify code -// int i = snprintf(res, len, "{log_files:[ "); -// utarray_foreach(log_files, char **, log_file) -// { -// i += snprintf(res + i, len - i, "%s,", *log_file); -// } -// // will write over the last comma/space -// snprintf(res + i - 1, len, "]}"); -// http_ok(aio, res); -// free(res); -// utarray_free(log_files); -// } - static int http_resp_files(nng_aio *aio, void *data, size_t len, const char *disposition) { @@ -365,7 +264,7 @@ int read_file(const char *file_name, void **datap, size_t *lenp) if ((f = fopen(file_name, "rb")) == NULL) { nlog_error("open fail: %s", file_name); - return NEU_ERR_EINTERNAL; + return NEU_ERR_FILE_OPEN_FAILURE; } len = st.st_size; @@ -375,8 +274,8 @@ int read_file(const char *file_name, void **datap, size_t *lenp) goto done; } if (fread(data, 1, len, f) != len) { - rv = errno; - nlog_info("rv = %d", rv); + nlog_error("file read failued, errno = %d", errno); + rv = NEU_ERR_FILE_READ_FAILURE; free(data); goto done; }