Kernel: Tidy up DevPtsFS construction and handle OOM errors

- Use KResultOr and TRY() to propagate errors
- Check for OOM when creating new inodes
This commit is contained in:
Andreas Kling 2021-09-06 10:42:09 +02:00
parent cbc4c98a87
commit efe4e230ee
Notes: sideshowbarker 2024-07-18 04:37:40 +09:00
3 changed files with 6 additions and 11 deletions

View File

@ -11,9 +11,9 @@
namespace Kernel {
NonnullRefPtr<DevPtsFS> DevPtsFS::create()
KResultOr<NonnullRefPtr<DevPtsFS>> DevPtsFS::try_create()
{
return adopt_ref(*new DevPtsFS);
return adopt_nonnull_ref_or_enomem(new (nothrow) DevPtsFS);
}
DevPtsFS::DevPtsFS()
@ -26,10 +26,7 @@ DevPtsFS::~DevPtsFS()
KResult DevPtsFS::initialize()
{
m_root_inode = adopt_ref_if_nonnull(new (nothrow) DevPtsFSInode(*this, 1, nullptr));
if (!m_root_inode)
return ENOMEM;
m_root_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevPtsFSInode(*this, 1, nullptr)));
m_root_inode->m_metadata.inode = { fsid(), 1 };
m_root_inode->m_metadata.mode = 0040555;
m_root_inode->m_metadata.uid = 0;
@ -64,8 +61,7 @@ KResultOr<NonnullRefPtr<Inode>> DevPtsFS::get_inode(InodeIdentifier inode_id) co
auto* device = Device::get_device(201, pty_index);
VERIFY(device);
// FIXME: Handle OOM
auto inode = adopt_ref(*new DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index(), static_cast<SlavePTY*>(device)));
auto inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index(), static_cast<SlavePTY*>(device))));
inode->m_metadata.inode = inode_id;
inode->m_metadata.size = 0;
inode->m_metadata.uid = device->uid();
@ -74,7 +70,6 @@ KResultOr<NonnullRefPtr<Inode>> DevPtsFS::get_inode(InodeIdentifier inode_id) co
inode->m_metadata.major_device = device->major();
inode->m_metadata.minor_device = device->minor();
inode->m_metadata.mtime = mepoch;
return inode;
}

View File

@ -20,7 +20,7 @@ class DevPtsFS final : public FileSystem {
public:
virtual ~DevPtsFS() override;
static NonnullRefPtr<DevPtsFS> create();
static KResultOr<NonnullRefPtr<DevPtsFS>> try_create();
virtual KResult initialize() override;
virtual StringView class_name() const override { return "DevPtsFS"sv; }

View File

@ -81,7 +81,7 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*>
} else if (fs_type == "proc"sv || fs_type == "ProcFS"sv) {
fs = TRY(ProcFS::try_create());
} else if (fs_type == "devpts"sv || fs_type == "DevPtsFS"sv) {
fs = DevPtsFS::create();
fs = TRY(DevPtsFS::try_create());
} else if (fs_type == "dev"sv || fs_type == "DevFS"sv) {
fs = DevFS::create();
} else if (fs_type == "sys"sv || fs_type == "SysFS"sv) {