mirror of
https://git.do.srb2.org/STJr/SRB2.git
synced 2025-01-31 05:30:48 +00:00
nix: Fork before game code and wait to catch signals and coredumps
Ditched signal_handler to avoid worrying about async-signal-safe functions. D_QuitNetGame is not called, so players whose programs are interrupted by a signal will time out from the server. Because the game runs in a child process, the window can close before the "Signal Caught" text box appears. "(core dumped)" is also included in the message if core dumping could be determined.
This commit is contained in:
parent
0ff421d001
commit
7c383e4a1f
4 changed files with 87 additions and 6 deletions
|
@ -125,6 +125,10 @@
|
|||
#define LOGMESSAGES // write message in log.txt
|
||||
#endif
|
||||
|
||||
#if (defined (__unix__) && !defined (_MSDOS)) || defined (UNIXCOMMON)
|
||||
#define NEWSIGNALHANDLER
|
||||
#endif
|
||||
|
||||
#ifdef LOGMESSAGES
|
||||
extern FILE *logstream;
|
||||
#endif
|
||||
|
|
|
@ -84,6 +84,10 @@ ticcmd_t *I_BaseTiccmd2(void);
|
|||
*/
|
||||
void I_Quit(void) FUNCNORETURN;
|
||||
|
||||
/** \brief Print a message and text box about a signal that was raised.
|
||||
*/
|
||||
void I_ReportSignal(int num, int coredumped);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
EvilForce = -1,
|
||||
|
|
|
@ -28,6 +28,11 @@
|
|||
|
||||
#include "time.h" // For log timestamps
|
||||
|
||||
#ifdef NEWSIGNALHANDLER
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SDL
|
||||
|
||||
#ifdef HAVE_TTF
|
||||
|
@ -181,6 +186,53 @@ int main(int argc, char **argv)
|
|||
#endif
|
||||
MakeCodeWritable();
|
||||
#endif
|
||||
|
||||
#ifdef NEWSIGNALHANDLER
|
||||
switch (fork())
|
||||
{
|
||||
case -1:
|
||||
I_Error(
|
||||
"Error setting up signal reporting: fork(): %s\n",
|
||||
strerror(errno)
|
||||
);
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
{
|
||||
int status;
|
||||
int signum;
|
||||
if (wait(&status) == -1)
|
||||
{
|
||||
I_Error(
|
||||
"Error setting up signal reporting: fork(): %s\n",
|
||||
strerror(errno)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (WIFSIGNALED (status))
|
||||
{
|
||||
signum = WTERMSIG (status);
|
||||
#ifdef WCOREDUMP
|
||||
I_ReportSignal(signum, WCOREDUMP (status));
|
||||
#else
|
||||
I_ReportSignal(signum, 0);
|
||||
#endif
|
||||
status = 128 + signum;
|
||||
}
|
||||
else if (WIFEXITED (status))
|
||||
{
|
||||
status = WEXITSTATUS (status);
|
||||
}
|
||||
|
||||
I_ShutdownSystem();
|
||||
exit(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif/*NEWSIGNALHANDLER*/
|
||||
|
||||
// startup SRB2
|
||||
CONS_Printf("Setting up SRB2...\n");
|
||||
D_SRB2Main();
|
||||
|
|
|
@ -229,13 +229,11 @@ SDL_bool framebuffer = SDL_FALSE;
|
|||
|
||||
UINT8 keyboard_started = false;
|
||||
|
||||
FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num)
|
||||
void I_ReportSignal(int num, int coredumped)
|
||||
{
|
||||
//static char msg[] = "oh no! back to reality!\r\n";
|
||||
const char * sigmsg;
|
||||
char sigdef[32];
|
||||
|
||||
D_QuitNetGame(); // Fix server freezes
|
||||
char msg[128];
|
||||
|
||||
switch (num)
|
||||
{
|
||||
|
@ -261,8 +259,21 @@ FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num)
|
|||
sigmsg = "SIGABRT - abnormal termination triggered by abort call";
|
||||
break;
|
||||
default:
|
||||
sprintf(sigdef,"signal number %d", num);
|
||||
sigmsg = sigdef;
|
||||
sprintf(msg,"signal number %d", num);
|
||||
if (coredumped)
|
||||
sigmsg = 0;
|
||||
else
|
||||
sigmsg = msg;
|
||||
}
|
||||
|
||||
if (coredumped)
|
||||
{
|
||||
if (sigmsg)
|
||||
sprintf(msg, "%s (core dumped)", sigmsg);
|
||||
else
|
||||
strcat(msg, " (core dumped)");
|
||||
|
||||
sigmsg = msg;
|
||||
}
|
||||
|
||||
I_OutputMsg("\nsignal_handler() error: %s\n", sigmsg);
|
||||
|
@ -270,11 +281,19 @@ FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num)
|
|||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
||||
"Signal caught",
|
||||
sigmsg, NULL);
|
||||
}
|
||||
|
||||
#ifndef NEWSIGNALHANDLER
|
||||
FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num)
|
||||
{
|
||||
D_QuitNetGame(); // Fix server freezes
|
||||
I_ReportSignal(num, 0);
|
||||
I_ShutdownSystem();
|
||||
signal(num, SIG_DFL); //default signal action
|
||||
raise(num);
|
||||
I_Quit();
|
||||
}
|
||||
#endif
|
||||
|
||||
FUNCNORETURN static ATTRNORETURN void quit_handler(int num)
|
||||
{
|
||||
|
@ -664,10 +683,12 @@ void I_StartupKeyboard (void)
|
|||
|
||||
// If these defines don't exist,
|
||||
// then compilation would have failed above us...
|
||||
#ifndef NEWSIGNALHANDLER
|
||||
signal(SIGILL , signal_handler);
|
||||
signal(SIGSEGV , signal_handler);
|
||||
signal(SIGABRT , signal_handler);
|
||||
signal(SIGFPE , signal_handler);
|
||||
#endif
|
||||
}
|
||||
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue