Move signal stuff into src/lib/empthread/, no functional change:

(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.
This commit is contained in:
Markus Armbruster 2006-06-05 08:51:02 +00:00
parent 1479673302
commit 32e4fbd284
7 changed files with 114 additions and 84 deletions

View file

@ -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)

View 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

View 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);

View 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;
}

92
src/lib/empthread/posix.c Normal file
View file

@ -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);
}

View 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

View 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)
{