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>
57 #include "empthread.h"
67 #include "prototypes.h"
69 static void nullify_objects(void);
70 static void init_files(void);
71 static void close_files(void);
74 static void loc_NTInit(void);
75 static void loc_NTTerm(void);
79 static int mainpid = 0;
84 * If yes, don't fork into background, don't catch certain signals,
85 * call abort() on internal error.
90 main(int argc, char **argv)
95 char *config_file = NULL;
100 #endif /* POSIXSIGNALS */
107 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
116 config_file = optarg;
119 flags |= EMPTH_PRINT;
122 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
126 printf("Usage: %s -d -p -s\n", argv[0]);
131 if (config_file == NULL) {
132 sprintf(tbuf, "%s/econfig", datadir);
136 logerror("------------------------------------------------------");
138 logerror("Empire server (pid %d) started", getpid());
140 logerror("Empire server started");
146 emp_config(config_file);
147 update_policy_check();
152 /* signal() should not be used with mit pthreads. Anyway if u
153 have a posix threads u definitly have posix signals -- Sasha */
154 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
156 act.sa_flags = SA_SIGINFO;
158 sigemptyset(&act.sa_mask);
159 if (debug == 0 && flags == 0) {
162 act.sa_handler = shutdwn;
163 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
164 a Linux box running POSIX threads -- STM */
165 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
166 sigaction(SIGUSR1, &act, NULL);
168 sigaction(SIGTERM, &act, NULL);
169 sigaction(SIGINT, &act, NULL);
170 act.sa_handler = panic;
171 sigaction(SIGBUS, &act, NULL);
172 sigaction(SIGSEGV, &act, NULL);
173 sigaction(SIGILL, &act, NULL);
174 sigaction(SIGFPE, &act, NULL);
175 act.sa_handler = SIG_IGN;
176 sigaction(SIGPIPE, &act, NULL);
178 if (debug == 0 && flags == 0) {
180 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
181 a Linux box running POSIX threads -- STM */
182 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
183 signal(SIGUSR1, shutdwn);
185 signal(SIGTERM, shutdwn);
186 signal(SIGBUS, panic);
187 signal(SIGSEGV, panic);
188 signal(SIGILL, panic);
189 signal(SIGFPE, panic);
190 signal(SIGINT, shutdwn);
192 signal(SIGPIPE, SIG_IGN);
193 #endif /* POSIXSIGNALS */
195 empth_init((char **)&player, flags);
209 if (opt_MOB_ACCESS) {
210 /* This fixes up mobility upon restart */
214 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
215 "AcceptPlayers", "Accept network connections", 0);
216 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
217 "KillIdle", "Kills idle player connections", 0);
218 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
219 "Schedules updates to occur", 0);
220 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
221 "DeleteItems", "Deletes old lost items", 0);
222 if (opt_MOB_ACCESS) {
223 /* Start the mobility access check thread */
224 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
225 "MobilityCheck", "Writes the timestamp file", 0);
229 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
230 "MarketUpdate", "Updates the market", 0);
232 #if defined(__linux__) && defined(_EMPTH_POSIX)
233 strcpy(tbuf, argv[0]);
234 for (op = 1; op < argc; op++) {
236 strcat(tbuf, argv[op]);
238 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
243 /* We should never get here. But, just in case... */
255 ef_open(EF_NATION, O_RDWR, EFF_MEM);
256 ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
257 ef_open(EF_SHIP, O_RDWR, EFF_MEM);
258 ef_open(EF_PLANE, O_RDWR, EFF_MEM);
259 ef_open(EF_LAND, O_RDWR, EFF_MEM);
260 ef_open(EF_NEWS, O_RDWR, 0);
261 ef_open(EF_LOAN, O_RDWR, 0);
262 ef_open(EF_TREATY, O_RDWR, 0);
263 ef_open(EF_NUKE, O_RDWR, EFF_MEM);
264 ef_open(EF_POWER, O_RDWR, 0);
265 ef_open(EF_TRADE, O_RDWR, 0);
266 ef_open(EF_MAP, O_RDWR, EFF_MEM);
267 ef_open(EF_BMAP, O_RDWR, EFF_MEM);
268 ef_open(EF_COMM, O_RDWR, 0);
269 ef_open(EF_LOST, O_RDWR, 0);
292 /* we're going down. try to close the files at least */
298 struct sigaction act;
301 sigemptyset(&act.sa_mask);
302 act.sa_handler = SIG_DFL;
303 sigaction(SIGBUS, &act, NULL);
304 sigaction(SIGSEGV, &act, NULL);
305 sigaction(SIGILL, &act, NULL);
306 sigaction(SIGFPE, &act, NULL);
308 signal(SIGBUS, SIG_DFL);
309 signal(SIGSEGV, SIG_DFL);
310 signal(SIGILL, SIG_DFL);
311 signal(SIGFPE, SIG_DFL);
312 #endif /* POSIXSIGNALS */
314 logerror("server received fatal signal %d", sig);
326 #if defined(__linux__) && defined(_EMPTH_POSIX)
327 /* This is a hack to get around the way pthreads work on Linux. This
328 may be useful on other platforms too where threads are turned into
330 if (getpid() != mainpid) {
334 if (me && me->name) {
335 if (strlen(me->name) > 5) {
336 /* Player threads are cleaned up below, so just have
337 them return. This should work. */
338 if (!strncmp("Player", me->name, 6)) {
343 /* Not a player thread - must be server thread, so exit */
349 logerror("Shutdown commencing (cleaning up threads.)");
351 for (p = player_next(0); p != 0; p = player_next(p)) {
352 if (p->state != PS_PLAYING)
354 pr_flash(p, "Server shutting down...\n");
355 p->state = PS_SHUTDOWN;
358 pr_flash(p, "Shutdown aborting command\n");
360 empth_wakeup(p->proc);
364 /* Sleep and let some cleanup happen - note this doesn't work
365 when called from a signal handler, since we may or may not
366 be in the right thread. So we just pass by and kill 'em
369 empth_sleep(now + 1);
372 for (p = player_next(0); p != 0; p = player_next(p)) {
375 empth_terminate(p->proc);
376 p = player_delete(p);
379 logerror("Server shutting down on signal %d", sig);
381 logerror("Server shutting down at Deity's request");
388 nullify_objects(void)
393 dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
394 dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
395 dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
396 dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
397 dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
398 dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
401 dchr[SCT_LIGHT].d_cost = -1;
\r
403 dchr[SCT_HEAVY].d_cost = -1;
\r
405 dchr[SCT_OIL].d_cost = -1;
\r
406 dchr[SCT_REFINE].d_cost = -1;
\r
408 for (i = 0; i < pln_maxno; i++) {
414 plchr[i].pl_fuel = 0;
416 for (i = 0; i < lnd_maxno; i++) {
421 /* Fix up the military values */
422 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
424 for (i = 0; i < shp_maxno; i++) {
430 if (mchr[i].m_flags & M_OIL)
434 for (i = 0; i < nuk_maxno; i++) {
440 for (i = 0; i <= SCT_MAXDEF; i++) {
446 for (i = 0; i < prd_maxno; i++) {
447 for (j = 0; j < MAXPRCON; j++) {
448 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
449 pchr[i].p_camt[j] = 0;
450 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
451 pchr[i].p_camt[j] = 0;
452 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
453 pchr[i].p_camt[j] = 0;
463 WORD wVersionRequested;
466 wVersionRequested = MAKEWORD(2, 0);
467 rc = WSAStartup(wVersionRequested, &wsaData);
469 logerror("WSAStartup failed. %d", rc);