From 34647e8ae0f410036daa2e75dbb60b38bcb220b6 Mon Sep 17 00:00:00 2001 From: fengzero Date: Thu, 14 Oct 2021 07:10:31 +0000 Subject: [PATCH] Add signal processing Make the program exit normally and make libasan memory detection take effect --- CMakeLists.txt | 5 ++-- plugins/opcua/plugin_opcua.c | 2 +- plugins/restful/handle.c | 28 ++++------------- .../{get_ttys_handle.c => normal_handle.c} | 17 ++++++++++- plugins/restful/normal_handle.h | 30 +++++++++++++++++++ plugins/restful/rw_handle.c | 1 + .../{get_ttys_handle.h => rw_handle.h} | 7 ++--- src/adapter/adapter.c | 17 ++++++----- src/core/neu_manager.c | 20 ++++++++----- src/core/neu_manager.h | 1 + src/main.c | 15 ++++++++-- 11 files changed, 94 insertions(+), 49 deletions(-) rename plugins/restful/{get_ttys_handle.c => normal_handle.c} (92%) create mode 100644 plugins/restful/normal_handle.h create mode 100644 plugins/restful/rw_handle.c rename plugins/restful/{get_ttys_handle.h => rw_handle.h} (87%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 81f842f6..3e3adda2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,13 +81,14 @@ set(NEURON_SOURCES src/core/neu_datatag_manager.c src/adapter/adapter.c plugins/restful/handle.c + plugins/restful/normal_handle.c + plugins/restful/rw_handle.c plugins/restful/adapter_handle.c plugins/restful/datatag_handle.c plugins/restful/group_config_handle.c plugins/restful/plugin_handle.c plugins/restful/rest.c - plugins/restful/http.c - plugins/restful/get_ttys_handle.c) + plugins/restful/http.c) set(NEURON_INCLUDE_DIRECTORIES include src plugins) add_executable(neuron) diff --git a/plugins/opcua/plugin_opcua.c b/plugins/opcua/plugin_opcua.c index 4f9f81a0..e73b7169 100644 --- a/plugins/opcua/plugin_opcua.c +++ b/plugins/opcua/plugin_opcua.c @@ -129,7 +129,7 @@ static int opcua_plugin_init(neu_plugin_t *plugin) static int opcua_plugin_uninit(neu_plugin_t *plugin) { - open62541_client_close(plugin->client); + // open62541_client_close(plugin->client); free(plugin->handle_context); log_info("Uninitialize plugin: %s", neu_plugin_module.module_name); return 0; diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index c5a2d9ff..28b3e43a 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -30,17 +30,14 @@ #include "adapter_handle.h" #include "datatag_handle.h" -#include "get_ttys_handle.h" #include "group_config_handle.h" #include "http.h" +#include "normal_handle.h" #include "plugin_handle.h" +#include "rw_handle.h" #include "handle.h" -static void ping(nng_aio *aio); -static void login(nng_aio *aio); -static void logout(nng_aio *aio); - struct neu_rest_handle_ctx { void *plugin; }; @@ -61,19 +58,19 @@ struct neu_rest_handler api_handlers[] = { .method = NEU_REST_METHOD_POST, .type = NEU_REST_HANDLER_FUNCTION, .url = "/api/v2/ping", - .value.handler = ping, + .value.handler = handle_ping, }, { .method = NEU_REST_METHOD_POST, .type = NEU_REST_HANDLER_FUNCTION, .url = "/api/v2/login", - .value.handler = login, + .value.handler = handle_login, }, { .method = NEU_REST_METHOD_POST, .type = NEU_REST_HANDLER_FUNCTION, .url = "/api/v2/logout", - .value.handler = logout, + .value.handler = handle_logout, }, { .method = NEU_REST_METHOD_POST, @@ -195,21 +192,6 @@ void neu_rest_api_handler(const struct neu_rest_handler **handlers, *size = sizeof(api_handlers) / sizeof(struct neu_rest_handler); } -static void ping(nng_aio *aio) -{ - http_ok(aio, "{\"status\": \"OK\"}"); -} - -static void login(nng_aio *aio) -{ - (void) aio; -} - -static void logout(nng_aio *aio) -{ - (void) aio; -} - neu_rest_handle_ctx_t *neu_rest_init_ctx(void *plugin) { rest_ctx = calloc(1, sizeof(neu_rest_handle_ctx_t)); diff --git a/plugins/restful/get_ttys_handle.c b/plugins/restful/normal_handle.c similarity index 92% rename from plugins/restful/get_ttys_handle.c rename to plugins/restful/normal_handle.c index 44bc3c06..5bcecc1b 100644 --- a/plugins/restful/get_ttys_handle.c +++ b/plugins/restful/normal_handle.c @@ -30,7 +30,7 @@ #include "handle.h" #include "http.h" -#include "get_ttys_handle.h" +#include "normal_handle.h" #define DEV_TTY_LENTH 261 @@ -86,3 +86,18 @@ void handle_get_ttys(nng_aio *aio) free(ttys_res.ttys); } } + +void handle_ping(nng_aio *aio) +{ + http_ok(aio, "{\"status\": \"OK\"}"); +} + +void handle_login(nng_aio *aio) +{ + (void) aio; +} + +void handle_logout(nng_aio *aio) +{ + (void) aio; +} \ No newline at end of file diff --git a/plugins/restful/normal_handle.h b/plugins/restful/normal_handle.h new file mode 100644 index 00000000..bd6a363f --- /dev/null +++ b/plugins/restful/normal_handle.h @@ -0,0 +1,30 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#ifndef _NEU_NORMAL_HANDLE_H_ +#define _NEU_NORMAL_HANDLE_H_ + +#include + +void handle_get_ttys(nng_aio *aio); +void handle_ping(nng_aio *aio); +void handle_login(nng_aio *aio); +void handle_logout(nng_aio *aio); + +#endif \ No newline at end of file diff --git a/plugins/restful/rw_handle.c b/plugins/restful/rw_handle.c new file mode 100644 index 00000000..911f0eb8 --- /dev/null +++ b/plugins/restful/rw_handle.c @@ -0,0 +1 @@ +#include "rw_handle.h" \ No newline at end of file diff --git a/plugins/restful/get_ttys_handle.h b/plugins/restful/rw_handle.h similarity index 87% rename from plugins/restful/get_ttys_handle.h rename to plugins/restful/rw_handle.h index e9916378..87bed095 100644 --- a/plugins/restful/get_ttys_handle.h +++ b/plugins/restful/rw_handle.h @@ -16,11 +16,8 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **/ -#ifndef _NEU_TTY_REST_TTY_HANDLE_H_ -#define _NEU_TTY_REST_TTY_HANDLE_H_ -#include - -void handle_get_ttys(nng_aio *aio); +#ifndef _NEU_RW_HANDLE_H_ +#define _NEU_RW_HANDLE_H_ #endif \ No newline at end of file diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index 594dfa0a..7145df25 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -191,7 +191,8 @@ static void adapter_loop(void *arg) rv = nng_recvmsg(adapter->sock, &msg, 0); if (rv != 0) { - log_warn("Manage pipe no message received"); + if (rv != NNG_ETIMEDOUT) + log_warn("Manage pipe no message received %d", rv); continue; } @@ -331,13 +332,11 @@ static void adapter_loop(void *arg) const neu_plugin_intf_funs_t *intf_funs; neu_request_t req; if (adapter->plugin_module) { - ssize_t size; neu_reqresp_read_resp_t read_resp; read_resp.grp_config = cmd_ptr->grp_config; void * buf = core_databuf_get_ptr(cmd_ptr->databuf); size_t buf_len = core_databuf_get_len(cmd_ptr->databuf); - size = - neu_dvalue_deserialize(buf, buf_len, &read_resp.data_val); + neu_dvalue_deserialize(buf, buf_len, &read_resp.data_val); intf_funs = adapter->plugin_module->intf_funs; req.req_id = adapter_get_req_id(adapter); @@ -388,13 +387,11 @@ static void adapter_loop(void *arg) const neu_plugin_intf_funs_t *intf_funs; neu_request_t req; if (adapter->plugin_module) { - ssize_t size; neu_reqresp_write_resp_t write_resp; write_resp.grp_config = cmd_ptr->grp_config; void * buf = core_databuf_get_ptr(cmd_ptr->databuf); size_t buf_len = core_databuf_get_len(cmd_ptr->databuf); - size = - neu_dvalue_deserialize(buf, buf_len, &write_resp.data_val); + neu_dvalue_deserialize(buf, buf_len, &write_resp.data_val); intf_funs = adapter->plugin_module->intf_funs; req.req_id = adapter_get_req_id(adapter); @@ -444,7 +441,7 @@ static int adapter_command(neu_adapter_t *adapter, neu_request_t *cmd, return (-1); } - log_info("Get command from plugin %d", cmd->req_type); + log_info("Get command from plugin %d, %s", cmd->req_type, adapter->name); switch (cmd->req_type) { case NEU_REQRESP_SUBSCRIBE_NODE: { ADAPTER_SEND_MSG(adapter, cmd, rv, MSG_CMD_SUBSCRIBE_NODE, @@ -876,6 +873,7 @@ neu_adapter_t *neu_adapter_create(neu_adapter_info_t *info, neu_manager_t * manager) { neu_adapter_t *adapter; + nng_duration recv_timeout = 100; if (manager == NULL) { log_error("Create adapter with NULL manager"); @@ -948,6 +946,9 @@ neu_adapter_t *neu_adapter_create(neu_adapter_info_t *info, neu_panic("The adapter(%s) can't open pipe", adapter->name); } + nng_setopt(adapter->sock, NNG_OPT_RECVTIMEO, &recv_timeout, + sizeof(recv_timeout)); + log_info("Success to create adapter: %s", adapter->name); return adapter; } diff --git a/src/core/neu_manager.c b/src/core/neu_manager.c index f1237eb6..61c03319 100644 --- a/src/core/neu_manager.c +++ b/src/core/neu_manager.c @@ -552,13 +552,12 @@ static int manager_unreg_adapter(neu_manager_t *manager, adapter_id_t id) nng_mtx_lock(manager->adapters_mtx); index = find_reg_adapter_index_by_id(reg_adapters, id); if (index != SIZE_MAX) { - adapter_reg_entity_t *reg_entity; + // adapter_reg_entity_t *reg_entity; - reg_entity = (adapter_reg_entity_t *) vector_get(reg_adapters, index); - adapter = reg_entity->adapter; - datatag_mng = reg_entity->datatag_manager; - cv = reg_entity->cv; - vector_erase(reg_adapters, index); + // reg_entity = (adapter_reg_entity_t *) vector_get(reg_adapters, + // index); adapter = reg_entity->adapter; datatag_mng = + // reg_entity->datatag_manager; cv = reg_entity->cv; + // vector_erase(reg_adapters, index); } nng_mtx_unlock(manager->adapters_mtx); @@ -1325,9 +1324,9 @@ static void manager_loop(void *arg) } log_info("End message loop of neu_manager"); - remove_default_grp_configs(manager); stop_and_unreg_bind_adapters(manager); unregister_all_reg_plugins(manager); + remove_default_grp_configs(manager); nng_close(manager_bind->mng_sock); return; } @@ -1368,6 +1367,13 @@ neu_manager_t *neu_manager_create() return manager; } +void neu_manager_stop(neu_manager_t *manager) +{ + nng_mtx_lock(manager->mtx); + manager->stop = true; + nng_mtx_unlock(manager->mtx); +} + void neu_manager_destroy(neu_manager_t *manager) { nng_thread_destroy(manager->thrd); diff --git a/src/core/neu_manager.h b/src/core/neu_manager.h index e4ba4b59..7c7fb4bd 100644 --- a/src/core/neu_manager.h +++ b/src/core/neu_manager.h @@ -28,6 +28,7 @@ typedef struct neu_manager neu_manager_t; neu_manager_t *neu_manager_create(); +void neu_manager_stop(neu_manager_t *manager); void neu_manager_destroy(neu_manager_t *manager); const char *neu_manager_get_url(neu_manager_t *manager); diff --git a/src/main.c b/src/main.c index 3e0c4113..2983b4cb 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,7 @@ **/ #include +#include #include #include #include @@ -36,6 +37,7 @@ static nng_mtx *log_mtx; FILE * g_logfile; +neu_manager_t * manager; static void log_lock(bool lock, void *udata) { @@ -52,7 +54,7 @@ static void init() nng_mtx_alloc(&log_mtx); log_set_lock(log_lock, log_mtx); log_set_level(LOG_DEBUG); - FILE *g_logfile = fopen("neuron.log", "a"); + g_logfile = fopen("neuron.log", "a"); if (g_logfile == NULL) { fprintf(stderr, "Failed to open logfile when" @@ -79,6 +81,12 @@ static int read_neuron_config() return neu_config_init("./neuron.yaml"); } +static void sig_handler(int sig) +{ + log_warn("recv sig: %d", sig); + neu_manager_stop(manager); +} + int main(int argc, char *argv[]) { @@ -113,7 +121,10 @@ int main(int argc, char *argv[]) goto main_end; } - neu_manager_t *manager; + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + signal(SIGABRT, sig_handler); + log_info("running neuron main process, daemon: %d", is_daemon); manager = neu_manager_create(); if (manager == NULL) {