-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindCIMXMLHandler.c
1028 lines (916 loc) · 32.4 KB
/
indCIMXMLHandler.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
/*
* indCIMXMLHandler.c
*
* © Copyright IBM Corp. 2005, 2007
*
* THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
* ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
* CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
*
* You can obtain a current copy of the Eclipse Public License from
* http://www.opensource.org/licenses/eclipse-1.0.php
*
* Author: Adrian Schuur <[email protected]>
*
* Description:
*
* CIM XML handler for indications.
*
*/
#include "cmpi/cmpidt.h"
#include "cmpi/cmpift.h"
#include "cmpi/cmpimacs.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "trace.h"
#include "fileRepository.h"
#include "providerMgr.h"
#include "internalProvider.h"
#include "cimRequest.h"
#include "native.h"
#include "control.h"
#include "instance.h"
#include "support.h"
extern void closeProviderContext(BinRequestContext * ctx);
extern int exportIndication(char *url, char *payload, char **resp,
char **msg);
extern void dumpSegments(void *);
extern UtilStringBuffer *segments2stringBuffer(RespSegment * rs);
extern UtilStringBuffer *newStringBuffer(int);
extern void setStatus(CMPIStatus *st, CMPIrc rc, char *msg);
extern ExpSegments exportIndicationReq(CMPIInstance *ci, char *id);
extern void memLinkObjectPath(CMPIObjectPath * op);
static const CMPIBroker *_broker;
static int
interOpNameSpace(const CMPIObjectPath * cop, CMPIStatus *st)
{
char *ns = (char *) CMGetNameSpace(cop, NULL)->hdl;
if (strcasecmp(ns, "root/interop")) {
setStatus(st, CMPI_RC_ERR_FAILED,
"Object must reside in root/interop");
return 0;
}
return 1;
}
int RIEnabled=-1;
/*
* ------------------------------------------------------------------ *
* Instance MI Cleanup
* ------------------------------------------------------------------
*/
CMPIStatus
IndCIMXMLHandlerCleanup(CMPIInstanceMI * mi, const CMPIContext *ctx,
CMPIBoolean terminating)
{
CMPIStatus st = { CMPI_RC_OK, NULL };
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerCleanup");
_SFCB_RETURN(st);
}
/*
* ------------------------------------------------------------------ *
* Instance MI Functions
* ------------------------------------------------------------------
*/
static CMPIContext *
prepareUpcall(CMPIContext *ctx)
{
/*
* used to invoke the internal provider in upcalls, otherwise we will *
* * be routed here (interOpProvider) again
*/
CMPIContext *ctxLocal;
ctxLocal = native_clone_CMPIContext(ctx);
CMPIValue val;
val.string = sfcb_native_new_CMPIString("$DefaultProvider$", NULL, 0);
ctxLocal->ft->addEntry(ctxLocal, "rerouteToProvider", &val, CMPI_string);
return ctxLocal;
}
CMPIStatus
IndCIMXMLHandlerEnumInstanceNames(CMPIInstanceMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * ref)
{
CMPIStatus st;
CMPIEnumeration *enm;
CMPIContext *ctxLocal;
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerEnumInstanceNames");
if (interOpNameSpace(ref, &st) != 1)
_SFCB_RETURN(st);
ctxLocal = prepareUpcall((CMPIContext *) ctx);
#ifdef HAVE_OPTIMIZED_ENUMERATION
CMPIString *cn;
CMPIObjectPath *refLocal;
cn = CMGetClassName(ref, &st);
if (strcasecmp(CMGetCharPtr(cn), "cim_listenerdestination") == 0) {
enm =
_broker->bft->enumerateInstanceNames(_broker, ctxLocal, ref, &st);
while (enm && enm->ft->hasNext(enm, &st)) {
CMReturnObjectPath(rslt, (enm->ft->getNext(enm, &st)).value.ref);
}
refLocal =
CMNewObjectPath(_broker, "root/interop",
"cim_listenerdestinationcimxml", &st);
enm =
_broker->bft->enumerateInstanceNames(_broker, ctxLocal, refLocal,
&st);
while (enm && enm->ft->hasNext(enm, &st)) {
CMReturnObjectPath(rslt, (enm->ft->getNext(enm, &st)).value.ref);
}
refLocal =
CMNewObjectPath(_broker, "root/interop",
"cim_indicationhandlercimxml", &st);
enm =
_broker->bft->enumerateInstanceNames(_broker, ctxLocal, refLocal,
&st);
while (enm && enm->ft->hasNext(enm, &st)) {
CMReturnObjectPath(rslt, (enm->ft->getNext(enm, &st)).value.ref);
}
CMRelease(refLocal);
} else {
enm =
_broker->bft->enumerateInstanceNames(_broker, ctxLocal, ref, &st);
while (enm && enm->ft->hasNext(enm, &st)) {
CMReturnObjectPath(rslt, (enm->ft->getNext(enm, &st)).value.ref);
}
}
#else
enm = _broker->bft->enumerateInstanceNames(_broker, ctxLocal, ref, &st);
while (enm && enm->ft->hasNext(enm, &st)) {
CMReturnObjectPath(rslt, (enm->ft->getNext(enm, &st)).value.ref);
}
#endif
CMRelease(ctxLocal);
if (enm)
CMRelease(enm);
_SFCB_RETURN(st);
}
void
filterInternalProps(CMPIInstance* ci)
{
CMPIStatus pst = { CMPI_RC_OK, NULL };
CMGetProperty(ci, "LastSequenceNumber", &pst);
/* prop is set, need to clear it out */
if (pst.rc != CMPI_RC_ERR_NOT_FOUND) {
filterFlagProperty(ci, "LastSequenceNumber");
}
CMGetProperty(ci, "SequenceContext", &pst);
/* prop is set, need to clear it out */
if (pst.rc != CMPI_RC_ERR_NOT_FOUND) {
filterFlagProperty(ci, "SequenceContext");
}
return;
}
extern int isChild(const char *ns, const char *parent,
const char *child);
static int
isa(const char *sns, const char *child, const char *parent)
{
int rv;
_SFCB_ENTER(TRACE_INDPROVIDER, "isa");
if (strcasecmp(child, parent) == 0)
return 1;
rv = isChild(sns, parent, child);
_SFCB_RETURN(rv);
}
CMPIStatus
IndCIMXMLHandlerEnumInstances(CMPIInstanceMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * ref,
const char **properties)
{
CMPIStatus st;
CMPIEnumeration *enm;
CMPIContext *ctxLocal;
CMPIInstance* ci;
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerEnumInstances");
if (interOpNameSpace(ref, &st) != 1)
_SFCB_RETURN(st);
ctxLocal = prepareUpcall((CMPIContext *) ctx);
#ifdef HAVE_OPTIMIZED_ENUMERATION
CMPIString *cn;
CMPIObjectPath *refLocal;
cn = CMGetClassName(ref, &st);
if (strcasecmp(CMGetCharPtr(cn), "cim_listenerdestination") == 0) {
enm =
_broker->bft->enumerateInstances(_broker, ctxLocal, ref,
properties, &st);
while (enm && enm->ft->hasNext(enm, &st)) {
ci=(enm->ft->getNext(enm, &st)).value.inst;
filterInternalProps(ci);
CMReturnInstance(rslt, ci);
}
refLocal =
CMNewObjectPath(_broker, "root/interop",
"cim_listenerdestinationcimxml", &st);
enm =
_broker->bft->enumerateInstances(_broker, ctxLocal, refLocal,
properties, &st);
while (enm && enm->ft->hasNext(enm, &st)) {
ci=(enm->ft->getNext(enm, &st)).value.inst;
filterInternalProps(ci);
CMReturnInstance(rslt, ci);
}
refLocal =
CMNewObjectPath(_broker, "root/interop",
"cim_indicationhandlercimxml", &st);
enm =
_broker->bft->enumerateInstances(_broker, ctxLocal, refLocal,
properties, &st);
while (enm && enm->ft->hasNext(enm, &st)) {
ci=(enm->ft->getNext(enm, &st)).value.inst;
filterInternalProps(ci);
CMReturnInstance(rslt, ci);
}
CMRelease(refLocal);
} else {
enm =
_broker->bft->enumerateInstances(_broker, ctxLocal, ref,
properties, &st);
while (enm && enm->ft->hasNext(enm, &st)) {
ci=(enm->ft->getNext(enm, &st)).value.inst;
filterInternalProps(ci);
CMReturnInstance(rslt, ci);
}
}
#else
enm =
_broker->bft->enumerateInstances(_broker, ctxLocal, ref, properties,
&st);
while (enm && enm->ft->hasNext(enm, &st)) {
ci=(enm->ft->getNext(enm, &st)).value.inst;
filterInternalProps(ci);
CMReturnInstance(rslt, ci);
}
#endif
CMRelease(ctxLocal);
if (enm)
CMRelease(enm);
_SFCB_RETURN(st);
}
CMPIStatus
IndCIMXMLHandlerGetInstance(CMPIInstanceMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * cop,
const char **properties)
{
CMPIStatus st;
CMPIInstance* ci;
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerGetInstance");
ci = internalProviderGetInstance(cop, &st);
if (st.rc == CMPI_RC_OK) {
if (isa("root/interop", CMGetCharPtr(CMGetClassName(cop,NULL)), "cim_indicationhandler")) {
filterInternalProps(ci);
}
if (properties) {
ci->ft->setPropertyFilter(ci, properties, NULL);
}
CMReturnInstance(rslt, ci);
}
_SFCB_RETURN(st);
}
CMPIStatus
IndCIMXMLHandlerCreateInstance(CMPIInstanceMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * cop,
const CMPIInstance *ci)
{
CMPIStatus st = { CMPI_RC_OK, NULL };
CMPIArgs *in,
*out = NULL;
CMPIObjectPath *op;
unsigned short persistenceType;
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerCreateInstance");
if (interOpNameSpace(cop, &st) == 0)
_SFCB_RETURN(st);
CMPIInstance *ciLocal = CMClone(ci, NULL);
memLinkInstance(ciLocal);
CMPIObjectPath* copLocal = CMClone(cop, NULL);
memLinkObjectPath(copLocal);
setCCN(copLocal,ciLocal,"CIM_ComputerSystem");
internalProviderGetInstance(copLocal, &st);
if (st.rc == CMPI_RC_ERR_FAILED)
_SFCB_RETURN(st);
if (st.rc == CMPI_RC_OK) {
setStatus(&st, CMPI_RC_ERR_ALREADY_EXISTS, NULL);
_SFCB_RETURN(st);
}
CMPIString *sysname=ciLocal->ft->getProperty(ciLocal,"SystemName",&st).value.string;
if (sysname == NULL || sysname->hdl == NULL) {
char hostName[512];
hostName[0]=0;
gethostname(hostName,511); /* should be the same as SystemName of IndicationService */
CMAddKey(copLocal, "SystemName", hostName, CMPI_chars);
CMSetProperty(ciLocal,"SystemName",hostName,CMPI_chars);
}
CMPIString *dest =
CMGetProperty(ciLocal, "destination", &st).value.string;
if (dest == NULL || CMGetCharPtr(dest) == NULL) {
setStatus(&st, CMPI_RC_ERR_FAILED,
"Destination property not found; is required");
CMRelease(ciLocal);
_SFCB_RETURN(st);
} else { /* if no scheme is given, assume http (as
* req. for param by mof) */
char *ds = CMGetCharPtr(dest);
if (strstr(ds, "://") == NULL) {
char *prefix = "http://";
int n = strlen(ds) + strlen(prefix) + 1;
char *newdest = malloc(n * sizeof(*newdest));
strcpy(newdest, prefix);
strcat(newdest, ds);
CMSetProperty(ciLocal, "destination", newdest, CMPI_chars);
free(newdest);
}
}
CMPIData persistence =
CMGetProperty(ciLocal, "persistencetype", &st);
if (persistence.state == CMPI_nullValue
|| persistence.state == CMPI_notFound) {
persistenceType = 2; /* default is 2 = permanent */
} else if (persistence.value.uint16 < 1 || persistence.value.uint16 > 3) {
setStatus(&st, CMPI_RC_ERR_FAILED,
"PersistenceType property must be 1, 2, or 3");
CMRelease(ciLocal);
_SFCB_RETURN(st);
} else {
persistenceType = persistence.value.uint16;
}
CMSetProperty(ciLocal, "persistencetype", &persistenceType, CMPI_uint16);
if (CMClassPathIsA(_broker, copLocal, "cim_listenerdestination", NULL)) {
//get the creation timestamp
struct timeval tv;
struct timezone tz;
char context[100];
gettimeofday(&tv, &tz);
struct tm cttm;
char * gtime = malloc(15 * sizeof(*gtime));
memset(gtime, 0, 15 * sizeof(char));
if (gmtime_r(&tv.tv_sec, &cttm) != NULL) {
strftime(gtime, 15, "%Y%m%d%H%M%S", &cttm);
}
// Even though reliable indications may be disabled, we need to do this
// in case it ever gets enabled.
// Get the IndicationService name
CMPIObjectPath * isop = CMNewObjectPath(_broker, "root/interop", "CIM_IndicationService", NULL);
CMPIEnumeration * isenm = _broker->bft->enumerateInstances(_broker, ctx, isop, NULL, NULL);
CMPIData isinst = CMGetNext(isenm, NULL);
CMPIData mc = CMGetProperty(isinst.value.inst, "Name", NULL);
// build the context string
sprintf (context,"%s#%s#",mc.value.string->ft->getCharPtr(mc.value.string,NULL),gtime);
CMPIValue scontext;
scontext.string = sfcb_native_new_CMPIString(context, NULL, 0);
free(gtime);
// set the properties
CMSetProperty(ciLocal, "SequenceContext", &scontext, CMPI_string);
CMPIValue zarro = {.sint64 = -1 };
CMSetProperty(ciLocal, "LastSequenceNumber", &zarro, CMPI_sint64);
}
_SFCB_TRACE_VAR(CMPIString *str = CDToString(_broker, copLocal, NULL));
_SFCB_TRACE_VAR(CMPIString *ns = CMGetNameSpace(copLocal, NULL));
_SFCB_TRACE(1,("--- handler %s %s", (char *) ns->hdl, (char *) str->hdl));
in = CMNewArgs(_broker, NULL);
CMAddArg(in, "handler", &ciLocal, CMPI_instance);
CMAddArg(in, "key", &copLocal, CMPI_ref);
op = CMNewObjectPath(_broker, "root/interop",
"cim_indicationsubscription", &st);
CBInvokeMethod(_broker, ctx, op, "_addHandler", in, out, &st);
if (st.rc == CMPI_RC_OK) {
st = InternalProviderCreateInstance(NULL, ctx, rslt, copLocal, ciLocal);
}
else {
CBInvokeMethod(_broker,ctx,op,"_removeHandler",in,out,NULL);
}
_SFCB_RETURN(st);
}
/*
* ModifyInstance only for ListenerDestination.Destination
*/
CMPIStatus
IndCIMXMLHandlerModifyInstance(CMPIInstanceMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * cop,
const CMPIInstance *ci,
const char **properties)
{
CMPIStatus st = { CMPI_RC_OK, NULL };
CMPIString *cn = CMGetClassName(cop, NULL);
const char *cns = cn->ft->getCharPtr(cn,NULL);
CMPIArgs *in;
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerModifyInstance");
if(isa("root/interop", cns, "cim_listenerdestination")) {
_SFCB_TRACE(1,("--- modify %s", cns));
CMPIData newDest = CMGetProperty(ci, "Destination", &st);
fprintf(stderr, "new dest is %s\n", CMGetCharPtr(newDest.value.string));
if(newDest.state != CMPI_goodValue) {
st.rc = CMPI_RC_ERR_FAILED;
return st;
}
in=CMNewArgs(_broker,NULL);
CMAddArg(in,"handler",&ci,CMPI_instance);
CMAddArg(in,"key",&cop,CMPI_ref);
/* cn needs to be IndicationSub to route the IM call to interopProv */
CMPIObjectPath* sop=CMNewObjectPath(_broker,"root/interop","cim_indicationsubscription",&st);
CBInvokeMethod(_broker,ctx,sop,"_updateHandler",in,NULL,&st);
if (st.rc==CMPI_RC_OK) {
st=InternalProviderModifyInstance(NULL,ctx,rslt,cop,ci,properties);
}
else {
CBInvokeMethod(_broker,ctx,sop,"_removeHandler",in,NULL,NULL);
}
}
_SFCB_RETURN(st);
}
CMPIStatus
IndCIMXMLHandlerDeleteInstance(CMPIInstanceMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * cop)
{
CMPIStatus st = { CMPI_RC_OK, NULL };
CMPIArgs *in,
*out = NULL;
CMPIObjectPath *op;
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerDeleteInstance");
if (interOpNameSpace(cop, &st) == 0)
_SFCB_RETURN(st);
internalProviderGetInstance(cop, &st);
if (st.rc)
_SFCB_RETURN(st);
in = CMNewArgs(_broker, NULL);
CMAddArg(in, "key", &cop, CMPI_ref);
op = CMNewObjectPath(_broker, "root/interop",
"cim_indicationsubscription", &st);
CBInvokeMethod(_broker, ctx, op, "_removeHandler", in, out, &st);
if (st.rc == CMPI_RC_OK) {
st = InternalProviderDeleteInstance(NULL, ctx, rslt, cop);
}
_SFCB_RETURN(st);
}
CMPIStatus
IndCIMXMLHandlerExecQuery(CMPIInstanceMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * cop,
const char *lang, const char *query)
{
CMPIStatus st = { CMPI_RC_ERR_NOT_SUPPORTED, NULL };
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerExecQuery");
_SFCB_RETURN(st);
}
/*
* ---------------------------------------------------------------------------
*/
/*
* Method Provider Interface
*/
/*
* ---------------------------------------------------------------------------
*/
static int retryRunning = 0;
static pthread_mutex_t RQlock = PTHREAD_MUTEX_INITIALIZER;
pthread_t t;
pthread_attr_t tattr;
CMPIStatus
IndCIMXMLHandlerMethodCleanup(CMPIMethodMI * mi,
const CMPIContext *ctx,
CMPIBoolean terminating)
{
CMPIStatus st = { CMPI_RC_OK, NULL };
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerMethodCleanup");
if (retryRunning == 1) {
_SFCB_TRACE(1, ("--- Stopping indication retry thread"));
pthread_kill(t, SIGUSR2);
// Wait for thread to complete
pthread_join(t, NULL);
_SFCB_TRACE(1, ("--- Indication retry thread stopped"));
}
_SFCB_RETURN(st);
}
/** \brief deliverInd - Sends the indication to the destination
*
* Performs the actual delivery of the indication payload to
* the target destination
*/
int
deliverInd(const CMPIObjectPath * ref, const CMPIArgs * in, CMPIInstance * ind)
{
_SFCB_ENTER(TRACE_INDPROVIDER, "deliverInd");
CMPIInstance *hci;
CMPIStatus st = { CMPI_RC_OK, NULL };
CMPIString *dest;
char strId[64];
ExpSegments xs;
UtilStringBuffer *sb;
static int id = 1;
char *resp;
char *msg;
int rc = 0;
if ((hci = internalProviderGetInstance(ref, &st)) == NULL) {
_SFCB_RETURN(1);
}
dest = CMGetProperty(hci, "destination", NULL).value.string;
_SFCB_TRACE(1, ("--- destination: %s\n", (char *) dest->hdl));
sprintf(strId, "%d", id++);
xs = exportIndicationReq(ind, strId);
sb = segments2stringBuffer(xs.segments);
rc = exportIndication((char*)dest->hdl,
(char*)sb->ft->getCharPtr(sb), &resp, &msg);
RespSegment rs = xs.segments[5];
UtilStringBuffer *usb = (UtilStringBuffer *) rs.txt;
CMRelease(usb);
CMRelease(sb);
if (resp)
free(resp);
if (msg)
free(msg);
_SFCB_RETURN(rc);
}
// Retry queue element and control vars
typedef struct rtelement {
CMPIObjectPath * ref; // LD
CMPIObjectPath * sub; // Subscription
CMPIObjectPath * ind; // indication with key
CMPIObjectPath * SFCBIndEle; // SFCB_indicationelement
CMPIInstance * indInst; // holds actual instance
int count;
time_t lasttry;
unsigned int instanceID;
struct rtelement *next,*prev;
} RTElement;
static RTElement *RQhead,
*RQtail;
/** \brief enqRetry - Add to retry queue
*
* Adds the element to the retry queue
* Initializes the queue if empty
* Adds the current time as the last retry time.
*/
int
enqRetry(RTElement * element, const CMPIContext * ctx, int repo)
{
_SFCB_ENTER(TRACE_INDPROVIDER, "enqRetry");
// Put this one on the retry queue
if (pthread_mutex_lock(&RQlock) != 0) {
// lock failed
return 1;
}
if (RQhead == NULL) {
// Queue is empty
_SFCB_TRACE(1,("--- Adding indication to new retry queue."));
RQhead = element;
RQtail = element;
RQtail->next = element;
RQtail->prev = element;
} else {
_SFCB_TRACE(1,("--- Adding indication to retry queue."));
element->next = RQtail->next;
element->next->prev = element;
RQtail->next = element;
element->prev = RQtail;
RQtail = element;
}
if (pthread_mutex_unlock(&RQlock) != 0) {
// lock failed
return 1;
}
_SFCB_RETURN(0);
}
/** \brief dqRetry - Remove from the retry queue
*
* Removes the element from the retry queue
* Cleans up the queue if empty
*/
int
dqRetry(CMPIContext * ctx, RTElement * cur)
{
_SFCB_ENTER(TRACE_INDPROVIDER, "dqRetry");
// Delete the instance in the repo
// Could use hashtable?
CMPIObjectPath * op=CMNewObjectPath(_broker,"root/interop","SFCB_IndicationElement",NULL);
CMAddKey(op,"IndicationID",&cur->instanceID,CMPI_uint32);
CBDeleteInstance(_broker,ctx,cur->ind);
CMRelease(op);
// Remove the entry from the queue, closing the hole
if (cur->next == cur) {
// queue is empty
free(cur);
RQhead = NULL;
RQtail = NULL;
} else {
// not last
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
/* 3485438-77204 - update qhead/qtail */
if (cur == RQhead) RQhead=cur->next;
if (cur == RQtail) RQtail=cur->prev;
CMRelease(cur->ref);
CMRelease(cur->sub);
if (cur)
free(cur);
}
_SFCB_RETURN(0);
}
static int retryShutdown = 0;
void
handle_sig_retry(int signum)
{
_SFCB_ENTER(TRACE_INDPROVIDER, "handle_sig_retry");
//Indicate provider is shutting down
retryShutdown = 1;
}
/** \brief retryExport - Manages retries
*
* Spawned as a thread when a retry queue exists to
* manage the retry attempts at the specified intervals
* Thread quits when queue is empty.
*/
void *
retryExport(void *lctx)
{
_SFCB_ENTER(TRACE_INDPROVIDER, "retryExport");
CMPIObjectPath *ref;
CMPIArgs *in;
CMPIInstance *sub;
CMPIContext *ctx = (CMPIContext *) lctx;
CMPIContext *ctxLocal;
RTElement *cur,
*purge;
struct timeval tv;
struct timezone tz;
int rint,
maxcount,
ract,
rtint;
CMPIUint64 sfc = 0;
CMPIObjectPath *op;
CMPIEnumeration *isenm = NULL;
//Setup signal handlers
struct sigaction sa;
sa.sa_handler = handle_sig_retry;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR2, &sa, NULL);
CMPIStatus st = { CMPI_RC_OK, NULL };
int rc = 0;
ctxLocal = prepareUpcall(ctx);
// Get the retry params from IndService
op = CMNewObjectPath(_broker, "root/interop", "CIM_IndicationService",
NULL);
isenm = _broker->bft->enumerateInstances(_broker, ctx, op, NULL, NULL);
CMPIData isinst = CMGetNext(isenm, NULL);
CMPIData mc =
CMGetProperty(isinst.value.inst, "DeliveryRetryAttempts", NULL);
CMPIData ri =
CMGetProperty(isinst.value.inst, "DeliveryRetryInterval", NULL);
CMPIData ra =
CMGetProperty(isinst.value.inst, "SubscriptionRemovalAction", NULL);
CMPIData rti =
CMGetProperty(isinst.value.inst, "SubscriptionRemovalTimeInterval",
NULL);
maxcount = mc.value.uint16; // Number of times to retry delivery
rint = ri.value.uint32; // Interval between retries
rtint = rti.value.uint32; // Time to allow sub to keep failing until
ract = ra.value.uint16; // ... this action is taken
// Now, run the queue
sleep(5); //Prevent deadlock on startup when localmode is used.
pthread_mutex_lock(&RQlock);
cur = RQhead;
while (RQhead != NULL) {
if(retryShutdown) break; // Provider shutdown
ref = cur->ref;
// Build the CMPIArgs that deliverInd needs
CMPIInstance *iinst=cur->indInst;
in=CMNewArgs(_broker,NULL);
CMAddArg(in,"indication",&iinst,CMPI_instance);
sub=internalProviderGetInstance(cur->sub,&st);
if (st.rc == CMPI_RC_ERR_NOT_FOUND) {
// sub got deleted, purge this indication and move on
_SFCB_TRACE(1,("--- Subscription for indication gone, deleting indication."));
purge = cur;
cur = cur->next;
dqRetry(ctx,purge);
} else {
// Still valid, retry
gettimeofday(&tv, &tz);
if ((cur->lasttry + rint) > tv.tv_sec) {
_SFCB_TRACE(1,("--- sleeping."));
// no retries are ready, release the lock
// and sleep for an interval, then relock
pthread_mutex_unlock(&RQlock);
sleep(rint);
if(retryShutdown) break; // Provider shutdown
pthread_mutex_lock(&RQlock);
}
rc = deliverInd(ref, in, iinst);
if ((rc == 0) || (cur->count >= maxcount - 1)) {
// either it worked, or we maxed out on retries
// If it succeeded, clear the failtime
if (rc == 0) {
_SFCB_TRACE(1,("--- Indication succeeded."));
sfc = 0;
CMSetProperty(sub, "DeliveryFailureTime", &sfc, CMPI_uint64);
}
// remove from queue in either case
_SFCB_TRACE(1,("--- Indication removed."));
purge = cur;
cur = cur->next;
dqRetry(ctx,purge);
} else {
// still failing, leave on queue
_SFCB_TRACE(1,("--- Indication still failing."));
cur->count++;
gettimeofday(&tv, &tz);
cur->lasttry = tv.tv_sec;
CMPIData sfcp =
CMGetProperty(sub, "DeliveryFailureTime", NULL);
sfc = sfcp.value.uint64;
if (sfc == 0) {
// if the time isn't set, this is the first failure
sfc = tv.tv_sec;
CMSetProperty(sub, "DeliveryFailureTime", &sfc, CMPI_uint64);
CBModifyInstance(_broker, ctxLocal, cur->sub, sub, NULL);
cur = cur->next;
} else if (sfc + rtint < tv.tv_sec) {
// Exceeded subscription removal threshold, if action is:
// 2, delete the sub; 3, disable the sub; otherwise, nothing
if (ract == 2) {
_SFCB_TRACE(1,("--- Subscription threshold reached, deleting."));
CMPIContext *ctxDel = prepareNorespCtx(ctx);
CBDeleteInstance(_broker, ctxDel, cur->sub);
purge = cur;
cur = cur->next;
dqRetry(ctx,purge);
} else if (ract == 3) {
// Set sub state to disable(4)
_SFCB_TRACE(1,("--- Subscription threshold reached, disable."));
CMPIUint16 sst = 4;
CMSetProperty(sub, "SubscriptionState", &sst, CMPI_uint16);
CBModifyInstance(_broker, ctx, cur->sub, sub, NULL);
purge = cur;
cur = cur->next;
dqRetry(ctx,purge);
}
} else {
cur = cur->next;
}
}
}
}
// Queue went dry, cleanup and exit
_SFCB_TRACE(1,("--- Indication retry queue empty, thread exitting."));
pthread_mutex_unlock(&RQlock);
retryRunning = 0;
CMRelease(ctxLocal);
CMRelease(ctx);
_SFCB_RETURN(NULL);
}
CMPIStatus
IndCIMXMLHandlerInvokeMethod(CMPIMethodMI * mi,
const CMPIContext *ctx,
const CMPIResult *rslt,
const CMPIObjectPath * ref,
const char *methodName,
const CMPIArgs * in, CMPIArgs * out)
{
_SFCB_ENTER(TRACE_INDPROVIDER, "IndCIMXMLHandlerInvokeMethod");
CMPIStatus st = { CMPI_RC_OK, NULL };
int drc = 0;
struct timeval tv;
struct timezone tz;
static unsigned int indID=1;
if (interOpNameSpace(ref, &st) == 0)
_SFCB_RETURN(st);
if (strcasecmp(methodName, "_deliver") == 0) {
#ifndef SETTABLERETRY
// On the first indication, check if reliable indications are enabled.
if (RIEnabled == -1) {
#endif
CMPIObjectPath *op=CMNewObjectPath(_broker,"root/interop","CIM_IndicationService",NULL);
CMPIEnumeration *isenm = _broker->bft->enumerateInstances(_broker, ctx, op, NULL, NULL);
CMPIData isinst=CMGetNext(isenm,NULL);
CMPIData mc=CMGetProperty(isinst.value.inst,"DeliveryRetryAttempts",NULL);
RIEnabled=mc.value.uint16;
#ifndef SETTABLERETRY
}
#endif
CMPIInstance *indo=CMGetArg(in,"indication",NULL).value.inst;
CMPIInstance *ind=CMClone(indo,NULL);
CMPIContext *ctxLocal=NULL;
CMPIObjectPath *iop=NULL,*subop=NULL;
CMPIInstance *sub=NULL;
if (RIEnabled) {
ctxLocal = prepareUpcall((CMPIContext *) ctx);
// Set the indication sequence values
iop=CMGetObjectPath(ind,NULL);
CMAddKey(iop,"SFCB_IndicationID",&indID,CMPI_uint32);
CMSetProperty(ind,"SFCB_IndicationID",&indID,CMPI_uint32);
// Prevent this property from showing up in the indication
filterFlagProperty(ind, "SFCB_IndicationID");
sub=CMGetArg(in,"subscription",NULL).value.inst;
CMPIData handler=CMGetProperty(sub, "Handler", &st);
CMPIObjectPath *hop=handler.value.ref;
// Get the handler instance from the hashtable via a
// methodcall to interopProvider
in=CMNewArgs(_broker,NULL);
CMAddArg(in,"handler",&hop,CMPI_ref);
out=CMNewArgs(_broker,NULL);
CMPIObjectPath* sop=CMNewObjectPath(_broker,"root/interop","cim_indicationsubscription",&st);
CBInvokeMethod(_broker,ctx,sop,"_getHandler",in,out,&st);
CMPIInstance *hdlr=CMGetArg(out,"hin",NULL).value.inst;
if (hdlr == NULL) {
mlogf(M_ERROR,M_SHOW,"Deliver indication failed, hdlr is null. rc:%d\n",st.rc);
_SFCB_RETURN(st);
}
// Build the complete sequence context
// Get the stub from the handler
CMPIString *context = CMGetProperty(hdlr, "SequenceContext", &st).value.string;
// and add the sfcb start time
char *cstr=malloc( (strlen(context->ft->getCharPtr(context,NULL)) + strlen(sfcBrokerStart) + 1) * sizeof(char));
sprintf(cstr,"%s%s",context->ft->getCharPtr(context,NULL),sfcBrokerStart);
context = sfcb_native_new_CMPIString(cstr, NULL, 0);
// and put it in the indication
CMSetProperty(ind, "SequenceContext", &context, CMPI_string);
free(cstr);
CMRelease(context);
// Get the proper sequence number
CMPIValue lastseq = CMGetProperty(hdlr, "LastSequenceNumber", &st).value;
lastseq.sint64++;
// Handle wrapping of the signed int
if (lastseq.sint64 < 0) lastseq.sint64=0;
// Update the last used number in the handler
CMSetProperty(hdlr, "LastSequenceNumber", &lastseq.sint64, CMPI_sint64);
in=CMNewArgs(_broker,NULL);
CMAddArg(in,"handler",&hdlr,CMPI_instance);
CMAddArg(in,"key",&hop,CMPI_ref);
CBInvokeMethod(_broker,ctx,sop,"_updateHandler",in,NULL,&st);
if (st.rc != CMPI_RC_OK) {
mlogf(M_ERROR,M_SHOW,"Failed to update LastSequenceNumber. rc:%d\n",st.rc);
}
// And the indication
CMSetProperty(ind, "SequenceNumber", &lastseq, CMPI_sint64);
}
// Now send the indication
drc = deliverInd(ref, in, ind);
switch (drc) {
case 0: /* Success */
case 400: /* Bad Request XML */
case 501: /* Not Implemented */
break;
default:
if (RIEnabled){
_SFCB_TRACE(1,("--- Indication delivery failed, adding to retry queue"));
// Indication delivery failed, send to retry queue
// build an element
RTElement *element;
element = malloc(sizeof(*element));
element->ref=ref->ft->clone(ref,NULL);
// Get the OP of the subscription and indication
subop=CMGetObjectPath(sub,NULL);
element->sub=subop->ft->clone(subop,NULL);
element->ind=iop->ft->clone(iop,NULL);
element->indInst=ind->ft->clone(ind,NULL);
// Store other attrs
element->instanceID=indID;
element->count=0;
gettimeofday(&tv, &tz);
element->lasttry=tv.tv_sec;
indID++;
// Add it to the retry queue
enqRetry(element,ctx,1);
// And launch the thread if it isn't already running
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
if (retryRunning == 0) {