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
38 #include <sys/ioctl.h>
49 #include "../lib/gen/getopt.h"
58 #include "empthread.h"
67 #include "prototypes.h"
69 static void nullify_objects(void);
70 static void init_files(void);
71 static void close_files(void);
72 static void create_pidfile(char *, pid_t);
75 static void loc_NTInit(void);
76 static void loc_NTTerm(void);
79 static char pidfname[] = "server.pid";
81 /* Debugging? If yes call abort() on internal error. */
83 /* Run as daemon? If yes, detach from controlling terminal etc. */
87 print_usage(char *program_name)
90 printf("Usage: %s -i -I service_name -r -R service_name -D datadir -e config_file -d -p\n",
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);
96 printf("Usage: %s -D datadir -e config_file -d -p -s\n", program_name);
97 printf("-s stack check flag (include print flag)\n");
99 printf("-p print flag\n");
100 printf("-d debug mode\n");
104 main(int argc, char **argv)
108 int install_service_set = 0;
109 char *service_name = NULL;
110 int remove_service_set = 0;
113 char *config_file = NULL;
117 while ((op = getopt(argc, argv, "D:de:iI:rR:hp")) != EOF) {
119 while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
133 config_file = optarg;
136 flags |= EMPTH_PRINT;
141 service_name = optarg;
146 install_service_set++;
150 service_name = optarg;
155 remove_service_set++;
159 flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
165 print_usage(argv[0]);
171 if ((debug || datadir_set || config_file != NULL) &&
172 remove_service_set) {
173 fprintf(stderr, "Can't use -d, -D or -e with either "
174 "-r or -R options when starting the server\n");
177 if (debug && install_service_set) {
178 fprintf(stderr, "Can't use -d with either "
179 "-i or -I options when starting the server\n");
182 if (install_service_set && remove_service_set) {
183 fprintf(stderr, "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 (emp_config(config_file) < 0)
197 if (chdir(datadir)) {
198 fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno));
203 if (install_service_set)
204 return install_service(argv[0], service_name, datadir_set, config_file);
210 if (daemonize != 0) {
211 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
212 if (StartServiceCtrlDispatcher(DispatchTable))
216 * If it is service startup error then exit otherwise
217 * start server in the foreground
219 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
220 logerror("Failed to dispatch service (%d)", GetLastError());
235 CANT_HAPPEN("main thread terminated");
242 * Initialize for serving, acquire resources.
251 update_policy_check();
261 if (opt_MOB_ACCESS) {
262 /* This fixes up mobility upon restart */
273 start_server(int flags)
277 struct sigaction act;
281 create_pidfile(pidfname, pid);
282 logerror("------------------------------------------------------");
283 logerror("Empire server (pid %d) started", (int)pid);
286 /* signal() should not be used with mit pthreads. Anyway if u
287 have a posix threads u definitly have posix signals -- Sasha */
288 sigemptyset(&act.sa_mask);
289 act.sa_handler = shutdwn;
290 sigaction(SIGTERM, &act, NULL);
291 sigaction(SIGINT, &act, NULL);
292 act.sa_handler = panic;
293 sigaction(SIGBUS, &act, NULL);
294 sigaction(SIGSEGV, &act, NULL);
295 sigaction(SIGILL, &act, NULL);
296 sigaction(SIGFPE, &act, NULL);
297 act.sa_handler = SIG_IGN;
298 sigaction(SIGPIPE, &act, NULL);
301 empth_init((char **)&player, flags);
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);
324 * Finish serving, release resources.
337 create_pidfile(char *fname, pid_t pid)
339 FILE *pidf = fopen(fname, "w");
341 || fprintf(pidf, "%d\n", pid) < 0
343 logerror("Can't write PID file (%s)", strerror(errno));
352 failed |= !ef_open(EF_NATION, O_RDWR, EFF_MEM);
353 failed |= !ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
354 failed |= !ef_open(EF_SHIP, O_RDWR, EFF_MEM);
355 failed |= !ef_open(EF_PLANE, O_RDWR, EFF_MEM);
356 failed |= !ef_open(EF_LAND, O_RDWR, EFF_MEM);
357 failed |= !ef_open(EF_NEWS, O_RDWR, 0);
358 failed |= !ef_open(EF_LOAN, O_RDWR, 0);
359 failed |= !ef_open(EF_TREATY, O_RDWR, 0);
360 failed |= !ef_open(EF_NUKE, O_RDWR, EFF_MEM);
361 failed |= !ef_open(EF_POWER, O_RDWR, 0);
362 failed |= !ef_open(EF_TRADE, O_RDWR, 0);
363 failed |= !ef_open(EF_MAP, O_RDWR, EFF_MEM);
364 failed |= !ef_open(EF_BMAP, O_RDWR, EFF_MEM);
365 failed |= !ef_open(EF_COMM, O_RDWR, 0);
366 failed |= !ef_open(EF_LOST, O_RDWR, 0);
368 logerror("Missing files, giving up");
393 /* we're going down. try to close the files at least */
398 struct sigaction act;
401 sigemptyset(&act.sa_mask);
402 act.sa_handler = SIG_DFL;
403 sigaction(SIGBUS, &act, NULL);
404 sigaction(SIGSEGV, &act, NULL);
405 sigaction(SIGILL, &act, NULL);
406 sigaction(SIGFPE, &act, NULL);
407 logerror("server received fatal signal %d", sig);
410 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
411 && sig != SIGILL && sig != SIGFPE))
424 #if defined(__linux__) && defined(_EMPTH_POSIX)
425 /* This is a hack to get around the way pthreads work on Linux. This
426 may be useful on other platforms too where threads are turned into
428 if (getpid() != mainpid) {
432 if (me && me->name) {
433 if (strlen(me->name) > 5) {
434 /* Player threads are cleaned up below, so just have
435 them return. This should work. */
436 if (!strncmp("Player", me->name, 6)) {
441 /* Not a player thread - must be server thread, so exit */
447 logerror("Shutdown commencing (cleaning up threads.)");
449 for (p = player_next(0); p != 0; p = player_next(p)) {
450 if (p->state != PS_PLAYING)
452 pr_flash(p, "Server shutting down...\n");
453 p->state = PS_SHUTDOWN;
456 pr_flash(p, "Shutdown aborting command\n");
458 empth_wakeup(p->proc);
462 /* Sleep and let some cleanup happen - note this doesn't work
463 when called from a signal handler, since we may or may not
464 be in the right thread. So we just pass by and kill 'em
467 empth_sleep(now + 1);
470 for (p = player_next(0); p != 0; p = player_next(p)) {
473 empth_terminate(p->proc);
474 p = player_delete(p);
477 logerror("Server shutting down on signal %d", sig);
479 logerror("Server shutting down at deity's request");
493 nullify_objects(void)
498 dchr[SCT_CAPIT] = bigcity_dchr;
500 dchr[SCT_LIGHT].d_cost = -1;
502 dchr[SCT_HEAVY].d_cost = -1;
504 dchr[SCT_OIL].d_cost = -1;
505 dchr[SCT_REFINE].d_cost = -1;
507 for (i = 0; i < pln_maxno; i++) {
513 plchr[i].pl_fuel = 0;
515 for (i = 0; i < lnd_maxno; i++) {
520 /* Fix up the military values */
521 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
523 for (i = 0; i < shp_maxno; i++) {
529 if (mchr[i].m_flags & M_OIL)
533 for (i = 0; i < nuk_maxno; i++) {
539 for (i = 0; i <= SCT_MAXDEF; i++) {
545 for (i = 0; i < prd_maxno; i++) {
546 for (j = 0; j < MAXPRCON; j++) {
547 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
548 pchr[i].p_camt[j] = 0;
549 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
550 pchr[i].p_camt[j] = 0;
551 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
552 pchr[i].p_camt[j] = 0;
562 WORD wVersionRequested;
565 wVersionRequested = MAKEWORD(2, 0);
566 rc = WSAStartup(wVersionRequested, &wsaData);
568 logerror("WSAStartup failed. %d", rc);