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>
56 #include "empthread.h"
66 #include "prototypes.h"
68 s_char program[] = "server";
70 extern void player_accept();
71 extern void player_kill_idle();
72 extern void update_sched();
73 extern void delete_lostitems();
74 void nullify_objects(void);
75 void init_files(void);
76 void close_files(void);
79 static void loc_NTInit(void);
80 static void loc_NTTerm(void);
83 extern void mobility_init();
84 extern void mobility_check();
85 extern void market_update();
88 static int mainpid = 0;
101 char *config_file = NULL;
102 extern s_char *datadir;
106 struct sigaction act;
107 #endif /* POSIXSIGNALS */
112 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
121 config_file = optarg;
124 flags |= EMPTH_PRINT;
127 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
131 printf("Usage: %s -d -p -s\n", argv[0]);
136 if (config_file == NULL) {
137 sprintf(tbuf, "%s/econfig", datadir);
141 logerror("------------------------------------------------------");
143 logerror("Empire server (pid %d) started", getpid());
145 logerror("Empire server started");
151 emp_config(config_file);
152 update_policy_check();
157 /* signal() should not be used with mit pthreads. Anyway if u
158 have a posix threads u definitly have posix signals -- Sasha */
159 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
161 act.sa_flags = SA_SIGINFO;
163 sigemptyset(&act.sa_mask);
164 if (debug == 0 && flags == 0) {
167 act.sa_handler = shutdwn;
168 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
169 a Linux box running POSIX threads -- STM */
170 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
171 sigaction(SIGUSR1, &act, NULL);
173 sigaction(SIGTERM, &act, NULL);
174 sigaction(SIGINT, &act, NULL);
175 act.sa_handler = panic;
176 sigaction(SIGBUS, &act, NULL);
177 sigaction(SIGSEGV, &act, NULL);
178 sigaction(SIGILL, &act, NULL);
179 sigaction(SIGFPE, &act, NULL);
180 act.sa_handler = SIG_IGN;
181 sigaction(SIGPIPE, &act, NULL);
183 if (debug == 0 && flags == 0) {
185 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
186 a Linux box running POSIX threads -- STM */
187 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
188 signal(SIGUSR1, shutdwn);
190 signal(SIGTERM, shutdwn);
191 signal(SIGBUS, panic);
192 signal(SIGSEGV, panic);
193 signal(SIGILL, panic);
194 signal(SIGFPE, panic);
195 signal(SIGINT, shutdwn);
197 signal(SIGPIPE, SIG_IGN);
198 #endif /* POSIXSIGNALS */
200 empth_init((char **)&player, flags);
207 setfdtablesize(32, 256);
214 gamehours(now, hour);
216 if (opt_MOB_ACCESS) {
217 /* This fixes up mobility upon restart */
221 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
222 "AcceptPlayers", "Accept network connections", 0);
223 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
224 "KillIdle", "Kills idle player connections", 0);
225 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
226 "Schedules updates to occur", 0);
227 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
228 "DeleteItems", "Deletes old lost items", 0);
229 if (opt_MOB_ACCESS) {
230 /* Start the mobility access check thread */
231 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
232 "MobilityCheck", "Writes the timestamp file", 0);
236 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
237 "MarketUpdate", "Updates the market", 0);
239 #if defined(__linux__) && defined(_EMPTH_POSIX)
240 strcpy(tbuf, argv[0]);
241 for (op = 1; op < argc; op++) {
243 strcat(tbuf, argv[op]);
245 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
250 /* We should never get here. But, just in case... */
262 ef_open(EF_NATION, O_RDWR, EFF_MEM);
263 ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
264 ef_open(EF_SHIP, O_RDWR, EFF_MEM);
265 ef_open(EF_PLANE, O_RDWR, EFF_MEM);
266 ef_open(EF_LAND, O_RDWR, EFF_MEM);
267 ef_open(EF_NEWS, O_RDWR, 0);
268 ef_open(EF_LOAN, O_RDWR, 0);
269 ef_open(EF_TREATY, O_RDWR, 0);
270 ef_open(EF_NUKE, O_RDWR, EFF_MEM);
271 ef_open(EF_POWER, O_RDWR, 0);
272 ef_open(EF_TRADE, O_RDWR, 0);
273 ef_open(EF_MAP, O_RDWR, EFF_MEM);
274 ef_open(EF_BMAP, O_RDWR, EFF_MEM);
275 ef_open(EF_COMM, O_RDWR, 0);
276 ef_open(EF_LOST, O_RDWR, 0);
299 /* we're going down. try to close the files at least */
305 struct sigaction act;
308 sigemptyset(&act.sa_mask);
309 act.sa_handler = SIG_DFL;
310 sigaction(SIGBUS, &act, NULL);
311 sigaction(SIGSEGV, &act, NULL);
312 sigaction(SIGILL, &act, NULL);
313 sigaction(SIGFPE, &act, NULL);
315 signal(SIGBUS, SIG_DFL);
316 signal(SIGSEGV, SIG_DFL);
317 signal(SIGILL, SIG_DFL);
318 signal(SIGFPE, SIG_DFL);
319 #endif /* POSIXSIGNALS */
321 logerror("server received fatal signal %d", sig);
333 #if defined(__linux__) && defined(_EMPTH_POSIX)
334 /* This is a hack to get around the way pthreads work on Linux. This
335 may be useful on other platforms too where threads are turned into
337 if (getpid() != mainpid) {
341 if (me && me->name) {
342 if (strlen(me->name) > 5) {
343 /* Player threads are cleaned up below, so just have
344 them return. This should work. */
345 if (!strncmp("Player", me->name, 6)) {
350 /* Not a player thread - must be server thread, so exit */
356 logerror("Shutdown commencing (cleaning up threads.)");
358 for (p = player_next(0); p != 0; p = player_next(p)) {
359 if (p->state != PS_PLAYING)
361 pr_flash(p, "Server shutting down...\n");
362 p->state = PS_SHUTDOWN;
365 pr_flash(p, "Shutdown aborting command\n");
367 empth_wakeup(p->proc);
371 /* Sleep and let some cleanup happen - note this doesn't work
372 when called from a signal handler, since we may or may not
373 be in the right thread. So we just pass by and kill 'em
376 empth_sleep(now + 1);
379 for (p = player_next(0); p != 0; p = player_next(p)) {
382 empth_terminate(p->proc);
383 p = player_delete(p);
386 logerror("Server shutting down on signal %d", sig);
388 logerror("Server shutting down at Deity's request");
395 nullify_objects(void)
400 dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
401 dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
402 dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
403 dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
404 dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
405 dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
407 for (i = 0; i < pln_maxno; i++) {
413 plchr[i].pl_fuel = 0;
415 plchr[i].pl_name = plchr[i].pl_planename;
417 for (i = 0; i < lnd_maxno; i++) {
422 /* Fix up the military values */
423 for (j = 0; j < lchr[i].l_nv; j++) {
424 if (lchr[i].l_vtype[j] == V_MILIT)
425 lchr[i].l_mil = lchr[i].l_vamt[j];
428 for (i = 0; i < shp_maxno; i++) {
434 if (mchr[i].m_flags & M_OIL)
438 for (i = 0; i < nuk_maxno; i++) {
444 for (i = 0; i < sct_maxno; i++) {
450 for (i = 0; i < prd_maxno; i++) {
451 for (j = 0; j < pchr[i].p_nv; j++) {
452 if (opt_NO_HCMS && pchr[i].p_vtype[j] == V_HCM)
453 pchr[i].p_vamt[j] = 0;
454 if (opt_NO_LCMS && pchr[i].p_vtype[j] == V_LCM)
455 pchr[i].p_vamt[j] = 0;
456 if (opt_NO_OIL && pchr[i].p_vtype[j] == V_OIL)
457 pchr[i].p_vamt[j] = 0;
467 WORD wVersionRequested;
470 wVersionRequested = MAKEWORD(2, 0);
471 rc = WSAStartup(wVersionRequested, &wsaData);
473 logerror("WSAStartup failed. %d", rc);