Mod aaa-12 log pty add pthread lock

RCA:
SOL:
修改人:zhangtao
检视人:
This commit is contained in:
zhangtaohz 2019-08-30 17:34:00 +08:00
parent b143e20424
commit 3d7c220514
1 changed files with 72 additions and 41 deletions

View File

@ -20,20 +20,62 @@
#define LOG_CONF_KEY_PTY_LEVEL_STR "pty.level" #define LOG_CONF_KEY_PTY_LEVEL_STR "pty.level"
#define LOG_CONF_KEY_PTY_MODULE_STR "pty.module" #define LOG_CONF_KEY_PTY_MODULE_STR "pty.module"
pthread_mutex_t g_mutex_pty;
volatile int g_epoll_fd = -1;
int g_epoll_fd = -1;
int g_watch_fd = -1; int g_watch_fd = -1;
int g_inotify_fd = -1; int g_inotify_fd = -1;
pthread_t g_monitor_thread; pthread_t g_monitor_thread;
int access(const char *pathname, int mode);
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);
static ret_code write_pty_conf()
{
char value_str[128]="";
uint level = LOG_INFO;
char *module = NULL;
ret_code ret = RET_OK;
memset(value_str, 0, sizeof(value_str));
sprintf(value_str, "%s%s", LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME);
if (access(value_str, 0) == 0)
{
/* 获取配置 */
memset(value_str, 0, sizeof(value_str));
if (get_log_file_conf(LOG_CONF_KEY_PTY_LEVEL_STR, value_str, sizeof(value_str)) == 0)
{
level = atoi(value_str);
}
memset(value_str, 0, sizeof(value_str));
if (get_log_file_conf(LOG_CONF_KEY_PTY_MODULE_STR, value_str, sizeof(value_str)) == 0)
{
module = value_str;
}
pthread_mutex_lock(&g_mutex_pty);
int ret_conf = log_conf((u8)level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, module,
write_pty_content, NULL);
pthread_mutex_unlock(&g_mutex_pty);
if (ret_conf != 0)
{
ULOG_ERR(g_log, "Log's pty configure which is written is failure");
ret = RET_ERR;
}
else
{
ULOG_DEBUG(g_log, "Pty config level(%u) and module(%s) successed.", level, module!=NULL?module:"NULL");
}
}
return ret;
}
static void *pty_monitor_thread(void *arg) static void *pty_monitor_thread(void *arg)
{ {
struct epoll_event events[MAX_EVENT_NUMBER]; struct epoll_event events[MAX_EVENT_NUMBER];
int ret; int ret;
ret_code ret_c;
char buf[MAX_LINE_SZ]; char buf[MAX_LINE_SZ];
ssize_t len; ssize_t len;
char *ptr; char *ptr;
@ -82,8 +124,11 @@ static void *pty_monitor_thread(void *arg)
sleep(1); sleep(1);
ULOG_DEBUG(g_log, "Current ev mask:%u, wo will write configure and restart rsyslog", inotify_ev->mask); 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, ret_c = write_pty_conf();
write_pty_content, NULL); if (ret_c != RET_OK) {
ULOG_ERR(g_log, "Pty monitor write pty configure is failure(code:%d)", ret_c);
continue;
}
if (log_sev_restart() != 0) { if (log_sev_restart() != 0) {
ULOG_ERR(g_log, "Pty monitor restart rsyslog 2 is failure"); ULOG_ERR(g_log, "Pty monitor restart rsyslog 2 is failure");
@ -233,10 +278,12 @@ static int config_log_pty(const log_pty_t *conf)
break; break;
case LOG_ON: case LOG_ON:
if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, conf->module, pthread_mutex_lock(&g_mutex_pty);
write_pty_content, NULL) != 0) { ret = log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, conf->module,
write_pty_content, NULL);
pthread_mutex_unlock(&g_mutex_pty);
if (ret != 0) {
ULOG_ERR(g_log, "Log's pty configure which is written is failure"); ULOG_ERR(g_log, "Log's pty configure which is written is failure");
ret = -1;
} else { } else {
ret = 0; ret = 0;
} }
@ -283,46 +330,30 @@ void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data
ret_code pty_initial() ret_code pty_initial()
{ {
char value_str[128]=""; int thread_ret = pthread_mutex_init(&g_mutex_pty, NULL);
uint level = 6; if (thread_ret != 0) {
char *module = NULL; ULOG_DEBUG(g_log, "Initiating pthread lock is failure(cdoe:%d)", thread_ret);
ret_code ret = RET_OK; }
memset(value_str, 0, sizeof(value_str)); ret_code ret = write_pty_conf();
sprintf(value_str, "%s%s", LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME); if (ret != RET_OK) {
if (access(value_str, 0) == 0) ULOG_DEBUG(g_log, "Initiating pty is failure(cdoe:%d)", ret);
{ } else {
/* 获取配置 */ ULOG_INFO(g_log, "Initiating pty is success");
memset(value_str, 0, sizeof(value_str)); }
if (get_log_file_conf(LOG_CONF_KEY_PTY_LEVEL_STR, value_str, sizeof(value_str)) == 0)
{
level = atoi(value_str);
}
memset(value_str, 0, sizeof(value_str));
if (get_log_file_conf(LOG_CONF_KEY_PTY_MODULE_STR, value_str, sizeof(value_str)) == 0)
{
module = value_str;
}
if (log_conf((u8)level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, module,
write_pty_content, NULL) != 0)
{
ULOG_ERR(g_log, "Log's pty configure which is written is failure");
ret = RET_ERR;
}
else
{
ULOG_DEBUG(g_log, "Pty config level(%u) and module(%s) successed.", level, module!=NULL?module:"NULL");
}
}
return ret; return ret;
} }
ret_code pty_exit() ret_code pty_exit()
{ {
ret_code ret = RET_OK;
int thread_ret = pthread_mutex_destroy(&g_mutex_pty);
if (0 != thread_ret) {
ret = RET_ERR;
ULOG_DEBUG(g_log, "Destroying pthread lock is failure(cdoe:%d)", thread_ret);
}
stop_dev_pty_monitor(); stop_dev_pty_monitor();
return RET_OK; return ret;
} }