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
|
/*
ELAPI
Module contains functions to resolve the event.
Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <errno.h> /* for errors */
#include <string.h> /* for strcmp() */
#include "elapi_priv.h"
#include "elapi_event.h"
#include "elapi_basic.h"
#include "trace.h"
#include "config.h"
/*****************************************/
/* Individual callbacks are defined here */
/*****************************************/
/* Timestamp resolution callback */
static int elapi_timestamp_cb(struct elapi_resolve_data *resolver,
struct collection_item *item,
int *skip)
{
int error = EOK;
char timestamp[TIME_ARRAY_SIZE + 1];
int length;
TRACE_FLOW_STRING("elapi_timestamp_cb", "Entry");
/* Construct the time stamp */
length = strftime(timestamp,
TIME_ARRAY_SIZE,
(const char *)(col_get_item_data(item)),
&(resolver->local_time));
/* Update the time stamp item */
error = col_modify_str_item(item,
NULL,
timestamp,
length + 1);
TRACE_FLOW_NUMBER("elapi_timestamp_cb. Exit. Returning", error);
return error;
}
/* UTC time resolution callback */
static int elapi_utctime_cb(struct elapi_resolve_data *resolver,
struct collection_item *item,
int *skip)
{
int error = EOK;
TRACE_FLOW_STRING("elapi_utctime_cb", "Entry");
/* Update the UTC item */
error = col_modify_int_item(item,
NULL,
(int)(resolver->tm));
TRACE_FLOW_NUMBER("elapi_utctime_cb. Exit. Returning", error);
return error;
}
/* Offset resolution callback */
static int elapi_offset_cb(struct elapi_resolve_data *resolver,
struct collection_item *item,
int *skip)
{
int error = EOK;
TRACE_FLOW_STRING("elapi_offset_cb", "Entry");
/* Update the offset item */
error = col_modify_int_item(item,
NULL,
(int)(resolver->offset));
TRACE_FLOW_NUMBER("elapi_offset_cb. Exit. Returning", error);
return error;
}
/* Message resolution callback */
static int elapi_message_cb(struct elapi_resolve_data *resolver,
struct collection_item *item,
int *skip)
{
TRACE_FLOW_STRING("elapi_message_cb", "Entry");
/* Save pointer to message item */
resolver->message = item;
TRACE_FLOW_NUMBER("elapi_message_cb.", "Exit");
return EOK;
}
/*****************************************/
/* Array of structures for resolution of
* the different event properties.
*/
struct elapi_resolve_list elapi_known_fields[] = {
{ E_TIMESTAMP, { COL_TYPE_STRING, elapi_timestamp_cb }},
{ E_UTCTIME, { COL_TYPE_INTEGER, elapi_utctime_cb }},
{ E_OFFSET, { COL_TYPE_INTEGER, elapi_offset_cb }},
{ E_MESSAGE, { COL_TYPE_STRING, elapi_message_cb }},
/* ADD NEW CALLBACKS HERE */
{ NULL, { COL_TYPE_ANY, NULL }}
};
/*****************************************/
/* A callback function to do substitutions
* of different properties as we copy the event.
*/
static int elapi_resolve_item(struct collection_item *item,
void *ext_data,
int *skip)
{
int error = EOK;
struct elapi_resolve_data *resolver;
struct collection_item *res_item;
struct elapi_rslv_item_data *rslv_pair;
int res;
TRACE_FLOW_STRING("elapi_resolve_item", "Entry");
/* Do we care about this field ? */
if (strncmp(col_get_item_property(item, NULL),
E_PREFIX,
E_PREFIX_LEN) != 0) {
TRACE_FLOW_STRING("elapi_resolve_item. Skipping resoltion.", "Exit");
return EOK;
}
TRACE_FLOW_STRING("Item to resolve: ", col_get_item_property(item, NULL));
/* This is an internal field that might need resolution */
resolver = (struct elapi_resolve_data *)ext_data;
/* NOTE: This iteration loop uses advanced iterator
* capabilities. Read more about it before you decide
* to use this code as an example.
*/
while (1) {
/* Advance to next item in the list */
error = col_iterate_collection(resolver->handle->resolve_list,
&res_item);
if (error) {
TRACE_ERROR_NUMBER("Failed to iterate collection", error);
return error;
}
/* Are we done ? This means we looped and did not find
* the item. */
if (res_item == NULL) break;
/* Compare items */
res = col_compare_items(item,
res_item,
COL_CMPIN_PROP_EQU,
NULL);
if (res == 0) {
/* Item names are the same, so drill down and get expected type. */
rslv_pair = *((struct elapi_rslv_item_data **)col_get_item_data(res_item));
/* Make sure that types matched too */
if (rslv_pair->type == col_get_item_type(item)) {
/* This is the item we need to resolve so resolve */
error = rslv_pair->resolve_cb(resolver,
item,
skip);
if (error) {
TRACE_ERROR_NUMBER("Failed to resolve item", error);
return error;
}
/* Pin down the iterator here */
col_pin_iterator(resolver->handle->resolve_list);
/* Break out of loop */
break;
}
}
}
TRACE_FLOW_STRING("elapi_resolve_item", "Exit");
return error;
}
/* Message resolution function */
int elapi_resolve_message(struct collection_item *item,
struct collection_item *event)
{
int error = EOK;
struct elapi_data_out *serialized;
TRACE_FLOW_STRING("elapi_resolve_message", "Entry");
/* I prefer to allocate it rather than do it on stack.
* The main reason is that I would have to memset
* the struct to init it.
* So why not just use the interface we already have.
*/
error = elapi_alloc_serialized_data(&serialized);
if (error) {
TRACE_ERROR_NUMBER("Failed to allocate serialized data", error);
return error;
}
/* Call string substitution */
error = elapi_sprintf(serialized,
(const char *)col_get_item_data(item),
event);
if (error) {
TRACE_ERROR_NUMBER("Failed to build message", error);
elapi_free_serialized_data(serialized);
return error;
}
/* Put the resolved value into the item */
error = col_modify_str_item(item,
NULL,
(char *)serialized->buffer,
serialized->length + 1);
/* Clean data regardless of the result */
elapi_free_serialized_data(serialized);
if (error) {
TRACE_ERROR_NUMBER("Failed to modify message item", error);
return error;
}
TRACE_FLOW_NUMBER("elapi_resolve_message. Exit. Returning", error);
return error;
}
/* Resolve event */
int elapi_resolve_event(struct collection_item **final_event,
struct collection_item *event,
struct elapi_dispatcher *handle)
{
int error = EOK;
struct elapi_resolve_data resolver;
struct collection_item *new_event;
time_t local;
time_t utc;
TRACE_FLOW_STRING("elapi_create_event_ctx", "Entry");
/* Prepare the resolver */
resolver.message = NULL;
resolver.handle = handle;
/* Get seconds */
resolver.tm = time(NULL);
/* Convert to local and UTC structured time */
localtime_r(&resolver.tm, &(resolver.local_time));
gmtime_r(&resolver.tm, &(resolver.utc_time));
/* Convert back */
utc = mktime(&(resolver.utc_time));
local = mktime(&(resolver.local_time));
/* Get offset - it is safe to typecast to int here */
resolver.offset = (int)(difftime(local, utc));
/* NOTE: We will use FLATDOT mode.
* We will see what people have to say
* about this approach...
*/
error = col_copy_collection_with_cb(&new_event,
event,
NULL,
COL_COPY_FLATDOT,
elapi_resolve_item,
(void *)&resolver);
if (error) {
TRACE_ERROR_NUMBER("Failed to resolve the event", error);
return error;
}
if (resolver.message) {
/* Now resolve message. We need to do it last since
* we do not know the order of the properties
* and message can be referencing properties
* that are later than message in the list
* and have not been resolved yet.
*/
error = elapi_resolve_message(resolver.message,
new_event);
if (error) {
TRACE_ERROR_NUMBER("Failed to resolve the event", error);
col_destroy_collection(new_event);
return error;
}
}
*final_event = new_event;
TRACE_FLOW_STRING("elapi_create_event_ctx", "Exit");
return error;
}
/* Function to initialize resolution list */
int elapi_init_resolve_list(struct collection_iterator **list)
{
int error = EOK;
struct elapi_resolve_list *current;
struct collection_item *col = NULL;
struct collection_iterator *iterator = NULL;
struct elapi_rslv_item_data *bin_data;
TRACE_FLOW_STRING("elapi_init_resolve_list", "Entry");
/* Create collection of fields that we know how to process */
error = col_create_collection(&col,
ELAPI_RESOLVE_ITEM,
COL_CLASS_ELAPI_RES_ITEM);
if (error) {
TRACE_ERROR_NUMBER("Failed to create collection", error);
return error;
}
/* Loop through the static array and turn it into a collection */
current = elapi_known_fields;
while (current->name) {
bin_data = &(current->resolve_item);
error = col_add_binary_property(col,
NULL,
current->name,
(void *)&bin_data,
sizeof(struct elapi_rslv_item_data *));
if (error) {
TRACE_ERROR_NUMBER("Failed to add item resolver", error);
col_destroy_collection(col);
return error;
}
current++;
}
/* Now bind iterator */
error = col_bind_iterator(&iterator, col, COL_TRAVERSE_FLAT);
if (error) {
TRACE_ERROR_NUMBER("Failed to bind collection", error);
col_destroy_collection(col);
return error;
}
/* We do not need the collection itself - we have iterator */
col_destroy_collection(col);
*list = iterator;
TRACE_FLOW_STRING("elapi_init_resolve_list", "Exit");
return error;
}
|