]> git.pond.sub.org Git - empserver/blob - src/server/main.c
(debug): New.
[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 "server.h"
68 #include "prototypes.h"
69
70 static void nullify_objects(void);
71 static void init_files(void);
72 static void close_files(void);
73
74 #if defined(_WIN32)
75 static void loc_NTInit(void);
76 static void loc_NTTerm(void);
77 #endif
78
79 #if !defined(_WIN32)
80 static int mainpid = 0;
81 #endif
82
83 /*
84  * Debugging?
85  * If yes, don't fork into background, don't catch certain signals,
86  * call abort() on internal error.
87  */
88 int debug = 0;
89
90 int
91 main(int argc, char **argv)
92 {
93     time_t now;
94     int hour[2];
95     int flags = 0;
96     int op;
97     char *config_file = NULL;
98     extern char *optarg;
99     s_char tbuf[256];
100 #ifdef POSIXSIGNALS
101     struct sigaction act;
102 #endif /* POSIXSIGNALS */
103
104     loginit("server");
105
106 #if !defined(_WIN32)
107     mainpid = getpid();
108
109     while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
110         switch (op) {
111         case 'D':
112             datadir = optarg;
113             break;
114         case 'd':
115             debug++;
116             break;
117         case 'e':
118             config_file = optarg;
119             break;
120         case 'p':
121             flags |= EMPTH_PRINT;
122             break;
123         case 's':
124             flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
125             break;
126         case 'h':
127         default:
128             printf("Usage: %s -d -p -s\n", argv[0]);
129             return 0;
130         }
131     }
132 #endif
133     if (config_file == NULL) {
134         sprintf(tbuf, "%s/econfig", datadir);
135         config_file = tbuf;
136     }
137
138     logerror("------------------------------------------------------");
139 #if !defined(_WIN32)
140     logerror("Empire server (pid %d) started", getpid());
141 #else
142     logerror("Empire server started");
143 #endif /* _WIN32 */
144
145 #if defined(_WIN32)
146     loc_NTInit();
147 #endif
148     emp_config(config_file);
149     update_policy_check();
150
151     nullify_objects();
152
153 #if !defined(_WIN32)
154     /* signal() should not be used with mit pthreads. Anyway if u
155        have a posix threads u definitly have posix signals -- Sasha */
156 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
157 #ifdef SA_SIGINFO
158     act.sa_flags = SA_SIGINFO;
159 #endif
160     sigemptyset(&act.sa_mask);
161     if (debug == 0 && flags == 0) {
162         disassoc();
163     }
164     act.sa_handler = shutdwn;
165     /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
166        a Linux box running POSIX threads -- STM */
167 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
168     sigaction(SIGUSR1, &act, NULL);
169 #endif
170     sigaction(SIGTERM, &act, NULL);
171     sigaction(SIGINT, &act, NULL);
172     act.sa_handler = panic;
173     sigaction(SIGBUS, &act, NULL);
174     sigaction(SIGSEGV, &act, NULL);
175     sigaction(SIGILL, &act, NULL);
176     sigaction(SIGFPE, &act, NULL);
177     act.sa_handler = SIG_IGN;
178     sigaction(SIGPIPE, &act, NULL);
179 #else
180     if (debug == 0 && flags == 0) {
181         disassoc();
182         /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
183            a Linux box running POSIX threads -- STM */
184 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
185         signal(SIGUSR1, shutdwn);
186 #endif
187         signal(SIGTERM, shutdwn);
188         signal(SIGBUS, panic);
189         signal(SIGSEGV, panic);
190         signal(SIGILL, panic);
191         signal(SIGFPE, panic);
192         signal(SIGINT, shutdwn);
193     }
194     signal(SIGPIPE, SIG_IGN);
195 #endif /* POSIXSIGNALS */
196 #endif /* _WIN32 */
197     empth_init((char **)&player, flags);
198     time(&now);
199 #if !defined(_WIN32)
200     srandom(now);
201 #else
202     srand(now);
203 #endif /* _WIN32 */
204     global_init();
205     shutdown_init();
206     player_init();
207     ef_init();
208     init_files();
209     io_init();
210     gamehours(now, hour);
211
212     if (opt_MOB_ACCESS) {
213         /* This fixes up mobility upon restart */
214         mobility_init();
215     }
216
217     empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
218                  "AcceptPlayers", "Accept network connections", 0);
219     empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
220                  "KillIdle", "Kills idle player connections", 0);
221     empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
222                  "Schedules updates to occur", 0);
223     empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
224                  "DeleteItems", "Deletes old lost items", 0);
225     if (opt_MOB_ACCESS) {
226         /* Start the mobility access check thread */
227         empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
228                      "MobilityCheck", "Writes the timestamp file", 0);
229     }
230
231     if (opt_MARKET) {
232         empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
233                      "MarketUpdate", "Updates the market", 0);
234     }
235 #if defined(__linux__) && defined(_EMPTH_POSIX)
236     strcpy(tbuf, argv[0]);
237     for (op = 1; op < argc; op++) {
238         strcat(tbuf, " ");
239         strcat(tbuf, argv[op]);
240     }
241     sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
242 #endif
243
244     empth_exit();
245
246 /* We should never get here.  But, just in case... */
247     close_files();
248
249 #if defined(_WIN32)
250     loc_NTTerm();
251 #endif
252     return 0;
253 }
254
255 static void
256 init_files(void)
257 {
258     ef_open(EF_NATION, O_RDWR, EFF_MEM);
259     ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
260     ef_open(EF_SHIP, O_RDWR, EFF_MEM);
261     ef_open(EF_PLANE, O_RDWR, EFF_MEM);
262     ef_open(EF_LAND, O_RDWR, EFF_MEM);
263     ef_open(EF_NEWS, O_RDWR, 0);
264     ef_open(EF_LOAN, O_RDWR, 0);
265     ef_open(EF_TREATY, O_RDWR, 0);
266     ef_open(EF_NUKE, O_RDWR, EFF_MEM);
267     ef_open(EF_POWER, O_RDWR, 0);
268     ef_open(EF_TRADE, O_RDWR, 0);
269     ef_open(EF_MAP, O_RDWR, EFF_MEM);
270     ef_open(EF_BMAP, O_RDWR, EFF_MEM);
271     ef_open(EF_COMM, O_RDWR, 0);
272     ef_open(EF_LOST, O_RDWR, 0);
273 }
274
275 static void
276 close_files(void)
277 {
278     ef_close(EF_NATION);
279     ef_close(EF_SECTOR);
280     ef_close(EF_SHIP);
281     ef_close(EF_PLANE);
282     ef_close(EF_LAND);
283     ef_close(EF_NEWS);
284     ef_close(EF_LOAN);
285     ef_close(EF_TREATY);
286     ef_close(EF_NUKE);
287     ef_close(EF_POWER);
288     ef_close(EF_TRADE);
289     ef_close(EF_MAP);
290     ef_close(EF_COMM);
291     ef_close(EF_BMAP);
292     ef_close(EF_LOST);
293 }
294
295 /* we're going down.  try to close the files at least */
296 void
297 panic(int sig)
298 {
299 #if !defined(_WIN32)
300 #ifdef POSIXSIGNALS
301     struct sigaction act;
302
303     act.sa_flags = 0;
304     sigemptyset(&act.sa_mask);
305     act.sa_handler = SIG_DFL;
306     sigaction(SIGBUS, &act, NULL);
307     sigaction(SIGSEGV, &act, NULL);
308     sigaction(SIGILL, &act, NULL);
309     sigaction(SIGFPE, &act, NULL);
310 #else
311     signal(SIGBUS, SIG_DFL);
312     signal(SIGSEGV, SIG_DFL);
313     signal(SIGILL, SIG_DFL);
314     signal(SIGFPE, SIG_DFL);
315 #endif /* POSIXSIGNALS */
316 #endif /* _WIN32 */
317     logerror("server received fatal signal %d", sig);
318     log_last_commands();
319     close_files();
320     _exit(0);
321 }
322
323 void
324 shutdwn(int sig)
325 {
326     struct player *p;
327     time_t now;
328
329 #if defined(__linux__) && defined(_EMPTH_POSIX)
330 /* This is a hack to get around the way pthreads work on Linux.  This
331    may be useful on other platforms too where threads are turned into
332    processes. */
333     if (getpid() != mainpid) {
334         empth_t *me;
335
336         me = empth_self();
337         if (me && me->name) {
338             if (strlen(me->name) > 5) {
339                 /* Player threads are cleaned up below, so just have
340                    them return.  This should work. */
341                 if (!strncmp("Player", me->name, 6)) {
342                     return;
343                 }
344             }
345         }
346         /* Not a player thread - must be server thread, so exit */
347         empth_exit();
348         return;
349     }
350 #endif
351
352     logerror("Shutdown commencing (cleaning up threads.)");
353
354     for (p = player_next(0); p != 0; p = player_next(p)) {
355         if (p->state != PS_PLAYING)
356             continue;
357         pr_flash(p, "Server shutting down...\n");
358         p->state = PS_SHUTDOWN;
359         p->aborted++;
360         if (p->command) {
361             pr_flash(p, "Shutdown aborting command\n");
362         }
363         empth_wakeup(p->proc);
364     }
365
366     if (!sig) {
367         /* Sleep and let some cleanup happen - note this doesn't work
368            when called from a signal handler, since we may or may not
369            be in the right thread.  So we just pass by and kill 'em
370            all. */
371         time(&now);
372         empth_sleep(now + 1);
373     }
374
375     for (p = player_next(0); p != 0; p = player_next(p)) {
376         p->state = PS_KILL;
377         p->aborted++;
378         empth_terminate(p->proc);
379         p = player_delete(p);
380     }
381     if (sig)
382         logerror("Server shutting down on signal %d", sig);
383     else
384         logerror("Server shutting down at Deity's request");
385     close_files();
386     _exit(0);
387 }
388
389
390 static void
391 nullify_objects(void)
392 {
393     int i, j;
394
395     if (opt_BIG_CITY) {
396         dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
397         dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
398         dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
399         dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
400         dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
401         dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
402     }
403     for (i = 0; i < pln_maxno; i++) {
404         if (opt_NO_HCMS)
405             plchr[i].pl_hcm = 0;
406         if (opt_NO_LCMS)
407             plchr[i].pl_lcm = 0;
408         if (opt_NO_OIL)
409             plchr[i].pl_fuel = 0;
410         if (opt_PLANENAMES)
411             plchr[i].pl_name = plchr[i].pl_planename;
412     }
413     for (i = 0; i < lnd_maxno; i++) {
414         if (opt_NO_HCMS)
415             lchr[i].l_hcm = 0;
416         if (opt_NO_LCMS)
417             lchr[i].l_lcm = 0;
418         /* Fix up the military values */
419         for (j = 0; j < lchr[i].l_nv; j++) {
420             if (lchr[i].l_vtype[j] == V_MILIT)
421                 lchr[i].l_mil = lchr[i].l_vamt[j];
422         }
423     }
424     for (i = 0; i < shp_maxno; i++) {
425         if (opt_NO_HCMS)
426             mchr[i].m_hcm = 0;
427         if (opt_NO_LCMS)
428             mchr[i].m_lcm = 0;
429         if (opt_NO_OIL) {
430             if (mchr[i].m_flags & M_OIL)
431                 mchr[i].m_name = 0;
432         }
433     }
434     for (i = 0; i < nuk_maxno; i++) {
435         if (opt_NO_HCMS)
436             nchr[i].n_hcm = 0;
437         if (opt_NO_LCMS)
438             nchr[i].n_lcm = 0;
439     }
440     for (i = 0; i < sct_maxno; i++) {
441         if (opt_NO_HCMS)
442             dchr[i].d_hcms = 0;
443         if (opt_NO_LCMS)
444             dchr[i].d_lcms = 0;
445     }
446     for (i = 0; i < prd_maxno; i++) {
447         for (j = 0; j < pchr[i].p_nv; j++) {
448             if (opt_NO_HCMS && pchr[i].p_vtype[j] == V_HCM)
449                 pchr[i].p_vamt[j] = 0;
450             if (opt_NO_LCMS && pchr[i].p_vtype[j] == V_LCM)
451                 pchr[i].p_vamt[j] = 0;
452             if (opt_NO_OIL && pchr[i].p_vtype[j] == V_OIL)
453                 pchr[i].p_vamt[j] = 0;
454         }
455     }
456 }
457
458 #if defined(_WIN32)
459 static void
460 loc_NTInit()
461 {
462     int rc;
463     WORD wVersionRequested;
464     WSADATA wsaData;
465
466     wVersionRequested = MAKEWORD(2, 0);
467     rc = WSAStartup(wVersionRequested, &wsaData);
468     if (rc != 0) {
469         logerror("WSAStartup failed.  %d", rc);
470         _exit(1);
471     }
472 }
473 #endif
474
475 #if defined(_WIN32)
476 static void
477 loc_NTTerm()
478 {
479     WSACleanup();
480 }
481 #endif