Bug 1442791 - Try avoiding /proc on BSDs in GetExecutablePathImpl(). r?jgilbert draft
authorJan Beich <jbeich@FreeBSD.org>
Sat, 03 Mar 2018 20:23:58 +0000
changeset 762904 59aa62bdb2a0a17af0f488defaadcf717c0fe89e
parent 762903 c9b4dd5e62a33127026a7814016184be49d8ca85
push id101286
push userbmo:jbeich@FreeBSD.org
push dateSat, 03 Mar 2018 21:49:33 +0000
reviewersjgilbert
bugs1442791
milestone60.0a1
Bug 1442791 - Try avoiding /proc on BSDs in GetExecutablePathImpl(). r?jgilbert MozReview-Commit-ID: 7dz00BQAQMo
gfx/angle/checkout/src/common/system_utils_linux.cpp
--- a/gfx/angle/checkout/src/common/system_utils_linux.cpp
+++ b/gfx/angle/checkout/src/common/system_utils_linux.cpp
@@ -10,22 +10,48 @@
 
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include <array>
 
+#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
+#include <sys/sysctl.h>
+#include <limits.h>
+#endif
+
 namespace angle
 {
 
 namespace
 {
 
+#if defined(KERN_PROC_PATHNAME)
+std::string GetExecutablePathImpl()
+{
+    char path[PATH_MAX + 1];
+    size_t path_len = sizeof(path);
+    int mib[] = {
+        CTL_KERN,
+#if defined(__NetBSD__)
+        KERN_PROC_ARGS,
+        -1,
+        KERN_PROC_PATHNAME,
+#else
+        KERN_PROC,
+        KERN_PROC_PATHNAME,
+        -1,
+#endif
+    };
+    int result = sysctl(mib, ArraySize(mib), path, &path_len, NULL, 0);
+    return result ? "" : path;
+}
+#else
 std::string GetExecutablePathImpl()
 {
     // We cannot use lstat to get the size of /proc/self/exe as it always returns 0
     // so we just use a big buffer and hope the path fits in it.
     char path[4096];
 
 #if defined(__linux__)
     ssize_t result = readlink("/proc/self/exe", path, sizeof(path) - 1);
@@ -37,16 +63,17 @@ std::string GetExecutablePathImpl()
     if (result < 0 || static_cast<size_t>(result) >= sizeof(path) - 1)
     {
         return "";
     }
 
     path[result] = '\0';
     return path;
 }
+#endif
 
 std::string GetExecutableDirectoryImpl()
 {
     std::string executablePath = GetExecutablePath();
     size_t lastPathSepLoc      = executablePath.find_last_of("/");
     return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
 }