diff options
author | Michal Gorny <mgorny@gentoo.org> | 2019-03-03 10:06:40 +0000 |
---|---|---|
committer | Michal Gorny <mgorny@gentoo.org> | 2019-03-03 10:06:40 +0000 |
commit | 8f04766d133440bc5d6f8241395c44f0eb2b0b74 (patch) | |
tree | 3bcec4055b831adabc2a3528ea3f708b4908fa37 | |
parent | d3516b51cae6f3ec38be2db718534f0446057b77 (diff) | |
download | bcm5719-llvm-8f04766d133440bc5d6f8241395c44f0eb2b0b74.tar.gz bcm5719-llvm-8f04766d133440bc5d6f8241395c44f0eb2b0b74.zip |
[llvm] [Support] Reimplement getMainExecutable() using sysctl on NetBSD
Use sysctl() to implement getMainExecutable() on NetBSD, rather than
trying to guess the correct path from argv[0]. This is one
of the fixes to recent clang-check-mac-libcxx-fixed-compilation-db.cpp
test failure on NetBSD.
This has been historically done on both FreeBSD and NetBSD in r303015,
and reverted in r303285 due to buggy implementation on FreeBSD.
However, FWIK the NetBSD implementation does not suffer from the same
bugs and is more reliable than playing with argv[0].
Differential Revision: https://reviews.llvm.org/D56975
llvm-svn: 355283
-rw-r--r-- | llvm/lib/Support/Unix/Path.inc | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc index 5eba86d2077..d1f82bc9e72 100644 --- a/llvm/lib/Support/Unix/Path.inc +++ b/llvm/lib/Support/Unix/Path.inc @@ -90,6 +90,11 @@ #define STATVFS_F_FLAG(vfs) (vfs).f_flags #endif +#if defined(__NetBSD__) +#include <sys/param.h> +#include <sys/sysctl.h> +#endif + using namespace llvm; namespace llvm { @@ -98,7 +103,7 @@ namespace fs { const file_t kInvalidFile = -1; -#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ +#if defined(__FreeBSD__) || defined(__OpenBSD__) || \ defined(__minix) || defined(__FreeBSD_kernel__) || defined(__linux__) || \ defined(__CYGWIN__) || defined(__DragonFly__) || defined(_AIX) || defined(__GNU__) static int @@ -169,7 +174,18 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) { if (realpath(exe_path, link_path)) return link_path; } -#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ +#elif defined(__NetBSD__) + // NB: sysctl() solution can't be used on FreeBSD since it may return + // a wrong path when a file is hardlinked in multiple locations. + // See r303285 for an earlier revert. + + char exe_path[PATH_MAX]; + int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; + size_t len = sizeof(exe_path); + + if (sysctl(mib, 4, exe_path, &len, nullptr, 0) == 0) + return exe_path; +#elif defined(__FreeBSD__) || defined(__OpenBSD__) || \ defined(__minix) || defined(__DragonFly__) || \ defined(__FreeBSD_kernel__) || defined(_AIX) char exe_path[PATH_MAX]; |