Mod aaa-12 dynamic ssh or tenlet can print syslog
RCA: SOL: 修改人:zhangtao 检视人:
This commit is contained in:
parent
a9e2537777
commit
6efd824b67
|
@ -1,22 +1,170 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/inotify.h>
|
||||||
|
#include <sys/epoll.h>
|
||||||
|
|
||||||
#include "log_pty.h"
|
#include "log_pty.h"
|
||||||
#include "log_types.h"
|
#include "log_types.h"
|
||||||
#include "log_common.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_DEV_PTY_DIR LOG_DEV_DIR"pts/"
|
||||||
#define LOG_CONF_PTY_FILE_NAME "log-pty.conf"
|
#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)
|
static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
|
|
||||||
if ((dir = opendir(LOG_DEV_PTY_DIR)) == NULL) {
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +195,11 @@ static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, v
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (start_dev_pty_monitor() != RET_OK) {
|
||||||
|
ULOG_ERR(g_log, "Starting pty monitor is failure");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +210,7 @@ static int config_log_pty(const log_pty_t *conf)
|
||||||
switch (conf->on)
|
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);
|
ret = log_off_with_file(LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME);
|
||||||
break;
|
break;
|
||||||
case LOG_ON:
|
case LOG_ON:
|
||||||
|
|
Loading…
Reference in New Issue