2019-07-26 10:22:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
#include "log_pty.h"
|
2019-08-02 09:58:02 +00:00
|
|
|
#include "log_types.h"
|
2019-07-26 10:22:43 +00:00
|
|
|
#include "log_common.h"
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
#define LOG_DEV_PTY_DIR LOG_DEV_DIR"pts/"
|
|
|
|
#define LOG_CONF_PTY_FILE_NAME "log-pty.conf"
|
2019-07-29 10:20:17 +00:00
|
|
|
|
2019-07-26 10:22:43 +00:00
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
|
2019-07-26 10:22:43 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2019-07-29 03:50:57 +00:00
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
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));
|
2019-08-01 08:44:18 +00:00
|
|
|
return -1;
|
2019-07-26 10:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *ptr;
|
2019-07-29 03:50:57 +00:00
|
|
|
char path[MAX_PATH_SZ];
|
2019-07-26 10:22:43 +00:00
|
|
|
while ((ptr = readdir(dir)) != NULL) {
|
|
|
|
if ((strcmp(ptr->d_name, ".") == 0)
|
|
|
|
|| (strcmp(ptr->d_name, "..") == 0)
|
|
|
|
|| (ptr->d_type == DT_DIR)) { ///current dir OR parrent dir
|
|
|
|
ULOG_DEBUG(g_log,"The file:[%s] or directory jump over\n", ptr->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
if (strstr(ptr->d_name, "ptmx") != NULL) {
|
2019-07-26 10:22:43 +00:00
|
|
|
ULOG_DEBUG(g_log,"The file:[%s] isn't redirected\n", ptr->d_name);
|
2019-07-29 03:50:57 +00:00
|
|
|
continue;
|
2019-07-26 10:22:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
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);
|
2019-08-01 08:44:18 +00:00
|
|
|
return -1;
|
2019-07-29 10:20:17 +00:00
|
|
|
}
|
2019-07-29 03:50:57 +00:00
|
|
|
if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) {
|
2019-07-31 09:32:43 +00:00
|
|
|
ULOG_ERR(g_log, "Writing pty[module:%s] of log is failure", filter_mod);
|
2019-08-01 08:44:18 +00:00
|
|
|
return -1;
|
2019-07-26 10:22:43 +00:00
|
|
|
}
|
2019-07-29 03:50:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
static int config_log_pty(const log_pty_t *conf)
|
2019-07-29 03:50:57 +00:00
|
|
|
{
|
2019-08-13 08:25:43 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
switch (conf->on)
|
|
|
|
{
|
|
|
|
case LOG_OFF:
|
|
|
|
ret = delete_conf_file(LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME);
|
|
|
|
break;
|
|
|
|
case LOG_ON:
|
|
|
|
if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, conf->module,
|
|
|
|
write_pty_content, NULL) != 0) {
|
|
|
|
ULOG_ERR(g_log, "Log's pty 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;
|
2019-07-26 10:22:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data)
|
2019-07-26 10:22:43 +00:00
|
|
|
{
|
2019-07-31 09:32:43 +00:00
|
|
|
u32 need_len = sizeof(log_pty_t);
|
2019-07-26 10:22:43 +00:00
|
|
|
if (input_len < need_len) {
|
|
|
|
ULOG_WARNING(g_log,
|
2019-07-31 09:32:43 +00:00
|
|
|
"The input paramter of rpc log pty is needed length of %u, but the actual length is %u",
|
2019-07-26 10:22:43 +00:00
|
|
|
need_len, input_len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-13 08:25:43 +00:00
|
|
|
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");
|
2019-08-13 10:22:20 +00:00
|
|
|
return;
|
2019-08-13 08:25:43 +00:00
|
|
|
}
|
|
|
|
rpc_return_null(conn);
|
2019-07-26 10:22:43 +00:00
|
|
|
}
|
|
|
|
|