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 */
42 #include <sys/ioctl.h>
51 #include "../lib/gen/getopt.h"
59 #include "empthread.h"
69 #include "prototypes.h"
71 static void nullify_objects(void);
72 static void init_files(void);
75 static void loc_NTInit(void);
78 static int mainpid = 0;
82 * If yes, don't fork into background, don't catch certain signals,
83 * call abort() on internal error.
88 print_usage(char *program_name)
91 printf("Usage: %s -i -r -D datadir -e config_file -d\n", program_name);
92 printf("-i install service\n");
93 printf("-r remove service\n");
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 int install_service_set = 0;
108 int remove_service_set = 0;
112 char *config_file = NULL;
124 while ((op = getopt(argc, argv, "D:de:irh")) != EOF) {
126 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
139 config_file = optarg;
143 install_service_set++;
146 remove_service_set++;
150 flags |= EMPTH_PRINT;
153 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
158 print_usage(argv[0]);
164 if ((debug || datadir_set || config_file != NULL) &&
165 (install_service_set || remove_service_set)) {
166 logerror("Can't use -d or -D or -e with either "
167 "-r or -i options when starting the server");
168 printf("Can't use -d or -D or -e with either -r "
172 if (install_service_set && remove_service_set) {
173 logerror("Can't use both -r and -i options when starting "
175 printf("Can't use both -r and -i options\n");
178 if (install_service_set)
179 return install_service(argv[0]);
180 if (remove_service_set)
181 return remove_service();
184 if (config_file == NULL) {
185 sprintf(tbuf, "%s/econfig", datadir);
189 logerror("------------------------------------------------------");
190 logerror("Empire server (pid %d) started", (int)getpid());
194 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
195 if (StartServiceCtrlDispatcher(DispatchTable))
198 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
199 logerror("Failed to dispatch service (%d)", GetLastError());
200 printf("Failed to dispatch service (%d)\n", GetLastError());
202 } else /* start in the foreground */
206 if (debug == 0 && flags == 0) {
211 start_server(flags, config_file);
213 #if defined(__linux__) && defined(_EMPTH_POSIX)
214 strcpy(tbuf, argv[0]);
215 for (op = 1; op < argc; op++) {
217 strcat(tbuf, argv[op]);
219 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
224 /* We should never get here. But, just in case... */
235 start_server(int flags, char *config_file)
238 struct sigaction act;
239 #endif /* POSIXSIGNALS */
244 emp_config(config_file);
245 update_policy_check();
250 /* signal() should not be used with mit pthreads. Anyway if u
251 have a posix threads u definitly have posix signals -- Sasha */
252 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
254 act.sa_flags = SA_SIGINFO;
256 sigemptyset(&act.sa_mask);
257 act.sa_handler = shutdwn;
258 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
259 a Linux box running POSIX threads -- STM */
260 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
261 sigaction(SIGUSR1, &act, NULL);
263 sigaction(SIGTERM, &act, NULL);
264 sigaction(SIGINT, &act, NULL);
265 act.sa_handler = panic;
266 sigaction(SIGBUS, &act, NULL);
267 sigaction(SIGSEGV, &act, NULL);
268 sigaction(SIGILL, &act, NULL);
269 sigaction(SIGFPE, &act, NULL);
270 act.sa_handler = SIG_IGN;
271 sigaction(SIGPIPE, &act, NULL);
273 if (debug == 0 && flags == 0) {
274 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
275 a Linux box running POSIX threads -- STM */
276 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
277 signal(SIGUSR1, shutdwn);
279 signal(SIGTERM, shutdwn);
280 signal(SIGBUS, panic);
281 signal(SIGSEGV, panic);
282 signal(SIGILL, panic);
283 signal(SIGFPE, panic);
284 signal(SIGINT, shutdwn);
286 signal(SIGPIPE, SIG_IGN);
287 #endif /* POSIXSIGNALS */
289 empth_init((char **)&player, flags);
298 if (opt_MOB_ACCESS) {
299 /* This fixes up mobility upon restart */
303 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
304 "AcceptPlayers", "Accept network connections", 0);
305 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
306 "KillIdle", "Kills idle player connections", 0);
307 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
308 "Schedules updates to occur", 0);
309 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
310 "DeleteItems", "Deletes old lost items", 0);
311 if (opt_MOB_ACCESS) {
312 /* Start the mobility access check thread */
313 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
314 "MobilityCheck", "Writes the timestamp file", 0);
318 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
319 "MarketUpdate", "Updates the market", 0);
326 ef_open(EF_NATION, O_RDWR, EFF_MEM);
327 ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
328 ef_open(EF_SHIP, O_RDWR, EFF_MEM);
329 ef_open(EF_PLANE, O_RDWR, EFF_MEM);
330 ef_open(EF_LAND, O_RDWR, EFF_MEM);
331 ef_open(EF_NEWS, O_RDWR, 0);
332 ef_open(EF_LOAN, O_RDWR, 0);
333 ef_open(EF_TREATY, O_RDWR, 0);
334 ef_open(EF_NUKE, O_RDWR, EFF_MEM);
335 ef_open(EF_POWER, O_RDWR, 0);
336 ef_open(EF_TRADE, O_RDWR, 0);
337 ef_open(EF_MAP, O_RDWR, EFF_MEM);
338 ef_open(EF_BMAP, O_RDWR, EFF_MEM);
339 ef_open(EF_COMM, O_RDWR, 0);
340 ef_open(EF_LOST, O_RDWR, 0);
363 /* we're going down. try to close the files at least */
369 struct sigaction act;
372 sigemptyset(&act.sa_mask);
373 act.sa_handler = SIG_DFL;
374 sigaction(SIGBUS, &act, NULL);
375 sigaction(SIGSEGV, &act, NULL);
376 sigaction(SIGILL, &act, NULL);
377 sigaction(SIGFPE, &act, NULL);
379 signal(SIGBUS, SIG_DFL);
380 signal(SIGSEGV, SIG_DFL);
381 signal(SIGILL, SIG_DFL);
382 signal(SIGFPE, SIG_DFL);
383 #endif /* POSIXSIGNALS */
384 logerror("server received fatal signal %d", sig);
387 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
388 && sig != SIGILL && sig != SIGFPE))
401 #if defined(__linux__) && defined(_EMPTH_POSIX)
402 /* This is a hack to get around the way pthreads work on Linux. This
403 may be useful on other platforms too where threads are turned into
405 if (getpid() != mainpid) {
409 if (me && me->name) {
410 if (strlen(me->name) > 5) {
411 /* Player threads are cleaned up below, so just have
412 them return. This should work. */
413 if (!strncmp("Player", me->name, 6)) {
418 /* Not a player thread - must be server thread, so exit */
424 logerror("Shutdown commencing (cleaning up threads.)");
426 for (p = player_next(0); p != 0; p = player_next(p)) {
427 if (p->state != PS_PLAYING)
429 pr_flash(p, "Server shutting down...\n");
430 p->state = PS_SHUTDOWN;
433 pr_flash(p, "Shutdown aborting command\n");
435 empth_wakeup(p->proc);
439 /* Sleep and let some cleanup happen - note this doesn't work
440 when called from a signal handler, since we may or may not
441 be in the right thread. So we just pass by and kill 'em
444 empth_sleep(now + 1);
447 for (p = player_next(0); p != 0; p = player_next(p)) {
450 empth_terminate(p->proc);
451 p = player_delete(p);
454 logerror("Server shutting down on signal %d", sig);
456 logerror("Server shutting down at Deity's request");
463 nullify_objects(void)
468 dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
469 dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
470 dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
471 dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
472 dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
473 dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
476 dchr[SCT_LIGHT].d_cost = -1;
478 dchr[SCT_HEAVY].d_cost = -1;
480 dchr[SCT_OIL].d_cost = -1;
481 dchr[SCT_REFINE].d_cost = -1;
483 for (i = 0; i < pln_maxno; i++) {
489 plchr[i].pl_fuel = 0;
491 for (i = 0; i < lnd_maxno; i++) {
496 /* Fix up the military values */
497 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
499 for (i = 0; i < shp_maxno; i++) {
505 if (mchr[i].m_flags & M_OIL)
509 for (i = 0; i < nuk_maxno; i++) {
515 for (i = 0; i <= SCT_MAXDEF; i++) {
521 for (i = 0; i < prd_maxno; i++) {
522 for (j = 0; j < MAXPRCON; j++) {
523 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
524 pchr[i].p_camt[j] = 0;
525 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
526 pchr[i].p_camt[j] = 0;
527 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
528 pchr[i].p_camt[j] = 0;
538 WORD wVersionRequested;
541 wVersionRequested = MAKEWORD(2, 0);
542 rc = WSAStartup(wVersionRequested, &wsaData);
544 logerror("WSAStartup failed. %d", rc);