]> git.pond.sub.org Git - empserver/commitdiff
Move signal stuff into src/lib/empthread/, no functional change:
authorMarkus Armbruster <armbru@pond.sub.org>
Mon, 5 Jun 2006 08:51:02 +0000 (08:51 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Mon, 5 Jun 2006 08:51:02 +0000 (08:51 +0000)
(empth_init_signals): New, factored out of start_server().  Call from
empth_init().
(panic): Move to posix.c, internal linkage.
(empth_obj): Add new posix.o

(empth_start, empth_alarm) [EMPTH_POSIX]: Clean up pointless messing
with signal handlers.
(empth_init, empth_wakeup) [EMPTH_POSIX]: Clean up a bit.

Make.mk
include/empthread.h
include/prototypes.h
src/lib/empthread/lwp.c
src/lib/empthread/posix.c [new file with mode: 0644]
src/lib/empthread/pthread.c
src/server/main.c

diff --git a/Make.mk b/Make.mk
index 26e814c7a66a2a96c143586e2933087ca9d5570c..01bb64db33a6f7563e90cd0986092e07b0b82c54 100644 (file)
--- a/Make.mk
+++ b/Make.mk
@@ -113,11 +113,11 @@ info.html := $(addprefix info.html/, $(addsuffix .html, $(info)))
 
 # Conditionally generated files:
 ifeq ($(empthread),LWP)
-empth_obj := src/lib/empthread/lwp.o
+empth_obj := src/lib/empthread/lwp.o src/lib/empthread/posix.o
 empth_lib := lib/liblwp.a
 endif
 ifeq ($(empthread),POSIX)
-empth_obj := src/lib/empthread/pthread.o
+empth_obj := src/lib/empthread/pthread.o src/lib/empthread/posix.o
 empth_lib :=
 endif
 ifeq ($(empthread),Windows)
index f40d4782b57d50704c4baf51ce673ee779ff6aa8..4c952b405ea2824735e4d5095bb1e4d7190fc228 100644 (file)
@@ -211,4 +211,11 @@ void empth_sem_signal(empth_sem_t *sem);
  */
 void empth_sem_wait(empth_sem_t *sem);
 
+
+/*
+ * Stuff for implementations, not for clients.
+ */
+
+void empth_init_signals(void);
+
 #endif
index ec669930a4161722d16abf9f69c3e12cc538aa7e..e241f6dee5fc8467e90f58f28e7bdcb0c759eb26 100644 (file)
@@ -45,7 +45,6 @@
 #include "commodity.h"
 
 /* src/server/main.c */
-extern void panic(int sig);
 extern void shutdwn(int sig);
 extern void init_server(void);
 extern void start_server(int);
index 0bc79edb9aa69a26493b8db262622720f9302729..6c5cbe83a6fb4b3dfcc7b7893fc2ef6346b1ef61 100644 (file)
@@ -47,6 +47,7 @@ int
 empth_init(void **ctx, int flags)
 {
     empth_flags = flags;
+    empth_init_signals();
     empth_main = lwpInitSystem(PP_MAIN, (char **)ctx, flags);
     return 0;
 }
diff --git a/src/lib/empthread/posix.c b/src/lib/empthread/posix.c
new file mode 100644 (file)
index 0000000..5ec6643
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ *  Empire - A multi-player, client/server Internet based war game.
+ *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                           Ken Stevens, Steve McClure
+ *
+ *  This program 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
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  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
+ *
+ *  ---
+ *
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
+ *
+ *  ---
+ *
+ *  posix.c: Thread-related code common to POSIX systems
+ * 
+ *  Known contributors to this file:
+ *     Markus Armbruster, 2006
+ */
+
+#include <config.h>
+
+#include <signal.h>
+#include "empthread.h"
+#include "prototypes.h"
+
+static void panic(int sig);
+
+void
+empth_init_signals(void)
+{
+    struct sigaction act;
+
+    act.sa_flags = 0;
+    sigemptyset(&act.sa_mask);
+    act.sa_handler = shutdwn;
+    sigaction(SIGTERM, &act, NULL);
+    sigaction(SIGINT, &act, NULL);
+    act.sa_handler = panic;
+    sigaction(SIGBUS, &act, NULL);
+    sigaction(SIGSEGV, &act, NULL);
+    sigaction(SIGILL, &act, NULL);
+    sigaction(SIGFPE, &act, NULL);
+    act.sa_handler = SIG_IGN;
+    sigaction(SIGPIPE, &act, NULL);
+}
+
+/* we're going down.  try to close the files at least */
+static void
+panic(int sig)
+{
+    struct sigaction act;
+
+    act.sa_flags = 0;
+    sigemptyset(&act.sa_mask);
+    act.sa_handler = SIG_DFL;
+    sigaction(SIGBUS, &act, NULL);
+    sigaction(SIGSEGV, &act, NULL);
+    sigaction(SIGILL, &act, NULL);
+    sigaction(SIGFPE, &act, NULL);
+
+    /*
+     * This code calls functions that are not safe to call from a
+     * signal handler!  That could probably be rectified with some
+     * effort.  However, we're already in a bad state.  Is it wise to
+     * flush that state to disk, possibly overwriting good state?
+     * FIXME make the code safe as far as practical
+     */
+    logerror("server received fatal signal %d", sig);
+    log_last_commands();
+    ef_fin_srv();
+    /* End of unsafe code */
+
+    if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
+                   && sig != SIGILL && sig != SIGFPE))
+       _exit(1);
+    if (raise(sig))
+       _exit(1);
+}
index 6ada23fd8423dcfe3f95628d7b7d27be9572ca8a..045fbb2e8de781f996f52f0d0064304f92a9c493 100644 (file)
@@ -99,24 +99,6 @@ static void *
 empth_start(void *arg)
 {
     empth_t *ctx = arg;
-    struct sigaction act;
-
-    /* actually it should inherit all this from main but... */
-    act.sa_flags = 0;
-    sigemptyset(&act.sa_mask);
-    act.sa_handler = shutdwn;
-    sigaction(SIGTERM, &act, NULL);
-    sigaction(SIGINT, &act, NULL);
-    act.sa_handler = panic;
-    sigaction(SIGBUS, &act, NULL);
-    sigaction(SIGSEGV, &act, NULL);
-    sigaction(SIGILL, &act, NULL);
-    sigaction(SIGFPE, &act, NULL);
-    act.sa_handler = SIG_IGN;
-    sigaction(SIGPIPE, &act, NULL);
-
-    act.sa_handler = empth_alarm;
-    sigaction(SIGALRM, &act, NULL);
 
     ctx->id = pthread_self();
     pthread_setspecific(ctx_key, ctx);
@@ -164,15 +146,18 @@ empth_init(void **ctx_ptr, int flags)
     empth_t *ctx;
     struct sigaction act;
 
-    pthread_key_create(&ctx_key, NULL);
-    pthread_mutex_init(&mtx_ctxsw, NULL);
+    empth_flags = flags;
+    udata = ctx_ptr;
 
+    empth_init_signals();
     act.sa_flags = 0;
     sigemptyset(&act.sa_mask);
     act.sa_handler = empth_alarm;
     sigaction(SIGALRM, &act, NULL);
 
-    udata = ctx_ptr;
+    pthread_key_create(&ctx_key, NULL);
+    pthread_mutex_init(&mtx_ctxsw, NULL);
+
     ctx = malloc(sizeof(empth_t));
     if (!ctx) {
        logerror("pthread init failed: not enough memory");
@@ -186,7 +171,6 @@ empth_init(void **ctx_ptr, int flags)
     ctx->state = 0;
     pthread_setspecific(ctx_key, ctx);
     pthread_mutex_lock(&mtx_ctxsw);
-    empth_flags = flags;
     logerror("pthreads initialized");
     return 0;
 }
@@ -371,14 +355,11 @@ empth_select(int fd, int flags)
 static void
 empth_alarm(int sig)
 {
-    struct sigaction act;
+    /*
+     * Nothing to do --- we handle this signal just to let
+     * empth_wakeup() interrupt system calls.
+     */
     empth_status("got alarm signal");
-#ifdef SA_RESTART
-    act.sa_flags &= ~SA_RESTART;
-#endif
-    sigemptyset(&act.sa_mask);
-    act.sa_handler = empth_alarm;
-    sigaction(SIGALRM, &act, NULL);
 }
 
 void
@@ -386,7 +367,6 @@ empth_wakeup(empth_t *a)
 {
     empth_status("waking up thread %s", a->name);
     pthread_kill(a->id, SIGALRM);
-    empth_status("waiting for it to run");
 }
 
 void
index 3069a8ca1af88d85f8fe58b80f4bc6bf52ad110c..d0a75241696ab284cee95cb4cedbfddf628800b1 100644 (file)
@@ -25,7 +25,7 @@
  *
  *  ---
  *
- *  main.c: Thread and signal initialization for Empire Server
+ *  main.c: Empire Server main, startup and shutdown
  * 
  *  Known contributors to this file:
  *     Dave Pare, 1994
@@ -37,7 +37,6 @@
 
 #include <config.h>
 
-#include <signal.h>
 #if !defined(_WIN32)
 #include <sys/ioctl.h>
 #endif
@@ -152,7 +151,7 @@ main(int argc, char **argv)
        case 'r':
            remove_service_set++;
            break;
-#endif
+#endif /* _WIN32 */
        case 's':
            flags |= EMPTH_STACKCHECK;
            break;
@@ -185,10 +184,7 @@ main(int argc, char **argv)
            "options\n");
        exit(EXIT_FAILURE);
     }
-#endif /* _WIN32 */
 
-
-#if defined(_WIN32)
     if (remove_service_set)
         return remove_service(service_name);
     if (install_service_set) {
@@ -305,32 +301,12 @@ void
 start_server(int flags)
 {
     pid_t pid;
-#if !defined(_WIN32)
-    struct sigaction act;
-#endif
 
     pid = getpid();
     create_pidfile(pidfname, pid);
     logerror("------------------------------------------------------");
     logerror("Empire server (pid %d) started", (int)pid);
 
-#if !defined(_WIN32)
-    /* signal() should not be used with mit pthreads. Anyway if u
-       have a posix threads u definitly have posix signals -- Sasha */
-    act.sa_flags = 0;
-    sigemptyset(&act.sa_mask);
-    act.sa_handler = shutdwn;
-    sigaction(SIGTERM, &act, NULL);
-    sigaction(SIGINT, &act, NULL);
-    act.sa_handler = panic;
-    sigaction(SIGBUS, &act, NULL);
-    sigaction(SIGSEGV, &act, NULL);
-    sigaction(SIGILL, &act, NULL);
-    sigaction(SIGFPE, &act, NULL);
-    act.sa_handler = SIG_IGN;
-    sigaction(SIGPIPE, &act, NULL);
-#endif /* !_WIN32 */
-
     empth_init((void **)&player, flags);
 
     empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
@@ -378,31 +354,6 @@ create_pidfile(char *fname, pid_t pid)
     }
 }
 
-/* we're going down.  try to close the files at least */
-#if !defined(_WIN32)
-void
-panic(int sig)
-{
-    struct sigaction act;
-
-    act.sa_flags = 0;
-    sigemptyset(&act.sa_mask);
-    act.sa_handler = SIG_DFL;
-    sigaction(SIGBUS, &act, NULL);
-    sigaction(SIGSEGV, &act, NULL);
-    sigaction(SIGILL, &act, NULL);
-    sigaction(SIGFPE, &act, NULL);
-    logerror("server received fatal signal %d", sig);
-    log_last_commands();
-    ef_fin_srv();
-    if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
-                   && sig != SIGILL && sig != SIGFPE))
-       _exit(1);
-    if (raise(sig))
-       _exit(1);
-}
-#endif /* _WIN32 */
-
 void
 shutdwn(int sig)
 {