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
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"
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);
80 static int mainpid = 0;
85 * If yes, don't fork into background, don't catch certain signals,
86 * call abort() on internal error.
91 main(int argc, char **argv)
97 char *config_file = NULL;
101 struct sigaction act;
102 #endif /* POSIXSIGNALS */
109 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
118 config_file = optarg;
121 flags |= EMPTH_PRINT;
124 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
128 printf("Usage: %s -d -p -s\n", argv[0]);
133 if (config_file == NULL) {
134 sprintf(tbuf, "%s/econfig", datadir);
138 logerror("------------------------------------------------------");
140 logerror("Empire server (pid %d) started", getpid());
142 logerror("Empire server started");
148 emp_config(config_file);
149 update_policy_check();
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)
158 act.sa_flags = SA_SIGINFO;
160 sigemptyset(&act.sa_mask);
161 if (debug == 0 && flags == 0) {
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);
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);
180 if (debug == 0 && flags == 0) {
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);
187 signal(SIGTERM, shutdwn);
188 signal(SIGBUS, panic);
189 signal(SIGSEGV, panic);
190 signal(SIGILL, panic);
191 signal(SIGFPE, panic);
192 signal(SIGINT, shutdwn);
194 signal(SIGPIPE, SIG_IGN);
195 #endif /* POSIXSIGNALS */
197 empth_init((char **)&player, flags);
210 gamehours(now, hour);
212 if (opt_MOB_ACCESS) {
213 /* This fixes up mobility upon restart */
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);
232 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
233 "MarketUpdate", "Updates the market", 0);
235 #if defined(__linux__) && defined(_EMPTH_POSIX)
236 strcpy(tbuf, argv[0]);
237 for (op = 1; op < argc; op++) {
239 strcat(tbuf, argv[op]);
241 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
246 /* We should never get here. But, just in case... */
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);
295 /* we're going down. try to close the files at least */
301 struct sigaction act;
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);
311 signal(SIGBUS, SIG_DFL);
312 signal(SIGSEGV, SIG_DFL);
313 signal(SIGILL, SIG_DFL);
314 signal(SIGFPE, SIG_DFL);
315 #endif /* POSIXSIGNALS */
317 logerror("server received fatal signal %d", sig);
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
333 if (getpid() != mainpid) {
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)) {
346 /* Not a player thread - must be server thread, so exit */
352 logerror("Shutdown commencing (cleaning up threads.)");
354 for (p = player_next(0); p != 0; p = player_next(p)) {
355 if (p->state != PS_PLAYING)
357 pr_flash(p, "Server shutting down...\n");
358 p->state = PS_SHUTDOWN;
361 pr_flash(p, "Shutdown aborting command\n");
363 empth_wakeup(p->proc);
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
372 empth_sleep(now + 1);
375 for (p = player_next(0); p != 0; p = player_next(p)) {
378 empth_terminate(p->proc);
379 p = player_delete(p);
382 logerror("Server shutting down on signal %d", sig);
384 logerror("Server shutting down at Deity's request");
391 nullify_objects(void)
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;
403 for (i = 0; i < pln_maxno; i++) {
409 plchr[i].pl_fuel = 0;
411 plchr[i].pl_name = plchr[i].pl_planename;
413 for (i = 0; i < lnd_maxno; i++) {
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];
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_maxno; i++) {
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;
463 WORD wVersionRequested;
466 wVersionRequested = MAKEWORD(2, 0);
467 rc = WSAStartup(wVersionRequested, &wsaData);
469 logerror("WSAStartup failed. %d", rc);