Move oops actions from log.c up to application

Change oops() to call the new oops_handler function pointer instead of
offering a fixed set of actions.  Change server's main() to install a
handler for the action requested by -E.
This commit is contained in:
Markus Armbruster 2008-04-25 20:20:07 +02:00
parent 6709931d51
commit f55860670a
3 changed files with 24 additions and 24 deletions

View file

@ -69,13 +69,6 @@
#define hours(x) (60*60*(x)) #define hours(x) (60*60*(x))
#define days(x) (60*60*24*(x)) #define days(x) (60*60*24*(x))
enum oops_action {
OOPS_ABORT,
OOPS_CRASH_DUMP,
OOPS_NOTHING
};
extern enum oops_action oops_action;
/* /*
* If EXPR is true, an internal error occured. * If EXPR is true, an internal error occured.
* Return EXPR != 0. * Return EXPR != 0.
@ -90,6 +83,7 @@ extern enum oops_action oops_action;
#define CANT_REACH() (void)oops(NULL, __FILE__, __LINE__) #define CANT_REACH() (void)oops(NULL, __FILE__, __LINE__)
extern int oops(char *, char *, int); extern int oops(char *, char *, int);
extern void (*oops_handler)(void);
void exit_nomem(void) ATTRIBUTE((noreturn)); void exit_nomem(void) ATTRIBUTE((noreturn));

View file

@ -46,7 +46,7 @@
#include "player.h" #include "player.h"
#include "prototypes.h" #include "prototypes.h"
enum oops_action oops_action = OOPS_ABORT; void (*oops_handler)(void) = abort;
static char logfile[32]; static char logfile[32];
static int logfd = -1; static int logfd = -1;
@ -126,24 +126,14 @@ logerror(char *format, ...)
/* /*
* Log internal error MSG occured in FILE:LINE. * Log internal error MSG occured in FILE:LINE.
* If debugging, call abort(), else return 1. * Call oops handler, and if it returns, return 1.
* Oops handler defaults to abort().
*/ */
int int
oops(char *msg, char *file, int line) oops(char *msg, char *file, int line)
{ {
logerror("Oops: %s in %s:%d", msg ? msg : "bug", file, line); logerror("Oops: %s in %s:%d", msg ? msg : "bug", file, line);
switch (oops_action) { oops_handler();
case OOPS_ABORT:
abort();
case OOPS_CRASH_DUMP:
#ifndef _WIN32
if (fork() == 0)
abort();
#endif
/* fall through */
case OOPS_NOTHING:
break;
}
return 1; return 1;
} }

View file

@ -68,6 +68,8 @@
#include "ship.h" #include "ship.h"
#include "version.h" #include "version.h"
static void ignore(void);
static void crash_dump(void);
static void create_pidfile(char *, pid_t); static void create_pidfile(char *, pid_t);
#if defined(_WIN32) #if defined(_WIN32)
@ -133,6 +135,7 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL }; static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
static void (*oops_hndlr[])(void) = { abort, crash_dump, ignore };
int flags = 0; int flags = 0;
#if defined(_WIN32) #if defined(_WIN32)
int install_service_set = 0; int install_service_set = 0;
@ -144,7 +147,7 @@ main(int argc, char **argv)
int op, idx, sig; int op, idx, sig;
unsigned seed = time(NULL); unsigned seed = time(NULL);
oops_action = OOPS_NOTHING; oops_handler = ignore;
#ifdef _WIN32 #ifdef _WIN32
# define XOPTS "iI:uU:" # define XOPTS "iI:uU:"
@ -157,7 +160,7 @@ main(int argc, char **argv)
flags |= EMPTH_PRINT; flags |= EMPTH_PRINT;
/* fall through */ /* fall through */
case 'd': case 'd':
oops_action = OOPS_ABORT; oops_handler = abort;
daemonize = 0; daemonize = 0;
break; break;
case 'e': case 'e':
@ -169,7 +172,7 @@ main(int argc, char **argv)
help(argv[0], "invalid argument for -E"); help(argv[0], "invalid argument for -E");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
oops_action = idx; oops_handler = oops_hndlr[idx];
break; break;
#if defined(_WIN32) #if defined(_WIN32)
case 'I': case 'I':
@ -304,6 +307,19 @@ main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
static void
ignore(void)
{
}
static void
crash_dump(void)
{
#ifndef _WIN32
if (fork() == 0)
abort();
#endif
}
/* /*
* Initialize for serving, acquire resources. * Initialize for serving, acquire resources.