2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * main.c: Empire Server main, startup and shutdown
29 * Known contributors to this file:
31 * Steve McClure, 1996, 1998
33 * Ron Koenderink, 2004-2009
34 * Markus Armbruster, 2005-2011
50 #include "sys/socket.h"
54 #include "empthread.h"
63 #include "prototypes.h"
68 static void ignore(void);
69 static void crash_dump(void);
70 static void init_server(unsigned, int);
71 static void create_pidfile(char *, pid_t);
74 static void loc_NTInit(void);
78 * Lock to synchronize player threads with update and shutdown.
79 * Update and shutdown takes it exclusive, commands take it shared.
81 empth_rwlock_t *play_lock;
83 static char pidfname[] = "server.pid";
85 /* Run as daemon? If yes, detach from controlling terminal etc. */
86 static int daemonize = 1;
89 help(char *program_name, char *complaint)
92 fprintf(stderr, "%s: %s\n", program_name, complaint);
93 fprintf(stderr, "Try -h for help.\n");
97 print_usage(char *program_name)
99 printf("Usage: %s [OPTION]...\n"
100 " -d debug mode, implies -E abort\n"
101 " -e CONFIG-FILE configuration file\n"
103 " -E ACTION what to do on oops: abort, crash-dump, nothing (default)\n"
105 " -i install service `%s'\n"
106 " -I NAME install service NAME\n"
108 " -p threading debug mode, implies -d\n"
110 " -u uninstall service `%s'\n"
111 " -U NAME uninstall service NAME\n"
113 " -s enable stack checking\n"
114 " -R RANDOM-SEED random seed\n"
115 " -h display this help and exit\n"
116 " -v display version information and exit\n",
117 program_name, dflt_econfig
119 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
125 main(int argc, char **argv)
127 static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
128 static void (*oops_hndlr[])(void) = { abort, crash_dump, ignore };
131 int install_service_set = 0;
132 char *program_name = NULL;
133 char *service_name = NULL;
134 int remove_service_set = 0;
136 char *config_file = NULL;
137 int force_bad_state = 0;
139 unsigned seed = time(NULL);
141 oops_handler = ignore;
144 # define XOPTS "iI:uU:"
148 while ((op = getopt(argc, argv, "de:E:FhpsR:v" XOPTS)) != EOF) {
151 flags |= EMPTH_PRINT;
154 oops_handler = abort;
158 config_file = optarg;
161 idx = stmtch(optarg, oops_key, 0, sizeof(*oops_key));
163 help(argv[0], "invalid argument for -E");
166 oops_handler = oops_hndlr[idx];
173 service_name = optarg;
176 install_service_set++;
179 service_name = optarg;
182 remove_service_set++;
186 flags |= EMPTH_STACKCHECK;
189 seed = strtoul(optarg, NULL, 10);
192 printf("%s\n\n%s", version, legal);
195 print_usage(argv[0]);
203 /* silently ignore operands for backward compatibility */
206 if ((!daemonize || flags || config_file != NULL)
207 && remove_service_set) {
208 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
209 "-u or -U options\n");
212 if ((!daemonize || flags) && install_service_set) {
214 "Can't use -d, -p or -s with either -i or -I options\n");
217 if (install_service_set && remove_service_set) {
218 fprintf(stderr, "Can't use both -u or -U and -i or -I options\n");
222 if (remove_service_set)
223 return remove_service(service_name);
224 if (install_service_set) {
225 program_name = _fullpath(NULL, argv[0], 0);
226 if (config_file != NULL)
227 config_file = _fullpath(NULL, config_file, 0);
232 if (emp_config(config_file) < 0)
235 if (read_builtin_tables() < 0)
237 if (read_custom_tables() < 0)
239 if (chdir(gamedir)) {
240 fprintf(stderr, "Can't chdir to %s (%s)\n",
241 gamedir, strerror(errno));
246 if (install_service_set)
247 return install_service(program_name, service_name, config_file);
250 init_server(seed, force_bad_state);
253 if (daemonize != 0) {
254 SERVICE_TABLE_ENTRY DispatchTable[]={
255 {"Empire Server", service_main},
258 if (StartServiceCtrlDispatcher(DispatchTable))
262 * If it is service startup error then exit otherwise
263 * start server in the foreground
265 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
266 logerror("Failed to dispatch service (%lu)",
276 if (disassoc() < 0) {
277 logerror("Can't become daemon (%s)", strerror(errno));
286 sig = empth_wait_for_signal();
289 /* if you make changes here, also update relo() */
314 logerror("Crash dump is not implemented");
322 logerror("Can't fork for crash dump (%s)", strerror(errno));
326 raise(SIGABRT); /* child */
329 while (waitpid(pid, &status, 0) < 0) {
330 if (errno != EINTR) {
331 logerror("Can't get crash dumping child's status (%s)",
336 run_hook(post_crash_dump_hook, "post-crash-dump");
337 logerror("Crash dump complete");
342 * Initialize for serving, acquire resources.
345 init_server(unsigned seed, int force_bad_state)
352 ef_init_srv(force_bad_state);
363 start_server(int flags)
368 create_pidfile(pidfname, pid);
369 logerror("------------------------------------------------------");
370 logerror("Empire server (pid %d) started", (int)pid);
372 empth_init((void **)&player, flags);
374 if (journal_startup() < 0)
379 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", NULL);
383 * Finish serving, release resources.
394 create_pidfile(char *fname, pid_t pid)
396 FILE *pidf = fopen(fname, "w");
398 || fprintf(pidf, "%d\n", (int)pid) < 0
400 logerror("Can't write PID file (%s)", strerror(errno));
412 logerror("Shutdown commencing (cleaning up threads.)");
414 for (p = player_next(NULL); p; p = player_next(p)) {
415 if (p->state != PS_PLAYING)
417 pr_flash(p, "Server shutting down...\n");
420 p->may_sleep = PLAYER_SLEEP_NEVER;
422 pr_flash(p, "Shutdown aborting command\n");
424 empth_wakeup(p->proc);
426 empth_rwlock_wrlock(play_lock);
430 for (i = 1; i <= 3 && player_next(NULL); i++) {
431 logerror("Waiting for player threads to terminate\n");
432 empth_sleep(now + i);
435 for (p = player_next(NULL); p; p = player_next(p))
436 logerror("Player %d lingers, output might be lost", p->cnum);
439 logerror("Server shutting down on signal %d", sig);
441 logerror("Server shutting down at deity's request");
457 rc = w32_socket_init();
459 logerror("WSAStartup Failed, error code %d\n", rc);