From 3b27ad602adecc8b2e5970659f210716a6de7d0e Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Wed, 31 Jul 2019 17:32:43 +0800 Subject: [PATCH] =?UTF-8?q?Mod=20=20aaa-12=20modify=20remote=20and=20pty?= =?UTF-8?q?=20RCA=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.openrpc.Makefile | 2 +- Platform/build/user.trace.api.Makefile | 2 +- Platform/build/user.ulog.log_sched.Makefile | 8 +- Platform/common/rpc/rpc_module.h | 2 +- Platform/user/ulog/log-sched/log_common.c | 27 ++-- .../ulog/log-sched/{log_tty.c => log_pty.c} | 40 +++--- Platform/user/ulog/log-sched/log_pty.h | 10 ++ Platform/user/ulog/log-sched/log_remote.c | 123 ++++++++++++++---- Platform/user/ulog/log-sched/log_remote.h | 3 +- Platform/user/ulog/log-sched/log_sched.c | 46 +++++-- Platform/user/ulog/log-sched/log_tty.h | 10 -- 11 files changed, 193 insertions(+), 80 deletions(-) rename Platform/user/ulog/log-sched/{log_tty.c => log_pty.c} (50%) create mode 100755 Platform/user/ulog/log-sched/log_pty.h delete mode 100755 Platform/user/ulog/log-sched/log_tty.h diff --git a/Platform/build/user.openrpc.Makefile b/Platform/build/user.openrpc.Makefile index fbdf5680d..cdcb0ec13 100755 --- a/Platform/build/user.openrpc.Makefile +++ b/Platform/build/user.openrpc.Makefile @@ -43,7 +43,7 @@ PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) #gcc libs -ARM64_LIBS := ../thirdparty/arm64/libev-arm64.so -lpthread -lm +ARM64_LIBS := ../thirdparty/arm64/libev-arm64.so -lpthread -lm LINUX_LIBS := ../thirdparty/x86_64/libev-linux.so -lpthread -lm ifeq ($(PLAT_ARM64), TRUE) diff --git a/Platform/build/user.trace.api.Makefile b/Platform/build/user.trace.api.Makefile index e69b124f3..10c1e2209 100755 --- a/Platform/build/user.trace.api.Makefile +++ b/Platform/build/user.trace.api.Makefile @@ -38,7 +38,7 @@ PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/trace -I../user/netlink_ua PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) -PLAT_ARM64_LDFLAGS := -fPIC -shared -lpthread +PLAT_ARM64_LDFLAGS := -fPIC -shared PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) diff --git a/Platform/build/user.ulog.log_sched.Makefile b/Platform/build/user.ulog.log_sched.Makefile index 70fe33360..134a94902 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_tty.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 # MRS Board Source Files PLAT_LINUX_SRCS = $(COMMON_SRCS) @@ -38,12 +38,12 @@ PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/rpc -I../user/ulog PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) -PLAT_ARM64_LDFLAGS := -lpthread +PLAT_ARM64_LDFLAGS := PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) #gcc libs -ARM64_LIBS := ./libopenrpc-arm64.so ./libulogapi-arm64.so ../thirdparty/arm64/libev-arm64.so -LINUX_LIBS := ./libopenrpc-linux.so ./libulogapi-linux.so ../thirdparty/x86_64/libev-linux.so +ARM64_LIBS := ./libopenrpc-arm64.so ./libulogapi-arm64.so ../thirdparty/arm64/libev-arm64.so -lpthread +LINUX_LIBS := ./libopenrpc-linux.so ./libulogapi-linux.so ../thirdparty/x86_64/libev-linux.so -lpthread ifeq ($(PLAT_ARM64), TRUE) DEPEND_LIB += ./debug/libopenrpc-arm64.so ./debug/libulogapi-arm64.so diff --git a/Platform/common/rpc/rpc_module.h b/Platform/common/rpc/rpc_module.h index 529059531..db6ec93e1 100755 --- a/Platform/common/rpc/rpc_module.h +++ b/Platform/common/rpc/rpc_module.h @@ -21,7 +21,7 @@ typedef struct _rpc_module rpc_module; #define MODULE_REG_ARRAY \ { \ {"ConfigManger#0", "127.0.0.1", 10002, 1}, \ - {RPC_MODULE_SYSLOG_NAME, "127.0.0.1", 10003, 2} \ + {RPC_MODULE_SYSLOG_NAME, "127.0.0.1", 10003, 1} \ } #endif /* RPC_MODULE_H_ */ diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c index c0350fd48..00b4eaf9a 100755 --- a/Platform/user/ulog/log-sched/log_common.c +++ b/Platform/user/ulog/log-sched/log_common.c @@ -29,14 +29,17 @@ static int copy_file(const char *src, const char *dst) char buff[1024]; int len; int n; - while(feof(src_fp) > 0) + while((len = fread(buff, 1, sizeof(buff), src_fp)) > 0) { - len = fread(buff, 1, sizeof(buff), src_fp); n = fwrite(buff, 1, len ,dst_fp); if (n != len) { ULOG_ERR(g_log, "Configure file which is written[length:%d, actual len:%n] is failure:%s", n, len, strerror(errno)); goto END; } + + if (feof(src_fp) == 0) { + break; + } } ret = 0; @@ -93,14 +96,16 @@ END: fclose(fp); } - if ((ret == 1) && (exist_backup == 1)) { - // 恢复备份配置 - if (rename(bak_file, conf_path_file) < 0) { - ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); - } - } else { - if (unlink(bak_file) < 0) { - ULOG_WARNING(g_log, "Delete back file:%s is failure", bak_file); + if (exist_backup == 1) { + if (ret == 1) { + // 恢复备份配置 + if (rename(bak_file, conf_path_file) < 0) { + ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); + } + } else { + if (unlink(bak_file) < 0) { + ULOG_WARNING(g_log, "Delete back file:%s is failure", bak_file); + } } } @@ -124,7 +129,7 @@ int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *a int i; int ret = 1; char line[MAX_LINE_SZ + 100] = {0}; - ULOG_INFO(g_log, "filter module:%s\n", filter_mod); + ULOG_DEBUG(g_log, "filter module:%s\n", filter_mod); if ((filter_mod != NULL) && (strlen(filter_mod) > 0)) { snprintf(line, sizeof(line), FILTER_CONTENT, filter_mod); if (fputs(line, fp) == EOF) { diff --git a/Platform/user/ulog/log-sched/log_tty.c b/Platform/user/ulog/log-sched/log_pty.c similarity index 50% rename from Platform/user/ulog/log-sched/log_tty.c rename to Platform/user/ulog/log-sched/log_pty.c index 35e40c1d3..43f78ce6c 100755 --- a/Platform/user/ulog/log-sched/log_tty.c +++ b/Platform/user/ulog/log-sched/log_pty.c @@ -3,18 +3,19 @@ #include #include -#include "log_tty.h" +#include "log_pty.h" #include "log_common.h" -#define LOG_CONF_TTY_FILE_NAME "log-tty.conf" +#define LOG_DEV_PTY_DIR LOG_DEV_DIR"pts/" +#define LOG_CONF_PTY_FILE_NAME "log-pty.conf" -static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) { DIR *dir; - if ((dir = opendir(LOG_DEV_DIR)) == NULL) { - ULOG_ERR(g_log, "Open dir:[%s] is failure:%d\n", LOG_DEV_DIR, strerror(errno)); + if ((dir = opendir(LOG_DEV_PTY_DIR)) == NULL) { + ULOG_ERR(g_log, "Open dir:[%s] is failure:%d\n", LOG_DEV_PTY_DIR, strerror(errno)); return 1; } @@ -28,21 +29,18 @@ static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, v continue; } - if ((strstr(ptr->d_name, "tty") == NULL) - || (strcmp(ptr->d_name, "tty") == 0) - || (strcmp(ptr->d_name, "ttyprintk") == 0) - || (strstr(ptr->d_name, "ttyS") != NULL)) { + if (strstr(ptr->d_name, "ptmx") != NULL) { ULOG_DEBUG(g_log,"The file:[%s] isn't redirected\n", ptr->d_name); continue; } - ULOG_DEBUG(g_log, "tty name:%s", ptr->d_name); - if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name) < 0) { - ULOG_ERR(g_log, "Setting tty of log[%s] is failure", ptr->d_name); + ULOG_DEBUG(g_log, "pty name:%s", ptr->d_name); + if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_PTY_DIR, ptr->d_name) < 0) { + ULOG_ERR(g_log, "Setting pty of log[%s] is failure", ptr->d_name); return 1; } if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) { - ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod); + ULOG_ERR(g_log, "Writing pty[module:%s] of log is failure", filter_mod); return 1; } @@ -51,27 +49,27 @@ static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, v return 0; } -static int config_log_tty(const log_tty_t *conf) +static int config_log_pty(const log_pty_t *conf) { - if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_TTY_FILE_NAME, conf->module_name, - write_tty_content, NULL) != 0) { - ULOG_ERR(g_log, "Log's tty configure which is written is failure"); + if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, conf->module_name, + write_pty_content, NULL) != 0) { + ULOG_ERR(g_log, "Log's pty configure which is written is failure"); return 1; } return 0; } -void rpc_conf_log_tty(rpc_conn *conn, pointer input, int input_len, pointer data) +void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data) { - u32 need_len = sizeof(log_tty_t); + u32 need_len = sizeof(log_pty_t); if (input_len < need_len) { ULOG_WARNING(g_log, - "The input paramter of rpc log tty is needed length of %u, but the actual length is %u", + "The input paramter of rpc log pty is needed length of %u, but the actual length is %u", need_len, input_len); return; } - config_log_tty((const log_tty_t *)input); + config_log_pty((const log_pty_t *)input); } diff --git a/Platform/user/ulog/log-sched/log_pty.h b/Platform/user/ulog/log-sched/log_pty.h new file mode 100755 index 000000000..9a940bb25 --- /dev/null +++ b/Platform/user/ulog/log-sched/log_pty.h @@ -0,0 +1,10 @@ +#ifndef _LOG_PTY_H +#define _LOG_PTY_H + +#include "log_console.h" + +typedef log_console_t log_pty_t; + +void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data); + +#endif \ No newline at end of file diff --git a/Platform/user/ulog/log-sched/log_remote.c b/Platform/user/ulog/log-sched/log_remote.c index dda147bcc..06a43893a 100755 --- a/Platform/user/ulog/log-sched/log_remote.c +++ b/Platform/user/ulog/log-sched/log_remote.c @@ -20,44 +20,90 @@ static rfc_key_fmt rfc_tbl[] = { {LOG_RFC_5424, "RFC5424fmt"} }; -static int match_line(const log_remote_host_t *conf) +typedef int (*op_func)(const log_remote_host_t *conf); + +static int add_remote_host(const log_remote_host_t *conf); +static int del_remote_host(const log_remote_host_t *conf); + +static op_func remote_funcs[] = { + add_remote_host, + del_remote_host +}; + +static int match_remote_config(const char *line, const void *src) { + char text_level[MAX_LINE_SZ], old_redirect[MAX_LINE_SZ]; + int n; + + ULOG_DEBUG(g_log, "The line:%s will be matched", line); + n = sscanf(line, "%s"REDIRECT_SEPERATE"%s", text_level, old_redirect); + if (errno != 0) { + // 错误发生 + ULOG_ERR(g_log, "Parsing remote line is failure:%s", strerror(errno)); + return -1; + } + + if (n == 2) { + // 匹配到 + // 是否相同配置判读 + ULOG_DEBUG(g_log, "%s will compare with %s", old_redirect, (const char *)src); + if (strcmp(old_redirect, src) == 0) { + return 0; + } + } else { + // 未能识别行 + ULOG_DEBUG(g_log, "The line:%s can't be parsed", line); + } + return 1; } -static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, + int (*match_cb)(const char *line, const void *src), + int (*final_cb)(FILE *fp, const u8 level, const char *filter_mod, void *arg), + void *arg) { int ret = 1; FILE *bak_fp = NULL; char path[MAX_PATH_SZ]; snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME); - bak_fp = fopen(bak_fp, "r"); + bak_fp = fopen(path, "r"); if (bak_fp == NULL) { - ULOG_ERR(g_log, "Opening remote back file:%s is failure:%s", path, strerror(errno)); - return 1; + if (errno == ENOENT) { + goto FINAL_PHASE; + } else { + ULOG_ERR(g_log, "Opening remote backup file:%s is failure:%s", path, strerror(errno)); + return 1; + } } char *line = NULL; ssize_t n, n1; - char *pad, *host, *rfc_fmt; - u16 port; + char text_level[MAX_LINE_SZ], old_redirect[MAX_LINE_SZ]; while ((n = getline(&line, &n, bak_fp)) != -1) { - n1 = sscanf(line, "%s"REDIRECT_SEPERATE"%s:%u:%s", &pad, &host, &port, &rfc_fmt); - if (n1 == 4) { - // 匹配到 - } if (errno != 0) { - // 错误发生 - } else { - // 未知行,跳过 + int match = match_cb(line, arg); + if (match == -1) { + ULOG_ERR(g_log, "Configure which is matched is failure"); + } else if (match == 0) { + ULOG_DEBUG(g_log, "Be matched"); continue; } - - if (write_conf_content(fp, level, filter_mod, arg) != 0) { - ULOG_ERR(g_log, "Write remote configure is failure"); + + ULOG_DEBUG(g_log, "Recover old line:%s to file:%s", line, path); + n1 = fwrite(line, 1, n, fp); + if (n != n1) { + ULOG_ERR(g_log, "Recovering old line:%s to file is failure:%s", line, strerror(errno)); goto END; } } +FINAL_PHASE: + if (final_cb != NULL) { + if (final_cb(fp, level, filter_mod, arg) != 0) { + ULOG_ERR(g_log, "Executing final callback is failure"); + goto END; + } + } ret = 0; END: @@ -71,8 +117,19 @@ END: return ret; } +static int add_remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +{ + return remote_conf_content(fp, level, filter_mod, match_remote_config, write_conf_content, arg); +} + +static int del_remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +{ + return remote_conf_content(fp, level, filter_mod, match_remote_config, NULL, arg); +} + static int add_remote_host(const log_remote_host_t *conf) -{ +{ + ULOG_INFO(g_log, "Adding remote log server[%s:%u], rfc is %s", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); char prefix[1] = ""; if (conf->rfc == LOG_RFC_5424) { strcat(prefix, "@"); @@ -84,8 +141,8 @@ static int add_remote_host(const log_remote_host_t *conf) ULOG_ERR(g_log, "Setting remote redirect[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); return 1; } - if (log_conf_append(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, - write_conf_content, (void *)redirect) != 0) { + if (log_conf(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, + add_remote_conf_content, (void *)redirect) != 0) { ULOG_ERR(g_log, "Adding remote server[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); return 1; } @@ -95,6 +152,25 @@ static int add_remote_host(const log_remote_host_t *conf) static int del_remote_host(const log_remote_host_t *conf) { + int ret; + char prefix[1] = ""; + if (conf->rfc == LOG_RFC_5424) { + strcat(prefix, "@"); + }; + char redirect[MAX_LINE_SZ]; + if (snprintf(redirect, sizeof(redirect), "%s@%s:%u:%s", + prefix, conf->host, conf->port, rfc_tbl[conf->rfc].fmt) < 0) { + ULOG_ERR(g_log, "Setting remote redirect[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); + return 1; + } + + if (log_conf(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, + del_remote_conf_content, (void *)redirect) != 0) { + ULOG_ERR(g_log, "Adding remote server[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); + return 1; + } + + return ret; } static int config_log_remote_host(const log_remote_host_t *conf) @@ -103,9 +179,12 @@ static int config_log_remote_host(const log_remote_host_t *conf) ULOG_WARNING(g_log, "Unknown rfc format key:%u", conf->rfc); return 1; } + if (conf->op >= LOG_REMOTE_MAX) { + ULOG_WARNING(g_log, "Unknown operation type:%u", conf->op); + return 1; + } - - return 0; + return remote_funcs[conf->op](conf); } void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data) diff --git a/Platform/user/ulog/log-sched/log_remote.h b/Platform/user/ulog/log-sched/log_remote.h index c813fce19..7d6b43f01 100755 --- a/Platform/user/ulog/log-sched/log_remote.h +++ b/Platform/user/ulog/log-sched/log_remote.h @@ -7,7 +7,8 @@ typedef enum { LOG_REMOTE_OP_ADD = 0, - LOG_REMOTE_OP_DEL + LOG_REMOTE_OP_DEL, + LOG_REMOTE_MAX } log_op_t; typedef enum { diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c index 9057d1449..bb6ee93cf 100755 --- a/Platform/user/ulog/log-sched/log_sched.c +++ b/Platform/user/ulog/log-sched/log_sched.c @@ -2,12 +2,14 @@ #include #include #include +#include +#include #include "rpc_server.h" #include "ulog_api.h" #include "log_file.h" #include "log_console.h" -#include "log_tty.h" +#include "log_pty.h" #include "log_remote.h" #include "rpc_module.h" @@ -16,9 +18,21 @@ #define SERVICE_LOG_FILE_NAME "log-file" #define SERIVCE_LOG_CONSOLE_NAME "log-console" -#define SERVICE_LOG_TTY_NAME "log-tty" +#define SERVICE_LOG_PTY_NAME "log-pty" +#define SERVICE_LOG_REMOTE_NAME "log-remote" ulog_t *g_log = NULL; +static sem_t g_sem; + + +void signal_exit() +{ + if (sem_post(&g_sem) == -1) { + ULOG_ERR(g_log, "Posting semaphore is failure:%s", strerror(errno)); + } + + ULOG_DEBUG(g_log, "Receive shutdown signal"); +} int main(int argc, char **argv) @@ -52,15 +66,23 @@ int main(int argc, char **argv) if (server == NULL) { ULOG_ERR(g_log, "start server error"); + return 1; + } + + if (sem_init(&g_sem, 0, 0) != 0) { + ULOG_ERR(g_log, "Init sem is failure:%s", strerror(errno)); return 1; - - } + } + 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", rpc_conf_log_file); rpc_server_regservice(server, SERIVCE_LOG_CONSOLE_NAME, "conf_log_console", rpc_conf_log_console); - rpc_server_regservice(server, SERVICE_LOG_TTY_NAME, "conf_log_tty", rpc_conf_log_tty); + rpc_server_regservice(server, SERVICE_LOG_PTY_NAME, "conf_log_pty", rpc_conf_log_pty); + rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_remote", rpc_conf_log_remote); + +/* log_file_t log = {0}; log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS; @@ -75,17 +97,25 @@ int main(int argc, char **argv) rpc_conf_log_console(NULL, &console, sizeof(console), NULL); - log_tty_t tty = {0}; + log_pty_t tty = {0}; //strcpy(tty.module_name, "111"); tty.level = LOG_DEBUG; tty.on = 1; - rpc_conf_log_tty(NULL, &tty, sizeof(tty), NULL); + rpc_conf_log_pty(NULL, &tty, sizeof(tty), NULL); log_remote_host_t remote = {0}; - strcpy(remote.host, "192.168.3.1"); + remote.op = LOG_REMOTE_OP_DEL; + strcpy(remote.host, "192.168.3.11"); remote.rfc = LOG_RFC_5424; remote.port = 514; rpc_conf_log_remote(NULL, &remote, sizeof(remote), NULL); +*/ + if (sem_wait(&g_sem) == -1) { + ULOG_ERR(g_log, "Waitting semaphore is failure:%s", strerror(errno)); + } + + + ULOG_INFO(g_log, "%s is shutdown", LOG_SCHED_MODULE_NAME); END: ulog_close(g_log); return 0; diff --git a/Platform/user/ulog/log-sched/log_tty.h b/Platform/user/ulog/log-sched/log_tty.h deleted file mode 100755 index aab60c8fd..000000000 --- a/Platform/user/ulog/log-sched/log_tty.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef _LOG_TTY_H -#define _LOG_TTY_H - -#include "log_console.h" - -typedef log_console_t log_tty_t; - -void rpc_conf_log_tty(rpc_conn *conn, pointer input, int input_len, pointer data); - -#endif \ No newline at end of file