Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/windows/systemw.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ void MTY_SetCrashFunc(MTY_CrashFunc func, void *opaque)
SYSTEM_OPAQUE = opaque;
}

static BOOL WINAPI system_handler_routine(DWORD dwCtrlType)
{
// Console close == SIGTERM, Ctrl+C == SIGINT
if (SYSTEM_CRASH_FUNC)
SYSTEM_CRASH_FUNC(dwCtrlType == CTRL_CLOSE_EVENT || dwCtrlType == CTRL_C_EVENT, SYSTEM_OPAQUE);

// Prevent secondary calls to the SYSTEM_CRASH_FUNC
SYSTEM_CRASH_FUNC = NULL;
Comment on lines +235 to +240

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you need to protect against multi-threaded scenarios, but perhaps you should do an atomic exchange of the crash func pointer into a local variable while setting the static variable to NULL.


// For all except CTRL_LOGOFF_EVENT, exit the process with code 1
if (dwCtrlType != CTRL_LOGOFF_EVENT)
ExitProcess(1);

// Prevent additional handlers from running, except for CTRL_LOGOFF_EVENT, which doesn't necessarily mean THIS user is logging off
return dwCtrlType != CTRL_LOGOFF_EVENT;
}

void MTY_OpenConsole(const char *title)
{
HWND console = GetConsoleWindow();
Expand Down Expand Up @@ -262,6 +279,9 @@ void MTY_OpenConsole(const char *title)
SetConsoleTitle(titlew);

MTY_Free(titlew);

// Add Ctrl handler
SetConsoleCtrlHandler(system_handler_routine, TRUE);
}
}

Expand Down