forked from draios/sysdig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.h
239 lines (183 loc) · 5.61 KB
/
filter.h
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright (C) 2013-2014 Draios inc.
This file is part of sysdig.
sysdig is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
sysdig is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sysdig. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <set>
#ifdef HAS_FILTERING
class sinsp_filter_expression;
class sinsp_filter_check;
/*
* Operators to compare events
*/
enum cmpop {
CO_NONE = 0,
CO_EQ = 1,
CO_NE = 2,
CO_LT = 3,
CO_LE = 4,
CO_GT = 5,
CO_GE = 6,
CO_CONTAINS = 7,
CO_IN = 8,
CO_EXISTS = 9,
CO_ICONTAINS = 10,
CO_STARTSWITH = 11,
CO_GLOB = 12,
CO_PMATCH = 13
};
enum boolop
{
BO_NONE = 0,
BO_NOT = 1,
BO_OR = 2,
BO_AND = 4,
// obtained by bitwise OR'ing with one of above ops
BO_ORNOT = 3,
BO_ANDNOT = 5,
};
/** @defgroup filter Filtering events
* Filtering infrastructure.
* @{
*/
/*!
\brief This is the class that runs sysdig-type filters.
*/
class SINSP_PUBLIC sinsp_filter
{
public:
/*!
\brief Constructs the filter.
\param inspector Pointer to the inspector instance that will generate the
events to be filtered.
*/
sinsp_filter(sinsp* inspector);
~sinsp_filter();
/*!
\brief Applies the filter to the given event.
\param evt Pointer that needs to be filtered.
\return true if the event is accepted by the filter, false if it's rejected.
*/
bool run(sinsp_evt *evt);
void push_expression(boolop op);
void pop_expression();
void add_check(sinsp_filter_check* chk);
private:
void parse_check(sinsp_filter_expression* parent_expr, boolop op);
sinsp* m_inspector;
sinsp_filter_expression* m_curexpr;
sinsp_filter_expression* m_filter;
friend class sinsp_evt_formatter;
};
/*!
\brief This is the class that compiles sysdig-type filters.
*/
class SINSP_PUBLIC sinsp_filter_compiler
{
public:
/*!
\brief Constructs the compiler.
\param inspector Pointer to the inspector instance that will generate the
events to be filtered.
\param fltstr the filter string to compile.
\param ttable_only for internal use only.
\note Throws a sinsp_exception if the filter syntax is not valid.
*/
sinsp_filter_compiler(sinsp* inspector/* xxx needed? */, const string& fltstr, bool ttable_only=false);
~sinsp_filter_compiler();
sinsp_filter* compile();
private:
enum state
{
ST_EXPRESSION_DONE,
ST_NEED_EXPRESSION,
};
sinsp_filter* compile_();
char next();
bool compare_no_consume(const string& str);
vector<char> next_operand(bool expecting_first_operand, bool in_clause);
cmpop next_comparison_operator();
void parse_check();
static bool isblank(char c);
static bool is_special_char(char c);
static bool is_bracket(char c);
sinsp* m_inspector;
bool m_ttable_only;
string m_fltstr;
int32_t m_scanpos;
int32_t m_scansize;
state m_state;
boolop m_last_boolop;
int32_t m_nest_level;
sinsp_filter* m_filter;
friend class sinsp_evt_formatter;
};
/*!
\brief This class represents a filter optimized using event
types. It actually consists of collections of sinsp_filter objects
grouped by event type.
*/
class SINSP_PUBLIC sinsp_evttype_filter
{
public:
sinsp_evttype_filter();
virtual ~sinsp_evttype_filter();
void add(std::string &name,
std::set<uint32_t> &evttypes,
std::set<string> &tags,
sinsp_filter* filter);
// rulesets are arbitrary numbers and should be managed by the caller.
// Note that rulesets are used to index into a std::vector so
// specifying unnecessarily large rulesets will result in
// unnecessarily large vectors.
// Find those rules matching the provided pattern and set
// their enabled status to enabled.
void enable(const std::string &pattern, bool enabled, uint16_t ruleset = 0);
// Find those rules that have a tag in the set of tags and set
// their enabled status to enabled. Note that the enabled
// status is on the rules, and not the tags--if a rule R has
// tags (a, b), and you call eanble_tags([a], true) and then
// enable_tags([b], false), R will be disabled despite the
// fact it has tag a and was enabled by the first call to
// enable_tags.
void enable_tags(const std::set<string> &tags, bool enabled, uint16_t ruleset = 0);
// Match all filters against the provided event.
bool run(sinsp_evt *evt, uint16_t ruleset = 0);
private:
class filter_wrapper {
public:
filter_wrapper();
virtual ~filter_wrapper();
sinsp_filter *filter;
std::set<uint32_t> evttypes;
// Indexes from ruleset to enabled/disabled. Unlike
// m_filter_by_evttype, this is managed as a vector as we're
// expecting ruleset ids that are small and grouped in the range
// 0..k, as compared to all possible event types.
std::vector<bool> enabled;
};
// Maps from event type to filter. There can be multiple
// filters per event type.
std::list<filter_wrapper *> *m_filter_by_evttype[PPM_EVENT_MAX];
// It's possible to add an event type filter with an empty
// list of event types, meaning it should run for all event
// types.
std::list<filter_wrapper *> m_catchall_evttype_filters;
// Maps from tag to list of filters having that tag.
std::map<std::string, std::list<filter_wrapper *>> m_filter_by_tag;
// This holds all the filters in
// m_filter_by_evttype/m_catchall_evttype_filters, so they can
// be cleaned up.
map<std::string,filter_wrapper *> m_evttype_filters;
};
/*@}*/
#endif // HAS_FILTERING