Files
FEX-Emu--FEX/Source/Common/FDUtils.h
T
Ryan Houdek 465ecd9b19 Mark code regions that require glibc memory allocations.
This ensures that when we enable glibc fault testing these sections
won't break CI.
2023-03-30 16:28:33 -07:00

36 lines
925 B
C++

#pragma once
#include <FEXCore/Utils/Allocator.h>
#include <FEXCore/Utils/LogManager.h>
#include <FEXCore/fextl/string.h>
#include <fcntl.h>
#include <filesystem>
#include <linux/limits.h>
#include <optional>
#include <unistd.h>
namespace FEX {
[[maybe_unused]]
static
std::optional<fextl::string> get_fdpath(int fd) {
FEXCore::Allocator::YesIKnowImNotSupposedToUseTheGlibcAllocator glibc;
char SymlinkPath[PATH_MAX];
std::filesystem::path Path = std::filesystem::path("/proc/self/fd") / std::to_string(fd);
int Result = readlinkat(AT_FDCWD, Path.c_str(), SymlinkPath, sizeof(SymlinkPath));
if (Result != -1) {
return fextl::string(SymlinkPath, Result);
}
// Not fatal if an FD doesn't point to a file
return std::nullopt;
}
inline
int get_fdpath(int fd, char *SymlinkPath) {
auto Path = fmt::format("/proc/self/fd/{}", fd);
return readlinkat(AT_FDCWD, Path.c_str(), SymlinkPath, PATH_MAX);
}
}