/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- * Ken Stevens, Steve McClure
+ * Copyright (C) 1986-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure, Markus Armbruster
*
- * This program is free software; you can redistribute it and/or modify
+ * Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* ---
*
* main.c: Empire Server main, startup and shutdown
- *
+ *
* Known contributors to this file:
* Dave Pare, 1994
* Steve McClure, 1996, 1998
* Doug Hay, 1998
- * Ron Koenderink, 2004-2005
- * Markus Armbruster, 2005-2006
+ * Ron Koenderink, 2004-2009
+ * Markus Armbruster, 2005-2011
*/
#include <config.h>
-#if !defined(_WIN32)
-#include <sys/ioctl.h>
-#endif
#include <errno.h>
#include <signal.h>
#include <stdio.h>
-#include <string.h>
+#ifndef _WIN32
+#include <sys/wait.h>
+#endif
+#include <unistd.h>
#if defined(_WIN32)
-#define WIN32
-#include <winsock2.h>
-#undef NS_ALL
#include <process.h>
-#include "../lib/gen/getopt.h"
#include "service.h"
-#include "direct.h"
+#include "sys/socket.h"
#endif
-#include "misc.h"
-#include "nat.h"
+#include "empio.h"
+#include "empthread.h"
#include "file.h"
#include "journal.h"
-#include "player.h"
-#include "empthread.h"
+#include "match.h"
+#include "misc.h"
+#include "optlist.h"
#include "plane.h"
-#include "nuke.h"
-#include "land.h"
-#include "ship.h"
-#include "sect.h"
+#include "player.h"
#include "product.h"
-#include "optlist.h"
+#include "prototypes.h"
+#include "sect.h"
#include "server.h"
#include "version.h"
-#include "prototypes.h"
+static void ignore(void);
+static void crash_dump(void);
+static void init_server(unsigned, int);
static void create_pidfile(char *, pid_t);
#if defined(_WIN32)
static void loc_NTInit(void);
-static void loc_NTTerm(void);
#endif
+/*
+ * Lock to synchronize player threads with update.
+ * Update holds it exclusive, commands hold it shared.
+ */
+empth_rwlock_t *update_lock;
+/*
+ * Lock to synchronize player threads with shutdown.
+ * Shutdown holds it exclusive, player threads in state PS_PLAYING
+ * hold it shared.
+ */
+empth_rwlock_t *shutdown_lock;
+
static char pidfname[] = "server.pid";
/* Run as daemon? If yes, detach from controlling terminal etc. */
static int daemonize = 1;
+static void
+help(char *program_name, char *complaint)
+{
+ if (complaint)
+ fprintf(stderr, "%s: %s\n", program_name, complaint);
+ fprintf(stderr, "Try -h for help.\n");
+}
+
static void
print_usage(char *program_name)
{
printf("Usage: %s [OPTION]...\n"
- " -d debug mode\n"
+ " -d debug mode, implies -E abort\n"
" -e CONFIG-FILE configuration file\n"
" (default %s)\n"
- " -h display this help and exit\n"
+ " -E ACTION what to do on oops: abort, crash-dump, nothing (default)\n"
#ifdef _WIN32
" -i install service `%s'\n"
" -I NAME install service NAME\n"
#endif
" -p threading debug mode, implies -d\n"
#ifdef _WIN32
- " -r remove service `%s'\n"
- " -R NAME remove service NAME\n"
+ " -u uninstall service `%s'\n"
+ " -U NAME uninstall service NAME\n"
#endif
" -s enable stack checking\n"
+ " -R RANDOM-SEED random seed\n"
+ " -h display this help and exit\n"
" -v display version information and exit\n",
program_name, dflt_econfig
#ifdef _WIN32
int
main(int argc, char **argv)
{
+ static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
+ static void (*oops_hndlr[])(void) = { abort, crash_dump, ignore };
int flags = 0;
#if defined(_WIN32)
int install_service_set = 0;
int remove_service_set = 0;
#endif
char *config_file = NULL;
- int op, sig;
+ int force_bad_state = 0;
+ int op, idx, sig;
+ unsigned seed = time(NULL);
+
+ oops_handler = ignore;
#ifdef _WIN32
-# define XOPTS "iI:rR:"
+# define XOPTS "iI:uU:"
#else
# define XOPTS
#endif
- while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
+ while ((op = getopt(argc, argv, "de:E:FhpsR:v" XOPTS)) != EOF) {
switch (op) {
case 'p':
flags |= EMPTH_PRINT;
/* fall through */
case 'd':
- debug = 1;
+ oops_handler = abort;
daemonize = 0;
break;
case 'e':
config_file = optarg;
break;
+ case 'E':
+ idx = stmtch(optarg, oops_key, 0, sizeof(*oops_key));
+ if (idx < 0) {
+ help(argv[0], "invalid argument for -E");
+ return EXIT_FAILURE;
+ }
+ oops_handler = oops_hndlr[idx];
+ break;
+ case 'F':
+ force_bad_state = 1;
+ break;
#if defined(_WIN32)
case 'I':
service_name = optarg;
case 'i':
install_service_set++;
break;
- case 'R':
+ case 'U':
service_name = optarg;
/* fall through */
- case 'r':
+ case 'u':
remove_service_set++;
break;
#endif /* _WIN32 */
case 's':
flags |= EMPTH_STACKCHECK;
break;
+ case 'R':
+ seed = strtoul(optarg, NULL, 10);
+ break;
case 'v':
printf("%s\n\n%s", version, legal);
return EXIT_SUCCESS;
print_usage(argv[0]);
return EXIT_SUCCESS;
default:
- fprintf(stderr, "Try -h for help.\n");
+ help(argv[0], NULL);
return EXIT_FAILURE;
}
}
+ /* silently ignore operands for backward compatibility */
+
#if defined(_WIN32)
- if ((debug || flags || config_file != NULL) &&
- remove_service_set) {
+ if ((!daemonize || flags || config_file != NULL)
+ && remove_service_set) {
fprintf(stderr, "Can't use -p, -s, -d or -e with either "
- "-r or -R options\n");
+ "-u or -U options\n");
exit(EXIT_FAILURE);
}
- if ((debug || flags) && install_service_set) {
- fprintf(stderr, "Can't use -d, -p or -s with either "
- "-i or -I options\n");
+ if ((!daemonize || flags) && install_service_set) {
+ fprintf(stderr,
+ "Can't use -d, -p or -s with either -i or -I options\n");
exit(EXIT_FAILURE);
}
if (install_service_set && remove_service_set) {
- fprintf(stderr, "Can't use both -r or -R and -i or -I "
- "options\n");
+ fprintf(stderr, "Can't use both -u or -U and -i or -I options\n");
exit(EXIT_FAILURE);
}
if (remove_service_set)
- return remove_service(service_name);
+ return remove_service(service_name);
if (install_service_set) {
program_name = _fullpath(NULL, argv[0], 0);
if (config_file != NULL)
}
#endif /* _WIN32 */
+ empfile_init();
if (emp_config(config_file) < 0)
exit(EXIT_FAILURE);
- ef_init();
- if (chdir(configdir)) {
- fprintf(stderr, "Can't chdir to %s (%s)\n",
- configdir, strerror(errno));
- exit(EXIT_FAILURE);
- }
- if (chdir(builtindir)) {
- fprintf(stderr, "Can't chdir to %s (%s)\n",
- builtindir, strerror(errno));
- exit(EXIT_FAILURE);
- }
+ empfile_fixup();
if (read_builtin_tables() < 0)
exit(EXIT_FAILURE);
- if (chdir(configdir)) {
- fprintf(stderr, "Can't chdir to %s (%s)\n",
- configdir, strerror(errno));
- exit(EXIT_FAILURE);
- }
if (read_custom_tables() < 0)
exit(EXIT_FAILURE);
if (chdir(gamedir)) {
return install_service(program_name, service_name, config_file);
#endif /* _WIN32 */
- init_server();
+ init_server(seed, force_bad_state);
#if defined(_WIN32)
if (daemonize != 0) {
}
#endif /* !_WIN32 */
start_server(flags);
+ journal_prng(seed);
for (;;) {
sig = empth_wait_for_signal();
#ifdef SIGHUP
if (sig == SIGHUP) {
- journal_close();
- journal_open();
+ /* if you make changes here, also update relo() */
+ journal_reopen();
+ update_reschedule();
+ logreopen();
continue;
}
#endif
return EXIT_SUCCESS;
}
+static void
+ignore(void)
+{
+}
+
+static void
+crash_dump(void)
+{
+#ifdef _WIN32
+ logerror("Crash dump is not implemented");
+#else
+ pid_t pid;
+ int status;
+
+ fflush(NULL);
+ pid = fork();
+ if (pid < 0) {
+ logerror("Can't fork for crash dump (%s)", strerror(errno));
+ return;
+ }
+ if (pid == 0)
+ raise(SIGABRT); /* child */
+
+ /* parent */
+ while (waitpid(pid, &status, 0) < 0) {
+ if (errno != EINTR) {
+ logerror("Can't get crash dumping child's status (%s)",
+ strerror(errno));
+ return;
+ }
+ }
+ run_hook(post_crash_dump_hook, "post-crash-dump");
+ logerror("Crash dump complete");
+#endif
+}
/*
* Initialize for serving, acquire resources.
*/
-void
-init_server(void)
+static void
+init_server(unsigned seed, int force_bad_state)
{
- srandom(time(NULL));
+ srandom(seed);
#if defined(_WIN32)
loc_NTInit();
#endif
- update_policy_check();
- shutdown_init();
player_init();
- ef_init_srv();
+ ef_init_srv(force_bad_state);
io_init();
init_nreport();
- if (opt_MOB_ACCESS) {
- /* This fixes up mobility upon restart */
- mobility_init();
- }
-
loginit("server");
}
empth_init((void **)&player, flags);
- empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
- "AcceptPlayers", "Accept network connections", 0);
- empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
- "KillIdle", "Kills idle player connections", 0);
- empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
- "Schedules updates to occur", 0);
- empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
- "DeleteItems", "Deletes old lost items", 0);
- if (opt_MOB_ACCESS) {
- /* Start the mobility access check thread */
- empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
- "MobilityCheck", "Writes the timestamp file", 0);
- }
+ if (journal_startup() < 0)
+ exit(1);
- if (opt_MARKET) {
- empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
- "MarketUpdate", "Updates the market", 0);
- }
+ update_lock = empth_rwlock_create("Update");
+ shutdown_lock = empth_rwlock_create("Shutdown");
+ if (!update_lock || !shutdown_lock)
+ exit_nomem();
+
+ market_init();
+ update_init();
+ empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", NULL);
}
/*
finish_server(void)
{
ef_fin_srv();
-#if defined(_WIN32)
- loc_NTTerm();
-#endif
+ journal_shutdown();
remove(pidfname);
}
shutdwn(int sig)
{
struct player *p;
- time_t now;
+ time_t now = time(NULL);
+ int i;
logerror("Shutdown commencing (cleaning up threads.)");
- for (p = player_next(0); p != 0; p = player_next(p)) {
+ for (p = player_next(NULL); p; p = player_next(p)) {
if (p->state != PS_PLAYING)
continue;
pr_flash(p, "Server shutting down...\n");
- p->state = PS_SHUTDOWN;
- p->aborted++;
+ io_set_eof(p->iop);
+ p->aborted = 1;
+ p->may_sleep = PLAYER_SLEEP_NEVER;
if (p->command) {
pr_flash(p, "Shutdown aborting command\n");
}
empth_wakeup(p->proc);
}
- time(&now);
- empth_sleep(now + 1);
+ empth_rwlock_wrlock(shutdown_lock);
+ empth_yield();
- for (p = player_next(0); p != 0; p = player_next(p)) {
- p->state = PS_KILL;
- p->aborted++;
- empth_terminate(p->proc);
- p = player_delete(p);
+ for (i = 1; i <= 3 && player_next(NULL); i++) {
+ logerror("Waiting for player threads to terminate\n");
+ empth_sleep(now + i);
}
+
+ for (p = player_next(NULL); p; p = player_next(p))
+ logerror("Player %d lingers, output might be lost", p->cnum);
+
if (sig)
logerror("Server shutting down on signal %d", sig);
else
#if defined(_WIN32)
if (daemonize)
- stop_service();
+ stop_service();
#endif
exit(0);
}
loc_NTInit(void)
{
int rc;
- WORD wVersionRequested;
- WSADATA wsaData;
- wVersionRequested = MAKEWORD(2, 0);
- rc = WSAStartup(wVersionRequested, &wsaData);
+ rc = w32_socket_init();
if (rc != 0) {
- logerror("WSAStartup failed. %d", rc);
+ logerror("WSAStartup Failed, error code %d\n", rc);
exit(1);
}
}
-
-static void
-loc_NTTerm(void)
-{
- WSACleanup();
-}
#endif