2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2014
50 #include "sys/socket.h"
55 #include "empthread.h"
62 #include "prototypes.h"
66 static void ignore(void);
67 static void crash_dump(void);
68 static void init_server(unsigned, int);
69 static void create_pidfile(char *, pid_t);
72 static void loc_NTInit(void);
76 * Lock to synchronize player threads with update.
77 * Update holds it exclusive, commands hold it shared.
79 empth_rwlock_t *update_lock;
81 * Lock to synchronize player threads with shutdown.
82 * Shutdown holds it exclusive, player threads in state PS_PLAYING
85 empth_rwlock_t *shutdown_lock;
87 static char pidfname[] = "server.pid";
89 /* Run as daemon? If yes, detach from controlling terminal etc. */
90 static int daemonize = 1;
93 help(char *program_name, char *complaint)
96 fprintf(stderr, "%s: %s\n", program_name, complaint);
97 fprintf(stderr, "Try -h for help.\n");
101 print_usage(char *program_name)
103 printf("Usage: %s [OPTION]...\n"
104 " -d debug mode, implies -E abort\n"
105 " -e CONFIG-FILE configuration file\n"
107 " -E ACTION what to do on oops: abort, crash-dump, nothing (default)\n"
109 " -i install service `%s'\n"
110 " -I NAME install service NAME\n"
112 " -p threading debug mode, implies -d\n"
114 " -u uninstall service `%s'\n"
115 " -U NAME uninstall service NAME\n"
117 " -s enable stack checking\n"
118 " -R RANDOM-SEED random seed\n"
119 " -h display this help and exit\n"
120 " -v display version information and exit\n",
121 program_name, dflt_econfig
123 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
129 main(int argc, char **argv)
131 static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
132 static void (*oops_hndlr[])(void) = { abort, crash_dump, ignore };
135 int install_service_set = 0;
136 char *program_name = NULL;
137 char *service_name = NULL;
138 int remove_service_set = 0;
140 char *config_file = NULL;
141 int force_bad_state = 0;
146 oops_handler = ignore;
149 # define XOPTS "iI:uU:"
153 while ((op = getopt(argc, argv, "de:E:FhpsR:v" XOPTS)) != EOF) {
156 flags |= EMPTH_PRINT;
159 oops_handler = abort;
163 config_file = optarg;
166 idx = stmtch(optarg, oops_key, 0, sizeof(*oops_key));
168 help(argv[0], "invalid argument for -E");
171 oops_handler = oops_hndlr[idx];
178 service_name = optarg;
181 install_service_set++;
184 service_name = optarg;
187 remove_service_set++;
191 flags |= EMPTH_STACKCHECK;
194 seed = strtoul(optarg, NULL, 10);
198 printf("%s\n\n%s", version, legal);
201 print_usage(argv[0]);
209 /* silently ignore operands for backward compatibility */
212 if ((!daemonize || flags || config_file != NULL)
213 && remove_service_set) {
214 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
215 "-u or -U options\n");
218 if ((!daemonize || flags) && install_service_set) {
220 "Can't use -d, -p or -s with either -i or -I options\n");
223 if (install_service_set && remove_service_set) {
224 fprintf(stderr, "Can't use both -u or -U and -i or -I options\n");
228 if (remove_service_set)
229 return remove_service(service_name);
230 if (install_service_set) {
231 program_name = _fullpath(NULL, argv[0], 0);
232 if (config_file != NULL)
233 config_file = _fullpath(NULL, config_file, 0);
238 if (emp_config(config_file) < 0)
241 if (read_builtin_tables() < 0)
243 if (read_custom_tables() < 0)
245 if (chdir(gamedir)) {
246 fprintf(stderr, "Can't chdir to %s (%s)\n",
247 gamedir, strerror(errno));
252 if (install_service_set)
253 return install_service(program_name, service_name, config_file);
258 init_server(seed, force_bad_state);
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));
293 sig = empth_wait_for_signal();
296 /* if you make changes here, also update relo() */
321 logerror("Crash dump is not implemented");
329 logerror("Can't fork for crash dump (%s)", strerror(errno));
333 raise(SIGABRT); /* child */
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, int force_bad_state)
359 ef_init_srv(force_bad_state);
363 if (journal_startup() < 0)
373 start_server(int flags)
378 create_pidfile(pidfname, pid);
379 logerror("------------------------------------------------------");
380 logerror("Empire server (pid %d) started", (int)pid);
381 if (running_test_suite)
382 logerror("Configured for testing");
384 empth_init((void **)&player, flags);
386 update_lock = empth_rwlock_create("Update");
387 shutdown_lock = empth_rwlock_create("Shutdown");
388 if (!update_lock || !shutdown_lock)
393 empth_create(player_accept, 65536, flags, "AcceptPlayers", NULL);
397 * Finish serving, release resources.
408 create_pidfile(char *fname, pid_t pid)
410 FILE *pidf = fopen(fname, "w");
412 || fprintf(pidf, "%d\n", (int)pid) < 0
414 logerror("Can't write PID file (%s)", strerror(errno));
423 time_t now = time(NULL);
426 logerror("Shutdown commencing (cleaning up threads.)");
428 for (p = player_next(NULL); p; p = player_next(p)) {
429 if (p->state != PS_PLAYING)
431 pr_flash(p, "Server shutting down...\n");
434 p->may_sleep = PLAYER_SLEEP_NEVER;
436 pr_flash(p, "Shutdown aborting command\n");
438 empth_wakeup(p->proc);
441 empth_rwlock_wrlock(shutdown_lock);
444 for (i = 1; i <= 3 && player_next(NULL); i++) {
445 logerror("Waiting for player threads to terminate\n");
446 empth_sleep(now + i);
449 for (p = player_next(NULL); p; p = player_next(p))
450 logerror("Player %d lingers, output might be lost", p->cnum);
453 logerror("Server shutting down on signal %d", sig);
455 logerror("Server shutting down at deity's request");
471 rc = w32_socket_init();
473 logerror("WSAStartup Failed, error code %d\n", rc);