-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
1973 lines (1697 loc) · 49.1 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
Copyright (c) 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 <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.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 "util.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;
#ifdef SERVER_MAIN
static int checkpoint_finished = 0; /* 1 = failure, 2 = success */
#endif /* SERVER_MAIN */
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: #%d now listening on %s\n", l->oid, l->name);
return 1;
} else {
errlog("LISTEN: Can't start #%d listening on %s!\n", l->oid, l->name);
return 0;
}
}
static void
free_slistener(slistener * l)
{
network_close_listener(l->nlistener);
oklog("UNLISTEN: #%d 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();
}
void panicf(const char *fmt, ...) {
char buf[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
panic(buf);
}
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;
}
#ifdef SERVER_MAIN
static void
panic_signal(int sig)
{
char message[100];
sprintf(message, "Caught signal %d", sig);
panic(message);
}
static void
shutdown_signal(int sig)
{
unused(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.
*/
while (waitpid(-1, (void *) &status, WNOHANG) > 0);
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)
{
unused(id);
unused(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);
}
#endif /* SERVER_MAIN */
static const char *
object_name(Objid oid)
{
static Stream *s = 0;
if (!s)
s = new_stream(30);
if (valid(oid))
stream_printf(s, "%s (#%d)", db_object_name(oid), oid);
else
stream_printf(s, "#%d", 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);
}
#ifdef SERVER_MAIN
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: #%d 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: #%d 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);
}
#endif /* SERVER_MAIN */
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;
#ifdef SERVER_MAIN
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];
}
#endif /* SERVER_MAIN */
static int
server_set_connection_option(shandle * h, const char *option, Var value)
{
if (!mystrcasecmp(option, "binary")) {
h->binary = is_true(value);
network_set_connection_binary(h->nhandle, h->binary);
return 1;
}
return 0;
}
static int
server_connection_option(shandle * h, const char *option, Var * value)
{
if (!mystrcasecmp(option, "binary")) {
value->type = TYPE_INT;
value->v.num = h->binary;
return 1;
}
return 0;
}
static Var
server_connection_options(shandle * h, Var list)
{
Var pair;
pair = new_list(2);
pair.v.list[1].type = TYPE_STR;
pair.v.list[1].v.str = str_dup("binary");
pair.v.list[2].type = TYPE_INT;
pair.v.list[2].v.num = h->binary;
return listappend(list, pair);
}
static void
emergency_notify(Objid player, const char *line)
{
printf("#%d <- %s\n", player, line);
}
#ifdef SERVER_MAIN
static char *
read_stdin_line()
{
static Stream *s = 0;
char *line, buffer[1000];
int buflen;
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 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 #%d 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 #%d.\n",
first_valid);
}
wizard = first_valid;
db_set_object_flag(wizard, FLAG_WIZARD);
printf("** No wizards in database; wizzed #%d.\n", wizard);
}
printf("** Now running emergency commands as #%d ...\n", wizard);
}
printf("\nMOO (#%d)%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("** %d 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("** %d 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) {
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, "#%d", &wizard) == 1) {
printf("** Switching to wizard #%d...\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);
}
}
free_stream(s);
in_emergency_mode = 0;
oklog("EMERGENCY_MODE: Leaving mode; %s continue...\n",
start_ok ? "will" : "won't");
return start_ok;
}
#endif /* SERVER_MAIN */
/*
* 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 = outbound ? SYSTEM_OBJECT : l->oid;
h->tasks = new_task_queue(h->player, h->listener);
h->disconnect_me = 0;
h->outbound = outbound;
h->binary = 0;
h->print_messages = (!outbound && l->print_messages);
if (!outbound)
new_input_task(h->tasks, "");
oklog("%s: #%d 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));