]> git.pond.sub.org Git - empserver/blobdiff - src/server/main.c
Simplify checks whether player thread may sleep
[empserver] / src / server / main.c
index 89a74491d43f0f9f202b95cff136ff641e422841..336eb6a65d1e4c408b910b9271f4481bbdce08f8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, 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-2007
+ *     Ron Koenderink, 2004-2009
+ *     Markus Armbruster, 2005-2008
  */
 
 #include <config.h>
 #include <errno.h>
 #include <signal.h>
 #include <stdio.h>
+#ifndef _WIN32
+#include <sys/wait.h>
+#endif
+#include <unistd.h>
 
 #if defined(_WIN32)
 #include <winsock2.h>
 #include <process.h>
 #include "service.h"
 #endif
-#include <unistd.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"
@@ -67,6 +71,8 @@
 #include "ship.h"
 #include "version.h"
 
+static void ignore(void);
+static void crash_dump(void);
 static void create_pidfile(char *, pid_t);
 
 #if defined(_WIN32)
@@ -80,25 +86,27 @@ 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. */
 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"
+          "  -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"
@@ -122,6 +130,8 @@ print_usage(char *program_name)
 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;
@@ -130,26 +140,36 @@ main(int argc, char **argv)
     int remove_service_set = 0;
 #endif
     char *config_file = NULL;
-    int op, sig;
+    int op, idx, sig;
     unsigned seed = time(NULL);
 
+    oops_handler = ignore;
+
 #ifdef _WIN32
 # define XOPTS "iI:uU:"
 #else
 # define XOPTS
 #endif
-    while ((op = getopt(argc, argv, "de:hpsR:v" XOPTS)) != EOF) {
+    while ((op = getopt(argc, argv, "de:E:hpsR: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;
 #if defined(_WIN32)
        case 'I':
            service_name = optarg;
@@ -177,19 +197,19 @@ main(int argc, char **argv)
            print_usage(argv[0]);
            return EXIT_SUCCESS;
        default:
-           fprintf(stderr, "Try -h for help.\n");
+           help(argv[0], NULL);
            return EXIT_FAILURE;
        }
     }
 
 #if defined(_WIN32)
-    if ((debug || flags || config_file != NULL) &&
+    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 ((debug || flags) && install_service_set) {
+    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);
@@ -209,26 +229,12 @@ main(int argc, char **argv)
     }
 #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)) {
@@ -275,6 +281,7 @@ main(int argc, char **argv)
     }
 #endif /* !_WIN32 */
     start_server(flags);
+    journal_prng(seed);
 
     for (;;) {
        sig = empth_wait_for_signal();
@@ -283,6 +290,7 @@ main(int argc, char **argv)
            /* if you make changes here, also update relo() */
            journal_reopen();
            update_reschedule();
+           logreopen();
            continue;
        }
 #endif
@@ -295,6 +303,41 @@ main(int argc, char **argv)
     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.
@@ -332,9 +375,7 @@ 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);
+    empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", NULL);
 
     market_init();
     update_init();
@@ -373,12 +414,12 @@ shutdwn(int sig)
 
     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");