mirror of
https://github.com/emqx/neuron.git
synced 2026-10-29 23:19:18 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90a1eae13f | ||
|
|
a64449bc8a | ||
|
|
a0ee5346f9 | ||
|
|
8d166fb159 | ||
|
|
191e69ec3e | ||
|
|
6af32bd9fc | ||
|
|
13becdd6af | ||
|
|
5c2ed5f1a3 | ||
|
|
a0bb29c100 | ||
|
|
fb6e92665a | ||
|
|
d7929b5553 | ||
|
|
a11d622fa6 | ||
|
|
c3ed9cc69d | ||
|
|
54dfc3709e | ||
|
|
253f254bd3 | ||
|
|
214c82b508 | ||
|
|
fdfc819c25 | ||
|
|
2ba608f9fe | ||
|
|
89069da064 | ||
|
|
aec03ecaf9 | ||
|
|
40fdc17b57 | ||
|
|
dda1007c50 | ||
|
|
da4295aec9 | ||
|
|
cfb3d073c5 | ||
|
|
cc86ffd497 | ||
|
|
f32188e3c1 | ||
|
|
93af6bfe76 | ||
|
|
5c543e0077 | ||
|
|
f6c591e951 | ||
|
|
eb332bd745 | ||
|
|
d026d1f754 | ||
|
|
fd6c3aca96 | ||
|
|
faee3ab68c | ||
|
|
68587420e0 | ||
|
|
af5195cfb9 | ||
|
|
157dd2521a | ||
|
|
a84aff8f3e | ||
|
|
08a132b2f9 | ||
|
|
bc48b324dd | ||
|
|
b46a319b26 | ||
|
|
3fb6b3409a | ||
|
|
5d195d5cb1 | ||
|
|
9833af02da | ||
|
|
5d2cff0aef | ||
|
|
4be97991fe | ||
|
|
943cc82caa | ||
|
|
e0d932e2e3 | ||
|
|
e7fc946ae6 |
@@ -8,8 +8,8 @@ on:
|
||||
- published
|
||||
|
||||
env:
|
||||
VERSION: 2.3.4
|
||||
UI_VERSION: 2.3.2
|
||||
VERSION: 2.3.11
|
||||
UI_VERSION: 2.3.9
|
||||
BRANCH: v2.3
|
||||
|
||||
jobs:
|
||||
@@ -96,7 +96,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: delete old daily-pre-release
|
||||
uses: dev-drprasad/delete-tag-and-release@v0.2.0
|
||||
uses: dev-drprasad/delete-tag-and-release@v0.2.1
|
||||
with:
|
||||
delete_release: true
|
||||
tag_name: ${{ env.BRANCH }}-daily
|
||||
@@ -258,4 +258,4 @@ jobs:
|
||||
upload_url: ${{ github.event.release.upload_url }}
|
||||
asset_path: ./${{ steps.package.outputs.name}}
|
||||
asset_name: ${{ matrix.type }}-${{ env.VERSION }}-linux-${{ matrix.arch }}.${{ matrix.package }}
|
||||
asset_content_type: application/octet-stream
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
+8
-6
@@ -59,6 +59,9 @@ else()
|
||||
link_directories(/usr/local/lib)
|
||||
endif()
|
||||
|
||||
set(PERSIST_SOURCES
|
||||
src/persist/persist.c
|
||||
src/persist/json/persist_json_plugin.c)
|
||||
aux_source_directory(src/parser NEURON_SRC_PARSE)
|
||||
set(NEURON_BASE_SOURCES
|
||||
src/base/tag.c
|
||||
@@ -74,7 +77,8 @@ set(NEURON_BASE_SOURCES
|
||||
src/utils/neu_jwt.c
|
||||
src/utils/base64.c
|
||||
src/utils/async_queue.c
|
||||
src/utils/mem_cache.c)
|
||||
src/utils/mem_cache.c
|
||||
${PERSIST_SOURCES})
|
||||
|
||||
add_library(neuron-base SHARED)
|
||||
target_sources(neuron-base PRIVATE ${NEURON_BASE_SOURCES} ${NEURON_SRC_PARSE})
|
||||
@@ -89,9 +93,6 @@ add_dependencies(neuron-base neuron-version)
|
||||
#find_package(MbedTLS)
|
||||
target_link_libraries(neuron-base mbedtls mbedx509 mbedcrypto)
|
||||
|
||||
set(PERSIST_SOURCES
|
||||
src/persist/persist.c
|
||||
src/persist/json/persist_json_plugin.c)
|
||||
set(NEURON_SOURCES
|
||||
src/main.c
|
||||
src/argparse.c
|
||||
@@ -123,8 +124,7 @@ set(NEURON_SOURCES
|
||||
plugins/restful/proxy.c
|
||||
plugins/restful/websocket.c
|
||||
plugins/monitor/monitor.c
|
||||
plugins/monitor/metric_handle.c
|
||||
${PERSIST_SOURCES})
|
||||
plugins/monitor/metric_handle.c)
|
||||
|
||||
set(CMAKE_BUILD_RPATH ./)
|
||||
add_executable(neuron)
|
||||
@@ -147,6 +147,8 @@ file(COPY ${CMAKE_SOURCE_DIR}/default_plugins.json DESTINATION ${CMAKE_BINARY_DI
|
||||
add_subdirectory(plugins/modbus)
|
||||
add_subdirectory(plugins/mqtt)
|
||||
add_subdirectory(plugins/ekuiper)
|
||||
add_subdirectory(plugins/file)
|
||||
|
||||
|
||||
add_subdirectory(simulator)
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -24,7 +24,9 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "adapter/adapter_info.h"
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include "adapter_info.h"
|
||||
#include "persist/json/persist_json_plugin.h"
|
||||
|
||||
typedef neu_json_plugin_req_plugin_t neu_persist_plugin_info_t;
|
||||
@@ -84,12 +86,14 @@ static inline void neu_persist_user_info_fini(neu_persist_user_info_t *info)
|
||||
* Create persister.
|
||||
* @return 0 on success, -1 otherwise.
|
||||
*/
|
||||
int neu_persister_create();
|
||||
int neu_persister_create(const char *schema_dir);
|
||||
/**
|
||||
* Destroy perister.
|
||||
*/
|
||||
void neu_persister_destroy();
|
||||
|
||||
sqlite3 *neu_persister_get_db();
|
||||
|
||||
/**
|
||||
* Persist nodes.
|
||||
* @param node_info neu_persist_node_info_t.
|
||||
@@ -306,4 +310,4 @@ int neu_persister_delete_user(const char *user_name);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -94,7 +94,7 @@ inline static const char *neu_type_string(neu_type_e type)
|
||||
return "";
|
||||
}
|
||||
|
||||
#define NEU_VALUE_SIZE 128
|
||||
#define NEU_VALUE_SIZE 512
|
||||
typedef union {
|
||||
bool boolean;
|
||||
int8_t i8;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/plugins")
|
||||
|
||||
set(CMAKE_BUILD_RPATH ./)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugins/file/file.json DESTINATION ${CMAKE_BINARY_DIR}/plugins/schema/)
|
||||
|
||||
set(PLUGIN_NAME plugin-file)
|
||||
set(PLUGIN_SOURCES file_plugin.c file_req.c)
|
||||
add_library(${PLUGIN_NAME} SHARED)
|
||||
target_include_directories(${PLUGIN_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include/neuron)
|
||||
target_sources(${PLUGIN_NAME} PRIVATE ${PLUGIN_SOURCES})
|
||||
target_link_libraries(${PLUGIN_NAME} neuron-base)
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"tag_regex": [
|
||||
{
|
||||
"type": 13,
|
||||
"regex": ""
|
||||
}
|
||||
],
|
||||
"file_length": {
|
||||
"name": "file_length",
|
||||
"description": "",
|
||||
"attribute": "required",
|
||||
"type": "int",
|
||||
"default": 512,
|
||||
"valid": {
|
||||
"min": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2022 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.
|
||||
**/
|
||||
|
||||
#include "file_req.h"
|
||||
|
||||
static neu_plugin_t *driver_open(void);
|
||||
|
||||
static int driver_close(neu_plugin_t *plugin);
|
||||
static int driver_init(neu_plugin_t *plugin);
|
||||
static int driver_uninit(neu_plugin_t *plugin);
|
||||
static int driver_start(neu_plugin_t *plugin);
|
||||
static int driver_stop(neu_plugin_t *plugin);
|
||||
static int driver_config(neu_plugin_t *plugin, const char *config);
|
||||
static int driver_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
|
||||
void *data);
|
||||
|
||||
static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group);
|
||||
static int driver_validate_tag(neu_plugin_t *plugin, neu_datatag_t *tag);
|
||||
static int driver_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value);
|
||||
|
||||
static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
.open = driver_open,
|
||||
.close = driver_close,
|
||||
.init = driver_init,
|
||||
.uninit = driver_uninit,
|
||||
.start = driver_start,
|
||||
.stop = driver_stop,
|
||||
.setting = driver_config,
|
||||
.request = driver_request,
|
||||
|
||||
.driver.validate_tag = driver_validate_tag,
|
||||
.driver.group_timer = driver_group_timer,
|
||||
.driver.write_tag = driver_write,
|
||||
};
|
||||
|
||||
const neu_plugin_module_t neu_plugin_module = {
|
||||
.version = NEURON_PLUGIN_VER_1_0,
|
||||
.module_name = "file",
|
||||
.module_descr = "The plugin is used to read and write files.",
|
||||
.module_descr_zh = "该插件用于读写文件。",
|
||||
.intf_funs = &plugin_intf_funs,
|
||||
.kind = NEU_PLUGIN_KIND_SYSTEM,
|
||||
.type = NEU_NA_TYPE_DRIVER,
|
||||
.display = true,
|
||||
.single = false,
|
||||
};
|
||||
|
||||
static neu_plugin_t *driver_open(void)
|
||||
{
|
||||
neu_plugin_t *plugin = calloc(1, sizeof(neu_plugin_t));
|
||||
|
||||
neu_plugin_common_init(&plugin->common);
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
static int driver_close(neu_plugin_t *plugin)
|
||||
{
|
||||
free(plugin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_init(neu_plugin_t *plugin)
|
||||
{
|
||||
plugin->events = neu_event_new();
|
||||
plugin->common.link_state = NEU_NODE_LINK_STATE_CONNECTED;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_uninit(neu_plugin_t *plugin)
|
||||
{
|
||||
neu_event_close(plugin->events);
|
||||
plog_info(plugin, "node; file uininit ok");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_start(neu_plugin_t *plugin)
|
||||
{
|
||||
(void) plugin;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_stop(neu_plugin_t *plugin)
|
||||
{
|
||||
(void) plugin;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_config(neu_plugin_t *plugin, const char *config)
|
||||
{
|
||||
char * err_param = NULL;
|
||||
neu_json_elem_t file_length = { .name = "file_length", .t = NEU_JSON_INT };
|
||||
|
||||
int ret = neu_parse_param((char *) config, &err_param, 1, &file_length);
|
||||
if (ret != 0) {
|
||||
plog_warn(plugin, "config:%s, decode error: %s", config, err_param);
|
||||
free(err_param);
|
||||
return -1;
|
||||
}
|
||||
|
||||
plugin->file_length = file_length.v.val_int;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
|
||||
void *data)
|
||||
{
|
||||
(void) plugin;
|
||||
(void) head;
|
||||
(void) data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_validate_tag(neu_plugin_t *plugin, neu_datatag_t *tag)
|
||||
{
|
||||
plog_debug(plugin, "validate tag success, name:%s, address:%s,type:%d ",
|
||||
tag->name, tag->address, tag->type);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
|
||||
{
|
||||
return file_group_timer(plugin, group);
|
||||
}
|
||||
|
||||
static int driver_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value)
|
||||
{
|
||||
return file_write(plugin, req, tag, value);
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2022 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.
|
||||
**/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "file_req.h"
|
||||
|
||||
struct file_group_data {
|
||||
UT_array *tags;
|
||||
char * group;
|
||||
};
|
||||
|
||||
static void plugin_group_free(neu_plugin_group_t *pgp);
|
||||
|
||||
int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
|
||||
{
|
||||
struct file_group_data *gd = NULL;
|
||||
int length = plugin->file_length;
|
||||
|
||||
if (group->user_data == NULL) {
|
||||
gd = calloc(1, sizeof(struct file_group_data));
|
||||
group->user_data = gd;
|
||||
group->group_free = plugin_group_free;
|
||||
utarray_new(gd->tags, &ut_ptr_icd);
|
||||
|
||||
gd->group = strdup(group->group_name);
|
||||
}
|
||||
|
||||
utarray_foreach(group->tags, neu_datatag_t *, tag)
|
||||
{
|
||||
FILE * f = NULL;
|
||||
struct stat st = { 0 };
|
||||
char * buf = calloc(1, length);
|
||||
neu_dvalue_t dvalue = { 0 };
|
||||
|
||||
if (stat(tag->address, &st) != 0) {
|
||||
dvalue.type = NEU_TYPE_ERROR;
|
||||
dvalue.value.i32 = NEU_ERR_FILE_NOT_EXIST;
|
||||
goto dvalue_result;
|
||||
}
|
||||
if ((f = fopen(tag->address, "rb+")) == NULL) {
|
||||
nlog_error("open file:%s", tag->address);
|
||||
dvalue.type = NEU_TYPE_ERROR;
|
||||
dvalue.value.i32 = NEU_ERR_PLUGIN_READ_FAILURE;
|
||||
goto dvalue_result;
|
||||
}
|
||||
|
||||
int ret = fread(buf, sizeof(char), length, f);
|
||||
if (ret == 0) {
|
||||
nlog_error("read file failed:%s", tag->address);
|
||||
dvalue.type = NEU_TYPE_ERROR;
|
||||
dvalue.value.i32 = NEU_ERR_PLUGIN_READ_FAILURE;
|
||||
goto dvalue_result;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
int size = ftell(f);
|
||||
if (size > length) {
|
||||
dvalue.type = NEU_TYPE_ERROR;
|
||||
dvalue.value.i32 = NEU_ERR_PLUGIN_READ_FAILURE;
|
||||
goto dvalue_result;
|
||||
}
|
||||
|
||||
dvalue.type = NEU_TYPE_STRING;
|
||||
strncpy(dvalue.value.str, buf, strlen(buf));
|
||||
fclose(f);
|
||||
|
||||
dvalue_result:
|
||||
plugin->common.adapter_callbacks->driver.update(
|
||||
plugin->common.adapter, group->group_name, tag->name, dvalue);
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void plugin_group_free(neu_plugin_group_t *pgp)
|
||||
{
|
||||
struct file_group_data *gd = (struct file_group_data *) pgp->user_data;
|
||||
|
||||
utarray_foreach(gd->tags, neu_datatag_t **, tag) { free(*tag); }
|
||||
|
||||
utarray_free(gd->tags);
|
||||
free(gd->group);
|
||||
|
||||
free(gd);
|
||||
}
|
||||
|
||||
int file_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value)
|
||||
{
|
||||
FILE *fp = fopen(tag->address, "w");
|
||||
int ret = 0;
|
||||
|
||||
if (0 == fwrite(value.str, sizeof(char), strlen(value.str), fp)) {
|
||||
nlog_error("write file failed:%s", tag->address);
|
||||
ret = NEU_ERR_PLUGIN_WRITE_FAILURE;
|
||||
goto dvalue_result;
|
||||
}
|
||||
|
||||
neu_datatag_string_ltoh(value.str, strlen(value.str));
|
||||
|
||||
dvalue_result:
|
||||
fclose(fp);
|
||||
plugin->common.adapter_callbacks->driver.write_response(
|
||||
plugin->common.adapter, req, ret);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2022 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_M_PLUGIN_file_REQ_H_
|
||||
#define _NEU_M_PLUGIN_file_REQ_H_
|
||||
|
||||
#include <neuron.h>
|
||||
|
||||
struct neu_plugin {
|
||||
neu_plugin_common_t common;
|
||||
|
||||
neu_events_t *events;
|
||||
|
||||
int file_length;
|
||||
};
|
||||
|
||||
int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group);
|
||||
int file_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value);
|
||||
|
||||
#endif
|
||||
@@ -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:
|
||||
|
||||
+42
-153
@@ -44,32 +44,51 @@
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
|
||||
VALIDATE_JWT(aio);
|
||||
|
||||
rv = mkdir("neuron_logs", S_IRWXU);
|
||||
if (0 != rv && EEXIST == errno) {
|
||||
rv = 0;
|
||||
/* check whether the neuron directory exists */
|
||||
rv = access("../neuron", F_OK);
|
||||
if (0 != rv) {
|
||||
nlog_error("The neuron directory does not exists");
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_FILE_NOT_EXIST, {
|
||||
http_response(aio, error_code.error, result_error);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// check if coredump exists
|
||||
rv = access("./core", F_OK);
|
||||
if (0 == rv) {
|
||||
rv = system("cp -r ./core neuron_logs");
|
||||
if (0 != rv) {
|
||||
nlog_error("failed to copy coredump file");
|
||||
rv = system("rm -rf /tmp/neuron_debug.tar.gz");
|
||||
nlog_warn("remove old neuron_debug.tar.gz, rv: %d", rv);
|
||||
/* tar the neuron directory */
|
||||
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, {
|
||||
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, {
|
||||
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, {
|
||||
http_response(aio, error_code.error, result_error);
|
||||
});
|
||||
@@ -77,48 +96,18 @@ void handle_logs_files(nng_aio *aio)
|
||||
}
|
||||
}
|
||||
|
||||
rv = system("cp -r ./logs neuron_logs");
|
||||
if (0 != rv) {
|
||||
nlog_error("failed to copy logs file");
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, {
|
||||
http_response(aio, error_code.error, result_error);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
rv = system("tar -zcvf neuron_logs.tar.gz neuron_logs");
|
||||
if (0 != rv) {
|
||||
nlog_error("failed to create neuron_logs.tar.gz");
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, {
|
||||
http_response(aio, error_code.error, result_error);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
strcpy(path, "neuron_logs.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, { http_response(aio, error_code.error, result_error); });
|
||||
return;
|
||||
}
|
||||
|
||||
// delete neuron_logs folder from build
|
||||
rv = system("rm -rf neuron_logs.tar.gz neuron_logs");
|
||||
if (0 != rv) {
|
||||
nlog_error("failed to delete neuron_logs folder");
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_COMMAND_EXECUTION_FAILED, {
|
||||
http_response(aio, error_code.error, result_error);
|
||||
});
|
||||
return;
|
||||
}
|
||||
/* handle http response */
|
||||
rv = http_resp_files(aio, data, len,
|
||||
"attachment; filename=neuron_debug.tar.gz");
|
||||
|
||||
// handle http response
|
||||
strcpy(disp, "attachment; filename=neuron_logs.tar.gz");
|
||||
rv = http_resp_files(aio, data, len, disp);
|
||||
if (0 != rv) {
|
||||
return;
|
||||
}
|
||||
nlog_notice("download neuron_debug.tar.gz, ret: %d", rv);
|
||||
}
|
||||
|
||||
void handle_log_level(nng_aio *aio)
|
||||
@@ -185,106 +174,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 +254,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 +264,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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ static inline int reset_password()
|
||||
"$5$PwFeXpBBIBZuZdZl$fP8fFPWCLoaWcnVXVSR.3Xi8TEqCvX92gjhowNNn6S4",
|
||||
};
|
||||
|
||||
int rv = neu_persister_create();
|
||||
int rv = neu_persister_create(g_config_dir);
|
||||
if (0 != rv) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
@@ -389,7 +389,7 @@ ssize_t neu_conn_recv(neu_conn_t *conn, uint8_t *buf, ssize_t len)
|
||||
conn->fd, len, ret, strerror(errno), errno);
|
||||
}
|
||||
|
||||
if (errno == EPIPE || ret == -1) {
|
||||
if (errno == EPIPE || ret == -1 || ret == 0) {
|
||||
conn_disconnect(conn);
|
||||
}
|
||||
|
||||
@@ -842,7 +842,7 @@ static void conn_tcp_server_add_client(neu_conn_t *conn, int fd,
|
||||
static void conn_tcp_server_del_client(neu_conn_t *conn, int fd)
|
||||
{
|
||||
for (int i = 0; i < conn->param.params.tcp_server.max_link; i++) {
|
||||
if (conn->tcp_server.clients[i].fd == fd) {
|
||||
if (fd > 0 && conn->tcp_server.clients[i].fd == fd) {
|
||||
close(fd);
|
||||
conn->tcp_server.clients[i].fd = 0;
|
||||
conn->tcp_server.n_client -= 1;
|
||||
|
||||
@@ -88,6 +88,11 @@ static void *event_loop(void *arg)
|
||||
if (ret == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ret == -1 && errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ret == -1 || events->stop) {
|
||||
zlog_warn(neuron, "event loop exit, errno: %s(%d), stop: %d",
|
||||
strerror(errno), errno, events->stop);
|
||||
|
||||
+3
-2
@@ -42,7 +42,7 @@ static void sig_handler(int sig)
|
||||
{
|
||||
nlog_warn("recv sig: %d", sig);
|
||||
|
||||
if (sig != SIGABRT) {
|
||||
if (sig == SIGINT || sig == SIGTERM) {
|
||||
neu_manager_destroy(g_manager);
|
||||
neu_persister_destroy();
|
||||
}
|
||||
@@ -58,6 +58,7 @@ static int neuron_run(const neu_cli_args_t *args)
|
||||
signal(SIGINT, sig_handler);
|
||||
signal(SIGTERM, sig_handler);
|
||||
signal(SIGABRT, sig_handler);
|
||||
signal(SIGSEGV, sig_handler);
|
||||
|
||||
// try to enable core dump
|
||||
rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
|
||||
@@ -65,7 +66,7 @@ static int neuron_run(const neu_cli_args_t *args)
|
||||
nlog_warn("neuron process failed enable core dump, ignore");
|
||||
}
|
||||
|
||||
rv = neu_persister_create();
|
||||
rv = neu_persister_create(args->config_dir);
|
||||
assert(rv == 0);
|
||||
|
||||
zlog_notice(neuron, "neuron process, daemon: %d, version: %s (%s %s)",
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
#include "json/json.h"
|
||||
|
||||
#include "persist_json_plugin.h"
|
||||
#include "persist/json/persist_json_plugin.h"
|
||||
|
||||
int neu_json_decode_plugin_req(char *buf, neu_json_plugin_req_t **result)
|
||||
{
|
||||
|
||||
@@ -430,7 +430,7 @@ static int apply_schemas(sqlite3 *db, const char *dir)
|
||||
return rv;
|
||||
}
|
||||
|
||||
int neu_persister_create()
|
||||
int neu_persister_create(const char *schema_dir)
|
||||
{
|
||||
int rv = sqlite3_open(db_file, &global_db);
|
||||
|
||||
@@ -455,7 +455,7 @@ int neu_persister_create()
|
||||
return -1;
|
||||
}
|
||||
|
||||
rv = apply_schemas(global_db, g_config_dir);
|
||||
rv = apply_schemas(global_db, schema_dir);
|
||||
if (rv != 0) {
|
||||
nlog_fatal("db apply schemas fail");
|
||||
sqlite3_close(global_db);
|
||||
@@ -465,6 +465,11 @@ int neu_persister_create()
|
||||
return 0;
|
||||
}
|
||||
|
||||
sqlite3 *neu_persister_get_db()
|
||||
{
|
||||
return global_db;
|
||||
}
|
||||
|
||||
void neu_persister_destroy()
|
||||
{
|
||||
sqlite3_close(global_db);
|
||||
|
||||
Reference in New Issue
Block a user