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