From 145ad48c0ee3a2cdd959d4eb412db43662143ce2 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 29 Sep 2023 19:09:09 +0200
Subject: [PATCH] Allow the use of posix_spawn() instead of vfork/execlp()
Backport of #7041
Co-authored-by: elahav <elahav@users.noreply.github.com>
---
include/SDL_config.h.cmake | 2 ++
src/misc/unix/SDL_sysurl.c | 13 +++++++++++++
2 files changed, 15 insertions(+)
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 7512bb1cc51e..35923d020729 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -262,6 +262,8 @@
#cmakedefine HAVE_XINPUT_GAMEPAD_EX @HAVE_XINPUT_GAMEPAD_EX@
#cmakedefine HAVE_XINPUT_STATE_EX @HAVE_XINPUT_STATE_EX@
+#cmakedefine USE_POSIX_SPAWN @USE_POSIX_SPAWN@
+
/* SDL internal assertion support */
#if @SDL_DEFAULT_ASSERT_LEVEL_CONFIGURED@
#cmakedefine SDL_DEFAULT_ASSERT_LEVEL @SDL_DEFAULT_ASSERT_LEVEL@
diff --git a/src/misc/unix/SDL_sysurl.c b/src/misc/unix/SDL_sysurl.c
index 75d692bd5543..58215824297c 100644
--- a/src/misc/unix/SDL_sysurl.c
+++ b/src/misc/unix/SDL_sysurl.c
@@ -32,6 +32,18 @@ int SDL_SYS_OpenURL(const char *url)
{
const pid_t pid1 = fork();
if (pid1 == 0) { /* child process */
+#ifdef USE_POSIX_SPAWN
+ pid_t pid2;
+ const char *args[] = { "xdg-open", url, NULL };
+ /* Clear LD_PRELOAD so Chrome opens correctly when this application is launched by Steam */
+ unsetenv("LD_PRELOAD");
+ if (posix_spawnp(&pid2, args[0], NULL, NULL, (char **)args, environ) == 0) {
+ /* Child process doesn't wait for possibly-blocking grandchild. */
+ _exit(EXIT_SUCCESS);
+ } else {
+ _exit(EXIT_FAILURE);
+ }
+#else
pid_t pid2;
/* Clear LD_PRELOAD so Chrome opens correctly when this application is launched by Steam */
unsetenv("LD_PRELOAD");
@@ -46,6 +58,7 @@ int SDL_SYS_OpenURL(const char *url)
/* Child process doesn't wait for possibly-blocking grandchild. */
_exit(EXIT_SUCCESS);
}
+#endif /* USE_POSIX_SPAWN */
} else if (pid1 < 0) {
return SDL_SetError("fork() failed: %s", strerror(errno));
} else {