SDL: Check for O_CLOEXEC definition in fcntl.h at configuration time,

From 5a0ea7fb17b46df6e6fc1074c0f96bfa18851a7a Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Sat, 27 Nov 2021 19:10:10 +0300
Subject: [PATCH] Check for O_CLOEXEC definition in fcntl.h at configuration
 time,

If not available, define it internally as 0 (in SDL_internal.h).
Define it as available for macOS >= 10.7 in SDL_config_macosx.h.
---
 CMakeLists.txt              |  4 ++++
 cmake/sdlchecks.cmake       |  8 +++++++
 configure                   | 44 +++++++++++++++++++++++++++++++++----
 configure.ac                | 24 ++++++++++++++++----
 include/SDL_config.h.cmake  |  1 +
 include/SDL_config.h.in     |  6 ++---
 include/SDL_config_macosx.h |  4 ++++
 src/SDL_internal.h          |  4 ++++
 8 files changed, 84 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d0bc157ece4..9411c76e79c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -993,6 +993,10 @@ if(UNIX OR APPLE)
   endif()
 endif()
 
+if(UNIX OR APPLE OR HAIKU OR RISCOS)
+  CheckO_CLOEXEC()
+endif()
+
 if(SDL_JOYSTICK)
   if(SDL_VIRTUAL_JOYSTICK)
     set(HAVE_VIRTUAL_JOYSTICK TRUE)
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 4b1a6e000c8..32be19c2fc0 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -53,6 +53,14 @@ macro(CheckDLOPEN)
   endif()
 endmacro()
 
+macro(CheckO_CLOEXEC)
+  check_c_source_compiles("
+    #include <fcntl.h>
+    int flag = O_CLOEXEC;
+    int main(void) {
+   }" HAVE_O_CLOEXEC)
+endmacro()
+
 # Requires:
 # - n/a
 macro(CheckOSS)
diff --git a/configure b/configure
index 4f613288339..cbd5782169d 100755
--- a/configure
+++ b/configure
@@ -20233,8 +20233,7 @@ int
 main ()
 {
 
-	  EGL_DISPMANX_WINDOW_T window;
-
+          EGL_DISPMANX_WINDOW_T window;
           bcm_host_init();
 
   ;
@@ -24362,6 +24361,39 @@ $as_echo "#define SDL_LOADSO_DLOPEN 1" >>confdefs.h
     fi
 }
 
+CheckO_CLOEXEC()
+{
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for O_CLOEXEC" >&5
+$as_echo_n "checking for O_CLOEXEC... " >&6; }
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+#include <fcntl.h>
+int flag = O_CLOEXEC;
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  have_o_cloexec=yes
+else
+  have_o_cloexec=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_o_cloexec" >&5
+$as_echo "$have_o_cloexec" >&6; }
+    if test $have_o_cloexec = yes; then
+
+$as_echo "#define HAVE_O_CLOEXEC 1" >>confdefs.h
+
+    fi
+}
+
 CheckUSBHID()
 {
     case "$host" in
@@ -25055,6 +25087,7 @@ $as_echo "#define SDL_VIDEO_DRIVER_ANDROID 1" >>confdefs.h
         CheckDLOPEN
         CheckARM
         CheckNEON
+        CheckO_CLOEXEC
         CheckOSS
         CheckALSA
         CheckPipewire
@@ -25321,10 +25354,8 @@ $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
 
         # Set up the core platform files
         SOURCES="$SOURCES $srcdir/src/core/windows/*.c"
-
         SOURCES="$SOURCES $srcdir/src/misc/windows/*.c"
         have_misc=yes
-
         # Use the Windows locale APIs.
         SOURCES="$SOURCES $srcdir/src/locale/windows/*.c"
         have_locale=yes
@@ -25556,6 +25587,7 @@ fi
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckHaikuVideo
         CheckHaikuGL
         CheckPTHREAD
@@ -25623,6 +25655,7 @@ $as_echo "#define SDL_FILESYSTEM_HAIKU 1" >>confdefs.h
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckMETAL
         CheckVulkan
         CheckPTHREAD
@@ -25749,6 +25782,7 @@ $as_echo "#define SDL_VIDEO_RENDER_OGL_ES2 1" >>confdefs.h
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckCOCOA
         CheckMETAL
         CheckX11
@@ -25845,6 +25879,7 @@ $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
         CheckOffscreenVideo
         CheckInputEvents
         CheckPTHREAD
+        CheckO_CLOEXEC
 
         # Set up files for the timer library
         if test x$enable_timers = xyes; then
@@ -25940,6 +25975,7 @@ $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckOSS
         CheckPTHREAD
         CheckClockGettime
diff --git a/configure.ac b/configure.ac
index 2cc063a21ef..625681a123a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1701,8 +1701,7 @@ CheckRPI()
           #include <bcm_host.h>
           #include <EGL/eglplatform.h>
         ]], [[
-	  EGL_DISPMANX_WINDOW_T window;
-
+          EGL_DISPMANX_WINDOW_T window;
           bcm_host_init();
         ]])], [have_video_rpi=yes],[])
         AC_MSG_RESULT($have_video_rpi)
@@ -3296,6 +3295,19 @@ CheckDLOPEN()
     fi
 }
 
+CheckO_CLOEXEC()
+{
+    AC_MSG_CHECKING(for O_CLOEXEC)
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+#include <fcntl.h>
+int flag = O_CLOEXEC;
+        ]],[])], [have_o_cloexec=yes],[have_o_cloexec=no])
+    AC_MSG_RESULT($have_o_cloexec)
+    if test $have_o_cloexec = yes; then
+       AC_DEFINE(HAVE_O_CLOEXEC, 1, [ ])
+    fi
+}
+
 dnl Check for the usbhid(3) library on *BSD
 CheckUSBHID()
 {
@@ -3630,6 +3642,7 @@ case "$host" in
         CheckDLOPEN
         CheckARM
         CheckNEON
+        CheckO_CLOEXEC
         CheckOSS
         CheckALSA
         CheckPipewire
@@ -3862,10 +3875,8 @@ case "$host" in
 
         # Set up the core platform files
         SOURCES="$SOURCES $srcdir/src/core/windows/*.c"
-
         SOURCES="$SOURCES $srcdir/src/misc/windows/*.c"
         have_misc=yes
-
         # Use the Windows locale APIs.
         SOURCES="$SOURCES $srcdir/src/locale/windows/*.c"
         have_locale=yes
@@ -4014,6 +4025,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckHaikuVideo
         CheckHaikuGL
         CheckPTHREAD
@@ -4071,6 +4083,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckMETAL
         CheckVulkan
         CheckPTHREAD
@@ -4175,6 +4188,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckCOCOA
         CheckMETAL
         CheckX11
@@ -4259,6 +4273,7 @@ case "$host" in
         CheckOffscreenVideo
         CheckInputEvents
         CheckPTHREAD
+        CheckO_CLOEXEC
 
         # Set up files for the timer library
         if test x$enable_timers = xyes; then
@@ -4338,6 +4353,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckOSS
         CheckPTHREAD
         CheckClockGettime
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 46b404ee19e..38655812ae1 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -222,6 +222,7 @@
 #cmakedefine HAVE_INOTIFY_INIT 1
 #cmakedefine HAVE_INOTIFY_INIT1 1
 #cmakedefine HAVE_INOTIFY 1
+#cmakedefine HAVE_O_CLOEXEC 1
 
 /* Apple platforms might be building universal binaries, where Intel builds
    can use immintrin.h but other architectures can't. */
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index ddab098d164..9073ceac8cc 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -215,6 +215,7 @@
 #define HAVE_STDINT_H 1
 #endif /* HAVE_LIBC */
 
+#undef HAVE_O_CLOEXEC
 #undef HAVE_ALTIVEC_H
 #undef HAVE_DBUS_DBUS_H
 #undef HAVE_FCITX
@@ -234,14 +235,13 @@
 #undef HAVE_DXGI_H
 #undef HAVE_WINDOWS_GAMING_INPUT_H
 #undef HAVE_XINPUT_H
+#undef HAVE_XINPUT_GAMEPAD_EX
+#undef HAVE_XINPUT_STATE_EX
 
 #undef HAVE_MMDEVICEAPI_H
 #undef HAVE_AUDIOCLIENT_H
 #undef HAVE_SENSORSAPI_H
 
-#undef HAVE_XINPUT_GAMEPAD_EX
-#undef HAVE_XINPUT_STATE_EX
-
 /* SDL internal assertion support */
 #undef SDL_DEFAULT_ASSERT_LEVEL
 
diff --git a/include/SDL_config_macosx.h b/include/SDL_config_macosx.h
index 40c72063b9f..2a59959ab74 100644
--- a/include/SDL_config_macosx.h
+++ b/include/SDL_config_macosx.h
@@ -144,6 +144,10 @@
 # endif
 #endif
 
+#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
+#define HAVE_O_CLOEXEC 1
+#endif
+
 #define HAVE_GCC_ATOMICS 1
 
 /* Enable various audio drivers */
diff --git a/src/SDL_internal.h b/src/SDL_internal.h
index 660c24740cc..c06a4773ae6 100644
--- a/src/SDL_internal.h
+++ b/src/SDL_internal.h
@@ -51,6 +51,10 @@
 
 #include "SDL_config.h"
 
+#ifndef HAVE_O_CLOEXEC
+#define O_CLOEXEC                       0
+#endif
+
 /* A few #defines to reduce SDL2 footprint.
    Only effective when library is statically linked.
    You have to manually edit this file. */