summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-08-16 03:16:45 +0430
committerAndreas Kling <kling@serenityos.org>2021-08-16 21:16:14 +0200
commit64bc5f668dad954f1dd65ec6a1b98d312af16e96 (patch)
tree413dc38c29a13731e0eba76b987eb266f9463a2d
parente9feced041e1855327a01c2905a7353031ceaa91 (diff)
downloadserenity-64bc5f668dad954f1dd65ec6a1b98d312af16e96.zip
Kernel: Avoid enumerating all the fds to find a specific one in procfs
-rw-r--r--Kernel/ProcessSpecificExposed.cpp30
1 files changed, 9 insertions, 21 deletions
diff --git a/Kernel/ProcessSpecificExposed.cpp b/Kernel/ProcessSpecificExposed.cpp
index b71295887c..1ed2820f94 100644
--- a/Kernel/ProcessSpecificExposed.cpp
+++ b/Kernel/ProcessSpecificExposed.cpp
@@ -106,29 +106,17 @@ KResult Process::traverse_file_descriptions_directory(unsigned fsid, Function<bo
KResultOr<NonnullRefPtr<Inode>> Process::lookup_file_descriptions_directory(const ProcFS& procfs, StringView name) const
{
- KResultOr<NonnullRefPtr<ProcFSProcessPropertyInode>> file_description_link { ENOENT };
- // FIXME: Try to exit the loop earlier
- size_t count = 0;
- fds().enumerate([&](auto& file_description_metadata) {
- if (!file_description_metadata.is_valid()) {
- count++;
- return;
- }
- if (name.to_uint() == count) {
- auto maybe_inode = ProcFSProcessPropertyInode::try_create_for_file_description_link(procfs, static_cast<unsigned>(count), pid());
- if (maybe_inode.is_error()) {
- file_description_link = maybe_inode.error();
- return;
- }
+ auto maybe_index = name.to_uint();
+ if (!maybe_index.has_value())
+ return ENOENT;
- file_description_link = maybe_inode.release_value();
- }
- count++;
- });
+ if (!fds().get_if_valid(*maybe_index))
+ return ENOENT;
- if (file_description_link.is_error())
- return file_description_link.error();
- return file_description_link.release_value();
+ auto maybe_inode = ProcFSProcessPropertyInode::try_create_for_file_description_link(procfs, *maybe_index, pid());
+ if (maybe_inode.is_error())
+ return maybe_inode.error();
+ return maybe_inode.release_value();
}
KResult Process::procfs_get_pledge_stats(KBufferBuilder& builder) const