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
|
--- NetGamesUSA.com/ngStats/ngStatsUT.cfg Thu Oct 18 18:27:19 2001
+++ NetGamesUSA.com/ngStats/ngStatsUT.cfg Thu Oct 18 18:27:19 2001
@@ -93,7 +93,7 @@
# false = do not create graphs
-SpawnBrowser true
+SpawnBrowser false
#SpawnBrowser = Advanced option for those running dedicated servers on which it
#is undesirable to have ngStatsUT.exe automatically launch the default web browser
--- NetGamesUSA.com/ngWorldStats/bin/bgWorldStats Thu Jan 1 01:00:00 1970
+++ NetGamesUSA.com/ngWorldStats/bin/bgWorldStats Thu Oct 18 18:27:19 2001
@@ -0,0 +1,277 @@
+#!/bin/bash
+defopts=-vrl
+
+# bgWorldStats 1.2
+# Copyright (c) 2001 "Ouch@Schooner.COM"
+#
+# Permission to use, copy, modify, and distribute this software and its
+# documentation for any purpose and without fee is hereby granted,
+# provided that the above copyright notice appear in all copies.
+# The author makes no representations about the suitability of this
+# software for any purpose. It is provided "as is" without express
+# or implied warranty.
+#
+# http://illuminati.guildhappy.com/
+#
+# This will run the NG stats sender in the background, immediately returning
+# to UT.
+#
+# Installation:
+# Put this script (chmod a+x) into same directory as the existing
+# ngWorldStats binary:
+# <UT>/NetGamesUSA.com/ngWorldStats/bin
+# and call it "bgWorldStats"
+#
+# Change your server's .ini to include this:
+#
+# WorldBatcherURL=../NetGamesUSA.com/ngWorldStats/bin/bgWorldStats
+# WorldBatcherParams=-vrl
+#
+# Or, edit the "defopts" line above to include "-vrl"
+#
+# NOTICE:
+# This script has only been tested on FreeBSD with the Linux UT server
+# started with the "-nohomedir" option.
+#
+
+#
+# Inspired by ngWorldStats.sh, originally by <abfackeln@abfackeln.com>
+# and modified (v1.2) by Leif Sawyer <leif@gci.net>
+#
+
+#
+# Change history:
+# 2001-02-26 1.2 released
+# close leaked fd's (bash required), add -C and -R options
+# 2001-01-30 1.1 released
+# first public release
+
+Usage() {
+ echo 2>&1 Usage: `basename $0` '[-vlrDCR] [-s statsbin] [-d logsdir] [-g gametype]
+ Options:
+ -v verbose ngWorldStats output
+ -r carry forward last result code
+ -l direct output to transcript log (only saved if an error occurs)
+
+ Debugging (only when needed):
+ -D enable debugging output
+ -C do not close leaked file descriptors
+ -R do not remove transcript logs of successful runs
+
+ Change defaults:
+ -d logs directory path (if relative, must be from <UT>/System)
+ ["'$logs'"]
+ -g game type ["UT"]
+ -s ngWorldStats program to use ["ngWorldStats"] - not a pathname'
+ exit 2
+}
+
+# UT runs us as "../NetGamesUSA.com/ngWorldStats/bin/bgWorldStats"
+# UT execs us from the "<UT>/System" directory.
+
+bin=`dirname $0`
+logs=$bin/../logs
+
+args=`getopt DCRrvls:g:d: $defopts $*`
+if [ $? != 0 ]; then Usage; fi
+oargs=$@
+set -- $args
+for i
+do
+ case "$1" in
+ -D) debug=true ;;
+ -v) verbose=-v ;;
+ -r) doresults=true ;;
+ -l) dologfile=true ;;
+ -R) dontcleanlogs=true ;;
+ -C) dontclose=false ;;
+ -s) statsbin=$2; shift ;;
+ -d) logs=$2; shift ;;
+ -g) game=$2; shift ;;
+ --) shift; break ;;
+ esac
+ shift
+done
+case $# in
+0) ;;
+*) Usage;;
+esac
+
+#
+# Main variables
+#
+
+# ngWorldStats executable
+stats=$bin/${statsbin:-ngWorldStats}
+
+# game
+game=${game:-UT}
+
+# Saved ngWorldStats result from last run
+results=$logs/.lastresult
+
+# When we were run
+now=`date +%y%m%d-%H%M`
+
+if ${dologfile-false}
+then
+ # redirect stdout to log file of our run
+ logf=$logs/.ngrun.$now
+ exec > $logf 2>&1
+fi
+
+if ${debug-false}
+then
+ echo Started at: `date`
+ echo Called as: $0 $oargs
+ echo In Directory: `pwd`
+ echo ""
+fi
+
+# ngWorldStats exit codes
+ngE=1 # error (?)
+ngNoLog=83 # no logfiles
+ngBusy=97 # ngWorldStats already running
+ngOK=99 # ngWorldStats successful
+ngSyntax=139 # Server Syntax error
+
+#
+# Ugly shell locking code
+#
+# All systems don't have shlock(1) (from INN) or lockfile (1) (from procmail);
+# FreeBSD has lockf(1) but it doesn't have the semantics I want here.
+# So this makes up for it in a "somewhat" portable, safe fashion, under
+# the assumption that there isn't going to be significant contention.
+#
+# Lock using file path $1, returning an error message
+# (with original lock time and pid) if it fails.
+#
+# return 0 for OK; 1 for locked; 2 for lock file error
+# Error message to stdout
+#
+Lock() {
+ l=$1
+ if ! mkdir $l >/dev/null 2>&1
+ then
+ if [ ! -d $l ]
+ then
+ echo "ERROR: $l not a lock dir"
+ return 2
+ elif [ ! -r $l/lock ]
+ then
+ # race?
+ echo "ERROR: $l does not have a lock info file"
+ return 2
+ fi
+ read p d < $l/lock
+ # still around?
+ if ps p$p >/dev/null 2>&1
+ then
+ echo ALREADY LOCKED: by pid $p at $d
+ return 1
+ fi
+ # must be stale, override it
+ echo 1>&2 "STALE LOCK REMOVED: by pid $p at $d"
+ fi
+ echo $$ `date` > $l/lock
+ return 0
+}
+Unlock() {
+ l=$1
+ rm -rf $l
+}
+
+#
+# Start the real work here
+#
+
+#
+# Take a lock, or return busy to the UT server
+#
+
+lockf=$logs/.ngWS.lock
+ret=`Lock $lockf`
+if [ $? != 0 ]
+then
+ echo Lock error: $ret
+ exit $ngBusy
+fi
+
+#
+# UT leaks file descriptors (including the listening socket) to us.
+# If we don't close these, the next server instance won't be able to bind to
+# the ServerQueryPort (i.e., 7778) and will pick the next available one
+# (i.e., 7783), even if we are in the background
+#
+# How many leaked? Lots. On my server it was fds 3..70, with 8 of those
+# sockets. I don't know if it varies, so I just close "a whole bunch".
+#
+close-fds() {
+ ${debug-false} && fstat -p$$
+ fd=3
+ while [ $fd -lt $1 ]
+ do
+ # this next line requires bash2 to work for fd>9 (damn)
+ eval "exec $fd<&-"
+ fd=$(($fd+1))
+ done
+ ${debug-false} && fstat -p$$
+}
+
+# prevent fd leaks
+# do this before forking child to avoid race conditions
+${dontclose-false} || close-fds 200
+
+#
+# Start child process to send stats
+#
+
+(
+ # Give parent a moment to go away
+ sleep 1
+
+ echo Exec: $stats -d $logs -g UT $verbose
+ echo Start: `date`
+ $stats -d $logs -g UT $verbose
+ ret=$?
+ echo End: `date`
+
+ case "$ret" in
+ $ngNoLog) echo "No logfiles found" ;;
+ $ngBusy) echo "ngWorldStats already running" ;;
+ $ngOK) echo "ngWorldStats successful" ;;
+ $ngSyntax) echo "Server Syntax error" ;;
+ *) echo "ngWorldStats returned $ret" ;;
+ esac
+
+ # remove transcript log if run was OK
+ if ${dologfile-false}
+ then
+ case "$ret" in
+ $ngOK) ${dontcleanlogs-false} || rm -f $logf;;
+ esac
+ fi
+
+ if ${doresults-false}
+ then
+ echo $ret > $results
+ else
+ rm -f $results 2> /dev/null
+ fi
+
+ Unlock $lockf
+) &
+
+#
+# Meanwhile, in the parent process, return result to UT
+#
+
+# close the log file - the child will complete the output
+exec >&- 2>&-
+
+if ${doresults-false} && [ -r $results ]
+then
+ lastresults=`cat $results`
+fi
+
+exit ${lastresults:-$ngOK}
--- NetGamesUSA.com/ngWorldStats/bin/ngWorldStats.exe Thu Oct 18 18:27:19 2001
+++ NetGamesUSA.com/ngWorldStats/bin/ngWorldStats.exe Thu Oct 18 18:27:19 2001
@@ -1,56 +1,277 @@
-#!/bin/sh
+#!/bin/bash
+defopts=-vrl
+
+# bgWorldStats 1.2
+# Copyright (c) 2001 "Ouch@Schooner.COM"
+#
+# Permission to use, copy, modify, and distribute this software and its
+# documentation for any purpose and without fee is hereby granted,
+# provided that the above copyright notice appear in all copies.
+# The author makes no representations about the suitability of this
+# software for any purpose. It is provided "as is" without express
+# or implied warranty.
#
-# ngWorldStats.exe for linux
-# version 0.3.0, 2000-10-06
-# abfackeln@abfackeln.com
+# http://illuminati.guildhappy.com/
#
-########################################
+# This will run the NG stats sender in the background, immediately returning
+# to UT.
#
-# this file contains no user servicable parts.
+# Installation:
+# Put this script (chmod a+x) into same directory as the existing
+# ngWorldStats binary:
+# <UT>/NetGamesUSA.com/ngWorldStats/bin
+# and call it "bgWorldStats"
#
-########################################
+# Change your server's .ini to include this:
#
-# the objective of this script was to execute the
-# ngWorldStats batcher without hanging up the
-# UT server by waiting for the NetGames server
-# to respond (which sometimes may take a long
-# time and force the UT server to switch ports
-# which is obviously a bad thing)
+# WorldBatcherURL=../NetGamesUSA.com/ngWorldStats/bin/bgWorldStats
+# WorldBatcherParams=-vrl
#
-# a new objective is to support the home
-# directory option introduced in loki games
-# linux UT 4.25 server by searching for all
-# possible locations for the ngWorldStats
-# binary executable file
+# Or, edit the "defopts" line above to include "-vrl"
#
-# comments and suggestions to enhance this
-# script should be sent to abfackeln at
-# abfackeln@abfackeln.com
+# NOTICE:
+# This script has only been tested on FreeBSD with the Linux UT server
+# started with the "-nohomedir" option.
+#
+
+#
+# Inspired by ngWorldStats.sh, originally by <abfackeln@abfackeln.com>
+# and modified (v1.2) by Leif Sawyer <leif@gci.net>
+#
+
#
-# as always, any comments are welcome and
-# i hope that this script alleviates all of
-# the ngWorldStats problems from the past
+# Change history:
+# 2001-02-26 1.2 released
+# close leaked fd's (bash required), add -C and -R options
+# 2001-01-30 1.1 released
+# first public release
+
+Usage() {
+ echo 2>&1 Usage: `basename $0` '[-vlrDCR] [-s statsbin] [-d logsdir] [-g gametype]
+ Options:
+ -v verbose ngWorldStats output
+ -r carry forward last result code
+ -l direct output to transcript log (only saved if an error occurs)
+
+ Debugging (only when needed):
+ -D enable debugging output
+ -C do not close leaked file descriptors
+ -R do not remove transcript logs of successful runs
+
+ Change defaults:
+ -d logs directory path (if relative, must be from <UT>/System)
+ ["'$logs'"]
+ -g game type ["UT"]
+ -s ngWorldStats program to use ["ngWorldStats"] - not a pathname'
+ exit 2
+}
+
+# UT runs us as "../NetGamesUSA.com/ngWorldStats/bin/bgWorldStats"
+# UT execs us from the "<UT>/System" directory.
+
+bin=`dirname $0`
+logs=$bin/../logs
+
+args=`getopt DCRrvls:g:d: $defopts $*`
+if [ $? != 0 ]; then Usage; fi
+oargs=$@
+set -- $args
+for i
+do
+ case "$1" in
+ -D) debug=true ;;
+ -v) verbose=-v ;;
+ -r) doresults=true ;;
+ -l) dologfile=true ;;
+ -R) dontcleanlogs=true ;;
+ -C) dontclose=false ;;
+ -s) statsbin=$2; shift ;;
+ -d) logs=$2; shift ;;
+ -g) game=$2; shift ;;
+ --) shift; break ;;
+ esac
+ shift
+done
+case $# in
+0) ;;
+*) Usage;;
+esac
+
#
-# -abf.
+# Main variables
#
-########################################
-WorldBatcherURL="../NetGamesUSA.com/ngWorldStats/bin/ngWorldStats"
+# ngWorldStats executable
+stats=$bin/${statsbin:-ngWorldStats}
-if [ ! -x "$WorldBatcherURL" ]; then
- INSTALL_DIR=`cat $HOME/.loki/ut/install_dir`
- HomeBatcherURL="$INSTALL_DIR/NetGamesUSA.com/ngWorldStats/bin/ngWorldStats"
+# game
+game=${game:-UT}
- if [ ! -x "$HomeBatcherURL" ]; then
- echo "ngWS ERROR: can not exec: $WorldBatcherURL or $HomeBatcherURL"
- exit 0
+# Saved ngWorldStats result from last run
+results=$logs/.lastresult
+
+# When we were run
+now=`date +%y%m%d-%H%M`
+
+if ${dologfile-false}
+then
+ # redirect stdout to log file of our run
+ logf=$logs/.ngrun.$now
+ exec > $logf 2>&1
+fi
+
+if ${debug-false}
+then
+ echo Started at: `date`
+ echo Called as: $0 $oargs
+ echo In Directory: `pwd`
+ echo ""
+fi
+
+# ngWorldStats exit codes
+ngE=1 # error (?)
+ngNoLog=83 # no logfiles
+ngBusy=97 # ngWorldStats already running
+ngOK=99 # ngWorldStats successful
+ngSyntax=139 # Server Syntax error
+
+#
+# Ugly shell locking code
+#
+# All systems don't have shlock(1) (from INN) or lockfile (1) (from procmail);
+# FreeBSD has lockf(1) but it doesn't have the semantics I want here.
+# So this makes up for it in a "somewhat" portable, safe fashion, under
+# the assumption that there isn't going to be significant contention.
+#
+# Lock using file path $1, returning an error message
+# (with original lock time and pid) if it fails.
+#
+# return 0 for OK; 1 for locked; 2 for lock file error
+# Error message to stdout
+#
+Lock() {
+ l=$1
+ if ! mkdir $l >/dev/null 2>&1
+ then
+ if [ ! -d $l ]
+ then
+ echo "ERROR: $l not a lock dir"
+ return 2
+ elif [ ! -r $l/lock ]
+ then
+ # race?
+ echo "ERROR: $l does not have a lock info file"
+ return 2
+ fi
+ read p d < $l/lock
+ # still around?
+ if ps p$p >/dev/null 2>&1
+ then
+ echo ALREADY LOCKED: by pid $p at $d
+ return 1
fi
+ # must be stale, override it
+ echo 1>&2 "STALE LOCK REMOVED: by pid $p at $d"
+ fi
+ echo $$ `date` > $l/lock
+ return 0
+}
+Unlock() {
+ l=$1
+ rm -rf $l
+}
+
+#
+# Start the real work here
+#
+
+#
+# Take a lock, or return busy to the UT server
+#
- WorldBatcherURL="$HomeBatcherURL"
+lockf=$logs/.ngWS.lock
+ret=`Lock $lockf`
+if [ $? != 0 ]
+then
+ echo Lock error: $ret
+ exit $ngBusy
fi
-$WorldBatcherURL $* &
-echo "ngWS OK: $WorldBatcherURL $* &"
+#
+# UT leaks file descriptors (including the listening socket) to us.
+# If we don't close these, the next server instance won't be able to bind to
+# the ServerQueryPort (i.e., 7778) and will pick the next available one
+# (i.e., 7783), even if we are in the background
+#
+# How many leaked? Lots. On my server it was fds 3..70, with 8 of those
+# sockets. I don't know if it varies, so I just close "a whole bunch".
+#
+close-fds() {
+ ${debug-false} && fstat -p$$
+ fd=3
+ while [ $fd -lt $1 ]
+ do
+ # this next line requires bash2 to work for fd>9 (damn)
+ eval "exec $fd<&-"
+ fd=$(($fd+1))
+ done
+ ${debug-false} && fstat -p$$
+}
+
+# prevent fd leaks
+# do this before forking child to avoid race conditions
+${dontclose-false} || close-fds 200
+
+#
+# Start child process to send stats
+#
-exit 99
+(
+ # Give parent a moment to go away
+ sleep 1
+
+ echo Exec: $stats -d $logs -g UT $verbose
+ echo Start: `date`
+ $stats -d $logs -g UT $verbose
+ ret=$?
+ echo End: `date`
+
+ case "$ret" in
+ $ngNoLog) echo "No logfiles found" ;;
+ $ngBusy) echo "ngWorldStats already running" ;;
+ $ngOK) echo "ngWorldStats successful" ;;
+ $ngSyntax) echo "Server Syntax error" ;;
+ *) echo "ngWorldStats returned $ret" ;;
+ esac
+
+ # remove transcript log if run was OK
+ if ${dologfile-false}
+ then
+ case "$ret" in
+ $ngOK) ${dontcleanlogs-false} || rm -f $logf;;
+ esac
+ fi
+
+ if ${doresults-false}
+ then
+ echo $ret > $results
+ else
+ rm -f $results 2> /dev/null
+ fi
+
+ Unlock $lockf
+) &
+
+#
+# Meanwhile, in the parent process, return result to UT
+#
+
+# close the log file - the child will complete the output
+exec >&- 2>&-
+
+if ${doresults-false} && [ -r $results ]
+then
+ lastresults=`cat $results`
+fi
+exit ${lastresults:-$ngOK}
|