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
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.
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.
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
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.
28 * main.c: Thread and signal initialization for Empire Server
30 * Known contributors to this file:
32 * Steve McClure, 1996, 1998
36 #if defined(aix) || defined(linux)
38 #endif /* aix or linux */
43 #include <sys/ioctl.h>
58 #include "empthread.h"
68 #include "prototypes.h"
70 static void nullify_objects(void);
71 static void init_files(void);
72 static void close_files(void);
75 static void loc_NTInit(void);
76 static void loc_NTTerm(void);
79 static int mainpid = 0;
83 * If yes, don't fork into background, don't catch certain signals,
84 * call abort() on internal error.
89 main(int argc, char **argv)
94 char *config_file = NULL;
99 #endif /* POSIXSIGNALS */
106 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
115 config_file = optarg;
118 flags |= EMPTH_PRINT;
121 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
125 printf("Usage: %s -d -p -s\n", argv[0]);
130 if (config_file == NULL) {
131 sprintf(tbuf, "%s/econfig", datadir);
135 logerror("------------------------------------------------------");
136 logerror("Empire server (pid %d) started", getpid());
141 emp_config(config_file);
142 update_policy_check();
147 /* signal() should not be used with mit pthreads. Anyway if u
148 have a posix threads u definitly have posix signals -- Sasha */
149 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
151 act.sa_flags = SA_SIGINFO;
153 sigemptyset(&act.sa_mask);
154 if (debug == 0 && flags == 0) {
157 act.sa_handler = shutdwn;
158 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
159 a Linux box running POSIX threads -- STM */
160 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
161 sigaction(SIGUSR1, &act, NULL);
163 sigaction(SIGTERM, &act, NULL);
164 sigaction(SIGINT, &act, NULL);
165 act.sa_handler = panic;
166 sigaction(SIGBUS, &act, NULL);
167 sigaction(SIGSEGV, &act, NULL);
168 sigaction(SIGILL, &act, NULL);
169 sigaction(SIGFPE, &act, NULL);
170 act.sa_handler = SIG_IGN;
171 sigaction(SIGPIPE, &act, NULL);
173 if (debug == 0 && flags == 0) {
175 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
176 a Linux box running POSIX threads -- STM */
177 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
178 signal(SIGUSR1, shutdwn);
180 signal(SIGTERM, shutdwn);
181 signal(SIGBUS, panic);
182 signal(SIGSEGV, panic);
183 signal(SIGILL, panic);
184 signal(SIGFPE, panic);
185 signal(SIGINT, shutdwn);
187 signal(SIGPIPE, SIG_IGN);
188 #endif /* POSIXSIGNALS */
190 empth_init((char **)&player, flags);
204 if (opt_MOB_ACCESS) {
205 /* This fixes up mobility upon restart */
209 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
210 "AcceptPlayers", "Accept network connections", 0);
211 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
212 "KillIdle", "Kills idle player connections", 0);
213 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
214 "Schedules updates to occur", 0);
215 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
216 "DeleteItems", "Deletes old lost items", 0);
217 if (opt_MOB_ACCESS) {
218 /* Start the mobility access check thread */
219 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
220 "MobilityCheck", "Writes the timestamp file", 0);
224 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
225 "MarketUpdate", "Updates the market", 0);
227 #if defined(__linux__) && defined(_EMPTH_POSIX)
228 strcpy(tbuf, argv[0]);
229 for (op = 1; op < argc; op++) {
231 strcat(tbuf, argv[op]);
233 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
238 /* We should never get here. But, just in case... */
250 ef_open(EF_NATION, O_RDWR, EFF_MEM);
251 ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
252 ef_open(EF_SHIP, O_RDWR, EFF_MEM);
253 ef_open(EF_PLANE, O_RDWR, EFF_MEM);
254 ef_open(EF_LAND, O_RDWR, EFF_MEM);
255 ef_open(EF_NEWS, O_RDWR, 0);
256 ef_open(EF_LOAN, O_RDWR, 0);
257 ef_open(EF_TREATY, O_RDWR, 0);
258 ef_open(EF_NUKE, O_RDWR, EFF_MEM);
259 ef_open(EF_POWER, O_RDWR, 0);
260 ef_open(EF_TRADE, O_RDWR, 0);
261 ef_open(EF_MAP, O_RDWR, EFF_MEM);
262 ef_open(EF_BMAP, O_RDWR, EFF_MEM);
263 ef_open(EF_COMM, O_RDWR, 0);
264 ef_open(EF_LOST, O_RDWR, 0);
287 /* we're going down. try to close the files at least */
293 struct sigaction act;
296 sigemptyset(&act.sa_mask);
297 act.sa_handler = SIG_DFL;
298 sigaction(SIGBUS, &act, NULL);
299 sigaction(SIGSEGV, &act, NULL);
300 sigaction(SIGILL, &act, NULL);
301 sigaction(SIGFPE, &act, NULL);
303 signal(SIGBUS, SIG_DFL);
304 signal(SIGSEGV, SIG_DFL);
305 signal(SIGILL, SIG_DFL);
306 signal(SIGFPE, SIG_DFL);
307 #endif /* POSIXSIGNALS */
309 logerror("server received fatal signal %d", sig);
321 #if defined(__linux__) && defined(_EMPTH_POSIX)
322 /* This is a hack to get around the way pthreads work on Linux. This
323 may be useful on other platforms too where threads are turned into
325 if (getpid() != mainpid) {
329 if (me && me->name) {
330 if (strlen(me->name) > 5) {
331 /* Player threads are cleaned up below, so just have
332 them return. This should work. */
333 if (!strncmp("Player", me->name, 6)) {
338 /* Not a player thread - must be server thread, so exit */
344 logerror("Shutdown commencing (cleaning up threads.)");
346 for (p = player_next(0); p != 0; p = player_next(p)) {
347 if (p->state != PS_PLAYING)
349 pr_flash(p, "Server shutting down...\n");
350 p->state = PS_SHUTDOWN;
353 pr_flash(p, "Shutdown aborting command\n");
355 empth_wakeup(p->proc);
359 /* Sleep and let some cleanup happen - note this doesn't work
360 when called from a signal handler, since we may or may not
361 be in the right thread. So we just pass by and kill 'em
364 empth_sleep(now + 1);
367 for (p = player_next(0); p != 0; p = player_next(p)) {
370 empth_terminate(p->proc);
371 p = player_delete(p);
374 logerror("Server shutting down on signal %d", sig);
376 logerror("Server shutting down at Deity's request");
383 nullify_objects(void)
388 dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
389 dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
390 dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
391 dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
392 dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
393 dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
396 dchr[SCT_LIGHT].d_cost = -1;
398 dchr[SCT_HEAVY].d_cost = -1;
400 dchr[SCT_OIL].d_cost = -1;
401 dchr[SCT_REFINE].d_cost = -1;
403 for (i = 0; i < pln_maxno; i++) {
409 plchr[i].pl_fuel = 0;
411 for (i = 0; i < lnd_maxno; i++) {
416 /* Fix up the military values */
417 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
419 for (i = 0; i < shp_maxno; i++) {
425 if (mchr[i].m_flags & M_OIL)
429 for (i = 0; i < nuk_maxno; i++) {
435 for (i = 0; i <= SCT_MAXDEF; i++) {
441 for (i = 0; i < prd_maxno; i++) {
442 for (j = 0; j < MAXPRCON; j++) {
443 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
444 pchr[i].p_camt[j] = 0;
445 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
446 pchr[i].p_camt[j] = 0;
447 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
448 pchr[i].p_camt[j] = 0;
458 WORD wVersionRequested;
461 wVersionRequested = MAKEWORD(2, 0);
462 rc = WSAStartup(wVersionRequested, &wsaData);
464 logerror("WSAStartup failed. %d", rc);