Skip to content

Commit 5114f81

Browse files
committed
Merge #10057: [init] Deduplicated sigaction() boilerplate
81a3857 Deduplicated sigaction() boilerplate (Thomas Snider) Tree-SHA512: 705b73f285a3d76504ba01476e072fdce67731b65f309bb04e4bbd765556c37e127cb769b475de2d68b33f50d7737fb136aefa0fa7c725a11ad16a47b9d0365f
2 parents e6156a0 + 81a3857 commit 5114f81

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/init.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,31 @@ void Shutdown()
265265
}
266266

267267
/**
268-
* Signal handlers are very limited in what they are allowed to do, so:
268+
* Signal handlers are very limited in what they are allowed to do.
269+
* The execution context the handler is invoked in is not guaranteed,
270+
* so we restrict handler operations to just touching variables:
269271
*/
270-
void HandleSIGTERM(int)
272+
static void HandleSIGTERM(int)
271273
{
272274
fRequestShutdown = true;
273275
}
274276

275-
void HandleSIGHUP(int)
277+
static void HandleSIGHUP(int)
276278
{
277279
fReopenDebugLog = true;
278280
}
279281

282+
#ifndef WIN32
283+
static void registerSignalHandler(int signal, void(*handler)(int))
284+
{
285+
struct sigaction sa;
286+
sa.sa_handler = handler;
287+
sigemptyset(&sa.sa_mask);
288+
sa.sa_flags = 0;
289+
sigaction(signal, &sa, NULL);
290+
}
291+
#endif
292+
280293
bool static Bind(CConnman& connman, const CService &addr, unsigned int flags) {
281294
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
282295
return false;
@@ -848,19 +861,11 @@ bool AppInitBasicSetup()
848861
}
849862

850863
// Clean shutdown on SIGTERM
851-
struct sigaction sa;
852-
sa.sa_handler = HandleSIGTERM;
853-
sigemptyset(&sa.sa_mask);
854-
sa.sa_flags = 0;
855-
sigaction(SIGTERM, &sa, NULL);
856-
sigaction(SIGINT, &sa, NULL);
864+
registerSignalHandler(SIGTERM, HandleSIGTERM);
865+
registerSignalHandler(SIGINT, HandleSIGTERM);
857866

858867
// Reopen debug.log on SIGHUP
859-
struct sigaction sa_hup;
860-
sa_hup.sa_handler = HandleSIGHUP;
861-
sigemptyset(&sa_hup.sa_mask);
862-
sa_hup.sa_flags = 0;
863-
sigaction(SIGHUP, &sa_hup, NULL);
868+
registerSignalHandler(SIGHUP, HandleSIGHUP);
864869

865870
// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
866871
signal(SIGPIPE, SIG_IGN);

0 commit comments

Comments
 (0)