summaryrefslogtreecommitdiff
path: root/plugins/imzmq3/imzmq3.c
blob: 08b1dbe4c46ea98c042d0815b70d31f6e0364d8c (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
/* imzmq3.c
 *
 * This input plugin enables rsyslog to read messages from a ZeroMQ
 * queue.
 *
 * Copyright 2012 Talksum, Inc.
 *
 * 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, 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
 * 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/>.
 * 
 * Authors: 
 * David Kelly <davidk@talksum.com>
 * Hongfei Cheng <hongfeic@talksum.com>
 */


#include "config.h"
#include "rsyslog.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cfsysline.h"
#include "dirty.h"
#include "errmsg.h"
#include "glbl.h"
#include "module-template.h"
#include "msg.h"
#include "net.h"
#include "parser.h"
#include "prop.h"
#include "ruleset.h"
#include "srUtils.h"
#include "unicode-helper.h"

#include <czmq.h>

MODULE_TYPE_INPUT
MODULE_TYPE_NOKEEP
MODULE_CNFNAME("imzmq3");

/* convienent symbols to denote a socket we want to bind
 * vs one we want to just connect to 
 */
#define ACTION_CONNECT 1
#define ACTION_BIND    2

/* Module static data */
DEF_IMOD_STATIC_DATA
DEFobjCurrIf(errmsg)
DEFobjCurrIf(glbl)
DEFobjCurrIf(prop)
DEFobjCurrIf(ruleset)

 
/* ----------------------------------------------------------------------------
 * structs to describe sockets
 */
typedef struct _socket_type {
    char*  name;
    int    type;
} socket_type;

/* more overkill, but seems nice to be consistent.*/
typedef struct _socket_action {
    char* name;
    int   action;
} socket_action;

typedef struct _poller_data {
    ruleset_t*  ruleset;
    thrdInfo_t* thread;
} poller_data;


/* a linked-list of subscription topics */
typedef struct sublist_t {
    char* subscribe;
    struct sublist_t* next;
} sublist;

struct instanceConf_s {
    int                    type;
    int                    action;
    char*                  description;
    int                    sndHWM; /* if you want more than 2^32 messages, */
    int                    rcvHWM; /* then pass in 0 (the default). */
    char*                  identity;
    sublist*               subscriptions;
    int                    sndBuf;
    int                    rcvBuf;
    int                    linger;
    int                    backlog;
    int                    sndTimeout;
    int                    rcvTimeout;
    int                    maxMsgSize;
    int                    rate;
    int                    recoveryIVL;
    int                    multicastHops;
    int                    reconnectIVL;
    int                    reconnectIVLMax;
    int                    ipv4Only;
    int                    affinity;
    uchar*                 pszBindRuleset;
    ruleset_t*             pBindRuleset;
    struct instanceConf_s* next;
    
};

struct modConfData_s {
    rsconf_t* pConf;
    instanceConf_t* root;
    instanceConf_t* tail;
    int             io_threads;
};
struct lstn_s {
    struct lstn_s* next;
    void* sock;
    ruleset_t* pRuleset;
};

/* ----------------------------------------------------------------------------
 *  Static definitions/initializations.
 */
static modConfData_t*       runModConf      = NULL;
static struct lstn_s*       lcnfRoot        = NULL;
static struct lstn_s*       lcnfLast        = NULL;
static prop_t*              s_namep         = NULL;
static zloop_t*             s_zloop         = NULL;
static zctx_t*              s_context       = NULL;
static socket_type          socketTypes[]   = {
    {"SUB",    ZMQ_SUB  },
    {"PULL",   ZMQ_PULL },
    {"ROUTER", ZMQ_ROUTER },
    {"XSUB",   ZMQ_XSUB }
};

static socket_action        socketActions[] = {
    {"BIND",    ACTION_BIND},
    {"CONNECT", ACTION_CONNECT},
};

static struct cnfparamdescr modpdescr[] = {
    { "ioThreads", eCmdHdlrInt,     0 },
};

static struct cnfparamblk modpblk = {
    CNFPARAMBLK_VERSION,
    sizeof(modpdescr)/sizeof(struct cnfparamdescr),
    modpdescr
};

static struct cnfparamdescr inppdescr[] = {
    { "description",         eCmdHdlrGetWord, 0 },
    { "sockType",            eCmdHdlrGetWord, 0 },
    { "subscribe",           eCmdHdlrGetWord, 0 },
    { "ruleset",             eCmdHdlrGetWord, 0 },
    { "action",              eCmdHdlrGetWord, 0 },
    { "sndHWM",              eCmdHdlrInt,     0 },
    { "rcvHWM",              eCmdHdlrInt,     0 },
    { "identity",            eCmdHdlrGetWord, 0 },
    { "sndBuf",              eCmdHdlrInt,     0 },
    { "rcvBuf",              eCmdHdlrInt,     0 },
    { "linger",              eCmdHdlrInt,     0 },
    { "backlog",             eCmdHdlrInt,     0 },
    { "sndTimeout",          eCmdHdlrInt,     0 },
    { "rcvTimeout",          eCmdHdlrInt,     0 },
    { "maxMsgSize",          eCmdHdlrInt,     0 },
    { "rate",                eCmdHdlrInt,     0 },
    { "recoveryIVL",         eCmdHdlrInt,     0 },
    { "multicastHops",       eCmdHdlrInt,     0 },
    { "reconnectIVL",        eCmdHdlrInt,     0 },
    { "reconnectIVLMax",     eCmdHdlrInt,     0 },
    { "ipv4Only",            eCmdHdlrInt,     0 },
    { "affinity",            eCmdHdlrInt,     0 }
};

static struct cnfparamblk inppblk = {
    CNFPARAMBLK_VERSION,
    sizeof(inppdescr)/sizeof(struct cnfparamdescr),
    inppdescr
};

#include "im-helper.h" /* must be included AFTER the type definitions! */

/* ----------------------------------------------------------------------------
 * Helper functions
 */

/* get the name of a socket type, return the ZMQ_XXX type
   or -1 if not a supported type (see above)
*/
static int getSocketType(char* name) {
    int type = -1;
    uint i;
    
    /* match name with known socket type */
    for(i=0; i<sizeof(socketTypes)/sizeof(socket_type); ++i) {
        if( !strcmp(socketTypes[i].name, name) ) {
            type = socketTypes[i].type;
            break;
        }
    }
    
    /* whine if no match was found. */
    if (type == -1) 
        errmsg.LogError(0, NO_ERRCODE, "unknown type %s",name);
    
    return type;
}


static int getSocketAction(char* name) {
    int action = -1;
    uint i;

    /* match name with known socket action */
    for(i=0; i < sizeof(socketActions)/sizeof(socket_action); ++i) {
        if(!strcmp(socketActions[i].name, name)) {
            action = socketActions[i].action;
            break;
        }
    }

    /* whine if no matching action was found */
    if (action == -1) 
        errmsg.LogError(0, NO_ERRCODE, "unknown action %s",name);
    
    return action;
}


static void setDefaults(instanceConf_t* info) {
    info->type            = -1;
    info->action          = -1;
    info->description     = NULL;
    info->sndHWM          = -1;
    info->rcvHWM          = -1;
    info->identity        = NULL;
    info->subscriptions   = NULL;
    info->pszBindRuleset  = NULL;
    info->pBindRuleset    = NULL;
    info->sndBuf          = -1;
    info->rcvBuf          = -1;
    info->linger          = -1;
    info->backlog         = -1;
    info->sndTimeout      = -1;
    info->rcvTimeout      = -1;
    info->maxMsgSize      = -1;
    info->rate            = -1;
    info->recoveryIVL     = -1;
    info->multicastHops   = -1;
    info->reconnectIVL    = -1;
    info->reconnectIVLMax = -1;
    info->ipv4Only        = -1;
    info->affinity        = -1;
    info->next            = NULL;
};

/* given a comma separated list of subscriptions, create a char* array of them
 * to set later 
 */
static rsRetVal parseSubscriptions(char* subscribes, sublist** subList){
    char* tok = strtok(subscribes, ",");
    sublist* currentSub;
    sublist* head;
    DEFiRet;

    /* create empty list */
    CHKmalloc(*subList = (sublist*)MALLOC(sizeof(sublist)));
    head = *subList;
    head->next = NULL;
    head->subscribe=NULL;
    currentSub=head;
    
    if(tok) {
        head->subscribe=strdup(tok);
        for(tok=strtok(NULL, ","); tok!=NULL;tok=strtok(NULL, ",")) {
            CHKmalloc(currentSub->next = (sublist*)MALLOC(sizeof(sublist)));
            currentSub=currentSub->next;
            currentSub->subscribe=strdup(tok);
            currentSub->next=NULL;
        }
    } else {
        /* make empty subscription ie subscribe="" */
        head->subscribe=strdup("");
    }
    /* TODO: temporary logging */
    currentSub = head;
    DBGPRINTF("imzmq3: Subscriptions:");
    for(currentSub = head; currentSub != NULL; currentSub=currentSub->next) {
        DBGPRINTF("'%s'", currentSub->subscribe); 
    }
    DBGPRINTF("\n");
finalize_it:
    RETiRet;
}

static rsRetVal validateConfig(instanceConf_t* info) {

    if (info->type == -1) {
        errmsg.LogError(0, RS_RET_INVALID_PARAMS,
                        "you entered an invalid type");
        return RS_RET_INVALID_PARAMS;
    }
    if (info->action == -1) {
        errmsg.LogError(0, RS_RET_INVALID_PARAMS,
                        "you entered an invalid action");
        return RS_RET_INVALID_PARAMS;
    }
    if (info->description == NULL) {
        errmsg.LogError(0, RS_RET_INVALID_PARAMS,
                        "you didn't enter a description");
        return RS_RET_INVALID_PARAMS;
    }
    if(info->type == ZMQ_SUB && info->subscriptions == NULL) {
        errmsg.LogError(0, RS_RET_INVALID_PARAMS,
                        "SUB sockets need a subscription");
        return RS_RET_INVALID_PARAMS;
    }
    if(info->type != ZMQ_SUB && info->subscriptions != NULL) {
        errmsg.LogError(0, RS_RET_INVALID_PARAMS,
                        "only SUB sockets can have subscriptions");
        return RS_RET_INVALID_PARAMS;
    }
    return RS_RET_OK;
}

static rsRetVal createContext() {
    if (s_context == NULL) {
        DBGPRINTF("imzmq3: creating zctx...");
        zsys_handler_set(NULL);
        s_context = zctx_new();
        
        if (s_context == NULL) {
            errmsg.LogError(0, RS_RET_INVALID_PARAMS,
                            "zctx_new failed: %s",
                            zmq_strerror(errno));
            /* DK: really should do better than invalid params...*/
            return RS_RET_INVALID_PARAMS;
        }
        DBGPRINTF("success!\n");
        if (runModConf->io_threads > 1) {
            DBGPRINTF("setting io worker threads to %d\n", runModConf->io_threads);
            zctx_set_iothreads(s_context, runModConf->io_threads);
        }
    }
    return RS_RET_OK;
}

static rsRetVal createSocket(instanceConf_t* info, void** sock) {
    int rv;
    sublist* sub;

    *sock = zsocket_new(s_context, info->type);
    if (!sock) {
        errmsg.LogError(0,
                        RS_RET_INVALID_PARAMS,
                        "zsocket_new failed: %s, for type %d",
                        zmq_strerror(errno),info->type);
        /* DK: invalid params seems right here */
        return RS_RET_INVALID_PARAMS;
    }
    DBGPRINTF("imzmq3: socket of type %d created successfully\n", info->type)
    /* Set options *before* the connect/bind. */
    if (info->identity)             zsocket_set_identity(*sock, info->identity);
    if (info->sndBuf > -1)          zsocket_set_sndbuf(*sock, info->sndBuf);
    if (info->rcvBuf > -1)          zsocket_set_rcvbuf(*sock, info->rcvBuf);
    if (info->linger > -1)          zsocket_set_linger(*sock, info->linger);
    if (info->backlog > -1)         zsocket_set_backlog(*sock, info->backlog);
    if (info->sndTimeout > -1)      zsocket_set_sndtimeo(*sock, info->sndTimeout);
    if (info->rcvTimeout > -1)      zsocket_set_rcvtimeo(*sock, info->rcvTimeout);
    if (info->maxMsgSize > -1)      zsocket_set_maxmsgsize(*sock, info->maxMsgSize);
    if (info->rate > -1)            zsocket_set_rate(*sock, info->rate);
    if (info->recoveryIVL > -1)     zsocket_set_recovery_ivl(*sock, info->recoveryIVL);
    if (info->multicastHops > -1)   zsocket_set_multicast_hops(*sock, info->multicastHops);
    if (info->reconnectIVL > -1)    zsocket_set_reconnect_ivl(*sock, info->reconnectIVL);
    if (info->reconnectIVLMax > -1) zsocket_set_reconnect_ivl_max(*sock, info->reconnectIVLMax);
    if (info->ipv4Only > -1)        zsocket_set_ipv4only(*sock, info->ipv4Only);
    if (info->affinity > -1)        zsocket_set_affinity(*sock, info->affinity);
    if (info->sndHWM > -1 )         zsocket_set_sndhwm(*sock, info->sndHWM);
    if (info->rcvHWM > -1 )         zsocket_set_rcvhwm(*sock, info->rcvHWM);
    /* Set subscriptions.*/
    if (info->type == ZMQ_SUB) {
        for(sub = info->subscriptions; sub!=NULL; sub=sub->next) {
            zsocket_set_subscribe(*sock, sub->subscribe);
        }
    }

    /* Do the bind/connect... */
    if (info->action==ACTION_CONNECT) {
        rv = zsocket_connect(*sock, info->description);
        if (rv == -1) {
            errmsg.LogError(0,
                            RS_RET_INVALID_PARAMS,
                            "zmq_connect using %s failed: %s",
                            info->description, zmq_strerror(errno));
            return RS_RET_INVALID_PARAMS;
        }
        DBGPRINTF("imzmq3: connect for %s successful\n",info->description);
    } else {
        rv = zsocket_bind(*sock, info->description);
        if (rv == -1) {
            errmsg.LogError(0,
                            RS_RET_INVALID_PARAMS,
                            "zmq_bind using %s failed: %s",
                            info->description, zmq_strerror(errno));
            return RS_RET_INVALID_PARAMS;
        }
        DBGPRINTF("imzmq3: bind for %s successful\n",info->description);
    }
    return RS_RET_OK;
}

/* ----------------------------------------------------------------------------
 * Module endpoints
 */


/* add an actual endpoint
 */
static rsRetVal createInstance(instanceConf_t** pinst) {
    DEFiRet;
    instanceConf_t* inst;
    CHKmalloc(inst = MALLOC(sizeof(instanceConf_t)));
    
    /* set defaults into new instance config struct */
    setDefaults(inst);
    
    /* add this to the config */
    if (runModConf->root == NULL || runModConf->tail == NULL) {
        runModConf->tail = runModConf->root = inst;
    } else {
        runModConf->tail->next = inst;
        runModConf->tail       = inst;
    }
    *pinst = inst;
finalize_it:
    RETiRet;
}

static rsRetVal createListener(struct cnfparamvals* pvals) {
    instanceConf_t* inst;
    int i;
    DEFiRet;
    
    CHKiRet(createInstance(&inst));
    for(i = 0 ; i < inppblk.nParams ; ++i) {
        if(!pvals[i].bUsed) 
            continue;	
        if(!strcmp(inppblk.descr[i].name, "ruleset")) {
            inst->pszBindRuleset = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL);
        } else if(!strcmp(inppblk.descr[i].name, "description")) {
            inst->description = es_str2cstr(pvals[i].val.d.estr, NULL);
        } else if(!strcmp(inppblk.descr[i].name, "sockType")){
            inst->type = getSocketType(es_str2cstr(pvals[i].val.d.estr, NULL));
        } else if(!strcmp(inppblk.descr[i].name, "action")){
            inst->action = getSocketAction(es_str2cstr(pvals[i].val.d.estr, NULL));
        } else if(!strcmp(inppblk.descr[i].name, "sndHWM")) {
            inst->sndHWM = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "rcvHWM")) {
            inst->rcvHWM = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "subscribe")) {
            CHKiRet(parseSubscriptions(es_str2cstr(pvals[i].val.d.estr, NULL), 
                                       &inst->subscriptions));
        } else if(!strcmp(inppblk.descr[i].name, "identity")){
            inst->identity = es_str2cstr(pvals[i].val.d.estr, NULL);
        } else if(!strcmp(inppblk.descr[i].name, "sndBuf")) {
            inst->sndBuf = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "rcvBuf")) {
            inst->rcvBuf = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "linger")) {
            inst->linger = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "backlog")) {
            inst->backlog = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "sndTimeout")) {
            inst->sndTimeout = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "rcvTimeout")) {
            inst->rcvTimeout = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "maxMsgSize")) {
            inst->maxMsgSize = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "rate")) {
            inst->rate = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "recoveryIVL")) {
            inst->recoveryIVL = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "multicastHops")) {
            inst->multicastHops = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "reconnectIVL")) {
            inst->reconnectIVL = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "reconnectIVLMax")) {
            inst->reconnectIVLMax = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "ipv4Only")) {
            inst->ipv4Only = (int) pvals[i].val.d.n;
        } else if(!strcmp(inppblk.descr[i].name, "affinity")) {
            inst->affinity = (int) pvals[i].val.d.n;
        } else {
            errmsg.LogError(0, NO_ERRCODE, "imzmq3: program error, non-handled "
                            "param '%s'\n", inppblk.descr[i].name);
        }
    
    }
finalize_it:
    RETiRet;
}

static rsRetVal addListener(instanceConf_t* inst){
    /* create the socket */
    void* sock;
    struct lstn_s* newcnfinfo;
    DEFiRet;

    CHKiRet(createSocket(inst, &sock));

    /* now create new lstn_s struct */
    CHKmalloc(newcnfinfo=(struct lstn_s*)MALLOC(sizeof(struct lstn_s)));
    newcnfinfo->next = NULL;
    newcnfinfo->sock = sock;
    newcnfinfo->pRuleset = inst->pBindRuleset;
    
    /* add this struct to the global */
    if(lcnfRoot == NULL) {
        lcnfRoot = newcnfinfo;
    } 
    if(lcnfLast == NULL) {
        lcnfLast = newcnfinfo;
    } else {
        lcnfLast->next = newcnfinfo;
        lcnfLast = newcnfinfo;
    }

finalize_it:
    RETiRet;
}

static int handlePoll(zloop_t __attribute__((unused)) * loop, zmq_pollitem_t *poller, void* pd) {
    msg_t* pMsg;
    poller_data* pollerData = (poller_data*)pd;

    char* buf = zstr_recv(poller->socket);
    if (msgConstruct(&pMsg) == RS_RET_OK) {
        MsgSetRawMsg(pMsg, buf, strlen(buf));
        MsgSetInputName(pMsg, s_namep);
        MsgSetHOSTNAME(pMsg, glbl.GetLocalHostName(), ustrlen(glbl.GetLocalHostName()));
        MsgSetRcvFrom(pMsg, glbl.GetLocalHostNameProp());
        MsgSetRcvFromIP(pMsg, glbl.GetLocalHostIP());
        MsgSetMSGoffs(pMsg, 0);
        MsgSetFlowControlType(pMsg, eFLOWCTL_NO_DELAY);
        MsgSetRuleset(pMsg, pollerData->ruleset);
        pMsg->msgFlags = NEEDS_PARSING | PARSE_HOSTNAME;
        submitMsg2(pMsg);
    }
    
    /* gotta free the string returned from zstr_recv() */
    free(buf);
    
    if( pollerData->thread->bShallStop == TRUE) {
        /* a handler that returns -1 will terminate the 
           czmq reactor loop
        */
        return -1; 
    }
    
    return 0;
}

/* called when runInput is called by rsyslog 
 */
static rsRetVal rcv_loop(thrdInfo_t* pThrd){
    size_t          n_items = 0;
    size_t          i;
    int             rv;
    zmq_pollitem_t* items = NULL;
    poller_data*    pollerData = NULL;
    struct lstn_s*  current;
    instanceConf_t* inst;
    DEFiRet;

    /* now add listeners. This actually creates the sockets, etc... */
    for (inst = runModConf->root; inst != NULL; inst=inst->next) {
        addListener(inst);
    }
    if (lcnfRoot == NULL) {
        errmsg.LogError(0, NO_ERRCODE, "imzmq3: no listeners were "
                        "started, input not activated.\n");
        ABORT_FINALIZE(RS_RET_NO_RUN);
    }

    /* count the # of items first */
    for(current=lcnfRoot;current!=NULL;current=current->next)
        n_items++;

    /* make arrays of pollitems, pollerdata so they are easy to delete later */
    
    /* create the poll items*/ 
    CHKmalloc(items = (zmq_pollitem_t*)MALLOC(sizeof(zmq_pollitem_t)*n_items));
    
    /* create poller data (stuff to pass into the zmq closure called when we get a message)*/
    CHKmalloc(pollerData = (poller_data*)MALLOC(sizeof(poller_data)*n_items));

    /* loop through and initialize the poll items and poller_data arrays...*/
    for(i=0, current = lcnfRoot; current != NULL; current = current->next, i++) {
        /* create the socket, update items.*/
        items[i].socket=current->sock;
        items[i].events = ZMQ_POLLIN;
        
        /* now update the poller_data for this item */
        pollerData[i].thread  = pThrd;
        pollerData[i].ruleset = current->pRuleset;
    }

    s_zloop = zloop_new();
    for(i=0; i<n_items; ++i) {
        
        rv = zloop_poller(s_zloop, &items[i], handlePoll, &pollerData[i]);
        if (rv) {
            errmsg.LogError(0, NO_ERRCODE, "imzmq3: zloop_poller failed for item %zu: %s", i, zmq_strerror(errno));
        } 
    }
    DBGPRINTF("imzmq3: zloop_poller starting...");
    zloop_start(s_zloop);   
    zloop_destroy(&s_zloop);
    DBGPRINTF("imzmq3: zloop_poller stopped.");
finalize_it:
    zctx_destroy(&s_context);

    free(items);
    free(pollerData);
    RETiRet;
}

/* ----------------------------------------------------------------------------
 * input module functions
 */

BEGINrunInput
CODESTARTrunInput
    CHKiRet(rcv_loop(pThrd));
finalize_it:
    RETiRet;
ENDrunInput


/* initialize and return if will run or not */
BEGINwillRun
CODESTARTwillRun
    /* we need to create the inputName property (only once during our
       lifetime) */
    CHKiRet(prop.Construct(&s_namep));
    CHKiRet(prop.SetString(s_namep,
                           UCHAR_CONSTANT("imzmq3"),
                           sizeof("imzmq3") - 1));
    CHKiRet(prop.ConstructFinalize(s_namep));

finalize_it:
ENDwillRun


BEGINafterRun
CODESTARTafterRun
    /* do cleanup here */
    if (s_namep != NULL)
        prop.Destruct(&s_namep);
ENDafterRun


BEGINmodExit
CODESTARTmodExit
    /* release what we no longer need */
    objRelease(errmsg, CORE_COMPONENT);
    objRelease(glbl, CORE_COMPONENT);
    objRelease(prop, CORE_COMPONENT);
    objRelease(ruleset, CORE_COMPONENT);
ENDmodExit


BEGINisCompatibleWithFeature
CODESTARTisCompatibleWithFeature
    if (eFeat == sFEATURENonCancelInputTermination)
        iRet = RS_RET_OK;
ENDisCompatibleWithFeature


BEGINbeginCnfLoad
CODESTARTbeginCnfLoad
    /* After endCnfLoad() (BEGINendCnfLoad...ENDendCnfLoad) is called,
     * the pModConf pointer must not be used to change the in-memory
     * config object. It's safe to use the same pointer for accessing
     * the config object until freeCnf() (BEGINfreeCnf...ENDfreeCnf). */
    runModConf = pModConf;
    runModConf->pConf = pConf;
    /* init module config */
    runModConf->io_threads = 0; /* 0 means don't set it */
ENDbeginCnfLoad


BEGINsetModCnf
    struct cnfparamvals* pvals = NULL;
    int i;
CODESTARTsetModCnf
    pvals = nvlstGetParams(lst, &modpblk, NULL);
    if (NULL == pvals) {
         errmsg.LogError(0, RS_RET_MISSING_CNFPARAMS, "imzmq3: error processing module "
                         " config parameters ['module(...)']");
         ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS);
    }
 
    for (i=0; i < modpblk.nParams; ++i) {
        if (!pvals[i].bUsed)
            continue;
        if (!strcmp(modpblk.descr[i].name, "ioThreads")) {
            runModConf->io_threads = (int)pvals[i].val.d.n;
        } else {
            errmsg.LogError(0, RS_RET_INVALID_PARAMS, 
                           "imzmq3: config error, unknown "
                           "param %s in setModCnf\n", 
                           modpblk.descr[i].name);
        }   
    }
 
finalize_it:
    if (pvals != NULL)
        cnfparamvalsDestruct(pvals, &modpblk);
ENDsetModCnf


BEGINendCnfLoad
CODESTARTendCnfLoad
    /* Last chance to make changes to the in-memory config object for this
     * input module. After this call, the config object must no longer be
     * changed. */
    if (pModConf != runModConf) {
        errmsg.LogError(0, NO_ERRCODE, "imzmq3: pointer of in-memory config object has "
                        "changed - pModConf=%p, runModConf=%p", pModConf, runModConf);
    }
    assert(pModConf == runModConf);
ENDendCnfLoad


/* function to generate error message if framework does not find requested ruleset */
static inline void
std_checkRuleset_genErrMsg(__attribute__((unused)) modConfData_t *modConf, instanceConf_t *inst)
{
    errmsg.LogError(0, NO_ERRCODE, "imzmq3: ruleset '%s' for socket %s not found - "
                    "using default ruleset instead", inst->pszBindRuleset,
                    inst->description);
}


BEGINcheckCnf
instanceConf_t* inst;
CODESTARTcheckCnf
    for(inst = pModConf->root; inst!=NULL; inst=inst->next) {
        std_checkRuleset(pModConf, inst);
        /* now, validate the instanceConf */
        CHKiRet(validateConfig(inst));
    }
finalize_it:
    RETiRet;
ENDcheckCnf


BEGINactivateCnfPrePrivDrop
CODESTARTactivateCnfPrePrivDrop
    if (pModConf != runModConf) {
        errmsg.LogError(0, NO_ERRCODE, "imzmq3: pointer of in-memory config object has "
                        "changed - pModConf=%p, runModConf=%p", pModConf, runModConf);
    }
    assert(pModConf == runModConf);

    /* first create the context */
    createContext();

    /* could setup context here, and set the global worker threads
       and so on... */
ENDactivateCnfPrePrivDrop


BEGINactivateCnf
CODESTARTactivateCnf
    if (pModConf != runModConf) {
        errmsg.LogError(0, NO_ERRCODE, "imzmq3: pointer of in-memory config object has "
                        "changed - pModConf=%p, runModConf=%p", pModConf, runModConf);
    }
    assert(pModConf == runModConf);
ENDactivateCnf


BEGINfreeCnf
    struct lstn_s *lstn, *lstn_r;
    instanceConf_t *inst, *inst_r;
    sublist *sub, *sub_r;
CODESTARTfreeCnf
    DBGPRINTF("imzmq3: BEGINfreeCnf ...\n");
    if (pModConf != runModConf) {
        errmsg.LogError(0, NO_ERRCODE, "imzmq3: pointer of in-memory config object has "
                        "changed - pModConf=%p, runModConf=%p", pModConf, runModConf);
    }
    for (lstn = lcnfRoot; lstn != NULL; ) {
        lstn_r = lstn;
        lstn = lstn_r->next;
        free(lstn_r);
    }
    for (inst = pModConf->root ; inst != NULL ; ) {
        for (sub = inst->subscriptions; sub != NULL; ) {
            free(sub->subscribe);
            sub_r = sub;
            sub = sub_r->next;
            free(sub_r);
        }
        free(inst->pszBindRuleset);
        inst_r = inst;
        inst = inst->next;
        free(inst_r);
    }
ENDfreeCnf


BEGINnewInpInst
    struct cnfparamvals* pvals;
CODESTARTnewInpInst

    DBGPRINTF("newInpInst (imzmq3)\n");
    pvals = nvlstGetParams(lst, &inppblk, NULL);
    if(NULL==pvals) {
        errmsg.LogError(0, RS_RET_MISSING_CNFPARAMS,
                        "imzmq3: required parameters are missing\n");
        ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS);
    }
    DBGPRINTF("imzmq3: input param blk:\n");
    cnfparamsPrint(&inppblk, pvals);
    
    /* now, parse the config params and so on... */
    CHKiRet(createListener(pvals));

finalize_it:
CODE_STD_FINALIZERnewInpInst
    cnfparamvalsDestruct(pvals, &inppblk);
ENDnewInpInst


BEGINqueryEtryPt
CODESTARTqueryEtryPt
CODEqueryEtryPt_STD_IMOD_QUERIES
CODEqueryEtryPt_STD_CONF2_QUERIES
CODEqueryEtryPt_STD_CONF2_setModCnf_QUERIES
CODEqueryEtryPt_STD_CONF2_PREPRIVDROP_QUERIES
CODEqueryEtryPt_STD_CONF2_IMOD_QUERIES
CODEqueryEtryPt_IsCompatibleWithFeature_IF_OMOD_QUERIES
ENDqueryEtryPt


BEGINmodInit()
CODESTARTmodInit
    /* we only support the current interface specification */
    *ipIFVersProvided = CURR_MOD_IF_VERSION;
CODEmodInit_QueryRegCFSLineHdlr
    CHKiRet(objUse(errmsg, CORE_COMPONENT));
    CHKiRet(objUse(glbl, CORE_COMPONENT));
    CHKiRet(objUse(prop, CORE_COMPONENT));
    CHKiRet(objUse(ruleset, CORE_COMPONENT));
ENDmodInit