2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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>
50 #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);
73 static void close_files(void);
74 static void create_pidfile(char *, pid_t);
77 static void loc_NTInit(void);
78 static void loc_NTTerm(void);
81 static char pidfname[] = "server.pid";
83 /* Run as daemon? If yes, detach from controlling terminal etc. */
87 print_usage(char *program_name)
89 printf("Usage: %s [OPTION]...\n"
91 " -e CONFIG-FILE configuration file\n"
93 " -h display this help and exit\n"
95 " -i install service `%s'\n"
96 " -I NAME install service NAME\n"
98 " -p threading debug mode, implies -d\n"
100 " -r remove service `%s'\n"
101 " -R NAME remove service NAME\n"
103 " -s enable stack checking\n"
104 " -v display version information and exit\n",
105 program_name, dflt_econfig
107 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
113 main(int argc, char **argv)
117 int install_service_set = 0;
118 char *program_name = NULL;
119 char *service_name = NULL;
120 int remove_service_set = 0;
122 char *config_file = NULL;
126 # define XOPTS "iI:rR:"
130 while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
133 flags |= EMPTH_PRINT;
140 config_file = optarg;
144 service_name = optarg;
149 install_service_set++;
152 service_name = optarg;
157 remove_service_set++;
161 flags |= EMPTH_STACKCHECK;
164 printf("Wolfpack Empire %d.%d.%d\n",
165 EMP_VERS_MAJOR, EMP_VERS_MINOR, EMP_VERS_PATCH);
168 print_usage(argv[0]);
171 fprintf(stderr, "Try -h for help.\n");
177 if ((debug || flags || config_file != NULL) &&
178 remove_service_set) {
179 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
180 "-r or -R options\n");
183 if ((debug || flags) && install_service_set) {
184 fprintf(stderr, "Can't use -d, -p or -s with either "
185 "-i or -I options\n");
188 if (install_service_set && remove_service_set) {
189 fprintf(stderr, "Can't use both -r or -R and -i or -I "
197 if (remove_service_set)
198 return remove_service(service_name);
199 if (install_service_set) {
200 program_name = _fullpath(NULL, argv[0], 0);
201 if (config_file != NULL)
202 config_file = _fullpath(NULL, config_file, 0);
206 if (emp_config(config_file) < 0)
208 if (chdir(datadir)) {
209 fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno));
214 if (install_service_set)
215 return install_service(program_name, service_name, config_file);
221 if (daemonize != 0) {
222 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
223 if (StartServiceCtrlDispatcher(DispatchTable))
227 * If it is service startup error then exit otherwise
228 * start server in the foreground
230 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
231 logerror("Failed to dispatch service (%d)", GetLastError());
246 CANT_HAPPEN("main thread terminated");
253 * Initialize for serving, acquire resources.
262 update_policy_check();
272 if (opt_MOB_ACCESS) {
273 /* This fixes up mobility upon restart */
284 start_server(int flags)
288 struct sigaction act;
292 create_pidfile(pidfname, pid);
293 logerror("------------------------------------------------------");
294 logerror("Empire server (pid %d) started", (int)pid);
297 /* signal() should not be used with mit pthreads. Anyway if u
298 have a posix threads u definitly have posix signals -- Sasha */
299 sigemptyset(&act.sa_mask);
300 act.sa_handler = shutdwn;
301 sigaction(SIGTERM, &act, NULL);
302 sigaction(SIGINT, &act, NULL);
303 act.sa_handler = panic;
304 sigaction(SIGBUS, &act, NULL);
305 sigaction(SIGSEGV, &act, NULL);
306 sigaction(SIGILL, &act, NULL);
307 sigaction(SIGFPE, &act, NULL);
308 act.sa_handler = SIG_IGN;
309 sigaction(SIGPIPE, &act, NULL);
312 empth_init((void **)&player, flags);
314 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
315 "AcceptPlayers", "Accept network connections", 0);
316 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
317 "KillIdle", "Kills idle player connections", 0);
318 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
319 "Schedules updates to occur", 0);
320 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
321 "DeleteItems", "Deletes old lost items", 0);
322 if (opt_MOB_ACCESS) {
323 /* Start the mobility access check thread */
324 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
325 "MobilityCheck", "Writes the timestamp file", 0);
329 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
330 "MarketUpdate", "Updates the market", 0);
335 * Finish serving, release resources.
348 create_pidfile(char *fname, pid_t pid)
350 FILE *pidf = fopen(fname, "w");
352 || fprintf(pidf, "%d\n", pid) < 0
354 logerror("Can't write PID file (%s)", strerror(errno));
363 failed |= !ef_open(EF_NATION, O_RDWR, EFF_MEM);
364 failed |= !ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
365 failed |= !ef_open(EF_SHIP, O_RDWR, EFF_MEM);
366 failed |= !ef_open(EF_PLANE, O_RDWR, EFF_MEM);
367 failed |= !ef_open(EF_LAND, O_RDWR, EFF_MEM);
368 failed |= !ef_open(EF_NEWS, O_RDWR, 0);
369 failed |= !ef_open(EF_LOAN, O_RDWR, 0);
370 failed |= !ef_open(EF_TREATY, O_RDWR, 0);
371 failed |= !ef_open(EF_NUKE, O_RDWR, EFF_MEM);
372 failed |= !ef_open(EF_POWER, O_RDWR, 0);
373 failed |= !ef_open(EF_TRADE, O_RDWR, 0);
374 failed |= !ef_open(EF_MAP, O_RDWR, EFF_MEM);
375 failed |= !ef_open(EF_BMAP, O_RDWR, EFF_MEM);
376 failed |= !ef_open(EF_COMM, O_RDWR, 0);
377 failed |= !ef_open(EF_LOST, O_RDWR, 0);
379 logerror("Missing files, giving up");
404 /* we're going down. try to close the files at least */
409 struct sigaction act;
412 sigemptyset(&act.sa_mask);
413 act.sa_handler = SIG_DFL;
414 sigaction(SIGBUS, &act, NULL);
415 sigaction(SIGSEGV, &act, NULL);
416 sigaction(SIGILL, &act, NULL);
417 sigaction(SIGFPE, &act, NULL);
418 logerror("server received fatal signal %d", sig);
421 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
422 && sig != SIGILL && sig != SIGFPE))
435 logerror("Shutdown commencing (cleaning up threads.)");
437 for (p = player_next(0); p != 0; p = player_next(p)) {
438 if (p->state != PS_PLAYING)
440 pr_flash(p, "Server shutting down...\n");
441 p->state = PS_SHUTDOWN;
444 pr_flash(p, "Shutdown aborting command\n");
446 empth_wakeup(p->proc);
450 /* Sleep and let some cleanup happen - note this doesn't work
451 when called from a signal handler, since we may or may not
452 be in the right thread. So we just pass by and kill 'em
455 empth_sleep(now + 1);
458 for (p = player_next(0); p != 0; p = player_next(p)) {
461 empth_terminate(p->proc);
462 p = player_delete(p);
465 logerror("Server shutting down on signal %d", sig);
467 logerror("Server shutting down at deity's request");
481 nullify_objects(void)
486 dchr[SCT_CAPIT] = bigcity_dchr;
488 dchr[SCT_LIGHT].d_cost = -1;
490 dchr[SCT_HEAVY].d_cost = -1;
492 dchr[SCT_OIL].d_cost = -1;
493 dchr[SCT_REFINE].d_cost = -1;
495 for (i = 0; i < pln_maxno; i++) {
501 plchr[i].pl_fuel = 0;
503 for (i = 0; i < lnd_maxno; i++) {
508 /* Fix up the military values */
509 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
511 for (i = 0; i < shp_maxno; i++) {
517 if (mchr[i].m_flags & M_OIL)
521 for (i = 0; i < nuk_maxno; i++) {
527 for (i = 0; i <= SCT_MAXDEF; i++) {
533 for (i = 0; i < prd_maxno; i++) {
534 for (j = 0; j < MAXPRCON; j++) {
535 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
536 pchr[i].p_camt[j] = 0;
537 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
538 pchr[i].p_camt[j] = 0;
539 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
540 pchr[i].p_camt[j] = 0;
550 WORD wVersionRequested;
553 wVersionRequested = MAKEWORD(2, 0);
554 rc = WSAStartup(wVersionRequested, &wsaData);
556 logerror("WSAStartup failed. %d", rc);