[PATCH] D70198: On FreeBSD use AT_EXECPATH from ELF auxiliary vectors for getExecutablePath

Ed Maste via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 13 11:50:40 PST 2019


emaste created this revision.
emaste added reviewers: dim, theraven.
Herald added subscribers: hiraditya, krytarowski.
Herald added a project: LLVM.

/proc/curproc/file and the KERN_PROC_PATHNAME sysctl may not return the desired path if there are multiple hardlinks to the file.

Unfortunately we do not yet have a standard interface to obtain this in FreeBSD, but one will be added shortly. I will post a followup change to move to that based on FreeBSD version leaving this as a fallback, and eventually we can leave only that one.


https://reviews.llvm.org/D70198

Files:
  llvm/lib/Support/Unix/Path.inc


Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -38,6 +38,9 @@
 #include <mach-o/dyld.h>
 #include <sys/attr.h>
 #include <copyfile.h>
+#elif defined(__FreeBSD__)
+#include <machine/elf.h>
+extern char **environ;
 #elif defined(__DragonFly__)
 #include <sys/mount.h>
 #endif
@@ -183,13 +186,32 @@
     if (realpath(exe_path, link_path))
       return link_path;
   }
-#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) ||   \
-    defined(__minix) || defined(__DragonFly__) ||                              \
-    defined(__FreeBSD_kernel__) || defined(_AIX)
+#elif defined(__FreeBSD__)
+  // On FreeBSD if the exec path specified in ELF auxiliary vectors is
+  // preferred, if available.  /proc/curproc/file and the KERN_PROC_PATHNAME
+  // sysctl may not return the desired path if there are multiple hardlinks
+  // to the file.
+  char exe_path[PATH_MAX];
+  char **p = ::environ;
+  while (*p++ != 0)
+    ;
+  // ELF auxiliary vectors immediately follow the process's environment.
+  for (;;) {
+    switch (*(uintptr_t *)p++) {
+    case AT_EXECPATH:
+      return *p;
+    case AT_NULL:
+      break;
+    }
+    p++;
+  }
+  // Fall back to argv[0] if auxiliary vectors are not available.
+  if (getprogpath(exe_path, argv0) != NULL)
+    return exe_path;
+#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__minix) || \
+    defined(__DragonFly__) || defined(__FreeBSD_kernel__) || defined(_AIX)
   const char *curproc = "/proc/curproc/file";
   char exe_path[PATH_MAX];
-  // /proc is not mounted by default under FreeBSD, but gives more accurate
-  // information than argv[0] when it is.
   if (sys::fs::exists(curproc)) {
     ssize_t len = readlink(curproc, exe_path, sizeof(exe_path));
     if (len > 0) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70198.229148.patch
Type: text/x-patch
Size: 1887 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191113/c12752df/attachment.bin>


More information about the llvm-commits mailing list