# 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)
*/
void empth_sem_wait(empth_sem_t *sem);
+
+/*
+ * Stuff for implementations, not for clients.
+ */
+
+void empth_init_signals(void);
+
#endif
#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);
empth_init(void **ctx, int flags)
{
empth_flags = flags;
+ empth_init_signals();
empth_main = lwpInitSystem(PP_MAIN, (char **)ctx, flags);
return 0;
}
--- /dev/null
+/*
+ * 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);
+}
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);
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");
ctx->state = 0;
pthread_setspecific(ctx_key, ctx);
pthread_mutex_lock(&mtx_ctxsw);
- empth_flags = flags;
logerror("pthreads initialized");
return 0;
}
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
{
empth_status("waking up thread %s", a->name);
pthread_kill(a->id, SIGALRM);
- empth_status("waiting for it to run");
}
void
*
* ---
*
- * 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
#include <config.h>
-#include <signal.h>
#if !defined(_WIN32)
#include <sys/ioctl.h>
#endif
case 'r':
remove_service_set++;
break;
-#endif
+#endif /* _WIN32 */
case 's':
flags |= EMPTH_STACKCHECK;
break;
"options\n");
exit(EXIT_FAILURE);
}
-#endif /* _WIN32 */
-
-#if defined(_WIN32)
if (remove_service_set)
return remove_service(service_name);
if (install_service_set) {
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,
}
}
-/* 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)
{