Skip to content

Commit e83307d

Browse files
committed
further improve handling signals on linux
1 parent 4faf3f6 commit e83307d

File tree

4 files changed

+92
-34
lines changed

4 files changed

+92
-34
lines changed

sources/libcore/concurrent/process.cpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
#ifdef CAGE_SYSTEM_WINDOWS
77
#include "../windowsMinimumInclude.h"
8-
#include <signal.h>
8+
#include <csignal>
99
#include <psapi.h>
1010
#else
1111
#include <fcntl.h>
12-
#include <signal.h>
12+
#include <csignal>
1313
#include <sys/ioctl.h>
1414
#include <sys/stat.h>
1515
#include <sys/types.h>
@@ -546,33 +546,4 @@ namespace cage
546546
return {};
547547
return r;
548548
}
549-
550-
namespace
551-
{
552-
Delegate<void()> sigTermHandler;
553-
void sigTermHandlerEntry(int)
554-
{
555-
if (sigTermHandler)
556-
sigTermHandler();
557-
}
558-
559-
Delegate<void()> sigIntHandler;
560-
void sigIntHandlerEntry(int)
561-
{
562-
if (sigIntHandler)
563-
sigIntHandler();
564-
}
565-
}
566-
567-
void installSigTermHandler(Delegate<void()> handler)
568-
{
569-
sigTermHandler = handler;
570-
signal(SIGTERM, &sigTermHandlerEntry);
571-
}
572-
573-
void installSigIntHandler(Delegate<void()> handler)
574-
{
575-
sigIntHandler = handler;
576-
signal(SIGINT, &sigIntHandlerEntry);
577-
}
578549
}

sources/libcore/errors/crashHandlerLinux.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <ucontext.h>
1212
#include <unistd.h>
1313

14-
#include <cage-core/core.h>
14+
#include <cage-core/process.h> // installSigTermHandler
1515

1616
namespace cage
1717
{
@@ -140,9 +140,25 @@ namespace cage
140140
performPreviousHandler(sig, si, uctx);
141141
}
142142

143+
Delegate<void()> sigTermHandler;
144+
Delegate<void()> sigIntHandler;
145+
143146
void intHandler(int sig, siginfo_t *si, void *uctx)
144147
{
145148
safeWrite(Stringizer() + "signal handler: " + sigToStr(sig) + " (" + sig + ")\n");
149+
150+
switch (sig)
151+
{
152+
case SIGTERM:
153+
if (sigTermHandler)
154+
sigTermHandler();
155+
break;
156+
case SIGINT:
157+
if (sigIntHandler)
158+
sigIntHandler();
159+
break;
160+
}
161+
146162
performPreviousHandler(sig, si, uctx);
147163
}
148164

@@ -172,6 +188,16 @@ namespace cage
172188
}
173189
} setupHandlers;
174190
}
191+
192+
void installSigTermHandler(Delegate<void()> handler)
193+
{
194+
sigTermHandler = handler;
195+
}
196+
197+
void installSigIntHandler(Delegate<void()> handler)
198+
{
199+
sigIntHandler = handler;
200+
}
175201
}
176202

177203
#endif // CAGE_SYSTEM_LINUX

sources/libcore/errors/crashHandlerMacos.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifdef CAGE_SYSTEM_MAC
22

3-
#include <cage-core/core.h>
3+
#include <csignal>
4+
5+
#include <cage-core/process.h> // installSigTermHandler
46

57
namespace cage
68
{
@@ -16,6 +18,35 @@ namespace cage
1618
SetupHandlers() { crashHandlerThreadInit(); }
1719
} setupHandlers;
1820
}
21+
22+
namespace
23+
{
24+
Delegate<void()> sigTermHandler;
25+
void sigTermHandlerEntry(int)
26+
{
27+
if (sigTermHandler)
28+
sigTermHandler();
29+
}
30+
31+
Delegate<void()> sigIntHandler;
32+
void sigIntHandlerEntry(int)
33+
{
34+
if (sigIntHandler)
35+
sigIntHandler();
36+
}
37+
}
38+
39+
void installSigTermHandler(Delegate<void()> handler)
40+
{
41+
sigTermHandler = handler;
42+
signal(SIGTERM, &sigTermHandlerEntry);
43+
}
44+
45+
void installSigIntHandler(Delegate<void()> handler)
46+
{
47+
sigIntHandler = handler;
48+
signal(SIGINT, &sigIntHandlerEntry);
49+
}
1950
}
2051

2152
#endif // CAGE_SYSTEM_MAC

sources/libcore/errors/crashHandlerWindows.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#ifdef CAGE_SYSTEM_WINDOWS
22

33
#include "../windowsMinimumInclude.h"
4+
#include <csignal>
45
#include <DbgHelp.h>
56

67
#pragma comment(lib, "DbgHelp.lib")
78
#define EXCEPTION_CPP 0xE06D7363
89
#define EXCEPTION_DOTNET 0xE0434352
910
#define EXCEPTION_RENAME_THREAD 0x406D1388
1011

11-
#include <cage-core/core.h>
1212
#include <cage-core/concurrent.h>
13+
#include <cage-core/process.h> // installSigTermHandler
1314

1415
namespace cage
1516
{
@@ -295,6 +296,35 @@ namespace cage
295296
}
296297
} setupHandlers;
297298
}
299+
300+
namespace
301+
{
302+
Delegate<void()> sigTermHandler;
303+
void sigTermHandlerEntry(int)
304+
{
305+
if (sigTermHandler)
306+
sigTermHandler();
307+
}
308+
309+
Delegate<void()> sigIntHandler;
310+
void sigIntHandlerEntry(int)
311+
{
312+
if (sigIntHandler)
313+
sigIntHandler();
314+
}
315+
}
316+
317+
void installSigTermHandler(Delegate<void()> handler)
318+
{
319+
sigTermHandler = handler;
320+
signal(SIGTERM, &sigTermHandlerEntry);
321+
}
322+
323+
void installSigIntHandler(Delegate<void()> handler)
324+
{
325+
sigIntHandler = handler;
326+
signal(SIGINT, &sigIntHandlerEntry);
327+
}
298328
}
299329

300330
#endif // CAGE_SYSTEM_WINDOWS

0 commit comments

Comments
 (0)