]> git.pond.sub.org Git - empserver/blobdiff - src/server/main.c
Clean up superfluous includes
[empserver] / src / server / main.c
index 022f32ab86f1fa69e6836e3f75335c169aa9bf65..44dbd626d5bdb740550cf298e81f277cdbf858ab 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, 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,
@@ -14,8 +14,7 @@
  *  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/>.
  *
  *  ---
  *
@@ -32,7 +31,7 @@
  *     Steve McClure, 1996, 1998
  *     Doug Hay, 1998
  *     Ron Koenderink, 2004-2009
- *     Markus Armbruster, 2005-2008
+ *     Markus Armbruster, 2005-2011
  */
 
 #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"
 #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)
 static void loc_NTInit(void);
-static void loc_NTTerm(void);
 #endif
 
 /*
@@ -107,7 +101,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"
@@ -119,6 +112,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
@@ -140,6 +134,7 @@ main(int argc, char **argv)
     int remove_service_set = 0;
 #endif
     char *config_file = NULL;
+    int force_bad_state = 0;
     int op, idx, sig;
     unsigned seed = time(NULL);
 
@@ -150,7 +145,7 @@ main(int argc, char **argv)
 #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;
@@ -170,6 +165,9 @@ main(int argc, char **argv)
            }
            oops_handler = oops_hndlr[idx];
            break;
+       case 'F':
+           force_bad_state = 1;
+           break;
 #if defined(_WIN32)
        case 'I':
            service_name = optarg;
@@ -202,26 +200,27 @@ main(int argc, char **argv)
        }
     }
 
+    /* silently ignore operands for backward compatibility */
+
 #if defined(_WIN32)
-    if ((!daemonize || 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 "
            "-u or -U options\n");
        exit(EXIT_FAILURE);
     }
     if ((!daemonize || flags) && install_service_set) {
-       fprintf(stderr, "Can't use -d, -p or -s with either "
-           "-i or -I options\n");
+       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 -u or -U 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)
@@ -248,7 +247,7 @@ main(int argc, char **argv)
        return install_service(program_name, service_name, config_file);
 #endif /* _WIN32 */
 
-    init_server(seed);
+    init_server(seed, force_bad_state);
 
 #if defined(_WIN32)
     if (daemonize != 0) {
@@ -342,15 +341,15 @@ crash_dump(void)
 /*
  * Initialize for serving, acquire resources.
  */
-void
-init_server(unsigned seed)
+static void
+init_server(unsigned seed, int force_bad_state)
 {
     srandom(seed);
 #if defined(_WIN32)
     loc_NTInit();
 #endif
     player_init();
-    ef_init_srv();
+    ef_init_srv(force_bad_state);
     io_init();
     init_nreport();
 
@@ -387,9 +386,6 @@ void
 finish_server(void)
 {
     ef_fin_srv();
-#if defined(_WIN32)
-    loc_NTTerm();
-#endif
     journal_shutdown();
     remove(pidfname);
 }
@@ -410,6 +406,8 @@ void
 shutdwn(int sig)
 {
     struct player *p;
+    time_t now;
+    int i;
 
     logerror("Shutdown commencing (cleaning up threads.)");
 
@@ -417,15 +415,26 @@ shutdwn(int sig)
        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_yield();
+    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
@@ -434,7 +443,7 @@ shutdwn(int sig)
 
 #if defined(_WIN32)
     if (daemonize)
-        stop_service();
+       stop_service();
 #endif
     exit(0);
 }
@@ -444,20 +453,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