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.
88 print_usage(char *program_name)
91 printf("Usage: %s -i -I service_name -r -R service_name -D datadir -e config_file -d\n", program_name);
92 printf("-i install service with the default name %s\n", DEFAULT_SERVICE_NAME);
93 printf("-r remove service with the default name %s\n", DEFAULT_SERVICE_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 int install_service_set = 0;
108 char *service_name = NULL;
109 int remove_service_set = 0;
112 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) {
135 config_file = optarg;
139 service_name = optarg;
144 install_service_set++;
148 service_name = optarg;
153 remove_service_set++;
157 flags |= EMPTH_PRINT;
160 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
165 print_usage(argv[0]);
171 if ((debug || datadir_set || config_file != NULL) &&
172 remove_service_set) {
173 logerror("Can't use -d, -D or -e with either "
174 "-r or -R options when starting the server");
177 if (debug && install_service_set) {
178 logerror("Can't use -d with either "
179 "-i or -I options when starting the server");
182 if (install_service_set && remove_service_set) {
183 logerror("Can't use both -r or -R and -i or -I options when starting "
191 if (remove_service_set)
192 return remove_service(service_name);
195 if (config_file == NULL) {
196 sprintf(tbuf, "%s/econfig", datadir);
199 emp_config(config_file);
202 if (install_service_set)
203 return install_service(argv[0], service_name, datadir_set, config_file);
208 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
209 if (StartServiceCtrlDispatcher(DispatchTable))
213 * If it is service startup error then exit otherwise
214 * start server in the foreground
216 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
217 logerror("Failed to dispatch service (%d)", GetLastError());
218 printf("Failed to dispatch service (%d)\n", GetLastError());
228 if (daemon != 0 && flags == 0)
233 #if defined(__linux__) && defined(_EMPTH_POSIX)
234 strcpy(tbuf, argv[0]);
235 for (op = 1; op < argc; op++) {
237 strcat(tbuf, argv[op]);
239 sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
244 /* We should never get here. But, just in case... */
255 init_server(int flags)
258 struct sigaction act;
259 #endif /* POSIXSIGNALS */
264 update_policy_check();
269 /* signal() should not be used with mit pthreads. Anyway if u
270 have a posix threads u definitly have posix signals -- Sasha */
271 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
273 act.sa_flags = SA_SIGINFO;
275 sigemptyset(&act.sa_mask);
276 act.sa_handler = shutdwn;
277 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
278 a Linux box running POSIX threads -- STM */
279 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
280 sigaction(SIGUSR1, &act, NULL);
282 sigaction(SIGTERM, &act, NULL);
283 sigaction(SIGINT, &act, NULL);
284 act.sa_handler = panic;
285 sigaction(SIGBUS, &act, NULL);
286 sigaction(SIGSEGV, &act, NULL);
287 sigaction(SIGILL, &act, NULL);
288 sigaction(SIGFPE, &act, NULL);
289 act.sa_handler = SIG_IGN;
290 sigaction(SIGPIPE, &act, NULL);
292 if (debug == 0 && flags == 0) {
293 /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
294 a Linux box running POSIX threads -- STM */
295 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
296 signal(SIGUSR1, shutdwn);
298 signal(SIGTERM, shutdwn);
299 signal(SIGBUS, panic);
300 signal(SIGSEGV, panic);
301 signal(SIGILL, panic);
302 signal(SIGFPE, panic);
303 signal(SIGINT, shutdwn);
305 signal(SIGPIPE, SIG_IGN);
306 #endif /* POSIXSIGNALS */
308 empth_init((char **)&player, flags);
318 if (opt_MOB_ACCESS) {
319 /* This fixes up mobility upon restart */
324 logerror("------------------------------------------------------");
325 logerror("Empire server (pid %d) started", (int)getpid());
329 start_server(int flags)
331 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
332 "AcceptPlayers", "Accept network connections", 0);
333 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
334 "KillIdle", "Kills idle player connections", 0);
335 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
336 "Schedules updates to occur", 0);
337 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
338 "DeleteItems", "Deletes old lost items", 0);
339 if (opt_MOB_ACCESS) {
340 /* Start the mobility access check thread */
341 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
342 "MobilityCheck", "Writes the timestamp file", 0);
346 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
347 "MarketUpdate", "Updates the market", 0);
355 failed |= !ef_open(EF_NATION, O_RDWR, EFF_MEM);
356 failed |= !ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
357 failed |= !ef_open(EF_SHIP, O_RDWR, EFF_MEM);
358 failed |= !ef_open(EF_PLANE, O_RDWR, EFF_MEM);
359 failed |= !ef_open(EF_LAND, O_RDWR, EFF_MEM);
360 failed |= !ef_open(EF_NEWS, O_RDWR, 0);
361 failed |= !ef_open(EF_LOAN, O_RDWR, 0);
362 failed |= !ef_open(EF_TREATY, O_RDWR, 0);
363 failed |= !ef_open(EF_NUKE, O_RDWR, EFF_MEM);
364 failed |= !ef_open(EF_POWER, O_RDWR, 0);
365 failed |= !ef_open(EF_TRADE, O_RDWR, 0);
366 failed |= !ef_open(EF_MAP, O_RDWR, EFF_MEM);
367 failed |= !ef_open(EF_BMAP, O_RDWR, EFF_MEM);
368 failed |= !ef_open(EF_COMM, O_RDWR, 0);
369 failed |= !ef_open(EF_LOST, O_RDWR, 0);
371 logerror("Missing files, giving up");
396 /* we're going down. try to close the files at least */
402 struct sigaction act;
405 sigemptyset(&act.sa_mask);
406 act.sa_handler = SIG_DFL;
407 sigaction(SIGBUS, &act, NULL);
408 sigaction(SIGSEGV, &act, NULL);
409 sigaction(SIGILL, &act, NULL);
410 sigaction(SIGFPE, &act, NULL);
412 signal(SIGBUS, SIG_DFL);
413 signal(SIGSEGV, SIG_DFL);
414 signal(SIGILL, SIG_DFL);
415 signal(SIGFPE, SIG_DFL);
416 #endif /* POSIXSIGNALS */
417 logerror("server received fatal signal %d", sig);
420 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
421 && sig != SIGILL && sig != SIGFPE))
434 #if defined(__linux__) && defined(_EMPTH_POSIX)
435 /* This is a hack to get around the way pthreads work on Linux. This
436 may be useful on other platforms too where threads are turned into
438 if (getpid() != mainpid) {
442 if (me && me->name) {
443 if (strlen(me->name) > 5) {
444 /* Player threads are cleaned up below, so just have
445 them return. This should work. */
446 if (!strncmp("Player", me->name, 6)) {
451 /* Not a player thread - must be server thread, so exit */
457 logerror("Shutdown commencing (cleaning up threads.)");
459 for (p = player_next(0); p != 0; p = player_next(p)) {
460 if (p->state != PS_PLAYING)
462 pr_flash(p, "Server shutting down...\n");
463 p->state = PS_SHUTDOWN;
466 pr_flash(p, "Shutdown aborting command\n");
468 empth_wakeup(p->proc);
472 /* Sleep and let some cleanup happen - note this doesn't work
473 when called from a signal handler, since we may or may not
474 be in the right thread. So we just pass by and kill 'em
477 empth_sleep(now + 1);
480 for (p = player_next(0); p != 0; p = player_next(p)) {
483 empth_terminate(p->proc);
484 p = player_delete(p);
487 logerror("Server shutting down on signal %d", sig);
489 logerror("Server shutting down at Deity's request");
496 nullify_objects(void)
501 dchr[SCT_CAPIT] = bigcity_dchr;
503 dchr[SCT_LIGHT].d_cost = -1;
505 dchr[SCT_HEAVY].d_cost = -1;
507 dchr[SCT_OIL].d_cost = -1;
508 dchr[SCT_REFINE].d_cost = -1;
510 for (i = 0; i < pln_maxno; i++) {
516 plchr[i].pl_fuel = 0;
518 for (i = 0; i < lnd_maxno; i++) {
523 /* Fix up the military values */
524 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
526 for (i = 0; i < shp_maxno; i++) {
532 if (mchr[i].m_flags & M_OIL)
536 for (i = 0; i < nuk_maxno; i++) {
542 for (i = 0; i <= SCT_MAXDEF; i++) {
548 for (i = 0; i < prd_maxno; i++) {
549 for (j = 0; j < MAXPRCON; j++) {
550 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
551 pchr[i].p_camt[j] = 0;
552 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
553 pchr[i].p_camt[j] = 0;
554 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
555 pchr[i].p_camt[j] = 0;
565 WORD wVersionRequested;
568 wVersionRequested = MAKEWORD(2, 0);
569 rc = WSAStartup(wVersionRequested, &wsaData);
571 logerror("WSAStartup failed. %d", rc);