Kernel: Allow ProcRO inode fail and pass arbitrary argument to it
This commit is contained in:
@@ -76,7 +76,7 @@ namespace Kernel
|
|||||||
class ProcROInode final : public TmpInode
|
class ProcROInode final : public TmpInode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::RefPtr<ProcROInode>> create_new(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem&, mode_t, uid_t, gid_t);
|
static BAN::ErrorOr<BAN::RefPtr<ProcROInode>> create_new(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem&, void*, mode_t, uid_t, gid_t);
|
||||||
~ProcROInode() = default;
|
~ProcROInode() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -92,10 +92,11 @@ namespace Kernel
|
|||||||
virtual bool has_hungup_impl() const override { return false; }
|
virtual bool has_hungup_impl() const override { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ProcROInode(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem&, const TmpInodeInfo&);
|
ProcROInode(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem&, void*, const TmpInodeInfo&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t (*m_callback)(off_t, BAN::ByteSpan);
|
BAN::ErrorOr<size_t> (*m_callback)(off_t, BAN::ByteSpan, void*);
|
||||||
|
void* m_argument;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ProcSymlinkInode final : public TmpInode
|
class ProcSymlinkInode final : public TmpInode
|
||||||
|
|||||||
@@ -16,22 +16,23 @@ namespace Kernel
|
|||||||
MUST(s_instance->TmpFileSystem::initialize(0555, 0, 0));
|
MUST(s_instance->TmpFileSystem::initialize(0555, 0, 0));
|
||||||
|
|
||||||
auto meminfo_inode = MUST(ProcROInode::create_new(
|
auto meminfo_inode = MUST(ProcROInode::create_new(
|
||||||
[](off_t offset, BAN::ByteSpan buffer) -> size_t
|
[](off_t offset, BAN::ByteSpan buffer, void*) -> BAN::ErrorOr<size_t>
|
||||||
{
|
{
|
||||||
ASSERT(offset >= 0);
|
ASSERT(offset >= 0);
|
||||||
if ((size_t)offset >= sizeof(full_meminfo_t))
|
if ((size_t)offset >= sizeof(full_meminfo_t))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
full_meminfo_t meminfo;
|
const full_meminfo_t meminfo {
|
||||||
meminfo.page_size = PAGE_SIZE;
|
.page_size = PAGE_SIZE,
|
||||||
meminfo.free_pages = Heap::get().free_pages();
|
.free_pages = Heap::get().free_pages(),
|
||||||
meminfo.used_pages = Heap::get().used_pages();
|
.used_pages = Heap::get().used_pages(),
|
||||||
|
};
|
||||||
|
|
||||||
size_t bytes = BAN::Math::min<size_t>(sizeof(full_meminfo_t) - offset, buffer.size());
|
size_t bytes = BAN::Math::min<size_t>(sizeof(full_meminfo_t) - offset, buffer.size());
|
||||||
memcpy(buffer.data(), (uint8_t*)&meminfo + offset, bytes);
|
memcpy(buffer.data(), (uint8_t*)&meminfo + offset, bytes);
|
||||||
return bytes;
|
return bytes;
|
||||||
},
|
},
|
||||||
*s_instance, 0444, 0, 0
|
*s_instance, nullptr, 0444, 0, 0
|
||||||
));
|
));
|
||||||
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*meminfo_inode, "meminfo"_sv));
|
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*meminfo_inode, "meminfo"_sv));
|
||||||
|
|
||||||
|
|||||||
@@ -95,19 +95,20 @@ namespace Kernel
|
|||||||
return (m_process.*m_callback)();
|
return (m_process.*m_callback)();
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<ProcROInode>> ProcROInode::create_new(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
BAN::ErrorOr<BAN::RefPtr<ProcROInode>> ProcROInode::create_new(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem& fs, void* argument, mode_t mode, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
auto inode_info = create_inode_info(Mode::IFREG | mode, uid, gid);
|
auto inode_info = create_inode_info(Mode::IFREG | mode, uid, gid);
|
||||||
|
|
||||||
auto* inode_ptr = new ProcROInode(callback, fs, inode_info);
|
auto* inode_ptr = new ProcROInode(callback, fs, argument, inode_info);
|
||||||
if (inode_ptr == nullptr)
|
if (inode_ptr == nullptr)
|
||||||
return BAN::Error::from_errno(ENOMEM);
|
return BAN::Error::from_errno(ENOMEM);
|
||||||
return BAN::RefPtr<ProcROInode>::adopt(inode_ptr);
|
return BAN::RefPtr<ProcROInode>::adopt(inode_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcROInode::ProcROInode(size_t (*callback)(off_t, BAN::ByteSpan), TmpFileSystem& fs, const TmpInodeInfo& inode_info)
|
ProcROInode::ProcROInode(BAN::ErrorOr<size_t> (*callback)(off_t, BAN::ByteSpan, void*), TmpFileSystem& fs, void* argument, const TmpInodeInfo& inode_info)
|
||||||
: TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
|
: TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
|
||||||
, m_callback(callback)
|
, m_callback(callback)
|
||||||
|
, m_argument(argument)
|
||||||
{
|
{
|
||||||
m_mode |= Inode::Mode::IFREG;
|
m_mode |= Inode::Mode::IFREG;
|
||||||
}
|
}
|
||||||
@@ -116,7 +117,7 @@ namespace Kernel
|
|||||||
{
|
{
|
||||||
if (offset < 0)
|
if (offset < 0)
|
||||||
return BAN::Error::from_errno(EINVAL);
|
return BAN::Error::from_errno(EINVAL);
|
||||||
return m_callback(offset, buffer);
|
return TRY(m_callback(offset, buffer, m_argument));
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::RefPtr<ProcSymlinkInode>> ProcSymlinkInode::create_new(BAN::ErrorOr<BAN::String> (*callback)(void*), void (*destructor)(void*), void* data, TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
BAN::ErrorOr<BAN::RefPtr<ProcSymlinkInode>> ProcSymlinkInode::create_new(BAN::ErrorOr<BAN::String> (*callback)(void*), void (*destructor)(void*), void* data, TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
|
||||||
|
|||||||
Reference in New Issue
Block a user