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;
120 while ((op = getopt(argc, argv, "D:de:irh")) != EOF) {
122 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
135 config_file = optarg;
139 install_service_set++;
142 remove_service_set++;
146 flags |= EMPTH_PRINT;
149 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
154 print_usage(argv[0]);
160 if ((debug || datadir_set || config_file != NULL) &&
161 (install_service_set || remove_service_set)) {
162 logerror("Can't use -d or -D or -e with either "
163 "-r or -i options when starting the server");
164 printf("Can't use -d or -D or -e with either -r "
168 if (install_service_set && remove_service_set) {
169 logerror("Can't use both -r and -i options when starting "
171 printf("Can't use both -r and -i options\n");
174 if (install_service_set)
175 return install_service(argv[0]);
176 if (remove_service_set)
177 return remove_service();
180 if (config_file == NULL) {
181 sprintf(tbuf, "%s/econfig", datadir);
185 logerror("------------------------------------------------------");
186 logerror("Empire server (pid %d) started", (int)getpid());
190 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
191 if (StartServiceCtrlDispatcher(DispatchTable))
194 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
195 logerror("Failed to dispatch service (%d)", GetLastError());
196 printf("Failed to dispatch service (%d)\n", GetLastError());
198 } else /* start in the foreground */
202 if (debug == 0 && flags == 0) {
207 start_server(flags, config_file);
209 #if defined(__linux__) && defined(_EMPTH_POSIX)
210 strcpy(tbuf, argv[0]);
211 for (op = 1; op < argc; op++) {
213 strcat(tbuf, argv[op]);
215 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
220 /* We should never get here. But, just in case... */
231 start_server(int flags, char *config_file)
234 struct sigaction act;
235 #endif /* POSIXSIGNALS */
240 emp_config(config_file);
241 update_policy_check();
246 /* signal() should not be used with mit pthreads. Anyway if u
247 have a posix threads u definitly have posix signals -- Sasha */
248 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
250 act.sa_flags = SA_SIGINFO;
252 sigemptyset(&act.sa_mask);
253 act.sa_handler = shutdwn;
254 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
255 a Linux box running POSIX threads -- STM */
256 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
257 sigaction(SIGUSR1, &act, NULL);
259 sigaction(SIGTERM, &act, NULL);
260 sigaction(SIGINT, &act, NULL);
261 act.sa_handler = panic;
262 sigaction(SIGBUS, &act, NULL);
263 sigaction(SIGSEGV, &act, NULL);
264 sigaction(SIGILL, &act, NULL);
265 sigaction(SIGFPE, &act, NULL);
266 act.sa_handler = SIG_IGN;
267 sigaction(SIGPIPE, &act, NULL);
269 if (debug == 0 && flags == 0) {
270 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
271 a Linux box running POSIX threads -- STM */
272 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
273 signal(SIGUSR1, shutdwn);
275 signal(SIGTERM, shutdwn);
276 signal(SIGBUS, panic);
277 signal(SIGSEGV, panic);
278 signal(SIGILL, panic);
279 signal(SIGFPE, panic);
280 signal(SIGINT, shutdwn);
282 signal(SIGPIPE, SIG_IGN);
283 #endif /* POSIXSIGNALS */
285 empth_init((char **)&player, flags);
294 if (opt_MOB_ACCESS) {
295 /* This fixes up mobility upon restart */
299 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
300 "AcceptPlayers", "Accept network connections", 0);
301 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
302 "KillIdle", "Kills idle player connections", 0);
303 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
304 "Schedules updates to occur", 0);
305 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
306 "DeleteItems", "Deletes old lost items", 0);
307 if (opt_MOB_ACCESS) {
308 /* Start the mobility access check thread */
309 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
310 "MobilityCheck", "Writes the timestamp file", 0);
314 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
315 "MarketUpdate", "Updates the market", 0);
322 ef_open(EF_NATION, O_RDWR, EFF_MEM);
323 ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
324 ef_open(EF_SHIP, O_RDWR, EFF_MEM);
325 ef_open(EF_PLANE, O_RDWR, EFF_MEM);
326 ef_open(EF_LAND, O_RDWR, EFF_MEM);
327 ef_open(EF_NEWS, O_RDWR, 0);
328 ef_open(EF_LOAN, O_RDWR, 0);
329 ef_open(EF_TREATY, O_RDWR, 0);
330 ef_open(EF_NUKE, O_RDWR, EFF_MEM);
331 ef_open(EF_POWER, O_RDWR, 0);
332 ef_open(EF_TRADE, O_RDWR, 0);
333 ef_open(EF_MAP, O_RDWR, EFF_MEM);
334 ef_open(EF_BMAP, O_RDWR, EFF_MEM);
335 ef_open(EF_COMM, O_RDWR, 0);
336 ef_open(EF_LOST, O_RDWR, 0);
359 /* we're going down. try to close the files at least */
365 struct sigaction act;
368 sigemptyset(&act.sa_mask);
369 act.sa_handler = SIG_DFL;
370 sigaction(SIGBUS, &act, NULL);
371 sigaction(SIGSEGV, &act, NULL);
372 sigaction(SIGILL, &act, NULL);
373 sigaction(SIGFPE, &act, NULL);
375 signal(SIGBUS, SIG_DFL);
376 signal(SIGSEGV, SIG_DFL);
377 signal(SIGILL, SIG_DFL);
378 signal(SIGFPE, SIG_DFL);
379 #endif /* POSIXSIGNALS */
380 logerror("server received fatal signal %d", sig);
383 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
384 && sig != SIGILL && sig != SIGFPE))
397 #if defined(__linux__) && defined(_EMPTH_POSIX)
398 /* This is a hack to get around the way pthreads work on Linux. This
399 may be useful on other platforms too where threads are turned into
401 if (getpid() != mainpid) {
405 if (me && me->name) {
406 if (strlen(me->name) > 5) {
407 /* Player threads are cleaned up below, so just have
408 them return. This should work. */
409 if (!strncmp("Player", me->name, 6)) {
414 /* Not a player thread - must be server thread, so exit */
420 logerror("Shutdown commencing (cleaning up threads.)");
422 for (p = player_next(0); p != 0; p = player_next(p)) {
423 if (p->state != PS_PLAYING)
425 pr_flash(p, "Server shutting down...\n");
426 p->state = PS_SHUTDOWN;
429 pr_flash(p, "Shutdown aborting command\n");
431 empth_wakeup(p->proc);
435 /* Sleep and let some cleanup happen - note this doesn't work
436 when called from a signal handler, since we may or may not
437 be in the right thread. So we just pass by and kill 'em
440 empth_sleep(now + 1);
443 for (p = player_next(0); p != 0; p = player_next(p)) {
446 empth_terminate(p->proc);
447 p = player_delete(p);
450 logerror("Server shutting down on signal %d", sig);
452 logerror("Server shutting down at Deity's request");
459 nullify_objects(void)
464 dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
465 dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
466 dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
467 dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
468 dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
469 dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
472 dchr[SCT_LIGHT].d_cost = -1;
474 dchr[SCT_HEAVY].d_cost = -1;
476 dchr[SCT_OIL].d_cost = -1;
477 dchr[SCT_REFINE].d_cost = -1;
479 for (i = 0; i < pln_maxno; i++) {
485 plchr[i].pl_fuel = 0;
487 for (i = 0; i < lnd_maxno; i++) {
492 /* Fix up the military values */
493 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
495 for (i = 0; i < shp_maxno; i++) {
501 if (mchr[i].m_flags & M_OIL)
505 for (i = 0; i < nuk_maxno; i++) {
511 for (i = 0; i <= SCT_MAXDEF; i++) {
517 for (i = 0; i < prd_maxno; i++) {
518 for (j = 0; j < MAXPRCON; j++) {
519 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
520 pchr[i].p_camt[j] = 0;
521 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
522 pchr[i].p_camt[j] = 0;
523 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
524 pchr[i].p_camt[j] = 0;
534 WORD wVersionRequested;
537 wVersionRequested = MAKEWORD(2, 0);
538 rc = WSAStartup(wVersionRequested, &wsaData);
540 logerror("WSAStartup failed. %d", rc);