-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEcProcess.m
7394 lines (6693 loc) · 187 KB
/
EcProcess.m
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
/** Enterprise Control Configuration and Logging
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: Febrary 2010
Originally developed from 1996 to 2012 by Brainstorm, and donated to
the FSF.
This file is part of the GNUstep project.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import <Foundation/Foundation.h>
#import <GNUstepBase/GSObjCRuntime.h>
#import <GNUstepBase/NSObject+GNUstepBase.h>
#if GS_USE_GNUTLS
#import <GNUstepBase/GSTLS.h>
static void
setupTLS(NSUserDefaults *u)
{
#if !defined(TLS_DISTRIBUTED_OBJECTS)
if ([u boolForKey: @"EncryptedDO"])
#endif
{
ENTER_POOL
/* Enable encrypted DO if supported by the base library.
*/
if ([NSSocketPort respondsToSelector:
@selector(setClientOptionsForTLS:)])
{
NSDictionary *d;
if (nil == (d = [u dictionaryForKey: @"ClientOptionsForTLS"])
&& nil == (d = [u dictionaryForKey: @"OptionsForTLS"]))
{
d = [NSDictionary dictionary];
}
else
{
NSMutableDictionary *opts;
id o;
/* If we were passed data rather than filenames
* we must set it up as cached data corresponding
* to well known names.
*/
opts = AUTORELEASE([d mutableCopy]);
o = [opts objectForKey: GSTLSCertificateKeyFile];
if ([o isKindOfClass: [NSData class]])
{
[GSTLSObject setData: o
forTLSFile: @"distributed-objects-client-key"];
[opts setObject: @"distributed-objects-client-key"
forKey: GSTLSCertificateKeyFile];
}
o = [opts objectForKey: GSTLSCertificateFile];
if ([o isKindOfClass: [NSData class]])
{
[GSTLSObject setData: o
forTLSFile: @"distributed-objects-client-crt"];
[opts setObject: @"distributed-objects-client-crt"
forKey: GSTLSCertificateFile];
}
d = opts;
}
[NSSocketPort
performSelector: @selector(setClientOptionsForTLS:)
withObject: d];
if (nil == (d = [u dictionaryForKey: @"ServerOptionsForTLS"])
&& nil == (d = [u dictionaryForKey: @"OptionsForTLS"]))
{
d = [NSDictionary dictionary];
}
else
{
NSMutableDictionary *opts;
id o;
/* If we were passed data rather than filenames
* we must set it up as cached data corresponding
* to well known names.
*/
opts = AUTORELEASE([d mutableCopy]);
o = [opts objectForKey: GSTLSCertificateKeyFile];
if ([o isKindOfClass: [NSData class]])
{
[GSTLSObject setData: o
forTLSFile: @"distributed-objects-server-key"];
[opts setObject: @"distributed-objects-server-key"
forKey: GSTLSCertificateKeyFile];
}
o = [opts objectForKey: GSTLSCertificateFile];
if ([o isKindOfClass: [NSData class]])
{
[GSTLSObject setData: o
forTLSFile: @"distributed-objects-server-crt"];
[opts setObject: @"distributed-objects-server-crt"
forKey: GSTLSCertificateFile];
}
d = opts;
}
[NSSocketPort
performSelector: @selector(setServerOptionsForTLS:)
withObject: d];
}
LEAVE_POOL
}
}
#else
static void
setupTLS(NSUserDefaults *u)
{
}
#endif
#import "EcProcess.h"
#import "EcLogger.h"
#import "EcAlarm.h"
#import "EcAlarmDestination.h"
#import "EcHost.h"
#import "EcUserDefaults.h"
#import "EcBroadcastProxy.h"
#import "EcMemoryLogger.h"
#include "config.h"
#if defined(HAVE_LIBCRYPT)
extern char *crypt(const char *key, const char *salt);
#endif
#if HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h>
#else
#if HAVE_VALGRIND_H
#include <valgrind.h>
#endif
#endif
#ifdef HAVE_SYS_SIGNAL_H
#include <sys/signal.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#ifdef HAVE_SYS_FCNTL_H
#include <sys/fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#if defined(HAVE_TERMIOS_H)
#include <termios.h>
#endif
#if defined(HAVE_GETTID)
# include <sys/syscall.h>
# include <sys/types.h>
#endif
#include <stdio.h>
NSString * const EcDidQuitNotification = @"EcDidQuitNotification";
NSString * const EcWillQuitNotification = @"EcWillQuitNotification";
static NSLock_error_handler *original_NSLock_error_handler = NULL;
static void
EcLock_error_handler(id obj, SEL _cmd, BOOL stop, NSString *msg)
{
if (stop && EcProc)
{
EcAlarm *a;
a = [EcAlarm alarmForManagedObject: nil
at: nil
withEventType: EcAlarmEventTypeProcessingError
probableCause: EcAlarmSoftwareProgramError
specificProblem: @"Deadlock detected in this process"
perceivedSeverity: EcAlarmSeverityCritical
proposedRepairAction:
_(@"Please examine in gdb to determine the cause of the"
@" deadlock if possible, then obtain a core dump for examination;"
@" using 'kill -ABRT' for example")
additionalText: [obj description]];
[EcProc alarm: a];
}
(original_NSLock_error_handler)(obj, _cmd, stop, msg);
}
#ifndef __MINGW__
static int reservedPipe[2] = { 0, 0 };
static NSInteger descriptorsMaximum = 0;
#endif
/* Flag to say whether a restart was caused by the mememory maximum
*/
static BOOL memRestart = NO;
#if !defined(EC_DEFAULTS_PREFIX)
#define EC_DEFAULTS_PREFIX nil
#endif
#if !defined(EC_EFFECTIVE_USER)
#define EC_EFFECTIVE_USER nil
#endif
NSUInteger
ecNativeThreadID()
{
#if defined(__MINGW__)
return (NSUInteger)GetCurrentThreadId();
#elif defined(HAVE_GETTID)
return (NSUInteger)syscall(SYS_gettid);
#else
return NSNotFound;
#endif
}
static NSString * const ecControlKey = @"EcControlKey";
/* Return the number of bytes represented by a hexadecimal string (length/2)
* or the number of 8bit characters if the string is not hexadecimal digits.
* If the string is hexadecimal, standardise o uppercase.
*/
static size_t
checkHex(char *str)
{
const char *src = str;
uint8_t *dst = (uint8_t*)str;
size_t l;
while (*src)
{
if (isxdigit(*src))
{
if (islower(*src))
{
*dst = toupper(*src);
}
else
{
*dst = *src;
}
dst++;
}
else if (!isspace(*src))
{
*dst = '\0';
return 0; // Bad character
}
src++;
}
*dst = '\0';
l = ((char*)dst) - str;
if (l%2 == 1)
{
return 0; // Not an even number of digits
}
return l/2; // Return number of bytes represented
}
#if 0
static size_t
trim(char *str)
{
size_t len = 0;
char *frontp = str - 1;
char *endp = NULL;
if (NULL == str || '\0' == str[0])
{
return 0;
}
len = strlen(str);
endp = str + len;
while (isspace(*(++frontp)))
;
while (isspace(*(--endp)) && endp != frontp)
;
if (str + len - 1 != endp)
{
*++endp = '\0';
}
else if (frontp != str && endp == frontp)
{
*str = '\0';
}
if (frontp != str)
{
endp = str;
while (*frontp)
{
*endp++ = *frontp++;
}
*endp = '\0';
}
return endp - str;
}
#endif
@interface EcDefaultRegistration : NSObject
{
NSString *name; // The name/key of the default (without prefix)
NSString *type; // The type text for the default
NSString *help; // The help text for the default
SEL cmd; // method to update when default values change
id obj; // The latest value of the default
id val; // The fallback value of the default
}
+ (void) defaultsChanged: (NSUserDefaults*)defs;
+ (NSMutableString*) listHelp: (NSString*)key;
+ (NSDictionary*) merge: (NSDictionary*)d;
+ (void) registerDefault: (NSString*)name
withTypeText: (NSString*)type
andHelpText: (NSString*)help
action: (SEL)cmd
value: (id)value;
+ (void) showHelp;
@end
/* Lock for controlling access to per-process singleton instance.
*/
static NSRecursiveLock *ecLock = nil;
static NSString *configError = nil;
static BOOL configInProgress = NO;
static BOOL cmdFlagDaemon = NO;
static BOOL cmdFlagTesting = NO;
static BOOL cmdIsRunning = NO;
static BOOL cmdIsRegistered = NO;
static BOOL cmdKeepStderr = NO;
static BOOL cmdKillDebug = NO;
static NSString *cmdBase = nil;
static NSString *cmdInst = nil;
static NSString *cmdName = nil;
static NSString *cmdUser = nil;
static NSUserDefaults *cmdDefs = nil;
static NSString *cmdDebugName = nil;
static NSMutableDictionary *cmdLogMap = nil;
static id<EcMemoryLogger> cmdMemoryLogger = nil;
static NSMutableArray *ecConfigClients = nil;
static NSDate *started = nil; /* Time object was created. */
static NSDate *memStats = nil; /* Time stats were started. */
static NSTimeInterval lastIP = 0.0; /* Time of last input to object. */
static NSTimeInterval lastOP = 0.0; /* Time of last output by object. */
static Class cDateClass = 0;
static Class dateClass = 0;
static Class stringClass = 0;
static int cmdSignalled = 0;
static NSDictionary *ecOperators = nil; // Operator configuration
static NSTimeInterval initAt = 0.0;
/* Internal value for use only by quitting mechanism.
*/
static NSTimeInterval beganQuitting = 0.0; // Start of orderly shutdown
static BOOL ecQuitHandled = NO; // Has ecHandleQuit run?
static NSTimeInterval ecQuitLimit = 180.0; // Time allowed for quit
static NSInteger ecQuitStatus = 0; // Status for the quit
static NSString *ecQuitReason = nil; // Reason for the quit
/* Test to see if the process is trying to quit gracefully.
* If quitting has taken over three minutes, abort immediately.
*/
static BOOL
ecIsQuitting()
{
if (0.0 == beganQuitting)
{
return NO;
}
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (now - beganQuitting >= ecQuitLimit)
{
NSLog(@"abort: quitting took too long (after %g sec)\n",
(now - beganQuitting));
signal(SIGABRT, SIG_DFL);
abort();
}
return YES;
}
static RETSIGTYPE
ihandler(int sig)
{
static BOOL beenHere = NO;
signal(sig, ihandler);
if (NO == beenHere)
{
beenHere = YES;
signal(SIGABRT, SIG_DFL);
abort();
}
exit(sig);
#if RETSIGTYPE != void
return 0;
#endif
}
static RETSIGTYPE
qhandler(int sig)
{
if (SIGHUP == sig)
{
static int hupCount = 0;
/* We allow multiple HUP signals since, while shutting down we may
* attempt to write out messages to our terminal, generating more
* signals, and we want to ignore those and shut down cleanly.
*/
if (hupCount++ < 1000)
{
cmdSignalled = 0; // Allow signal to be set.
}
}
/* We store the signal value in a global variable and return to normal
* processing ... that way later code can check on the state of the
* variable and take action outside the handler.
* We can't act immediately here inside the handler as the signal may
* have interrupted some vital library (eg malloc()) and left it in a
* state such that our code can't continue. For instance if we try to
* cleanup after a signal and call free(), the process may hang waiting
* for a lock that the interupted function still holds.
*/
if (0 == cmdSignalled)
{
cmdSignalled = sig; // Record signal for event loop.
}
else
{
static BOOL beenHere = NO;
/* We have been signalled more than once ... so let's try to
* crash rather than continuing.
*/
if (NO == beenHere)
{
beenHere = YES;
signal(SIGABRT, SIG_DFL);
abort();
}
exit(cmdSignalled); // Exit with *first* signal number
}
#if RETSIGTYPE != void
return 0;
#endif
}
NSString*
cmdVersion(NSString *ver)
{
static NSString *version = @"1997-2013";
if (ver != nil)
{
ASSIGNCOPY(version, ver);
}
return version;
}
static NSString *homeDir = nil;
NSString*
cmdHomeDir()
{
return homeDir;
}
void
cmdSetHome(NSString *home)
{
ASSIGNCOPY(homeDir, home);
}
static NSString *logsDir = nil;
NSString*
ecLogsSubdirectory()
{
return logsDir;
}
void
ecSetLogsSubdirectory(NSString *pathComponent)
{
ASSIGNCOPY(logsDir, pathComponent);
}
static NSString *userDir = nil;
static NSString*
cmdUserDir()
{
if (userDir == nil)
return NSHomeDirectoryForUser(cmdUser);
else
return userDir;
}
static NSString*
cmdSetUserDirectory(NSString *dir)
{
if (dir == nil)
{
dir = NSHomeDirectoryForUser(cmdUser);
}
else if ([dir isAbsolutePath] == NO)
{
dir = [NSHomeDirectoryForUser(cmdUser)
stringByAppendingPathComponent: dir];
}
ASSIGNCOPY(userDir, dir);
return userDir;
}
static NSString *dataDir = nil;
/* Return the current data directory.
* Create the directory path if necessary.
*/
NSString*
cmdDataDir()
{
if (dataDir == nil)
{
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *str = cmdUserDir();
BOOL flag;
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
if ([mgr createDirectoryAtPath: str
withIntermediateDirectories: YES
attributes: nil
error: NULL] == NO)
{
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
NSLog(@"Unable to create directory - %@", str);
return nil;
}
}
else
{
flag = YES;
}
}
if (flag == NO)
{
NSLog(@"The path '%@' is not a directory", str);
return nil;
}
str = [str stringByAppendingPathComponent: @"Data"];
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
if ([mgr createDirectoryAtPath: str
withIntermediateDirectories: YES
attributes: nil
error: NULL] == NO)
{
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
NSLog(@"Unable to create directory - %@", str);
return nil;
}
}
else
{
flag = YES;
}
}
if (flag == NO)
{
NSLog(@"The path '%@' is not a directory", str);
return nil;
}
if (homeDir != nil)
{
str = [str stringByAppendingPathComponent: homeDir];
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
if ([mgr createDirectoryAtPath: str
withIntermediateDirectories: YES
attributes: nil
error: NULL] == NO)
{
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
NSLog(@"Unable to create directory - %@", str);
return nil;
}
}
else
{
flag = YES;
}
}
if (flag == NO)
{
NSLog(@"The path '%@' is not a directory", str);
return nil;
}
}
ASSIGNCOPY(dataDir, str);
}
return dataDir;
}
/* Return the current logging directory - if 'today' is not nil, treat it as
* the name of a subdirectory in which todays logs should be archived.
* Create the directory path if necessary.
*/
NSString*
cmdLogsDir(NSString *date)
{
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *str = cmdUserDir();
NSString *component;
BOOL flag;
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
if ([mgr createDirectoryAtPath: str
withIntermediateDirectories: YES
attributes: nil
error: NULL] == NO)
{
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
NSLog(@"Unable to create directory - %@", str);
return nil;
}
}
else
{
flag = YES;
}
}
if (flag == NO)
{
NSLog(@"The path '%@' is not a directory", str);
return nil;
}
component = ecLogsSubdirectory();
if (nil == component)
{
component = @"DebugLogs";
}
str = [str stringByAppendingPathComponent: component];
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
if ([mgr createDirectoryAtPath: str
withIntermediateDirectories: YES
attributes: nil
error: NULL] == NO)
{
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
NSLog(@"Unable to create directory - %@", str);
return nil;
}
}
else
{
flag = YES;
}
}
if (flag == NO)
{
NSLog(@"The path '%@' is not a directory", str);
return nil;
}
if (date != nil)
{
str = [str stringByAppendingPathComponent: date];
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
if ([mgr createDirectoryAtPath: str
withIntermediateDirectories: YES
attributes: nil
error: NULL] == NO)
{
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
NSLog(@"Unable to create directory - %@", str);
return nil;
}
}
else
{
flag = YES;
}
}
if (flag == NO)
{
NSLog(@"The path '%@' is not a directory", str);
return nil;
}
}
if (homeDir != nil)
{
str = [str stringByAppendingPathComponent: homeDir];
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
if ([mgr createDirectoryAtPath: str
withIntermediateDirectories: YES
attributes: nil
error: NULL] == NO)
{
if ([mgr fileExistsAtPath: str isDirectory: &flag] == NO)
{
NSLog(@"Unable to create directory - %@", str);
return nil;
}
}
else
{
flag = YES;
}
}
if (flag == NO)
{
NSLog(@"The path '%@' is not a directory", str);
return nil;
}
}
return str;
}
NSString*
cmdLogKey(EcLogType t)
{
switch (t)
{
case LT_DEBUG: return @"Debug";
case LT_WARNING: return @"Warn";
case LT_ERROR: return @"Error";
case LT_AUDIT: return @"Audit";
case LT_ALERT: return @"Alert";
case LT_CONSOLE: return @"Audit"; // Console messages are audited
default: return @"UnknownLogType";
}
}
NSString*
cmdLogName()
{
static NSString *cmdLogName = nil;
if (nil == cmdLogName)
{
[ecLock lock];
if (nil == cmdLogName)
{
NSString *n = cmdName;
if (nil == n)
{
n = [[NSProcessInfo processInfo] processName];
}
cmdLogName = [n copy];
}
[ecLock unlock];
}
return cmdLogName;
}
NSString*
cmdLogFormat(EcLogType t, NSString *fmt)
{
static NSString *h = nil;
NSCalendarDate *c = [[cDateClass alloc] init];
NSString *f = cmdLogKey(t);
NSString *n = cmdLogName();
NSString *d;
NSString *result;
if (h == nil)
{
h = [[[NSHost currentHost] wellKnownName] copy];
}
d = [c descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F %z" locale: nil];
result = [stringClass stringWithFormat: @"%@(%@): %@ %@ - %@\n",
n, h, d, f, fmt];
RELEASE(c);
return result;
}
EcProcess *EcProc = nil;
static NSConnection *EcProcConnection = nil;
static EcAlarmDestination *alarmDestination = nil;
static EcLogger *alertLogger = nil;
static EcLogger *auditLogger = nil;
static EcLogger *debugLogger = nil;
static EcLogger *errorLogger = nil;
static EcLogger *warningLogger = nil;
static NSMutableSet *cmdActions = nil;
static id cmdServer = nil;
static id cmdPTimer = nil;
static NSDictionary *cmdConf = nil;
static NSDate *cmdFirst = nil;
static NSDate *cmdLast = nil;
static BOOL cmdIsTransient = NO;
static NSMutableSet *cmdDebugModes = nil;
static NSMutableDictionary *cmdDebugKnown = nil;
static NSMutableString *replyBuffer = nil;
static SEL cmdTimSelector = 0;
static NSTimeInterval cmdTimInterval = 60.0;
static NSMutableArray *noNetConfig = nil;
static NSMutableDictionary *servers = nil;
static int coreSize = -2; // Not yet set
static BOOL __hasLSAN = NO;
static BOOL __setLSAN = NO;
int __lsan_do_recoverable_leak_check(void) __attribute__((weak));
static int (*ecLeakCheck)(void) = __lsan_do_recoverable_leak_check;
static BOOL
hasLSAN()
{
if (NO == __setLSAN)
{
if (ecLeakCheck)
{
__hasLSAN = YES;
}
__setLSAN = YES;
}
return __hasLSAN;
}
static NSString *hostName = nil;
static NSString *
ecHostName()
{
NSString *name;
[ecLock lock];
if (nil == hostName)
{
hostName = [[[NSHost currentHost] wellKnownName] retain];
}
name = [hostName retain];
[ecLock unlock];
return [name autorelease];
}
static NSString *fullName = nil;
NSString *
ecFullName()
{
NSString *name;
[ecLock lock];
if (nil == fullName)
{
fullName = [[NSString alloc] initWithFormat: @"%@:%@",
ecHostName(), cmdLogName()];
}
name = [fullName retain];
[ecLock unlock];
return [name autorelease];
}
static EcAlarmSeverity memAlarm = EcAlarmSeverityMajor;
static NSString *memType = nil;
static NSString *memUnit = @"KB";
static int memSize = 1024; // Report KB
static NSTimeInterval memTime = 0.0; // Time of last check
static uint64_t memMaximum = 0;
static uint64_t memAllowed = 0;
static uint64_t memInitial = 0;
static uint64_t excAvge = 0; // current period average
static uint64_t memAvge = 0; // current period average
static uint64_t excStrt = 0; // excluded usage at first check
static uint64_t memStrt = 0; // total usage at first check
static uint64_t excLast = 0; // excluded usage at last check
static uint64_t memLast = 0; // total usage at last check
static uint64_t excPrev = 0; // excluded usage at previous warning
static uint64_t memPrev = 0; // total usage at previous warning
static uint64_t excPeak = 0; // excluded peak usage
static uint64_t memPeak = 0; // total peak usage
static uint64_t memBase = 0; // base memory threshold
static uint64_t memWarn = 0; // warning alarm threshold
static uint64_t memMinr = 0; // minor alarm threshold
static uint64_t memMajr = 0; // major alarm threshold
static uint64_t memCrit = 0; // critical aarm threshold
static uint64_t memSlot = 0; // minute counter
static uint64_t excRoll[10]; // last N values
static uint64_t memRoll[10]; // last N values
#define MEMCOUNT (sizeof(memRoll)/sizeof(*memRoll))
static NSString*
setMemAlarm(NSString *str)
{
if (nil == str) str = @"Major";
if ([str caseInsensitiveCompare: @"critical"] == NSOrderedSame)
{
memAlarm = EcAlarmSeverityCritical;
}
else if ([str caseInsensitiveCompare: @"major"] == NSOrderedSame)
{
memAlarm = EcAlarmSeverityMajor;
}
else if ([str caseInsensitiveCompare: @"minor"] == NSOrderedSame)
{
memAlarm = EcAlarmSeverityMinor;
}
else if ([str caseInsensitiveCompare: @"warning"] == NSOrderedSame)
{
memAlarm = EcAlarmSeverityWarning;
}
else
{
memAlarm = EcAlarmSeverityMajor;
}
return [EcAlarm stringFromSeverity: memAlarm];
}
static void
setMemBase()
{
if (0 == memAllowed)
{
if (0 == memBase || memSlot < MEMCOUNT)
{
/* Base must be 20% larger than peak over first ten minutes.
*/
memBase = (memPeak * 120) / 100;
/* The base memory must be at least half the maximum memory.
*/
if (memMaximum > 0 && memBase < memMaximum * 1024 * 512)
{
memBase = memMaximum * 1024 * 512;