Kernel: Avoid enumerating all the fds to find a specific one in procfs

This commit is contained in:
Ali Mohammad Pur 2021-08-16 03:16:45 +04:30 committed by Andreas Kling
parent e9feced041
commit 64bc5f668d
Notes: sideshowbarker 2024-07-18 05:38:01 +09:00

View file

@ -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