diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c index d35461863..3a110f826 100755 --- a/Platform/user/ulog/log-sched/log_common.c +++ b/Platform/user/ulog/log-sched/log_common.c @@ -10,10 +10,9 @@ #define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n" - - static int copy_file(const char *src, const char *dst) { + //return -1; int ret = -1; FILE *src_fp = NULL,*dst_fp = NULL; src_fp = fopen(src, "r"); @@ -63,12 +62,12 @@ static int __log_conf(const char *mode, const u8 level, const char *conf_path, c u8 exist_backup = 1; /********** rsyslog configure **********/ - char conf_path_file[MAX_PATH_SZ]; + char conf_path_file[MAX_PATH_SZ], bak_file[MAX_PATH_SZ]; + snprintf(conf_path_file, sizeof(conf_path_file), "%s%s", conf_path, conf_file); - - char bak_file[MAX_PATH_SZ]; snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file); - //if (rename(conf_path_file, bak_file) < 0) + + //if (rename(conf_path_file, bak_file) < 0) { if (copy_file(conf_path_file, bak_file) != 0) { if (errno == ENOENT) { exist_backup = 0; @@ -77,7 +76,7 @@ static int __log_conf(const char *mode, const u8 level, const char *conf_path, c ULOG_ERR(g_log, "Baking configure file:%s is failure:%s", conf_path_file, strerror(errno)); return ret; } - } + } fp = fopen(conf_path_file, mode); if (fp == NULL) { @@ -229,30 +228,40 @@ int log_off_with_file(const char *path, const char *file_name) return ret; } -void rpc_conf_proc(rpc_conn *conn, pointer input, int input_len, int need_len, rpc_cb cb) +void rpc_conf_proc(rpc_conn *conn, pointer input, int input_len, int need_len, rpc_cb cb, void *arg) { char str_err[1024]; + if (input == NULL) { + strncpy(str_err, "Input is not null", sizeof(str_err)); + ULOG_WARNING(g_log, str_err); + goto FAIL; + } + if (input_len < need_len) { if (snprintf(str_err, sizeof(str_err), "The input paramter of rpc log is needed length of %u, but the actual length is %u", need_len, input_len) < 0) { - ULOG_ERR(g_log, "Setting error to string is failure"); - return; + strncpy(str_err, "Setting error message is failure", sizeof(str_err)); + ULOG_ERR(g_log, str_err); + } else { + ULOG_WARNING(g_log, str_err); } - ULOG_WARNING(g_log, str_err); goto FAIL; } - if (cb(input, str_err, sizeof(str_err)) != 0) { + // ret == 1,不需要重启服务;ret == 0,需要重启服务 + int ret = cb(input, arg, str_err, sizeof(str_err)); + if (ret < 0) { ULOG_ERR(g_log, str_err); goto FAIL; } - if (log_sev_restart() != 0) { + + if ((ret == 0) && (log_sev_restart() != 0)) { if (snprintf(str_err, sizeof(str_err), "Restarting log service is failure") < 0) { - ULOG_ERR(g_log, "Setting error to string is failure"); - return; + strncpy(str_err, "Setting error message is failure", sizeof(str_err)); + ULOG_ERR(g_log, str_err); } goto FAIL; } diff --git a/Platform/user/ulog/log-sched/log_common.h b/Platform/user/ulog/log-sched/log_common.h index e8c517021..371eec309 100755 --- a/Platform/user/ulog/log-sched/log_common.h +++ b/Platform/user/ulog/log-sched/log_common.h @@ -15,13 +15,12 @@ #define REDIRECT_SEPERATE " " -typedef int (*rpc_cb)(const void *arg, char *str_err, int str_len); +typedef int (*rpc_cb)(pointer *input, const void *arg, char *str_err, int str_len); extern ulog_t *g_log; extern FILE *g_conf_fp; extern char g_conf_file[MAX_PATH_SZ]; - int log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg); int log_conf_append(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, @@ -30,6 +29,6 @@ int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *a int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg); int log_level_to_str(const u8 level, char *str, u32 len); int log_off_with_file(const char *path, const char *file_name); -void rpc_conf_proc(rpc_conn *conn, pointer input, int input_len, int need_len, rpc_cb cb); +void rpc_conf_proc(rpc_conn *conn, pointer input, int input_len, int need_len, rpc_cb cb, void *arg); #endif diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c index 04aeba645..7ef36bfdd 100755 --- a/Platform/user/ulog/log-sched/log_console.c +++ b/Platform/user/ulog/log-sched/log_console.c @@ -78,9 +78,9 @@ static int config_log_console(const log_console_t *conf) return ret; } -static int __rpc_conf_log_console(const void *arg, char *str_err, int str_len) +static int __rpc_conf_log_console(pointer input, const void *arg, char *str_err, int str_len) { - if (config_log_console((const log_console_t *)arg) != 0) { + if (config_log_console((const log_console_t *)input) != 0) { strncpy(str_err, "Configuring console of log is faiure", str_len); return -1; } @@ -90,7 +90,7 @@ static int __rpc_conf_log_console(const void *arg, char *str_err, int str_len) void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer data) { - rpc_conf_proc(conn, input, input_len, sizeof(log_console_t), __rpc_conf_log_console); + rpc_conf_proc(conn, input, input_len, sizeof(log_console_t), __rpc_conf_log_console, NULL); /* u32 need_len = sizeof(log_console_t); diff --git a/Platform/user/ulog/log-sched/log_pty.c b/Platform/user/ulog/log-sched/log_pty.c index 2b03f1dfc..419d0681c 100755 --- a/Platform/user/ulog/log-sched/log_pty.c +++ b/Platform/user/ulog/log-sched/log_pty.c @@ -76,21 +76,17 @@ static int config_log_pty(const log_pty_t *conf) return ret; } -void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data) +static int __rpc_conf_log_pty(pointer input, const void *arg, char *str_err, int str_len) { - u32 need_len = sizeof(log_pty_t); - if (input_len < need_len) { - ULOG_WARNING(g_log, - "The input paramter of rpc log pty is needed length of %u, but the actual length is %u", - need_len, input_len); - return; + if (config_log_pty((const log_pty_t *)input) != 0) { + strncpy(str_err, "Configuring pty of log is faiure", str_len); + return -1; } - - if (config_log_pty((const log_pty_t *)input) != 0) { - ULOG_ERR(g_log, "Configuring pty of log is faiure"); - rpc_return_error(conn, RET_ERR, "Configuring pty of log is faiure"); - return; - } - rpc_return_null(conn); + return 0; +} + +void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data) +{ + rpc_conf_proc(conn, input, input_len, sizeof(log_pty_t), __rpc_conf_log_pty, NULL); } diff --git a/Platform/user/ulog/log-sched/log_remote.c b/Platform/user/ulog/log-sched/log_remote.c index 8a9e2010d..c3157d5b8 100755 --- a/Platform/user/ulog/log-sched/log_remote.c +++ b/Platform/user/ulog/log-sched/log_remote.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -39,12 +40,6 @@ static int match_remote_config(const char *line, const void *src) 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) { // 匹配到 // 是否相同配置判读 @@ -52,6 +47,10 @@ static int match_remote_config(const char *line, const void *src) if (strcmp(old_redirect, src) == 0) { return 0; } + } else if (n == EOF) { + // 错误发生 + ULOG_ERR(g_log, "Parsing remote line is failure:%s", strerror(errno)); + return -1; } else { // 未能识别行 ULOG_DEBUG(g_log, "The line:%s can't be parsed", line); @@ -68,6 +67,7 @@ static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, int ret = -1; FILE *bak_fp = NULL; char path[MAX_PATH_SZ]; + char *line = NULL; snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME); bak_fp = fopen(path, "r"); if (bak_fp == NULL) { @@ -79,7 +79,7 @@ static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, } } - char *line = NULL; + ssize_t n, n1; char text_level[MAX_LINE_SZ], old_redirect[MAX_LINE_SZ]; while ((n = getline(&line, &n, bak_fp)) != -1) { @@ -98,7 +98,6 @@ static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, goto END; } } - FINAL_PHASE: if (final_cb != NULL) { if (final_cb(fp, level, filter_mod, arg) != 0) { @@ -124,7 +123,10 @@ static int remote_conf_level_content(FILE *fp, const u8 level) int ret = -1; FILE *bak_fp = NULL; char path[MAX_PATH_SZ]; - snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME); + if (snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME) < 0) { + ULOG_DEBUG(g_log, "[remote-level]:Buildbackup path(%s) is failure", BAK_FILE, LOG_CONF_REMOTE_FILE_NAME); + return -1; + } bak_fp = fopen(path, "r"); if (bak_fp == NULL) { ULOG_ERR(g_log, "Opening remote backup file:%s is failure:%s", path, strerror(errno)); @@ -137,12 +139,7 @@ static int remote_conf_level_content(FILE *fp, const u8 level) char text_level[MAX_LINE_SZ], old_redirect[MAX_LINE_SZ]; char rewrite_line[MAX_LINE_SZ]; while ((n1 = getline(&line, &n, bak_fp)) != -1) { - n3 = 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)); - goto END; - } + n3 = sscanf(line, "%s"REDIRECT_SEPERATE"%s", text_level, old_redirect); if (n3 == 2) { // 匹配到 // 改变该行日志级别 @@ -155,6 +152,10 @@ static int remote_conf_level_content(FILE *fp, const u8 level) strcat(rewrite_line, old_redirect); strcat(rewrite_line, "\n"); n3 = strlen(rewrite_line); + } else if (n == EOF) { + // 错误发生 + ULOG_ERR(g_log, "Parsing remote line is failure:%s", strerror(errno)); + goto END; } else { // 未能识别行 ULOG_DEBUG(g_log, "The line:%s can't be parsed", line); @@ -176,7 +177,6 @@ END: if (bak_fp != NULL) { fclose(bak_fp); } - return ret; } @@ -206,14 +206,13 @@ static int get_log_level() char *line = NULL; u8 value; while ((n1 = getline(&line, &n, g_conf_fp)) != -1) { - n2 = sscanf(line, LOG_CONF_KEY_REMOTE_LEVEL"%u", &value); - if (errno != 0) { - // 错误发生 - ULOG_ERR(g_log, "Parsing level from configure is failure:%s", strerror(errno)); - return -1; - } + ULOG_DEBUG(g_log, "config file line:%s", line); + n2 = sscanf(line, LOG_CONF_KEY_REMOTE_LEVEL"%u", &value); if (n2 == 1) { return value; + } else if (n2 == EOF) { + ULOG_ERR(g_log, "Parsing level from configure is failure:%s", strerror(errno)); + return -1; } else { ULOG_DEBUG(g_log, "Unknown level row:%s", line); } @@ -225,7 +224,7 @@ static int get_log_level() 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] = ""; + char prefix[2] = {0}; if (conf->rfc == LOG_RFC_5424) { strcat(prefix, "@"); }; @@ -242,6 +241,7 @@ static int add_remote_host(const log_remote_host_t *conf) ULOG_ERR(g_log, "Getting log level is failure"); return -1; } + if (log_conf(level, 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); @@ -316,21 +316,35 @@ static int config_log_remote_level(const log_remote_level_t *level) return -1; } fflush(g_conf_fp); - - if (log_conf(level->level, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, - modify_remote_conf_log_level, NULL) != 0) { - ULOG_ERR(g_log, "Modifing log level:%u of remote server [%s:%u:%s] is faulure", level->level); + + //判断是否有rsyslog的remote配置,如果有,才写入 + char path[MAX_PATH_SZ]; + if (snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME) < 0) { + ULOG_DEBUG(g_log, "Build backup path(%s) is failure", BAK_FILE, LOG_CONF_REMOTE_FILE_NAME); return -1; } + if(access(path, F_OK) != 0) { + ULOG_DEBUG(g_log, "Backup file:%s is not exist:%s", path, strerror(errno)); + return 1; + } else { + if (log_conf(level->level, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, + modify_remote_conf_log_level, NULL) != 0) { + ULOG_ERR(g_log, "Modifing log level:%u of remote server is faulure", level->level); + return -1; + } + } return 0; } static void rpc_conf_log_remote(const log_op_t op, rpc_conn *conn, pointer input, int input_len, pointer data) { + char str_err[1024]; + if (input == NULL) { - ULOG_WARNING(g_log, "Remote log configure can't null"); - return; + strncpy(str_err, "Remote log configure can't be null", sizeof(str_err)); + ULOG_WARNING(g_log, str_err); + goto FAIL; } u32 need_len = sizeof(log_remote_host_t); @@ -346,40 +360,51 @@ static void rpc_conf_log_remote(const log_op_t op, rpc_conn *conn, point rpc_return_error(conn, RET_ERR, "Configuring remote of log is faiure"); return; } + rpc_return_null(conn); +FAIL: + rpc_return_error(conn, RET_ERR, str_err); +} + +static int __rpc_conf_log_remote(pointer input, const void *arg, char *str_err, int str_len) +{ + log_op_t op; + memcpy(&op, arg, sizeof(op)); + + int ret = config_log_remote_host(op, (const log_remote_level_t *)input); + if (ret < 0) { + strncpy(str_err, "Configuring remote of log is faiure", str_len); + } + return ret; } void rpc_conf_log_add_remote(rpc_conn *conn, pointer input, int input_len, pointer data) { - rpc_conf_log_remote(LOG_REMOTE_OP_ADD, conn, input, input_len, data); + //rpc_conf_log_remote(LOG_REMOTE_OP_ADD, conn, input, input_len, data); + + log_op_t op = LOG_REMOTE_OP_ADD; + rpc_conf_proc(conn, input, input_len, sizeof(log_remote_host_t), __rpc_conf_log_remote, (void *)&op); } void rpc_conf_log_del_remote(rpc_conn *conn, pointer input, int input_len, pointer data) { - rpc_conf_log_remote(LOG_REMOTE_OP_DEL, conn, input, input_len, data); + //rpc_conf_log_remote(LOG_REMOTE_OP_DEL, conn, input, input_len, data); + + log_op_t op = LOG_REMOTE_OP_DEL; + rpc_conf_proc(conn, input, input_len, sizeof(log_remote_host_t), __rpc_conf_log_remote, (void *)&op); } +static int __rpc_conf_log_remote_level(pointer input, const void *arg, char *str_err, int str_len) +{ + int ret = config_log_remote_level((const log_remote_level_t *)input); + if (ret < 0) { + strncpy(str_err, "Configuring remote level of log is faiure", str_len); + } + return ret; +} void rpc_conf_log_remote_level(rpc_conn *conn, pointer input, int input_len, pointer data) { - if (input == NULL) { - ULOG_WARNING(g_log, "Remote log level can't null"); - return; - } - - u32 need_len = sizeof(log_remote_level_t); - if (input_len < need_len) { - ULOG_WARNING(g_log, - "The input paramter of rpc log remote level is needed length of %u, but the actual length is %u", - need_len, input_len); - return; - } - - if (config_log_remote_level((const log_remote_level_t *)input) != 0) { - ULOG_ERR(g_log, "Configuring remote level of log is faiure"); - rpc_return_error(conn, RET_ERR, "Configuring remote level of log is faiure"); - return; - } - rpc_return_null(conn); + rpc_conf_proc(conn, input, input_len, sizeof(log_remote_level_t), __rpc_conf_log_remote_level, NULL); } diff --git a/Platform/user/ulog/log-sched/sev_sched.c b/Platform/user/ulog/log-sched/sev_sched.c index f95ff5add..6847553c6 100755 --- a/Platform/user/ulog/log-sched/sev_sched.c +++ b/Platform/user/ulog/log-sched/sev_sched.c @@ -8,8 +8,14 @@ #define SEV_TIMEOUT 1 #define SEV_CMD "systemctl restart rsyslog" +typedef enum { + SEV_STATE_RESTARTING, + SEV_STATE_WAIT +} sev_state_t; + static sem_t g_sem; -static int is_exit = 0; +static volatile int g_is_exit = 0; +static volatile sev_state_t g_sev_state = SEV_STATE_WAIT; static void sev_restart() { @@ -43,12 +49,15 @@ void sev_loop() ULOG_ERR(g_log, "Waitting semaphore is failure:%s", strerror(errno)); break; } - if (is_exit == 1) { + g_sev_state = SEV_STATE_RESTARTING; + if (g_is_exit == 1) { ULOG_DEBUG(g_log, "Service loop is exit"); break; } - sleep(SEV_TIMEOUT); + // 减少在短时间内频繁更改配置使重启变多,这里进行限制 + sleep(SEV_TIMEOUT); + g_sev_state = SEV_STATE_WAIT; sev_restart(); } } @@ -75,7 +84,7 @@ static int set_sem() int log_sev_exit() { - is_exit = 1; + g_is_exit = 1; set_sem(); sem_destroy(&g_sem); @@ -84,6 +93,7 @@ int log_sev_exit() int log_sev_restart() { + /* int val; if (sem_getvalue(&g_sem, &val) != 0) { ULOG_ERR(g_log, "Get semaphore value is failure:%s", strerror(errno)); @@ -93,7 +103,12 @@ int log_sev_restart() if (val > 0) { ULOG_ERR(g_log, "Current semaphore value is %d, not need to set semaphore"); return 0; - } + } + */ + if (g_sev_state == SEV_STATE_RESTARTING) { + ULOG_DEBUG(g_log, "Current service state:%u, not need to set semaphore", g_sev_state); + return 0; + } if (set_sem() != 0) { return -1;