]> git.pond.sub.org Git - empserver/blob - src/server/main.c
Declare all configuration variables in optlist.h. Include that
[empserver] / src / server / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  main.c: Thread and signal initialization for Empire Server
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996, 1998
33  *     Doug Hay, 1998
34  */
35
36 #if defined(aix) || defined(linux)
37 #include <unistd.h>
38 #endif /* aix or linux */
39
40 #include <signal.h>
41 #include <errno.h>
42 #if !defined(_WIN32)
43 #include <sys/ioctl.h>
44 #endif
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <string.h>
48
49 #if defined(_WIN32)
50 #include <winsock.h>
51 #endif
52
53 #include "misc.h"
54 #include "nat.h"
55 #include "file.h"
56 #include "player.h"
57 #include "empthread.h"
58 #include "plane.h"
59 #include "nuke.h"
60 #include "land.h"
61 #include "ship.h"
62 #include "sect.h"
63 #include "var.h"
64 #include "product.h"
65 #include "optlist.h"
66 #include "global.h"
67 #include "prototypes.h"
68
69 s_char program[] = "server";
70
71 extern void player_accept(void *);
72 extern void player_kill_idle(void *);
73 extern void update_sched(void *);
74 extern void delete_lostitems(void *);
75 void nullify_objects(void);
76 void init_files(void);
77 void close_files(void);
78
79 #if defined(_WIN32)
80 static void loc_NTInit(void);
81 static void loc_NTTerm(void);
82 #endif
83
84 extern void mobility_init(void);
85 extern void mobility_check(void *);
86 extern void market_update(void *);
87
88 #if !defined(_WIN32)
89 static int mainpid = 0;
90 #endif
91
92 int
93 main(int argc, char **argv)
94 {
95     time_t now;
96     int hour[2];
97     int flags = 0;
98     int debug = 0;
99     int op;
100     char *config_file = NULL;
101     extern char *optarg;
102     s_char tbuf[256];
103 #ifdef POSIXSIGNALS
104     struct sigaction act;
105 #endif /* POSIXSIGNALS */
106
107 #if !defined(_WIN32)
108     mainpid = getpid();
109
110     while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
111         switch (op) {
112         case 'D':
113             datadir = optarg;
114             break;
115         case 'd':
116             debug++;
117             break;
118         case 'e':
119             config_file = optarg;
120             break;
121         case 'p':
122             flags |= EMPTH_PRINT;
123             break;
124         case 's':
125             flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
126             break;
127         case 'h':
128         default:
129             printf("Usage: %s -d -p -s\n", argv[0]);
130             return 0;
131         }
132     }
133 #endif
134     if (config_file == NULL) {
135         sprintf(tbuf, "%s/econfig", datadir);
136         config_file = tbuf;
137     }
138
139     logerror("------------------------------------------------------");
140 #if !defined(_WIN32)
141     logerror("Empire server (pid %d) started", getpid());
142 #else
143     logerror("Empire server started");
144 #endif /* _WIN32 */
145
146 #if defined(_WIN32)
147     loc_NTInit();
148 #endif
149     emp_config(config_file);
150     update_policy_check();
151
152     nullify_objects();
153
154 #if !defined(_WIN32)
155     /* signal() should not be used with mit pthreads. Anyway if u
156        have a posix threads u definitly have posix signals -- Sasha */
157 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
158 #ifdef SA_SIGINFO
159     act.sa_flags = SA_SIGINFO;
160 #endif
161     sigemptyset(&act.sa_mask);
162     if (debug == 0 && flags == 0) {
163         disassoc();
164     }
165     act.sa_handler = shutdwn;
166     /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
167        a Linux box running POSIX threads -- STM */
168 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
169     sigaction(SIGUSR1, &act, NULL);
170 #endif
171     sigaction(SIGTERM, &act, NULL);
172     sigaction(SIGINT, &act, NULL);
173     act.sa_handler = panic;
174     sigaction(SIGBUS, &act, NULL);
175     sigaction(SIGSEGV, &act, NULL);
176     sigaction(SIGILL, &act, NULL);
177     sigaction(SIGFPE, &act, NULL);
178     act.sa_handler = SIG_IGN;
179     sigaction(SIGPIPE, &act, NULL);
180 #else
181     if (debug == 0 && flags == 0) {
182         disassoc();
183         /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
184            a Linux box running POSIX threads -- STM */
185 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
186         signal(SIGUSR1, shutdwn);
187 #endif
188         signal(SIGTERM, shutdwn);
189         signal(SIGBUS, panic);
190         signal(SIGSEGV, panic);
191         signal(SIGILL, panic);
192         signal(SIGFPE, panic);
193         signal(SIGINT, shutdwn);
194     }
195     signal(SIGPIPE, SIG_IGN);
196 #endif /* POSIXSIGNALS */
197 #endif /* _WIN32 */
198     empth_init((char **)&player, flags);
199     time(&now);
200 #if !defined(_WIN32)
201     srandom(now);
202 #else
203     srand(now);
204 #endif /* _WIN32 */
205     global_init();
206     shutdown_init();
207     player_init();
208     ef_init();
209     init_files();
210     io_init();
211     gamehours(now, hour);
212
213     if (opt_MOB_ACCESS) {
214         /* This fixes up mobility upon restart */
215         mobility_init();
216     }
217
218     empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
219                  "AcceptPlayers", "Accept network connections", 0);
220     empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
221                  "KillIdle", "Kills idle player connections", 0);
222     empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
223                  "Schedules updates to occur", 0);
224     empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
225                  "DeleteItems", "Deletes old lost items", 0);
226     if (opt_MOB_ACCESS) {
227         /* Start the mobility access check thread */
228         empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
229                      "MobilityCheck", "Writes the timestamp file", 0);
230     }
231
232     if (opt_MARKET) {
233         empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
234                      "MarketUpdate", "Updates the market", 0);
235     }
236 #if defined(__linux__) && defined(_EMPTH_POSIX)
237     strcpy(tbuf, argv[0]);
238     for (op = 1; op < argc; op++) {
239         strcat(tbuf, " ");
240         strcat(tbuf, argv[op]);
241     }
242     sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
243 #endif
244
245     empth_exit();
246
247 /* We should never get here.  But, just in case... */
248     close_files();
249
250 #if defined(_WIN32)
251     loc_NTTerm();
252 #endif
253     return 0;
254 }
255
256 void
257 init_files(void)
258 {
259     ef_open(EF_NATION, O_RDWR, EFF_MEM);
260     ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
261     ef_open(EF_SHIP, O_RDWR, EFF_MEM);
262     ef_open(EF_PLANE, O_RDWR, EFF_MEM);
263     ef_open(EF_LAND, O_RDWR, EFF_MEM);
264     ef_open(EF_NEWS, O_RDWR, 0);
265     ef_open(EF_LOAN, O_RDWR, 0);
266     ef_open(EF_TREATY, O_RDWR, 0);
267     ef_open(EF_NUKE, O_RDWR, EFF_MEM);
268     ef_open(EF_POWER, O_RDWR, 0);
269     ef_open(EF_TRADE, O_RDWR, 0);
270     ef_open(EF_MAP, O_RDWR, EFF_MEM);
271     ef_open(EF_BMAP, O_RDWR, EFF_MEM);
272     ef_open(EF_COMM, O_RDWR, 0);
273     ef_open(EF_LOST, O_RDWR, 0);
274 }
275
276 void
277 close_files(void)
278 {
279     ef_close(EF_NATION);
280     ef_close(EF_SECTOR);
281     ef_close(EF_SHIP);
282     ef_close(EF_PLANE);
283     ef_close(EF_LAND);
284     ef_close(EF_NEWS);
285     ef_close(EF_LOAN);
286     ef_close(EF_TREATY);
287     ef_close(EF_NUKE);
288     ef_close(EF_POWER);
289     ef_close(EF_TRADE);
290     ef_close(EF_MAP);
291     ef_close(EF_COMM);
292     ef_close(EF_BMAP);
293     ef_close(EF_LOST);
294 }
295
296 /* we're going down.  try to close the files at least */
297 void
298 panic(int sig)
299 {
300 #if !defined(_WIN32)
301 #ifdef POSIXSIGNALS
302     struct sigaction act;
303
304     act.sa_flags = 0;
305     sigemptyset(&act.sa_mask);
306     act.sa_handler = SIG_DFL;
307     sigaction(SIGBUS, &act, NULL);
308     sigaction(SIGSEGV, &act, NULL);
309     sigaction(SIGILL, &act, NULL);
310     sigaction(SIGFPE, &act, NULL);
311 #else
312     signal(SIGBUS, SIG_DFL);
313     signal(SIGSEGV, SIG_DFL);
314     signal(SIGILL, SIG_DFL);
315     signal(SIGFPE, SIG_DFL);
316 #endif /* POSIXSIGNALS */
317 #endif /* _WIN32 */
318     logerror("server received fatal signal %d", sig);
319     log_last_commands();
320     close_files();
321     _exit(0);
322 }
323
324 void
325 shutdwn(int sig)
326 {
327     struct player *p;
328     time_t now;
329
330 #if defined(__linux__) && defined(_EMPTH_POSIX)
331 /* This is a hack to get around the way pthreads work on Linux.  This
332    may be useful on other platforms too where threads are turned into
333    processes. */
334     if (getpid() != mainpid) {
335         empth_t *me;
336
337         me = empth_self();
338         if (me && me->name) {
339             if (strlen(me->name) > 5) {
340                 /* Player threads are cleaned up below, so just have
341                    them return.  This should work. */
342                 if (!strncmp("Player", me->name, 6)) {
343                     return;
344                 }
345             }
346         }
347         /* Not a player thread - must be server thread, so exit */
348         empth_exit();
349         return;
350     }
351 #endif
352
353     logerror("Shutdown commencing (cleaning up threads.)");
354
355     for (p = player_next(0); p != 0; p = player_next(p)) {
356         if (p->state != PS_PLAYING)
357             continue;
358         pr_flash(p, "Server shutting down...\n");
359         p->state = PS_SHUTDOWN;
360         p->aborted++;
361         if (p->command) {
362             pr_flash(p, "Shutdown aborting command\n");
363         }
364         empth_wakeup(p->proc);
365     }
366
367     if (!sig) {
368         /* Sleep and let some cleanup happen - note this doesn't work
369            when called from a signal handler, since we may or may not
370            be in the right thread.  So we just pass by and kill 'em
371            all. */
372         time(&now);
373         empth_sleep(now + 1);
374     }
375
376     for (p = player_next(0); p != 0; p = player_next(p)) {
377         p->state = PS_KILL;
378         p->aborted++;
379         empth_terminate(p->proc);
380         p = player_delete(p);
381     }
382     if (sig)
383         logerror("Server shutting down on signal %d", sig);
384     else
385         logerror("Server shutting down at Deity's request");
386     close_files();
387     _exit(0);
388 }
389
390
391 void
392 nullify_objects(void)
393 {
394     int i, j;
395
396     if (opt_BIG_CITY) {
397         dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
398         dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
399         dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
400         dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
401         dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
402         dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
403     }
404     for (i = 0; i < pln_maxno; i++) {
405         if (opt_NO_HCMS)
406             plchr[i].pl_hcm = 0;
407         if (opt_NO_LCMS)
408             plchr[i].pl_lcm = 0;
409         if (opt_NO_OIL)
410             plchr[i].pl_fuel = 0;
411         if (opt_PLANENAMES)
412             plchr[i].pl_name = plchr[i].pl_planename;
413     }
414     for (i = 0; i < lnd_maxno; i++) {
415         if (opt_NO_HCMS)
416             lchr[i].l_hcm = 0;
417         if (opt_NO_LCMS)
418             lchr[i].l_lcm = 0;
419         /* Fix up the military values */
420         for (j = 0; j < lchr[i].l_nv; j++) {
421             if (lchr[i].l_vtype[j] == V_MILIT)
422                 lchr[i].l_mil = lchr[i].l_vamt[j];
423         }
424     }
425     for (i = 0; i < shp_maxno; i++) {
426         if (opt_NO_HCMS)
427             mchr[i].m_hcm = 0;
428         if (opt_NO_LCMS)
429             mchr[i].m_lcm = 0;
430         if (opt_NO_OIL) {
431             if (mchr[i].m_flags & M_OIL)
432                 mchr[i].m_name = 0;
433         }
434     }
435     for (i = 0; i < nuk_maxno; i++) {
436         if (opt_NO_HCMS)
437             nchr[i].n_hcm = 0;
438         if (opt_NO_LCMS)
439             nchr[i].n_lcm = 0;
440     }
441     for (i = 0; i < sct_maxno; i++) {
442         if (opt_NO_HCMS)
443             dchr[i].d_hcms = 0;
444         if (opt_NO_LCMS)
445             dchr[i].d_lcms = 0;
446     }
447     for (i = 0; i < prd_maxno; i++) {
448         for (j = 0; j < pchr[i].p_nv; j++) {
449             if (opt_NO_HCMS && pchr[i].p_vtype[j] == V_HCM)
450                 pchr[i].p_vamt[j] = 0;
451             if (opt_NO_LCMS && pchr[i].p_vtype[j] == V_LCM)
452                 pchr[i].p_vamt[j] = 0;
453             if (opt_NO_OIL && pchr[i].p_vtype[j] == V_OIL)
454                 pchr[i].p_vamt[j] = 0;
455         }
456     }
457 }
458
459 #if defined(_WIN32)
460 static void
461 loc_NTInit()
462 {
463     int rc;
464     WORD wVersionRequested;
465     WSADATA wsaData;
466
467     wVersionRequested = MAKEWORD(2, 0);
468     rc = WSAStartup(wVersionRequested, &wsaData);
469     if (rc != 0) {
470         logerror("WSAStartup failed.  %d", rc);
471         _exit(1);
472     }
473 }
474 #endif
475
476 #if defined(_WIN32)
477 static void
478 loc_NTTerm()
479 {
480     WSACleanup();
481 }
482 #endif