[llvm] 1a6d571 - [Support] Skip attempts to access /proc/self/fd on FreeBSD
Dimitry Andric via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 27 11:20:41 PDT 2022
Author: Mateusz Guzik
Date: 2022-03-27T20:19:41+02:00
New Revision: 1a6d571174f8a6565d3bb94cbd184a3861bf4a17
URL: https://github.com/llvm/llvm-project/commit/1a6d571174f8a6565d3bb94cbd184a3861bf4a17
DIFF: https://github.com/llvm/llvm-project/commit/1a6d571174f8a6565d3bb94cbd184a3861bf4a17.diff
LOG: [Support] Skip attempts to access /proc/self/fd on FreeBSD
In contrast to Linux it does not provide entries which can be readlinked
-- these are just regular files, not giving the expected outcome. That's
on top of procfs not being mounted by default to begin with.
This is probably the case on other BSDs as well, so I expect there will
be more ifdefs added down the road.
Reviewed By: emaste, dim
Differential Revision: https://reviews.llvm.org/D122545
Added:
Modified:
llvm/lib/Support/Unix/Path.inc
Removed:
################################################################################
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index c30c3a6e365e6..c4345f13f0a6e 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -950,7 +950,15 @@ ErrorOr<basic_file_status> directory_entry::status() const {
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 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
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 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
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();
}
More information about the llvm-commits
mailing list