[go: up one dir, main page]

Menu

[897c37]: / runtime / backend.c  Maximize  Restore  History

Download this file

269 lines (231 with data), 6.6 kB

  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
/*
** Code originally derived from Lpmud 1.3 (Lars Penj|)
**
** Significantly modified: Geoff Wong and Mike McGaughey.
*/
#include <sys/types.h>
#ifdef __WIN32__
#include <signal.h>
#else
#include <sys/wait.h>
#include <sys/signal.h>
#endif
#ifdef FCNTL
#include <fcntl.h>
#endif
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "security.h"
#include "proto.h"
#include "apply.h"
#include "comm.h"
#include "consts.h"
#include "fsecure.h"
#include "lpc.h"
/*
* The 'current_time' is updated whenever update_current_time is called.
*/
int current_time;
int current_time_us; /* microseconds */
void update_current_time()
{
struct timeval t;
gettimeofday(&t, (struct timezone *)0);
current_time = t.tv_sec;
current_time_us = t.tv_usec;
}
extern Obj *command_giver;
extern int num_player, num_made;
extern char * mud_lib;
void load_first_objects();
extern void call_out(),
unroot(),
prepare_ipc(),
open_error_log(),
ed_cmd (char *),
shutdowngame();
extern int get_message (char *, int),
player_parser (char *),
child_termination();
extern int game_is_being_shut_down;
int time_to_call_heart_beat = 0;
int comm_time_to_call_heart_beat = 0; /* this is set by
* interrupt, */
/*
* There are global variables that must be zeroed before any execution. In
* case of errors, there will not be a longjmp(), and the variables will have to
* be cleared explicitely. They are normally maintained by the code that use
* them.
*/
void
clear_state()
{
Sset_current(NULL, "clear state");
command_giver = NULL;
}
void
logon(Obj *ob)
{
/*
* Set current object to ob, so that the static function "logon" can be
* called.
*/
/* NOTE: this uses call_out, which currently sets command_giver to
the logon object - beware of subtle interactions if you are using
switch_interactive, etc, in the LPC.
*/
apply_clean(C("logon"), ob, 0);
}
#if 0
/*
* Take a player command and parse it. The command can also come from a NPC.
* Beware that 'str' can be modified and extended !
*/
int remote_command = 0; /* see if parse is being called
* through command(). */
int
parse_command(char *str, Obj *ob, int from_command)
{
Obj *save = command_giver;
int res;
if (from_command) remote_command = 1;
command_giver = ob;
res = player_parser(str);
command_giver = save;
if (from_command) remote_command = 0;
return res;
}
#endif
/*
* This is the backend. We will stay here for ever (almost).
* NB: profiling version. Every 5 minutes, dumps updated profile.
*/
void
backend()
{
char buff[MAX_TEXT_IN];
(void) printf("Setting up ipc.\n");
/* open sockets for connection from the outside world */
prepare_ipc();
open_error_log();
#ifndef __WIN32__
/* chroot() if we can */
if (geteuid() == 0) chroot(mud_lib);
/* go into secure mode - should we do this before loading objects? */
unroot();
#endif
(void) printf("Loading core objects.\n");
load_first_objects();
#ifndef __WIN32__
(void) signal(SIGHUP, startshutdowngame);
(void) signal(SIGCHLD, child_termination);
(void) signal(SIGPIPE, SIG_IGN);
#endif
/* We come here after errors, and have to clear some global variables. */
clear_state();
/* This is the backend which drives the entire process */
while (1)
{
if (game_is_being_shut_down)
{
shutdowngame();
break;
}
command_giver = 0;
if (get_message(buff, sizeof buff))
{
update_load_av();
/*
* Now we have a string from the player. This string can go to
* one of several places. If it is prepended with a '!', then it
* is an escape from the 'ed' editor, so we send it as a command
* to the parser. If any object function is waiting for an input
* string, then send it there. Otherwise, send the string to the
* parser. The player_parser() will find that current_object is
* 0, and then set current_object to point to the object that
* defines the command. This will enable such functions to be
* static.
*/
if (set_timer(MAX_LOOP_TIME))
{
apply_clean(C("catch_input"), command_giver, make_string(buff));
#if 0
gameval = apply_single(C("catch_input"), command_giver,make_string(bbuff));
// ? wipe(1);
#endif
}
reset_timer();
}
command_giver = 0;
//printf("\nBefore call_out() rCT=%d\n", (rCT - (int) control)/4);
call_out();
//printf("After call_out() rCT=%d\n", (rCT - (int) control)/4);
flush_players();
//clear_stack();
}
// get here on shutdown
}
/*
* There is a file with a list of objects to be initialized at start up.
* Also loads secure/master.
*/
Obj * Master = 0;
Obj * Emulate = 0;
void
load_first_objects()
{
Obj *o;
/* clear_stack(); */
#ifdef EMULATE_EFUN
if ((Emulate = load_object(C(EMULATE_EFUN))))
{
/* hash emulate functions! (fast lookup) */
add_functions(Emulate->code, Emulate->code, 0);
}
else
{
printf("Terminating: Failed to load %s\n", EMULATE_EFUN);
exit(1);
}
#endif
if (!(o = load_object(C(OBJ_MASTER))))
{
printf("Terminating: Unable to load %s.\n", OBJ_MASTER);
exit(1);
}
Master = o;
setuid_me((Obj *)0, o, GOD*2, 0);
}
static double load_av = 0.0;
void update_load_av()
{
extern double consts[5];
extern int current_time;
static int last_time;
int n;
double c;
static int acc = 0;
acc++;
if (current_time == last_time) return;
n = current_time - last_time;
if (n < sizeof consts / sizeof consts[0]) c = consts[n];
else c = 0; /*exp(-n / 900.0); */
load_av = c * load_av + acc * (1 - c) / n;
last_time = current_time;
acc = 0;
}
float LPC_load_av()
{
return (float)load_av;
}
#if 0
Val * query_load_av()
{
char buff[100];
sprintf(buff, "%.2f cmds/s, %.2f comp lines/s", load_av, LPC_compile_av());
return make_string(buff);
}
#endif