Bug 1442791 - Try avoiding /proc on BSDs in GetExecutablePathImpl(). r?jgilbert
MozReview-Commit-ID: 7dz00BQAQMo
--- 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) : "";
}