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

101 lines
3.0 KiB
C
Executable File

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include "ulog.h"
#include "log_common.h"
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"
#define BAK_FILE "/tmp/%s"
int write_log_conf(const u8 level, const char *conf_path, const char *conf_file,
const char *rediect_path, const char *filter_mod)
{
FILE *fp = NULL;
int ret = 1;
u8 exist_backup = 1;
/********** rsyslog configure **********/
char conf_path_file[MAX_LINE_SIZE];
snprintf(conf_path_file, sizeof(conf_path_file), "%s%s", conf_path, conf_file);
char bak_file[MAX_LINE_SIZE];
snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file);
if (rename(conf_path_file, bak_file) < 0) {
if (errno == ENOENT) {
exist_backup = 0;
ULOG_INFO(g_log, "Been not exist, Configure file:%s is need to backup");
} else {
ULOG_ERR(g_log, "Baking configure file:%s is failure:%s %d", conf_path_file, strerror(errno), errno);
return ret;
}
}
fp = fopen(conf_path_file, "w");
if (fp == NULL) {
ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno));
goto END;
}
int i;
char line[MAX_LINE_SIZE + 100] = {0};
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;
}
}
char tmp[20];
line[0] = '\0'; // 清零
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");
goto END;
}
strcat(line, tmp);
i++;
if (level >= i) {
strcat(line, ";");
}
}
strcat(line, " ");
strcat(line, rediect_path);
if (fputs(line, fp) == EOF) {
ULOG_ERR(g_log, "Configure file which is written is failure:%s", strerror(errno));
goto END;
}
ret = 0;
END:
if (fp != NULL) {
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));
}
}
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", strerror(errno));
return 1;
}
return 0;
}