[PATCH] D53557: [hurd] Make getMainExecutable get the real binary path

Reid Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 23 16:38:49 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL345104: [hurd] Make getMainExecutable get the real binary path (authored by rnk, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D53557?vs=170674&id=170784#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D53557

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


Index: llvm/trunk/lib/Support/Unix/Path.inc
===================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc
+++ llvm/trunk/lib/Support/Unix/Path.inc
@@ -179,14 +179,34 @@
   char exe_path[MAXPATHLEN];
   StringRef aPath("/proc/self/exe");
   if (sys::fs::exists(aPath)) {
-      // /proc is not always mounted under Linux (chroot for example).
-      ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path));
-      if (len >= 0)
-          return std::string(exe_path, len);
+    // /proc is not always mounted under Linux (chroot for example).
+    ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path));
+    if (len < 0)
+      return "";
+
+    // Null terminate the string for realpath. readlink never null
+    // terminates its output.
+    len = std::min(len, long(sizeof(exe_path) - 1));
+    exe_path[len] = '\0';
+
+    // At least on GNU/Hurd, /proc/self/exe is a symlink to the path that
+    // was used to start the program, and not the eventual binary file.
+    // We thus needs to run realpath over it to get the actual place
+    // where llvm was installed.
+#if _POSIX_VERSION >= 200112 || defined(__GLIBC__)
+    char *real_path = realpath(exe_path, NULL);
+    std::string ret = std::string(real_path);
+    free(real_path);
+    return ret;
+#else
+    char real_path[MAXPATHLEN];
+    realpath(exe_path, real_path);
+    return std::string(real_path);
+#endif
   } else {
-      // Fall back to the classical detection.
-      if (getprogpath(exe_path, argv0))
-        return exe_path;
+    // Fall back to the classical detection.
+    if (getprogpath(exe_path, argv0))
+      return exe_path;
   }
 #elif defined(HAVE_DLFCN_H) && defined(HAVE_DLADDR)
   // Use dladdr to get executable path if available.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53557.170784.patch
Type: text/x-patch
Size: 1811 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181023/5b89a843/attachment.bin>


More information about the llvm-commits mailing list