-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmrbig.c
1140 lines (1015 loc) · 25.7 KB
/
mrbig.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
#include "mrbig.h"
#define MEMSIZE 4
#define PATTERN_SIZE (sizeof big_pattern)
#define CHUNKS_MAX 10000
static char cfgfile[256];
char mrmachine[256], bind_addr[256] = "0.0.0.0";
static struct display {
struct sockaddr_in in_addr;
int s;
char* pdata;
int remaining;
struct display *next;
} *mrdisplay;
char cfgdir[256];
char pickupdir[256];
char now[1024];
static FILE *logfp = NULL;
static int mrport, mrsleep, mrloop;
int bootyellow, bootred;
double dfyellow, dfred;
int cpuyellow, cpured;
int memyellow, memred;
int debug = 0;
int dirsep;
int msgage;
int memsize = MEMSIZE;
int standalone = 0;
int report_size = 16384;
/* nosy memory management */
static int debug_memory = 0;
struct memchunk {
void *p;
size_t n;
char cl[20];
} chunks[CHUNKS_MAX];
static unsigned char big_pattern[] = {
1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
// 1,2,3,4,5,6,7,8,9,0, 1,2,3,4,5,6,7,8,9,0,
'1','2','3','4','5','6','7','8','9','0',
// '1','2','3','4','5','6','7','8','9','0',
// '1','2','3','4','5','6','7','8','9','0',
// '1','2','3','4','5','6','7','8','9','0',
// '1','2','3','4','5','6','7','8','9','0',
'q','w','e','r','t','y'
};
void mrlog(char *fmt, ...)
{
FILE *fp;
va_list ap;
if (standalone) fp = stderr;
else fp = logfp;
if (!fp) return;
va_start(ap, fmt);
vfprintf(fp, fmt, ap);
va_end(ap);
fprintf(fp, "\n");
fflush(fp);
}
#if 1
// Use this for logs that need to be written before there is a logfp
void startup_log(char *fmt, ...)
{
FILE *fp;
va_list ap;
fp = fopen("mrlog.log", "a");
if (!fp) return;
va_start(ap, fmt);
vfprintf(fp, fmt, ap);
va_end(ap);
fprintf(fp, "\n");
fclose(fp);
}
#else
void startup_log(char *fmt, ...)
{
return;
}
#endif
static void mrexit(char *reason, int status)
{
mrlog("mrexit(%s, %d)", reason, status);
startup_log("mrexit(%s, %d)", reason, status);
if (logfp) fclose(logfp);
exit(status);
}
static int check_chunk(int i)
{
unsigned char *q = chunks[i].p+chunks[i].n-PATTERN_SIZE;
if (memcmp(q, big_pattern, PATTERN_SIZE)) {
mrlog("Chunk %p (%s) has been tampered with",
chunks[i].p, chunks[i].cl);
for (i = 0; i < PATTERN_SIZE; i++) {
if (q[i] != big_pattern[i]) {
if (isprint(q[i])) {
mrlog("%d: '%c'", i, (int)q[i]);
} else {
mrlog("%d: %d", i, (int)q[i]);
}
}
}
return 1;
}
return 0;
}
void check_chunks(char *msg)
{
int i, corrupt = 0;
if (debug) mrlog("check_chunks(%s)", msg);
for (i = 0; i < CHUNKS_MAX; i++) {
if (chunks[i].p) {
corrupt |= check_chunk(i);
}
}
if (corrupt) {
mrlog("Memory corruption detected");
mrexit("check_chunks found memory corruption", EXIT_FAILURE);
} else {
if (debug) mrlog("Memory checks out OK");
}
}
static void dump_chunks(void)
{
int i, n;
mrlog("Chunks:");
n = 0;
for (i = 0; i < CHUNKS_MAX; i++) {
if (chunks[i].p) {
mrlog("%d: '%s' (%p) %ld bytes", i,
chunks[i].cl, chunks[i].p, (long)chunks[i].n);
n++;
}
}
mrlog("Total %d chunks", n);
}
static void store_chunk(void *p, size_t n, char *cl)
{
int i;
if (!debug_memory) return;
for (i = 0; i < CHUNKS_MAX; i++)
if (chunks[i].p == NULL) break;
if (i == CHUNKS_MAX) {
mrlog("No empty chunk slot for %p (%s), exiting", p, cl);
dump_chunks();
mrexit("store_chunk is out of slots", EXIT_FAILURE);
}
if (debug >= 3) mrlog("Storing chunk %p (%s) of %ld bytes in slot %d",
p, cl, (long)n, i);
chunks[i].p = p;
chunks[i].n = n;
mrlog("In store_chunk: i = %d", i);
strlcpy(chunks[i].cl, cl, 20);
mrlog("In store_chunk: i = %d", i);
memcpy(p+n-PATTERN_SIZE, big_pattern, PATTERN_SIZE);
}
static void remove_chunk(void *p, char *cl)
{
int i;
if (!debug_memory) return;
for (i = 0; i < CHUNKS_MAX; i++)
if (chunks[i].p == p) break;
if (i == CHUNKS_MAX) {
mrlog("Can't find chunk %p (%s)", p, cl);
dump_chunks();
mrlog("Continuing even though I can't find chunk %p (%s)",
p, cl);
}
if (check_chunk(i)) mrexit("remove_chunk: check not ok", EXIT_FAILURE);
if (debug >= 3) {
mrlog("Removing chunk %p (%s) from slot %d",
p, chunks[i].cl, i);
}
chunks[i].p = NULL;
}
void *big_malloc(char *p, size_t n)
{
void *a;
size_t m;
if (debug_memory) {
mrlog("big_malloc(%s, %ld)", p, (long)n);
m = memsize*(n+PATTERN_SIZE);
} else {
m = memsize*n;
}
a = malloc(m);
if (debug > 2) {
mrlog("Allocating %ld bytes (%p) on behalf of %s",
(long)m, a, p);
}
if (a == NULL) {
mrlog("Allocation '%s' failed, exiting", p);
mrexit("Out of memory", EXIT_FAILURE);
}
store_chunk(a, m, p);
return a;
}
void *big_realloc(char *p, void *q, size_t n)
{
void *a;
size_t m;
if (debug_memory) {
mrlog("big_realloc(%s, %p, %ld)", p, q, n);
m = memsize*(n+PATTERN_SIZE);
} else {
m = memsize*n;
}
remove_chunk(q, p);
a = realloc(q, m);
if (debug > 2) {
mrlog("Reallocating %ld bytes (%p => %p) on behalf of %s",
(long)m, q, a, p);
}
if (a == NULL) {
mrlog("Allocation '%s' failed, exiting", p);
mrexit("Out of memory", EXIT_FAILURE);
}
store_chunk(a, m, p);
return a;
}
void big_free(char *p, void *q)
{
if (q == NULL) {
if (debug) mrlog("No need to free %p on behalf of %s", q, p);
return;
}
if (debug > 2) mrlog("Freeing %p on behalf of %s", q, p);
remove_chunk(q, p);
free(q);
}
char *big_strdup(char *p, char *q)
{
char *a = big_malloc(p, strlen(q)+1);
return strcpy(a, q);
}
struct filechunk {
FILE *p;
char cl[20];
} files[100];
static void dump_files(void)
{
int i, n;
mrlog("Files:");
n = 0;
for (i = 0; i < 100; i++) {
if (files[i].p) {
mrlog("%d: '%s' (%p)", i, files[i].cl, files[i].p);
n++;
}
}
mrlog("Total %d files", n);
}
static void store_file(FILE *p, char *cl)
{
int i;
if (debug < 2) return;
mrlog("store_file(%p, %s)", p, cl);
for (i = 0; i < 100; i++)
if (files[i].p == NULL) break;
if (i == 100) {
mrlog("No empty files slot, exiting");
dump_files();
mrexit("Out of file slots", EXIT_FAILURE);
}
mrlog("Storing file %p (%s) in slot %d", p, cl, i);
dump_files();
files[i].p = p;
strlcpy(files[i].cl, cl, 20);
}
static void remove_file(FILE *p)
{
int i;
if (debug < 2) return;
mrlog("remove_file(%p)", p);
for (i = 0; i < 100; i++)
if (files[i].p == p) break;
if (i == 100) {
mrlog("Can't find file, exiting");
dump_files();
mrlog("Continuing even though I can't find file %p", p);
}
mrlog("Removing file %p (%s) from slot %d", p, files[i].cl, i);
dump_files();
files[i].p = NULL;
}
FILE *big_fopen(char *p, char *file, char *mode)
{
FILE *fp = fopen(file, mode);
if (debug > 1) {
mrlog("Opening '%s' in mode %s (%p) on behalf of %s",
file, mode, fp, p);
}
if (fp == NULL) {
mrlog("Can't open");
} else {
store_file(fp, p);
}
return fp;
}
int big_fclose(char *p, FILE *fp)
{
int n = fclose(fp);
if (debug > 1) mrlog("Closing %p on behalf of %s", fp, p);
if (n) {
mrlog("Can't close file");
} else {
remove_file(fp);
}
return n;
}
/* Remove the annoying carriage returns that Windows puts in text files */
void no_return(char *p)
{
char *q = p;
while (*p) {
if (*p != '\r') *q++ = *p;
p++;
}
*q = '\0';
}
int snprcat(char *str, size_t size, const char *fmt, ...)
{
int n, r;
va_list ap;
va_start(ap, fmt);
n = strlen(str);
r = vsnprintf(str+n, size-n, fmt, ap);
str[size-1] = '\0';
return r;
}
struct gracetime {
char *test;
time_t grace;
struct gracetime *next;
} *gracetime;
struct option {
char *name;
struct option *next;
} *options;
char *get_option(char *n, int partial)
{
struct option *o;
int l = strlen(n);
for (o = options; o; o = o->next) {
if ((partial && !strncmp(n, o->name, l))
|| !strcmp(n, o->name)) return o->name;
}
return NULL;
}
static struct option *insert_option(char *n)
{
struct option *o = big_malloc("insert_option", sizeof *o);
o->name = big_strdup("insert_option", n);
o->next = options;
options = o;
return o;
}
static void free_options(void)
{
struct option *o;
while ((o = options)) {
options = o->next;
big_free("free_optionlist", o->name);
big_free("free_optionlist", o);
}
}
static void insert_grace(char *test, time_t grace)
{
struct gracetime *gt = big_malloc("insert_grace", sizeof *gt);
gt->test = big_strdup("insert_grace", test);
gt->grace = grace;
gt->next = gracetime;
gracetime = gt;
}
static void free_grace(void)
{
struct gracetime *gt;
while (gracetime) {
gt = gracetime;
gracetime = gracetime->next;
big_free("free_grace", gt->test);
big_free("free_grace", gt);
}
}
static time_t lookup_grace(char *test)
{
struct gracetime *gt;
for (gt = gracetime; gt; gt = gt->next) {
if (!strcmp(test, gt->test)) return gt->grace;
}
return 0;
}
static void readcfg(void)
{
char b[256], key[256], value[256], *p;
struct display *mp;
int i;
if (debug > 1) mrlog("readcfg()");
/* Set all defaults */
strlcpy(mrmachine, "localhost", sizeof mrmachine);
mrport = 1984;
while (mrdisplay) {
mp = mrdisplay;
mrdisplay = mp->next;
big_free("readcfg: display", mp);
}
free_grace();
free_options();
mrsleep = 300;
mrloop = INT_MAX;
bootyellow = 60;
bootred = 30;
dfyellow = 90;
dfred = 95;
cpuyellow = 80;
cpured = 90;
memyellow = 100;
memred = 100;
msgage = 3600;
memsize = MEMSIZE;
pickupdir[0] = '\0';
if (logfp) big_fclose("readcfg:logfile", logfp);
logfp = NULL;
for (i = 0; get_cfg("mrbig", b, sizeof b, i); i++) {
if (b[0] == '#') continue;
if (sscanf(b, "%s %[^\n]", key, value) == 2) {
if (!strcmp(key, "machine")) {
strlcpy(mrmachine, value, sizeof mrmachine);
} else if (!strcmp(key, "port")) {
mrport = atoi(value);
} else if (!strcmp(key, "display")) {
mp = big_malloc("readcfg: display", sizeof *mp);
memset(&mp->in_addr, 0, sizeof mp->in_addr);
mp->in_addr.sin_family = AF_INET;
p = strchr(value, ':');
if (p) {
*p++ = '\0';
mp->in_addr.sin_port = htons(atoi(p));
} else {
mp->in_addr.sin_port = htons(mrport);
}
mp->in_addr.sin_addr.s_addr = inet_addr(value);
mp->next = mrdisplay;
mrdisplay = mp;
} else if (!strcmp(key, "sleep")) {
mrsleep = atoi(value);
} else if (!strcmp(key, "loop")) {
mrloop = atoi(value);
} else if (!strcmp(key, "bootyellow")) {
bootyellow = atoi(value);
} else if (!strcmp(key, "bootred")) {
bootred = atoi(value);
} else if (!strcmp(key, "debug")) {
debug = atoi(value);
} else if (!strcmp(key, "cpuyellow")) {
cpuyellow = atoi(value);
} else if (!strcmp(key, "cpured")) {
cpured = atoi(value);
} else if (!strcmp(key, "dfyellow")) {
dfyellow = atof(value);
} else if (!strcmp(key, "dfred")) {
dfred = atof(value);
} else if (!strcmp(key, "memyellow")) {
memyellow = atof(value);
} else if (!strcmp(key, "memred")) {
memred = atof(value);
} else if (!strcmp(key, "cfgdir")) {
strlcpy(cfgdir, value, sizeof cfgdir);
} else if (!strcmp(key, "msgage")) {
msgage = atoi(value);
} else if (!strcmp(key, "pickupdir")) {
strlcpy(pickupdir, value, sizeof pickupdir);
} else if (!strcmp(key, "logfile")) {
logfp = big_fopen("readcfg:logfile", value, "a");
} else if (!strcmp(key, "gracetime")) {
char test[1000];
int grace = 0;
sscanf(value, "%s %d", test, &grace);
insert_grace(test, grace);
} else if (!strcmp(key, "report_size")) {
report_size = atoi(value);
} else if (!strcmp(key, "option")) {
insert_option(value);
} else if (!strcmp(key, "memsize")) {
memsize = atoi(value);
} else if (!strcmp(key, "set")) {
char key[1000], value[1000];
key[0] = value[0] = '\0';
sscanf(value, "%s %s", key, value);
mrlog("This doesn't actually do anything");
}
}
}
/* Replace . with , in fqdn (historical reasons) */
for (p = mrmachine; *p; p++) {
if (*p == '.') *p = ',';
}
/* Make sure the main loop executes at least every mrsleep seconds */
if (mrloop > mrsleep) mrloop = mrsleep;
}
static WSADATA wsaData;
static int ws_started = 0;
int start_winsock(void)
{
int n;
if (debug > 1) mrlog("start_winsock()");
if (!ws_started) {
n = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (n != NO_ERROR) {
mrlog("Error at WSAStartup() [%d]", WSAGetLastError());
} else {
if (debug) mrlog("Winsock started");
ws_started = 1;
}
}
return ws_started;
}
void stop_winsock(void)
{
WSACleanup();
ws_started = 0;
}
struct teststatus {
char *machine, *test, *color;
time_t last;
time_t grace;
struct teststatus *next;
};
static struct teststatus *teststatus = NULL;
/*
Insert status into list.
Return 0 if color is unchanged and sleep time is not exceeded.
Return 1 if color is non-green but grace time is not exceeded.
Return 2 otherwise.
*/
static int insert_status(char *machine, char *test, char *color)
{
struct teststatus *s;
time_t now = time(NULL);
time_t grace = lookup_grace(test);
if (debug > 1) mrlog("insert_status(%s, %s, %s)", machine, test, color);
for (s = teststatus; s; s = s->next) {
if (!strcmp(machine, s->machine) &&
!strcmp(test, s->test)) {
if (debug) mrlog("insert_status found match");
break;
}
}
if (s == NULL) {
/* We have never seen this test before.
Allocate a new structure with initial values
and tell mrsend to report the real status to the bbd.
*/
s = big_malloc("insert_status: node", sizeof *s);
s->machine = big_strdup("insert_status: machine", machine);
s->test = big_strdup("insert_status: test", test);
s->next = teststatus;
if (debug) mrlog("insert_status: new test");
s->color = big_strdup("insert_status: color", color);
s->last = now;
s->grace = 0;
teststatus = s;
return 2;
}
if (!strcmp(color, s->color)) {
/* If status is unchanged, check the time.
*/
if (now < s->last) {
/* Someone adjusted the time or something.
Reset and tell mrsend to report the real status.
*/
mrlog("insert_status: Time has decreased!");
s->last = now;
s->grace = 0;
return 2;
}
if (now-s->last >= mrsleep) {
/* We must send a report or the display will
turn purple.
*/
if (debug) mrlog("insert_status: mrsleep exceeded");
s->last = now;
s->grace = 0;
return 2;
}
/* No need to do anything.
*/
return 0;
}
/* We now know that the current status is different from the
one last reported to the bbd.
*/
if (!strcmp(color, "green")) {
/* If the new status is green, insert the new status,
reset gracetime and report the real status.
*/
big_free("insert_status: color", s->color);
s->color = big_strdup("insert_status: color", color);
s->last = now;
s->grace = 0;
return 2;
}
if (strcmp(s->color, "green")) {
/* If the old status was anything but green, insert
the new status, reset gracetime and report the
real status.
*/
big_free("insert_status: color", s->color);
s->color = big_strdup("insert_status: color", color);
s->last = now;
s->grace = 0;
return 2;
}
/* We now know that the old status was green and the new
status is non-green.
*/
if (s->grace == 0) {
/* Start the clock ticking.
*/
s->grace = now+grace;
}
if (now >= s->grace) {
/* Grace time expired, send a real status report.
*/
big_free("insert_status: color", s->color);
s->color = big_strdup("insert_status: color", color);
s->last = now;
return 2;
}
/* Sit on our hands for a while. Tell mrsend to send a
green status to the bbd.
*/
s->last = now;
return 1;
}
/*
Format is: "status machine,domain,com.test colour [message]"
We can optimise this by parsing out the test name and the colour
and only send something if the colour has changed for this test
*or* the bbsleep time is exceeded. Then we can run the main loop
as often as we want without putting any more load on the bbd.
*/
//void mrsend(char *p)
void mrsend(char *machine, char *test, char *color, char *message)
{
struct display *mp;
struct sockaddr_in my_addr;
struct linger l_optval;
unsigned long nonblock;
char *p = NULL;
int is;
if (debug > 1) mrlog("mrsend(%s, %s, %s, %s)", machine, test, color, message);
#if 0
n = sscanf(p, "status %199[^.].%99[^ ] %99s",
machine, test, color);
if (n != 3) {
mrlog("Bogus string in mrsend, process anyway");
if (debug) mrlog("%s", p);
}
#endif
is = insert_status(machine, test, color);
if (is == 0) {
if (debug) mrlog("mrsend: no change, nothing to do");
return;
}
if (!start_winsock()) return;
/* Prepare the report */
p = big_malloc("mrsend()", report_size+1);
p[0] = '\0';
if (is == 1) {
snprcat(p, report_size, "status %s.%s green %s",
machine, test, message);
} else {
snprcat(p, report_size, "status %s.%s %s %s",
machine, test, color, message);
}
for (mp = mrdisplay; mp; mp = mp->next) {
mp->s = -1;
}
for (mp = mrdisplay; mp; mp = mp->next) {
mp->s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (mp->s == -1) {
mrlog("mrsend: socket failed: %d", WSAGetLastError());
continue;
}
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = 0;
my_addr.sin_addr.s_addr = inet_addr(bind_addr);
if (bind(mp->s, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
mrlog("mrsend: bind(%s) failed: [%d]", bind_addr, WSAGetLastError());
closesocket(mp->s);
mp->s = -1;
continue;
}
l_optval.l_onoff = 1;
l_optval.l_linger = 5;
nonblock = 1;
if (ioctlsocket(mp->s, FIONBIO, &nonblock) == SOCKET_ERROR) {
mrlog("mrsend: ioctlsocket failed: %d", WSAGetLastError());
closesocket(mp->s);
mp->s = -1;
continue;
}
if (setsockopt(mp->s, SOL_SOCKET, SO_LINGER, (const char*)&l_optval, sizeof(l_optval)) == SOCKET_ERROR) {
mrlog("mrsend: setsockopt failed: %d", WSAGetLastError());
closesocket(mp->s);
mp->s = -1;
continue;
}
if (debug) mrlog("Using address %s, port %d\n",
inet_ntoa(mp->in_addr.sin_addr),
ntohs(mp->in_addr.sin_port));
if (connect(mp->s, (struct sockaddr *)&mp->in_addr, sizeof(mp->in_addr)) == SOCKET_ERROR) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
mrlog("mrsend: connect: %d", WSAGetLastError());
l_optval.l_onoff = 0;
l_optval.l_linger = 0;
setsockopt(mp->s, SOL_SOCKET, SO_LINGER, (const char*)&l_optval, sizeof(l_optval));
closesocket(mp->s);
mp->s = -1;
continue;
}
}
mp->pdata = p;
mp->remaining = strlen(p);
}
time_t start_time = time(NULL);
for(;;) {
struct timeval timeo;
fd_set wfds;
int len;
int tot_remaining;
timeo.tv_sec = 1;
timeo.tv_usec = 0;
FD_ZERO(&wfds);
tot_remaining = 0;
for (mp = mrdisplay; mp; mp = mp->next) {
if (mp->s != -1) {
if (mp->remaining > 0) {
FD_SET(mp->s, &wfds);
tot_remaining += mp->remaining;
}
}
}
if (tot_remaining == 0) {
/* all data sent to displays */
goto cleanup;
}
if (time(NULL) > start_time + 10 || time(NULL) < start_time) {
mrlog("mrsend: send loop timed out");
/* this should not take more than 10 seconds. Network problem, so bail out */
goto cleanup;
}
select(255 /* ignored on winsock */, NULL, &wfds, NULL, &timeo);
for (mp = mrdisplay; mp; mp = mp->next) {
if (mp->s != -1) {
if (mp->remaining > 0) {
len = send(mp->s, mp->pdata, mp->remaining, 0);
if (len == SOCKET_ERROR) {
continue;
}
mp->pdata += len;
mp->remaining -= len;
if (mp->remaining == 0) {
shutdown(mp->s, SD_BOTH);
}
}
}
}
}
cleanup:
/* initiate socket shutdowns */
for (mp = mrdisplay; mp; mp = mp->next) {
if (mp->s != -1) {
shutdown(mp->s, SD_BOTH);
}
}
/* gracefully terminate sockets, finally applying force */
for (mp = mrdisplay; mp; mp = mp->next) {
int i;
if (mp->s != -1) {
for (i = 0; i < 10; i++) {
if (closesocket(mp->s) == WSAEWOULDBLOCK) {
Sleep(1000); /* wait for all data to be sent */
}
}
/* force the socket shut */
l_optval.l_onoff = 0;
l_optval.l_linger = 0;
setsockopt(mp->s, SOL_SOCKET, SO_LINGER, (const char*)&l_optval, sizeof(l_optval));
closesocket(mp->s);
mp->s = -1;
}
}
big_free("mrsend()", p);
}
#ifdef _WIN64
#define DWORD_REG uint64_t
#else
#define DWORD_REG uint32_t
#endif
static volatile int double_fault = 0;
static LONG CALLBACK
VectoredExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) {
EXCEPTION_RECORD* e = ExceptionInfo->ExceptionRecord;
CONTEXT* ctx = ExceptionInfo->ContextRecord;
DWORD_REG frame;
DWORD_REG prevframe;
DWORD_REG ret;
int i;
FILE* f;
time_t t;
if (double_fault != 0)
return EXCEPTION_CONTINUE_SEARCH;
double_fault = 1;
f = fopen("c:\\mrbig_crash.txt", "a");
if (!f)
return EXCEPTION_CONTINUE_SEARCH;
t = time(NULL);
fprintf(f, "MrBig detected an unhandled Exception at %s", ctime(&t));
fprintf(f, "ExceptionCode: 0x%08X\n", (unsigned int)e->ExceptionCode);
fprintf(f, "ExceptionFlags: 0x%08X\n", (unsigned int)e->ExceptionFlags);
fprintf(f, "ExceptionAddress: 0x%p\n", e->ExceptionAddress);
fprintf(f, "NumberParameters: 0x%08X\n", (unsigned int)e->NumberParameters);
for (i = 0; i < e->NumberParameters; i++) {
fprintf(f, "ExceptionInformation[%i]: 0x%08X\n", i, (unsigned int)e->ExceptionInformation[i]);
}
fprintf(f, "CALL STACK:\n");
fprintf(f, "crash: 0x%p\n", e->ExceptionAddress);
#ifdef _WIN64
frame = ctx->Rbp;
#else
frame = ctx->Ebp;
#endif
for (i = 0; i < 32; i++) {
fflush(f);
if (frame < 0x1000) {
fprintf(f, "frame %p looks invalid. stop.\n", (void*)frame);
break;
}
prevframe = *(DWORD_REG*)frame;
ret = *(((DWORD_REG*)frame) + 1);
fprintf(f, "frame %p (called from %p) (%p, %p, %p, %p)\n", (void*)frame, (void*)ret,
(void*)*(((DWORD_REG*)frame) + 2),
(void*)*(((DWORD_REG*)frame) + 3),
(void*)*(((DWORD_REG*)frame) + 4),
(void*)*(((DWORD_REG*)frame) + 5));
frame = prevframe;
}
fprintf(f, "CONTEXT:\n");
for (i = 0; i < sizeof(CONTEXT); i += 4) {
fprintf(f, " %08X", *(uint32_t*)((uintptr_t)ctx + i));
if ((i % 32) == 28)
fprintf(f, "\n");
}
fprintf(f, "\n");
fclose(f);
return EXCEPTION_CONTINUE_SEARCH;
};
void mrbig(void)
{
char *p;
time_t t, lastrun;
int sleeptime, i;
char hostname[256];
DWORD hostsize;
/*
* install exception logging/stacktrace handler.
* We need to resolve the symbol at run time because windows 2000
* does not have it.
*/
do {
HMODULE dll;
PVOID (*ptr) (ULONG First,PVECTORED_EXCEPTION_HANDLER Handler) = NULL;
dll = LoadLibraryEx("kernel32.dll", NULL, 0);
if (dll == NULL)
break;
ptr = (typeof(ptr))GetProcAddress(dll, "AddVectoredExceptionHandler");
if (ptr == NULL)
break;
ptr(1, VectoredExceptionHandler);
mrlog("VectoredExceptionHandler handler has been installed");
} while(0);
if (debug) {
mrlog("mrbig()");
}