Make it more async-signal-safe

This commit is contained in:
GoldenTails 2020-12-04 12:08:50 -06:00
parent bdb28a06f4
commit a0396d5e43
1 changed files with 11 additions and 7 deletions

View File

@ -256,10 +256,11 @@ static void write_backtrace(INT32 signal)
int fd = -1;
size_t size;
time_t rawtime;
struct tm * timeinfo;
struct tm timeinfo;
enum { MAX_SIZE = 1024 };
void *array[MAX_SIZE];
enum { BT_SIZE = 1024, STR_SIZE = 32 };
void *array[BT_SIZE];
char timestr[STR_SIZE];
const char *error = "An error occurred within SRB2! Send this stack trace to someone who can help!\n";
const char *error2 = "(Or find crash-log.txt in your SRB2 directory.)\n"; // Shown only to stderr.
@ -269,8 +270,10 @@ static void write_backtrace(INT32 signal)
if (fd == -1)
I_OutputMsg("\nWARNING: Couldn't open crash log for writing! Make sure your permissions are correct. Please save the below report!\n");
// Get the current time as a string.
time(&rawtime);
timeinfo = localtime(&rawtime);
localtime_r(&rawtime, &timeinfo);
strftime(timestr, STR_SIZE, "%a, %d %b %Y %T %z", &timeinfo);
CRASHLOG_WRITE("------------------------\n"); // Nice looking seperator
@ -280,7 +283,8 @@ static void write_backtrace(INT32 signal)
// Tell the log when we crashed.
CRASHLOG_WRITE("Time of crash: ");
CRASHLOG_WRITE(asctime(timeinfo));
CRASHLOG_WRITE(timestr);
CRASHLOG_WRITE("\n");
// Give the crash log the cause and a nice 'Backtrace:' thing
// The signal is given to the user when the parent process sees we crashed.
@ -290,8 +294,8 @@ static void write_backtrace(INT32 signal)
CRASHLOG_STDERR_WRITE("\nBacktrace:\n");
// Flood the output and log with the backtrace
size = backtrace(array, MAX_SIZE);
// Flood the output and log with the backtrace
size = backtrace(array, BT_SIZE);
backtrace_symbols_fd(array, size, fd);
backtrace_symbols_fd(array, size, STDERR_FILENO);