2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future 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
34 * Ron Koenderink, 2004-2005
35 * Markus Armbruster, 2005
42 #include <sys/ioctl.h>
53 #include "../lib/gen/getopt.h"
62 #include "empthread.h"
72 #include "prototypes.h"
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("%s\n\n%s", version, legal);
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)
208 if (chdir(configdir)) {
209 fprintf(stderr, "Can't chdir to %s (%s)\n", configdir, strerror(errno));
212 if (chdir(builtindir)) {
213 fprintf(stderr, "Can't chdir to %s (%s)\n", builtindir, strerror(errno));
216 if (read_builtin_tables() < 0)
218 if (chdir(configdir)) {
219 fprintf(stderr, "Can't chdir to %s (%s)\n", configdir, strerror(errno));
222 if (read_custom_tables() < 0)
224 if (chdir(gamedir)) {
225 fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
230 if (install_service_set)
231 return install_service(program_name, service_name, config_file);
237 if (daemonize != 0) {
238 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
239 if (StartServiceCtrlDispatcher(DispatchTable))
243 * If it is service startup error then exit otherwise
244 * start server in the foreground
246 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
247 logerror("Failed to dispatch service (%lu)", GetLastError());
256 if (disassoc() < 0) {
257 logerror("Can't become daemon (%s)", strerror(errno));
266 CANT_HAPPEN("main thread terminated");
273 * Initialize for serving, acquire resources.
282 update_policy_check();
289 if (opt_MOB_ACCESS) {
290 /* This fixes up mobility upon restart */
301 start_server(int flags)
305 struct sigaction act;
309 create_pidfile(pidfname, pid);
310 logerror("------------------------------------------------------");
311 logerror("Empire server (pid %d) started", (int)pid);
314 /* signal() should not be used with mit pthreads. Anyway if u
315 have a posix threads u definitly have posix signals -- Sasha */
317 sigemptyset(&act.sa_mask);
318 act.sa_handler = shutdwn;
319 sigaction(SIGTERM, &act, NULL);
320 sigaction(SIGINT, &act, NULL);
321 act.sa_handler = panic;
322 sigaction(SIGBUS, &act, NULL);
323 sigaction(SIGSEGV, &act, NULL);
324 sigaction(SIGILL, &act, NULL);
325 sigaction(SIGFPE, &act, NULL);
326 act.sa_handler = SIG_IGN;
327 sigaction(SIGPIPE, &act, NULL);
330 empth_init((void **)&player, flags);
332 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
333 "AcceptPlayers", "Accept network connections", 0);
334 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
335 "KillIdle", "Kills idle player connections", 0);
336 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
337 "Schedules updates to occur", 0);
338 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
339 "DeleteItems", "Deletes old lost items", 0);
340 if (opt_MOB_ACCESS) {
341 /* Start the mobility access check thread */
342 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
343 "MobilityCheck", "Writes the timestamp file", 0);
347 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
348 "MarketUpdate", "Updates the market", 0);
353 * Finish serving, release resources.
366 create_pidfile(char *fname, pid_t pid)
368 FILE *pidf = fopen(fname, "w");
370 || fprintf(pidf, "%d\n", (int)pid) < 0
372 logerror("Can't write PID file (%s)", strerror(errno));
377 /* we're going down. try to close the files at least */
382 struct sigaction act;
385 sigemptyset(&act.sa_mask);
386 act.sa_handler = SIG_DFL;
387 sigaction(SIGBUS, &act, NULL);
388 sigaction(SIGSEGV, &act, NULL);
389 sigaction(SIGILL, &act, NULL);
390 sigaction(SIGFPE, &act, NULL);
391 logerror("server received fatal signal %d", sig);
394 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
395 && sig != SIGILL && sig != SIGFPE))
408 logerror("Shutdown commencing (cleaning up threads.)");
410 for (p = player_next(0); p != 0; p = player_next(p)) {
411 if (p->state != PS_PLAYING)
413 pr_flash(p, "Server shutting down...\n");
414 p->state = PS_SHUTDOWN;
417 pr_flash(p, "Shutdown aborting command\n");
419 empth_wakeup(p->proc);
423 /* Sleep and let some cleanup happen - note this doesn't work
424 when called from a signal handler, since we may or may not
425 be in the right thread. So we just pass by and kill 'em
428 empth_sleep(now + 1);
431 for (p = player_next(0); p != 0; p = player_next(p)) {
434 empth_terminate(p->proc);
435 p = player_delete(p);
438 logerror("Server shutting down on signal %d", sig);
440 logerror("Server shutting down at deity's request");
457 WORD wVersionRequested;
460 wVersionRequested = MAKEWORD(2, 0);
461 rc = WSAStartup(wVersionRequested, &wsaData);
463 logerror("WSAStartup failed. %d", rc);