secgateway/Platform/user/ulog/log-sched/log_common.c

209 lines
6.1 KiB
C
Executable File

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "ulog.h"
#include "log_common.h"
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"
static int copy_file(const char *src, const char *dst)
{
int ret = -1;
FILE *src_fp = NULL,*dst_fp = NULL;
src_fp = fopen(src, "r");
if (src_fp == NULL) {
ULOG_ERR(g_log, "Opening src file:%s is failure:%s", src, strerror(errno));
goto END;
}
dst_fp = fopen(dst, "w");
if (dst_fp == NULL) {
ULOG_ERR(g_log, "Opening dst file:%s is failure:%s", src, strerror(errno));
goto END;
}
char buff[1024];
int len;
int n;
while((len = fread(buff, 1, sizeof(buff), src_fp)) > 0)
{
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;
END:
if (src_fp != NULL) {
fclose(src_fp);
}
if (dst_fp != NULL) {
fclose(dst_fp);
}
return ret;
}
static int __log_conf(const char *mode, 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)
{
FILE *fp = NULL;
int ret = -1;
u8 exist_backup = 1;
/********** rsyslog configure **********/
char conf_path_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 (copy_file(conf_path_file, bak_file) != 0) {
if (errno == ENOENT) {
exist_backup = 0;
ULOG_INFO(g_log, "Been not exist, Configure file:%s don't need to backup", conf_path_file);
} else {
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) {
ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno));
goto END;
}
ULOG_DEBUG(g_log, "cb_content:%x", cb_content);
if (cb_content(fp, level, filter_mod, arg) != 0) {
ULOG_ERR(g_log, "Callback log content is failure");
goto END;
}
ret = 0;
END:
if (fp != NULL) {
fclose(fp);
}
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);
}
}
}
return ret;
}
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)
{
return __log_conf("w", level, conf_path, conf_file, filter_mod, cb_content, arg);
}
int log_conf_append(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)
{
return __log_conf("a", level, conf_path, conf_file, filter_mod, cb_content, arg);
}
int log_level_to_str(const u8 level, char *str, u32 len)
{
u8 i;
char tmp[20];
for (i = 0; i <= level;) {
if (snprintf(tmp, sizeof(tmp), "*.=%s", g_level_array[i].str) < 0) {
ULOG_ERR(g_log, "Setting content of log file configure is failure");
return -1;
}
strcat(str, tmp);
i++;
if (level >= i) {
strcat(str, ";");
}
}
return 0;
}
int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{
int i;
int ret = -1;
char line[MAX_LINE_SZ + 100] = {0};
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) {
ULOG_ERR(g_log, "Message filter:%s of configure file which is written is failure:%s",
filter_mod, strerror(errno));
goto END;
}
}
line[0] = '\0'; // 清零
if (log_level_to_str(level, line, sizeof(line)) != 0) {
ULOG_ERR(g_log, "Setting content of log file configure is failure");
goto END;
}
strcat(line, REDIRECT_SEPERATE);
strcat(line, (const char *)arg);
strcat(line, "\n");
//if (fputs(line, fp) == EOF) {
int n = fwrite(line, 1, strlen(line), fp);
if (n != strlen(line)) {
ULOG_ERR(g_log, "Configure file which is written[length:%d] is failure:%s", n, strerror(errno));
goto END;
}
ret = 0;
END:
return ret;
}
int modify_authorizing(const char *redirect_path)
{
if (chmod(redirect_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) {
ULOG_ERR(g_log, "Authorizing of %s which is modified is failure:%s", redirect_path, strerror(errno));
return -1;
}
return 0;
}
int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{
if (write_conf_content(fp, level, filter_mod, arg) != 0) {
ULOG_ERR(g_log, "configure of log conosle which is written is failure");
return -1;
}
if (modify_authorizing((const char *)arg) != 0) {
ULOG_ERR(g_log, "Modifying authorizing of %s is failure", (const char *)arg);
return -1;
}
return 0;
}