mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-30 19:22:14 +00:00
Avoid signal-unsafe operations in POSIX signal handler (#16160)
This commit is contained in:
parent
56a7f0b7cf
commit
0bb87eb1ff
11 changed files with 53 additions and 42 deletions
|
@ -60,6 +60,7 @@
|
|||
#include "util/string.h"
|
||||
#include "util/tracy_wrapper.h"
|
||||
#include <vector>
|
||||
#include <csignal>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <signal.h>
|
||||
|
@ -81,31 +82,28 @@ namespace porting
|
|||
Signal handler (grabs Ctrl-C on POSIX systems)
|
||||
*/
|
||||
|
||||
static bool g_killed = false;
|
||||
volatile static std::sig_atomic_t g_killed = false;
|
||||
|
||||
bool *signal_handler_killstatus()
|
||||
volatile std::sig_atomic_t *signal_handler_killstatus()
|
||||
{
|
||||
return &g_killed;
|
||||
}
|
||||
|
||||
#if !defined(_WIN32) // POSIX
|
||||
#define STDERR_FILENO 2
|
||||
|
||||
static void signal_handler(int sig)
|
||||
{
|
||||
if (!g_killed) {
|
||||
if (sig == SIGINT) {
|
||||
dstream << "INFO: signal_handler(): "
|
||||
<< "Ctrl-C pressed, shutting down." << std::endl;
|
||||
const char *dbg_text{"INFO: signal_handler(): "
|
||||
"Ctrl-C pressed, shutting down.\n"};
|
||||
write(STDERR_FILENO, dbg_text, strlen(dbg_text));
|
||||
} else if (sig == SIGTERM) {
|
||||
dstream << "INFO: signal_handler(): "
|
||||
<< "got SIGTERM, shutting down." << std::endl;
|
||||
const char *dbg_text{"INFO: signal_handler(): "
|
||||
"got SIGTERM, shutting down.\n"};
|
||||
write(STDERR_FILENO, dbg_text, strlen(dbg_text));
|
||||
}
|
||||
|
||||
// Comment out for less clutter when testing scripts
|
||||
/*dstream << "INFO: sigint_handler(): "
|
||||
<< "Printing debug stacks" << std::endl;
|
||||
debug_stacks_print();*/
|
||||
|
||||
g_killed = true;
|
||||
} else {
|
||||
(void)signal(sig, SIG_DFL);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue