1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

Kernel: Improve ProcFS behavior in low memory conditions

When ProcFS could no longer allocate KBuffer objects to serve calls to
read, it would just return 0, indicating EOF. This then triggered
parsing errors because code assumed it read the file.

Because read isn't supposed to return ENOMEM, change ProcFS to populate
the file data upon file open or seek to the beginning. This also means
that calls to open can now return ENOMEM if needed. This allows the
caller to either be able to successfully open the file and read it, or
fail to open it in the first place.
This commit is contained in:
Tom 2020-09-17 13:51:09 -06:00 committed by Andreas Kling
parent b36f57e570
commit f98ca35b83
24 changed files with 398 additions and 243 deletions

View file

@ -40,18 +40,35 @@
#include <Kernel/VM/MemoryManager.h>
#include <LibC/errno_numbers.h>
//#define FILEDESCRIPTION_DEBUG
namespace Kernel {
NonnullRefPtr<FileDescription> FileDescription::create(Custody& custody)
KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(Custody& custody)
{
auto description = adopt(*new FileDescription(InodeFile::create(custody.inode())));
description->m_custody = custody;
auto result = description->attach();
if (result.is_error()) {
#ifdef FILEDESCRIPTION_DEBUG
dbg() << "Failed to create file description for custody: " << result;
#endif
return result;
}
return description;
}
NonnullRefPtr<FileDescription> FileDescription::create(File& file)
KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(File& file)
{
return adopt(*new FileDescription(file));
auto description = adopt(*new FileDescription(file));
auto result = description->attach();
if (result.is_error()) {
#ifdef FILEDESCRIPTION_DEBUG
dbg() << "Failed to create file description for file: " << result;
#endif
return result;
}
return description;
}
FileDescription::FileDescription(File& file)
@ -59,20 +76,29 @@ FileDescription::FileDescription(File& file)
{
if (file.is_inode())
m_inode = static_cast<InodeFile&>(file).inode();
if (is_socket())
socket()->attach(*this);
m_is_directory = metadata().is_directory();
}
FileDescription::~FileDescription()
{
if (is_socket())
socket()->detach(*this);
m_file->detach(*this);
if (is_fifo())
static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
// FIXME: Should this error path be observed somehow?
[[maybe_unused]] auto rc = m_file->close();
m_inode = nullptr;
(void)m_file->close();
if (m_inode)
m_inode->detach(*this);
}
KResult FileDescription::attach()
{
if (m_inode) {
auto result = m_inode->attach(*this);
if (result.is_error())
return result;
}
return m_file->attach(*this);
}
Thread::FileBlocker::BlockFlags FileDescription::should_unblock(Thread::FileBlocker::BlockFlags block_flags) const
@ -133,6 +159,10 @@ off_t FileDescription::seek(off_t offset, int whence)
// FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
m_current_offset = new_offset;
m_file->did_seek(*this, new_offset);
if (m_inode)
m_inode->did_seek(*this, new_offset);
evaluate_block_conditions();
return m_current_offset;
}