[PATCH] D122545: Skip attemps to access /proc/self/fd on FreeBSD

Mateusz Guzik via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 27 08:55:03 PDT 2022


mjguzik created this revision.
Herald added subscribers: dexonsmith, hiraditya, krytarowski.
Herald added a project: All.
mjguzik requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In contrast to Linux it does not provide entries which can be readlinked, resulting in the path used to open the file. That's on top of procfs not being mounted by default to begin with.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122545

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
@@ -950,7 +950,15 @@
   return s;
 }
 
-#if !defined(F_GETPATH)
+/*
+ * FreeBSD optionally provides /proc/self/fd, but it is incompatible with
+ * Linux. The thing to use is realpath.
+ */
+#if !defined(__FreeBSD__)
+#define TRY_PROC_SELF_FD
+#endif
+
+#if !defined(F_GETPATH) && defined(TRY_PROC_SELF_FD)
 static bool hasProcSelfFD() {
   // If we have a /proc filesystem mounted, we can quickly establish the
   // real name of the file with readlink
@@ -1137,6 +1145,7 @@
     RealPath->append(Buffer, Buffer + strlen(Buffer));
 #else
   char Buffer[PATH_MAX];
+#if defined(TRY_PROC_SELF_FD)
   if (hasProcSelfFD()) {
     char ProcPath[64];
     snprintf(ProcPath, sizeof(ProcPath), "/proc/self/fd/%d", ResultFD);
@@ -1144,13 +1153,16 @@
     if (CharCount > 0)
       RealPath->append(Buffer, Buffer + CharCount);
   } else {
+#endif
     SmallString<128> Storage;
     StringRef P = Name.toNullTerminatedStringRef(Storage);
 
     // Use ::realpath to get the real path name
     if (::realpath(P.begin(), Buffer) != nullptr)
       RealPath->append(Buffer, Buffer + strlen(Buffer));
+#if defined(TRY_PROC_SELF_FD)
   }
+#endif
 #endif
   return std::error_code();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122545.418455.patch
Type: text/x-patch
Size: 1350 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220327/a9a570ac/attachment.bin>


More information about the llvm-commits mailing list