From c85337eb78ee6c0a60d0ce0c05d643e65f865ef2 Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Thu, 15 Aug 2019 14:04:17 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=20aaa-12=20add=20log=20cmd=20remote=20RC?= =?UTF-8?q?A=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E4=BA=BA?= =?UTF-8?q?=EF=BC=9Azhangtao=20=E6=A3=80=E8=A7=86=E4=BA=BA=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Platform/build/user.ulog.log_sched.Makefile | 6 +- Platform/user/ulog/log-sched/log_console.c | 2 + Platform/user/ulog/log-sched/log_sched.c | 24 ++- Platform/user/ulog/log-sched/sev_sched.c | 167 ++++++++++++++++++++ Platform/user/ulog/log-sched/sev_sched.h | 10 ++ 5 files changed, 201 insertions(+), 8 deletions(-) create mode 100755 Platform/user/ulog/log-sched/sev_sched.c create mode 100755 Platform/user/ulog/log-sched/sev_sched.h diff --git a/Platform/build/user.ulog.log_sched.Makefile b/Platform/build/user.ulog.log_sched.Makefile index 31d79c12e..3d76e904a 100755 --- a/Platform/build/user.ulog.log_sched.Makefile +++ b/Platform/build/user.ulog.log_sched.Makefile @@ -27,7 +27,7 @@ VPATH = ../user/ulog/log-sched # set the source file, don't used .o because of ... -COMMON_SRCS = log_file.c log_console.c log_pty.c log_remote.c log_common.c log_sched.c +COMMON_SRCS = log_file.c log_console.c log_pty.c log_remote.c log_common.c log_sched.c sev_sched.c # MRS Board Source Files PLAT_LINUX_SRCS = $(COMMON_SRCS) @@ -42,8 +42,8 @@ PLAT_ARM64_LDFLAGS := PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) #gcc libs -ARM64_LIBS := -lopenrpc-arm64 -lulogapi-arm64 -lev -lpthread -LINUX_LIBS := -lopenrpc-linux -lulogapi-linux -lev -lpthread +ARM64_LIBS := -lopenrpc-arm64 -lulogapi-arm64 -lev -lpthread -levent +LINUX_LIBS := -lopenrpc-linux -lulogapi-linux -lev -lpthread -levent # this line must be at below of thus, because of... include ../../Common/common.Makefile diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c index 8ba817721..05beb5067 100755 --- a/Platform/user/ulog/log-sched/log_console.c +++ b/Platform/user/ulog/log-sched/log_console.c @@ -6,6 +6,7 @@ #include "log_types.h" #include "log_common.h" #include "rpc_conn.h" +#include "sev_sched.h" #define LOG_CONF_COSOLE_FILE_NAME "log-console.conf" @@ -92,6 +93,7 @@ void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer rpc_return_error(conn, RET_ERR, "Configuring console of log is faiure"); return; } + log_sev_restart(); rpc_return_null(conn); } diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c index 8d9f4f88d..7afc81922 100755 --- a/Platform/user/ulog/log-sched/log_sched.c +++ b/Platform/user/ulog/log-sched/log_sched.c @@ -13,6 +13,7 @@ #include "log_remote.h" #include "rpc_module.h" #include "ulog_in.h" +#include "sev_sched.h" #define LOG_SCHED_MODULE_NAME "log-sched" @@ -31,6 +32,8 @@ void signal_exit() ULOG_ERR(g_log, "Posting semaphore is failure:%s", strerror(errno)); } + log_sev_exit(); + ULOG_DEBUG(g_log, "Receive shutdown signal"); } @@ -73,23 +76,30 @@ int main(int argc, char **argv) if (server == NULL) { ULOG_ERR(g_log, "start server error"); - return -1; + goto END; } if (sem_init(&g_sem, 0, 0) != 0) { ULOG_ERR(g_log, "Init sem is failure:%s", strerror(errno)); - return -1; + goto END; } - signal(SIGINT, signal_exit); + + + if (log_sev_init() != 0) { + goto END; + } + + //signal(SIGINT, signal_exit); ULOG_INFO(g_log, "Server of log schedule is started"); - /* 注册配置处理函数 */ + /* 注册配置处理函数 */ rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, CONF_LOG_FILE_FUNC, rpc_conf_log_file); rpc_server_regservice(server, SERIVCE_LOG_CONSOLE_NAME, CONF_LOG_CONSOLE_FUNC, rpc_conf_log_console); rpc_server_regservice(server, SERVICE_LOG_PTY_NAME, CONF_LOG_PTY_FUNC, rpc_conf_log_pty); rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_ADD_HOST_FUNC, rpc_conf_log_add_remote); rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_DEL_HOST_FUNC, rpc_conf_log_del_remote); rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_LEVEL_FUNC, rpc_conf_log_remote_level); + /* log_file_t log = {0}; @@ -121,14 +131,18 @@ int main(int argc, char **argv) level.level = 5; rpc_conf_log_remote_level(NULL, &level, sizeof(level), NULL); */ - + /* if (sem_wait(&g_sem) == -1) { ULOG_ERR(g_log, "Waitting semaphore is failure:%s", strerror(errno)); } + */ + log_sev_wait(); ULOG_INFO(g_log, "%s is shutdown", LOG_SCHED_MODULE_NAME); END: + log_sev_free(); + if (g_conf_fp != NULL) { fclose(g_conf_fp); } diff --git a/Platform/user/ulog/log-sched/sev_sched.c b/Platform/user/ulog/log-sched/sev_sched.c new file mode 100755 index 000000000..843053955 --- /dev/null +++ b/Platform/user/ulog/log-sched/sev_sched.c @@ -0,0 +1,167 @@ +#include +#include + +#include +#include + +#include "log_common.h" +#include "ulog_api.h" + +#define SEV_EVENT_TIMEOUT 1 +#define SEV_CMD "systemctl restart rsyslog" + +static struct event_base* g_ev_base = NULL; +static struct event *g_ev_exit = NULL; +static struct event *g_ev_sev = NULL; + +static void signal_exit(evutil_socket_t fd, short event, void *arg) +{ + struct event *signal = arg; + + ULOG_DEBUG(g_log, "%s: got signal %d\n", __FUNCTION__, event_get_signal(signal)); + + event_del(signal); +} + +static void sev_restart(evutil_socket_t fd, short event, void *arg) +{ + struct event *signal = arg; + + ULOG_DEBUG(g_log, "%s: got signal %d\n", __FUNCTION__, event_get_signal(signal)); + + FILE *pp = popen(SEV_CMD, "r"); + if (pp == NULL) { + ULOG_ERR(g_log, "Open cmd:%s is failure:%s", SEV_CMD, strerror(errno)); + goto END; + } + + char buff[256]; + while(fread(buff, 1, sizeof(buff), pp) > 0) + { + ULOG_ERR(g_log, "Executing cmd:%s is failure, response content is %s", SEV_CMD, buff); + break; + } + +END: + if (pp != NULL) { + pclose(pp); + } + event_del(signal); +} + +int called = 0; + +static void +signal_cb(evutil_socket_t fd, short event, void *arg) +{ + struct event *signal = arg; + + printf("signal_cb: got signal %d\n", event_get_signal(signal)); + + if (called >= 2) + event_del(signal); + + called++; +} + + +int log_sev_init() +{ + ULOG_INFO(g_log, "Log service is initiating"); + g_ev_base = event_base_new(); + if (g_ev_base == NULL) { + ULOG_ERR(g_log, "Allacting event base is failure"); + goto FAIL; + } + + g_ev_exit = evsignal_new(g_ev_base, SIGINT, signal_exit, event_self_cbarg()); + if (g_ev_exit == NULL) { + ULOG_ERR(g_log, "Allacting singal of event exit is failure"); + goto FAIL; + } + + /* + g_ev_sev = evsignal_new(g_ev_base, -1, sev_restart, event_self_cbarg()); + if (g_ev_sev == NULL) { + ULOG_ERR(g_log, "Allacting singal of event service is failure"); + goto FAIL; + } + */ + + //struct timeval tv = {SEV_EVENT_TIMEOUT, 0}; + if (event_add(g_ev_exit, NULL) != 0) { + ULOG_ERR(g_log, "Adding event exit is failure"); + goto FAIL; + } + + ULOG_INFO(g_log, "Log service which is initited is success"); + return 0; +FAIL: + log_sev_free(); + ULOG_ERR(g_log, "Log service which is initited is failure"); + return -1; +} + +int log_sev_wait() +{ + int ret = event_base_dispatch(g_ev_base); + if (ret == -1) { + ULOG_ERR(g_log, "service waiting is failure"); + } else if (ret == 1) { + ULOG_INFO(g_log, "No events were registered"); + } + + return ret; +} + +int log_sev_exit() +{ + if (g_ev_exit == NULL) { + ULOG_WARNING(g_log, "Event exit was initiated"); + return -1; + } + + event_active(g_ev_exit, 0, 0); + + return 0; +} + +int log_sev_free() +{ + if (g_ev_sev != NULL) { + event_free(g_ev_sev); + g_ev_sev = NULL; + } + + if (g_ev_exit != NULL) { + event_free(g_ev_exit); + g_ev_exit = NULL; + } + if (g_ev_base != NULL) { + event_base_free(g_ev_base); + g_ev_base = NULL; + } +} + +int log_sev_restart() +{ + if (g_ev_sev == NULL) { + ULOG_WARNING(g_log, "Event service was initiated"); + return -1; + } + + // 如果pending,就不需要添加事件 + if (event_pending(g_ev_sev, 0, NULL)) { + ULOG_DEBUG(g_log, "Not need set event service"); + return 0; + } + + struct timeval tv = {SEV_EVENT_TIMEOUT, 0}; + if (event_add(g_ev_sev, NULL) != 0) { + ULOG_ERR(g_log, "Setting event service is failure"); + return -1; + } + ULOG_DEBUG(g_log, "Setting event service is success"); + + return 0; +} diff --git a/Platform/user/ulog/log-sched/sev_sched.h b/Platform/user/ulog/log-sched/sev_sched.h new file mode 100755 index 000000000..d60bf5c84 --- /dev/null +++ b/Platform/user/ulog/log-sched/sev_sched.h @@ -0,0 +1,10 @@ +#ifndef _SEV_SCHED_H +#define _SEV_SCHED_H + +int log_sev_init(); +int log_sev_wait(); +int log_sev_exit(); +int log_sev_free(); +int log_sev_restart(); + +#endif \ No newline at end of file