2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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: Empire Server main, startup and shutdown
30 * Known contributors to this file:
32 * Steve McClure, 1996, 1998
34 * Ron Koenderink, 2004-2005
35 * Markus Armbruster, 2005-2008
56 #include "empthread.h"
68 #include "prototypes.h"
74 static void ignore(void);
75 static void crash_dump(void);
76 static void create_pidfile(char *, pid_t);
79 static void loc_NTInit(void);
80 static void loc_NTTerm(void);
84 * Lock to synchronize player threads with update and shutdown.
85 * Update and shutdown takes it exclusive, commands take it shared.
87 empth_rwlock_t *play_lock;
90 * Is a thread attempting to take an exclusive play_lock?
91 * Threads holding a shared play_lock must not sleep while this is
94 int play_wrlock_wanted;
96 static char pidfname[] = "server.pid";
98 /* Run as daemon? If yes, detach from controlling terminal etc. */
99 static int daemonize = 1;
102 help(char *program_name, char *complaint)
105 fprintf(stderr, "%s: %s\n", program_name, complaint);
106 fprintf(stderr, "Try -h for help.\n");
110 print_usage(char *program_name)
112 printf("Usage: %s [OPTION]...\n"
113 " -d debug mode, implies -E abort\n"
114 " -e CONFIG-FILE configuration file\n"
116 " -E ACTION what to do on oops: abort, crash-dump, nothing (default)\n"
117 " -h display this help and exit\n"
119 " -i install service `%s'\n"
120 " -I NAME install service NAME\n"
122 " -p threading debug mode, implies -d\n"
124 " -u uninstall service `%s'\n"
125 " -U NAME uninstall service NAME\n"
127 " -s enable stack checking\n"
128 " -R RANDOM-SEED random seed\n"
129 " -v display version information and exit\n",
130 program_name, dflt_econfig
132 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
138 main(int argc, char **argv)
140 static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
141 static void (*oops_hndlr[])(void) = { abort, crash_dump, ignore };
144 int install_service_set = 0;
145 char *program_name = NULL;
146 char *service_name = NULL;
147 int remove_service_set = 0;
149 char *config_file = NULL;
151 unsigned seed = time(NULL);
153 oops_handler = ignore;
156 # define XOPTS "iI:uU:"
160 while ((op = getopt(argc, argv, "de:E:hpsR:v" XOPTS)) != EOF) {
163 flags |= EMPTH_PRINT;
166 oops_handler = abort;
170 config_file = optarg;
173 idx = stmtch(optarg, oops_key, 0, sizeof(*oops_key));
175 help(argv[0], "invalid argument for -E");
178 oops_handler = oops_hndlr[idx];
182 service_name = optarg;
185 install_service_set++;
188 service_name = optarg;
191 remove_service_set++;
195 flags |= EMPTH_STACKCHECK;
198 seed = strtoul(optarg, NULL, 10);
201 printf("%s\n\n%s", version, legal);
204 print_usage(argv[0]);
213 if ((!daemonize || flags || config_file != NULL) &&
214 remove_service_set) {
215 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
216 "-u or -U options\n");
219 if ((!daemonize || flags) && install_service_set) {
220 fprintf(stderr, "Can't use -d, -p or -s with either "
221 "-i or -I options\n");
224 if (install_service_set && remove_service_set) {
225 fprintf(stderr, "Can't use both -u or -U and -i or -I "
230 if (remove_service_set)
231 return remove_service(service_name);
232 if (install_service_set) {
233 program_name = _fullpath(NULL, argv[0], 0);
234 if (config_file != NULL)
235 config_file = _fullpath(NULL, config_file, 0);
240 if (emp_config(config_file) < 0)
243 if (read_builtin_tables() < 0)
245 if (read_custom_tables() < 0)
247 if (chdir(gamedir)) {
248 fprintf(stderr, "Can't chdir to %s (%s)\n",
249 gamedir, strerror(errno));
254 if (install_service_set)
255 return install_service(program_name, service_name, config_file);
261 if (daemonize != 0) {
262 SERVICE_TABLE_ENTRY DispatchTable[]={
263 {"Empire Server", service_main},
266 if (StartServiceCtrlDispatcher(DispatchTable))
270 * If it is service startup error then exit otherwise
271 * start server in the foreground
273 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
274 logerror("Failed to dispatch service (%lu)",
284 if (disassoc() < 0) {
285 logerror("Can't become daemon (%s)", strerror(errno));
294 sig = empth_wait_for_signal();
297 /* if you make changes here, also update relo() */
322 logerror("Crash dump is not implemented");
329 logerror("Can't fork for crash dump (%s)", strerror(errno));
336 while (waitpid(pid, &status, 0) < 0) {
337 if (errno != EINTR) {
338 logerror("Can't get crash dumping child's status (%s)",
343 run_hook(post_crash_dump_hook, "post-crash-dump");
344 logerror("Crash dump complete");
349 * Initialize for serving, acquire resources.
352 init_server(unsigned seed)
370 start_server(int flags)
375 create_pidfile(pidfname, pid);
376 logerror("------------------------------------------------------");
377 logerror("Empire server (pid %d) started", (int)pid);
379 empth_init((void **)&player, flags);
381 if (journal_startup() < 0)
384 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
391 * Finish serving, release resources.
405 create_pidfile(char *fname, pid_t pid)
407 FILE *pidf = fopen(fname, "w");
409 || fprintf(pidf, "%d\n", (int)pid) < 0
411 logerror("Can't write PID file (%s)", strerror(errno));
421 logerror("Shutdown commencing (cleaning up threads.)");
423 play_wrlock_wanted = 1;
424 for (p = player_next(0); p != 0; p = player_next(p)) {
425 if (p->state != PS_PLAYING)
427 pr_flash(p, "Server shutting down...\n");
428 p->state = PS_SHUTDOWN;
431 pr_flash(p, "Shutdown aborting command\n");
433 empth_wakeup(p->proc);
435 empth_rwlock_wrlock(play_lock);
437 logerror("Server shutting down on signal %d", sig);
439 logerror("Server shutting down at deity's request");
454 WORD wVersionRequested;
457 wVersionRequested = MAKEWORD(2, 0);
458 rc = WSAStartup(wVersionRequested, &wsaData);
460 logerror("WSAStartup failed. %d", rc);