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