From 30ff7d4659ed8694a5cd1419c80bf09467468f5d Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Thu, 7 Aug 2014 20:22:58 +0200 Subject: [PATCH] Make openURL call on linux non-blocking, because the spawned processes are not guaranteed to fork into the background, and enable automatic zombie cleanup to deal with that change (note: the return value is now slightly less accurate) --- src/modules/system/System.cpp | 23 ++++++++++++++++++++--- src/modules/system/System.h | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index dd74c120f..8b1367f8b 100755 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -28,6 +28,7 @@ #include //#include //#include +#include #include #elif defined(LOVE_WINDOWS) #include "common/utf8.h" @@ -41,6 +42,20 @@ namespace love namespace system { +System::System() +{ +#if defined(LOVE_LINUX) + // Enable automatic cleanup of zombie processes + struct sigaction act = {0}; + sigemptyset(&act.sa_mask); + act.sa_handler = SIG_DFL; + act.sa_flags = SA_NOCLDWAIT; + + // Requires linux 2.6 or higher, so anything remotely modern + sigaction(SIGCHLD, &act, nullptr); +#endif +} + std::string System::getOS() const { #if defined(LOVE_MACOSX) @@ -86,12 +101,14 @@ bool System::openURL(const std::string &url) const if (posix_spawnp(&pid, "xdg-open", nullptr, nullptr, const_cast(argv), environ) != 0) return false; - // Wait for xdg-open to complete (or fail.) + // Check if xdg-open already completed (or failed.) int status = 0; - if (waitpid(pid, &status, 0) == pid) + if (waitpid(pid, &status, WNOHANG) > 0) return (status == 0); else - return false; + // We can't tell what actually happens without waiting for + // the process to finish, which could take forever (literally). + return true; #elif defined(LOVE_WINDOWS) diff --git a/src/modules/system/System.h b/src/modules/system/System.h index 58eff3356..fb12b22fc 100644 --- a/src/modules/system/System.h +++ b/src/modules/system/System.h @@ -48,6 +48,7 @@ public: POWER_MAX_ENUM }; + System(); virtual ~System() {} // Implements Module.