summaryrefslogtreecommitdiffstats
path: root/server.c
blob: ff699ba0830efbb024bd404f6a394d9f71a133a2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
/******************************************************************************
  Copyright (c) 1992, 1995, 1996 Xerox Corporation.  All rights reserved.
  Portions of this code were written by Stephen White, aka ghond.
  Use and copying of this software and preparation of derivative works based
  upon this software are permitted.  Any distribution of this software or
  derivative works must comply with all applicable United States export
  control laws.  This software is made available AS IS, and Xerox Corporation
  makes no warranty about the software, its performance or its conformity to
  any specification.  Any person obtaining a copy of this software is requested
  to send their name and post office or electronic mail address to:
    Pavel Curtis
    Xerox PARC
    3333 Coyote Hill Rd.
    Palo Alto, CA 94304
    Pavel@Xerox.Com
 *****************************************************************************/

#include "my-types.h"		/* must be first on some systems */
#include "my-signal.h"
#include "my-stdarg.h"
#include "my-stdio.h"
#include "my-stdlib.h"
#include "my-string.h"
#include "my-unistd.h"
#include "my-wait.h"

#include "config.h"
#include "db.h"
#include "db_io.h"
#include "disassemble.h"
#include "execute.h"
#include "functions.h"
#include "list.h"
#include "log.h"
#include "network.h"
#include "options.h"
#include "parser.h"
#include "random.h"
#include "server.h"
#include "storage.h"
#include "streams.h"
#include "structures.h"
#include "tasks.h"
#include "timers.h"
#include "unparse.h"
#include "utils.h"
#include "version.h"

#include "execute.h"

static pid_t parent_pid;
int in_child = 0;

static const char *shutdown_message = 0;	/* shut down if non-zero */
static int in_emergency_mode = 0;
static Var checkpointed_connections;

typedef enum {
    CHKPT_OFF, CHKPT_TIMER, CHKPT_SIGNAL, CHKPT_FUNC
} Checkpoint_Reason;
static Checkpoint_Reason checkpoint_requested = CHKPT_OFF;

static int checkpoint_finished = 0;	/* 1 = failure, 2 = success */

typedef struct shandle {
    struct shandle *next, **prev;
    network_handle nhandle;
    time_t connection_time;
    time_t last_activity_time;
    Objid player;
    Objid listener;
    task_queue tasks;
    int disconnect_me;
    int outbound, binary;
    int print_messages;
} shandle;

static shandle *all_shandles = 0;

typedef struct slistener {
    struct slistener *next, **prev;
    network_listener nlistener;
    Objid oid;			/* listen(OID, DESC, PRINT_MESSAGES) */
    Var desc;
    int print_messages;
    const char *name;
} slistener;

static slistener *all_slisteners = 0;

server_listener null_server_listener = {0};

static void
free_shandle(shandle * h)
{
    *(h->prev) = h->next;
    if (h->next)
	h->next->prev = h->prev;

    free_task_queue(h->tasks);

    myfree(h, M_NETWORK);
}

static slistener *
new_slistener(Objid oid, Var desc, int print_messages, enum error *ee)
{
    slistener *l = mymalloc(sizeof(slistener), M_NETWORK);
    server_listener sl;
    enum error e;
    const char *name;

    sl.ptr = l;
    e = network_make_listener(sl, desc, &(l->nlistener), &(l->desc), &name);

    if (ee)
	*ee = e;

    if (e != E_NONE) {
	myfree(l, M_NETWORK);
	return 0;
    }
    l->oid = oid;
    l->print_messages = print_messages;
    l->name = str_dup(name);

    l->next = all_slisteners;
    l->prev = &all_slisteners;
    if (all_slisteners)
	all_slisteners->prev = &(l->next);
    all_slisteners = l;

    return l;
}

static int
start_listener(slistener * l)
{
    if (network_listen(l->nlistener)) {
	oklog("LISTEN: #%"PRIdN" now listening on %s\n", l->oid, l->name);
	return 1;
    } else {
	errlog("LISTEN: Can't start #%"PRIdN" listening on %s!\n", l->oid, l->name);
	return 0;
    }
}

static void
free_slistener(slistener * l)
{
    network_close_listener(l->nlistener);
    oklog("UNLISTEN: #%"PRIdN" no longer listening on %s\n", l->oid, l->name);

    *(l->prev) = l->next;
    if (l->next)
	l->next->prev = l->prev;

    free_var(l->desc);
    free_str(l->name);

    myfree(l, M_NETWORK);
}

static void
send_shutdown_message(const char *msg)
{
    shandle *h;
    Stream *s = new_stream(100);
    char *message;

    stream_printf(s, "*** Shutting down: %s ***", msg);
    message = stream_contents(s);
    for (h = all_shandles; h; h = h->next)
	network_send_line(h->nhandle, message, 1);
    free_stream(s);
}

static void
abort_server(void)
{
    signal(SIGINT, SIG_DFL);
    signal(SIGTERM, SIG_DFL);
    signal(SIGFPE, SIG_DFL);
    signal(SIGHUP, SIG_DFL);
    signal(SIGILL, SIG_DFL);
    signal(SIGQUIT, SIG_DFL);
    signal(SIGSEGV, SIG_DFL);
#ifdef SIGBUS
    signal(SIGBUS, SIG_DFL);
#endif
    signal(SIGUSR1, SIG_DFL);
    signal(SIGUSR2, SIG_DFL);
    signal(SIGCHLD, SIG_DFL);

    abort();
}

void
panic(const char *message)
{
    static int in_panic = 0;

    errlog("PANIC%s: %s\n", in_child ? " (in child)" : "", message);
    if (in_panic) {
	errlog("RECURSIVE PANIC: aborting\n");
	abort_server();
    }
    in_panic = 1;

    log_command_history();

    if (in_child) {		/* We're a forked checkpointer */
	errlog("Child shutting down parent via USR1 signal\n");
	kill(parent_pid, SIGUSR1);
	_exit(1);
    }
    print_error_backtrace("server panic", output_to_log);
    send_shutdown_message("server panic");
    network_shutdown();
    db_flush(FLUSH_PANIC);

    abort_server();
}

enum Fork_Result
fork_server(const char *subtask_name)
{
    pid_t pid;
    Stream *s = new_stream(100);

    stream_printf(s, "Forking %s", subtask_name);
    pid = fork();
    if (pid < 0) {
	log_perror(stream_contents(s));
	free_stream(s);
	return FORK_ERROR;
    }
    free_stream(s);
    if (pid == 0) {
	in_child = 1;
	return FORK_CHILD;
    } else
	return FORK_PARENT;
}

static void
panic_signal(int sig)
{
    char message[100];

    sprintf(message, "Caught signal %d", sig);
    panic(message);
}

static void
shutdown_signal(int sig)
{
    shutdown_message = "shutdown signal received";
}

static void
checkpoint_signal(int sig)
{
    checkpoint_requested = CHKPT_SIGNAL;

    signal(sig, checkpoint_signal);
}

static void
call_checkpoint_notifier(int successful)
{
    Var args;

    args = new_list(1);
    args.v.list[1].type = TYPE_INT;
    args.v.list[1].v.num = successful;
    run_server_task(-1, SYSTEM_OBJECT, "checkpoint_finished", args, "", 0);
}

static void
child_completed_signal(int sig)
{
    int status;

    /* (Void *) casts to avoid warnings on systems that mis-declare the
     * argument type.
     */
#if HAVE_WAITPID
    while (waitpid(-1, (void *) &status, WNOHANG) > 0);
#else
#if HAVE_WAIT3
    while (wait3((void *) &status, WNOHANG, 0) >= 0);
#else
#if HAVE_WAIT2
    while (wait2((void *) &status, WNOHANG) >= 0);
#else
    wait((void *) &status);
#endif
#endif
#endif

    signal(sig, child_completed_signal);

    checkpoint_finished = (status == 0) + 1;	/* 1 = failure, 2 = success */
}

static void
setup_signals(void)
{
    signal(SIGFPE, SIG_IGN);
    if (signal(SIGHUP, panic_signal) == SIG_IGN)
	signal(SIGHUP, SIG_IGN);
    signal(SIGILL, panic_signal);
    signal(SIGQUIT, panic_signal);
    signal(SIGSEGV, panic_signal);
#ifdef SIGBUS
    signal(SIGBUS, panic_signal);
#endif

    signal(SIGINT, shutdown_signal);
    signal(SIGTERM, shutdown_signal);
    signal(SIGUSR1, shutdown_signal);	/* remote shutdown signal */
    signal(SIGUSR2, checkpoint_signal);		/* remote checkpoint signal */

    signal(SIGCHLD, child_completed_signal);
}

static void
checkpoint_timer(Timer_ID id, Timer_Data data)
{
    checkpoint_requested = CHKPT_TIMER;
}

static void
set_checkpoint_timer(int first_time)
{
    Var v;
    int interval, now = time(0);
    static Timer_ID last_checkpoint_timer;

    v = get_system_property("dump_interval");
    if (v.type != TYPE_INT || v.v.num < 60 || now + v.v.num < now) {
	free_var(v);
	interval = 3600;	/* Once per hour */
    } else
	interval = v.v.num;

    if (!first_time)
	cancel_timer(last_checkpoint_timer);
    last_checkpoint_timer = set_timer(interval, checkpoint_timer, 0);
}

static const char *
object_name(Objid oid)
{
    static Stream *s = 0;

    if (!s)
	s = new_stream(30);

    if (valid(oid))
	stream_printf(s, "%s (#%"PRIdN")", db_object_name(oid), oid);
    else
	stream_printf(s, "#%"PRIdN"", oid);

    return reset_stream(s);
}

static void
call_notifier(Objid player, Objid handler, const char *verb_name)
{
    Var args;

    args = new_list(1);
    args.v.list[1].type = TYPE_OBJ;
    args.v.list[1].v.obj = player;
    run_server_task(player, handler, verb_name, args, "", 0);
}

int
get_server_option(Objid oid, const char *name, Var * r)
{
    if (((valid(oid) &&
	  db_find_property(oid, "server_options", r).ptr)
	 || (valid(SYSTEM_OBJECT) &&
	     db_find_property(SYSTEM_OBJECT, "server_options", r).ptr))
	&& r->type == TYPE_OBJ
	&& valid(r->v.obj)
	&& db_find_property(r->v.obj, name, r).ptr)
	return 1;

    return 0;
}

static void
send_message(Objid listener, network_handle nh, const char *msg_name,...)
{
    va_list args;
    Var msg;
    const char *line;

    va_start(args, msg_name);
    if (get_server_option(listener, msg_name, &msg)) {
	if (msg.type == TYPE_STR)
	    network_send_line(nh, msg.v.str, 1);
	else if (msg.type == TYPE_LIST) {
	    int i;

	    for (i = 1; i <= msg.v.list[0].v.num; i++)
		if (msg.v.list[i].type == TYPE_STR)
		    network_send_line(nh, msg.v.list[i].v.str, 1);
	}
    } else			/* Use default message */
	while ((line = va_arg(args, const char *)) != 0)
	    network_send_line(nh, line, 1);

    va_end(args);
}

static void
main_loop(void)
{
    int i;

    /* First, notify DB of disconnections for all checkpointed connections */
    for (i = 1; i <= checkpointed_connections.v.list[0].v.num; i++) {
	Var v;

	v = checkpointed_connections.v.list[i];
	call_notifier(v.v.list[1].v.obj, v.v.list[2].v.obj,
		      "user_disconnected");
    }
    free_var(checkpointed_connections);

    /* Second, run #0:server_started() */
    run_server_task(-1, SYSTEM_OBJECT, "server_started", new_list(0), "", 0);
    set_checkpoint_timer(1);

    /* Now, we enter the main server loop */
    while (shutdown_message == 0) {
	/* Check how long we have until the next task will be ready to run.
	 * We only care about three cases (== 0, == 1, and > 1), so we can
	 * map a `never' result from the task subsystem into 2.
	 */
	int task_seconds = next_task_start();
	int seconds_left = task_seconds < 0 ? 2 : task_seconds;
	shandle *h, *nexth;

	if (checkpoint_requested != CHKPT_OFF) {
	    if (checkpoint_requested == CHKPT_SIGNAL)
		oklog("CHECKPOINTING due to remote request signal.\n");
	    checkpoint_requested = CHKPT_OFF;
	    run_server_task(-1, SYSTEM_OBJECT, "checkpoint_started",
			    new_list(0), "", 0);
	    network_process_io(0);
#ifdef UNFORKED_CHECKPOINTS
	    call_checkpoint_notifier(db_flush(FLUSH_ALL_NOW));
#else
	    if (!db_flush(FLUSH_ALL_NOW))
		call_checkpoint_notifier(0);
#endif
	    set_checkpoint_timer(0);
	}
#ifndef UNFORKED_CHECKPOINTS
	if (checkpoint_finished) {
	    call_checkpoint_notifier(checkpoint_finished - 1);
	    checkpoint_finished = 0;
	}
#endif

	if (!network_process_io(seconds_left ? 1 : 0) && seconds_left > 1)
	    db_flush(FLUSH_ONE_SECOND);
	else
	    db_flush(FLUSH_IF_FULL);

	run_ready_tasks();

	{			/* Get rid of old un-logged-in or useless connections */
	    int now = time(0);

	    for (h = all_shandles; h; h = nexth) {
		Var v;

		nexth = h->next;

		if (!h->outbound && h->connection_time == 0
		    && (get_server_option(h->listener, "connect_timeout", &v)
			? (v.type == TYPE_INT && v.v.num > 0
			   && now - h->last_activity_time > v.v.num)
			: (now - h->last_activity_time
			   > DEFAULT_CONNECT_TIMEOUT))) {
		    call_notifier(h->player, h->listener, "user_disconnected");
		    oklog("TIMEOUT: #%"PRIdN" on %s\n",
			  h->player,
			  network_connection_name(h->nhandle));
		    if (h->print_messages)
			send_message(h->listener, h->nhandle, "timeout_msg",
				     "*** Timed-out waiting for login. ***",
				     0);
		    network_close(h->nhandle);
		    free_shandle(h);
		} else if (h->connection_time != 0 && !valid(h->player)) {
		    oklog("RECYCLED: #%"PRIdN" on %s\n",
			  h->player,
			  network_connection_name(h->nhandle));
		    if (h->print_messages)
			send_message(h->listener, h->nhandle,
				     "recycle_msg", "*** Recycled ***", 0);
		    network_close(h->nhandle);
		    free_shandle(h);
		} else if (h->disconnect_me) {
		    call_notifier(h->player, h->listener,
				  "user_disconnected");
		    oklog("DISCONNECTED: %s on %s\n",
			  object_name(h->player),
			  network_connection_name(h->nhandle));
		    if (h->print_messages)
			send_message(h->listener, h->nhandle, "boot_msg",
				     "*** Disconnected ***", 0);
		    network_close(h->nhandle);
		    free_shandle(h);
		}
	    }
	}
    }

    oklog("SHUTDOWN: %s\n", shutdown_message);
    send_shutdown_message(shutdown_message);
}

static shandle *
find_shandle(Objid player)
{
    shandle *h;

    for (h = all_shandles; h; h = h->next)
	if (h->player == player)
	    return h;

    return 0;
}

static char *cmdline_buffer;
static int cmdline_buflen;

static void
init_cmdline(int argc, char *argv[])
{
    char *p;
    int i;

    for (p = argv[0], i = 1;;) {
	if (*p++ == '\0' && (i >= argc || p != argv[i++]))
	    break;
    }

    cmdline_buffer = argv[0];
    cmdline_buflen = p - argv[0];
}

#define SERVER_CO_TABLE(DEFINE, H, VALUE, _)				\
    DEFINE(binary, _, TYPE_INT, num,					\
	   H->binary,							\
	   {								\
	       H->binary = is_true(VALUE);				\
	       network_set_connection_binary(H->nhandle, H->binary);	\
	   })								\

static int
server_set_connection_option(shandle * h, const char *option, Var value)
{
    CONNECTION_OPTION_SET(SERVER_CO_TABLE, h, option, value);
}

static int
server_connection_option(shandle * h, const char *option, Var * value)
{
    CONNECTION_OPTION_GET(SERVER_CO_TABLE, h, option, value);
}

static Var
server_connection_options(shandle * h, Var list)
{
    CONNECTION_OPTION_LIST(SERVER_CO_TABLE, h, list);
}

#undef SERVER_CO_TABLE

static char *
read_stdin_line()
{
    static Stream *s = 0;
    char *line, buffer[1000];
    int buflen;

    fflush(stdout);
    if (!s)
	s = new_stream(100);

    do {			/* Read even a very long line of input */
	fgets(buffer, sizeof(buffer), stdin);
	buflen = strlen(buffer);
	if (buflen == 0)
	    return 0;
	if (buffer[buflen - 1] == '\n') {
	    buffer[buflen - 1] = '\0';
	    buflen--;
	}
	stream_add_string(s, buffer);
    } while (buflen == sizeof(buffer) - 1);
    line = reset_stream(s);
    while (*line == ' ')
	line++;

    return line;
}

static void
emergency_notify(Objid player, const char *line)
{
    printf("#%"PRIdN" <- %s\n", player, line);
}

static int
emergency_mode()
{
    char *line;
    Var words;
    int nargs;
    const char *command;
    Stream *s = new_stream(100);
    Objid wizard = -1;
    int debug = 1;
    int start_ok = -1;

    oklog("EMERGENCY_MODE: Entering mode...\n");
    in_emergency_mode = 1;

    printf("\nLambdaMOO Emergency Holographic Wizard Mode\n");
    printf("-------------------------------------------\n");
    printf("\"Please state the nature of the wizardly emergency...\"\n");
    printf("(Type `help' for assistance.)\n\n");

    while (start_ok < 0) {
	/* Find/create a wizard to run commands as... */
	if (!is_wizard(wizard)) {
	    Objid first_valid = -1;

	    if (wizard >= 0)
		printf("** Object #%"PRIdN" is not a wizard...\n", wizard);

	    for (wizard = 0; wizard <= db_last_used_objid(); wizard++)
		if (is_wizard(wizard))
		    break;
		else if (valid(wizard) && first_valid < 0)
		    first_valid = wizard;

	    if (!is_wizard(wizard)) {
		if (first_valid < 0) {
		    first_valid = db_create_object();
		    printf("** No objects in database; created #%"PRIdN".\n",
			   first_valid);
		}
		wizard = first_valid;
		db_set_object_flag(wizard, FLAG_WIZARD);
		printf("** No wizards in database; wizzed #%"PRIdN".\n", wizard);
	    }
	    printf("** Now running emergency commands as #%"PRIdN" ...\n", wizard);
	}
	printf("\nMOO (#%"PRIdN")%s: ", wizard, debug ? "" : "[!d]");
	line = read_stdin_line();

	if (!line)
	    start_ok = 0;	/* treat EOF as "quit" */
	else if (*line == ';') {	/* eval command */
	    Var code, errors;
	    Program *program;
	    Var str;

	    str.type = TYPE_STR;
	    code = new_list(0);

	    if (*++line == ';')
		line++;
	    else {
		str.v.str = str_dup("return");
		code = listappend(code, str);
	    }

	    while (*line == ' ')
		line++;

	    if (*line == '\0') {	/* long form */
		printf("Type one or more lines of code, ending with `.' ");
		printf("alone on a line.\n");
		for (;;) {
		    line = read_stdin_line();
		    if (!strcmp(line, "."))
			break;
		    else {
			str.v.str = str_dup(line);
			code = listappend(code, str);
		    }
		}
	    } else {
		str.v.str = str_dup(line);
		code = listappend(code, str);
	    }
	    str.v.str = str_dup(";");
	    code = listappend(code, str);

	    program = parse_list_as_program(code, &errors);
	    free_var(code);
	    if (program) {
		Var result;

		switch (run_server_program_task(NOTHING, "emergency_mode",
						new_list(0), NOTHING,
						"emergency_mode", program,
						wizard, debug, wizard, "",
						&result)) {
		case OUTCOME_DONE:
		    printf("=> %s\n", value_to_literal(result));
		    free_var(result);
		    break;
		case OUTCOME_ABORTED:
		    printf("=> *Aborted*\n");
		    break;
		case OUTCOME_BLOCKED:
		    printf("=> *Suspended*\n");
		    break;
		}
		free_program(program);
	    } else {
		int i;

		printf("** %"PRIdN" errors during parsing:\n",
		       errors.v.list[0].v.num);
		for (i = 1; i <= errors.v.list[0].v.num; i++)
		    printf("  %s\n", errors.v.list[i].v.str);
	    }
	    free_var(errors);
	} else {
	    words = parse_into_wordlist(line);
	    nargs = words.v.list[0].v.num - 1;
	    if (nargs < 0)
		continue;
	    command = words.v.list[1].v.str;

	    if ((!mystrcasecmp(command, "program")
		 || !mystrcasecmp(command, ".program"))
		&& nargs == 1) {
		const char *verbref = words.v.list[2].v.str;
		db_verb_handle h;
		const char *message, *vname;

		h = find_verb_for_programming(wizard, verbref,
					      &message, &vname);
		printf("%s\n", message);
		if (h.ptr) {
		    Var code, str, errors;
		    char *line;
		    Program *program;

		    code = new_list(0);
		    str.type = TYPE_STR;

		    while (strcmp(line = read_stdin_line(), ".")) {
			str.v.str = str_dup(line);
			code = listappend(code, str);
		    }

		    program = parse_list_as_program(code, &errors);
		    if (program) {
			db_set_verb_program(h, program);
			printf("Verb programmed.\n");
		    } else {
			int i;

			printf("** %"PRIdN" errors during parsing:\n",
			       errors.v.list[0].v.num);
			for (i = 1; i <= errors.v.list[0].v.num; i++)
			    printf("  %s\n", errors.v.list[i].v.str);
			printf("Verb not programmed.\n");
		    }

		    free_var(code);
		    free_var(errors);
		}
	    } else if (!mystrcasecmp(command, "list") && nargs == 1) {
		const char *verbref = words.v.list[2].v.str;
		db_verb_handle h;
		const char *message, *vname;

		h = find_verb_for_programming(wizard, verbref,
					      &message, &vname);
		if (h.ptr)
		    unparse_to_file(stdout, db_verb_program(h), 0, 1,
				    MAIN_VECTOR);
		else
		    printf("%s\n", message);
	    } else if (!mystrcasecmp(command, "disassemble") && nargs == 1) {
		const char *verbref = words.v.list[2].v.str;
		db_verb_handle h;
		const char *message, *vname;

		h = find_verb_for_programming(wizard, verbref,
					      &message, &vname);
		if (h.ptr)
		    disassemble_to_file(stdout, db_verb_program(h));
		else
		    printf("%s\n", message);
	    } else if (!mystrcasecmp(command, "abort") && nargs == 0) {
	        printf("Bye.  (%s)\n\n", "NOT saving database");
		exit(1);
	    } else if (!mystrcasecmp(command, "quit") && nargs == 0) {
		start_ok = 0;
	    } else if (!mystrcasecmp(command, "continue") && nargs == 0) {
		start_ok = 1;
	    } else if (!mystrcasecmp(command, "debug") && nargs == 0) {
		debug = !debug;
	    } else if (!mystrcasecmp(command, "wizard") && nargs == 1
		       && sscanf(words.v.list[2].v.str, "#%"PRIdN"", &wizard) == 1) {
		printf("** Switching to wizard #%"PRIdN"...\n", wizard);
	    } else {
		if (mystrcasecmp(command, "help")
		    && mystrcasecmp(command, "?"))
		    printf("** Unknown or malformed command.\n");

		printf(";EXPR                 "
		       "Evaluate MOO expression, print result.\n");
		printf(";;CODE	              "
		       "Execute whole MOO verb, print result.\n");
		printf("    (For above, omitting EXPR or CODE lets you "
		       "enter several lines\n");
		printf("     of input at once; type a period alone on a "
		       "line to finish.)\n");
		printf("program OBJ:VERB      "
		       "Set the MOO code of an existing verb.\n");
		printf("list OBJ:VERB         "
		       "List the MOO code of an existing verb.\n");
		printf("disassemble OBJ:VERB  "
		       "List the internal form of an existing verb.\n");
		printf("debug                 "
		       "Toggle evaluation with(out) `d' bit.\n");
		printf("wizard #XX            "
		       "Execute future commands as wizard #XX.\n");
		printf("continue              "
		       "End emergency mode, continue start-up.\n");
		printf("quit                  "
		       "Exit server normally, saving database.\n");
		printf("abort                 "
		       "Exit server *without* saving database.\n");
		printf("help, ?               "
		       "Print this text.\n\n");

		printf("NOTE: *NO* forked or suspended tasks will run "
		       "until you exit this mode.\n\n");
		printf("\"Please remember to turn me off when you go...\"\n");
	    }

	    free_var(words);
	}
    }

    printf("Bye.  (%s)\n\n", start_ok ? "continuing" : "saving database");
#if NETWORK_PROTOCOL != NP_SINGLE
    fclose(stdout);
#endif

    free_stream(s);
    in_emergency_mode = 0;
    oklog("EMERGENCY_MODE: Leaving mode; %s continue...\n",
	  start_ok ? "will" : "won't");
    return start_ok;
}


/*
 * Exported interface
 */

void
set_server_cmdline(const char *line)
{
    /* This technique works for all UNIX systems I've seen on which this is
     * possible to do at all, and it's safe on all systems.  The only systems
     * I know of where this doesn't work run System V Release 4, on which the
     * kernel keeps its own copy of the original cmdline for printing by the
     * `ps' command; on these systems, it would appear that there does not
     * exist a way to get around this at all.  Thus, this works everywhere I
     * know of where it's possible to do the job at all...
     */
    char *p = cmdline_buffer, *e = p + cmdline_buflen - 1;

    while (*line && p < e)
	*p++ = *line++;
    while (p < e)
	*p++ = ' ';		/* Pad with blanks, not nulls; on SunOS and
				 * maybe other systems, nulls would confuse
				 * `ps'.  (*sigh*)
				 */
    *e = '\0';
}

int
server_flag_option(const char *name)
{
    Var v;

    if (get_server_option(SYSTEM_OBJECT, name, &v))
	return is_true(v);
    else
	return 0;
}

int
server_int_option(const char *name, int defallt)
{
    Var v;

    if (get_server_option(SYSTEM_OBJECT, name, &v))
	return (v.type == TYPE_INT ? v.v.num : defallt);
    else
	return defallt;
}

const char *
server_string_option(const char *name, const char *defallt)
{
    Var v;

    if (get_server_option(SYSTEM_OBJECT, name, &v))
	return (v.type == TYPE_STR ? v.v.str : 0);
    else
	return defallt;
}

static Objid next_unconnected_player = NOTHING - 1;

server_handle
server_new_connection(server_listener sl, network_handle nh, int outbound)
{
    slistener *l = sl.ptr;
    shandle *h = (shandle *) mymalloc(sizeof(shandle), M_NETWORK);
    server_handle result;

    h->next = all_shandles;
    h->prev = &all_shandles;
    if (all_shandles)
	all_shandles->prev = &(h->next);
    all_shandles = h;

    h->nhandle = nh;
    h->connection_time = 0;
    h->last_activity_time = time(0);
    h->player = next_unconnected_player--;
    h->listener = l ? l->oid : SYSTEM_OBJECT;
    h->tasks = new_task_queue(h->player, h->listener);
    h->disconnect_me = 0;
    h->outbound = outbound;
    h->binary = 0;
    h->print_messages = l ? l->print_messages : !outbound;

    if (l || !outbound) {
	new_input_task(h->tasks, "", 0);
	/*
	 * Suspend input at the network level until the above input task
	 * is processed.  At the point when it is dequeued, tasks.c will
	 * notice that the queued input size is below the low water mark
	 * and resume input.
	 */
	task_suspend_input(h->tasks);
    }

    oklog("%s: #%"PRIdN" on %s\n",
	  outbound ? "CONNECT" : "ACCEPT",
	  h->player, network_connection_name(nh));

    result.ptr = h;
    return result;
}

void
server_refuse_connection(server_listener sl, network_handle nh)
{
    slistener *l = sl.ptr;

    if (l->print_messages)
	send_message(l->oid, nh, "server_full_msg",
		     "*** Sorry, but the server cannot accept any more"
		     " connections right now.",
		     "*** Please try again later.",
		     0);
    errlog("SERVER FULL: refusing connection on %s from %s\n",
	   l->name, network_connection_name(nh));
}

void
server_receive_line(server_handle sh, const char *line)
{
    shandle *h = (shandle *) sh.ptr;

    h->last_activity_time = time(0);
    new_input_task(h->tasks, line, h->binary);
}

void
server_close(server_handle sh)
{
    shandle *h = (shandle *) sh.ptr;

    oklog("CLIENT DISCONNECTED: %s on %s\n",
	  object_name(h->player),
	  network_connection_name(h->nhandle));
    h->disconnect_me = 1;
    call_notifier(h->player, h->listener, "user_client_disconnected");
    free_shandle(h);
}

void
server_suspend_input(Objid connection)
{
    shandle *h = find_shandle(connection);

    network_suspend_input(h->nhandle);
}

void
server_resume_input(Objid connection)
{
    shandle *h = find_shandle(connection);

    network_resume_input(h->nhandle);
}

void
player_connected(Objid old_id, Objid new_id, int is_newly_created)
{
    shandle *existing_h = find_shandle(new_id);
    shandle *new_h = find_shandle(old_id);

    if (!new_h)
	panic("Non-existent shandle connected");

    new_h->player = new_id;
    new_h->connection_time = time(0);

    if (existing_h) {
	/* we now have two shandles with the same player value while
	 * find_shandle assumes there can only be one.  This needs to
	 * be remedied before any call_notifier() call; luckily, the
	 * latter only needs listener value.
	 */
	Objid existing_listener = existing_h->listener;
	/* network_connection_name is allowed to reuse the same string
	 * storage, so we have to copy one of them.
	 */
	char *name1 = str_dup(network_connection_name(existing_h->nhandle));

	oklog("REDIRECTED: %s, was %s, now %s\n",
	      object_name(new_id),
	      name1,
	      network_connection_name(new_h->nhandle));
	free_str(name1);
	if (existing_h->print_messages)
	    send_message(existing_listener, existing_h->nhandle,
			 "redirect_from_msg",
			 "*** Redirecting connection to new port ***", 0);
	if (new_h->print_messages)
	    send_message(new_h->listener, new_h->nhandle, "redirect_to_msg",
			 "*** Redirecting old connection to this port ***", 0);
	network_close(existing_h->nhandle);
	free_shandle(existing_h);
	if (existing_listener == new_h->listener)
	    call_notifier(new_id, new_h->listener, "user_reconnected");
	else {
	    new_h->disconnect_me = 1;
	    call_notifier(new_id, existing_listener,
			  "user_client_disconnected");
	    new_h->disconnect_me = 0;
	    call_notifier(new_id, new_h->listener, "user_connected");
	}
    } else {
	oklog("%s: %s on %s\n",
	      is_newly_created ? "CREATED" : "CONNECTED",
	      object_name(new_h->player),
	      network_connection_name(new_h->nhandle));
	if (new_h->print_messages) {
	    if (is_newly_created)
		send_message(new_h->listener, new_h->nhandle, "create_msg",
			     "*** Created ***", 0);
	    else
		send_message(new_h->listener, new_h->nhandle, "connect_msg",
			     "*** Connected ***", 0);
	}
	call_notifier(new_id, new_h->listener,
		      is_newly_created ? "user_created" : "user_connected");
    }
}

void
notify(Objid player, const char *message)
{
    shandle *h = find_shandle(player);

    if (h && !h->disconnect_me)
	network_send_line(h->nhandle, message, 1);
    else if (in_emergency_mode)
	emergency_notify(player, message);
}

void
boot_player(Objid player)
{
    shandle *h = find_shandle(player);

    if (h)
	h->disconnect_me = 1;
}

void
write_active_connections(void)
{
    int count = 0;
    shandle *h;

    for (h = all_shandles; h; h = h->next)
	count++;

    dbio_printf("%d active connections with listeners\n", count);

    for (h = all_shandles; h; h = h->next)
	dbio_printf("%"PRIdN" %"PRIdN"\n", h->player, h->listener);
}

int
read_active_connections(void)
{
    int count, i, have_listeners = 0;
    char c;

    i = dbio_scanf("%d active connections%c", &count, &c);
    if (i == EOF) {		/* older database format */
	checkpointed_connections = new_list(0);
	return 1;
    } else if (i != 2) {
	errlog("READ_ACTIVE_CONNECTIONS: Bad active connections count.\n");
	return 0;
    } else if (c == ' ') {
	if (strcmp(dbio_read_string(), "with listeners") != 0) {
	    errlog("READ_ACTIVE_CONNECTIONS: Bad listeners tag.\n");
	    return 0;
	} else
	    have_listeners = 1;
    } else if (c != '\n') {
	errlog("READ_ACTIVE_CONNECTIONS: Bad EOL.\n");
	return 0;
    }
    checkpointed_connections = new_list(count);
    for (i = 1; i <= count; i++) {
	Objid who, listener;
	Var v;

	if (have_listeners) {
	    if (dbio_scanf("%"PRIdN" %"PRIdN"\n", &who, &listener) != 2) {
		errlog("READ_ACTIVE_CONNECTIONS: Bad conn/listener pair.\n");
		return 0;
	    }
	} else {
	    who = dbio_read_num();
	    listener = SYSTEM_OBJECT;
	}
	checkpointed_connections.v.list[i] = v = new_list(2);
	v.v.list[1].type = v.v.list[2].type = TYPE_OBJ;
	v.v.list[1].v.obj = who;
	v.v.list[2].v.obj = listener;
    }

    return 1;
}

int
main(int argc, char **argv)
{
    char *this_program = str_dup(argv[0]);
    const char *log_file = 0;
    int emergency = 0;
    Var desc;
    slistener *l;

    init_cmdline(argc, argv);

    argc--;
    argv++;
    while (argc > 0 && argv[0][0] == '-') {
	/* Deal with any command-line options */
	switch (argv[0][1]) {
	case 'e':		/* Emergency wizard mode */
	    emergency = 1;
	    break;
	case 'l':		/* Specified log file */
	    if (argc > 1) {
		log_file = argv[1];
		argc--;
		argv++;
	    } else
		argc = 0;	/* Provoke usage message below */
	    break;
	default:
	    argc = 0;		/* Provoke usage message below */
	}
	argc--;
	argv++;
    }

    if (log_file) {
	FILE *f = fopen(log_file, "a");

	if (f)
	    set_log_file(f);
	else {
	    perror("Error opening specified log file");
	    exit(1);
	}
    } else
	set_log_file(stderr);

    if (!db_initialize(&argc, &argv)
	|| !network_initialize(argc, argv, &desc)) {
	fprintf(stderr, "Usage: %s [-e] [-l log-file] %s %s\n",
		this_program, db_usage_string(), network_usage_string());
	exit(1);
    }
#if NETWORK_PROTOCOL != NP_SINGLE
    if (!emergency)
	fclose(stdout);
#endif
    if (log_file)
	fclose(stderr);

    oklog("STARTING: Version %s of the LambdaMOO server\n", server_version);
    oklog("          (Using %s protocol)\n", network_protocol_name());
    oklog("          (Task timeouts measured in %s seconds.)\n",
	  virtual_timer_available()? "server CPU" : "wall-clock");

    register_bi_functions();

    l = new_slistener(SYSTEM_OBJECT, desc, 1, 0);
    if (!l) {
	errlog("Can't create initial connection point!\n");
	exit(1);
    }
    free_var(desc);

    if (!db_load())
	exit(1);

    load_server_options();

    SRANDOM(time(0));

    parent_pid = getpid();
    setup_signals();
    reset_command_history();

    if (!emergency || emergency_mode()) {
	if (!start_listener(l))
	    exit(1);

	main_loop();
	network_shutdown();
    }
    db_shutdown();
    free_str(this_program);

    return 0;
}


/**** built in functions ****/

static package
bf_server_version(Var arglist, Byte next, void *vdata, Objid progr)
{
    Var r;
    r.type = TYPE_STR;
    r.v.str = str_dup(server_version);
    free_var(arglist);
    return make_var_pack(r);
}

static package
bf_renumber(Var arglist, Byte next, void *vdata, Objid progr)
{
    Var r;
    Objid o = arglist.v.list[1].v.obj;
    free_var(arglist);

    if (!valid(o))
	return make_error_pack(E_INVARG);
    else if (!is_wizard(progr))
	return make_error_pack(E_PERM);

    r.type = TYPE_OBJ;
    r.v.obj = db_renumber_object(o);
    return make_var_pack(r);
}

static package
bf_reset_max_object(Var arglist, Byte next, void *vdata, Objid progr)
{
    free_var(arglist);

    if (!is_wizard(progr))
	return make_error_pack(E_PERM);

    db_reset_last_used_objid();
    return no_var_pack();
}

static package
bf_memory_usage(Var arglist, Byte next, void *vdata, Objid progr)
{
    Var r;
    r = memory_usage();
    free_var(arglist);
    return make_var_pack(r);
}

static package
bf_shutdown(Var arglist, Byte next, void *vdata, Objid progr)
{
    /*
     * The stream 's' and its contents will leak, but we're shutting down,
     * so it doesn't really matter.
     */

    Stream *s;
    int nargs = arglist.v.list[0].v.num;
    const char *msg = (nargs >= 1 ? arglist.v.list[1].v.str : 0);

    if (!is_wizard(progr)) {
	free_var(arglist);
	return make_error_pack(E_PERM);
    }
    s = new_stream(100);
    stream_printf(s, "shutdown() called by %s", object_name(progr));
    if (msg)
	stream_printf(s, ": %s", msg);
    shutdown_message = stream_contents(s);

    free_var(arglist);
    return no_var_pack();
}

static package
bf_dump_database(Var arglist, Byte next, void *vdata, Objid progr)
{
    free_var(arglist);
    if (!is_wizard(progr))
	return make_error_pack(E_PERM);

    checkpoint_requested = CHKPT_FUNC;
    return no_var_pack();
}

static package
bf_db_disk_size(Var arglist, Byte next, void *vdata, Objid progr)
{
    Var v;

    free_var(arglist);
    v.type = TYPE_INT;
    if ((v.v.num = db_disk_size()) < 0)
	return make_raise_pack(E_QUOTA, "No database file(s) available", zero);
    else
	return make_var_pack(v);
}

#ifdef OUTBOUND_NETWORK
static slistener *
find_slistener_by_oid(Objid obj)
{
    slistener *l;

    for (l = all_slisteners; l; l = l->next)
	if (l->oid == obj)
	    return l;

    return 0;
}
#endif /* OUTBOUND_NETWORK */

static package
bf_open_network_connection(Var arglist, Byte next, void *vdata, Objid progr)
{
#ifdef OUTBOUND_NETWORK

    Var r;
    enum error e;
    server_listener sl;
    slistener l;

    if (!is_wizard(progr)) {
        free_var(arglist);
        return make_error_pack(E_PERM);
    }

    if (arglist.v.list[0].v.num == 3) {
	Objid oid;

	if (arglist.v.list[3].type != TYPE_OBJ) {
	    return make_error_pack(E_TYPE);
	}
	oid = arglist.v.list[3].v.obj;
	arglist = listdelete(arglist, 3);

	sl.ptr = find_slistener_by_oid(oid);
	if (!sl.ptr) {
	    /* Create a temporary */
	    l.print_messages = 0;
	    l.name = "open_network_connection";
	    l.desc = zero;
	    l.oid = oid;
	    sl.ptr = &l;
	}
    } else {
	sl.ptr = NULL;
    }

    e = network_open_connection(arglist, sl);
    free_var(arglist);
    if (e == E_NONE) {
	/* The connection was successfully opened, implying that
	 * server_new_connection was called, implying and a new negative
	 * player number was allocated for the connection.  Thus, the old
	 * value of next_unconnected_player is the number of our connection.
	 */
	r.type = TYPE_OBJ;
	r.v.obj = next_unconnected_player + 1;
    } else {
	r.type = TYPE_ERR;
	r.v.err = e;
    }
    if (r.type == TYPE_ERR)
	return make_error_pack(r.v.err);
    else
	return make_var_pack(r);

#else				/* !OUTBOUND_NETWORK */

    /* This function is disabled in this server. */

    free_var(arglist);
    return make_error_pack(E_PERM);

#endif
}

static package
bf_connected_players(Var arglist, Byte next, void *vdata, Objid progr)
{
    shandle *h;
    int nargs = arglist.v.list[0].v.num;
    int show_all = (nargs >= 1 && is_true(arglist.v.list[1]));
    int count = 0;
    Var result;

    free_var(arglist);
    for (h = all_shandles; h; h = h->next)
	if ((show_all || h->connection_time != 0) && !h->disconnect_me)
	    count++;

    result = new_list(count);
    count = 0;

    for (h = all_shandles; h; h = h->next) {
	if ((show_all || h->connection_time != 0) && !h->disconnect_me) {
	    count++;
	    result.v.list[count].type = TYPE_OBJ;
	    result.v.list[count].v.obj = h->player;
	}
    }

    return make_var_pack(result);
}

static package
bf_connected_seconds(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (player) */
    Var r;
    shandle *h = find_shandle(arglist.v.list[1].v.obj);

    r.type = TYPE_INT;
    if (h && h->connection_time != 0 && !h->disconnect_me)
	r.v.num = time(0) - h->connection_time;
    else
	r.v.num = -1;
    free_var(arglist);
    if (r.v.num < 0)
	return make_error_pack(E_INVARG);
    else
	return make_var_pack(r);
}

static package
bf_idle_seconds(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (player) */
    Var r;
    shandle *h = find_shandle(arglist.v.list[1].v.obj);

    r.type = TYPE_INT;
    if (h && !h->disconnect_me)
	r.v.num = time(0) - h->last_activity_time;
    else
	r.v.num = -1;
    free_var(arglist);
    if (r.v.num < 0)
	return make_error_pack(E_INVARG);
    else
	return make_var_pack(r);
}

static package
bf_connection_name(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (player) */
    Objid who = arglist.v.list[1].v.obj;
    shandle *h = find_shandle(who);
    const char *conn_name;
    Var r;

    if (h && !h->disconnect_me)
	conn_name = network_connection_name(h->nhandle);
    else
	conn_name = 0;

    free_var(arglist);
    if (!is_wizard(progr) && progr != who)
	return make_error_pack(E_PERM);
    else if (!conn_name)
	return make_error_pack(E_INVARG);
    else {
	r.type = TYPE_STR;
	r.v.str = str_dup(conn_name);
	return make_var_pack(r);
    }
}

static package
bf_notify(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (player, string [, no_flush]) */
    Objid conn = arglist.v.list[1].v.obj;
    const char *line = arglist.v.list[2].v.str;
    int no_flush = (arglist.v.list[0].v.num > 2
		    ? is_true(arglist.v.list[3])
		    : 0);
    shandle *h = find_shandle(conn);
    Var r;

    if (!is_wizard(progr) && progr != conn) {
	free_var(arglist);
	return make_error_pack(E_PERM);
    }
    r.type = TYPE_INT;
    if (h && !h->disconnect_me) {
	if (h->binary) {
	    int length;

	    line = binary_to_raw_bytes(line, &length);
	    if (!line) {
		free_var(arglist);
		return make_error_pack(E_INVARG);
	    }
	    r.v.num = network_send_bytes(h->nhandle, line, length, !no_flush);
	} else
	    r.v.num = network_send_line(h->nhandle, line, !no_flush);
    } else {
	if (in_emergency_mode)
	    emergency_notify(conn, line);
	r.v.num = 1;
    }
    free_var(arglist);
    return make_var_pack(r);
}

static package
bf_boot_player(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (object) */
    Objid oid = arglist.v.list[1].v.obj;

    free_var(arglist);

    if (oid != progr && !is_wizard(progr))
	return make_error_pack(E_PERM);

    boot_player(oid);
    return no_var_pack();
}

static package
bf_set_connection_option(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (conn, option, value) */
    Objid oid = arglist.v.list[1].v.obj;
    const char *option = arglist.v.list[2].v.str;
    Var value = arglist.v.list[3];
    shandle *h = find_shandle(oid);
    enum error e = E_NONE;

    if (oid != progr && !is_wizard(progr))
	e = E_PERM;
    else if (!h || h->disconnect_me
	     || (!server_set_connection_option(h, option, value)
		 && !tasks_set_connection_option(h->tasks, option, value)
		 && !network_set_connection_option(h->nhandle, option, value)))
	e = E_INVARG;

    free_var(arglist);
    if (e == E_NONE)
	return no_var_pack();
    else
	return make_error_pack(e);
}

static package
bf_connection_options(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (conn [, opt-name]) */
    Objid oid = arglist.v.list[1].v.obj;
    int nargs = arglist.v.list[0].v.num;
    const char *oname = (nargs >= 2 ? arglist.v.list[2].v.str : 0);
    shandle *h = find_shandle(oid);
    Var ans;

    if (!h || h->disconnect_me) {
	free_var(arglist);
	return make_error_pack(E_INVARG);
    } else if (oid != progr && !is_wizard(progr)) {
	free_var(arglist);
	return make_error_pack(E_PERM);
    }
    if (oname) {
	if (!server_connection_option(h, oname, &ans)
	    && !tasks_connection_option(h->tasks, oname, &ans)
	    && !network_connection_option(h->nhandle, oname, &ans)) {
	    free_var(arglist);
	    return make_error_pack(E_INVARG);
	}
    } else {
	ans = new_list(0);
	ans = server_connection_options(h, ans);
	ans = tasks_connection_options(h->tasks, ans);
	ans = network_connection_options(h->nhandle, ans);
    }

    free_var(arglist);
    return make_var_pack(ans);
}

static slistener *
find_slistener(Var desc)
{
    slistener *l;

    for (l = all_slisteners; l; l = l->next)
	if (equality(desc, l->desc, 0))
	    return l;

    return 0;
}

static package
bf_listen(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (oid, desc) */
    Objid oid = arglist.v.list[1].v.obj;
    Var desc = arglist.v.list[2];
    int nargs = arglist.v.list[0].v.num;
    int print_messages = nargs >= 3 && is_true(arglist.v.list[3]);
    enum error e;
    slistener *l = 0;

    if (!is_wizard(progr))
	e = E_PERM;
    else if (!valid(oid) || find_slistener(desc))
	e = E_INVARG;
    else if (!(l = new_slistener(oid, desc, print_messages, &e)));	/* Do nothing; e is already set */
    else if (!start_listener(l))
	e = E_QUOTA;

    free_var(arglist);
    if (e == E_NONE)
	return make_var_pack(var_ref(l->desc));
    else
	return make_error_pack(e);
}

static package
bf_unlisten(Var arglist, Byte next, void *vdata, Objid progr)
{				/* (desc) */
    Var desc = arglist.v.list[1];
    enum error e = E_NONE;
    slistener *l = 0;

    if (!is_wizard(progr))
	e = E_PERM;
    else if (!(l = find_slistener(desc)))
	e = E_INVARG;

    free_var(arglist);
    if (e == E_NONE) {
	free_slistener(l);
	return no_var_pack();
    } else
	return make_error_pack(e);
}

static package
bf_listeners(Var arglist, Byte next, void *vdata, Objid progr)
{				/* () */
    int i, count = 0;
    Var list, entry;
    slistener *l;

    free_var(arglist);
    for (l = all_slisteners; l; l = l->next)
	count++;
    list = new_list(count);
    for (i = 1, l = all_slisteners; l; i++, l = l->next) {
	list.v.list[i] = entry = new_list(3);
	entry.v.list[1].type = TYPE_OBJ;
	entry.v.list[1].v.obj = l->oid;
	entry.v.list[2] = var_ref(l->desc);
	entry.v.list[3].type = TYPE_INT;
	entry.v.list[3].v.num = l->print_messages;
    }

    return make_var_pack(list);
}

static package
bf_buffered_output_length(Var arglist, Byte next, void *vdata, Objid progr)
{				/* ([connection]) */
    int nargs = arglist.v.list[0].v.num;
    Objid conn = nargs >= 1 ? arglist.v.list[1].v.obj : 0;
    Var r;

    free_var(arglist);
    r.type = TYPE_INT;
    if (nargs == 0)
	r.v.num = MAX_QUEUED_OUTPUT;
    else {
	shandle *h = find_shandle(conn);

	if (!h)
	    return make_error_pack(E_INVARG);
	else if (progr != conn && !is_wizard(progr))
	    return make_error_pack(E_PERM);

	r.v.num = network_buffered_output_length(h->nhandle);
    }

    return make_var_pack(r);
}

void
register_server(void)
{
    register_function("server_version", 0, 0, bf_server_version);
    register_function("renumber", 1, 1, bf_renumber, TYPE_OBJ);
    register_function("reset_max_object", 0, 0, bf_reset_max_object);
    register_function("memory_usage", 0, 0, bf_memory_usage);
    register_function("shutdown", 0, 1, bf_shutdown, TYPE_STR);
    register_function("dump_database", 0, 0, bf_dump_database);
    register_function("db_disk_size", 0, 0, bf_db_disk_size);
    register_function("open_network_connection", 0, -1,
		      bf_open_network_connection);
    register_function("connected_players", 0, 1, bf_connected_players,
		      TYPE_ANY);
    register_function("connected_seconds", 1, 1, bf_connected_seconds,
		      TYPE_OBJ);
    register_function("idle_seconds", 1, 1, bf_idle_seconds, TYPE_OBJ);
    register_function("connection_name", 1, 1, bf_connection_name, TYPE_OBJ);
    register_function("notify", 2, 3, bf_notify, TYPE_OBJ, TYPE_STR, TYPE_ANY);
    register_function("boot_player", 1, 1, bf_boot_player, TYPE_OBJ);
    register_function("set_connection_option", 3, 3, bf_set_connection_option,
		      TYPE_OBJ, TYPE_STR, TYPE_ANY);
    register_function("connection_option", 2, 2, bf_connection_options,
		      TYPE_OBJ, TYPE_STR);
    register_function("connection_options", 1, 1, bf_connection_options,
		      TYPE_OBJ);
    register_function("listen", 2, 3, bf_listen, TYPE_OBJ, TYPE_ANY, TYPE_ANY);
    register_function("unlisten", 1, 1, bf_unlisten, TYPE_ANY);
    register_function("listeners", 0, 0, bf_listeners);
    register_function("buffered_output_length", 0, 1,
		      bf_buffered_output_length, TYPE_OBJ);
}

char rcsid_server[] = "$Id$";

/* 
 * $Log$
 * Revision 1.12  2007/06/02 21:34:36  wrog
 * fix player_connect() so that the user_client_disconnected hook
 * sees a disconnected player, same as with server_close()
 *
 * Revision 1.11  2007/05/29 12:21:47  wrog
 * fixes server panic (or lost messages) caused by attempting to write to freed network handle during #0:user_reconnected; removes the one case where server and network handles were not being freed together
 *
 * Revision 1.10  2006/11/21 18:42:37  pschwan
 * b=1500775
 * fixes two use-after-free bugs that could lead very rarely to
 * calling the wrong functions during player connection
 *
 * Revision 1.9  2005/09/29 18:46:18  bjj
 * Add third argument to open_network_connection() that associates a specific listener object with the new connection.  This simplifies a lot of outbound connection management.
 *
 * Revision 1.8  2004/05/25 07:28:55  wrog
 * indentation fixes
 *
 * Revision 1.7  2004/05/22 01:25:44  wrog
 * merging in WROGUE changes (W_SRCIP, W_STARTUP, W_OOB)
 *
 * Revision 1.5.10.4  2004/05/21 23:02:56  wrog
 * NP_SINGLE needs to have stdout stay open
 *
 * Revision 1.5.10.3  2004/05/20 19:57:11  wrog
 * fixed flushing issues w.r.t. emergency mode;
 * close stdout and stderr when we are not using them
 *
 * Revision 1.6  2003/06/12 18:16:56  bjj
 * Suspend input on connection until :do_login_command() can run.
 *
 * Revision 1.5.10.2  2003/06/11 10:40:16  wrog
 * added binary argument to new_input_task()
 *
 * Revision 1.5.10.1  2003/06/07 12:59:04  wrog
 * introduced connection_option macros
 *
 * Revision 1.5  1998/12/29 06:56:32  nop
 * Fixed leak in onc().
 *
 * Revision 1.4  1998/12/14 13:18:57  nop
 * Merge UNSAFE_OPTS (ref fixups); fix Log tag placement to fit CVS whims
 *
 * Revision 1.3  1997/03/03 05:03:52  nop
 * steak2: move protectedness into builtin struct, load_server_options()
 * now required for $server_options updates.
 *
 * Revision 1.2  1997/03/03 04:19:24  nop
 * GNU Indent normalization
 *
 * Revision 1.1.1.1  1997/03/03 03:45:01  nop
 * LambdaMOO 1.8.0p5
 *
 * Revision 2.11  1996/05/12  21:26:10  pavel
 * Slightly cleaned up some code in bf_shutdown().  Release 1.8.0p5.
 *
 * Revision 2.10  1996/04/08  01:01:19  pavel
 * Made get_server_option() public.  Added `-l LOGFILE' option.  Slightly
 * reordered the start-up sequence to more frequently avoid writing into the
 * log when the server is about to abort anyway.  Release 1.8.0p3.
 *
 * Revision 2.9  1996/03/19  07:10:10  pavel
 * Changed connect timeout handling to use an in-DB server option.  Fixed
 * task_id() in emergency-mode evals.  Moved built-in function registration to
 * happen after the start-up messages.  Release 1.8.0p2.
 *
 * Revision 2.8  1996/03/10  01:10:50  pavel
 * Added `connection_option()' built-in function.  Made argument to
 * shutdown() optional.  Release 1.8.0.
 *
 * Revision 2.7  1996/02/11  00:49:03  pavel
 * Made connection messages come from properties on LISTENER.server_options,
 * if defined.  Enhanced the robustness of the system-option-fetching code in
 * the face of invalid objects.  Release 1.8.0beta2.
 *
 * Revision 2.6  1996/02/08  06:53:21  pavel
 * Added `list' and `disassemble' commands to emergency wizard mode.  Now
 * ignore SIGFPE.  Added support for tasks-module connection options.  Added
 * connection_options() function.  Renamed err/logf() to errlog/oklog() and
 * TYPE_NUM to TYPE_INT.  Updated copyright notice for 1996.
 * Release 1.8.0beta1.
 *
 * Revision 2.5  1996/01/16  07:25:25  pavel
 * Fixed uninitialized-variable bug in `program' in emergency wizard mode.
 * Release 1.8.0alpha6.
 *
 * Revision 2.4  1996/01/11  07:36:41  pavel
 * Added CHKPT_FUNC reason, missing forever.  Added support for binary I/O.
 * Added calls to $user_disconnected() on timeout and for booted un-logged-in
 * connections.  Added buffered_output_length() built-in.
 * Release 1.8.0alpha5.
 *
 * Revision 2.3  1995/12/31  00:06:00  pavel
 * Added support for the new multiple-listening-points interface, including
 * listen(), unlisten(), and listeners().  Allowed DB-configurable messages to
 * be given as lists of strings.  Suppressed DB-configurable messages to
 * outbound connections.  Moved server-full handling to here from net_multi.c,
 * also making the message DB-configurable.    Release 1.8.0alpha4.
 *
 * Revision 2.2  1995/12/28  00:33:08  pavel
 * Added db_disk_size() built-in.  Release 1.8.0alpha3.
 *
 * Revision 2.1  1995/12/11  07:41:12  pavel
 * Fixed checkpoint timing to test for overflow when adding $dump_interval to
 * the current time.  Added an optional argument to `connected_players()' to
 * include even un-logged-in or outbound connections.
 *
 * Release 1.8.0alpha2.
 *
 * Revision 2.0  1995/11/30  04:31:19  pavel
 * New baseline version, corresponding to release 1.8.0alpha1.
 *
 * Revision 1.19  1992/10/23  23:03:47  pavel
 * Added copyright notice.
 *
 * Revision 1.18  1992/10/23  22:22:24  pavel
 * Change to #include "my-wait.h" instead of <sys/wait.h>.
 *
 * Revision 1.17  1992/10/21  03:02:35  pavel
 * Converted to use new automatic configuration system.
 *
 * Revision 1.16  1992/10/17  20:52:15  pavel
 * Global rename of strdup->str_dup, strref->str_ref, vardup->var_dup, and
 * varref->var_ref.
 *
 * Revision 1.15  1992/10/02  00:01:28  pavel
 * Fixed a bug in the main loop whereby external shutdown requests (e.g., the
 * interrupt signal) might not be noticed until some user I/O or a task
 * execution occurred.
 *
 * Revision 1.14  1992/09/26  02:26:20  pavel
 * Added support for printing the network protocol name on server start-up.
 *
 * Revision 1.13  1992/09/15  15:53:00  pjames
 * Renamed FLUSH choices.
 *
 * Revision 1.12  1992/09/14  17:46:16  pjames
 * Moved db_modification code to db modules.
 * Added set up for disk based data base updating.
 *
 * Revision 1.11  1992/09/08  22:08:56  pjames
 * Added all code from bf_server.c.
 *
 * Revision 1.10  1992/09/03  23:52:33  pavel
 * Added support for multiple complete network implementations.
 *
 * Revision 1.9  1992/08/28  16:01:04  pjames
 * Changed myfree(*, M_STRING) to free_str(*).
 *
 * Revision 1.8  1992/08/26  18:30:45  pavel
 * Dump interval and retry interval are now local integers.
 * Changed logic in regards to using POSIX.
 *
 * Revision 1.7  1992/08/13  21:28:11  pjames
 * Now only calls one procedure to register built in functions.
 *
 * Revision 1.6  1992/08/12  01:50:42  pjames
 * run_ready_tasks() was moved to before logoff actions, so that logoffs
 * would happen faster.
 *
 * Revision 1.5  1992/08/10  16:55:31  pjames
 * Updated #includes.  Added register_bf_log().
 *
 * Revision 1.4  1992/07/30  23:22:26  pavel
 * Added a remote checkpoint-request signal (SIGUSR2), analogous to the existing
 * shutdown-requestion signal (SIGUSR1).
 *
 * Revision 1.3  1992/07/30  00:40:59  pavel
 * Add support for compiling on RISC/os 4.52 and NonStop-UX A22.
 *
 * Revision 1.2  1992/07/21  00:06:27  pavel
 * Added rcsid_<filename-root> declaration to hold the RCS ident. string.
 *
 * Revision 1.1  1992/07/20  23:23:12  pavel
 * Initial RCS-controlled version.
 */