Skip to content

Commit

Permalink
Merge pull request dotnet/coreclr#4309 from adityamandaleeka/sigterm
Browse files Browse the repository at this point in the history
Add support for handling SIGTERM gracefully [WIP]

Commit migrated from dotnet/coreclr@597f60b
  • Loading branch information
adityamandaleeka committed Apr 27, 2016
2 parents 55f4546 + e1c210c commit c9fdfeb
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 58 deletions.
7 changes: 7 additions & 0 deletions src/coreclr/src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -6490,6 +6490,7 @@ struct PAL_SEHException

typedef VOID (PALAPI *PHARDWARE_EXCEPTION_HANDLER)(PAL_SEHException* ex);
typedef BOOL (PALAPI *PHARDWARE_EXCEPTION_SAFETY_CHECK_FUNCTION)(PCONTEXT contextRecord, PEXCEPTION_RECORD exceptionRecord);
typedef VOID (PALAPI *PTERMINATION_REQUEST_HANDLER)();
typedef DWORD (PALAPI *PGET_GCMARKER_EXCEPTION_CODE)(LPVOID ip);

PALIMPORT
Expand All @@ -6512,6 +6513,12 @@ PAL_ThrowExceptionFromContext(
IN CONTEXT* context,
IN PAL_SEHException* ex);

PALIMPORT
VOID
PALAPI
PAL_SetTerminationRequestHandler(
IN PTERMINATION_REQUEST_HANDLER terminationRequestHandler);

//
// This holder is used to indicate that a hardware
// exception should be raised as a C++ exception
Expand Down
34 changes: 33 additions & 1 deletion src/coreclr/src/pal/src/exception/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static void sigtrap_handler(int code, siginfo_t *siginfo, void *context);
static void sigbus_handler(int code, siginfo_t *siginfo, void *context);
static void sigint_handler(int code, siginfo_t *siginfo, void *context);
static void sigquit_handler(int code, siginfo_t *siginfo, void *context);
static void sigterm_handler(int code, siginfo_t *siginfo, void *context);

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

Expand All @@ -91,7 +92,7 @@ struct sigaction g_previous_sigbus;
struct sigaction g_previous_sigsegv;
struct sigaction g_previous_sigint;
struct sigaction g_previous_sigquit;

struct sigaction g_previous_sigterm;

/* public function definitions ************************************************/

Expand Down Expand Up @@ -131,6 +132,7 @@ BOOL SEHInitializeSignals()
handle_signal(SIGSEGV, sigsegv_handler, &g_previous_sigsegv);
handle_signal(SIGINT, sigint_handler, &g_previous_sigint);
handle_signal(SIGQUIT, sigquit_handler, &g_previous_sigquit);
handle_signal(SIGTERM, sigterm_handler, &g_previous_sigterm);

#ifdef INJECT_ACTIVATION_SIGNAL
handle_signal(INJECT_ACTIVATION_SIGNAL, inject_activation_handler, NULL);
Expand Down Expand Up @@ -178,6 +180,7 @@ void SEHCleanupSignals()
restore_signal(SIGSEGV, &g_previous_sigsegv);
restore_signal(SIGINT, &g_previous_sigint);
restore_signal(SIGQUIT, &g_previous_sigquit);
restore_signal(SIGTERM, &g_previous_sigterm);
}

/* internal function definitions **********************************************/
Expand Down Expand Up @@ -384,6 +387,35 @@ static void sigquit_handler(int code, siginfo_t *siginfo, void *context)
kill(gPID, code);
}

/*++
Function :
sigterm_handler
handle SIGTERM signal
Parameters :
POSIX signal handler parameter list ("man sigaction" for details)
(no return value)
--*/
static void sigterm_handler(int code, siginfo_t *siginfo, void *context)
{
if (PALIsInitialized())
{
// g_pSynchronizationManager shouldn't be null if PAL is initialized.
_ASSERTE(g_pSynchronizationManager != nullptr);

g_pSynchronizationManager->SendTerminationRequestToWorkerThread();
}
else
{
if (g_previous_sigterm.sa_sigaction != NULL)
{
g_previous_sigterm.sa_sigaction(code, siginfo, context);
}
}
}

#ifdef INJECT_ACTIVATION_SIGNAL
/*++
Function :
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/src/pal/src/include/pal/corunix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,10 @@ namespace CorUnix
CPalThread *pThread
) = 0;

virtual
PAL_ERROR
SendTerminationRequestToWorkerThread() = 0;

//
// This routine is primarily meant for use by WaitForMultipleObjects[Ex].
// The caller must individually release each of the returned controller
Expand Down
Loading

0 comments on commit c9fdfeb

Please sign in to comment.