diff --git a/AK/MappedFile.cpp b/AK/MappedFile.cpp index 3957d042a3b..6323c1bb93f 100644 --- a/AK/MappedFile.cpp +++ b/AK/MappedFile.cpp @@ -21,6 +21,15 @@ Result, OSError> MappedFile::map(const String& path) if (fd < 0) return OSError(errno); + return map_from_fd_and_close(fd, path); +} + +Result, OSError> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] String const& path) +{ + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { + return OSError(errno); + } + ScopeGuard fd_close_guard = [fd] { close(fd); }; diff --git a/AK/MappedFile.h b/AK/MappedFile.h index 23a8eeccd6e..9002d8beb1a 100644 --- a/AK/MappedFile.h +++ b/AK/MappedFile.h @@ -20,6 +20,7 @@ class MappedFile : public RefCounted { public: static Result, OSError> map(const String& path); + static Result, OSError> map_from_fd_and_close(int fd, String const& path); ~MappedFile(); void* data() { return m_data; }