forked from calvinwilliams/logpipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogpipe_fifo.c
152 lines (130 loc) · 5.33 KB
/
logpipe_fifo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* logpipe - Distribute log collector
* author : calvin
* email : [email protected]
*
* Licensed under the LGPL v2.1, see the file LICENSE in base directory.
*/
#include "logpipe_in.h"
#if 0
/* 创建内部状态输出有名管道 */
int CreateLogpipeFifo( struct LogpipeEnv *p_env )
{
struct epoll_event event ;
int nret = 0 ;
/* 创建内部状态输出有名管道 */
nret = mkfifo( p_env->logpipe_fifo_path_filename , 00777 ) ;
if( nret == -1 )
{
INFOLOGC( "mkfifo[%s] failed[%d]" , p_env->logpipe_fifo_path_filename , nret )
}
else
{
INFOLOGC( "mkfifo[%s] ok" , p_env->logpipe_fifo_path_filename )
}
/* 创建内部状态输出有名管道文件监控描述字 */
p_env->logpipe_fifo_inotify_fd = inotify_init() ;
if( p_env->logpipe_fifo_inotify_fd == -1 )
{
ERRORLOGC( "inotify_init failed , errno[%d]" , errno )
return -1;
}
/* 注册内部状态输出有名管道文件打开事件 */
p_env->logpipe_fifo_inotify_wd = inotify_add_watch( p_env->logpipe_fifo_inotify_fd , p_env->logpipe_fifo_path_filename , (uint32_t)(IN_OPEN|IN_DELETE_SELF|IN_MOVE_SELF|IN_Q_OVERFLOW) );
if( p_env->logpipe_fifo_inotify_wd == -1 )
{
ERRORLOGC( "inotify_add_watch[%s] failed , errno[%d]" , p_env->logpipe_fifo_path_filename , errno )
return -1;
}
else
{
INFOLOGC( "inotify_add_watch[%s] ok , inotify_fd[%d] inotify_wd[%d]" , p_env->logpipe_fifo_path_filename , p_env->logpipe_fifo_inotify_fd , p_env->logpipe_fifo_inotify_wd )
}
/* 内部状态输出有名管道描述字加入epoll */
memset( & event , 0x00 , sizeof(struct epoll_event) );
event.events = EPOLLIN | EPOLLERR ;
event.data.ptr = & (p_env->logpipe_fifo_inotify_fd) ;
nret = epoll_ctl( p_env->epoll_fd , EPOLL_CTL_ADD , p_env->logpipe_fifo_inotify_fd , & event ) ;
if( nret == -1 )
{
ERRORLOGC( "epoll_ctl[%d] add logpipe_fifo fd[%d] failed , errno[%d]" , p_env->epoll_fd , p_env->logpipe_fifo_inotify_fd , errno )
return -1;
}
else
{
INFOLOGC( "epoll_ctl[%d] add logpipe_fifo fd[%d] ok" , p_env->epoll_fd , p_env->logpipe_fifo_inotify_fd )
}
return 0;
}
/* 处理内部状态输出有名管道事件 */
int ProcessLogpipeFifoEvents( struct LogpipeEnv *p_env )
{
char inotify_read_buffer[ 1024 + 1 ] ;
long inotify_read_buflen ;
struct inotify_event *p_inotify_event = NULL ;
struct inotify_event *p_overflow_inotify_event = NULL ;
int nret = 0 ;
/* 读文件变化事件 */
DEBUGLOGC( "read logpipe_fifo_inotify[%d] ..." , p_env->logpipe_fifo_inotify_fd )
memset( inotify_read_buffer , 0x00 , sizeof(inotify_read_buffer) );
inotify_read_buflen = read( p_env->logpipe_fifo_inotify_fd , inotify_read_buffer , sizeof(inotify_read_buffer)-1 ) ;
if( inotify_read_buflen == -1 )
{
FATALLOGC( "read logpipe_fifo_inotify[%d] failed , errno[%d]" , p_env->logpipe_fifo_inotify_fd , errno )
return -1;
}
else
{
INFOLOGC( "read logpipe_fifo_inotify[%d] ok , [%d]bytes" , p_env->logpipe_fifo_inotify_fd , inotify_read_buflen )
}
p_inotify_event = (struct inotify_event *)(inotify_read_buffer) ;
p_overflow_inotify_event = (struct inotify_event *)(inotify_read_buffer+inotify_read_buflen) ;
while( p_inotify_event < p_overflow_inotify_event )
{
if( p_inotify_event->mask & IN_DELETE_SELF || p_inotify_event->mask & IN_MOVE_SELF )
{
INFOLOGC( "INOTIFY_EVENT IN_DELETE_SELF or IN_MOVED_SELF , wd[%d] mask[0x%X] cookie[%d] len[%d] name[%.*s]" , p_inotify_event->wd , p_inotify_event->mask , p_inotify_event->cookie , p_inotify_event->len , p_inotify_event->len , p_inotify_event->name )
/* 解除文件监控订阅 */
inotify_rm_watch( p_env->logpipe_fifo_inotify_fd , p_env->logpipe_fifo_inotify_wd );
/* 删除文件监控描述字 */
epoll_ctl( p_env->epoll_fd , EPOLL_CTL_DEL , p_env->logpipe_fifo_inotify_fd , NULL );
close( p_env->logpipe_fifo_inotify_fd );
/* 创建内部状态输出有名管道 */
memset( p_env->logpipe_fifo_path_filename , 0x00 , sizeof(p_env->logpipe_fifo_path_filename) );
snprintf( p_env->logpipe_fifo_path_filename , sizeof(p_env->logpipe_fifo_path_filename)-1 , "%s/etc/logpipe.fifo" , getenv("HOME") );
nret = CreateLogpipeFifo( p_env ) ;
if( nret )
{
ERRORLOGC( "CreateLogpipeFifo failed[%d]" , nret )
return -1;
}
else
{
INFOLOGC( "CreateLogpipeFifo ok" )
}
}
else if( p_inotify_event->mask & IN_OPEN )
{
INFOLOGC( "INOTIFY_EVENT IN_OPEN , wd[%d] mask[0x%X] cookie[%d] len[%d] name[%.*s]" , p_inotify_event->wd , p_inotify_event->mask , p_inotify_event->cookie , p_inotify_event->len , p_inotify_event->len , p_inotify_event->name )
FILE *fp = NULL ;
fp = fopen( p_env->logpipe_fifo_path_filename , "w" ) ;
if( fp )
{
fprintf( fp , "logpipe v%s build %s %s\n" , __LOGPIPE_VERSION , __DATE__ , __TIME__ );
fprintf( fp , "-----------------------------------\n" );
fprintf( fp , " config_path_filename[%s]\n" , p_env->config_path_filename );
fprintf( fp , " log_file[%s]\n" , p_env->log_file );
fprintf( fp , "logpipe_fifo_path_filename[%s]\n" , p_env->logpipe_fifo_path_filename );
fputc( EOF , fp );
fclose( fp );
}
}
else
{
ERRORLOGC( "unknow file inotify event mask[0x%X]" , p_inotify_event->mask )
}
p_inotify_event = (struct inotify_event *)( (char*)p_inotify_event + sizeof(struct inotify_event) + p_inotify_event->len ) ;
}
return 0;
}
#endif