Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a785c40

Browse files
Add SIGTERM handling logic that properly shuts down the EE.
1 parent de32aed commit a785c40

File tree

6 files changed

+178
-58
lines changed

6 files changed

+178
-58
lines changed

src/pal/inc/pal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6490,6 +6490,7 @@ struct PAL_SEHException
64906490

64916491
typedef VOID (PALAPI *PHARDWARE_EXCEPTION_HANDLER)(PAL_SEHException* ex);
64926492
typedef BOOL (PALAPI *PHARDWARE_EXCEPTION_SAFETY_CHECK_FUNCTION)(PCONTEXT contextRecord, PEXCEPTION_RECORD exceptionRecord);
6493+
typedef VOID (PALAPI *PTERMINATION_REQUEST_HANDLER)();
64936494
typedef DWORD (PALAPI *PGET_GCMARKER_EXCEPTION_CODE)(LPVOID ip);
64946495

64956496
PALIMPORT
@@ -6512,6 +6513,12 @@ PAL_ThrowExceptionFromContext(
65126513
IN CONTEXT* context,
65136514
IN PAL_SEHException* ex);
65146515

6516+
PALIMPORT
6517+
VOID
6518+
PALAPI
6519+
PAL_SetTerminationRequestHandler(
6520+
IN PTERMINATION_REQUEST_HANDLER terminationRequestHandler);
6521+
65156522
//
65166523
// This holder is used to indicate that a hardware
65176524
// exception should be raised as a C++ exception

src/pal/src/exception/signal.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static void sigtrap_handler(int code, siginfo_t *siginfo, void *context);
7272
static void sigbus_handler(int code, siginfo_t *siginfo, void *context);
7373
static void sigint_handler(int code, siginfo_t *siginfo, void *context);
7474
static void sigquit_handler(int code, siginfo_t *siginfo, void *context);
75+
static void sigterm_handler(int code, siginfo_t *siginfo, void *context);
7576

7677
static void common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext, int numParams, ...);
7778

@@ -91,7 +92,7 @@ struct sigaction g_previous_sigbus;
9192
struct sigaction g_previous_sigsegv;
9293
struct sigaction g_previous_sigint;
9394
struct sigaction g_previous_sigquit;
94-
95+
struct sigaction g_previous_sigterm;
9596

9697
/* public function definitions ************************************************/
9798

@@ -131,6 +132,7 @@ BOOL SEHInitializeSignals()
131132
handle_signal(SIGSEGV, sigsegv_handler, &g_previous_sigsegv);
132133
handle_signal(SIGINT, sigint_handler, &g_previous_sigint);
133134
handle_signal(SIGQUIT, sigquit_handler, &g_previous_sigquit);
135+
handle_signal(SIGTERM, sigterm_handler, &g_previous_sigterm);
134136

135137
#ifdef INJECT_ACTIVATION_SIGNAL
136138
handle_signal(INJECT_ACTIVATION_SIGNAL, inject_activation_handler, NULL);
@@ -178,6 +180,7 @@ void SEHCleanupSignals()
178180
restore_signal(SIGSEGV, &g_previous_sigsegv);
179181
restore_signal(SIGINT, &g_previous_sigint);
180182
restore_signal(SIGQUIT, &g_previous_sigquit);
183+
restore_signal(SIGTERM, &g_previous_sigterm);
181184
}
182185

183186
/* internal function definitions **********************************************/
@@ -384,6 +387,35 @@ static void sigquit_handler(int code, siginfo_t *siginfo, void *context)
384387
kill(gPID, code);
385388
}
386389

390+
/*++
391+
Function :
392+
sigterm_handler
393+
394+
handle SIGTERM signal
395+
396+
Parameters :
397+
POSIX signal handler parameter list ("man sigaction" for details)
398+
399+
(no return value)
400+
--*/
401+
static void sigterm_handler(int code, siginfo_t *siginfo, void *context)
402+
{
403+
if (PALIsInitialized())
404+
{
405+
// g_pSynchronizationManager shouldn't be null if PAL is initialized.
406+
_ASSERTE(g_pSynchronizationManager != nullptr);
407+
408+
g_pSynchronizationManager->SendTerminationRequestToWorkerThread();
409+
}
410+
else
411+
{
412+
if (g_previous_sigterm.sa_sigaction != NULL)
413+
{
414+
g_previous_sigterm.sa_sigaction(code, siginfo, context);
415+
}
416+
}
417+
}
418+
387419
#ifdef INJECT_ACTIVATION_SIGNAL
388420
/*++
389421
Function :

src/pal/src/include/pal/corunix.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,10 @@ namespace CorUnix
11241124
CPalThread *pThread
11251125
) = 0;
11261126

1127+
virtual
1128+
PAL_ERROR
1129+
SendTerminationRequestToWorkerThread() = 0;
1130+
11271131
//
11281132
// This routine is primarily meant for use by WaitForMultipleObjects[Ex].
11291133
// The caller must individually release each of the returned controller

0 commit comments

Comments
 (0)