forked from stripydog/kplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kplex.c
1795 lines (1599 loc) · 52.4 KB
/
kplex.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
/* kplex: An anything to anything boat data multiplexer for Linux
* Currently this program only supports nmea-0183 data.
* For currently supported interfaces see kplex_mods.h
* Copyright Keith Young 2012-2017
* For copying information, see the file COPYING distributed with this file
*/
/* This file (kplex.c) contains the main body of the program and
* central multiplexing engine. Initialisation and read/write routines are
* defined in interface-specific files
*/
#include "kplex.h"
#include "kplex_mods.h"
#include "version.h"
#include <signal.h>
#include <pwd.h>
#include <time.h>
#include <syslog.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <inttypes.h>
#include <fcntl.h>
/* Macro to identify kplex Proprietary sentences */
#define isprop(sptr) (sptr->len >= 7 && sptr->data[1] == 'P' && sptr->data[2] == 'K' && sptr->data[3] == 'P' && sptr->data[4] == 'X')
/* Globals. Sadly. Used in signal handlers so few other simple options */
pthread_key_t ifkey; /* Key for Thread local pointer to interface struct */
pthread_t reaper; /* tid of thread responsible for reaping */
int timetodie=0; /* Set on receipt of SIGTERM or SIGINT */
time_t graceperiod=3; /* Grace period for unsent data before shutdown (secs)*/
int debuglevel=0; /* debug off by default */
/* Signal handler for SIGUSR1 used by interface threads. Note that this is
* highly dubious: pthread_exit() is not async safe. No associated problems
* reported so far and if they do occur they should occur on exit, but this
* will be changed in the next release
*/
void terminate(int sig)
{
pthread_exit((void *)&sig);
}
/* Sleep function not relying on SIGALRM for thread safety
* Unnecessary on many platforms but here to minimise portability issues
* Could do this with nanosleep() or select()
*/
int mysleep(time_t sleepytime)
{
struct timespec rqtp;
rqtp.tv_sec = sleepytime;
rqtp.tv_nsec=0;
return (nanosleep(&rqtp,NULL));
}
/* functions */
/*
* Check an NMEA 0183 checksum
* Args: pointer to struct senblk
* Returns: 0 if checksum matches checksum field, -1 otherwise
*
*/
int checkcksum (senblk_t *sptr)
{
int cksm=0;
int rcvdcksum=0,i,end;
char *ptr;
for(i=0,end=sptr->len-6,ptr=sptr->data+1; i < end; ptr++,i++)
cksm ^= *ptr;
if (*ptr != '*')
return -1;
for (i=0,++ptr;i<2;i++,ptr++) {
if (*ptr>47 && *ptr<58)
rcvdcksum+=*ptr-48;
else if (*ptr>64 && *ptr<71)
rcvdcksum+=*ptr-55;
else if (*ptr>96 && *ptr<103)
rcvdcksum+=*ptr-87;
if (!i)
rcvdcksum<<=4;
}
if (cksm == rcvdcksum)
return (0);
else
return(-1);
}
/*
* Perform filtering on sentences
* Args: senblk to be filtered, pointer to filter
* Returns: 0 if contents of senblk passes filter, -1 otherwise
*/
int senfilter(senblk_t *sptr, sfilter_t *filter)
{
unsigned int mask = (unsigned int) -1 ^ IDMINORMASK;
sf_rule_t *fptr;
char *cptr;
int i;
time_t tsecs;
struct timeval tv;
/* We shouldn't actually be filtering any NULL packets, but check anyway */
if (sptr == NULL || filter == NULL || filter->rules == NULL)
return(0);
/* inputs should have ensured all sentences ended with \r\n so if we check
* for \r here, we only have to check for \r in the for loops, not \n too */
if (*sptr->data == '\r')
return(1);
for (fptr=filter->rules;fptr;fptr=fptr->next) {
if ((fptr->src.id) && (fptr->src.id != (sptr->src&mask)))
continue;
for (i=0,cptr=sptr->data+1;i<5 && *cptr != '\r';i++,cptr++)
if(fptr->match[i] && fptr->match[i] != *cptr)
break;
if (i!=5)
continue;
if (fptr->type == ACCEPT) {
return(0);
}
if (fptr->type == DENY) {
return(-1);
}
/* type is limit. Hopefully. */
(void) gettimeofday(&tv,NULL);
if (tv.tv_sec < fptr->info.limit->timeout)
return(-1);
if ((tsecs=(tv.tv_sec - fptr->info.limit->timeout)) <
fptr->info.limit->last.tv_sec)
return(-1);
if (tsecs == fptr->info.limit->last.tv_sec &&
(tv.tv_usec < fptr->info.limit->last.tv_usec ))
return(-1);
/* at least timeout since last seen: Update info and pass */
memcpy(&fptr->info.limit->last,&tv,sizeof(struct timeval));
return(0);
}
return(0);
}
/*
* Free a failover rule and any attached source list
* Args: Pointer to rule structure
* Returns: Nothing
*/
void free_srclist(struct srclist *src)
{
struct srclist *tsrc;
for (;src;src=tsrc) {
tsrc=src->next;
if (src->src.name)
free(src->src.name);
free(src);
}
}
/*
* Free a filter
* Args: pointer to filter to be freed
* Returns: Nothing
*/
void free_filter(sfilter_t *fptr)
{
sf_rule_t *rptr,*trptr;
if (fptr == NULL)
return;
pthread_mutex_lock(&fptr->lock);
if (--fptr->refcount) {
pthread_mutex_unlock(&fptr->lock);
return;
}
if (fptr->rules)
for (rptr=fptr->rules;rptr;rptr=trptr) {
trptr=rptr->next;
if (fptr->type == FAILOVER)
free_srclist(rptr->info.source);
free(rptr);
}
free(fptr);
}
/*
* Add a failover source to a failover rule
* Args: address of head of the source list and new source item
* Returns: Nothing
* Side Effect: source item linked into source list according to failover time
*/
void link_src_to_rule (struct srclist **list, struct srclist *src)
{
for (;*list;list=&(*list)->next)
if ((*list)->failtime > src->failtime)
break;
src->next=(*list);
*list=src;
}
/*
* Test if a sentence came from a failover input that is active
* Args: Pointer to filter head, pointer to senblk to be tested
* Returns: 1 if senblk should be passed, 0 if not
*/
int isactive(sfilter_t *filter,senblk_t *sptr)
{
time_t now=time(NULL);
unsigned int mask = (unsigned int) -1 ^ IDMINORMASK;
unsigned int src;
char *cptr,*mptr;
sf_rule_t *rule;
struct srclist *rptr;
time_t last;
int i;
if (filter == NULL || sptr == NULL)
return(1);
src = sptr->src & mask;
for(rule=filter->rules;rule;rule=rule->next) {
for (i=0,cptr=sptr->data+1,mptr=rule->match;i<5;i++,cptr++,mptr++)
if(*mptr && *cptr != *mptr)
break;
if (i == 5)
break;
}
if (!rule)
return(1);
for (last=0,rptr=rule->info.source;rptr;rptr=rptr->next) {
if (rptr->src.id == src) {
rptr->lasttime = now;
if (last+rptr->failtime < now)
return(1);
else
return(0);
}
if (rptr->lasttime > last)
last = rptr->lasttime;
}
return(0);
}
/*
* Add a failover specification
* Args: address of ofilter pointer and pointer to string containing failover
* spec
* Returns: 0 on success, -1 on error
* Side effects: New Failover added to ofilter
*/
int addfailover(sfilter_t **head,char *spec)
{
sf_rule_t *newrule;
struct srclist *src;
char *cptr,*nptr;
int n,done;
time_t now;
if ((newrule=(sf_rule_t *)malloc(sizeof(sf_rule_t))) == NULL) {
return(-1);
}
newrule->info.source=NULL;
for (errno=0,cptr=spec,n=0;n<5;spec++,n++,cptr++) {
if (!*cptr || *cptr== ':') {
free(newrule);
return(-1);
}
newrule->match[n] = (*cptr == '*')?0:*cptr;
}
if (*cptr++ != ':') {
free(newrule);
return(-1);
}
for (now=time(NULL),done=0;!done && *cptr;src=NULL,cptr++) {
if ((src=(struct srclist *)malloc(sizeof(struct srclist))) == NULL) {
free(newrule);
return(-1);
}
for(src->failtime=0;*cptr && *cptr >= '0' && *cptr <= '9';cptr++)
src->failtime=src->failtime*10+(*cptr - '0');
if (*cptr++ != ':')
break;
for(nptr=cptr;*cptr && *cptr != ':';)
cptr++;
if (*cptr)
*cptr='\0';
else
done++;
if ((src->src.name = strdup(nptr)) == NULL) {
logerr(errno,"Failed to allocate memory for string duplication");
free(newrule);
return(-1);
}
src->lasttime=now;
link_src_to_rule(&newrule->info.source,src);
}
if (done) {
if (!*head) {
if (((*head)=(sfilter_t *)malloc(sizeof(sfilter_t)))) {
(*head)->type=FAILOVER;
(*head)->refcount=1;
pthread_mutex_init(&(*head)->lock,NULL);
(*head)->rules=NULL;
}
}
if (*head) {
newrule->next=(*head)->rules;
(*head)->rules=newrule;
return(0);
}
}
if (src)
free(src);
if (newrule->info.source)
free_srclist(newrule->info.source);
free(newrule);
return(-1);
}
/*
* Exit function used by interface handlers. Interface objects are cleaned
* up by the destructor functions of thread local storage
* Args: exit status (unused)
* Returns: Nothing
*/
void iface_thread_exit(int ret)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set,SIGUSR1);
pthread_sigmask(SIG_BLOCK,&set,NULL);
pthread_exit((void *)&ret);
}
/*
* Initialise an ioqueue
* Args: iface_t to add queue to, size of queue (in senblk structures)
* Returns: 0 on success, -1 on failure
*/
int init_q(iface_t *ifa, size_t size)
{
ioqueue_t *newq;
senblk_t *sptr;
int i;
if ((newq=(ioqueue_t *)malloc(sizeof(ioqueue_t))) == NULL)
return(-1);
if ((newq->base=(senblk_t *)calloc(size,sizeof(senblk_t))) ==NULL) {
i=errno;
free(newq);
errno=i;
return(-1);
}
/* "base" always points to the allocated memory so that we can free() it.
* All senblks initially allocated to the free list
*/
newq->free=newq->base;
/* Initiailise senblk queue pointers */
for (i=0,sptr=newq->free,--size;i<size;++i,++sptr)
sptr->next=sptr+1;
sptr->next = NULL;
newq->qhead = newq->qtail = NULL;
newq->owner=ifa;
pthread_mutex_init(&newq->q_mutex,NULL);
pthread_cond_init(&newq->freshmeat,NULL);
newq->active=1;
ifa->q=newq;
return(0);
}
/*
* Copy information in a senblk structure (data and len only)
* Args: pointers to dest and source senblk structures
* Returns: pointer to dest senblk
*/
senblk_t *senblk_copy(senblk_t *dptr,senblk_t *sptr)
{
dptr->len=sptr->len;
dptr->src=sptr->src;
dptr->next=NULL;
return (senblk_t *) memcpy((void *)dptr->data,(const void *)sptr->data,
sptr->len);
}
/*
* Add an senblk to an ioqueue
* Args: Pointer to senblk and Pointer to queue it is to be added to
* Returns: None
*/
void push_senblk(senblk_t *sptr, ioqueue_t *q)
{
senblk_t *tptr;
pthread_mutex_lock(&q->q_mutex);
if (sptr == NULL) {
/* NULL senblk pointer is magic "off" switch for a queue */
q->active = 0;
} else {
/* Get a senblk from the queue's free list if possible...*/
if (q->free) {
tptr=q->free;
q->free=q->free->next;
} else {
/* ...if not steal from the head of the queue, dropping previous
contents. */
tptr=q->qhead;
q->qhead=q->qhead->next;
if (q->drops < 0)
q->drops++;
DEBUG(4,"Dropped senblk q=%s",(q->owner->name)?q->owner->name:"(unknown)");
}
(void) senblk_copy(tptr,sptr);
/* If there is anything on the queue already, set it's "next" member
to point to the new senblk */
if (q->qtail)
q->qtail->next=tptr;
/* Set tail pointer to the new senblk */
q->qtail=tptr;
/* queue head needs to point to new senblk if there was nothing
previously on the queue */
if (q->qhead == NULL)
q->qhead=tptr;
}
pthread_cond_broadcast(&q->freshmeat);
pthread_mutex_unlock(&q->q_mutex);
}
/*
* Get the next senblk from the head of a queue
* Args: Queue to retrieve from
* Returns: Pointer to next senblk on the queue or NULL if the queue is
* no longer active
* This function blocks until data are available or the queue is shut down
*/
senblk_t *next_senblk(ioqueue_t *q)
{
senblk_t *tptr;
pthread_mutex_lock(&q->q_mutex);
while ((tptr = q->qhead) == NULL) {
/* No data available for reading */
if (!q->active) {
/* Return NULL if the queue has been shut down */
pthread_mutex_unlock(&q->q_mutex);
return ((senblk_t *)NULL);
}
/* Wait until something is available */
pthread_cond_wait(&q->freshmeat,&q->q_mutex);
}
/* set qhead to next element (which may be NULL)
If the last element in the queue, set the tail pointer to NULL too */
if ((q->qhead=tptr->next) == NULL)
q->qtail=NULL;
pthread_mutex_unlock(&q->q_mutex);
return(tptr);
}
/*
* Get the last senblk from a queue, discarding all before it
* Args: Queue to retrieve from
* Returns: Pointer to last senblk on the queue or NULL if the queue is
* no longer active
* This function blocks until data are available or the queue is shut down
*/
senblk_t *last_senblk(ioqueue_t *q)
{
senblk_t *tptr,*nptr;
pthread_mutex_lock(&q->q_mutex);
/* Move all but last senblk on the queue to the free list */
if ((tptr=q->qhead) != NULL) {
for (nptr=tptr->next;nptr;tptr=nptr,nptr=nptr->next) {
tptr->next=q->free;
q->free=tptr;
}
q->qhead=tptr;
}
while ((tptr = q->qhead) == NULL) {
/* No data available for reading */
if (!q->active) {
/* Return NULL if the queue has been shut down */
pthread_mutex_unlock(&q->q_mutex);
return ((senblk_t *)NULL);
}
/* Wait until something is available */
pthread_cond_wait(&q->freshmeat,&q->q_mutex);
}
/* set qhead to next element (which may be NULL)
If the last element in the queue, set the tail pointer to NULL too */
if ((q->qhead=tptr->next) == NULL)
q->qtail=NULL;
pthread_mutex_unlock(&q->q_mutex);
return(tptr);
}
/*
* Flush a queue, returning anything on it to the free list
* Args: Queue to be flushed
* Returns: Nothing
* Side Effect: Returns anything on the queue to the free list
*/
void flush_queue(ioqueue_t *q)
{
pthread_mutex_lock(&q->q_mutex);
if (q->qhead != NULL) {
q->qtail->next = q->free;
q->free=q->qhead;
q->qhead=q->qtail=NULL;
}
pthread_mutex_unlock(&q->q_mutex);
}
/*
* Return a senblk to a queue's free list
* Args: pointer to senblk, and pointer to the queue whose free list it is to
* be added to
* Returns: Nothing
*/
void senblk_free(senblk_t *sptr, ioqueue_t *q)
{
pthread_mutex_lock(&q->q_mutex);
/* Adding to head of free list is quicker than tail */
sptr->next = q->free;
q->free=sptr;
pthread_mutex_unlock(&q->q_mutex);
}
iface_t *get_default_global()
{
iface_t *ifp;
struct if_engine *ifg;
if ((ifp = (iface_t *) malloc(sizeof(iface_t))) == NULL)
return(NULL);
memset((void *) ifp,0,sizeof(iface_t));
ifp->type = GLOBAL;
ifp->options = NULL;
if ((ifg = (struct if_engine *)malloc(sizeof(struct if_engine))) == NULL) {
free(ifp);
return(NULL);
}
ifg->flags=0;
ifg->logto=LOG_DAEMON;
ifp->strict=-1;
ifp->checksum=0;
ifp->info = (void *)ifg;
return(ifp);
}
/* Process proprietary sentence. Anything starting $PKPX
* Args: senblk_t * containing sentence, iface_t pointing to engine.
* currently unused but we may use it later for adding to the engine's queue
* Returns -1 if sentence is unrecognised or invalid, 0 if processing
* should continue with current senblk, 1 if this senblk should be dropped
* but sentence was valid
* Side effects: Swaps Query with Response where appropriate
*/
int process_prop(senblk_t *sptr, iface_t *eptr)
{
if (sptr->data[6] != ',')
return(-1);
switch (sptr->data[5]) {
case 'Q':
/* Query Sentence */
if (sptr->data[7] == 'V') {
sptr->len=sprintf(sptr->data,"$PKPXR,%s",VERSION);
} else
return -1;
break;
case 'C':
/* Command: None currently defined */
case 'R':
/* Response: shouldn't get this */
default:
return -1;
}
sptr->len+=sprintf(sptr->data+sptr->len,"*%02X\r\n",
calcsum(sptr->data+1,sptr->len-1));
sptr->src=0;
return(0);
}
/*
* This is the heart of the multiplexer. All inputs add to the tail of the
* Engine's queue. The engine takes from the head of its queue and copies
* to all outputs on its output list.
* Args: Pointer to information structure (iface_t, cast to void)
* Returns: Nothing
*/
void *run_engine(void *info)
{
senblk_t *sptr;
iface_t *optr;
iface_t *eptr = (iface_t *)info;
int retval=0;
(void) pthread_detach(pthread_self());
for (;;) {
sptr = next_senblk(eptr->q);
if (sptr==NULL)
/* Queue has been marked inactive */
break;
if (isprop(sptr)) {
if (process_prop(sptr,eptr)) {
senblk_free(sptr,eptr->q);
continue;
}
}
if (isactive(eptr->ofilter,sptr)) {
pthread_mutex_lock(&eptr->lists->io_mutex);
/* Traverse list of outputs and push a copy of senblk to each */
for (optr=eptr->lists->outputs;optr;optr=optr->next) {
if ((optr->q) && ((!sptr) ||
((sptr->src != optr->id) || (flag_test(optr,F_LOOPBACK))))) {
push_senblk(sptr,optr->q);
}
}
pthread_mutex_unlock(&eptr->lists->io_mutex);
}
senblk_free(sptr,eptr->q);
}
pthread_exit(&retval);
}
/*
* Start processing an interface and add it to an iolist, input or output,
* depending on direction
* Args: Pointer to interface structure (cast to void *)
* Returns: Nothing
* We should come into this with SIGUSR1 blocked
*/
void start_interface(void *ptr)
{
iface_t *ifa = (iface_t *)ptr;
iface_t **lptr;
iface_t **iptr;
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
pthread_mutex_lock(&ifa->lists->io_mutex);
ifa->tid = pthread_self();
if (pthread_setspecific(ifkey,ptr)) {
perror("Falied to set key");
exit(1);
}
for (iptr=&ifa->lists->initialized;*iptr!=ifa;iptr=&(*iptr)->next)
if (*iptr == NULL) {
perror("interface does not exist on initialized list!");
exit(1);
}
*iptr=(*iptr)->next;
/* We've unlinked from initialized. Exit if we've been told to already */
if (ifa->direction == NONE) {
pthread_mutex_unlock(&ifa->lists->io_mutex);
iface_thread_exit(0);
}
/* Set lptr to point to the input or output list, as appropriate */
lptr=(ifa->direction==IN)?&ifa->lists->inputs:&ifa->lists->outputs;
if (*lptr)
ifa->next=(*lptr);
else
ifa->next=NULL;
(*lptr)=ifa;
if (ifa->lists->initialized == NULL)
pthread_cond_broadcast(&ifa->lists->init_cond);
else
while (ifa->lists->initialized)
pthread_cond_wait(&ifa->lists->init_cond,&ifa->lists->io_mutex);
pthread_mutex_unlock(&ifa->lists->io_mutex);
pthread_sigmask(SIG_UNBLOCK,&set,NULL);
if (ifa->direction == IN) {
ifa->read(ifa);
} else
ifa->write(ifa);
}
/*
* link an interface into the initialized list
* Args: interface structure pointer
* Returns: 0 on success. There is no failure condition
* Side Effects: links interface to the initialized list
*/
int link_to_initialized(iface_t *ifa)
{
iface_t **iptr;
pthread_mutex_lock(&ifa->lists->io_mutex);
for (iptr=&ifa->lists->initialized;(*iptr);iptr=&(*iptr)->next);
(*iptr)=ifa;
ifa->next=NULL;
pthread_mutex_unlock(&ifa->lists->io_mutex);
return(0);
}
/*
* Free all the data associated with an interface except the iface_t itself
* Args: Pointer to iface_t to be freed
* Returns: Nothing
* Side Effects: Cleanup routines invoked, de-coupled from any pair, all data
* other than the main interface structure is freed
* Because of dealing with the pair, the io_mutex should be locked before
* involing this routine
*/
void free_if_data(iface_t *ifa)
{
if ((ifa->direction == OUT) && ifa->q) {
/* output interfaces have queues which need freeing */
free(ifa->q->base);
free(ifa->q);
}
free_filter(ifa->ifilter);
free_filter(ifa->ofilter);
if (ifa->info) {
if (ifa->cleanup)
ifa->cleanup(ifa);
free(ifa->info);
}
if (ifa->pair) {
ifa->pair->pair=NULL;
if (ifa->pair->direction == OUT) {
pthread_mutex_lock(&ifa->pair->q->q_mutex);
ifa->pair->q->active=0;
pthread_cond_broadcast(&ifa->pair->q->freshmeat);
pthread_mutex_unlock(&ifa->pair->q->q_mutex);
} else {
if (ifa->pair->tid)
pthread_kill(ifa->pair->tid,SIGUSR1);
else
ifa->pair->direction = NONE;
}
} else
if (ifa->name && !(ifa->id & IDMINORMASK)) {
free(ifa->name);
}
}
/*
* Take an interface off the input or output iolist and place it on the "dead"
* list waiting to be cleaned up
* Args: Pointer to interface structure
* Returns: 0 on success. Might add other possible return vals later
* Should this be broken into link from input/output then link to dead?
*/
int unlink_interface(iface_t *ifa)
{
iface_t **lptr;
iface_t *tptr;
if (ifa->direction != NONE) {
/* Set lptr to point to the input or output list, as appropriate */
lptr=(ifa->direction==IN)?&ifa->lists->inputs:&ifa->lists->outputs;
if ((*lptr) == ifa) {
/* If target interface is the head of the list, set the list pointer
to point to the next interface in the list */
(*lptr)=(*lptr)->next;
} else {
/* Traverse the list until we find the interface before our target and
make its next pointer point to the element after our target */
for (tptr=(*lptr);tptr->next != ifa;tptr=tptr->next);
tptr->next = ifa->next;
}
if (ifa->direction != OUT)
if (!ifa->lists->inputs) {
for(tptr=ifa->lists->outputs;tptr;tptr=tptr->next)
if (tptr->direction == BOTH)
break;
if (tptr == NULL) {
pthread_mutex_lock(&ifa->lists->engine->q->q_mutex);
ifa->lists->engine->q->active=0;
pthread_cond_broadcast(&ifa->lists->engine->q->freshmeat);
pthread_mutex_unlock(&ifa->lists->engine->q->q_mutex);
if (timetodie == 0)
timetodie++;
}
}
}
free_if_data(ifa);
/* Add to the dead list */
if ((tptr=ifa->lists->dead) == NULL)
ifa->lists->dead=ifa;
else {
for(;tptr->next;tptr=tptr->next);
tptr->next=ifa;
}
ifa->next=NULL;
return(0);
}
/*
* Cleanup routine for interfaces, used as destructor for pointer to interface
* structure in the handler thread's local storage
* Args: pointer to interface structure
* Returns: Nothing
*/
void iface_destroy(void *ifptr)
{
iface_t *ifa = (iface_t *) ifptr;
DEBUG(3,"Cleaning up data for exiting %s %s %s id %x",
(ifa->direction == IN)?"input":"output",(ifa->id & IDMINORBITS)?
"connection":"interface",ifa->name,ifa->id);
sigset_t set,saved;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &set, &saved);
pthread_mutex_lock(&ifa->lists->io_mutex);
if (ifa->tid) {
unlink_interface(ifa);
/* Signal the reaper thread */
(void) pthread_kill(reaper,SIGUSR2);
} else
free_if_data(ifa);
pthread_mutex_unlock(&ifa->lists->io_mutex);
pthread_sigmask(SIG_SETMASK,&saved,NULL);
}
/*
* add a filter to an interface
* Args: pointer to filter to be added
* Returns: pointer to filter to be added
*/
sfilter_t *addfilter(sfilter_t *filter)
{
if (!filter)
return (NULL);
pthread_mutex_lock(&filter->lock);
++(filter->refcount);
pthread_mutex_unlock(&filter->lock);
return(filter);
}
/*
* Duplicate an interface
* Used when creating IN/OUT pair for bidirectional communication
* Args: pointer to interface to be duplicated
* Returns: Pointer to duplicate interface
*/
iface_t *ifdup (iface_t *ifa)
{
iface_t *newif;
if ((newif=(iface_t *) malloc(sizeof(iface_t))) == (iface_t *) NULL)
return(NULL);
if (iftypes[ifa->type].ifdup_func) {
if ((newif->info=(*iftypes[ifa->type].ifdup_func)(ifa->info)) == NULL) {
free(newif);
return(NULL);
}
} else
newif->info = NULL;
ifa->pair=newif;
newif->tid=ifa->tid;
newif->flags=ifa->flags;
newif->id=ifa->id;
newif->name=ifa->name;
newif->pair=ifa;
newif->next=NULL;
newif->type=ifa->type;
newif->lists=ifa->lists;
newif->read=ifa->read;
newif->readbuf=ifa->readbuf;
newif->write=ifa->write;
newif->cleanup=ifa->cleanup;
newif->options=NULL;
newif->ifilter=addfilter(ifa->ifilter);
newif->ofilter=addfilter(ifa->ofilter);
newif->checksum=ifa->checksum;
newif->strict=ifa->strict;
return(newif);
}
/*
* Return the path to the kplex config file
* Args: None
* Returns: pointer to name of config file
*
* First choice is conf file in user's home directory, seocnd is global
*/
char *get_def_config()
{
char *confptr;
char *buf;
struct passwd *pw;
if ((confptr=getenv("KPLEXCONF")))
return (confptr);
if ((confptr=getenv("HOME")) == NULL)
if ((pw=getpwuid(getuid())))
confptr=pw->pw_dir;
if (confptr) {
if ((buf = malloc(strlen(confptr)+strlen(KPLEXHOMECONF)+2)) == NULL) {
perror("failed to allocate memory");
exit(1);
}
strcpy(buf,confptr);
strcat(buf,"/");
#ifdef KPLEXHOMECONFOSX
/*
* Deprecate OSX specific config file. This seemed like a good idea at the time
* but has no apparent advantage over ~/.kplex.conf
*/
int doosxconf=1;
size_t osxlen,baselen;
if ((osxlen=strlen(KPLEXHOMECONFOSX)) <
(baselen=strlen(KPLEXOLDHOMECONFOSX))) {
osxlen=baselen;
}
if ((strlen(KPLEXHOMECONF)) < osxlen) {
if (realloc(buf,(baselen=strlen(buf))+osxlen+1) == NULL) {
perror("Can't query OSX config file");
doosxconf=0;
}
}
if (doosxconf) {
strcat(buf,KPLEXOLDHOMECONFOSX);
if (!access(buf,F_OK)) {
logwarn("Use of %s is deprecated for kplex config.\nPlease move this file to ~/%s to suppress this warning",buf,KPLEXHOMECONF);
return(buf);
}
strcpy(buf+baselen,KPLEXHOMECONFOSX);
if (!access(buf,F_OK)) {
return(buf);
}
buf[baselen]='\0';
}
#endif
strcat(buf,KPLEXHOMECONF);
if (!access(buf,F_OK))
return(buf);
free(buf);
}
if (!access(KPLEXGLOBALCONF,F_OK))
return(KPLEXGLOBALCONF);