Subject: [PATCH v3 1/4] compat/posix.h: track SA_RESTART fallback

From: "Carlo Marcelo Arenas Belón via GitGitGadget" <[email-masked]>
Date: Thu, 26 Jun 2025 08:53:05 +0000

From: =?UTF-8?q?Carlo=20Marcelo=20Arenas=20Bel=C3=B3n?= <[email-masked]>

Systems without SA_RESTART are using custom CFLAGS or headers
instead of the standard header file.

Correct that, and invent a Makefile variable to track the
exceptions which will become handy in the next commits.

Signed-off-by: Carlo Marcelo Arenas Belón <[email-masked]>
---
 Makefile             |  5 +++++
 compat/mingw-posix.h |  1 -
 compat/posix.h       |  8 ++++++++
 config.mak.uname     |  7 ++++---
 configure.ac         | 16 ++++++++++++++++
 meson.build          |  4 ++++
 6 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index 70d1543b6b86..f0839356199c 100644
--- a/Makefile
+++ b/Makefile
@@ -37,6 +37,8 @@ include shared.mak
 # when attempting to read from an fopen'ed directory (or even to fopen
 # it at all).
 #
+# Define NO_RESTARTABLE_SIGNALS if don't have support for SA_RESTART
+#
 # Define OPEN_RETURNS_EINTR if your open() system call may return EINTR
 # when a signal is received (as opposed to restarting).
 #
@@ -1811,6 +1813,9 @@ ifdef FREAD_READS_DIRECTORIES
 	COMPAT_CFLAGS += -DFREAD_READS_DIRECTORIES
 	COMPAT_OBJS += compat/fopen.o
 endif
+ifdef NO_RESTARTABLE_SIGNALS
+	COMPAT_CFLAGS += -DNO_RESTARTABLE_SIGNALS
+endif
 ifdef OPEN_RETURNS_EINTR
 	COMPAT_CFLAGS += -DOPEN_RETURNS_EINTR
 endif
diff --git a/compat/mingw-posix.h b/compat/mingw-posix.h
index 88e0cf92924b..a0dca756d104 100644
--- a/compat/mingw-posix.h
+++ b/compat/mingw-posix.h
@@ -95,7 +95,6 @@ struct sigaction {
 	sig_handler_t sa_handler;
 	unsigned sa_flags;
 };
-#define SA_RESTART 0
 
 struct itimerval {
 	struct timeval it_value, it_interval;
diff --git a/compat/posix.h b/compat/posix.h
index 067a00f33b83..b514e67902d2 100644
--- a/compat/posix.h
+++ b/compat/posix.h
@@ -250,6 +250,14 @@ char *gitdirname(char *);
 #define NAME_MAX 255
 #endif
 
+/*
+ * On most systems <signal.h> would have given us this, but
+ * not on some systems (e.g. NonStop, QNX).
+ */
+#ifndef SA_RESTART
+# define SA_RESTART 0 /* disabled for sigaction() */
+#endif
+
 typedef uintmax_t timestamp_t;
 #define PRItime PRIuMAX
 #define parse_timestamp strtoumax
diff --git a/config.mak.uname b/config.mak.uname
index b1c5c4d5e8ed..435078dc620b 100644
--- a/config.mak.uname
+++ b/config.mak.uname
@@ -486,6 +486,7 @@ ifeq ($(uname_S),Windows)
 	NO_STRTOUMAX = YesPlease
 	NO_MKDTEMP = YesPlease
 	NO_INTTYPES_H = YesPlease
+	NO_RESTARTABLE_SIGNALS = YesPlease
 	CSPRNG_METHOD = rtlgenrandom
 	# VS2015 with UCRT claims that snprintf and friends are C99 compliant,
 	# so we don't need this:
@@ -654,8 +655,6 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
 	FREAD_READS_DIRECTORIES = UnfortunatelyYes
 
 	# Not detected (nor checked for) by './configure'.
-	# We don't have SA_RESTART on NonStop, unfortunalety.
-	COMPAT_CFLAGS += -DSA_RESTART=0
 	# Apparently needed in compat/fnmatch/fnmatch.c.
 	COMPAT_CFLAGS += -DHAVE_STRING_H=1
 	NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
@@ -664,6 +663,7 @@ ifeq ($(uname_S),NONSTOP_KERNEL)
 	NO_MMAP = YesPlease
 	NO_POLL = YesPlease
 	NO_INTPTR_T = UnfortunatelyYes
+	NO_RESTARTABLE_SIGNALS = UnfortunatelyYes
 	CSPRNG_METHOD = openssl
 	SANE_TOOL_PATH = /usr/coreutils/bin:/usr/local/bin
 	SHELL_PATH = /usr/coreutils/bin/bash
@@ -699,6 +699,7 @@ ifeq ($(uname_S),MINGW)
 	NEEDS_LIBICONV = YesPlease
 	NO_STRTOUMAX = YesPlease
 	NO_MKDTEMP = YesPlease
+	NO_RESTARTABLE_SIGNALS = YesPlease
 	NO_SVN_TESTS = YesPlease
 
 	# The builtin FSMonitor requires Named Pipes and Threads on Windows.
@@ -782,7 +783,6 @@ ifeq ($(uname_S),MINGW)
         endif
 endif
 ifeq ($(uname_S),QNX)
-	COMPAT_CFLAGS += -DSA_RESTART=0
 	EXPAT_NEEDS_XMLPARSE_H = YesPlease
 	HAVE_STRINGS_H = YesPlease
 	NEEDS_SOCKET = YesPlease
@@ -794,4 +794,5 @@ ifeq ($(uname_S),QNX)
 	NO_PTHREADS = YesPlease
 	NO_STRCASESTR = YesPlease
 	NO_STRLCPY = YesPlease
+	NO_RESTARTABLE_SIGNALS = UnfortunatelyYes
 endif
diff --git a/configure.ac b/configure.ac
index f6caab919a3e..631ca67dd1aa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -862,6 +862,22 @@ fi
 GIT_CONF_SUBST([ICONV_OMITS_BOM])
 fi
 
+# Define NO_RESTARTABLE_SIGNALS if don't have support for SA_RESTART
+
+AC_CACHE_CHECK([whether SA_RESTART is supported], [ac_cv_siginterrupt], [
+	AC_COMPILE_IFELSE(
+		[AC_LANG_PROGRAM([#include <signal.h>], [[
+			#ifdef SA_RESTART
+			restartable signals supported
+			#endif
+		]])],[
+			ac_cv_siginterrupt=no
+			NO_RESTARTABLE_SIGNALS=UnfortunatelyYes
+		], [ac_cv_siginterrupt=yes]
+	)
+])
+GIT_CONF_SUBST([NO_RESTARTABLE_SIGNALS])
+
 ## Checks for typedefs, structures, and compiler characteristics.
 AC_MSG_NOTICE([CHECKS for typedefs, structures, and compiler characteristics])
 #
diff --git a/meson.build b/meson.build
index 7fea4a34d684..b6711393a5fa 100644
--- a/meson.build
+++ b/meson.build
@@ -1094,6 +1094,10 @@ else
   build_options_config.set('NO_EXPAT', '1')
 endif
 
+if compiler.get_define('SA_RESTART', prefix: '#include <signal.h>') == ''
+  libgit_c_args += '-DNO_RESTARTABLE_SIGNALS'
+endif
+
 if not compiler.has_header('sys/select.h')
   libgit_c_args += '-DNO_SYS_SELECT_H'
 endif
-- 
gitgitgadget