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-2009
50 #include "sys/socket.h"
54 #include "empthread.h"
66 #include "prototypes.h"
72 static void ignore(void);
73 static void crash_dump(void);
74 static void create_pidfile(char *, pid_t);
77 static void loc_NTInit(void);
81 * Lock to synchronize player threads with update and shutdown.
82 * Update and shutdown takes it exclusive, commands take it shared.
84 empth_rwlock_t *play_lock;
86 static char pidfname[] = "server.pid";
88 /* Run as daemon? If yes, detach from controlling terminal etc. */
89 static int daemonize = 1;
92 help(char *program_name, char *complaint)
95 fprintf(stderr, "%s: %s\n", program_name, complaint);
96 fprintf(stderr, "Try -h for help.\n");
100 print_usage(char *program_name)
102 printf("Usage: %s [OPTION]...\n"
103 " -d debug mode, implies -E abort\n"
104 " -e CONFIG-FILE configuration file\n"
106 " -E ACTION what to do on oops: abort, crash-dump, nothing (default)\n"
108 " -i install service `%s'\n"
109 " -I NAME install service NAME\n"
111 " -p threading debug mode, implies -d\n"
113 " -u uninstall service `%s'\n"
114 " -U NAME uninstall service NAME\n"
116 " -s enable stack checking\n"
117 " -R RANDOM-SEED random seed\n"
118 " -h display this help and exit\n"
119 " -v display version information and exit\n",
120 program_name, dflt_econfig
122 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
128 main(int argc, char **argv)
130 static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
131 static void (*oops_hndlr[])(void) = { abort, crash_dump, ignore };
134 int install_service_set = 0;
135 char *program_name = NULL;
136 char *service_name = NULL;
137 int remove_service_set = 0;
139 char *config_file = NULL;
141 unsigned seed = time(NULL);
143 oops_handler = ignore;
146 # define XOPTS "iI:uU:"
150 while ((op = getopt(argc, argv, "de:E:hpsR:v" XOPTS)) != EOF) {
153 flags |= EMPTH_PRINT;
156 oops_handler = abort;
160 config_file = optarg;
163 idx = stmtch(optarg, oops_key, 0, sizeof(*oops_key));
165 help(argv[0], "invalid argument for -E");
168 oops_handler = oops_hndlr[idx];
172 service_name = optarg;
175 install_service_set++;
178 service_name = optarg;
181 remove_service_set++;
185 flags |= EMPTH_STACKCHECK;
188 seed = strtoul(optarg, NULL, 10);
191 printf("%s\n\n%s", version, legal);
194 print_usage(argv[0]);
202 /* silently ignore operands for backward compatibility */
205 if ((!daemonize || flags || config_file != NULL)
206 && remove_service_set) {
207 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
208 "-u or -U options\n");
211 if ((!daemonize || flags) && install_service_set) {
213 "Can't use -d, -p or -s with either -i or -I options\n");
216 if (install_service_set && remove_service_set) {
217 fprintf(stderr, "Can't use both -u or -U and -i or -I options\n");
221 if (remove_service_set)
222 return remove_service(service_name);
223 if (install_service_set) {
224 program_name = _fullpath(NULL, argv[0], 0);
225 if (config_file != NULL)
226 config_file = _fullpath(NULL, config_file, 0);
231 if (emp_config(config_file) < 0)
234 if (read_builtin_tables() < 0)
236 if (read_custom_tables() < 0)
238 if (chdir(gamedir)) {
239 fprintf(stderr, "Can't chdir to %s (%s)\n",
240 gamedir, strerror(errno));
245 if (install_service_set)
246 return install_service(program_name, service_name, config_file);
252 if (daemonize != 0) {
253 SERVICE_TABLE_ENTRY DispatchTable[]={
254 {"Empire Server", service_main},
257 if (StartServiceCtrlDispatcher(DispatchTable))
261 * If it is service startup error then exit otherwise
262 * start server in the foreground
264 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
265 logerror("Failed to dispatch service (%lu)",
275 if (disassoc() < 0) {
276 logerror("Can't become daemon (%s)", strerror(errno));
285 sig = empth_wait_for_signal();
288 /* if you make changes here, also update relo() */
313 logerror("Crash dump is not implemented");
321 logerror("Can't fork for crash dump (%s)", strerror(errno));
325 raise(SIGABRT); /* child */
328 while (waitpid(pid, &status, 0) < 0) {
329 if (errno != EINTR) {
330 logerror("Can't get crash dumping child's status (%s)",
335 run_hook(post_crash_dump_hook, "post-crash-dump");
336 logerror("Crash dump complete");
341 * Initialize for serving, acquire resources.
344 init_server(unsigned seed)
362 start_server(int flags)
367 create_pidfile(pidfname, pid);
368 logerror("------------------------------------------------------");
369 logerror("Empire server (pid %d) started", (int)pid);
371 empth_init((void **)&player, flags);
373 if (journal_startup() < 0)
378 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", NULL);
382 * Finish serving, release resources.
393 create_pidfile(char *fname, pid_t pid)
395 FILE *pidf = fopen(fname, "w");
397 || fprintf(pidf, "%d\n", (int)pid) < 0
399 logerror("Can't write PID file (%s)", strerror(errno));
409 int i, queues_drained;
411 logerror("Shutdown commencing (cleaning up threads.)");
413 for (p = player_next(NULL); p; p = player_next(p)) {
414 if (p->state != PS_PLAYING)
416 pr_flash(p, "Server shutting down...\n");
417 p->state = PS_SHUTDOWN;
418 p->may_sleep = PLAYER_SLEEP_NEVER;
421 pr_flash(p, "Shutdown aborting command\n");
423 empth_wakeup(p->proc);
425 empth_rwlock_wrlock(play_lock);
429 for (i = 1; i <= 3; i++) {
431 for (p = player_next(NULL); p; p = player_next(p)) {
432 if (io_outputwaiting(p->iop))
437 logerror("Waiting for player output to drain\n");
438 empth_sleep(now + i);
441 for (p = player_next(NULL); p; p = player_next(p)) {
442 if (io_outputwaiting(p->iop))
443 logerror("Output for player %d lost", p->cnum);
447 logerror("Server shutting down on signal %d", sig);
449 logerror("Server shutting down at deity's request");
465 rc = w32_socket_init();
467 logerror("WSAStartup Failed, error code %d\n", rc);