]> git.pond.sub.org Git - empserver/blobdiff - src/server/main.c
Move -h to the end of the emp_server -h output
[empserver] / src / server / main.c
index 03c5b1535ee062ed1ed72cecef13d48b8e4527d6..a53a4c087e1dc2defd48ee1fedc00d08b0fc9b7c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *  ---
  *
  *  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-2008
+ *     Ron Koenderink, 2004-2009
+ *     Markus Armbruster, 2005-2009
  */
 
 #include <config.h>
 #include <unistd.h>
 
 #if defined(_WIN32)
-#include <winsock2.h>
-#undef NS_ALL
 #include <process.h>
 #include "service.h"
+#include "sys/socket.h"
 #endif
 
 #include "empio.h"
@@ -77,7 +76,6 @@ static void create_pidfile(char *, pid_t);
 
 #if defined(_WIN32)
 static void loc_NTInit(void);
-static void loc_NTTerm(void);
 #endif
 
 /*
@@ -86,13 +84,6 @@ static void loc_NTTerm(void);
  */
 empth_rwlock_t *play_lock;
 
-/*
- * Is a thread attempting to take an exclusive play_lock?
- * Threads holding a shared play_lock must not sleep while this is
- * true.
- */
-int play_wrlock_wanted;
-
 static char pidfname[] = "server.pid";
 
 /* Run as daemon?  If yes, detach from controlling terminal etc. */
@@ -114,7 +105,6 @@ print_usage(char *program_name)
           "  -e CONFIG-FILE  configuration file\n"
           "                  (default %s)\n"
           "  -E ACTION       what to do on oops: abort, crash-dump, nothing (default)\n"
-          "  -h              display this help and exit\n"
 #ifdef _WIN32
           "  -i              install service `%s'\n"
           "  -I NAME         install service NAME\n"
@@ -126,6 +116,7 @@ print_usage(char *program_name)
 #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
@@ -209,6 +200,8 @@ main(int argc, char **argv)
        }
     }
 
+    /* silently ignore operands for backward compatibility */
+
 #if defined(_WIN32)
     if ((!daemonize || flags || config_file != NULL) &&
        remove_service_set) {
@@ -324,13 +317,14 @@ crash_dump(void)
     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)
-       abort();                /* child */
+       raise(SIGABRT);         /* child */
 
     /* parent */
     while (waitpid(pid, &status, 0) < 0) {
@@ -381,12 +375,9 @@ start_server(int flags)
     if (journal_startup() < 0)
        exit(1);
 
-    empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
-    empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
-    empth_create(delete_lostitems, 50 * 1024, flags, "DeleteItems", 0);
-
     market_init();
     update_init();
+    empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", NULL);
 }
 
 /*
@@ -396,9 +387,6 @@ void
 finish_server(void)
 {
     ef_fin_srv();
-#if defined(_WIN32)
-    loc_NTTerm();
-#endif
     journal_shutdown();
     remove(pidfname);
 }
@@ -419,15 +407,17 @@ void
 shutdwn(int sig)
 {
     struct player *p;
+    time_t now;
+    int i, queues_drained;
 
     logerror("Shutdown commencing (cleaning up threads.)");
 
-    play_wrlock_wanted = 1;
-    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->may_sleep = PLAYER_SLEEP_NEVER;
        p->aborted++;
        if (p->command) {
            pr_flash(p, "Shutdown aborting command\n");
@@ -435,6 +425,26 @@ shutdwn(int sig)
        empth_wakeup(p->proc);
     }
     empth_rwlock_wrlock(play_lock);
+
+    now = time(NULL);
+    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");
+       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);
+    }
+
     if (sig)
        logerror("Server shutting down on signal %d", sig);
     else
@@ -453,20 +463,11 @@ static void
 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