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

206 lines
5.3 KiB
C
Executable File

#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include "log_console.h"
#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"
#define LOG_REDIRECT_CONSOLE "/dev/console"
#define PROC_SERIAL_INFO_PATH "/proc/tty/driver/serial"
#define SERIAL_DRIVER_PREFIX "uart"
#define SERIAL_NO_DRIVER_KEY SERIAL_DRIVER_PREFIX":unknown"
#define SERIAL_PREFIX "ttyS"
#ifndef CM_LOG_CONF_CONSOLE_FILE
#define CM_LOG_CONF_CONSOLE_FILE "/etc/rsyslog.d/log-console.conf"
#endif
static int write_console_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{
int ret = -1;
FILE *driver_fp = NULL;
driver_fp = fopen(PROC_SERIAL_INFO_PATH, "r");
if (driver_fp == NULL) {
ULOG_ERR(g_log, "Opening file:%s is failure:%s", PROC_SERIAL_INFO_PATH, strerror(errno));
return ret;
}
char *line = NULL;
size_t n;
int num;
char path[MAX_PATH_SZ];
while ((n = getline(&line, &n, driver_fp)) != -1) {
ULOG_DEBUG(g_log, "Serial line:%s", line);
if (strstr(line, SERIAL_DRIVER_PREFIX) == NULL) {
ULOG_DEBUG(g_log, "%s isn't driver line", line);
continue;
}
num = atoi(line);
if (strstr(line, SERIAL_NO_DRIVER_KEY) != NULL) {
ULOG_DEBUG(g_log, "%s%d don't have serial", SERIAL_PREFIX, num);
continue;
}
if (snprintf(path, sizeof(path), "%s%s%d", LOG_DEV_DIR, SERIAL_PREFIX, num) < 0) {
ULOG_ERR(g_log, "Setting %s%d of log console is failure", SERIAL_PREFIX, num);
goto END;
}
ULOG_DEBUG(g_log, "ttyS name:%s", path);
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);
goto END;
}
}
ret = 0;
END:
if (line != NULL) {
free(line);
}
fclose(driver_fp);
return ret;
}
static int config_log_console(const log_console_t *conf)
{
int ret = -1;
switch (conf->on)
{
case LOG_OFF:
ret = log_off_with_file(LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME);
break;
case LOG_ON:
if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module,
write_console_content, NULL) != 0) {
ULOG_ERR(g_log, "Log's console configure which is written is failure");
ret = -1;
} else {
ret = 0;
}
break;
default:
ULOG_WARNING(g_log, "Unknown on value:%u", conf->on);
break;
}
return ret;
}
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 *)input) != 0) {
strncpy(str_err, "Configuring console of log is faiure", str_len);
return -1;
}
return 0;
}
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, NULL);
/*
u32 need_len = sizeof(log_console_t);
if (input_len < need_len) {
ULOG_WARNING(g_log,
"The input paramter of rpc log console is needed length of %u, but the actual length is %u",
need_len, input_len);
return;
}
if (config_log_console((const log_console_t *)input) != 0) {
ULOG_ERR(g_log, "Configuring console of log is faiure");
rpc_return_error(conn, RET_ERR, "Configuring console of log is faiure");
return;
}
log_sev_restart();
rpc_return_null(conn);
*/
}
ret_code console_initial()
{
ret_code ret = RET_ERR;
FILE *fp = NULL;
char *pos = NULL;
char *pos2 = NULL;
char ttyfile_str[128] = "";
fp = fopen(CM_LOG_CONF_CONSOLE_FILE, "r");
if (NULL == fp) {
return ret;
}
if (fseek(fp, 0, SEEK_SET) == -1)
{
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
fclose(fp);
return ret;
}
ssize_t n;
char *line = NULL;
int str_len;
while ((getline(&line, &n, fp)) != -1)
{
pos = strstr(line, "/");
if (pos != NULL)
{
if ((pos2 = strstr(pos, " ")) != NULL)
{
str_len = strlen(pos) - strlen(pos2);
}
else
{
str_len = strlen(pos);
}
memset(ttyfile_str, 0, sizeof(ttyfile_str));
strncpy(ttyfile_str, pos, str_len);
/* 滤除掉段尾的回车换行 */
if ('\n' == ttyfile_str[strlen(ttyfile_str)-1])
{
ttyfile_str[strlen(ttyfile_str)-1] = '\0';
}
if(modify_authorizing(ttyfile_str) == RET_OK)
{
ULOG_DEBUG(g_log, "Modify authorizing %s successed.", ttyfile_str);
}
else
{
ULOG_DEBUG(g_log, "Modify authorizing %s failed.", ttyfile_str);
}
}
}
if (NULL != line)
{
free(line);
}
ret = RET_OK;
if (NULL != fp)
{
fclose(fp);
}
return ret;
}