summaryrefslogtreecommitdiffstats
path: root/common/elapi/elapi_event.c
blob: 9322f8217a7665d3f4adab433f07416b5d5d20f2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
/*
    ELAPI

    Implementation of the ELAPI event interface.

    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 <sys/types.h>  /* for getpid() */
#include <unistd.h>     /* for getpid() */
#include <stdlib.h>     /* for realloc() */
#include <syslog.h>     /* for contants releted to severity */
#include <unistd.h>     /* for gethostname() */
#include <errno.h>      /* for errors */
#include <string.h>     /* for memset() and other */
#include <netdb.h>      /* for gethostbyname() */
#include <sys/socket.h> /* for inet_ntop() */
#include <arpa/inet.h>  /* for inet_ntop() */
#include <ctype.h>      /* for isspace() */
#include <stdarg.h>     /* for va_arg() */
#include <string.h>     /* for strndup() */
#include <ifaddrs.h>    /* for getifaddrs() */

#include "elapi_priv.h"
#include "elapi_event.h"
#include "trace.h"
#include "config.h"

#include "collection_tools.h"

/* Internal return states from key processing */
#define E_LIST_EMPTY       0
#define E_LIST_ERROR       1
#define E_LIST_LAST        2
#define E_LIST_ADD         3
#define E_LIST_REMOVE      4

#define LOCALHOSTDOMAIN "localhost.localdomain"
#define LOCALHOST       "localhost"
#define LOCALADDRESS    "127.0.0.1"
#define LOCALADDRESSV6  "::1"

const char *undefined = "undefined";
const char *str_yes = "yes";
const char *str_no = "no";
const char *str_true = "true";
const char *str_false = "false";

/* Default event template */
struct collection_item *default_template = NULL;


/* Function to add host identity information to the template */
int add_host_identity(struct collection_item *tpl, unsigned base)
{
    char hostname[NI_MAXHOST + 1];
    int error = EOK;
    int gai_ret_host = 0;
    int gai_ret_addr = 0;
    char host[NI_MAXHOST];
    char address[NI_MAXHOST];
    char *hnm, *haddr;
    struct ifaddrs *ifaddr, *ifa;
    int family;
    int set_hostname = 0;
    int set_ip = 0;

    TRACE_FLOW_STRING("add_host_identity", "Entry");

    memset(hostname, 0, sizeof(hostname));

    /* The goal here to collect information about the host.
     * there is no need to actually use it for establishing
     * any connections.
     * It is a best effort attempt.
     */

    /* If we are not asked for hostname then say we already have it */
    if (!(base & E_HAVE_HOSTNAME)) set_hostname = 1;
    /* If we are not asked for ip then say we already have it */
    if (!(base & E_HAVE_HOSTIP)) set_ip = 1;

    if (getifaddrs(&ifaddr) == 0) {

        TRACE_FLOW_STRING("getifaddrs", "Ok");

        /* Walk through linked list, maintaining head pointer so we
            can free list later */

        ifa = ifaddr;
        while (ifa != NULL) {

            TRACE_FLOW_STRING("Top of the loop", "");

            if (!ifa->ifa_addr) {
                ifa = ifa->ifa_next;
                continue;
            }

            family = ifa->ifa_addr->sa_family;

            TRACE_FLOW_NUMBER("Family", family);

            /* For an AF_INET* interface address, display the address */
            if (family == AF_INET || family == AF_INET6) {

                TRACE_FLOW_NUMBER("Got right family", family);
                /* Make sure the address is cleared - will help in comparisons */
                memset(host, 0, sizeof(host));
                memset(address, 0, sizeof(address));

                gai_ret_host = getnameinfo(ifa->ifa_addr,
                                           (family == AF_INET) ? sizeof(struct sockaddr_in) :
                                                                 sizeof(struct sockaddr_in6),
                                           host,
                                           NI_MAXHOST,
                                           NULL,
                                           0,
                                           0 /* Gets host name */);

                gai_ret_addr = getnameinfo(ifa->ifa_addr,
                                           (family == AF_INET) ? sizeof(struct sockaddr_in) :
                                                                 sizeof(struct sockaddr_in6),
                                           address,
                                           NI_MAXHOST,
                                           NULL,
                                           0,
                                           NI_NUMERICHOST /* Gets address as string */);

                /* If we have not set host name set it */
                if(!set_hostname) {

                    TRACE_FLOW_STRING("Host name is not set", "");

                    hnm = NULL;
                    /* Use host name returned by gethostname() as main host name */
                    if (!gethostname(hostname, NI_MAXHOST)) hnm = hostname;
                    else {
                        /* We we able to get a host name ? */
                        if (gai_ret_host == 0) {
                            TRACE_INFO_STRING("getnameinfo returned:", host);
                            hnm = host;
                        }
                    }

                    /* Do we have a host meaningful host name? */
                    if ((hnm) &&
                        ((strncasecmp(hnm, LOCALHOST, sizeof(LOCALHOST)) == 0 ) ||
                         (strncasecmp(hnm, LOCALHOSTDOMAIN, sizeof(LOCALHOSTDOMAIN)) == 0 ) ||
                         (strncasecmp(hnm, address, sizeof(address) == 0)))) hnm = NULL;

                    /* If host name is not NULL it would work for us */
                    if (hnm) {
                        TRACE_INFO_STRING("Adding host name:", hnm);
                        error = col_add_str_property(tpl, NULL, E_HOSTNAME, hnm, 0);
                        if (error) {
                            TRACE_ERROR_NUMBER("Failed to add host name. Error", error);
                            freeifaddrs(ifaddr);
                            return error;
                        }
                        /* Done with the name */
                        set_hostname = 1;
                    }
                }

                /* If we have not set processed ip address do it */
                if(!set_ip) {

                    TRACE_FLOW_STRING("Address is not set", "");

                    haddr = NULL;
                    if (gai_ret_addr == 0) {
                        TRACE_INFO_STRING("getnameinfo returned:", address);
                        if ((strncasecmp(address, LOCALADDRESS, sizeof(LOCALADDRESS)) != 0 ) &&
                            (strncasecmp(address, LOCALADDRESSV6, sizeof(LOCALADDRESSV6)) != 0 )) {
                            TRACE_INFO_STRING("Not an unhelpful address", "");
                            haddr = address;
                        }
                    }

                    if (haddr) {
                        TRACE_INFO_STRING("Adding host address:", haddr);
                        error = col_add_str_property(tpl, NULL, E_HOSTIP, haddr, 0);
                        if (error) {
                            TRACE_ERROR_NUMBER("Failed to add host name. Error", error);
                            freeifaddrs(ifaddr);
                            return error;
                        }
                        set_ip = 1;
                    }
                }

                /* If we have a name and we are told to deal with alias names */
                if ((set_hostname) && (base & E_HAVE_HOSTALIAS)) {

                    TRACE_INFO_NUMBER("gai_ret_host:", gai_ret_host);
                    TRACE_INFO_STRING("host:", host);
                    TRACE_INFO_STRING("address:", address);
                    TRACE_INFO_STRING("they are:", strncasecmp(host, address, sizeof(address)) == 0 ? "same" : "different");

                    /* Do we have a host meaningful host name? */
                    if ((gai_ret_host != 0) ||
                        ((gai_ret_host == 0) &&
                         ((strncasecmp(host, LOCALHOST, sizeof(LOCALHOST)) == 0 ) ||
                          (strncasecmp(host, LOCALHOSTDOMAIN, sizeof(LOCALHOSTDOMAIN)) == 0 ) ||
                          (strncasecmp(host, address, sizeof(address)) == 0)))) hnm = NULL;
                    else hnm = host;

                    if (hnm) {
                        TRACE_INFO_STRING("Adding alias host name:", hnm);
                        error = col_add_str_property(tpl, NULL, E_HOSTALIAS, hnm, 0);
                        if (error) {
                            TRACE_ERROR_NUMBER("Failed to add host name. Error", error);
                            freeifaddrs(ifaddr);
                            return error;
                        }
                    }
                }

                /* If we got then main IP and we are told to deal with opther IPs */
                if ((set_ip) && (base & E_HAVE_HOSTIPS)) {

                    /* Do we have a host meaningful host name? */
                    if ((gai_ret_addr != 0) ||
                        ((gai_ret_addr == 0) &&
                         ((strncasecmp(address, LOCALADDRESS, sizeof(LOCALADDRESS)) == 0 ) ||
                          (strncasecmp(address, LOCALADDRESSV6, sizeof(LOCALADDRESSV6)) == 0 )))) haddr = address;
                    else haddr = address;

                    if (haddr) {
                        TRACE_INFO_STRING("Adding alias host IP:", haddr);
                        error = col_add_str_property(tpl, NULL, E_HOSTIPS, haddr, 0);
                        if (error) {
                            TRACE_ERROR_NUMBER("Failed to add host name. Error", error);
                            freeifaddrs(ifaddr);
                            return error;
                        }
                    }
                }
            }
            TRACE_INFO_NUMBER("Moving to next", ifa->ifa_next);
            ifa = ifa->ifa_next;
            TRACE_INFO_NUMBER("Moved to", ifa);
        }

        freeifaddrs(ifaddr);
    }

    /* Make sure that we really have the name after all */
    if (!set_hostname) {
        TRACE_INFO_STRING("No host name using default:", undefined);
        error = col_add_str_property(tpl, NULL, E_HOSTNAME, undefined, 0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add host name. Error", error);
            return error;
        }
    }

    /* Make sure that we really have the IP after all */
    if (!set_ip) {
        TRACE_INFO_STRING("No host name using default:", undefined);
        error = col_add_str_property(tpl, NULL, E_HOSTIP, undefined, 0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add host name. Error", error);
            return error;
        }
    }

    TRACE_FLOW_STRING("add_host_identity", "Exit");
    return error;
}

/* Add base elements to template collection */
static int add_base_elements(struct collection_item *tpl, unsigned base)
{
    int error = EOK;
    int pass_base;

    TRACE_FLOW_STRING("add_base_elements", "Entry");

    /* Populate the template using base */
    if (base & E_HAVE_TIMESTAMP) {
        /* Value is the format string for strftime() */
        error = col_add_str_property(tpl, NULL, E_TIMESTAMP, E_TIMESTAMP_FORMAT, sizeof(E_TIMESTAMP_FORMAT));
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add timestamp. Error", error);
            return error;
        }
    }

    if (base & E_HAVE_UTCTIME) {
        /* Value does not matter */
        error = col_add_int_property(tpl, NULL, E_UTCTIME, 0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add UTC time. Error", error);
            return error;
        }
    }

    if (base & E_HAVE_PID) {
        /* Value is the current pid */
        error = col_add_long_property(tpl, NULL, E_PID, (long)getpid());
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add pid. Error", error);
            return error;
        }
    }

    if (base & E_HAVE_APPNAME) {
        /* Value does not matter */
        error = col_add_str_property(tpl, NULL, E_APPNAME, "", 1);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add application name. Error", error);
            return error;
        }
    }

    if (base & E_HAVE_SEVERITY) {
        /* Value is the default severity */
        error = col_add_int_property(tpl, NULL, E_SEVERITY, LOG_USER | LOG_INFO);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add pid. Error", error);
            return error;
        }
    }

    /* If we need to add aliases or other IPs call the function */
    if ((base & E_HAVE_HOSTNAME) ||
        (base & E_HAVE_HOSTIP) ||
        (base & E_HAVE_HOSTALIAS) ||
        (base & E_HAVE_HOSTIPS))  {

        pass_base = base;

        /* make sure we have extensions on top of the basic data */
        if ((base & E_HAVE_HOSTALIAS) && (!(base & E_HAVE_HOSTNAME))) pass_base |= E_HAVE_HOSTNAME;
        if ((base & E_HAVE_HOSTIPS) && (!(base & E_HAVE_HOSTIP))) pass_base |= E_HAVE_HOSTIP;

        error = add_host_identity(tpl, pass_base);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add host identity. Error", error);
            return error;
        }
    }

    TRACE_FLOW_STRING("add_base_elements", "Exit");
    return error;
}


/* Internal untility function to tokenize a string */
static int interprete_key(char *key,
                          int *type,
                          char **property,
                          int *prop_len,
                          int *has_len,
                          int *bool_type)
{
    int adjust_by = 0;
    char *start = NULL;
    char *cursor = NULL;
    char *end = NULL;
    int ret = E_LIST_EMPTY;

    TRACE_FLOW_STRING("interprete_key", "Entry");

    TRACE_INFO_STRING("Key", key);

    /* Initialize passed in data */
    *has_len = 0;
    *property = NULL;
    *type = COL_TYPE_STRING;

    cursor = key;

    while (isspace(*cursor)) cursor++;

    /* End of string - we are done */
    if (*cursor == '\0') {
        TRACE_ERROR_STRING("Empty key - end of processing!", "");
        return E_LIST_EMPTY;
    }

    /* This is the beginning of the formatted token */
    if (*cursor == '-') {

        /* This is a remove attribute case */

        cursor++;
        /* Skip spaces if any */
        while (isspace(*cursor)) cursor++;

        /* Mark the start of the actual property */
        start = cursor;

        /* Now we need to extract the name of the property */
        /* We will not be nice here - the add_property will validate if the name is ok */
        while ((*cursor != '\0') && (!isspace(*cursor))) cursor++;

        /* End of string - we are done */
        if (cursor == start) {
            TRACE_ERROR_STRING("Invalid key - end of processing!", "");
            return E_LIST_EMPTY;
        }

        *prop_len = cursor - start;
        *property = start;
        TRACE_INFO_STRING("We are told to remove the property!", *property);
        ret = E_LIST_REMOVE;
    }
    else if (*cursor == '%') {

        /* We got a full key with format string */

        cursor++;
        if ((*cursor == '*') && (*(cursor+1) == 's') && (*(cursor+2) == '(')) {
            *type = COL_TYPE_STRING;
            *has_len = 1;
            adjust_by = 3;
        }
        else if ((*cursor == 's') && (*(cursor+1) == '(')) {
            *type = COL_TYPE_STRING;
            adjust_by = 2;
        }
        else if (((*cursor == 'i')||(*cursor == 'd')) && (*(cursor+1) == '(')) {
            *type = COL_TYPE_INTEGER;
            adjust_by = 2;
        }
        else if ((*cursor == 'u') && (*(cursor+1) == '(')) {
            *type = COL_TYPE_INTEGER;
            adjust_by = 2;
        }
        else if ((*cursor == 'l') && ((*(cursor+1) == 'i')||(*(cursor+1) == 'd')) && (*(cursor+2) == '(')) {
            *type = COL_TYPE_LONG;
            adjust_by = 3;
        }
        else if ((*cursor == 'l') && (*(cursor+1) == 'u') && (*(cursor+2) == '(')) {
            *type = COL_TYPE_LONG;
            adjust_by = 3;
        }
        else if (((*cursor == 'f')||(*cursor == 'e')) && (*(cursor+1) == '(')) {
            *type = COL_TYPE_DOUBLE;
            adjust_by = 2;
        }
        else if (((*cursor == 's') || (*cursor == 'd')) && (*(cursor+1) == 'b') && (*(cursor+2) == '(')) {
            *type = COL_TYPE_BOOL;
            adjust_by = 3;
            if (*cursor == 's') *bool_type = 1;
            else *bool_type = 0;
        }
        else if ((*cursor == 'n') && (*(cursor+1) == '(')) {
            *type = COL_TYPE_BINARY;
            adjust_by = 2;
        }
        else {
            TRACE_ERROR_STRING("Invalid key - end of processing!", key);
            return E_LIST_ERROR;
        }

        cursor += adjust_by;

        /* Skip spaces if any */
        while (isspace(*cursor)) cursor++;

        start = cursor;

        /* Now we need to extract the name of the property */
        /* We will not be nice here - the add_property will validate if the name is ok */
        while ((*cursor != '\0') && (*cursor != ')') && (!isspace(*cursor))) cursor++;

        /* End of string - we are done */
        if ((*cursor == '\0') || (cursor == start)) {
            TRACE_ERROR_STRING("Invalid key - end of processing!", "");
            return E_LIST_EMPTY;
        }

        end = cursor;

        /* Skip spaces if any */
        while (isspace(*cursor)) cursor++;

        /* Check that end of the string is in proper format */
        if ((*cursor != ')') && (*(cursor + 1) != '\0')) {
            TRACE_ERROR_STRING("Invalid key - missing ')' .", key);
            return E_LIST_ERROR;
        }

        *property = start;
        *prop_len = end - start;

        TRACE_INFO_STRING("Property:", *property);
        TRACE_INFO_NUMBER("Property len:", *prop_len);
        ret = E_LIST_ADD;
    }
    else {
        /* Just got a key */
        /* Mark the start of the actual property */
        start = cursor;

        /* Now we need to extract the name of the property */
        /* We will not be nice here - the add_property will validate if the name is ok */
        while ((*cursor != '\0') && (!isspace(*cursor))) cursor++;

        /* End of string - we are done */
        if (cursor == start) {
            TRACE_ERROR_STRING("Invalid key - end of processing!", "");
            return E_LIST_EMPTY;
        }

        *prop_len = cursor - start;
        *property = start;
        TRACE_INFO_STRING("We are told to add/update the property (or last)!", *property);

        if(strncmp(*property, E_EOARG, *prop_len) == 0) ret = E_LIST_LAST;
        else ret = E_LIST_ADD;
    }

    TRACE_INFO_STRING("Returning Property:",*property);
    TRACE_INFO_NUMBER("Returning Property len:", *prop_len);
    TRACE_INFO_NUMBER("Returning Type:", *type);
    TRACE_INFO_NUMBER("Returning Has length:", *has_len);


    TRACE_FLOW_STRING("interprete_key", "Exit");

    return ret;
}

/* Make sure that the right string is given for bool value */
static int convert_bool(char *data_str, unsigned char *data_bool)
{
    TRACE_FLOW_STRING("convert_bool", "Called");
    TRACE_INFO_STRING("Data", data_str);

    if ((strncasecmp(data_str, str_true, sizeof(str_true)) == 0) ||
        (strncasecmp(data_str, str_yes, sizeof(str_yes)) == 0)) {
        TRACE_INFO_STRING("Matched TRUE", "");
        *data_bool = '\1';
        return 1;
    }
    if ((strncasecmp(data_str, str_false, sizeof(str_false)) == 0) ||
        (strncasecmp(data_str, str_no, sizeof(str_no)) == 0)) {
        TRACE_INFO_STRING("Matched FALSE", "");
        *data_bool = '\0';
        return 1;
    }
    TRACE_INFO_STRING("Matched NOTHING", "");
    return 0;
}


/* Process argument list */
/* Update collection based on the passed in arguments */
static int process_arg_list(struct collection_item *col,
                            va_list args)
{
    int error = EOK;
    char *arg = NULL;
    char *propcopy = NULL;
    int ret = 0;
    int type = 0;
    char *property = NULL;
    int prop_len = 0;
    int has_len = 0;
    int bool_type = 0;
    char *data_str = NULL;
    int data_int = 0;
    unsigned int data_uint = 0;
    long data_long = 0;
    unsigned long data_ulong = 0;
    void *data_bin = NULL;
    double data_dbl = 0.;
    int length = 0;
    void *data = NULL;
    unsigned char data_bool = '\0';

    TRACE_FLOW_STRING("process_arg_list", "Entry.");

    /* We will break from the loop when we find the last item */
    while (1) {

        /* Get next key */
        arg = va_arg(args, char *);

        if (arg == NULL) {
            TRACE_ERROR_STRING("Invalid NULL argument.", "Key can't be NULL");
            return EINVAL;
        }

        /* Interprete the key.
         * It can be just " key ",
         * it can be " - key ",
         * or it can be a formatted string
         * something like " %*s( key )".
         * Function will deal with all cases.
         * Passed in variables initialized and updated inside
         */
        ret = interprete_key(arg,
                             &type,
                             &property,
                             &prop_len,
                             &has_len,
                             &bool_type);

        if (ret == E_LIST_LAST) {
            TRACE_INFO_STRING("Process found last key", arg);
            break;
        }

        if ((ret == E_LIST_ADD) || (ret == E_LIST_REMOVE)) {
            /* We need to create a dup of the string */
            propcopy = strndup(property, prop_len);
            if (propcopy == NULL) {
                TRACE_ERROR_STRING("Failed to allocate property", arg);
                return ENOMEM;
            }

            TRACE_INFO_STRING("Processing property", propcopy);

            /* Are we supposed to add? */
            if (ret == E_LIST_ADD) {


                /* NOTE: We are not going to check if the key value pairs
                 * are consistent.
                 * It can be made a bit more bullet proof by adding
                 * significant complexity to the code but I do not
                 * think it makes much sense to do so.
                 * There is no way to prevent the argument mismatch
                 * issues 100%. Printf can crash if aguments are
                 * missed or bad, so do we...
                 */

                /* Get data */
                switch(type) {

                case COL_TYPE_STRING:   data_str = va_arg(args, char *);
                                        data = (void *)data_str;
                                        if (has_len) length = va_arg(args, int);
                                        else length = strlen(data_str) + 1;
                                        TRACE_INFO_STRING("Adding string:", data_str);
                                        TRACE_INFO_NUMBER("Length:",length);
                                        break;

                case COL_TYPE_BINARY:   data_bin = va_arg(args, void *);
                                        data = (void *)data_bin;
                                        length = va_arg(args, int);
                                        break;

                case COL_TYPE_INTEGER:  data_int = va_arg(args, int);
                                        data = (void *)(&data_int);
                                        length = sizeof(int);
                                        break;

                case COL_TYPE_UNSIGNED: data_uint = va_arg(args, unsigned int);
                                        data = (void *)(&data_uint);
                                        length = sizeof(unsigned int);
                                        break;

                case COL_TYPE_LONG:     data_long = va_arg(args, long);
                                        data = (void *)(&data_long);
                                        length = sizeof(long);
                                        break;

                case COL_TYPE_ULONG:    data_ulong = va_arg(args, unsigned long);
                                        data = (void *)(&data_ulong);
                                        length = sizeof(unsigned long);
                                        break;

                case COL_TYPE_DOUBLE:   data_dbl = va_arg(args, double);
                                        data = (void *)(&data_dbl);
                                        length = sizeof(double);
                                        break;

                case COL_TYPE_BOOL:     if (bool_type) {
                                            /* It is a string */
                                            data_str = va_arg(args,char *);
                                            /* Check if it is a valid str */
                                            if (!(convert_bool(data_str, &data_bool))) {
                                                TRACE_ERROR_STRING("Failed to to convert bool value", data_str);
                                                free(propcopy);
                                                return EINVAL;
                                            }
                                        }
                                        else {
                                            /* It is an int */
                                            data_int = va_arg(args, int);
                                            if (data_int) data_bool = 1;
                                            else data_bool = 0;
                                        }

                                        data = (void *)(&data_bool);
                                        length = sizeof(unsigned char);
                                        break;

                default:
                                        TRACE_ERROR_STRING("Invalid or unknown type", propcopy);
                                        free(propcopy);
                                        return EINVAL;
                }

                /* Insert or update */
                error = col_insert_property_with_ref(col,
                                                     NULL,
                                                     COL_DSP_END,
                                                     NULL,
                                                     0,
                                                     COL_INSERT_DUPOVER,
                                                     propcopy,
                                                     type,
                                                     data,
                                                     length,
                                                     NULL);
                if (error) {
                    TRACE_ERROR_STRING("Error inserting property", property);
                    free(propcopy);
                    return error;
                }
            }
            else {
                /* Remove case */
                while (error != ENOENT) {
                    error = col_delete_property(col,
                                                propcopy,
                                                COL_TYPE_ANY,
                                                COL_TRAVERSE_DEFAULT);

                    if ((error) && (error != ENOENT)) {
                        TRACE_ERROR_STRING("Error deleting property", propcopy);
                        free(propcopy);
                        return error;
                    }
                }
                error = EOK;
            }
            free(propcopy);
        }
        else {
            /* Errors related to key interpretation are handled here */
            TRACE_ERROR_STRING("Invalid arg", arg);
            return EINVAL;
        }
    } /* end of arg processing loop */

    TRACE_FLOW_STRING("process_arg_list", "Exit");
    return error;
}

static int get_default_template(struct collection_item **template)
{
    int error = EOK;

    TRACE_FLOW_STRING("get_default_template", "Entry");

    if (!default_template) {
        TRACE_INFO_STRING("Default template does not exit", "");
        error = elapi_set_default_template(E_BASE_DEFV1);
        if (error) {
            TRACE_ERROR_NUMBER("Set default template returned error", error);
            return error;
        }
    }

    *template = default_template;
    TRACE_FLOW_NUMBER("get_default_template. Exit returning", error);
    return error;
}

/* Cleanup callback installed when global template is used */
void clean_template(void)
{
    TRACE_FLOW_STRING("clean_template", "Entry");
    elapi_destroy_event_template(default_template);
    TRACE_FLOW_STRING("clean_template", "Exit");
}

/*****************************************************************************/

/* Create event template */
int elapi_create_event_template(struct collection_item **template,
                                unsigned base, ...)
{
    int error = EOK;
    struct collection_item *tpl = NULL;
    va_list args;

    TRACE_FLOW_STRING("elapi_create_event_template", "Entry");

    if (template == NULL ) {
        TRACE_ERROR_STRING("Template storage must be provided", "");
        return EINVAL;
    }

    *template = NULL;

    /* Create collection */
    error = col_create_collection(&tpl, E_TEMPLATE_NAME, COL_CLASS_ELAPI_TEMPLATE);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to create collection. Error", error);
        return error;
    }

    /* Add elements using base mask */
    error = add_base_elements(tpl, base);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to add base elements. Error", error);
        col_destroy_collection(tpl);
        return error;
    }

    /* Process varible arguments */
    va_start(args, base);

    /* Process variable argument list */
    error = process_arg_list(tpl, args);

    va_end(args);

    if (error) {
        TRACE_ERROR_NUMBER("Failed to process argument list. Error", error);
        col_destroy_collection(tpl);
        return error;
    }

    *template = tpl;

    TRACE_FLOW_STRING("elapi_create_event_template", "Exit");
    return error;
}

/* Function to destroy event template */
void elapi_destroy_event_template(struct collection_item *template)
{
    TRACE_FLOW_STRING("elapi_destroy_event_template", "Entry");

    col_destroy_collection(template);

    TRACE_FLOW_STRING("elapi_destroy_event_template", "Exit");
}

/* Create event */
int elapi_create_event(struct collection_item **event,
                       struct collection_item *template,
                       struct collection_item *collection,
                       int mode, ...)
{
    int error = EOK;
    struct collection_item *evt = NULL;
    va_list args;

    TRACE_FLOW_STRING("elapi_create_event", "Entry");

    /* Check storage */
    if (event == NULL ) {
        TRACE_ERROR_STRING("Event storage must be provided", "");
        return EINVAL;
    }

    /* Check for template */
    if (template == NULL ) {
        TRACE_ERROR_STRING("Template argument is missing", "");
        return EINVAL;
    }

    *event = NULL;

    /* Create collection */
    error = col_create_collection(&evt, E_EVENT_NAME, COL_CLASS_ELAPI_EVENT);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to create collection. Error", error);
        return error;
    }

    /* Add elements from the template */
    error = col_add_collection_to_collection(evt, NULL, NULL,
                                             (struct collection_item *)template,
                                             COL_ADD_MODE_FLAT);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to add elements from the template. Error", error);
        col_destroy_collection(evt);
        return error;
    }

    /* Add elements from the template */
    if (collection != NULL) {
        error = col_add_collection_to_collection(evt, NULL, NULL, collection, mode);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add elements from external collection. Error", error);
            col_destroy_collection(evt);
            return error;
        }
    }

    /* Process varible arguments */
    va_start(args, mode);

    /* Process variable argument list */
    error = process_arg_list(evt, args);

    va_end(args);

    if (error) {
        TRACE_ERROR_NUMBER("Failed to process argument list. Error", error);
        col_destroy_collection(evt);
        return error;
    }

    *event = evt;

    TRACE_FLOW_STRING("elapi_create_event", "Exit");
    return error;
}

/* Add/Updates/Removes the event attributes based on the and provided key value pairs */
int elapi_modify_event(struct collection_item *event,
                       struct collection_item *collection,
                       int mode, ...)
{
    int error = EOK;
    va_list args;

    TRACE_FLOW_STRING("elapi_modify_event", "Entry");

    /* Check event */
    if (event == NULL ) {
        TRACE_ERROR_STRING("Event must be provided", "");
        return EINVAL;
    }

    /* Add elements from the template */
    if (collection != NULL) {
        error = col_add_collection_to_collection(event, NULL, NULL, collection, mode);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to add elements from external collection. Error", error);
            col_destroy_collection(event);
            return error;
        }
    }

    /* Process varible arguments */
    va_start(args, mode);

    /* Process variable argument list */
    error = process_arg_list(event, args);

    va_end(args);

    if (error) {
        TRACE_ERROR_NUMBER("Failed to process argument list. Error", error);
        return error;
    }

    TRACE_FLOW_STRING("elapi_modify_event", "Exit");
    return error;
}

/* Create a copy of the event */
int elapi_copy_event(struct collection_item **new_event,
                     struct collection_item *source_event)
{
    int error = EOK;

    TRACE_FLOW_STRING("elapi_copy_event", "Entry");

    error = col_copy_collection(new_event,
                                source_event,
                                NULL);

    TRACE_FLOW_NUMBER("elapi_copy_event. Exit Returning", error);
    return error;
}

/* Function to destroy event. */
void elapi_destroy_event(struct collection_item *event)
{
    TRACE_FLOW_STRING("elapi_destroy_event", "Entry");

    col_destroy_collection(event);

    TRACE_FLOW_STRING("elapi_destroy_event", "Exit");
}

/* Initializes default internal template */
int elapi_set_default_template(unsigned base, ...)
{
    int error = EOK;
    struct collection_item *tpl;
    va_list args;

    TRACE_FLOW_STRING("elapi_set_default_template", "Entry");

    /* Clean previous instance of the default template */
    elapi_destroy_event_template(default_template);
    default_template = NULL;

    /* Create collection */
    error = col_create_collection(&tpl, E_TEMPLATE_NAME, COL_CLASS_ELAPI_TEMPLATE);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to create collection. Error", error);
        return error;
    }

    /* Add elements using base mask */
    error = add_base_elements(tpl, base);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to add base elements. Error", error);
        col_destroy_collection(tpl);
        return error;
    }

    /* Process varible arguments */
    va_start(args, base);

    /* Process variable argument list */
    error = process_arg_list(tpl, args);

    va_end(args);

    if (error) {
        TRACE_ERROR_NUMBER("Failed to process argument list. Error", error);
        col_destroy_collection(tpl);
        return error;
    }

    /* Install a cleanup callback */
    if (atexit(clean_template)) {
        TRACE_ERROR_NUMBER("Failed to install cleanup callback. Error", ENOSYS);
        col_destroy_collection(tpl);
        /* NOTE: Could not find a better error for this case */
        return ENOSYS;
    }

    default_template = tpl;

    TRACE_FLOW_STRING("elapi_set_default_template", "Exit");
    return error;
}


/* This function will use internal default template */
int elapi_create_simple_event(struct collection_item **event, ...)
{
    int error = EOK;
    struct collection_item *evt = NULL;
    va_list args;
    struct collection_item *template;

    TRACE_FLOW_STRING("elapi_create_simple_event", "Entry");

    /* Check storage */
    if (event == NULL ) {
        TRACE_ERROR_STRING("Event storage must be provided", "");
        return EINVAL;
    }

    *event = NULL;

    error = get_default_template(&template);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to get default template. Error", error);
        return error;
    }

    /* Create collection */
    error = col_create_collection(&evt, E_EVENT_NAME, COL_CLASS_ELAPI_EVENT);
    if (error) {
        TRACE_ERROR_NUMBER("Failed to create collection. Error", error);
        return error;
    }

    /* Add elements from the template */
    error = col_add_collection_to_collection(evt, NULL, NULL,
                                             template,
                                             COL_ADD_MODE_FLAT);

    if (error) {
        TRACE_ERROR_NUMBER("Failed to add elements from the template. Error", error);
        col_destroy_collection(evt);
        return error;
    }

    /* Process varible arguments */
    va_start(args, event);

    /* Process variable argument list */
    error = process_arg_list(evt, args);

    va_end(args);

    if (error) {
        TRACE_ERROR_NUMBER("Failed to process argument list. Error", error);
        col_destroy_collection(evt);
        return error;
    }

    *event = evt;

    TRACE_FLOW_STRING("elapi_create_simple_event", "Exit");
    return error;
}