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>
59 #include "empthread.h"
69 #include "prototypes.h"
71 static void nullify_objects(void);
72 static void init_files(void);
73 static void close_files(void);
76 static void loc_NTInit(void);
77 static void loc_NTTerm(void);
80 static int mainpid = 0;
84 * If yes, don't fork into background, don't catch certain signals,
85 * call abort() on internal error.
90 print_usage(char *program_name)
93 printf("Usage: %s -D datadir -e config_file -d\n", program_name);
95 printf("Usage: %s -D datadir -e config_file -d -p -s\n", program_name);
96 printf("-p print flag\n");
97 printf("-s stack check flag (include print flag)\n");
99 printf("-d debug mode\n");
103 main(int argc, char **argv)
107 char *config_file = NULL;
111 struct sigaction act;
112 #endif /* POSIXSIGNALS */
119 while ((op = getopt(argc, argv, "D:de:h")) != EOF) {
121 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
131 config_file = optarg;
135 flags |= EMPTH_PRINT;
138 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
143 print_usage(argv[0]);
148 if (config_file == NULL) {
149 sprintf(tbuf, "%s/econfig", datadir);
153 logerror("------------------------------------------------------");
154 logerror("Empire server (pid %d) started", getpid());
159 emp_config(config_file);
160 update_policy_check();
165 /* signal() should not be used with mit pthreads. Anyway if u
166 have a posix threads u definitly have posix signals -- Sasha */
167 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
169 act.sa_flags = SA_SIGINFO;
171 sigemptyset(&act.sa_mask);
172 if (debug == 0 && flags == 0) {
175 act.sa_handler = shutdwn;
176 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
177 a Linux box running POSIX threads -- STM */
178 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
179 sigaction(SIGUSR1, &act, NULL);
181 sigaction(SIGTERM, &act, NULL);
182 sigaction(SIGINT, &act, NULL);
183 act.sa_handler = panic;
184 sigaction(SIGBUS, &act, NULL);
185 sigaction(SIGSEGV, &act, NULL);
186 sigaction(SIGILL, &act, NULL);
187 sigaction(SIGFPE, &act, NULL);
188 act.sa_handler = SIG_IGN;
189 sigaction(SIGPIPE, &act, NULL);
191 if (debug == 0 && flags == 0) {
193 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
194 a Linux box running POSIX threads -- STM */
195 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
196 signal(SIGUSR1, shutdwn);
198 signal(SIGTERM, shutdwn);
199 signal(SIGBUS, panic);
200 signal(SIGSEGV, panic);
201 signal(SIGILL, panic);
202 signal(SIGFPE, panic);
203 signal(SIGINT, shutdwn);
205 signal(SIGPIPE, SIG_IGN);
206 #endif /* POSIXSIGNALS */
208 empth_init((char **)&player, flags);
217 if (opt_MOB_ACCESS) {
218 /* This fixes up mobility upon restart */
222 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
223 "AcceptPlayers", "Accept network connections", 0);
224 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
225 "KillIdle", "Kills idle player connections", 0);
226 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
227 "Schedules updates to occur", 0);
228 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
229 "DeleteItems", "Deletes old lost items", 0);
230 if (opt_MOB_ACCESS) {
231 /* Start the mobility access check thread */
232 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
233 "MobilityCheck", "Writes the timestamp file", 0);
237 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
238 "MarketUpdate", "Updates the market", 0);
240 #if defined(__linux__) && defined(_EMPTH_POSIX)
241 strcpy(tbuf, argv[0]);
242 for (op = 1; op < argc; op++) {
244 strcat(tbuf, argv[op]);
246 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
251 /* We should never get here. But, just in case... */
263 ef_open(EF_NATION, O_RDWR, EFF_MEM);
264 ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
265 ef_open(EF_SHIP, O_RDWR, EFF_MEM);
266 ef_open(EF_PLANE, O_RDWR, EFF_MEM);
267 ef_open(EF_LAND, O_RDWR, EFF_MEM);
268 ef_open(EF_NEWS, O_RDWR, 0);
269 ef_open(EF_LOAN, O_RDWR, 0);
270 ef_open(EF_TREATY, O_RDWR, 0);
271 ef_open(EF_NUKE, O_RDWR, EFF_MEM);
272 ef_open(EF_POWER, O_RDWR, 0);
273 ef_open(EF_TRADE, O_RDWR, 0);
274 ef_open(EF_MAP, O_RDWR, EFF_MEM);
275 ef_open(EF_BMAP, O_RDWR, EFF_MEM);
276 ef_open(EF_COMM, O_RDWR, 0);
277 ef_open(EF_LOST, O_RDWR, 0);
300 /* we're going down. try to close the files at least */
306 struct sigaction act;
309 sigemptyset(&act.sa_mask);
310 act.sa_handler = SIG_DFL;
311 sigaction(SIGBUS, &act, NULL);
312 sigaction(SIGSEGV, &act, NULL);
313 sigaction(SIGILL, &act, NULL);
314 sigaction(SIGFPE, &act, NULL);
316 signal(SIGBUS, SIG_DFL);
317 signal(SIGSEGV, SIG_DFL);
318 signal(SIGILL, SIG_DFL);
319 signal(SIGFPE, SIG_DFL);
320 #endif /* POSIXSIGNALS */
322 logerror("server received fatal signal %d", sig);
334 #if defined(__linux__) && defined(_EMPTH_POSIX)
335 /* This is a hack to get around the way pthreads work on Linux. This
336 may be useful on other platforms too where threads are turned into
338 if (getpid() != mainpid) {
342 if (me && me->name) {
343 if (strlen(me->name) > 5) {
344 /* Player threads are cleaned up below, so just have
345 them return. This should work. */
346 if (!strncmp("Player", me->name, 6)) {
351 /* Not a player thread - must be server thread, so exit */
357 logerror("Shutdown commencing (cleaning up threads.)");
359 for (p = player_next(0); p != 0; p = player_next(p)) {
360 if (p->state != PS_PLAYING)
362 pr_flash(p, "Server shutting down...\n");
363 p->state = PS_SHUTDOWN;
366 pr_flash(p, "Shutdown aborting command\n");
368 empth_wakeup(p->proc);
372 /* Sleep and let some cleanup happen - note this doesn't work
373 when called from a signal handler, since we may or may not
374 be in the right thread. So we just pass by and kill 'em
377 empth_sleep(now + 1);
380 for (p = player_next(0); p != 0; p = player_next(p)) {
383 empth_terminate(p->proc);
384 p = player_delete(p);
387 logerror("Server shutting down on signal %d", sig);
389 logerror("Server shutting down at Deity's request");
396 nullify_objects(void)
401 dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
402 dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
403 dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
404 dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
405 dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
406 dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
409 dchr[SCT_LIGHT].d_cost = -1;
411 dchr[SCT_HEAVY].d_cost = -1;
413 dchr[SCT_OIL].d_cost = -1;
414 dchr[SCT_REFINE].d_cost = -1;
416 for (i = 0; i < pln_maxno; i++) {
422 plchr[i].pl_fuel = 0;
424 for (i = 0; i < lnd_maxno; i++) {
429 /* Fix up the military values */
430 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
432 for (i = 0; i < shp_maxno; i++) {
438 if (mchr[i].m_flags & M_OIL)
442 for (i = 0; i < nuk_maxno; i++) {
448 for (i = 0; i <= SCT_MAXDEF; i++) {
454 for (i = 0; i < prd_maxno; i++) {
455 for (j = 0; j < MAXPRCON; j++) {
456 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
457 pchr[i].p_camt[j] = 0;
458 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
459 pchr[i].p_camt[j] = 0;
460 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
461 pchr[i].p_camt[j] = 0;
471 WORD wVersionRequested;
474 wVersionRequested = MAKEWORD(2, 0);
475 rc = WSAStartup(wVersionRequested, &wsaData);
477 logerror("WSAStartup failed. %d", rc);