Add aaa-12 add log cmd remote

RCA:
SOL:
修改人:zhangtao
检视人:
This commit is contained in:
zhangtaohz 2019-08-15 14:04:17 +08:00
parent f13d796c14
commit c85337eb78
5 changed files with 201 additions and 8 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -0,0 +1,167 @@
#include <errno.h>
#include <signal.h>
#include <event2/event-config.h>
#include <event2/event.h>
#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;
}

View File

@ -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