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 /* Debugging? If yes call abort() on internal error. */
85 /* Run as daemon? If yes, detach from controlling terminal etc. */
89 print_usage(char *program_name)
91 printf("Usage: %s [OPTION]...\n"
93 " -e CONFIG-FILE configuration file\n"
95 " -h display this help and exit\n"
97 " -i install service `%s'\n"
98 " -I NAME install service NAME\n"
100 " -p threading debug mode, implies -d\n"
102 " -r remove service `%s'\n"
103 " -R NAME remove service NAME\n"
105 " -s enable stack checking\n"
106 " -v display version information and exit\n",
107 program_name, dflt_econfig
109 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
115 main(int argc, char **argv)
119 int install_service_set = 0;
120 char *service_name = NULL;
121 int remove_service_set = 0;
123 char *config_file = NULL;
127 # define XOPTS "iI:rR:"
131 while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
134 flags |= EMPTH_PRINT;
141 config_file = optarg;
145 service_name = optarg;
150 install_service_set++;
153 service_name = optarg;
158 remove_service_set++;
162 flags |= EMPTH_STACKCHECK;
165 printf("Wolfpack Empire %d.%d.%d\n",
166 EMP_VERS_MAJOR, EMP_VERS_MINOR, EMP_VERS_PATCH);
169 print_usage(argv[0]);
172 fprintf(stderr, "Try -h for help.\n");
178 if ((debug || flags || config_file != NULL) &&
179 remove_service_set) {
180 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
181 "-r or -R options\n");
184 if ((debug || flags) && install_service_set) {
185 fprintf(stderr, "Can't use -d, -p or -s with either "
186 "-i or -I options\n");
189 if (install_service_set && remove_service_set) {
190 fprintf(stderr, "Can't use both -r or -R and -i or -I "
198 if (remove_service_set)
199 return remove_service(service_name);
202 if (emp_config(config_file) < 0)
204 if (chdir(datadir)) {
205 fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno));
210 if (install_service_set)
211 return install_service(argv[0], service_name, config_file);
217 if (daemonize != 0) {
218 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
219 if (StartServiceCtrlDispatcher(DispatchTable))
223 * If it is service startup error then exit otherwise
224 * start server in the foreground
226 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
227 logerror("Failed to dispatch service (%d)", GetLastError());
242 CANT_HAPPEN("main thread terminated");
249 * Initialize for serving, acquire resources.
258 update_policy_check();
268 if (opt_MOB_ACCESS) {
269 /* This fixes up mobility upon restart */
280 start_server(int flags)
284 struct sigaction act;
288 create_pidfile(pidfname, pid);
289 logerror("------------------------------------------------------");
290 logerror("Empire server (pid %d) started", (int)pid);
293 /* signal() should not be used with mit pthreads. Anyway if u
294 have a posix threads u definitly have posix signals -- Sasha */
295 sigemptyset(&act.sa_mask);
296 act.sa_handler = shutdwn;
297 sigaction(SIGTERM, &act, NULL);
298 sigaction(SIGINT, &act, NULL);
299 act.sa_handler = panic;
300 sigaction(SIGBUS, &act, NULL);
301 sigaction(SIGSEGV, &act, NULL);
302 sigaction(SIGILL, &act, NULL);
303 sigaction(SIGFPE, &act, NULL);
304 act.sa_handler = SIG_IGN;
305 sigaction(SIGPIPE, &act, NULL);
308 empth_init((char **)&player, flags);
310 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
311 "AcceptPlayers", "Accept network connections", 0);
312 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
313 "KillIdle", "Kills idle player connections", 0);
314 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
315 "Schedules updates to occur", 0);
316 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
317 "DeleteItems", "Deletes old lost items", 0);
318 if (opt_MOB_ACCESS) {
319 /* Start the mobility access check thread */
320 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
321 "MobilityCheck", "Writes the timestamp file", 0);
325 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
326 "MarketUpdate", "Updates the market", 0);
331 * Finish serving, release resources.
344 create_pidfile(char *fname, pid_t pid)
346 FILE *pidf = fopen(fname, "w");
348 || fprintf(pidf, "%d\n", pid) < 0
350 logerror("Can't write PID file (%s)", strerror(errno));
359 failed |= !ef_open(EF_NATION, O_RDWR, EFF_MEM);
360 failed |= !ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
361 failed |= !ef_open(EF_SHIP, O_RDWR, EFF_MEM);
362 failed |= !ef_open(EF_PLANE, O_RDWR, EFF_MEM);
363 failed |= !ef_open(EF_LAND, O_RDWR, EFF_MEM);
364 failed |= !ef_open(EF_NEWS, O_RDWR, 0);
365 failed |= !ef_open(EF_LOAN, O_RDWR, 0);
366 failed |= !ef_open(EF_TREATY, O_RDWR, 0);
367 failed |= !ef_open(EF_NUKE, O_RDWR, EFF_MEM);
368 failed |= !ef_open(EF_POWER, O_RDWR, 0);
369 failed |= !ef_open(EF_TRADE, O_RDWR, 0);
370 failed |= !ef_open(EF_MAP, O_RDWR, EFF_MEM);
371 failed |= !ef_open(EF_BMAP, O_RDWR, EFF_MEM);
372 failed |= !ef_open(EF_COMM, O_RDWR, 0);
373 failed |= !ef_open(EF_LOST, O_RDWR, 0);
375 logerror("Missing files, giving up");
400 /* we're going down. try to close the files at least */
405 struct sigaction act;
408 sigemptyset(&act.sa_mask);
409 act.sa_handler = SIG_DFL;
410 sigaction(SIGBUS, &act, NULL);
411 sigaction(SIGSEGV, &act, NULL);
412 sigaction(SIGILL, &act, NULL);
413 sigaction(SIGFPE, &act, NULL);
414 logerror("server received fatal signal %d", sig);
417 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
418 && sig != SIGILL && sig != SIGFPE))
431 logerror("Shutdown commencing (cleaning up threads.)");
433 for (p = player_next(0); p != 0; p = player_next(p)) {
434 if (p->state != PS_PLAYING)
436 pr_flash(p, "Server shutting down...\n");
437 p->state = PS_SHUTDOWN;
440 pr_flash(p, "Shutdown aborting command\n");
442 empth_wakeup(p->proc);
446 /* Sleep and let some cleanup happen - note this doesn't work
447 when called from a signal handler, since we may or may not
448 be in the right thread. So we just pass by and kill 'em
451 empth_sleep(now + 1);
454 for (p = player_next(0); p != 0; p = player_next(p)) {
457 empth_terminate(p->proc);
458 p = player_delete(p);
461 logerror("Server shutting down on signal %d", sig);
463 logerror("Server shutting down at deity's request");
477 nullify_objects(void)
482 dchr[SCT_CAPIT] = bigcity_dchr;
484 dchr[SCT_LIGHT].d_cost = -1;
486 dchr[SCT_HEAVY].d_cost = -1;
488 dchr[SCT_OIL].d_cost = -1;
489 dchr[SCT_REFINE].d_cost = -1;
491 for (i = 0; i < pln_maxno; i++) {
497 plchr[i].pl_fuel = 0;
499 for (i = 0; i < lnd_maxno; i++) {
504 /* Fix up the military values */
505 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
507 for (i = 0; i < shp_maxno; i++) {
513 if (mchr[i].m_flags & M_OIL)
517 for (i = 0; i < nuk_maxno; i++) {
523 for (i = 0; i <= SCT_MAXDEF; i++) {
529 for (i = 0; i < prd_maxno; i++) {
530 for (j = 0; j < MAXPRCON; j++) {
531 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
532 pchr[i].p_camt[j] = 0;
533 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
534 pchr[i].p_camt[j] = 0;
535 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
536 pchr[i].p_camt[j] = 0;
546 WORD wVersionRequested;
549 wVersionRequested = MAKEWORD(2, 0);
550 rc = WSAStartup(wVersionRequested, &wsaData);
552 logerror("WSAStartup failed. %d", rc);