-
Notifications
You must be signed in to change notification settings - Fork 9
/
ismutil.c
4187 lines (3730 loc) · 120 KB
/
ismutil.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
/*
* Copyright (c) 2012-2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#define TRACE_COMP Util
#include <ismutil.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include "unicode/unum.h"
#include <sys/syscall.h>
#include "unicode/ustring.h"
#include <ifaddrs.h>
#include <netpacket/packet.h>
#include <openssl/opensslv.h>
#include <openssl/rand.h>
#include <fendian.h>
#include <config.h>
#ifndef _WIN32
#include <sys/prctl.h>
#endif
#ifndef XSTR
#define XSTR(s) STR(s)
#define STR(s) #s
#endif
#include <trace.h>
#include "tracemodule.h"
#define ISM_COMMON_THREADDATA_HOME //This is the place we are going to actual define ism_common_threaddata so don't declare extern
#include "threadlocal.h"
#undef ISM_COMMON_THREADDATA_HOME
#define NANOS_IN_HOUR 3600000000000L
/* Names of function names we look for in in pluggable trace module */
#define ISM_COMMON_TRACEINIT_FUNC "ism_initTraceModule"
#define ISM_COMMON_TRACE_FUNC "ism_insertTrace"
#define ISM_COMMON_TRACEDATA_FUNC "ism_insertTraceData"
#define ISM_COMMON_SETERROR_FUNC "ism_insertSetError"
#define ISM_COMMON_SETERRORDATA_FUNC "ism_insertSetErrorData"
#define ISM_COMMON_TRACEMODULECFGUPDATE_FUNC "ism_insertCfgUpdate"
/*
* Global variables
*/
static ism_prop_t * ism_g_config_props = NULL;
char g_procname[20];
pthread_mutex_t g_utillock;
pthread_mutex_t trc_lock;
static ism_threadkey_t ism_threadKey;
//thread local storage used for memory account, in memory tracing, other pervasive things
__thread ism_tls_t *ism_common_threaddata = NULL;
extern char * ismcli_ISMClient(char *user, char *protocol, char *command, int proctype);
/*
* Thread local storage for utils
*/
/*---- start stuff for thread health monitoring -----*/
typedef int (*admin_restart_t)(int, void *);
static double tooLongTH=300; // 5 Mins
static int traceLoops=5 ;
static int stopLoops=5 ;
static int i_am_the_main=0;
static double next_st_trace=0;
static int stack_trace_cnt;
static const char *stDir;
/* Health categories */
typedef enum {
HC_OTH,
HC_ADM, /* Admin */
HC_IOP, /* TCP */
HC_SEC, /* Security */
HC_STO, /* Store */
HC_MAX
} ism_health_category_t ;
typedef struct ism_tls_health_t {
struct ism_tls_health_t *next;
double shouldBeBackAt;
pthread_t myId;
pid_t myLWP;
ism_health_category_t myCat;
char myName[20];
} ism_tls_health_t;
static int healthAlert=0;
static int adminStop =0;
static double shouldBeBackAt;
static ism_tls_health_t *healthHead=NULL;
static __thread ism_tls_health_t *threadHealth=NULL;
/*---- end stuff for thread health monitoring -----*/
static ism_tls_t * makeTLS(int need, const char * name);
static void constructMsgId(ism_rc_t code, char * buffer);
ism_common_TraceFunction_t traceFunction = ism_common_trace;
ism_common_TraceDataFunction_t traceDataFunction = ism_common_traceData;
ism_common_SetErrorFunction_t setErrorFunction = ism_common_setError_int;
ism_common_SetErrorDataFunction_t setErrorDataFunction = ism_common_setErrorData_int;
ism_common_TraceModuleCfgUpdate_t traceModuleCfgUpdate = NULL;
// First failure data capture trace level
#define TRACE_FFDC 2
/*
* External non-public interfaces
*/
XAPI void ism_common_initExecEnv(int type);
/*
* Initialize the utilities.
* This should be called only once
*/
XAPI void ism_common_initUtil2(int type) {
static int inited = 0;
int rc;
if (!inited) {
inited = 1;
/* Initialize thread local storage for the main thread */
rc = ism_common_createThreadKey(&ism_threadKey);
makeTLS(1024, g_procname);
/* Initialize properties and locks */
ism_g_config_props = ism_common_newProperties(256);
pthread_mutex_init(&g_utillock, 0);
pthread_mutex_init(&trc_lock, 0);
rc = prctl(PR_GET_NAME, (unsigned long)(uintptr_t)g_procname);
if (rc)
strcpy(g_procname, "imaserver");
/* Discover the execution environment */
ism_common_initExecEnv(type);
}
}
XAPI void ism_common_initUtil(void) {
ism_common_initUtil2(0);
}
XAPI void ism_common_initUtilMain(void) {
i_am_the_main = 1;
ism_common_initUtil();
}
XAPI void ism_common_makeTLS(int need, const char * name) {
makeTLS(need,name);
}
/*
* Make the TLS for a thread
*/
static ism_tls_t * makeTLS(int need, const char * name) {
ism_tls_t * tls = calloc(1, sizeof(ism_tls_t)+need);
if (tls != NULL) {
char namebuf[20] = "";
ism_common_threaddata = tls;
ism_common_initializeThreadMemUsage();
tls->trc_ts = ism_common_openTimestamp(ISM_TZF_LOCAL);
tls->data_alloc = need;
if (!name) {
prctl(PR_GET_NAME, (unsigned long)(uintptr_t)namebuf);
name = (const char *)namebuf;
}
ism_common_strlcpy(tls->tname, name, sizeof(tls->tname));
tls->tname_len = (int)strlen(tls->tname);
ism_common_setThreadKey(ism_threadKey, (void *)tls);
}
return tls;
}
/*
* Get the name of the process
*/
const char * ism_common_getProcessName(void) {
return g_procname;
}
/*
* Get the build version.
* This might be zero length for unofficial builds
*/
const char * ism_common_getVersion(void) {
return XSTR(ISM_VERSION);
}
/*
* Get the build label.
* This might be zero length for unofficial builds
*/
const char * ism_common_getBuildLabel(void) {
return XSTR(BUILD_LABEL);
}
/*
* Get info to uniquely identify level of source used in the build.
* For example a git commit hash or tag.
*
* May be 0-length if IMA_SOURCELEVEL_INFO env var not set during
* build.
*/
const char * ism_common_getSourceLevel(void) {
return XSTR(IMA_SOURCELEVEL_INFO);
}
/*
* Get the date and time of the build.
* This date and time are local to where the build was done and do not contain a timezone.
* This is the time the component is built an can vary between components of the same build.
* This is set for all builds.
*/
const char * ism_common_getBuildTime(void) {
return XSTR(ISMDATE) " " XSTR(ISMTIME);
}
/*
* Get the configuration property object
* @return the configuration properties object
*/
ism_prop_t * ism_common_getConfigProperties(void) {
return ism_g_config_props;
}
/*
* Trace static global variables
*/
static FILE * trcfile = NULL;
static char * trcFileName = NULL;
ism_domain_t ism_defaultDomain = {"DOM", 0, "Default" };
ism_trclevel_t * ism_defaultTrace = &ism_defaultDomain.trace;
int ismTraceLevel = 6; /* Deprecated, do not use */
int ismTraceBackup = 1;
char * ismTraceBackupDestination = NULL;
int ismTraceBackupCount = MAX_TRACE_FILES;
volatile uint64_t g_totalNonFatalFFDCs = 0; ///< Overall count of nonfatal FFDCs
static int trcFlush = 1;
static long trcSize = -1;
static long trcMax = 20 * 1024 * 1024;
static int trcMsgData = 0;
static int trcOpt = TOPT_TIME | TOPT_THREAD | TOPT_WHERE;
static char * g_trc_clientaddr = NULL;
static char * g_trc_clientid = NULL;
static char * g_trc_endpoint = NULL;
/*
* Reset trace routines so instead of using a trace module we use the builtin trace
*/
void ism_common_TraceModuleClear(void) {
traceFunction = ism_common_trace;
traceDataFunction = ism_common_traceData;
setErrorFunction = ism_common_setError_int;
setErrorDataFunction = ism_common_setErrorData_int;
traceModuleCfgUpdate = NULL;
}
/*
* Load and initialize a trace module.
* @param props The server properties
* @param errorBuffer A buffer to place an error string on failure
* @param errorBufSize The size of the error buffer
* @param trclevel The trace level for the default domain.
*/
bool ism_common_loadTraceModule(ism_prop_t * props, char * errorBuffer, int errorBufSize, int * trclevel) {
bool success = false;
bool default_loc = false;
const char *traceModuleLocation = ism_common_getStringProperty(props, TRACEMODULE_LOCATION_CFGITEM);
if ( (traceModuleLocation == NULL)
|| (strcmp(traceModuleLocation, "0") == 0)
|| (strcmp(traceModuleLocation, "") == 0)) {
traceModuleLocation = TRACEMODULE_DEFAULT_LOCATION;
default_loc = true;
}
dlerror(); /* clear any errors */
void * traceModuleHandle = dlopen(traceModuleLocation, RTLD_NOW);
if (traceModuleHandle != NULL) {
ism_common_TraceInitModule_t initStub = dlsym(traceModuleHandle, ISM_COMMON_TRACEINIT_FUNC);
if (!initStub) {
snprintf(errorBuffer, errorBufSize, "ism_initTraceModule: %s", dlerror());
dlclose(traceModuleHandle);
goto mod_exit;
}
ism_common_TraceFunction_t traceStub = dlsym(traceModuleHandle, ISM_COMMON_TRACE_FUNC);
if (!traceStub) {
snprintf(errorBuffer, errorBufSize, "ism_insertTrace: %s", dlerror());
dlclose(traceModuleHandle);
goto mod_exit;
}
ism_common_TraceDataFunction_t traceDataStub = dlsym(traceModuleHandle, ISM_COMMON_TRACEDATA_FUNC);
if (!traceDataStub) {
snprintf(errorBuffer, errorBufSize, "ism_insertTraceData: %s", dlerror());
dlclose(traceModuleHandle);
goto mod_exit;
}
ism_common_SetErrorFunction_t setErrorStub = dlsym(traceModuleHandle, ISM_COMMON_SETERROR_FUNC);
if (!setErrorStub) {
/* Optional function... not an error if it's not there just use the default */
setErrorStub = ism_common_setError_int;
}
ism_common_SetErrorDataFunction_t setErrorDataStub = dlsym(traceModuleHandle, ISM_COMMON_SETERRORDATA_FUNC);
if (!setErrorDataStub) {
/* Optional function... not an error if it's not there just use the default */
setErrorDataStub = ism_common_setErrorData_int;
}
ism_common_TraceModuleCfgUpdate_t cfgUpdateStub = dlsym(traceModuleHandle, ISM_COMMON_TRACEMODULECFGUPDATE_FUNC);
/* Optional function... not an error if it's not there, we just don't call a function */
/*
* Found all the functions so call init.
*/
int rc = initStub(props, errorBuffer, errorBufSize, trclevel);
if (rc == 0) {
traceFunction = traceStub;
traceDataFunction = traceDataStub;
setErrorFunction = setErrorStub;
setErrorDataFunction = setErrorDataStub;
traceModuleCfgUpdate = cfgUpdateStub;
success = true;
} else {
/* Init function will have filled in error string... */
dlclose(traceModuleHandle);
}
} else {
/*
* We don't fill in error string in this case (get shell acccess and use strace to investigate if necessary)
* as this is the "normal" case - there was no valid trace module
*/
}
mod_exit:
if (!success) {
/* If we didn't successfully setup up a module, ensure we don't have pointers to an old one */
ism_common_TraceModuleClear();
}
return (success || default_loc); /* BEAM suppression: file leak */
}
int g_traceInited = 0;
/*
* Initialize the trace.
* This is the initial configuration of trace when only the static config file is available.
* Much of the trace information will be modified later during dynnamic config but we need
* to have some trace available during this period.
*/
void ism_common_initTrace(void) {
const char * trcname = ism_common_getStringConfig("TraceFile");
const char * trclvl = ism_common_getStringConfig("TraceLevel");
const char * trcsel = ism_common_getStringConfig("TraceSelected");
const char * trcopt = ism_common_getStringConfig("TraceOptions");
const char * trcmsgdata = ism_common_getStringConfig("TraceMessageData");
const char * trcfilter = ism_common_getStringConfig("TraceFilter");
int tmptrclvl;
char traceModuleErrorBuffer[256] = {0};
bool traceModuleLoaded = true;
if (g_traceInited)
return;
g_traceInited = 1;
/* Set trace options */
if (trcopt)
ism_common_setTraceOptions(trcopt);
/* Set trace destination */
if (trcname)
ism_common_setTraceFile(trcname, trcOpt&TOPT_APPEND);
/* Set the trace level */
if (trclvl == NULL)
trclvl = "7";
ism_common_setTraceLevelX(ism_defaultTrace, trclvl);
if (trcsel == NULL)
trcsel = "7";
ism_common_setTraceLevelX(&ism_defaultDomain.selected, trcsel);
/* Set the maximum trace file size to very big as the config value is used later */
trcMax = 2000*1024*1024;
/*
* Load a trace module if there is one
*/
tmptrclvl = ism_common_getTraceLevel(); /* Note this is deprecated */
traceModuleLoaded = ism_common_loadTraceModule( ism_common_getConfigProperties()
, traceModuleErrorBuffer
, sizeof traceModuleErrorBuffer
, &tmptrclvl);
/* TODO: fix this as it uses deprecated function */
if (tmptrclvl != ism_common_getTraceLevel())
ism_common_setTraceLevel(tmptrclvl);
if (trcmsgdata) {
int val = strtoul(trcmsgdata, NULL, 0);
if (val < 0)
val = 0;
trcMsgData = val;
}
/* Put out an initial trace entry */
if (trclvl && trcname) {
TRACE(2, "============ Initialize trace ============\n");
trcFlush = 0;
}
/* Set any trace filter in static config */
/* TODO: remove this */
if (trcfilter)
ism_common_setTraceFilter(trcfilter);
if (!traceModuleLoaded) {
TRACE(2, "Load of trace module failed: %s\n", traceModuleErrorBuffer);
}
}
/*
* Loads the trace module in response to changes in dynamic config
*/
void ism_common_TraceModuleDynamicUpdate(ism_prop_t * props) {
char traceModuleErrorBuffer[256] = {0};;
int localtrclevel = ism_common_getTraceLevel(); /* uses deprecated method */
bool traceModuleLoaded = ism_common_loadTraceModule( props
, traceModuleErrorBuffer
, sizeof traceModuleErrorBuffer
, &localtrclevel);
if (traceModuleLoaded) {
/* TODO: Uses deprecated method */
if (localtrclevel != ism_common_getTraceLevel())
ism_common_setTraceLevel(localtrclevel);
TRACE(2, "============ Initialized trace module============\n");
} else {
TRACE(2, "Load of trace module failed: %s\n", traceModuleErrorBuffer);
}
}
/*
* If the trace module wants to know about changes in config, this
* function notifies it.
*/
void ism_common_TraceModuleConfigUpdate(ism_prop_t * props) {
/* If the trace module has a props changed function then call it now */
ism_common_TraceModuleCfgUpdate_t cfgUpdate = traceModuleCfgUpdate;
if (cfgUpdate) {
cfgUpdate(props);
}
}
///////////////////////////////////////////////////////////////////////////////
/// @brief
/// This function is used to write a block of data about a failure to
/// the trace file.
///
/// @param[in] function - Function name
/// @param[in] seqId - Unique Id within function
/// @param[in] filename - Filename
/// @param[in] lineNo - line number
/// @param[in] label - Text description of failure
/// @param[in] retcode - Associated return code
/// @param[in] ... - A variable number of arguments (grouped in
/// sets of 3 terminated with a NULL)
/// 1) ptr (void *) - Pointer to data to dump
/// 2) length (size_t) - Size of data
/// 3) label - Text label of data
///
///////////////////////////////////////////////////////////////////////////////
void ism_common_ffdc( const char *function
, uint32_t seqId
, bool core
, const char *filename
, uint32_t lineNo
, char *label
, uint32_t retcode
, ... )
{
va_list ap;
void *ptr;
size_t length;
char *datalabel;
char retcodeName[64];
char retcodeText[128];
// If we are going to shutdown put a big obvious error into the trace otherwise be a little less scary
if(core)
{
LOG( ERROR, Messaging, 3030, NULL, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
LOG( ERROR, Messaging, 3031, "%s%d", "!! Probe: {0}:{1}", function, seqId);
LOG( ERROR, Messaging, 3032, "%s%d", "!! Location: {0}:{1}", filename, lineNo);
LOG( ERROR, Messaging, 3033, "%s", "!! Label: {0}", label);
}
else
{
LOG( ERROR, Messaging,3035, "%s%d%s", "Error location: {0}({1}) : {2}", filename,lineNo,label);
}
if (retcode != 0)
{
ism_common_getErrorName(retcode, retcodeName, 64);
ism_common_getErrorString(retcode, retcodeText, 128);
LOG( ERROR, Messaging, 3034, "%s%d%s", "!! Retcode: {0} ({1}) - {2}", retcodeName, retcode, retcodeText);
}
if (TRACE_FFDC <= TRACE_DOMAIN->trcComponentLevels[TRACECOMP_XSTR(TRACE_COMP)])
{
va_start(ap, retcode);
do
{
datalabel=va_arg(ap, void *);
if (datalabel != NULL)
{
ptr=va_arg(ap, void *);
length=va_arg(ap, size_t);
if (ptr == NULL) length = 0; // If a NULL pointer was provided
traceDataFunction(datalabel, 0, filename, lineNo, ptr, length, length);
}
} while (datalabel != NULL);
va_end(ap);
}
if (!core)
{
// Write a message to the log with details of the failure
LOG( ERROR, Messaging, 3004, "%s%d",
"A non-fatal failure has occurred at location {0}:{1}. The failure recording routine has been called.",
function, seqId );
__sync_add_and_fetch(&g_totalNonFatalFFDCs , 1);
}
else
{
#if 0
// Mechanism to write a file when a core is being requested to allow a watching process to stop
// looping -- see the change set that introduced this change for more information.
FILE *fp = fopen("doneIt.failed", "w");
fprintf(fp, "FAILED in %s probe %u\n", function, seqId);
fclose(fp);
#endif
// If this error is terminal then bring down the server
LOG( ERROR, Messaging, 3005, "%s%d",
"An unrecoverable failure has occurred at location {0}:{1}. The failure recording routine has been called. The server will now stop and restart.",
function, seqId );
ism_common_shutdown(true);
}
return;
}
/*
* Get the number of non fatal FFDCs that have occurred
* Used to check that the memory monitoring is healthy and
* not leaking memory.
*
* Returned along with the memory statistics in the proxy and
* the gateway memory statistics
*/
uint64_t ism_common_get_ffdc_count(void){
return g_totalNonFatalFFDCs;
}
/*
* Get the domain object by ID.
* @param id The ID of the domain
* @return a domain object or NULL to indicate the ID is not a valid domain ID.
*/
ism_domain_t * ism_common_getDomainID(uint32_t id) {
if (id == 0)
return &ism_defaultDomain;
/* TODO: Implement when domains exist */
return NULL;
}
/*
* Get the domain object by name.
* @param name The name of the domain
* @return a domain object or NULL to indicate the name is not known.
*/
ism_domain_t * ism_common_getDomain(const char * name) {
if (!name || !*name || !strcmp(name, "Default"))
return &ism_defaultDomain;
/* TODO: Implement when domains exist */
return NULL;
}
/*
* Return the value of a hex digit or -1 if not a valid hex digit
*/
static int hexValue(char ch) {
if (ch <= '9' && ch >= '0')
return ch-'0';
if (ch >='A' && ch <= 'F')
return ch-'A'+10;
if (ch >='a' && ch <= 'f')
return ch-'a'+10;
return -1;
}
/*
* Converts the thread affinity from 64 bit number to 64 bytes array
* @param char - affinity as a string
* @param char[1024] - affinity as byte array
* @return index of last non-zero byte in the array
*/
int ism_common_parseThreadAffinity(const char *affStr, char affMap[CPU_SETSIZE]) {
int len;
int result = 0;
const char * ptr;
if (!affStr)
return 0;
if ((affStr[0] == '0') && ((affStr[1] == 'x') || (affStr[1] == 'X')))
affStr += 2;
len = strlen(affStr);
if (len == 0)
return 0;
ptr = affStr + (len - 1);
do {
int v = hexValue(*ptr);
if(v & 1)
affMap[result] = 1;
if(v & 2)
affMap[result+1] = 1;
if(v & 4)
affMap[result+2] = 1;
if(v & 8)
affMap[result+3] = 1;
ptr--;
result += 4;
} while(ptr >= affStr);
return result;
}
/*
* Use the logcategory.h header file to produce the name to id category mapping
*/
#define TRACECOMP_SPECIAL
int ism_common_getTraceComponentID(const char * component) {
if (component==NULL || !strcmp(component, "."))
return -1;
#undef TRACECOMP_
#define TRACECOMP_(name, val) else if (!strcmpi(component, #name)) return TRACECOMP_##name;
#include <tracecomponent.h>
return -1;
}
/*
* Use the logcategory.h header file to produce the name to id category name mapping
*/
#define TRACECOMP_SPECIAL
const char * ism_common_getTraceComponentName(int id) {
if (id == -1)
return "unknown";
#undef TRACECOMP_
#define TRACECOMP_(name, val) else if (id == val) return #name;
#include <tracecomponent.h>
return "unknown";
}
/*
* Return the max size of message data to trace
*/
int ism_common_getTraceMsgData(void) {
return trcMsgData;
}
/*
* Set the max size of message data to trace
*/
void ism_common_setTraceMsgData(int msgdata) {
if (msgdata < 0)
msgdata = 0;
trcMsgData = msgdata;
}
/*
* Set the trace options
*/
int ism_common_setTraceFilter(const char * str) {
char * ostr;
int len = 0;
char * token;
const char * comptoken = NULL;
char * compvaluetoken = NULL;
char * eos;
int complevel = -1;
int compID;
int rc = 0;
int opt = trcOpt;
int tracemax = 0;
if (str)
len = (int)strlen(str);
if (len) {
ostr = alloca(len+1);
strcpy(ostr, str);
token = (char *)ism_common_getToken(ostr, " ,\t\n\r", " ,\t\n\r", &ostr);
while (token) {
comptoken = (char *)ism_common_getToken(token, " \t=:", " \t=:", &compvaluetoken);
if (compvaluetoken && *compvaluetoken) {
/* Check for tokens that accept non-integer values */
if (!strcmpi(comptoken, "tracemax")) {
tracemax = ism_common_getBuffSize(comptoken, compvaluetoken, "0");
if ((tracemax == 0) || (tracemax >= 2000 * 1024 * 1024)) {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
} else {
ism_common_setTraceMax(tracemax);
}
} else {
/* Integer value tokens */
complevel = strtoul(compvaluetoken, &eos, 0);
if (!*eos) {
compID = ism_common_getTraceComponentID(comptoken);
if (compID >=0 && compID <TRACECOMP_MAX) {
if (complevel>=0 && complevel<=9) {
/* Valid level. Set the level */
ism_defaultTrace->trcComponentLevels[compID] = complevel;
} else {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
}
} else {
if (!strcmpi(comptoken, "msgdata")) {
if (complevel < 0)
complevel = 0;
trcMsgData = complevel;
} else if (!strcmpi(comptoken, "time")) {
if (complevel<0 || complevel>1) {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
}
if (complevel != 0)
opt |= TOPT_TIME;
else
opt &= ~TOPT_TIME;
} else if (!strcmpi(comptoken, "where")) {
if (complevel<0 || complevel>1) {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
}
if (complevel != 0)
opt |= TOPT_WHERE;
else
opt &= ~TOPT_WHERE;
} else if (!strcmpi(comptoken, "thread")) {
if (complevel<0 || complevel>1) {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
}
if (complevel != 0)
opt |= TOPT_THREAD;
else
opt &= ~TOPT_THREAD;
} else {
rc = ISMRC_BadPropertyName;
ism_common_setErrorData(rc, "%s", comptoken);
break;
}
}
} else {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
}
}
} else {
rc = ISMRC_BadPropertyName;
ism_common_setErrorData(rc, "%s", comptoken);
break;
}
token = ism_common_getToken(ostr, " ,\t\n\r", " ,\t\n\r", &ostr);
}
}
if (rc == 0) {
pthread_mutex_lock(&trc_lock);
trcOpt = opt;
pthread_mutex_unlock(&trc_lock);
}
return rc;
}
/*
* Set the trace max size.
* @param tracemax The maximum size to set. This must be greater than zero.
*/
void ism_common_setTraceMax(uint64_t tracemax) {
if (tracemax > 0) {
if (tracemax < 250000)
tracemax = 250000; /* Set min size */
trcMax = (long)tracemax;
}
TRACE(5, "Set the maximum trace size to: %ld\n", trcMax);
}
/*
* Set the trace level to show.
* Any trace entries at or the specified level or lower will be shown.
* <p>
* Normally the trace level is set up using the TraceLevel configuration value.
*
* @param level A level from 0 to 9. A value of 0 disables all trace.
* @return A return code: 0=good
*/
int ism_common_setTraceLevel(int level) {
char clevel [2];
if (level >= 0 && level <= 9) {
clevel[0] = level+'0';
clevel[1] = 0;
ism_common_setTraceLevelX(ism_defaultTrace, clevel);
return 0;
}
return 1;
}
/*
* Set extended trace level.
*/
int ism_common_setTraceLevelX(ism_trclevel_t * trcl, const char * lvl) {
int count;
int level = 0;
char * lp;
char * eos;
char * token;
char * comptoken;
char * compvaluetoken;
int complevel;
int compID;
int rc = 0;
if (!lvl)
return 1;
lp = alloca(strlen(lvl)+1);
strcpy(lp, lvl);
token = ism_common_getToken(lp, " ,\t\n\r", " ,\t\n\r", &lp);
if (!token)
token = "5";
level = strtoul(token, &eos, 0);
if (*eos) {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", "TraceLevel", token);
return rc;
}
trcl->trcLevel = level;
/* Set the default level for all */
for (count=0; count<TRACECOMP_MAX; count++) {
trcl->trcComponentLevels[count] = level;
}
token = (char *)ism_common_getToken(lp, " ,\t\n\r", " ,\t\n\r", &lp);
while (token) {
comptoken = (char *)ism_common_getToken(token, " \t=:", " \t=:", &compvaluetoken);
if (compvaluetoken && *compvaluetoken) {
/* Check for tokens that accept non-integer values */
complevel = strtoul(compvaluetoken, &eos, 0);
if (!*eos) {
compID = ism_common_getTraceComponentID(comptoken);
if (compID >=0 && compID <TRACECOMP_MAX) {
if (complevel>=0 && complevel<=9) {
/* Valid level. Set the level */
trcl->trcComponentLevels[compID] = complevel;
} else {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
}
} else {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, compvaluetoken);
break;
}
}
} else {
rc = ISMRC_BadPropertyName;
ism_common_setErrorData(rc, "%s", comptoken);
break;
}
token = ism_common_getToken(lp, " ,\t\n\r", " ,\t\n\r", &lp);
}
return rc;
}
/*
* Set trace connection configuration
*/
int ism_common_setTraceConnection(const char * trcconn) {
char * lp;
char * token;
char * comptoken;
char * compvaluetoken;
int rc = 0;
const char * trc_endpoint = NULL;
const char * trc_clientid = NULL;
const char * trc_clientaddr = NULL;
if (!trcconn)
return 1;
lp = alloca(strlen(trcconn)+1);
strcpy(lp, trcconn);
token = (char *)ism_common_getToken(lp, " ,\t\n\r", " ,\t\n\r", &lp);
while (token) {
comptoken = (char *)ism_common_getToken(token, " \t=:", " \t=:", &compvaluetoken);
if (!comptoken)
comptoken = "";
if (compvaluetoken) {
if (*compvaluetoken==0)
compvaluetoken = NULL;
if (!strcmpi(comptoken, "endpoint")) {
trc_endpoint = ism_common_strdup(ISM_MEM_PROBE(ism_memory_utils_misc,1000),compvaluetoken);
} else if (!strcmpi(comptoken, "clientid")) {
trc_clientid = ism_common_strdup(ISM_MEM_PROBE(ism_memory_utils_misc,1000),compvaluetoken);
} else if (!strcmpi(comptoken, "clientaddr")) {
trc_clientaddr = ism_common_strdup(ISM_MEM_PROBE(ism_memory_utils_misc,1000),compvaluetoken);
} else {
rc = ISMRC_BadPropertyName;
ism_common_setErrorData(rc, "%s", comptoken);
break;
}
} else {
rc = ISMRC_BadPropertyValue;
ism_common_setErrorData(rc, "%s%s", comptoken, "");
break;
}
token = ism_common_getToken(lp, " ,\t\n\r", " ,\t\n\r", &lp);
}
if (rc == 0) {