From dd215e8994457915f9c8c811c8dc76a7da5d594a Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Sat, 28 Mar 2020 19:14:48 +0000 Subject: [PATCH] Use `SA_RESTART` in `sa_flags` when setting up signal handlers. Explanation: if a signal interrupts certain syscalls such as `open`, `read`, or `write`, then the library function will by default fail with `errno` `EINTR`. But we almost never check for `EINTR`, so this is likely to cause spurious errors. We want to restart the syscall instead, which is what `SA_RESTART` is intended to do. Since our signal handlers (defined in init.cpp) only set a flag, restarting the syscall is safe and is always the Right Thing. See and for further information. Signed-off-by: Daira Hopwood --- src/init.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 1082d1053..fa1edd034 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -873,7 +873,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) struct sigaction sa; sa.sa_handler = HandleSIGTERM; sigemptyset(&sa.sa_mask); - sa.sa_flags = 0; + sa.sa_flags = SA_RESTART; sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); @@ -882,7 +882,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) struct sigaction sa_hup; sa_hup.sa_handler = HandleSIGHUP; sigemptyset(&sa_hup.sa_mask); - sa_hup.sa_flags = 0; + sa_hup.sa_flags = SA_RESTART; sigaction(SIGHUP, &sa_hup, NULL); // Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly