1
- pub mod bridge;
2
- pub mod logger;
3
-
4
- pub use bridge:: * ;
5
- use crossbeam:: channel:: { select, tick} ;
6
- use log:: { debug, info} ;
7
- use parking_lot:: Mutex ;
8
- use protobuf:: Message ;
9
- use signal_hook:: consts:: SIGTERM ;
10
1
use std:: {
11
2
env,
12
3
fs:: File ,
@@ -16,15 +7,17 @@ use std::{
16
7
time:: Duration ,
17
8
} ;
18
9
19
- #[ cfg( target_family = "unix" ) ]
20
- use signal_hook:: { consts:: SIGUSR1 , iterator:: Signals } ;
21
- #[ cfg( target_family = "unix" ) ]
22
- use std:: os:: unix:: prelude:: FromRawFd ;
10
+ use crossbeam:: channel:: { select, tick} ;
11
+ use log:: debug;
12
+ use parking_lot:: Mutex ;
13
+ use protobuf:: Message ;
14
+
15
+ pub mod logger;
23
16
24
- # [ cfg ( target_family = "windows" ) ]
25
- use std :: os :: windows :: prelude :: { FromRawHandle , RawHandle } ;
26
- # [ cfg ( target_family = "windows" ) ]
27
- use windows :: Win32 :: System :: Console :: { GetStdHandle , STD_INPUT_HANDLE , STD_OUTPUT_HANDLE } ;
17
+ pub mod bridge ;
18
+ pub use bridge :: * ;
19
+
20
+ pub mod sys ;
28
21
29
22
#[ derive( Clone ) ]
30
23
pub enum EncodeType {
@@ -37,62 +30,26 @@ pub struct Client {
37
30
writer : Arc < Mutex < BufWriter < File > > > ,
38
31
reader : Arc < Mutex < BufReader < File > > > ,
39
32
}
40
- #[ cfg( feature = "debug" ) ]
41
- const READ_PIPE_FD : i32 = 0 ;
42
- #[ cfg( not( feature = "debug" ) ) ]
43
- const READ_PIPE_FD : i32 = 3 ;
44
- #[ cfg( feature = "debug" ) ]
45
- const WRITE_PIPE_FD : i32 = 1 ;
46
- #[ cfg( not( feature = "debug" ) ) ]
47
- const WRITE_PIPE_FD : i32 = 4 ;
48
- const HIGH_PRIORIT_FD : i32 = 5 ;
49
33
50
34
impl Client {
51
-
52
35
pub fn can_use_high ( ) -> bool {
53
36
match env:: var ( "ELKEID_PLUGIN_HIGH_PRIORITY_PIPE" ) {
54
37
Ok ( value) => {
55
38
if !value. is_empty ( ) {
56
39
return true ;
57
40
}
58
-
59
41
}
60
42
Err ( _) => {
61
43
return false ;
62
44
}
63
-
64
45
}
65
46
false
66
47
}
67
48
pub fn new ( ignore_terminate : bool ) -> Self {
68
-
69
- let writer = Arc :: new ( Mutex :: new ( BufWriter :: with_capacity ( 512 * 1024 , unsafe {
70
- #[ cfg( target_family = "unix" ) ]
71
- {
72
- File :: from_raw_fd ( WRITE_PIPE_FD )
73
- }
74
-
75
- #[ cfg( target_family = "windows" ) ]
76
- {
77
- let raw_handle = GetStdHandle ( STD_OUTPUT_HANDLE ) . unwrap ( ) ;
78
- File :: from_raw_handle ( raw_handle. 0 as _ )
79
- }
80
- } ) ) ) ;
49
+ let writer = sys:: get_writer ( ) ;
81
50
let mut high_writer = writer. clone ( ) ;
82
- if Self :: can_use_high ( ) {
83
- high_writer = Arc :: new ( Mutex :: new ( BufWriter :: with_capacity ( 512 * 1024 , unsafe {
84
- #[ cfg( target_family = "unix" ) ]
85
- {
86
- File :: from_raw_fd ( HIGH_PRIORIT_FD )
87
- }
88
-
89
- #[ cfg( target_family = "windows" ) ]
90
- {
91
- let raw_handle = GetStdHandle ( STD_OUTPUT_HANDLE ) . unwrap ( ) ;
92
- File :: from_raw_handle ( raw_handle. 0 as _ )
93
- }
94
- } ) ) ) ;
95
-
51
+ if Self :: can_use_high ( ) {
52
+ high_writer = sys:: get_high_writer ( ) ;
96
53
let high_writer_c = high_writer. clone ( ) ;
97
54
thread:: spawn ( move || {
98
55
let ticker = tick ( Duration :: from_millis ( 200 ) ) ;
@@ -109,19 +66,8 @@ impl Client {
109
66
} ) ;
110
67
}
111
68
112
- let reader = Arc :: new ( Mutex :: new ( BufReader :: new ( unsafe {
113
- #[ cfg( target_family = "unix" ) ]
114
- {
115
- File :: from_raw_fd ( READ_PIPE_FD )
116
- }
69
+ let reader = sys:: get_reader ( ) ;
117
70
118
- #[ cfg( target_family = "windows" ) ]
119
- {
120
- let raw_handle = GetStdHandle ( STD_INPUT_HANDLE ) . unwrap ( ) ;
121
- File :: from_raw_handle ( raw_handle. 0 as _ )
122
- }
123
- } ) ) ) ;
124
-
125
71
let writer_c = writer. clone ( ) ;
126
72
thread:: spawn ( move || {
127
73
let ticker = tick ( Duration :: from_millis ( 200 ) ) ;
@@ -136,26 +82,18 @@ impl Client {
136
82
}
137
83
}
138
84
} ) ;
139
- #[ cfg( target_family = "unix" ) ]
85
+
86
+ sys:: regist_exception_handler ( ) ;
87
+
140
88
if ignore_terminate {
141
- let mut signals = Signals :: new ( & [ SIGTERM , SIGUSR1 ] ) . unwrap ( ) ;
142
- thread:: spawn ( move || {
143
- for sig in signals. forever ( ) {
144
- if sig == SIGTERM {
145
- info ! ( "received signal: {:?}, wait 3 secs to exit" , sig) ;
146
- thread:: sleep ( Duration :: from_secs ( 3 ) ) ;
147
- unsafe {
148
- if Self :: can_use_high ( ) {
149
- libc:: close ( HIGH_PRIORIT_FD ) ;
150
- }
151
- libc:: close ( WRITE_PIPE_FD ) ;
152
- libc:: close ( READ_PIPE_FD ) ;
153
- }
154
- }
155
- }
156
- } ) ;
89
+ sys:: ignore_terminate ( )
90
+ }
91
+
92
+ Self {
93
+ high_writer,
94
+ writer,
95
+ reader,
157
96
}
158
- Self { high_writer, writer, reader }
159
97
}
160
98
pub fn send_record ( & mut self , rec : & Record ) -> Result < ( ) , Error > {
161
99
let mut w = self . writer . lock ( ) ;
@@ -176,7 +114,6 @@ impl Client {
176
114
}
177
115
}
178
116
pub fn send_record_high_priority ( & mut self , rec : & Record ) -> Result < ( ) , Error > {
179
-
180
117
let mut w = self . high_writer . lock ( ) ;
181
118
#[ cfg( not( feature = "debug" ) ) ]
182
119
{
@@ -209,7 +146,6 @@ impl Client {
209
146
}
210
147
#[ cfg( feature = "debug" ) ]
211
148
{
212
-
213
149
for rec in recs. iter ( ) {
214
150
w. write_all ( b"{\" data_type\" :" ) ?;
215
151
w. write_all ( rec. data_type . to_string ( ) . as_bytes ( ) ) ?;
@@ -220,7 +156,7 @@ impl Client {
220
156
w. write_all ( b"}\n " ) ?
221
157
}
222
158
Ok ( ( ) )
223
- }
159
+ }
224
160
}
225
161
226
162
pub fn send_records ( & mut self , recs : & Vec < Record > ) -> Result < ( ) , Error > {
0 commit comments