Files
itgmania212121/stepmania/src/archutils/Unix/SignalHandler.cpp
T

108 lines
2.5 KiB
C++
Raw Normal View History

2003-04-23 22:48:36 +00:00
#include "global.h"
#include "RageLog.h"
#include "SignalHandler.h"
2004-01-02 05:04:03 +00:00
#include "GetSysInfo.h"
2003-04-23 22:48:36 +00:00
#include <unistd.h>
2003-11-11 22:28:26 +00:00
#include <sys/mman.h>
#include <errno.h>
2003-04-23 22:48:36 +00:00
static vector<SignalHandler::handler> handlers;
SaveSignals *saved_sigs;
static bool initted = false;
static int signals[] = {
2003-08-19 01:32:07 +00:00
SIGALRM, SIGBUS, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGABRT, SIGPIPE,
SIGQUIT, SIGSEGV, SIGTRAP, SIGTERM, SIGVTALRM, SIGXCPU, SIGXFSZ,
2003-05-06 04:07:17 +00:00
#if defined(HAVE_DECL_SIGPWR) && HAVE_DECL_SIGPWR
SIGPWR,
#endif
2003-04-23 22:48:36 +00:00
-1
};
/*
* Store signals and restore them. This lets us clean up after libraries
* that like to mess with our signal handler. (Of course, if we undo
* signal handlers for libraries, we need to be sure to handle whatever it
* was cleaning up by hand.)
*/
SaveSignals::SaveSignals()
{
/* Store the old signal handlers. */
for(int i = 0; signals[i] != -1; ++i)
{
2003-05-06 05:40:13 +00:00
struct sigaction sa;
sigaction(signals[i], NULL, &sa);
2004-01-03 06:50:23 +00:00
old_handlers.push_back(sa);
2003-04-23 22:48:36 +00:00
}
}
SaveSignals::~SaveSignals()
{
/* Restore the old signal handlers. */
for(unsigned i = 0; i < old_handlers.size(); ++i)
sigaction(signals[i], &old_handlers[i], NULL);
}
static void SigHandler(int sig)
{
for(unsigned i = 0; i < handlers.size(); ++i)
handlers[i](sig);
}
/* Hook up events to fatal signals, so we can clean up if we're killed. */
void SignalHandler::OnClose(handler h)
{
if(saved_sigs == NULL)
{
saved_sigs = new SaveSignals;
2003-11-11 22:28:26 +00:00
/* Allocate a separate signal stack. This makes the crash handler work
* if we run out of stack space. */
2004-01-02 05:04:03 +00:00
/* Ugh. Signal stack + pthreads + Linux 2.4 = crash or hang. */
CString system;
int version;
GetKernel( system, version );
void *p = NULL;
2004-02-15 22:29:51 +00:00
const int AltStackSize = 1024*64;
if( version > 20500 )
p = mmap( NULL, AltStackSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 );
2004-01-02 05:04:03 +00:00
2003-11-11 22:28:26 +00:00
if( p == (void *) -1 )
2004-02-15 22:29:51 +00:00
p = malloc( AltStackSize );
2003-11-11 22:28:26 +00:00
if( p != NULL )
{
stack_t ss;
ss.ss_sp = p;
2004-02-15 22:29:51 +00:00
ss.ss_size = AltStackSize;
2003-11-11 22:28:26 +00:00
ss.ss_flags = 0;
2004-02-15 22:29:51 +00:00
if( sigaltstack( &ss, NULL ) == -1 )
{
LOG->Info( "sigaltstack failed: %s", strerror(errno) );
p = NULL; /* no SA_ONSTACK */
}
2003-11-11 22:28:26 +00:00
}
2003-04-23 22:48:36 +00:00
/* Set up our signal handlers. */
for(int i = 0; signals[i] != -1; ++i)
{
struct sigaction sa;
sa.sa_handler = SigHandler;
2003-11-11 22:28:26 +00:00
sa.sa_flags = p != NULL? SA_ONSTACK:0;
2003-04-23 22:48:36 +00:00
sigemptyset(&sa.sa_mask);
sigaction(signals[i], &sa, NULL);
}
}
handlers.push_back(h);
}
2003-04-23 22:49:18 +00:00
/* Written by Glenn Maynard. Public domain, copyrighting a simple signal handler
2003-04-23 22:48:36 +00:00
* is dumb. */