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"
68 #include "prototypes.h"
70 static void nullify_objects(void);
71 static void init_files(void);
74 static void loc_NTInit(void);
77 static int mainpid = 0;
81 * If yes, don't fork into background, don't catch certain signals,
82 * call abort() on internal error.
87 print_usage(char *program_name)
90 printf("Usage: %s -i -I service_name -r -R service_name -D datadir -e config_file -d\n", program_name);
91 printf("-i install service with the default name %s\n", DEFAULT_SERVICE_NAME);
92 printf("-r remove service with the default name %s\n", DEFAULT_SERVICE_NAME);
94 printf("Usage: %s -D datadir -e config_file -d -p -s\n", program_name);
95 printf("-p print flag\n");
96 printf("-s stack check flag (include print flag)\n");
98 printf("-d debug mode\n");
102 main(int argc, char **argv)
106 int install_service_set = 0;
107 char *service_name = NULL;
108 int remove_service_set = 0;
111 char *config_file = NULL;
119 while ((op = getopt(argc, argv, "D:de:iI:rR:h")) != EOF) {
121 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
134 config_file = optarg;
138 service_name = optarg;
143 install_service_set++;
147 service_name = optarg;
152 remove_service_set++;
156 flags |= EMPTH_PRINT;
159 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
164 print_usage(argv[0]);
172 if ((debug || datadir_set || config_file != NULL) &&
173 remove_service_set) {
174 logerror("Can't use -d, -D or -e with either "
175 "-r or -R options when starting the server");
176 printf("Can't use -d, -D or -e with either -r "
180 if (debug && install_service_set) {
181 logerror("Can't use -d with either "
182 "-i or -I options when starting the server");
183 printf("Can't use -d with either -i "
187 if (install_service_set && remove_service_set) {
188 logerror("Can't use both -r or -R and -i or -I options when starting "
190 printf("Can't use both -r or -R and -i or -I options\n");
193 if (install_service_set)
194 return install_service(argv[0], service_name, datadir_set);
195 if (remove_service_set)
196 return remove_service(service_name);
199 if (config_file == NULL) {
200 sprintf(tbuf, "%s/econfig", datadir);
204 logerror("------------------------------------------------------");
205 logerror("Empire server (pid %d) started", (int)getpid());
209 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
210 if (StartServiceCtrlDispatcher(DispatchTable))
213 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
214 logerror("Failed to dispatch service (%d)", GetLastError());
215 printf("Failed to dispatch service (%d)\n", GetLastError());
217 } else /* start in the foreground */
221 if (debug == 0 && flags == 0) {
226 start_server(flags, config_file);
228 #if defined(__linux__) && defined(_EMPTH_POSIX)
229 strcpy(tbuf, argv[0]);
230 for (op = 1; op < argc; op++) {
232 strcat(tbuf, argv[op]);
234 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
239 /* We should never get here. But, just in case... */
250 start_server(int flags, char *config_file)
253 struct sigaction act;
254 #endif /* POSIXSIGNALS */
259 emp_config(config_file);
260 update_policy_check();
265 /* signal() should not be used with mit pthreads. Anyway if u
266 have a posix threads u definitly have posix signals -- Sasha */
267 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
269 act.sa_flags = SA_SIGINFO;
271 sigemptyset(&act.sa_mask);
272 act.sa_handler = shutdwn;
273 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
274 a Linux box running POSIX threads -- STM */
275 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
276 sigaction(SIGUSR1, &act, NULL);
278 sigaction(SIGTERM, &act, NULL);
279 sigaction(SIGINT, &act, NULL);
280 act.sa_handler = panic;
281 sigaction(SIGBUS, &act, NULL);
282 sigaction(SIGSEGV, &act, NULL);
283 sigaction(SIGILL, &act, NULL);
284 sigaction(SIGFPE, &act, NULL);
285 act.sa_handler = SIG_IGN;
286 sigaction(SIGPIPE, &act, NULL);
288 if (debug == 0 && flags == 0) {
289 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
290 a Linux box running POSIX threads -- STM */
291 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
292 signal(SIGUSR1, shutdwn);
294 signal(SIGTERM, shutdwn);
295 signal(SIGBUS, panic);
296 signal(SIGSEGV, panic);
297 signal(SIGILL, panic);
298 signal(SIGFPE, panic);
299 signal(SIGINT, shutdwn);
301 signal(SIGPIPE, SIG_IGN);
302 #endif /* POSIXSIGNALS */
304 empth_init((char **)&player, flags);
314 if (opt_MOB_ACCESS) {
315 /* This fixes up mobility upon restart */
319 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
320 "AcceptPlayers", "Accept network connections", 0);
321 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
322 "KillIdle", "Kills idle player connections", 0);
323 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
324 "Schedules updates to occur", 0);
325 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
326 "DeleteItems", "Deletes old lost items", 0);
327 if (opt_MOB_ACCESS) {
328 /* Start the mobility access check thread */
329 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
330 "MobilityCheck", "Writes the timestamp file", 0);
334 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
335 "MarketUpdate", "Updates the market", 0);
342 ef_open(EF_NATION, O_RDWR, EFF_MEM);
343 ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
344 ef_open(EF_SHIP, O_RDWR, EFF_MEM);
345 ef_open(EF_PLANE, O_RDWR, EFF_MEM);
346 ef_open(EF_LAND, O_RDWR, EFF_MEM);
347 ef_open(EF_NEWS, O_RDWR, 0);
348 ef_open(EF_LOAN, O_RDWR, 0);
349 ef_open(EF_TREATY, O_RDWR, 0);
350 ef_open(EF_NUKE, O_RDWR, EFF_MEM);
351 ef_open(EF_POWER, O_RDWR, 0);
352 ef_open(EF_TRADE, O_RDWR, 0);
353 ef_open(EF_MAP, O_RDWR, EFF_MEM);
354 ef_open(EF_BMAP, O_RDWR, EFF_MEM);
355 ef_open(EF_COMM, O_RDWR, 0);
356 ef_open(EF_LOST, O_RDWR, 0);
379 /* we're going down. try to close the files at least */
385 struct sigaction act;
388 sigemptyset(&act.sa_mask);
389 act.sa_handler = SIG_DFL;
390 sigaction(SIGBUS, &act, NULL);
391 sigaction(SIGSEGV, &act, NULL);
392 sigaction(SIGILL, &act, NULL);
393 sigaction(SIGFPE, &act, NULL);
395 signal(SIGBUS, SIG_DFL);
396 signal(SIGSEGV, SIG_DFL);
397 signal(SIGILL, SIG_DFL);
398 signal(SIGFPE, SIG_DFL);
399 #endif /* POSIXSIGNALS */
400 logerror("server received fatal signal %d", sig);
403 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
404 && sig != SIGILL && sig != SIGFPE))
417 #if defined(__linux__) && defined(_EMPTH_POSIX)
418 /* This is a hack to get around the way pthreads work on Linux. This
419 may be useful on other platforms too where threads are turned into
421 if (getpid() != mainpid) {
425 if (me && me->name) {
426 if (strlen(me->name) > 5) {
427 /* Player threads are cleaned up below, so just have
428 them return. This should work. */
429 if (!strncmp("Player", me->name, 6)) {
434 /* Not a player thread - must be server thread, so exit */
440 logerror("Shutdown commencing (cleaning up threads.)");
442 for (p = player_next(0); p != 0; p = player_next(p)) {
443 if (p->state != PS_PLAYING)
445 pr_flash(p, "Server shutting down...\n");
446 p->state = PS_SHUTDOWN;
449 pr_flash(p, "Shutdown aborting command\n");
451 empth_wakeup(p->proc);
455 /* Sleep and let some cleanup happen - note this doesn't work
456 when called from a signal handler, since we may or may not
457 be in the right thread. So we just pass by and kill 'em
460 empth_sleep(now + 1);
463 for (p = player_next(0); p != 0; p = player_next(p)) {
466 empth_terminate(p->proc);
467 p = player_delete(p);
470 logerror("Server shutting down on signal %d", sig);
472 logerror("Server shutting down at Deity's request");
479 nullify_objects(void)
484 dchr[SCT_CAPIT] = bigcity_dchr;
486 dchr[SCT_LIGHT].d_cost = -1;
488 dchr[SCT_HEAVY].d_cost = -1;
490 dchr[SCT_OIL].d_cost = -1;
491 dchr[SCT_REFINE].d_cost = -1;
493 for (i = 0; i < pln_maxno; i++) {
499 plchr[i].pl_fuel = 0;
501 for (i = 0; i < lnd_maxno; i++) {
506 /* Fix up the military values */
507 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
509 for (i = 0; i < shp_maxno; i++) {
515 if (mchr[i].m_flags & M_OIL)
519 for (i = 0; i < nuk_maxno; i++) {
525 for (i = 0; i <= SCT_MAXDEF; i++) {
531 for (i = 0; i < prd_maxno; i++) {
532 for (j = 0; j < MAXPRCON; j++) {
533 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
534 pchr[i].p_camt[j] = 0;
535 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
536 pchr[i].p_camt[j] = 0;
537 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
538 pchr[i].p_camt[j] = 0;
548 WORD wVersionRequested;
551 wVersionRequested = MAKEWORD(2, 0);
552 rc = WSAStartup(wVersionRequested, &wsaData);
554 logerror("WSAStartup failed. %d", rc);