From 6efd824b675901b0b0d7e79d63fd8219aa55a3cc Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Thu, 29 Aug 2019 15:41:08 +0800 Subject: [PATCH] =?UTF-8?q?Mod=20aaa-12=20=20dynamic=20ssh=20or=20tenlet?= =?UTF-8?q?=20can=20print=20syslog=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9Azhangtao=20=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E4=BA=BA=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Platform/user/ulog/log-sched/log_pty.c | 162 ++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 4 deletions(-) diff --git a/Platform/user/ulog/log-sched/log_pty.c b/Platform/user/ulog/log-sched/log_pty.c index 419d0681c..f1a981f41 100755 --- a/Platform/user/ulog/log-sched/log_pty.c +++ b/Platform/user/ulog/log-sched/log_pty.c @@ -1,22 +1,170 @@ #include -#include +#include #include #include +#include +#include +#include +#include #include "log_pty.h" #include "log_types.h" #include "log_common.h" +#include "ret_errno.h" +#define MAX_EVENT_NUMBER 64 #define LOG_DEV_PTY_DIR LOG_DEV_DIR"pts/" #define LOG_CONF_PTY_FILE_NAME "log-pty.conf" +int g_epoll_fd = -1; +int g_watch_fd = -1; +int g_inotify_fd = -1; +pthread_t g_monitor_thread; + +static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg); + +static void *pty_monitor_thread(void *arg) +{ + struct epoll_event events[MAX_EVENT_NUMBER]; + int ret; + char buf[MAX_LINE_SZ]; + ssize_t len; + char *ptr; + struct inotify_event *inotify_ev; + + ULOG_DEBUG(g_log, "Monitor pty is begining"); + while (1) { + //ULOG_DEBUG(g_log, "Epoll is waiting"); + ret = epoll_wait(g_epoll_fd, events, MAX_EVENT_NUMBER, 1000); + if (ret == -1) { + if (errno == EBADF) { + ULOG_DEBUG(g_log, "Epoll has been shut or invalid"); + } else { + ULOG_ERR(g_log, "Waiting epoll is failure:%s, code:%d", strerror(errno), errno); + } + break; + } + if (ret == 0) { // 超时 + continue; + } + + for (int i = 0; i < MAX_EVENT_NUMBER; i++) { + ULOG_DEBUG(g_log, "Epoll event value is 0x%x", events[i].events); + if ((events[i].events & EPOLLIN) != EPOLLIN) { + continue; + } + len = read(g_inotify_fd, buf, sizeof(buf)); + if ((len == -1) && (errno != EAGAIN)) { + ULOG_ERR(g_log, "Reading inotify is failure:%s", strerror(errno)); + break; + } + if (len <= 0) { + continue; + } + + for (ptr = buf; ptr < buf + len; + ptr += sizeof(struct inotify_event) + inotify_ev->len) { + inotify_ev = (struct inotify_event *) ptr; + if (((inotify_ev->mask & IN_CREATE) != IN_CREATE) + && ((inotify_ev->mask & IN_DELETE) != IN_DELETE)) { + ULOG_DEBUG(g_log, "Current ev mask:%u, anything willn't be done", inotify_ev->mask); + continue; + } + + // sleep一下,防止权限修改不成功 + sleep(1); + + ULOG_DEBUG(g_log, "Current ev mask:%u, wo will write configure and restart rsyslog", inotify_ev->mask); + log_conf(LOG_INFO, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, NULL, + write_pty_content, NULL); + + if (log_sev_restart() != 0) { + ULOG_ERR(g_log, "Pty monitor restart rsyslog 2 is failure"); + } + } + } + + } + + ULOG_ERR(g_log, "Monitor pty is end"); +} + +static void close_monitor() +{ + if (g_epoll_fd != -1) { + close(g_epoll_fd); + g_epoll_fd = -1; + } + + if (g_watch_fd != -1) { + inotify_rm_watch(g_inotify_fd, g_watch_fd); + close(g_watch_fd); + g_watch_fd = -1; + } + if (g_inotify_fd != -1) { + close(g_inotify_fd); + g_inotify_fd = -1; + } +} + +static ret_code start_dev_pty_monitor() +{ + if (g_inotify_fd != -1) { + ULOG_ERR(g_log, "Monitoring pty has started up, value:%d", g_inotify_fd); + return RET_OK; + } + + g_inotify_fd = inotify_init(); + if (g_inotify_fd == -1) { + ULOG_ERR(g_log, "Initiating file notify is failure:%s", strerror(errno)); + return RET_ERR; + } + g_watch_fd = inotify_add_watch(g_inotify_fd, LOG_DEV_PTY_DIR, IN_CREATE | IN_DELETE); + if(g_watch_fd == -1) { + ULOG_ERR(g_log, "Intify add watch is failure:%s", strerror(errno)); + goto FAIL; + } + + g_epoll_fd = epoll_create1(0); + if (g_epoll_fd == -1) { + ULOG_ERR(g_log, "Creating epoll is failure:%s", strerror(errno)); + goto FAIL; + } + + struct epoll_event ev; + ev.events = EPOLLIN; + ev.data.fd = g_inotify_fd; + if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, g_inotify_fd, &ev) == -1) { + ULOG_ERR(g_log, "Setting epoll is failure:%s", strerror(errno)); + goto FAIL; + } + + int ret = pthread_create(&g_monitor_thread, NULL, pty_monitor_thread, NULL); + if (ret != 0) { + ULOG_ERR(g_log, "Creating monitor thread is failure:%d", ret); + goto FAIL; + } + + return RET_OK; +FAIL: + close_monitor(); + return RET_ERR; +} + +static void stop_dev_pty_monitor() +{ + ULOG_DEBUG(g_log, "Stopping dev pty monitor"); + close_monitor(); + pthread_join(g_monitor_thread, NULL); +} + static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) { DIR *dir; 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)); + ULOG_ERR(g_log, "Open dir:[%s] is failure:%d", LOG_DEV_PTY_DIR, strerror(errno)); return -1; } @@ -45,7 +193,12 @@ static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, v return -1; } - } + } + + if (start_dev_pty_monitor() != RET_OK) { + ULOG_ERR(g_log, "Starting pty monitor is failure"); + return -1; + } return 0; } @@ -56,7 +209,8 @@ static int config_log_pty(const log_pty_t *conf) switch (conf->on) { - case LOG_OFF: + case LOG_OFF: + stop_dev_pty_monitor(); ret = log_off_with_file(LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME); break; case LOG_ON: