From 6870c0c1ab864836e91da8250cc12a610f3ef533 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 24 Dec 2023 14:06:26 +0100 Subject: [PATCH] Fix fork/exec and allow vfork Signed-off-by: falkTX --- modules/juce_core/native/juce_Files_linux.cpp | 10 +++++-- modules/juce_core/native/juce_Files_mac.mm | 14 ++++++---- .../juce_core/native/juce_SharedCode_posix.h | 28 +++++++++++-------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/modules/juce_core/native/juce_Files_linux.cpp b/modules/juce_core/native/juce_Files_linux.cpp index 03fa3eaabd..ff16ec55c6 100644 --- a/modules/juce_core/native/juce_Files_linux.cpp +++ b/modules/juce_core/native/juce_Files_linux.cpp @@ -218,15 +218,21 @@ bool Process::openDocument (const String& fileName, const String& parameters) const char* const argv[] = { "/bin/sh", "-c", cmdString.toUTF8(), nullptr }; +#if JUCE_USE_VFORK + const auto cpid = vfork(); +#else const auto cpid = fork(); +#endif if (cpid == 0) { +#if ! JUCE_USE_VFORK setsid(); +#endif // Child process - execv (argv[0], (char**) argv); - exit (0); + if (execv (argv[0], (char**) argv) < 0) + _exit (0); } return cpid >= 0; diff --git a/modules/juce_core/native/juce_Files_mac.mm b/modules/juce_core/native/juce_Files_mac.mm index ffd68758ae..48b9052a0f 100644 --- a/modules/juce_core/native/juce_Files_mac.mm +++ b/modules/juce_core/native/juce_Files_mac.mm @@ -92,15 +92,19 @@ namespace MacFileHelpers #else static bool launchExecutable (const String& pathAndArguments) { - auto cpid = fork(); + const char* const argv[4] = { "/bin/sh", "-c", pathAndArguments.toUTF8(), nullptr }; + +#if JUCE_USE_VFORK + const auto cpid = vfork(); +#else + const auto cpid = fork(); +#endif if (cpid == 0) { - const char* const argv[4] = { "/bin/sh", "-c", pathAndArguments.toUTF8(), nullptr }; - // Child process if (execve (argv[0], (char**) argv, nullptr) < 0) - exit (0); + _exit (0); } else { @@ -108,7 +112,7 @@ namespace MacFileHelpers return false; } - return true; + return cpid >= 0; } #endif } diff --git a/modules/juce_core/native/juce_SharedCode_posix.h b/modules/juce_core/native/juce_SharedCode_posix.h index cffc4cd701..e6c12460b0 100644 --- a/modules/juce_core/native/juce_SharedCode_posix.h +++ b/modules/juce_core/native/juce_SharedCode_posix.h @@ -1136,7 +1136,19 @@ public: if (pipe (pipeHandles) == 0) { - auto result = fork(); + Array argv; + + for (auto& arg : arguments) + if (arg.isNotEmpty()) + argv.add (const_cast (arg.toRawUTF8())); + + argv.add (nullptr); + +#if JUCE_USE_VFORK + const auto result = vfork(); +#else + const auto result = fork(); +#endif if (result < 0) { @@ -1145,6 +1157,7 @@ public: } else if (result == 0) { +#if ! JUCE_USE_VFORK // we're the child process.. close (pipeHandles[0]); // close the read handle @@ -1159,17 +1172,10 @@ public: dup2 (open ("/dev/null", O_WRONLY), STDERR_FILENO); close (pipeHandles[1]); +#endif - Array argv; - - for (auto& arg : arguments) - if (arg.isNotEmpty()) - argv.add (const_cast (arg.toRawUTF8())); - - argv.add (nullptr); - - execvp (exe.toRawUTF8(), argv.getRawDataPointer()); - _exit (-1); + if (execvp (exe.toRawUTF8(), argv.getRawDataPointer()) < 0) + _exit (-1); } else {