/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure, Markus Armbruster
*
* Empire is free software: you can redistribute it and/or modify
* Steve McClure, 1996, 1998
* Doug Hay, 1998
* Ron Koenderink, 2004-2009
- * Markus Armbruster, 2005-2009
+ * Markus Armbruster, 2005-2012
*/
#include <config.h>
#include "sys/socket.h"
#endif
+#include "chance.h"
#include "empio.h"
#include "empthread.h"
#include "file.h"
#include "journal.h"
-#include "land.h"
#include "match.h"
#include "misc.h"
-#include "nat.h"
-#include "nuke.h"
#include "optlist.h"
#include "plane.h"
#include "player.h"
#include "prototypes.h"
#include "sect.h"
#include "server.h"
-#include "ship.h"
#include "version.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)
#endif
/*
- * Lock to synchronize player threads with update and shutdown.
- * Update and shutdown takes it exclusive, commands take it shared.
+ * Lock to synchronize player threads with update.
+ * Update holds it exclusive, commands hold it shared.
*/
-empth_rwlock_t *play_lock;
+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";
int remove_service_set = 0;
#endif
char *config_file = NULL;
+ int force_bad_state = 0;
int op, idx, sig;
- unsigned seed = time(NULL);
+ unsigned seed = 0;
+ int seed_set = 0;
oops_handler = ignore;
#else
# define XOPTS
#endif
- while ((op = getopt(argc, argv, "de:E:hpsR:v" XOPTS)) != EOF) {
+ while ((op = getopt(argc, argv, "de:E:FhpsR:v" XOPTS)) != EOF) {
switch (op) {
case 'p':
flags |= EMPTH_PRINT;
}
oops_handler = oops_hndlr[idx];
break;
+ case 'F':
+ force_bad_state = 1;
+ break;
#if defined(_WIN32)
case 'I':
service_name = optarg;
break;
case 'R':
seed = strtoul(optarg, NULL, 10);
+ seed_set = 1;
break;
case 'v':
printf("%s\n\n%s", version, legal);
return install_service(program_name, service_name, config_file);
#endif /* _WIN32 */
- init_server(seed);
+ if (!seed_set)
+ seed = pick_seed();
+ 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();
/*
* Initialize for serving, acquire resources.
*/
-void
-init_server(unsigned seed)
+static void
+init_server(unsigned seed, int force_bad_state)
{
- srandom(seed);
+ seed_prng(seed);
#if defined(_WIN32)
loc_NTInit();
#endif
player_init();
- ef_init_srv();
+ ef_init_srv(force_bad_state);
io_init();
init_nreport();
+ if (journal_startup() < 0)
+ exit(1);
+ journal_prng(seed);
loginit("server");
}
empth_init((void **)&player, flags);
- if (journal_startup() < 0)
- exit(1);
+ 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);
+ empth_create(player_accept, 65536, flags, "AcceptPlayers", NULL);
}
/*
shutdwn(int sig)
{
struct player *p;
- time_t now;
- int i, queues_drained;
+ time_t now = time(NULL);
+ int i;
logerror("Shutdown commencing (cleaning up threads.)");
if (p->state != PS_PLAYING)
continue;
pr_flash(p, "Server shutting down...\n");
- p->state = PS_SHUTDOWN;
+ io_set_eof(p->iop);
+ p->aborted = 1;
p->may_sleep = PLAYER_SLEEP_NEVER;
- p->aborted++;
if (p->command) {
pr_flash(p, "Shutdown aborting command\n");
}
empth_wakeup(p->proc);
}
- empth_rwlock_wrlock(play_lock);
- now = time(NULL);
+ empth_rwlock_wrlock(shutdown_lock);
empth_yield();
- for (i = 1; i <= 3; i++) {
- queues_drained = 1;
- for (p = player_next(NULL); p; p = player_next(p)) {
- if (io_outputwaiting(p->iop))
- queues_drained = 0;
- }
- if (queues_drained)
- break;
- logerror("Waiting for player output to drain\n");
+
+ 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)) {
- if (io_outputwaiting(p->iop))
- logerror("Output for player %d lost", p->cnum);
- }
+ 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);