forked from rostedt/trace-cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-filter.c
2432 lines (2047 loc) · 51.6 KB
/
parse-filter.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010 Red Hat Inc, Steven Rostedt <[email protected]>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License (not later!)
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/types.h>
#include "event-parse.h"
#include "event-utils.h"
#define COMM "COMM"
static struct format_field comm = {
.name = "COMM",
};
struct event_list {
struct event_list *next;
struct event_format *event;
};
static void show_error(char *error_buf, const char *fmt, ...)
{
unsigned long long index;
const char *input;
va_list ap;
size_t len;
size_t i;
input = pevent_get_input_buf();
index = pevent_get_input_buf_ptr();
len = input ? strlen(input) : 0;
if (len) {
strcpy(error_buf, input);
error_buf[len] = '\n';
for (i = 1; i < len && i < index; i++)
error_buf[len+i] = ' ';
error_buf[len + i] = '^';
error_buf[len + i + 1] = '\n';
len += i+2;
}
va_start(ap, fmt);
vsnprintf(error_buf + len, PEVENT_FILTER_ERROR_BUFSZ - len, fmt, ap);
va_end(ap);
}
static void free_token(char *token)
{
pevent_free_token(token);
}
static enum event_type read_token(char **tok)
{
enum event_type type;
char *token = NULL;
do {
free_token(token);
type = pevent_read_token(&token);
} while (type == EVENT_NEWLINE || type == EVENT_SPACE);
/* If token is = or ! check to see if the next char is ~ */
if (token &&
(strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&
pevent_peek_char() == '~') {
/* append it */
*tok = malloc(3);
if (*tok == NULL) {
free_token(token);
return EVENT_ERROR;
}
sprintf(*tok, "%c%c", *token, '~');
free_token(token);
/* Now remove the '~' from the buffer */
pevent_read_token(&token);
free_token(token);
} else
*tok = token;
return type;
}
static int filter_cmp(const void *a, const void *b)
{
const struct filter_type *ea = a;
const struct filter_type *eb = b;
if (ea->event_id < eb->event_id)
return -1;
if (ea->event_id > eb->event_id)
return 1;
return 0;
}
static struct filter_type *
find_filter_type(struct event_filter *filter, int id)
{
struct filter_type *filter_type;
struct filter_type key;
key.event_id = id;
filter_type = bsearch(&key, filter->event_filters,
filter->filters,
sizeof(*filter->event_filters),
filter_cmp);
return filter_type;
}
static struct filter_type *
add_filter_type(struct event_filter *filter, int id)
{
struct filter_type *filter_type;
int i;
filter_type = find_filter_type(filter, id);
if (filter_type)
return filter_type;
filter_type = realloc(filter->event_filters,
sizeof(*filter->event_filters) *
(filter->filters + 1));
if (!filter_type)
return NULL;
filter->event_filters = filter_type;
for (i = 0; i < filter->filters; i++) {
if (filter->event_filters[i].event_id > id)
break;
}
if (i < filter->filters)
memmove(&filter->event_filters[i+1],
&filter->event_filters[i],
sizeof(*filter->event_filters) *
(filter->filters - i));
filter_type = &filter->event_filters[i];
filter_type->event_id = id;
filter_type->event = pevent_find_event(filter->pevent, id);
filter_type->filter = NULL;
filter->filters++;
return filter_type;
}
/**
* pevent_filter_alloc - create a new event filter
* @pevent: The pevent that this filter is associated with
*/
struct event_filter *pevent_filter_alloc(struct pevent *pevent)
{
struct event_filter *filter;
filter = malloc(sizeof(*filter));
if (filter == NULL)
return NULL;
memset(filter, 0, sizeof(*filter));
filter->pevent = pevent;
pevent_ref(pevent);
return filter;
}
static struct filter_arg *allocate_arg(void)
{
return calloc(1, sizeof(struct filter_arg));
}
static void free_arg(struct filter_arg *arg)
{
if (!arg)
return;
switch (arg->type) {
case FILTER_ARG_NONE:
case FILTER_ARG_BOOLEAN:
break;
case FILTER_ARG_NUM:
free_arg(arg->num.left);
free_arg(arg->num.right);
break;
case FILTER_ARG_EXP:
free_arg(arg->exp.left);
free_arg(arg->exp.right);
break;
case FILTER_ARG_STR:
free(arg->str.val);
regfree(&arg->str.reg);
free(arg->str.buffer);
break;
case FILTER_ARG_VALUE:
if (arg->value.type == FILTER_STRING ||
arg->value.type == FILTER_CHAR)
free(arg->value.str);
break;
case FILTER_ARG_OP:
free_arg(arg->op.left);
free_arg(arg->op.right);
default:
break;
}
free(arg);
}
static int add_event(struct event_list **events,
struct event_format *event)
{
struct event_list *list;
list = malloc(sizeof(*list));
if (list == NULL)
return -1;
list->next = *events;
*events = list;
list->event = event;
return 0;
}
static int event_match(struct event_format *event,
regex_t *sreg, regex_t *ereg)
{
if (sreg) {
return !regexec(sreg, event->system, 0, NULL, 0) &&
!regexec(ereg, event->name, 0, NULL, 0);
}
return !regexec(ereg, event->system, 0, NULL, 0) ||
!regexec(ereg, event->name, 0, NULL, 0);
}
static enum pevent_errno
find_event(struct pevent *pevent, struct event_list **events,
char *sys_name, char *event_name)
{
struct event_format *event;
regex_t ereg;
regex_t sreg;
int match = 0;
int fail = 0;
char *reg;
int ret;
int i;
if (!event_name) {
/* if no name is given, then swap sys and name */
event_name = sys_name;
sys_name = NULL;
}
reg = malloc(strlen(event_name) + 3);
if (reg == NULL)
return PEVENT_ERRNO__MEM_ALLOC_FAILED;
sprintf(reg, "^%s$", event_name);
ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB);
free(reg);
if (ret)
return PEVENT_ERRNO__INVALID_EVENT_NAME;
if (sys_name) {
reg = malloc(strlen(sys_name) + 3);
if (reg == NULL) {
regfree(&ereg);
return PEVENT_ERRNO__MEM_ALLOC_FAILED;
}
sprintf(reg, "^%s$", sys_name);
ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB);
free(reg);
if (ret) {
regfree(&ereg);
return PEVENT_ERRNO__INVALID_EVENT_NAME;
}
}
for (i = 0; i < pevent->nr_events; i++) {
event = pevent->events[i];
if (event_match(event, sys_name ? &sreg : NULL, &ereg)) {
match = 1;
if (add_event(events, event) < 0) {
fail = 1;
break;
}
}
}
regfree(&ereg);
if (sys_name)
regfree(&sreg);
if (!match)
return PEVENT_ERRNO__EVENT_NOT_FOUND;
if (fail)
return PEVENT_ERRNO__MEM_ALLOC_FAILED;
return 0;
}
static void free_events(struct event_list *events)
{
struct event_list *event;
while (events) {
event = events;
events = events->next;
free(event);
}
}
static enum pevent_errno
create_arg_item(struct event_format *event, const char *token,
enum event_type type, struct filter_arg **parg, char *error_str)
{
struct format_field *field;
struct filter_arg *arg;
arg = allocate_arg();
if (arg == NULL) {
show_error(error_str, "failed to allocate filter arg");
return PEVENT_ERRNO__MEM_ALLOC_FAILED;
}
switch (type) {
case EVENT_SQUOTE:
case EVENT_DQUOTE:
arg->type = FILTER_ARG_VALUE;
arg->value.type =
type == EVENT_DQUOTE ? FILTER_STRING : FILTER_CHAR;
arg->value.str = strdup(token);
if (!arg->value.str) {
free_arg(arg);
show_error(error_str, "failed to allocate string filter arg");
return PEVENT_ERRNO__MEM_ALLOC_FAILED;
}
break;
case EVENT_ITEM:
/* if it is a number, then convert it */
if (isdigit(token[0])) {
arg->type = FILTER_ARG_VALUE;
arg->value.type = FILTER_NUMBER;
arg->value.val = strtoull(token, NULL, 0);
break;
}
/* Consider this a field */
field = pevent_find_any_field(event, token);
if (!field) {
if (strcmp(token, COMM) != 0) {
/* not a field, Make it false */
arg->type = FILTER_ARG_BOOLEAN;
arg->boolean.value = FILTER_FALSE;
break;
}
/* If token is 'COMM' then it is special */
field = &comm;
}
arg->type = FILTER_ARG_FIELD;
arg->field.field = field;
break;
default:
free_arg(arg);
show_error(error_str, "expected a value but found %s", token);
return PEVENT_ERRNO__UNEXPECTED_TYPE;
}
*parg = arg;
return 0;
}
static struct filter_arg *
create_arg_op(enum filter_op_type btype)
{
struct filter_arg *arg;
arg = allocate_arg();
if (!arg)
return NULL;
arg->type = FILTER_ARG_OP;
arg->op.type = btype;
return arg;
}
static struct filter_arg *
create_arg_exp(enum filter_exp_type etype)
{
struct filter_arg *arg;
arg = allocate_arg();
if (!arg)
return NULL;
arg->type = FILTER_ARG_EXP;
arg->op.type = etype;
return arg;
}
static struct filter_arg *
create_arg_cmp(enum filter_exp_type etype)
{
struct filter_arg *arg;
arg = allocate_arg();
if (!arg)
return NULL;
/* Use NUM and change if necessary */
arg->type = FILTER_ARG_NUM;
arg->op.type = etype;
return arg;
}
static enum pevent_errno
add_right(struct filter_arg *op, struct filter_arg *arg, char *error_str)
{
struct filter_arg *left;
char *str;
int op_type;
int ret;
switch (op->type) {
case FILTER_ARG_EXP:
if (op->exp.right)
goto out_fail;
op->exp.right = arg;
break;
case FILTER_ARG_OP:
if (op->op.right)
goto out_fail;
op->op.right = arg;
break;
case FILTER_ARG_NUM:
if (op->op.right)
goto out_fail;
/*
* The arg must be num, str, or field
*/
switch (arg->type) {
case FILTER_ARG_VALUE:
case FILTER_ARG_FIELD:
break;
default:
show_error(error_str, "Illegal rvalue");
return PEVENT_ERRNO__ILLEGAL_RVALUE;
}
/*
* Depending on the type, we may need to
* convert this to a string or regex.
*/
switch (arg->value.type) {
case FILTER_CHAR:
/*
* A char should be converted to number if
* the string is 1 byte, and the compare
* is not a REGEX.
*/
if (strlen(arg->value.str) == 1 &&
op->num.type != FILTER_CMP_REGEX &&
op->num.type != FILTER_CMP_NOT_REGEX) {
arg->value.type = FILTER_NUMBER;
goto do_int;
}
/* fall through */
case FILTER_STRING:
/* convert op to a string arg */
op_type = op->num.type;
left = op->num.left;
str = arg->value.str;
/* reset the op for the new field */
memset(op, 0, sizeof(*op));
/*
* If left arg was a field not found then
* NULL the entire op.
*/
if (left->type == FILTER_ARG_BOOLEAN) {
free_arg(left);
free_arg(arg);
op->type = FILTER_ARG_BOOLEAN;
op->boolean.value = FILTER_FALSE;
break;
}
/* Left arg must be a field */
if (left->type != FILTER_ARG_FIELD) {
show_error(error_str,
"Illegal lvalue for string comparison");
return PEVENT_ERRNO__ILLEGAL_LVALUE;
}
/* Make sure this is a valid string compare */
switch (op_type) {
case FILTER_CMP_EQ:
op_type = FILTER_CMP_MATCH;
break;
case FILTER_CMP_NE:
op_type = FILTER_CMP_NOT_MATCH;
break;
case FILTER_CMP_REGEX:
case FILTER_CMP_NOT_REGEX:
ret = regcomp(&op->str.reg, str, REG_ICASE|REG_NOSUB);
if (ret) {
show_error(error_str,
"RegEx '%s' did not compute",
str);
return PEVENT_ERRNO__INVALID_REGEX;
}
break;
default:
show_error(error_str,
"Illegal comparison for string");
return PEVENT_ERRNO__ILLEGAL_STRING_CMP;
}
op->type = FILTER_ARG_STR;
op->str.type = op_type;
op->str.field = left->field.field;
op->str.val = strdup(str);
if (!op->str.val) {
show_error(error_str, "Failed to allocate string filter");
return PEVENT_ERRNO__MEM_ALLOC_FAILED;
}
/*
* Need a buffer to copy data for tests
*/
op->str.buffer = malloc(op->str.field->size + 1);
if (!op->str.buffer) {
show_error(error_str, "Failed to allocate string filter");
return PEVENT_ERRNO__MEM_ALLOC_FAILED;
}
/* Null terminate this buffer */
op->str.buffer[op->str.field->size] = 0;
/* We no longer have left or right args */
free_arg(arg);
free_arg(left);
break;
case FILTER_NUMBER:
do_int:
switch (op->num.type) {
case FILTER_CMP_REGEX:
case FILTER_CMP_NOT_REGEX:
show_error(error_str,
"Op not allowed with integers");
return PEVENT_ERRNO__ILLEGAL_INTEGER_CMP;
default:
break;
}
/* numeric compare */
op->num.right = arg;
break;
default:
goto out_fail;
}
break;
default:
goto out_fail;
}
return 0;
out_fail:
show_error(error_str, "Syntax error");
return PEVENT_ERRNO__SYNTAX_ERROR;
}
static struct filter_arg *
rotate_op_right(struct filter_arg *a, struct filter_arg *b)
{
struct filter_arg *arg;
arg = a->op.right;
a->op.right = b;
return arg;
}
static enum pevent_errno add_left(struct filter_arg *op, struct filter_arg *arg)
{
switch (op->type) {
case FILTER_ARG_EXP:
if (arg->type == FILTER_ARG_OP)
arg = rotate_op_right(arg, op);
op->exp.left = arg;
break;
case FILTER_ARG_OP:
op->op.left = arg;
break;
case FILTER_ARG_NUM:
if (arg->type == FILTER_ARG_OP)
arg = rotate_op_right(arg, op);
/* left arg of compares must be a field */
if (arg->type != FILTER_ARG_FIELD &&
arg->type != FILTER_ARG_BOOLEAN)
return PEVENT_ERRNO__INVALID_ARG_TYPE;
op->num.left = arg;
break;
default:
return PEVENT_ERRNO__INVALID_ARG_TYPE;
}
return 0;
}
enum op_type {
OP_NONE,
OP_BOOL,
OP_NOT,
OP_EXP,
OP_CMP,
};
static enum op_type process_op(const char *token,
enum filter_op_type *btype,
enum filter_cmp_type *ctype,
enum filter_exp_type *etype)
{
*btype = FILTER_OP_NOT;
*etype = FILTER_EXP_NONE;
*ctype = FILTER_CMP_NONE;
if (strcmp(token, "&&") == 0)
*btype = FILTER_OP_AND;
else if (strcmp(token, "||") == 0)
*btype = FILTER_OP_OR;
else if (strcmp(token, "!") == 0)
return OP_NOT;
if (*btype != FILTER_OP_NOT)
return OP_BOOL;
/* Check for value expressions */
if (strcmp(token, "+") == 0) {
*etype = FILTER_EXP_ADD;
} else if (strcmp(token, "-") == 0) {
*etype = FILTER_EXP_SUB;
} else if (strcmp(token, "*") == 0) {
*etype = FILTER_EXP_MUL;
} else if (strcmp(token, "/") == 0) {
*etype = FILTER_EXP_DIV;
} else if (strcmp(token, "%") == 0) {
*etype = FILTER_EXP_MOD;
} else if (strcmp(token, ">>") == 0) {
*etype = FILTER_EXP_RSHIFT;
} else if (strcmp(token, "<<") == 0) {
*etype = FILTER_EXP_LSHIFT;
} else if (strcmp(token, "&") == 0) {
*etype = FILTER_EXP_AND;
} else if (strcmp(token, "|") == 0) {
*etype = FILTER_EXP_OR;
} else if (strcmp(token, "^") == 0) {
*etype = FILTER_EXP_XOR;
} else if (strcmp(token, "~") == 0)
*etype = FILTER_EXP_NOT;
if (*etype != FILTER_EXP_NONE)
return OP_EXP;
/* Check for compares */
if (strcmp(token, "==") == 0)
*ctype = FILTER_CMP_EQ;
else if (strcmp(token, "!=") == 0)
*ctype = FILTER_CMP_NE;
else if (strcmp(token, "<") == 0)
*ctype = FILTER_CMP_LT;
else if (strcmp(token, ">") == 0)
*ctype = FILTER_CMP_GT;
else if (strcmp(token, "<=") == 0)
*ctype = FILTER_CMP_LE;
else if (strcmp(token, ">=") == 0)
*ctype = FILTER_CMP_GE;
else if (strcmp(token, "=~") == 0)
*ctype = FILTER_CMP_REGEX;
else if (strcmp(token, "!~") == 0)
*ctype = FILTER_CMP_NOT_REGEX;
else
return OP_NONE;
return OP_CMP;
}
static int check_op_done(struct filter_arg *arg)
{
switch (arg->type) {
case FILTER_ARG_EXP:
return arg->exp.right != NULL;
case FILTER_ARG_OP:
return arg->op.right != NULL;
case FILTER_ARG_NUM:
return arg->num.right != NULL;
case FILTER_ARG_STR:
/* A string conversion is always done */
return 1;
case FILTER_ARG_BOOLEAN:
/* field not found, is ok */
return 1;
default:
return 0;
}
}
enum filter_vals {
FILTER_VAL_NORM,
FILTER_VAL_FALSE,
FILTER_VAL_TRUE,
};
static enum pevent_errno
reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child,
struct filter_arg *arg, char *error_str)
{
struct filter_arg *other_child;
struct filter_arg **ptr;
if (parent->type != FILTER_ARG_OP &&
arg->type != FILTER_ARG_OP) {
show_error(error_str, "can not reparent other than OP");
return PEVENT_ERRNO__REPARENT_NOT_OP;
}
/* Get the sibling */
if (old_child->op.right == arg) {
ptr = &old_child->op.right;
other_child = old_child->op.left;
} else if (old_child->op.left == arg) {
ptr = &old_child->op.left;
other_child = old_child->op.right;
} else {
show_error(error_str, "Error in reparent op, find other child");
return PEVENT_ERRNO__REPARENT_FAILED;
}
/* Detach arg from old_child */
*ptr = NULL;
/* Check for root */
if (parent == old_child) {
free_arg(other_child);
*parent = *arg;
/* Free arg without recussion */
free(arg);
return 0;
}
if (parent->op.right == old_child)
ptr = &parent->op.right;
else if (parent->op.left == old_child)
ptr = &parent->op.left;
else {
show_error(error_str, "Error in reparent op");
return PEVENT_ERRNO__REPARENT_FAILED;
}
*ptr = arg;
free_arg(old_child);
return 0;
}
/* Returns either filter_vals (success) or pevent_errno (failfure) */
static int test_arg(struct filter_arg *parent, struct filter_arg *arg,
char *error_str)
{
int lval, rval;
switch (arg->type) {
/* bad case */
case FILTER_ARG_BOOLEAN:
return FILTER_VAL_FALSE + arg->boolean.value;
/* good cases: */
case FILTER_ARG_STR:
case FILTER_ARG_VALUE:
case FILTER_ARG_FIELD:
return FILTER_VAL_NORM;
case FILTER_ARG_EXP:
lval = test_arg(arg, arg->exp.left, error_str);
if (lval != FILTER_VAL_NORM)
return lval;
rval = test_arg(arg, arg->exp.right, error_str);
if (rval != FILTER_VAL_NORM)
return rval;
return FILTER_VAL_NORM;
case FILTER_ARG_NUM:
lval = test_arg(arg, arg->num.left, error_str);
if (lval != FILTER_VAL_NORM)
return lval;
rval = test_arg(arg, arg->num.right, error_str);
if (rval != FILTER_VAL_NORM)
return rval;
return FILTER_VAL_NORM;
case FILTER_ARG_OP:
if (arg->op.type != FILTER_OP_NOT) {
lval = test_arg(arg, arg->op.left, error_str);
switch (lval) {
case FILTER_VAL_NORM:
break;
case FILTER_VAL_TRUE:
if (arg->op.type == FILTER_OP_OR)
return FILTER_VAL_TRUE;
rval = test_arg(arg, arg->op.right, error_str);
if (rval != FILTER_VAL_NORM)
return rval;
return reparent_op_arg(parent, arg, arg->op.right,
error_str);
case FILTER_VAL_FALSE:
if (arg->op.type == FILTER_OP_AND)
return FILTER_VAL_FALSE;
rval = test_arg(arg, arg->op.right, error_str);
if (rval != FILTER_VAL_NORM)
return rval;
return reparent_op_arg(parent, arg, arg->op.right,
error_str);
default:
return lval;
}
}
rval = test_arg(arg, arg->op.right, error_str);
switch (rval) {
case FILTER_VAL_NORM:
default:
break;
case FILTER_VAL_TRUE:
if (arg->op.type == FILTER_OP_OR)
return FILTER_VAL_TRUE;
if (arg->op.type == FILTER_OP_NOT)
return FILTER_VAL_FALSE;
return reparent_op_arg(parent, arg, arg->op.left,
error_str);
case FILTER_VAL_FALSE:
if (arg->op.type == FILTER_OP_AND)
return FILTER_VAL_FALSE;
if (arg->op.type == FILTER_OP_NOT)
return FILTER_VAL_TRUE;
return reparent_op_arg(parent, arg, arg->op.left,
error_str);
}
return rval;
default:
show_error(error_str, "bad arg in filter tree");
return PEVENT_ERRNO__BAD_FILTER_ARG;
}
return FILTER_VAL_NORM;
}
/* Remove any unknown event fields */
static int collapse_tree(struct filter_arg *arg,
struct filter_arg **arg_collapsed, char *error_str)
{
int ret;
ret = test_arg(arg, arg, error_str);
switch (ret) {
case FILTER_VAL_NORM:
break;
case FILTER_VAL_TRUE:
case FILTER_VAL_FALSE:
free_arg(arg);
arg = allocate_arg();
if (arg) {
arg->type = FILTER_ARG_BOOLEAN;
arg->boolean.value = ret == FILTER_VAL_TRUE;
} else {
show_error(error_str, "Failed to allocate filter arg");
ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
}
break;
default:
/* test_arg() already set the error_str */
free_arg(arg);
arg = NULL;
break;
}
*arg_collapsed = arg;
return ret;
}
static enum pevent_errno
process_filter(struct event_format *event, struct filter_arg **parg,
char *error_str, int not)
{
enum event_type type;
char *token = NULL;
struct filter_arg *current_op = NULL;
struct filter_arg *current_exp = NULL;
struct filter_arg *left_item = NULL;
struct filter_arg *arg = NULL;
enum op_type op_type;
enum filter_op_type btype;
enum filter_exp_type etype;
enum filter_cmp_type ctype;
enum pevent_errno ret;
*parg = NULL;
do {
free(token);
type = read_token(&token);
switch (type) {
case EVENT_SQUOTE:
case EVENT_DQUOTE:
case EVENT_ITEM:
ret = create_arg_item(event, token, type, &arg, error_str);
if (ret < 0)
goto fail;
if (!left_item)
left_item = arg;
else if (current_exp) {
ret = add_right(current_exp, arg, error_str);
if (ret < 0)
goto fail;
left_item = NULL;
/* Not's only one one expression */
if (not) {
arg = NULL;
if (current_op)
goto fail_syntax;
free(token);
*parg = current_exp;
return 0;
}
} else
goto fail_syntax;
arg = NULL;
break;
case EVENT_DELIM:
if (*token == ',') {