-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathspliced.c
executable file
·1760 lines (1551 loc) · 55 KB
/
spliced.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
/*
* Vortex OpenSplice
*
* This software and documentation are Copyright 2006 to TO_YEAR ADLINK
* Technology Limited, its affiliated companies and licensors. All rights
* reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <assert.h>
#include <string.h>
#include "vortex_os.h"
#include "os_report.h"
#include "os_sharedmem.h"
#include "os_time.h"
#include "os_stdlib.h"
#include "c_typebase.h"
#include "c_stringSupport.h"
#include "v_leaseManager.h"
#include "spliced.h"
#include "v_spliced.h"
#include "s_misc.h"
#include "s_configuration.h"
#include "s_threadsMonitor.h"
#include "report.h"
#include "serviceMonitor.h"
#include "s_kernelManager.h"
#include "s_shmMonitor.h"
#include "s_gc.h"
#include "sr_componentInfo.h"
#include "ut_entryPoint.h"
#include "u_domain.h"
#include "u_serviceManager.h"
#include "u_observable.h"
#include "os_signalHandler.h"
#include "sr_componentInfo.h"
#include "ut_thread.h"
#ifdef OSPL_ENTRY_OVERRIDE
#include "ospl_entry_override.h"
#endif
#ifdef OSPL_ENV_SHMT
#include <dlfcn.h>
#include <link.h>
#define PURE_MAIN_SYMBOL "ospl_main"
#endif
C_STRUCT(spliced)
{
s_configuration config;
u_spliced service; /* splicedaemon service/participant */
u_serviceManager serviceManager;
serviceMonitor serviceMon;
c_bool isSingleProcess;
os_uint32 nrKnownServices;
os_uint32 nrTerminatedServices;
ut_thread heartbeatManager;
ut_thread durabilityClient;
sr_componentInfo *knownServices;
s_kernelManager km;
s_garbageCollector gc;
s_shmMonitor shmMonitor;
os_uint32 nrApplications;
sr_componentInfo *applications;
struct terminate {
int flag;
int shmClean;
os_cond cond;
os_mutex mtx;
} terminate;
u_service_cmdopts cfg_handle;
ut_threads threads;
s_threadsMonitor threadsMonitor;
};
static int
argumentsCheck(
spliced _this,
int argc,
char *argv[])
{
int retCode = SPLICED_EXIT_CODE_CONTINUE;
const char *name = argv[0];
assert(_this);
assert(name);
if ( argc > 0 )
{
if ((_this->cfg_handle.exeName = os_strdup(name)) == NULL) {
OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0,
"Memory claim (%"PA_PRIdSIZE" bytes) denied; could not duplicate string '%s'.",
strlen(name) + 1,
name);
retCode = SPLICED_EXIT_CODE_RECOVERABLE_ERROR;
goto err_name;
}
}
if ( argc < 2 ){
#ifdef CONF_PARSER_NOFILESYS
_this->cfg_handle.uri = os_strdup("osplcfg://ospl.xml");
#else
_this->cfg_handle.uri = NULL;
#endif
} else if (argc == 2) {
_this->cfg_handle.uri = os_strdup(argv[1]);
} else {
printf("usage: %s [<URI>]\n", name);
printf("\n");
printf("<URI> file://<abs path to configuration> \n");
retCode = SPLICED_EXIT_CODE_UNRECOVERABLE_ERROR;
}
u_serviceCheckEnvURI(&_this->cfg_handle);
err_name:
return retCode;
}
static void *
leaseRenewThread(
void *arg)
{
spliced _this;
int flag;
u_result result;
os_timeM renewed, prevRenewed;
os_duration lag;
ut_thread self;
assert(arg);
_this = (spliced)arg;
self = ut_threadLookupSelf(_this->threads);
/* Set the previous renew-time to maximum value that can be passed to
* os_timeMDiff(...), so no lag is reported for the first lease renewal.
*/
prevRenewed = OS_TIMEM_INFINITE;
do {
result = u_serviceRenewLease(u_service(_this->service), _this->config->leasePeriod);
if (result != U_RESULT_OK) {
OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0,
"Failed to update service lease");
}
renewed = os_timeMGet();
lag = os_timeMDiff(renewed, prevRenewed);
prevRenewed = renewed;
if (os_durationCompare(lag, _this->config->leasePeriod) == OS_MORE) {
OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0,
"Splice-daemon failed to renew its lease within "
"the configured lease expiry-time (%"PA_PRId64".%09d). "
"The lease renewal took %"PA_PRId64".%09d s.",
OS_DURATION_GET_SECONDS(_this->config->leasePeriod),
OS_DURATION_GET_NANOSECONDS(_this->config->leasePeriod),
OS_DURATION_GET_SECONDS(lag), OS_DURATION_GET_NANOSECONDS(lag));
}
/* Terminate flag is also accessed outside the mutex lock. This is no problem,
* protection guarantees no wait entry while setting the flag.
*/
os_mutexLock(&_this->terminate.mtx);
if ((flag = _this->terminate.flag) == SPLICED_EXIT_CODE_CONTINUE) {
(void)ut_condTimedWait(self, &_this->terminate.cond, &_this->terminate.mtx, _this->config->leaseRenewalPeriod);
}
os_mutexUnlock(&_this->terminate.mtx);
} while ((flag == SPLICED_EXIT_CODE_CONTINUE) && (result == U_RESULT_OK));
result = u_serviceRenewLease(u_service(_this->service), 300*OS_DURATION_SECOND);
if (result != U_RESULT_OK) {
OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0,
"Failed to update service lease");
}
return NULL;
}
static void heartbeatManagerAction(v_public p, void *arg)
{
v_leaseManager lm;
OS_UNUSED_ARG(arg);
lm = v_splicedGetHeartbeatManager(v_spliced(p), FALSE);
assert(lm);
OS_REPORT(OS_INFO,
"spliced::heartbeatManagerThread", 1,
"Heartbeat Manager for spliced started");
v_leaseManagerMain(lm);
OS_REPORT(OS_INFO,
"spliced::heartbeatManagerThread", 1,
"Heartbeat Manager for spliced stopped");
c_free(lm);
}
static void *
heartbeatManagerThread(
void *arg)
{
spliced _this = (spliced)arg;
ut_thread self = ut_threadLookupSelf(_this->threads);
u_result result;
/* We can not detect progress here. So, simulate a thread sleep. */
ut_threadAsleep(self, UT_SLEEP_INDEFINITELY);
result = u_observableAction(u_observable(_this->service), heartbeatManagerAction, NULL);
if (result != U_RESULT_OK) {
OS_REPORT(OS_WARNING, ut_threadGetName(self), 0,
"Failed to retrieve heartbeat manager");
}
return NULL;
}
static c_bool
startHeartbeatManager(
spliced _this)
{
c_bool started = FALSE;
v_leaseManager lm;
u_result ures;
_this->heartbeatManager = NULL;
if (_this->config->heartbeatAttribute) {
lm = u_splicedGetHeartbeatManager(_this->service, TRUE);
if (lm) {
ut_threadCreate(_this->threads, &(_this->heartbeatManager), S_THREAD_HEARTBEAT_THREAD,
_this->config->heartbeatAttribute, heartbeatManagerThread, _this);
if (_this->heartbeatManager != NULL) {
ures = u_splicedStartHeartbeat(_this->service, _this->config->heartbeatExpiryTime,
_this->config->heartbeatUpdateInterval);
started = ures == U_RESULT_OK;
} else {
OS_REPORT(OS_WARNING, "spliced::startHeartbeatManager", 0,
"Failed to start heartbeat manager thread");
}
c_free(lm);
} else {
OS_REPORT(OS_WARNING, "spliced::startHeartbeatManager", 0,
"Failed to initialize heartbeat manager");
}
} else {
ures = u_splicedStartHeartbeat(_this->service, _this->config->heartbeatExpiryTime,
_this->config->heartbeatUpdateInterval);
started = ures == U_RESULT_OK;
}
return started;
}
static os_boolean
stopHeartbeatManager(
spliced _this)
{
os_boolean result = OS_TRUE;
s_configuration config;
os_result osr;
v_leaseManager lm;
u_splicedStopHeartbeat(_this->service);
lm = u_splicedGetHeartbeatManager(_this->service, FALSE);
if (lm != NULL) {
v_leaseManagerNotify(lm, NULL, V_EVENT_TERMINATE);
if (_this->heartbeatManager != NULL) {
config = splicedGetConfiguration(_this);
osr = ut_threadTimedWaitExit(_this->heartbeatManager, config->serviceTerminatePeriod, NULL);
if (osr == os_resultSuccess) {
OS_REPORT(OS_ERROR, OS_FUNCTION, 0,
"Failed to join thread \"%s\":0x%" PA_PRIxADDR " (%s)",
ut_threadGetName(_this->heartbeatManager),
(os_address)os_threadIdToInteger(ut_threadGetId(_this->heartbeatManager)),
os_resultImage(osr));
result = OS_FALSE;
}
}
if (result) {
c_free(lm);
}
}
ut_threadAwake(ut_threadLookupSelf(_this->threads));
return result;
}
/* The function below is intended to retrieve the federation
* specific partition name that is used by DDSI. Currently,
* DDSI prevents alignment of almost all topics published on
* this partition. To use this partition for client durability
* an exception has to be made for the d_historicalData topic.
* Until that is has not been implemented the use of this
* partition is commented out.
*/
static char *
getFederationSpecificAlignmentPartitionName(
spliced _this)
{
u_result ures;
char name[U_DOMAIN_FEDERATIONSPECIFICPARTITIONNAME_MINBUFSIZE];
char *buf;
/* For the federation specific partition we use the same partition name
* that is used by ddsi. The name of this partition is a 35 byte name
* __NODE%08"PA_PRIx32" BUILT-IN PARTITION__ where the systemId is
* encoded as 8-byte hex representation.
*/
ures = u_participantFederationSpecificPartitionName(u_participant(_this->service), name, sizeof(name));
assert(ures == U_RESULT_OK);
OS_UNUSED_ARG(ures);
buf = os_strdup(name);
return buf;
}
static char *
getPartitionName(
spliced _this)
{
char *buf;
buf = os_strdup(_this->config->partition);
return buf;
}
static c_bool
durabilityClientStart(spliced _this)
{
os_threadAttr threadAttr;
u_result ures;
char *privatePartition;
char *requestPartition;
_this->durabilityClient = NULL;
/* Get a federation specific alignment partition */
privatePartition = getFederationSpecificAlignmentPartitionName(_this);
/* Get the requestPartition */
requestPartition = getPartitionName(_this);
ures = u_splicedDurabilityClientSetup(
_this->service,
_this->config->durablePolicies,
requestPartition,
"clientDurabilityPartition",
privatePartition);
os_free(requestPartition);
os_free(privatePartition);
if (ures == U_RESULT_OK) {
os_threadAttrInit(&threadAttr);
ut_threadCreate(_this->threads,
&(_this->durabilityClient),
S_THREAD_DURABILITYCLIENT,
&threadAttr,
u_splicedDurabilityClientMain,
_this->service);
if (_this->durabilityClient != NULL) {
/* We can not detect progress here. So, simulate a thread sleep. */
ut_threadAsleep(_this->durabilityClient, UT_SLEEP_INDEFINITELY);
} else {
OS_REPORT(OS_WARNING, "spliced::durabilityClientStart", 0,
"Failed to start durability client thread");
}
} else {
OS_REPORT(OS_WARNING, "spliced::durabilityClientStart", 0,
"Failed to setup durability client");
}
return (_this->durabilityClient != NULL);
}
static os_boolean
durabilityClientStop(spliced _this)
{
os_boolean result = OS_TRUE;
s_configuration config;
os_result osr;
u_result ures;
if (_this->durabilityClient != NULL) {
ures = u_splicedDurabilityClientTerminate(_this->service);
if (ures == U_RESULT_OK) {
config = splicedGetConfiguration(_this);
osr = ut_threadTimedWaitExit(_this->durabilityClient, config->serviceTerminatePeriod, NULL);
if (osr != os_resultSuccess) {
OS_REPORT(OS_ERROR, OS_FUNCTION, osr,
"Failed to join thread \"%s\":0x%" PA_PRIxADDR " (%s)",
ut_threadGetName(_this->durabilityClient),
(os_address)os_threadIdToInteger(ut_threadGetId(_this->durabilityClient)),
os_resultImage(osr));
result = OS_FALSE;
}
} else {
OS_REPORT(OS_WARNING, "spliced::durabilityClientStop", 0,
"Failed to terminate durability client");
}
}
ut_threadAwake(ut_threadLookupSelf(_this->threads));
return result;
}
const os_char*
splicedGetDomainName(
spliced _this)
{
assert(_this);
assert(_this->config);
assert(_this->config->domainName);
return _this->config->domainName;
}
static os_result
exitRequestHandler(
os_callbackArg ignore,
void *callingThreadContext,
void * arg)
{
struct terminate *terminate;
OS_UNUSED_ARG(callingThreadContext);
OS_UNUSED_ARG(ignore);
terminate = (struct terminate*) arg;
/* The os_callbackArg can be ignored, because setting the below flag will
* cause proper exit of the spliced.
*/
os_mutexLock(&terminate->mtx);
terminate->flag = SPLICED_EXIT_CODE_OK;
os_condBroadcast(&terminate->cond);
os_mutexUnlock(&terminate->mtx);
return os_resultSuccess; /* the main thread will take care of termination */
}
static void
splicedKnownServicesFree(
spliced _this)
{
os_uint32 i;
assert(_this != NULL);
for (i = 0; i < _this->nrKnownServices; i++) {
sr_componentInfoFree(_this->knownServices[i]);
_this->knownServices[i] = NULL;
}
os_free(_this->knownServices);
_this->knownServices = NULL;
_this->nrKnownServices = 0;
_this->nrTerminatedServices = 0;
}
static void
splicedApplicationsFree(
spliced _this)
{
os_uint32 i;
assert(_this != NULL);
for (i = 0; i < _this->nrApplications; i++) {
sr_componentInfoFree(_this->applications[i]);
_this->applications[i] = NULL;
}
os_free(_this->applications);
_this->applications = NULL;
_this->nrApplications = 0;
}
static c_bool
serviceCommandIsValid(
char **command)
{
c_bool result = FALSE;
char *suffixedCommand;
char *fullCommand;
assert(command != NULL);
assert(*command != NULL);
fullCommand = os_locate(*command, OS_ROK|OS_XOK);
if (fullCommand) {
os_free(*command);
*command = fullCommand;
result = TRUE;
}
if (!result && (sizeof(OS_EXESUFFIX) > 1)) {
/* Try the same thing with the exe suffix attached */
if (strstr(*command, OS_EXESUFFIX) == NULL) {
suffixedCommand = os_malloc(strlen(*command) + sizeof(OS_EXESUFFIX));
os_strcpy(suffixedCommand, *command);
os_strcat(suffixedCommand, OS_EXESUFFIX);
fullCommand = os_locate(suffixedCommand, OS_ROK|OS_XOK);
if (fullCommand) {
os_free(*command);
*command = fullCommand;
result = TRUE;
}
}
}
return result;
}
static char* splitOnFirstToken(char *original, char token)
{
int i;
for (i = 0; original[i] != '\0'; i++)
{
if (original[i] == token)
{
original[i] = '\0';
return &original[++i];
}
}
return NULL;
}
static os_result
deployLibrary(
sr_componentInfo info
)
{
os_library libraryHandle;
os_libraryAttr libraryAttr;
os_result result;
struct ut_entryPointWrapperArg *mwa;
os_threadAttr threadAttr;
os_threadId id;
int argc;
char ** argv;
char * tempString = NULL;
char * libraryName = NULL;
char * entryPoint = NULL;
char * saveptr;
/* First, determine the argc/argv parameters to the entry point.
* The manner of these varies as to whether we are deploying
* an internal service or a user application:
*
* The internal services require argv[1] to be the service "name"
* specified in the xml, so when started, it knows which xml section
* to parse in order to obtain its configuration. Applications
* do not have this requirement.
*/
argc = 0;
argv = os_malloc(256*sizeof(char *));
argv[argc++] = info->command;
if (info->isService)
{
argv[argc++] = info->name;
if (info->configuration)
{
argv[argc++] = info->configuration;
}
}
tempString = os_strtok_r(info->args, " ", &saveptr);
while (tempString)
{
argv[argc++] = os_strdup (tempString);
tempString = os_strtok_r(NULL, " ", &saveptr);
}
argv[argc] = NULL;
mwa = os_malloc(sizeof(struct ut_entryPointWrapperArg));
mwa->argc = argc;
mwa->argv = argv;
/* Initialise the library attributes */
os_libraryAttrInit(&libraryAttr);
/* Applications can have their library overriden if the user
* specifies the library attribute
*/
if (!info->isService && info->library && strlen(info->library) > 0)
{
libraryName = info->library;
}
else
{
libraryName = info->command;
}
/* Now open the library */
libraryHandle = os_libraryOpen (libraryName, &libraryAttr);
if (libraryHandle != NULL) {
/* Locate the entry point */
if (info->isService)
{
/* Derive entry point name from Command. When deploying a service
* we use the "ospl_" prefix as we have full control over the
* names of these libraries
*/
entryPoint = os_malloc (sizeof (char*) * (strlen(info->command) + 6));
sprintf (entryPoint, "ospl_%s", info->command);
}
else
{
/* Derive entry point name from Command */
entryPoint = os_malloc (sizeof (char*) * (strlen(info->command) + 6));
sprintf (entryPoint, "%s", info->command);
}
/* Writing: mwa->entryPoint = (ut_entryPointFunc)os_libraryGetSymbol (libraryHandle, entryPoint);
* would seem more natural, but results in: warning: ISO C forbids conversion of object pointer
* to function pointer type [-pedantic]
* dlopen man page suggests using the used assignment based on the POSIX.1-2003 (Technical
* Corrigendum 1) workaround.
*/
*(void **)&mwa->entryPoint = os_libraryGetSymbol (libraryHandle, entryPoint);
if (mwa->entryPoint != NULL) {
os_threadAttrInit(&threadAttr);
threadAttr.stackSize = ((size_t)1024*1024);
#ifndef INTEGRITY
threadAttr.schedClass = info->procAttr.schedClass;
threadAttr.schedPriority = info->procAttr.schedPriority;
#endif
/* Invoke the entry point as a new thread */
result = os_threadCreate(&id, info->name, &threadAttr, ut_entryPointWrapper, mwa);
if (result != os_resultSuccess)
{
result = os_resultFail;
OS_REPORT(OS_ERROR,OSRPT_CNTXT_SPLICED,0,
"Error starting thread for '%s'\n", info->name);
}
else
{
info->threadId = id;
/* Don't believe that a wait/sleep is required here.
* historically there was one
*/
}
} else {
result = os_resultFail;
OS_REPORT(OS_ERROR,OSRPT_CNTXT_SPLICED,0,
"Command '%s' not found\n", entryPoint);
}
} else {
result = os_resultFail;
OS_REPORT(OS_ERROR,OSRPT_CNTXT_SPLICED,0,
"Problem opening '%s'\n",libraryName);
}
if (entryPoint)
{
os_free (entryPoint);
}
return result;
}
static int
startServices(
spliced _this)
{
int retCode = SPLICED_EXIT_CODE_CONTINUE;
os_uint32 i;
os_result createResult;
sr_componentInfo info;
c_char* args;
os_size_t argc;
ut_thread self;
char *vg_cmd = NULL;
char *command = NULL;
char *vg_args = NULL;
assert(_this != NULL);
self = ut_threadLookupSelf(_this->threads);
#ifdef INTEGRITY
if ( !os_getIsSingleProcess() )
{
Semaphore serviceStartSem;
Error err;
serviceStartSem = SemaphoreObjectNumber(12);
err = ReleaseSemaphore(serviceStartSem);
assert (err == Success);
(void) err;
}
#endif
/* In case we haven't logged anything yet we call these methods now
* before we start any other domain services
* to (potentially) remove the previous versions of the files
*/
os_free(os_reportGetInfoFileName());
os_free(os_reportGetErrorFileName());
for (i = 0; i < _this->nrKnownServices; i++) {
ut_threadAwake(self);
info = _this->knownServices[i];
if (_this->isSingleProcess) {
/* This is a 'SingleProcess' configuration so the service must be
* deployed as a library.
*/
createResult = deployLibrary (info);
if (createResult == os_resultInvalid) {
OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED,
0, "Starting %s with <SingleProcess> not supported on this platform",
info->name);
retCode = SPLICED_EXIT_CODE_INAPPLICABLE_CONFIGURATION;
break;
}
else if (createResult != os_resultSuccess) {
OS_REPORT(OS_WARNING, OSRPT_CNTXT_SPLICED,
0, "Could not start service %s as thread",
info->name);
}
}
#ifndef INTEGRITY
else
{
#if !defined ( VXWORKS_RTP )&& !defined ( _WRS_KERNEL )
if (serviceCommandIsValid(&info->command))
#else
if (TRUE)
#endif
{
if (!strcmp(info->name,"networking"))
{
vg_cmd = os_getenv("VG_NETWORKING");
}
else if (!strcmp(info->name,"durability"))
{
vg_cmd = os_getenv("VG_DURABILITY");
}
else if (!strcmp(info->name,"snetworking"))
{
vg_cmd = os_getenv("VG_SNETWORKING");
}
else if (!strcmp(info->name,"cmsoap"))
{
vg_cmd = os_getenv("VG_CMSOAP");
}
if (!vg_cmd)
{
command = os_strdup(info->command);
/* allocate with room for 2 quotes, a space, and an end-of-string */
argc = 1+strlen(info->name)+
3+strlen(info->args)+
3+strlen(info->configuration)+
2;
}
else
{
/* get the valgrind command */
vg_args = splitOnFirstToken(vg_cmd, ' ');
command = os_locate(vg_cmd, OS_ROK|OS_XOK);
argc = 1+strlen(info->command)+
1+strlen(info->name)+
3+strlen(info->args)+
1+strlen(vg_args)+
3+strlen(info->configuration)+
2;
}
args = os_malloc(argc);
if (strlen(info->args) == 0) {
if (!vg_cmd) {
snprintf(args, argc, "\"%s\" \"%s\"", info->name, info->configuration);
} else {
snprintf(args, argc, "%s \"%s\" \"%s\" \"%s\"", vg_args, info->command, info->name, info->configuration);
}
} else {
if (!vg_cmd) {
snprintf(args, argc, "\"%s\" \"%s\" \"%s\"", info->name, info->configuration, info->args);
} else {
snprintf(args, argc, "%s \"%s\" \"%s\" \"%s\" \"%s\"", vg_args, info->command, info->name, info->configuration, info->args);
}
}
createResult = os_procCreate(command,
info->name, args,
&info->procAttr, &info->procId);
if (createResult == os_resultSuccess)
{
os_sharedMemoryRegisterUserProcess(splicedGetDomainName(_this), info->procId);
OS_REPORT(OS_INFO, OSRPT_CNTXT_SPLICED,
0, "Started service %s with args %s",
info->name, args);
}
else
{
OS_REPORT(OS_WARNING, OSRPT_CNTXT_SPLICED,
0, "Could not start service %s with args %s",
info->name, args);
}
os_free(args);
os_free(command);
}
else
{
retCode = SPLICED_EXIT_CODE_UNRECOVERABLE_ERROR;
OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0,
"Could not find file '%s' with read and execute permissions",
info->command);
break;
}
}
#endif /* !INTEGRITY */
}
return retCode;
}
static void
startApplications(
spliced _this)
{
os_uint32 i;
os_result deployResult;
ut_thread self;
assert(_this != NULL);
self = ut_threadLookupSelf(_this->threads);
for (i = 0; i < _this->nrApplications; i++) {
ut_threadAwake(self);
deployResult = deployLibrary (_this->applications[i]);
if (deployResult == os_resultInvalid) {
OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED,
0, "Starting Application %s with <SingleProcess> not supported on this platform",
_this->applications[i]->name);
}
else if (deployResult != os_resultSuccess) {
OS_REPORT(OS_WARNING, OSRPT_CNTXT_SPLICED,
0, "Could not start application %s as thread",
_this->applications[i]->name);
}
}
}
static void
getSingleProcessValue(
spliced _this,
u_cfElement spliceCfg)
{
assert(_this != NULL);
if (spliceCfg != NULL) {
c_char * value;
c_bool result;
u_cfData node;
c_iter iter;
iter = u_cfElementXPath(spliceCfg, "SingleProcess/#text");
if (iter) {
node = u_cfData(c_iterTakeFirst(iter));
if (node != NULL) {
result = u_cfDataStringValue(node, &value);
if (result) {
if (os_strcasecmp (value, "True" ) == 0) {
_this->isSingleProcess = 1;
}
os_free(value);
}
u_cfDataFree(node);
}
c_iterFree(iter);
}
}
}
static void
getKnownServices(
spliced _this,
u_cfElement spliceCfg)
{
c_iter services;
u_cfElement s;
os_uint32 i;
assert(_this != NULL);
i = 0;
if (spliceCfg != NULL) {
services = u_cfElementXPath(spliceCfg, "Service");
_this->nrKnownServices = c_iterLength(services);
if (_this->nrKnownServices > 0) {
_this->knownServices = os_malloc(_this->nrKnownServices * sizeof(sr_componentInfo));
i = 0;
s = c_iterTakeFirst(services);
while (s != NULL) {
_this->knownServices[i] = sr_componentInfoServiceNew(s, _this->cfg_handle.uri);
u_cfElementFree(s);
s = c_iterTakeFirst(services);
if (_this->knownServices[i] != NULL) {
i++;
}
}
}
c_iterFree(services);
_this->nrKnownServices = i;
}
}
static void
getApplications(
spliced _this,
u_cfElement spliceCfg)
{
c_iter services;
u_cfElement s;
os_uint32 i;
assert(_this != NULL);
i = 0;
if (spliceCfg != NULL) {
services = u_cfElementXPath(spliceCfg, "Application");
_this->nrApplications = c_iterLength(services);
if (_this->nrApplications > 0) {
_this->applications = (sr_componentInfo *)os_malloc((os_uint)(_this->nrApplications *
(int)sizeof(sr_componentInfo)));
if (_this->applications != NULL) {
i = 0;
s = c_iterTakeFirst(services);
while (s != NULL) {
_this->applications[i] = sr_componentInfoApplicationNew(s, _this->cfg_handle.uri);
u_cfElementFree(s);
s = c_iterTakeFirst(services);
if (_this->applications[i] != NULL) {
i++;
}
}
}
}
c_iterFree(services);
_this->nrApplications = i;
}
}
static void
readConfiguration(
spliced _this)
{
u_cfElement cfg;
u_cfElement dc;
c_iter domains;
assert(_this);
assert(_this->service);
assert(_this->config);
s_configurationRead(_this->config, _this);
cfg = u_participantGetConfiguration(u_participant(_this->service));
if (cfg != NULL) {
domains = u_cfElementXPath(cfg, "Domain");
dc = c_iterTakeFirst(domains);
if (dc != NULL) {
getSingleProcessValue (_this, dc);
getKnownServices(_this, dc);
getApplications(_this, dc);
u_cfElementFree(dc);
dc = c_iterTakeFirst(domains);
while(dc){
u_cfElementFree(dc);
dc = c_iterTakeFirst(domains);
}
}
c_iterFree(domains);