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

Commit 4d6398e

Browse files
Koundinya Veluridanmoseley
authored andcommitted
Handle SIGTERM on OSX (#10723)
* Handle SIGTERM on OSX Fixes #7394 * Address feedback
1 parent 2024852 commit 4d6398e

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/pal/src/exception/seh.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ Return value :
111111
BOOL
112112
SEHInitialize (CPalThread *pthrCurrent, DWORD flags)
113113
{
114-
#if !HAVE_MACH_EXCEPTIONS
115114
if (!SEHInitializeSignals(flags))
116115
{
117116
ERROR("SEHInitializeSignals failed!\n");
118117
SEHCleanup();
119118
return FALSE;
120119
}
121-
#endif
122120

123121
return TRUE;
124122
}
@@ -142,9 +140,8 @@ SEHCleanup()
142140

143141
#if HAVE_MACH_EXCEPTIONS
144142
SEHCleanupExceptionPort();
145-
#else
146-
SEHCleanupSignals();
147143
#endif
144+
SEHCleanupSignals();
148145
}
149146

150147
/*++

src/pal/src/exception/signal.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ SET_DEFAULT_DEBUG_CHANNEL(EXCEPT); // some headers have code with asserts, so do
3030
#include "pal/signal.hpp"
3131

3232
#include "pal/palinternal.h"
33+
34+
#include <errno.h>
35+
#include <signal.h>
36+
3337
#if !HAVE_MACH_EXCEPTIONS
3438
#include "pal/init.h"
3539
#include "pal/process.h"
3640
#include "pal/debug.h"
3741
#include "pal/virtual.h"
3842
#include "pal/utils.h"
3943

40-
#include <signal.h>
41-
#include <errno.h>
4244
#include <string.h>
4345
#include <sys/ucontext.h>
4446
#include <sys/utsname.h>
@@ -47,15 +49,16 @@ SET_DEFAULT_DEBUG_CHANNEL(EXCEPT); // some headers have code with asserts, so do
4749

4850
#include "pal/context.h"
4951

50-
using namespace CorUnix;
51-
5252
#ifdef SIGRTMIN
5353
#define INJECT_ACTIVATION_SIGNAL SIGRTMIN
5454
#endif
5555

5656
#if !defined(INJECT_ACTIVATION_SIGNAL) && defined(FEATURE_HIJACK)
5757
#error FEATURE_HIJACK requires INJECT_ACTIVATION_SIGNAL to be defined
5858
#endif
59+
#endif // !HAVE_MACH_EXCEPTIONS
60+
61+
using namespace CorUnix;
5962

6063
/* local type definitions *****************************************************/
6164

@@ -67,45 +70,50 @@ typedef void *siginfo_t;
6770
#endif /* !HAVE_SIGINFO_T */
6871
typedef void (*SIGFUNC)(int, siginfo_t *, void *);
6972

73+
#if !HAVE_MACH_EXCEPTIONS
7074
// Return context and status for the signal_handler_worker.
7175
struct SignalHandlerWorkerReturnPoint
7276
{
7377
bool returnFromHandler;
7478
CONTEXT context;
7579
};
80+
#endif // !HAVE_MACH_EXCEPTIONS
7681

7782
/* internal function declarations *********************************************/
7883

84+
static void sigterm_handler(int code, siginfo_t *siginfo, void *context);
85+
#if !HAVE_MACH_EXCEPTIONS
7986
static void sigill_handler(int code, siginfo_t *siginfo, void *context);
8087
static void sigfpe_handler(int code, siginfo_t *siginfo, void *context);
8188
static void sigsegv_handler(int code, siginfo_t *siginfo, void *context);
8289
static void sigtrap_handler(int code, siginfo_t *siginfo, void *context);
8390
static void sigbus_handler(int code, siginfo_t *siginfo, void *context);
8491
static void sigint_handler(int code, siginfo_t *siginfo, void *context);
8592
static void sigquit_handler(int code, siginfo_t *siginfo, void *context);
86-
static void sigterm_handler(int code, siginfo_t *siginfo, void *context);
8793

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

9096
#ifdef INJECT_ACTIVATION_SIGNAL
9197
static void inject_activation_handler(int code, siginfo_t *siginfo, void *context);
9298
#endif
99+
#endif // !HAVE_MACH_EXCEPTIONS
93100

94101
static void handle_signal(int signal_id, SIGFUNC sigfunc, struct sigaction *previousAction, int additionalFlags = 0);
95102
static void restore_signal(int signal_id, struct sigaction *previousAction);
96103

97104
/* internal data declarations *********************************************/
98105

106+
static bool registered_sigterm_handler = false;
107+
108+
struct sigaction g_previous_sigterm;
109+
#if !HAVE_MACH_EXCEPTIONS
99110
struct sigaction g_previous_sigill;
100111
struct sigaction g_previous_sigtrap;
101112
struct sigaction g_previous_sigfpe;
102113
struct sigaction g_previous_sigbus;
103114
struct sigaction g_previous_sigsegv;
104115
struct sigaction g_previous_sigint;
105116
struct sigaction g_previous_sigquit;
106-
struct sigaction g_previous_sigterm;
107-
108-
static bool registered_sigterm_handler = false;
109117

110118
#ifdef INJECT_ACTIVATION_SIGNAL
111119
struct sigaction g_previous_activation;
@@ -114,9 +122,11 @@ struct sigaction g_previous_activation;
114122
// Offset of the local variable containing native context in the common_signal_handler function.
115123
// This offset is relative to the frame pointer.
116124
int g_common_signal_handler_context_locvar_offset = 0;
125+
#endif // !HAVE_MACH_EXCEPTIONS
117126

118127
/* public function definitions ************************************************/
119128

129+
#if !HAVE_MACH_EXCEPTIONS
120130
/*++
121131
Function :
122132
EnsureSignalAlternateStack
@@ -198,6 +208,7 @@ void FreeSignalAlternateStack()
198208
free(oss.ss_sp);
199209
}
200210
}
211+
#endif // !HAVE_MACH_EXCEPTIONS
201212

202213
/*++
203214
Function :
@@ -215,6 +226,7 @@ BOOL SEHInitializeSignals(DWORD flags)
215226
{
216227
TRACE("Initializing signal handlers\n");
217228

229+
#if !HAVE_MACH_EXCEPTIONS
218230
/* we call handle_signal for every possible signal, even
219231
if we don't provide a signal handler.
220232
@@ -241,13 +253,15 @@ BOOL SEHInitializeSignals(DWORD flags)
241253
{
242254
return FALSE;
243255
}
256+
#endif // !HAVE_MACH_EXCEPTIONS
244257

245258
if (flags & PAL_INITIALIZE_REGISTER_SIGTERM_HANDLER)
246259
{
247260
handle_signal(SIGTERM, sigterm_handler, &g_previous_sigterm);
248261
registered_sigterm_handler = true;
249262
}
250263

264+
#if !HAVE_MACH_EXCEPTIONS
251265
#ifdef INJECT_ACTIVATION_SIGNAL
252266
handle_signal(INJECT_ACTIVATION_SIGNAL, inject_activation_handler, &g_previous_activation);
253267
#endif
@@ -261,6 +275,7 @@ BOOL SEHInitializeSignals(DWORD flags)
261275
issued a SIGPIPE will, instead, report an error and set errno to EPIPE.
262276
*/
263277
signal(SIGPIPE, SIG_IGN);
278+
#endif // !HAVE_MACH_EXCEPTIONS
264279

265280
return TRUE;
266281
}
@@ -285,26 +300,31 @@ void SEHCleanupSignals()
285300
{
286301
TRACE("Restoring default signal handlers\n");
287302

303+
#if !HAVE_MACH_EXCEPTIONS
288304
restore_signal(SIGILL, &g_previous_sigill);
289305
restore_signal(SIGTRAP, &g_previous_sigtrap);
290306
restore_signal(SIGFPE, &g_previous_sigfpe);
291307
restore_signal(SIGBUS, &g_previous_sigbus);
292308
restore_signal(SIGSEGV, &g_previous_sigsegv);
293309
restore_signal(SIGINT, &g_previous_sigint);
294310
restore_signal(SIGQUIT, &g_previous_sigquit);
311+
#endif // !HAVE_MACH_EXCEPTIONS
295312

296313
if (registered_sigterm_handler)
297314
{
298315
restore_signal(SIGTERM, &g_previous_sigterm);
299316
}
300317

318+
#if !HAVE_MACH_EXCEPTIONS
301319
#ifdef INJECT_ACTIVATION_SIGNAL
302320
restore_signal(INJECT_ACTIVATION_SIGNAL, &g_previous_activation);
303321
#endif
322+
#endif // !HAVE_MACH_EXCEPTIONS
304323
}
305324

306325
/* internal function definitions **********************************************/
307326

327+
#if !HAVE_MACH_EXCEPTIONS
308328
/*++
309329
Function :
310330
sigill_handler
@@ -571,6 +591,7 @@ static void sigquit_handler(int code, siginfo_t *siginfo, void *context)
571591
restore_signal(code, &g_previous_sigquit);
572592
kill(gPID, code);
573593
}
594+
#endif // !HAVE_MACH_EXCEPTIONS
574595

575596
/*++
576597
Function :
@@ -601,6 +622,7 @@ static void sigterm_handler(int code, siginfo_t *siginfo, void *context)
601622
}
602623
}
603624

625+
#if !HAVE_MACH_EXCEPTIONS
604626
#ifdef INJECT_ACTIVATION_SIGNAL
605627
/*++
606628
Function :
@@ -799,6 +821,7 @@ static bool common_signal_handler(int code, siginfo_t *siginfo, void *sigcontext
799821

800822
return false;
801823
}
824+
#endif // !HAVE_MACH_EXCEPTIONS
802825

803826
/*++
804827
Function :
@@ -856,5 +879,3 @@ void restore_signal(int signal_id, struct sigaction *previousAction)
856879
errno, strerror(errno));
857880
}
858881
}
859-
860-
#endif // !HAVE_MACH_EXCEPTIONS

src/pal/src/include/pal/signal.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Return :
111111
--*/
112112
void FreeSignalAlternateStack();
113113

114+
#endif // !HAVE_MACH_EXCEPTIONS
115+
114116
/*++
115117
Function :
116118
SEHInitializeSignals
@@ -135,7 +137,4 @@ Function :
135137
--*/
136138
void SEHCleanupSignals();
137139

138-
#endif // !HAVE_MACH_EXCEPTIONS
139-
140140
#endif /* _PAL_SIGNAL_HPP_ */
141-

0 commit comments

Comments
 (0)