Switch from vfork/exec to posix_spawn after clang static analyzer run

This commit is contained in:
Bart van Strien
2014-05-06 19:48:50 +02:00
parent d705b54198
commit ce385ec27a
+22 -28
View File
@@ -25,8 +25,9 @@
#if defined(LOVE_MACOSX) #if defined(LOVE_MACOSX)
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
#elif defined(LOVE_LINUX) #elif defined(LOVE_LINUX)
#include <stdlib.h> #include <spawn.h>
#include <unistd.h> //#include <stdlib.h>
//#include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#elif defined(LOVE_WINDOWS) #elif defined(LOVE_WINDOWS)
#include "common/utf8.h" #include "common/utf8.h"
@@ -53,12 +54,17 @@ std::string System::getOS() const
#endif #endif
} }
extern "C"
{
extern char **environ; // The environment, always available
}
bool System::openURL(const std::string &url) const bool System::openURL(const std::string &url) const
{ {
bool success = false;
#if defined(LOVE_MACOSX) #if defined(LOVE_MACOSX)
bool success = false;
// We could be lazy and use system("open " + url), but this is safer. // We could be lazy and use system("open " + url), but this is safer.
CFURLRef cfurl = CFURLCreateWithBytes(nullptr, CFURLRef cfurl = CFURLCreateWithBytes(nullptr,
(const UInt8 *) url.c_str(), (const UInt8 *) url.c_str(),
@@ -68,34 +74,24 @@ bool System::openURL(const std::string &url) const
success = LSOpenCFURLRef(cfurl, nullptr) == noErr; success = LSOpenCFURLRef(cfurl, nullptr) == noErr;
CFRelease(cfurl); CFRelease(cfurl);
return success;
#elif defined(LOVE_LINUX) #elif defined(LOVE_LINUX)
// Spawn a child process, which we'll replace with xdg-open. pid_t pid;
pid_t pid = vfork(); const char *argv[] = {"xdg-open", url.c_str(), nullptr};
if (pid == 0) // Child process. // Note: at the moment this process inherits our file descriptors.
{ // Note: the below const_cast is really ugly as well.
// Replace the child process with xdg-open and pass in the URL. if (posix_spawnp(&pid, "xdg-open", nullptr, nullptr, const_cast<char **>(argv), environ) != 0)
execlp("xdg-open", "xdg-open", url.c_str(), nullptr); return false;
// exec will only return if it errored, so we should exit with non-zero. // Wait for xdg-open to complete (or fail.)
_exit(1); int status = 0;
} if (waitpid(pid, &status, 0) == pid)
else if (pid > 0) // Parent process. return (status == 0);
{
// Wait for xdg-open to complete (or fail.)
int status = 0;
if (waitpid(pid, &status, 0) == pid)
success = (status == 0);
else
success = false;
}
else else
{ return false;
// vfork() failed.
success = false;
}
#elif defined(LOVE_WINDOWS) #elif defined(LOVE_WINDOWS)
@@ -109,11 +105,9 @@ bool System::openURL(const std::string &url) const
nullptr, nullptr,
SW_SHOW); SW_SHOW);
success = (int) result > 32; return (int) result > 32;
#endif #endif
return success;
} }
bool System::getConstant(const char *in, System::PowerState &out) bool System::getConstant(const char *in, System::PowerState &out)