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

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