/
lib-event.c
1004 lines (879 loc) · 25.5 KB
/
lib-event.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) 2017-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "lib-event-private.h"
#include "event-filter.h"
#include "array.h"
#include "llist.h"
#include "str.h"
#include "strescape.h"
#include "ioloop.h"
enum event_code {
EVENT_CODE_ALWAYS_LOG_SOURCE = 'a',
EVENT_CODE_CATEGORY = 'c',
EVENT_CODE_TV_LAST_SENT = 'l',
EVENT_CODE_SENDING_NAME = 'n',
EVENT_CODE_SOURCE = 's',
EVENT_CODE_FIELD_INTMAX = 'I',
EVENT_CODE_FIELD_STR = 'S',
EVENT_CODE_FIELD_TIMEVAL = 'T',
};
extern const struct event_passthrough event_passthrough_vfuncs;
static struct event *events = NULL;
static struct event *current_global_event = NULL;
static struct event_passthrough *event_last_passthrough = NULL;
static ARRAY(event_callback_t *) event_handlers;
static ARRAY(event_category_callback_t *) event_category_callbacks;
static ARRAY(struct event_category *) event_registered_categories;
static ARRAY(struct event *) global_event_stack;
static uint64_t event_id_counter = 0;
static struct event *last_passthrough_event(void)
{
return container_of(event_last_passthrough,
struct event, event_passthrough);
}
static void event_copy_parent_defaults(struct event *event,
const struct event *parent)
{
event->always_log_source = parent->always_log_source;
event->passthrough = parent->passthrough;
event->forced_debug = parent->forced_debug;
}
static bool
event_find_category(struct event *event, const struct event_category *category);
static struct event_field *
event_find_field_int(struct event *event, const char *key);
void event_copy_categories_fields(struct event *to, struct event *from)
{
unsigned int cat_count;
struct event_category *const *categories = event_get_categories(from, &cat_count);
while (cat_count-- > 0)
event_add_category(to, categories[cat_count]);
const struct event_field *fld;
if (!array_is_created(&from->fields))
return;
array_foreach(&from->fields, fld) {
switch (fld->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
event_add_str(to, fld->key, fld->value.str);
break;
case EVENT_FIELD_VALUE_TYPE_INTMAX:
event_add_int(to, fld->key, fld->value.intmax);
break;
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
event_add_timeval(to, fld->key, &fld->value.timeval);
break;
default:
break;
}
}
}
bool event_has_all_categories(struct event *event, const struct event *other)
{
struct event_category **cat;
if (!array_is_created(&other->categories))
return TRUE;
if (!array_is_created(&event->categories))
return FALSE;
array_foreach_modifiable(&other->categories, cat) {
if (!event_find_category(event, *cat))
return FALSE;
}
return TRUE;
}
bool event_has_all_fields(struct event *event, const struct event *other)
{
struct event_field *fld;
if (!array_is_created(&other->fields))
return TRUE;
array_foreach_modifiable(&other->fields, fld) {
if (event_find_field_int(event, fld->key) == NULL)
return FALSE;
}
return TRUE;
}
struct event *event_dup(const struct event *source)
{
struct event *ret = event_create(source->parent);
string_t *str = t_str_new(256);
const char *err;
event_export(source, str);
if (!event_import(ret, str_c(str), &err))
i_panic("event_import(%s) failed: %s", str_c(str), err);
return ret;
}
#undef event_create
struct event *event_create(struct event *parent, const char *source_filename,
unsigned int source_linenum)
{
struct event *event;
pool_t pool = pool_alloconly_create(MEMPOOL_GROWING"event", 64);
event = p_new(pool, struct event, 1);
event->event_passthrough = event_passthrough_vfuncs;
event->refcount = 1;
event->id = ++event_id_counter;
event->pool = pool;
event->tv_created = ioloop_timeval;
event->source_filename = source_filename;
event->source_linenum = source_linenum;
if (parent != NULL) {
event->parent = parent;
event_ref(event->parent);
event_copy_parent_defaults(event, parent);
}
DLLIST_PREPEND(&events, event);
return event;
}
#undef event_create_passthrough
struct event_passthrough *
event_create_passthrough(struct event *parent, const char *source_filename,
unsigned int source_linenum)
{
if (!parent->passthrough) {
if (event_last_passthrough != NULL) {
/* API is being used in a wrong or dangerous way */
i_panic("Can't create multiple passthrough events - finish the earlier with ->event()");
}
struct event *event =
event_create(parent, source_filename, source_linenum);
event->passthrough = TRUE;
/* This event only intends to extend the parent event.
Use the parent's creation timestamp. */
event->tv_created = parent->tv_created;
event_last_passthrough = &event->event_passthrough;
} else {
event_last_passthrough = &parent->event_passthrough;
}
return event_last_passthrough;
}
static bool
event_send_callbacks(struct event *event, enum event_callback_type type,
struct failure_context *ctx, const char *fmt, va_list args)
{
event_callback_t *const *callbackp;
array_foreach(&event_handlers, callbackp) {
bool ret;
T_BEGIN {
ret = (*callbackp)(event, type, ctx, fmt, args);
} T_END;
if (!ret) {
/* event sending was stopped */
return FALSE;
}
}
return TRUE;
}
struct event *event_ref(struct event *event)
{
i_assert(event->refcount > 0);
event->refcount++;
return event;
}
static void event_send_free(struct event *event, ...)
{
va_list args;
/* the args are empty and not used for anything, but there doesn't seem
to be any nice and standard way of passing an initialized va_list
as a parameter without va_start(). */
va_start(args, event);
(void)event_send_callbacks(event, EVENT_CALLBACK_TYPE_FREE,
NULL, NULL, args);
va_end(args);
}
void event_unref(struct event **_event)
{
struct event *event = *_event;
if (event == NULL)
return;
*_event = NULL;
i_assert(event->refcount > 0);
if (--event->refcount > 0)
return;
i_assert(event != current_global_event);
if (event->call_free)
event_send_free(event);
if (last_passthrough_event() == event)
event_last_passthrough = NULL;
if (event->log_prefix_from_system_pool)
i_free(event->log_prefix);
i_free(event->sending_name);
event_unref(&event->parent);
DLLIST_REMOVE(&events, event);
pool_unref(&event->pool);
}
struct event *events_get_head(void)
{
return events;
}
struct event *event_push_global(struct event *event)
{
if (current_global_event != NULL) {
if (!array_is_created(&global_event_stack))
i_array_init(&global_event_stack, 4);
array_append(&global_event_stack, ¤t_global_event, 1);
}
current_global_event = event;
return event;
}
struct event *event_pop_global(struct event *event)
{
i_assert(event != NULL);
i_assert(event == current_global_event);
if (!array_is_created(&global_event_stack) ||
array_count(&global_event_stack) == 0)
current_global_event = NULL;
else {
unsigned int event_count;
struct event *const *events =
array_get(&global_event_stack, &event_count);
i_assert(event_count > 0);
current_global_event = events[event_count-1];
array_delete(&global_event_stack, event_count-1, 1);
}
return current_global_event;
}
struct event *event_get_global(void)
{
return current_global_event;
}
static struct event *
event_set_log_prefix(struct event *event, const char *prefix, bool append)
{
if (event->log_prefix == NULL) {
/* allocate the first log prefix from the pool */
event->log_prefix = p_strdup(event->pool, prefix);
} else {
/* log prefix is being updated multiple times -
switch to system pool so we don't keep leaking memory */
if (event->log_prefix_from_system_pool)
i_free(event->log_prefix);
else
event->log_prefix_from_system_pool = TRUE;
event->log_prefix = i_strdup(prefix);
}
event->log_prefix_replace = !append;
return event;
}
struct event *
event_set_append_log_prefix(struct event *event, const char *prefix)
{
return event_set_log_prefix(event, prefix, TRUE);
}
struct event *event_replace_log_prefix(struct event *event, const char *prefix)
{
return event_set_log_prefix(event, prefix, FALSE);
}
struct event *
event_set_name(struct event *event, const char *name)
{
i_free(event->sending_name);
event->sending_name = i_strdup(name);
return event;
}
struct event *
event_set_source(struct event *event, const char *filename,
unsigned int linenum, bool literal_fname)
{
event->source_filename = literal_fname ? filename :
p_strdup(event->pool, filename);
event->source_linenum = linenum;
return event;
}
struct event *event_set_always_log_source(struct event *event)
{
event->always_log_source = TRUE;
return event;
}
struct event_category *event_category_find_registered(const char *name)
{
struct event_category *const *catp;
array_foreach(&event_registered_categories, catp) {
if (strcmp((*catp)->name, name) == 0)
return *catp;
}
return NULL;
}
struct event_category *const *
event_get_registered_categories(unsigned int *count_r)
{
return array_get(&event_registered_categories, count_r);
}
static void event_category_register(struct event_category *category)
{
event_category_callback_t *const *callbackp;
if (category->registered)
return;
/* register parent categories first */
if (category->parent != NULL)
event_category_register(category->parent);
/* Don't allow duplicate category structs with the same name.
Event filtering uses pointer comparisons for efficiency. */
i_assert(event_category_find_registered(category->name) == NULL);
category->registered = TRUE;
array_append(&event_registered_categories, &category, 1);
array_foreach(&event_category_callbacks, callbackp) T_BEGIN {
(*callbackp)(category);
} T_END;
}
static bool
event_find_category(struct event *event, const struct event_category *category)
{
struct event_category *const *categoryp;
array_foreach(&event->categories, categoryp) {
if (*categoryp == category)
return TRUE;
}
return FALSE;
}
struct event *
event_add_categories(struct event *event,
struct event_category *const *categories)
{
if (!array_is_created(&event->categories))
p_array_init(&event->categories, event->pool, 4);
for (unsigned int i = 0; categories[i] != NULL; i++) {
event_category_register(categories[i]);
if (!event_find_category(event, categories[i]))
array_append(&event->categories, &categories[i], 1);
}
return event;
}
struct event *
event_add_category(struct event *event, struct event_category *category)
{
struct event_category *const categories[] = { category, NULL };
return event_add_categories(event, categories);
}
static struct event_field *
event_find_field_int(struct event *event, const char *key)
{
struct event_field *field;
if (!array_is_created(&event->fields))
return NULL;
array_foreach_modifiable(&event->fields, field) {
if (strcmp(field->key, key) == 0)
return field;
}
return NULL;
}
const struct event_field *
event_find_field(struct event *event, const char *key)
{
const struct event_field *field = event_find_field_int(event, key);
if (field != NULL || event->parent == NULL)
return field;
return event_find_field(event->parent, key);
}
const char *
event_find_field_str(struct event *event, const char *key)
{
const struct event_field *field;
field = event_find_field(event, key);
if (field == NULL)
return NULL;
switch (field->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
return field->value.str;
case EVENT_FIELD_VALUE_TYPE_INTMAX:
return dec2str(field->value.intmax);
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
return t_strdup_printf("%"PRIdTIME_T".%u",
field->value.timeval.tv_sec,
(unsigned int)field->value.timeval.tv_usec);
}
i_unreached();
}
static struct event_field *
event_get_field(struct event *event, const char *key)
{
struct event_field *field;
field = event_find_field_int(event, key);
if (field == NULL) {
if (!array_is_created(&event->fields))
p_array_init(&event->fields, event->pool, 8);
field = array_append_space(&event->fields);
field->key = p_strdup(event->pool, key);
}
return field;
}
struct event *
event_add_str(struct event *event, const char *key, const char *value)
{
struct event_field *field;
if (value == NULL) {
/* silently ignoring is perhaps better than assert-crashing? */
return event;
}
field = event_get_field(event, key);
field->value_type = EVENT_FIELD_VALUE_TYPE_STR;
i_zero(&field->value);
field->value.str = p_strdup(event->pool, value);
return event;
}
struct event *
event_add_int(struct event *event, const char *key, intmax_t num)
{
struct event_field *field;
field = event_get_field(event, key);
field->value_type = EVENT_FIELD_VALUE_TYPE_INTMAX;
i_zero(&field->value);
field->value.intmax = num;
return event;
}
struct event *
event_inc_int(struct event *event, const char *key, intmax_t num)
{
struct event_field *field;
field = event_find_field_int(event, key);
if (field == NULL || field->value_type != EVENT_FIELD_VALUE_TYPE_INTMAX)
return event_add_int(event, key, num);
field->value.intmax += num;
return event;
}
struct event *
event_add_timeval(struct event *event, const char *key,
const struct timeval *tv)
{
struct event_field *field;
field = event_get_field(event, key);
field->value_type = EVENT_FIELD_VALUE_TYPE_TIMEVAL;
i_zero(&field->value);
field->value.timeval = *tv;
return event;
}
struct event *
event_add_fields(struct event *event,
const struct event_add_field *fields)
{
for (unsigned int i = 0; fields[i].key != NULL; i++) {
if (fields[i].value != NULL)
event_add_str(event, fields[i].key, fields[i].value);
else if (fields[i].value_timeval.tv_sec != 0)
event_add_timeval(event, fields[i].key, &fields[i].value_timeval);
else
event_add_int(event, fields[i].key, fields[i].value_intmax);
}
return event;
}
void event_field_clear(struct event *event, const char *key)
{
event_add_str(event, key, "");
}
struct event *event_get_parent(struct event *event)
{
return event->parent;
}
void event_get_create_time(struct event *event, struct timeval *tv_r)
{
*tv_r = event->tv_created;
}
bool event_get_last_send_time(struct event *event, struct timeval *tv_r)
{
*tv_r = event->tv_last_sent;
return tv_r->tv_sec != 0;
}
const struct event_field *
event_get_fields(struct event *event, unsigned int *count_r)
{
if (!array_is_created(&event->fields)) {
*count_r = 0;
return NULL;
}
return array_get(&event->fields, count_r);
}
struct event_category *const *
event_get_categories(struct event *event, unsigned int *count_r)
{
if (!array_is_created(&event->categories)) {
*count_r = 0;
return NULL;
}
return array_get(&event->categories, count_r);
}
void event_send(struct event *event, struct failure_context *ctx,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
event_vsend(event, ctx, fmt, args);
va_end(args);
}
void event_vsend(struct event *event, struct failure_context *ctx,
const char *fmt, va_list args)
{
event->tv_last_sent = ioloop_timeval;
if (event_send_callbacks(event, EVENT_CALLBACK_TYPE_EVENT,
ctx, fmt, args)) {
if (ctx->type != LOG_TYPE_DEBUG ||
event->sending_debug_log)
i_log_typev(ctx, fmt, args);
}
event_send_abort(event);
}
void event_send_abort(struct event *event)
{
/* if the event is sent again, it needs a new name */
i_free(event->sending_name);
if (event->passthrough)
event_unref(&event);
}
static void
event_export_field_value(string_t *dest, const struct event_field *field)
{
switch (field->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
str_append_c(dest, EVENT_CODE_FIELD_STR);
str_append_tabescaped(dest, field->key);
str_append_c(dest, '\t');
str_append_tabescaped(dest, field->value.str);
break;
case EVENT_FIELD_VALUE_TYPE_INTMAX:
str_append_c(dest, EVENT_CODE_FIELD_INTMAX);
str_append_tabescaped(dest, field->key);
str_printfa(dest, "\t%jd", field->value.intmax);
break;
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
str_append_c(dest, EVENT_CODE_FIELD_TIMEVAL);
str_append_tabescaped(dest, field->key);
str_printfa(dest, "\t%"PRIdTIME_T"\t%u",
field->value.timeval.tv_sec,
(unsigned int)field->value.timeval.tv_usec);
break;
}
}
void event_export(const struct event *event, string_t *dest)
{
/* required fields: */
str_printfa(dest, "%"PRIdTIME_T"\t%u",
event->tv_created.tv_sec,
(unsigned int)event->tv_created.tv_usec);
/* optional fields: */
if (event->source_filename != NULL) {
str_append_c(dest, '\t');
str_append_c(dest, EVENT_CODE_SOURCE);
str_append_tabescaped(dest, event->source_filename);
str_printfa(dest, "\t%u", event->source_linenum);
}
if (event->always_log_source) {
str_append_c(dest, '\t');
str_append_c(dest, EVENT_CODE_ALWAYS_LOG_SOURCE);
}
if (event->tv_last_sent.tv_sec != 0) {
str_printfa(dest, "\t%c%"PRIdTIME_T"\t%u",
EVENT_CODE_TV_LAST_SENT,
event->tv_last_sent.tv_sec,
(unsigned int)event->tv_last_sent.tv_usec);
}
if (event->sending_name != NULL) {
str_append_c(dest, '\t');
str_append_c(dest, EVENT_CODE_SENDING_NAME);
str_append_tabescaped(dest, event->sending_name);
}
if (array_is_created(&event->categories)) {
struct event_category *const *catp;
array_foreach(&event->categories, catp) {
str_append_c(dest, '\t');
str_append_c(dest, EVENT_CODE_CATEGORY);
str_append_tabescaped(dest, (*catp)->name);
}
}
if (array_is_created(&event->fields)) {
const struct event_field *field;
array_foreach(&event->fields, field) {
str_append_c(dest, '\t');
event_export_field_value(dest, field);
}
}
}
bool event_import(struct event *event, const char *str, const char **error_r)
{
return event_import_unescaped(event, t_strsplit_tabescaped(str), error_r);
}
static bool event_import_tv(const char *arg_secs, const char *arg_usecs,
struct timeval *tv_r, const char **error_r)
{
unsigned int usecs;
if (str_to_time(arg_secs, &tv_r->tv_sec) < 0) {
*error_r = "Invalid timeval seconds parameter";
return FALSE;
}
if (arg_usecs == NULL) {
*error_r = "Timeval missing microseconds parameter";
return FALSE;
}
if (str_to_uint(arg_usecs, &usecs) < 0 || usecs >= 1000000) {
*error_r = "Invalid timeval microseconds parameter";
return FALSE;
}
tv_r->tv_usec = usecs;
return TRUE;
}
bool event_import_unescaped(struct event *event, const char *const *args,
const char **error_r)
{
const char *error;
/* required fields: */
if (args[0] == NULL) {
*error_r = "Missing required fields";
return FALSE;
}
if (!event_import_tv(args[0], args[1], &event->tv_created, &error)) {
*error_r = t_strdup_printf("Invalid tv_created: %s", error);
return FALSE;
}
args += 2;
/* optional fields: */
while (*args != NULL) {
const char *arg = *args;
enum event_code code = arg[0];
arg++;
switch (code) {
case EVENT_CODE_ALWAYS_LOG_SOURCE:
event->always_log_source = TRUE;
break;
case EVENT_CODE_CATEGORY: {
struct event_category *category =
event_category_find_registered(arg);
if (category == NULL) {
*error_r = t_strdup_printf("Unregistered category: '%s'", arg);
return FALSE;
}
if (!array_is_created(&event->categories))
p_array_init(&event->categories, event->pool, 4);
array_append(&event->categories, &category, 1);
break;
}
case EVENT_CODE_TV_LAST_SENT:
if (!event_import_tv(arg, args[1], &event->tv_last_sent, &error)) {
*error_r = t_strdup_printf("Invalid tv_last_sent: %s", error);
return FALSE;
}
args++;
break;
case EVENT_CODE_SENDING_NAME:
i_free(event->sending_name);
event->sending_name = i_strdup(arg);
break;
case EVENT_CODE_SOURCE:
event->source_filename = p_strdup(event->pool, arg);
if (args[1] == NULL) {
*error_r = "Source line number missing";
return FALSE;
}
if (str_to_uint(args[1], &event->source_linenum) < 0) {
*error_r = "Invalid Source line number";
return FALSE;
}
args++;
break;
case EVENT_CODE_FIELD_INTMAX:
case EVENT_CODE_FIELD_STR:
case EVENT_CODE_FIELD_TIMEVAL: {
struct event_field *field =
event_get_field(event, arg);
if (args[1] == NULL) {
*error_r = "Field value is missing";
return FALSE;
}
args++;
i_zero(&field->value);
switch (code) {
case EVENT_CODE_FIELD_INTMAX:
field->value_type = EVENT_FIELD_VALUE_TYPE_INTMAX;
if (str_to_intmax(*args, &field->value.intmax) < 0) {
*error_r = t_strdup_printf(
"Invalid field value '%s' number for '%s'",
*args, field->key);
return FALSE;
}
break;
case EVENT_CODE_FIELD_STR:
field->value_type = EVENT_FIELD_VALUE_TYPE_STR;
field->value.str = p_strdup(event->pool, *args);
break;
case EVENT_CODE_FIELD_TIMEVAL:
field->value_type = EVENT_FIELD_VALUE_TYPE_TIMEVAL;
if (!event_import_tv(args[0], args[1],
&field->value.timeval, &error)) {
*error_r = t_strdup_printf(
"Field '%s' value '%s': %s",
field->key, args[1], error);
return FALSE;
}
args++;
break;
default:
i_unreached();
}
break;
}
}
args++;
}
return TRUE;
}
void event_register_callback(event_callback_t *callback)
{
array_append(&event_handlers, &callback, 1);
}
void event_unregister_callback(event_callback_t *callback)
{
event_callback_t *const *callbackp;
array_foreach(&event_handlers, callbackp) {
if (*callbackp == callback) {
array_delete(&event_handlers,
array_foreach_idx(&event_handlers, callbackp), 1);
return;
}
}
i_unreached();
}
void event_category_register_callback(event_category_callback_t *callback)
{
array_append(&event_category_callbacks, &callback, 1);
}
void event_category_unregister_callback(event_category_callback_t *callback)
{
event_category_callback_t *const *callbackp;
array_foreach(&event_category_callbacks, callbackp) {
if (*callbackp == callback) {
array_delete(&event_category_callbacks,
array_foreach_idx(&event_category_callbacks, callbackp), 1);
return;
}
}
i_unreached();
}
static void event_category_remove_from_array(struct event_category *category)
{
struct event_category *const *catp;
array_foreach(&event_registered_categories, catp) {
if (*catp == category) {
array_delete(&event_registered_categories,
array_foreach_idx(&event_registered_categories, catp), 1);
return;
}
}
i_unreached();
}
void event_category_unregister(struct event_category *category)
{
event_category_callback_t *const *callbackp;
if (!category->registered) {
/* it was never registered in the first place - ignore */
return;
}
category->registered = FALSE;
event_category_remove_from_array(category);
array_foreach(&event_category_callbacks, callbackp) T_BEGIN {
(*callbackp)(category);
} T_END;
}
static struct event_passthrough *
event_passthrough_set_append_log_prefix(const char *prefix)
{
event_set_append_log_prefix(last_passthrough_event(), prefix);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_replace_log_prefix(const char *prefix)
{
event_replace_log_prefix(last_passthrough_event(), prefix);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_set_name(const char *name)
{
event_set_name(last_passthrough_event(), name);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_set_source(const char *filename,
unsigned int linenum, bool literal_fname)
{
event_set_source(last_passthrough_event(), filename,
linenum, literal_fname);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_set_always_log_source(void)
{
event_set_always_log_source(last_passthrough_event());
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_add_categories(struct event_category *const *categories)
{
event_add_categories(last_passthrough_event(), categories);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_add_category(struct event_category *category)
{
event_add_category(last_passthrough_event(), category);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_add_fields(const struct event_add_field *fields)
{
event_add_fields(last_passthrough_event(), fields);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_add_str(const char *key, const char *value)
{
event_add_str(last_passthrough_event(), key, value);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_add_int(const char *key, intmax_t num)
{
event_add_int(last_passthrough_event(), key, num);
return event_last_passthrough;
}
static struct event_passthrough *
event_passthrough_add_timeval(const char *key, const struct timeval *tv)
{
event_add_timeval(last_passthrough_event(), key, tv);
return event_last_passthrough;
}
static struct event *event_passthrough_event(void)
{
struct event *event = last_passthrough_event();
event_last_passthrough = NULL;
return event;
}
const struct event_passthrough event_passthrough_vfuncs = {
event_passthrough_set_append_log_prefix,
event_passthrough_replace_log_prefix,
event_passthrough_set_name,
event_passthrough_set_source,
event_passthrough_set_always_log_source,
event_passthrough_add_categories,
event_passthrough_add_category,
event_passthrough_add_fields,
event_passthrough_add_str,
event_passthrough_add_int,
event_passthrough_add_timeval,
event_passthrough_event,
};
void lib_event_init(void)
{
i_array_init(&event_handlers, 4);
i_array_init(&event_category_callbacks, 4);
i_array_init(&event_registered_categories, 16);
}
void lib_event_deinit(void)
{
event_unset_global_debug_log_filter();
event_unset_global_debug_send_filter();
event_unset_global_core_log_filter();
for (struct event *event = events; event != NULL; event = event->next) {
i_warning("Event %p leaked (parent=%p): %s:%u",
event, event->parent,
event->source_filename, event->source_linenum);
}
array_free(&event_handlers);