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>
49 #include "../lib/gen/getopt.h"
58 #include "empthread.h"
68 #include "prototypes.h"
70 static void nullify_objects(void);
71 static void init_files(void);
72 static void close_files(void);
73 static void create_pidfile(char *, pid_t);
76 static void loc_NTInit(void);
77 static void loc_NTTerm(void);
80 static char pidfname[] = "server.pid";
82 /* Run as daemon? If yes, detach from controlling terminal etc. */
86 print_usage(char *program_name)
88 printf("Usage: %s [OPTION]...\n"
90 " -e CONFIG-FILE configuration file\n"
92 " -h display this help and exit\n"
94 " -i install service `%s'\n"
95 " -I NAME install service NAME\n"
97 " -p threading debug mode, implies -d\n"
99 " -r remove service `%s'\n"
100 " -R NAME remove service NAME\n"
102 " -s enable stack checking\n"
103 " -v display version information and exit\n",
104 program_name, dflt_econfig
106 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
112 main(int argc, char **argv)
116 int install_service_set = 0;
117 char *program_name = NULL;
118 char *service_name = NULL;
119 int remove_service_set = 0;
121 char *config_file = NULL;
125 # define XOPTS "iI:rR:"
129 while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
132 flags |= EMPTH_PRINT;
139 config_file = optarg;
143 service_name = optarg;
148 install_service_set++;
151 service_name = optarg;
156 remove_service_set++;
160 flags |= EMPTH_STACKCHECK;
163 printf("Wolfpack Empire %d.%d.%d\n",
164 EMP_VERS_MAJOR, EMP_VERS_MINOR, EMP_VERS_PATCH);
167 print_usage(argv[0]);
170 fprintf(stderr, "Try -h for help.\n");
176 if ((debug || flags || config_file != NULL) &&
177 remove_service_set) {
178 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
179 "-r or -R options\n");
182 if ((debug || flags) && install_service_set) {
183 fprintf(stderr, "Can't use -d, -p or -s with either "
184 "-i or -I options\n");
187 if (install_service_set && remove_service_set) {
188 fprintf(stderr, "Can't use both -r or -R and -i or -I "
196 if (remove_service_set)
197 return remove_service(service_name);
198 if (install_service_set) {
199 program_name = _fullpath(NULL, argv[0], 0);
200 if (config_file != NULL)
201 config_file = _fullpath(NULL, config_file, 0);
205 if (emp_config(config_file) < 0)
207 if (chdir(datadir)) {
208 fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno));
213 if (install_service_set)
214 return install_service(program_name, service_name, config_file);
220 if (daemonize != 0) {
221 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
222 if (StartServiceCtrlDispatcher(DispatchTable))
226 * If it is service startup error then exit otherwise
227 * start server in the foreground
229 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
230 logerror("Failed to dispatch service (%d)", GetLastError());
239 if (disassoc() < 0) {
240 logerror("Can't become daemon (%s)", strerror(errno));
249 CANT_HAPPEN("main thread terminated");
256 * Initialize for serving, acquire resources.
265 update_policy_check();
275 if (opt_MOB_ACCESS) {
276 /* This fixes up mobility upon restart */
287 start_server(int flags)
291 struct sigaction act;
295 create_pidfile(pidfname, pid);
296 logerror("------------------------------------------------------");
297 logerror("Empire server (pid %d) started", (int)pid);
300 /* signal() should not be used with mit pthreads. Anyway if u
301 have a posix threads u definitly have posix signals -- Sasha */
303 sigemptyset(&act.sa_mask);
304 act.sa_handler = shutdwn;
305 sigaction(SIGTERM, &act, NULL);
306 sigaction(SIGINT, &act, NULL);
307 act.sa_handler = panic;
308 sigaction(SIGBUS, &act, NULL);
309 sigaction(SIGSEGV, &act, NULL);
310 sigaction(SIGILL, &act, NULL);
311 sigaction(SIGFPE, &act, NULL);
312 act.sa_handler = SIG_IGN;
313 sigaction(SIGPIPE, &act, NULL);
316 empth_init((void **)&player, flags);
318 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
319 "AcceptPlayers", "Accept network connections", 0);
320 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
321 "KillIdle", "Kills idle player connections", 0);
322 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
323 "Schedules updates to occur", 0);
324 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
325 "DeleteItems", "Deletes old lost items", 0);
326 if (opt_MOB_ACCESS) {
327 /* Start the mobility access check thread */
328 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
329 "MobilityCheck", "Writes the timestamp file", 0);
333 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
334 "MarketUpdate", "Updates the market", 0);
339 * Finish serving, release resources.
352 create_pidfile(char *fname, pid_t pid)
354 FILE *pidf = fopen(fname, "w");
356 || fprintf(pidf, "%d\n", (int)pid) < 0
358 logerror("Can't write PID file (%s)", strerror(errno));
367 failed |= !ef_open(EF_NATION, EFF_MEM);
368 failed |= !ef_open(EF_SECTOR, EFF_MEM);
369 failed |= !ef_open(EF_SHIP, EFF_MEM);
370 failed |= !ef_open(EF_PLANE, EFF_MEM);
371 failed |= !ef_open(EF_LAND, EFF_MEM);
372 failed |= !ef_open(EF_NEWS, 0);
373 failed |= !ef_open(EF_LOAN, 0);
374 failed |= !ef_open(EF_TREATY, 0);
375 failed |= !ef_open(EF_NUKE, EFF_MEM);
376 failed |= !ef_open(EF_POWER, 0);
377 failed |= !ef_open(EF_TRADE, 0);
378 failed |= !ef_open(EF_MAP, EFF_MEM);
379 failed |= !ef_open(EF_BMAP, EFF_MEM);
380 failed |= !ef_open(EF_COMM, 0);
381 failed |= !ef_open(EF_LOST, 0);
383 logerror("Missing files, giving up");
408 /* we're going down. try to close the files at least */
413 struct sigaction act;
416 sigemptyset(&act.sa_mask);
417 act.sa_handler = SIG_DFL;
418 sigaction(SIGBUS, &act, NULL);
419 sigaction(SIGSEGV, &act, NULL);
420 sigaction(SIGILL, &act, NULL);
421 sigaction(SIGFPE, &act, NULL);
422 logerror("server received fatal signal %d", sig);
425 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
426 && sig != SIGILL && sig != SIGFPE))
439 logerror("Shutdown commencing (cleaning up threads.)");
441 for (p = player_next(0); p != 0; p = player_next(p)) {
442 if (p->state != PS_PLAYING)
444 pr_flash(p, "Server shutting down...\n");
445 p->state = PS_SHUTDOWN;
448 pr_flash(p, "Shutdown aborting command\n");
450 empth_wakeup(p->proc);
454 /* Sleep and let some cleanup happen - note this doesn't work
455 when called from a signal handler, since we may or may not
456 be in the right thread. So we just pass by and kill 'em
459 empth_sleep(now + 1);
462 for (p = player_next(0); p != 0; p = player_next(p)) {
465 empth_terminate(p->proc);
466 p = player_delete(p);
469 logerror("Server shutting down on signal %d", sig);
471 logerror("Server shutting down at deity's request");
485 nullify_objects(void)
490 dchr[SCT_CAPIT] = bigcity_dchr;
492 dchr[SCT_LIGHT].d_cost = -1;
494 dchr[SCT_HEAVY].d_cost = -1;
496 dchr[SCT_OIL].d_cost = -1;
497 dchr[SCT_REFINE].d_cost = -1;
499 for (i = 0; plchr[i].pl_name; i++) {
505 plchr[i].pl_fuel = 0;
507 for (i = 0; lchr[i].l_name; i++) {
512 /* Fix up the military values */
513 lchr[i].l_mil = lchr[i].l_item[I_MILIT];
515 for (i = 0; mchr[i].m_name; i++) {
521 mchr[i].m_flags &= ~M_OIL;
523 for (i = 0; nchr[i].n_name; i++) {
529 for (i = 0; i <= SCT_MAXDEF; i++) {
535 for (i = 0; pchr[i].p_name; i++) {
536 for (j = 0; j < MAXPRCON; j++) {
537 if (opt_NO_HCMS && pchr[i].p_ctype[j] == I_HCM)
538 pchr[i].p_camt[j] = 0;
539 if (opt_NO_LCMS && pchr[i].p_ctype[j] == I_LCM)
540 pchr[i].p_camt[j] = 0;
541 if (opt_NO_OIL && pchr[i].p_ctype[j] == I_OIL)
542 pchr[i].p_camt[j] = 0;
545 for (i = 0; intrchr[i].in_name; i++) {
547 intrchr[i].in_hcms = 0;
549 intrchr[i].in_lcms = 0;
558 WORD wVersionRequested;
561 wVersionRequested = MAKEWORD(2, 0);
562 rc = WSAStartup(wVersionRequested, &wsaData);
564 logerror("WSAStartup failed. %d", rc);