Kernel: Implement /proc/<n>/fd

This commit is contained in:
Bananymous 2025-11-24 00:19:51 +02:00
parent 3207f5d61f
commit 9d0990e5e8
3 changed files with 138 additions and 15 deletions

View File

@ -69,10 +69,6 @@ namespace Kernel
protected: protected:
virtual BAN::ErrorOr<BAN::String> link_target_impl() override; virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
// You may not write here and this is always non blocking
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(EINVAL); }
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(EINVAL); }
virtual bool can_read_impl() const override { return false; } virtual bool can_read_impl() const override { return false; }
virtual bool can_write_impl() const override { return false; } virtual bool can_write_impl() const override { return false; }
virtual bool has_error_impl() const override { return false; } virtual bool has_error_impl() const override { return false; }
@ -114,27 +110,54 @@ namespace Kernel
class ProcSymlinkInode final : public TmpInode class ProcSymlinkInode final : public TmpInode
{ {
public: public:
static BAN::ErrorOr<BAN::RefPtr<ProcSymlinkInode>> create_new(BAN::ErrorOr<BAN::String> (*)(void*), void* data, TmpFileSystem&, mode_t, uid_t, gid_t); static BAN::ErrorOr<BAN::RefPtr<ProcSymlinkInode>> create_new(BAN::ErrorOr<BAN::String> (*)(void*), void (*)(void*), void* data, TmpFileSystem&, mode_t, uid_t, gid_t);
~ProcSymlinkInode() = default; ~ProcSymlinkInode();
protected: protected:
virtual BAN::ErrorOr<BAN::String> link_target_impl() override; virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
// You may not write here and this is always non blocking
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override { return BAN::Error::from_errno(EINVAL); }
virtual BAN::ErrorOr<void> truncate_impl(size_t) override { return BAN::Error::from_errno(EINVAL); }
virtual bool can_read_impl() const override { return false; } virtual bool can_read_impl() const override { return false; }
virtual bool can_write_impl() const override { return false; } virtual bool can_write_impl() const override { return false; }
virtual bool has_error_impl() const override { return false; } virtual bool has_error_impl() const override { return false; }
virtual bool has_hungup_impl() const override { return false; } virtual bool has_hungup_impl() const override { return false; }
private: private:
ProcSymlinkInode(BAN::ErrorOr<BAN::String> (*callback)(void*), void* data, TmpFileSystem&, const TmpInodeInfo&); ProcSymlinkInode(BAN::ErrorOr<BAN::String> (*callback)(void*), void (*destructor)(void*), void* data, TmpFileSystem&, const TmpInodeInfo&);
private: private:
BAN::ErrorOr<BAN::String> (*m_callback)(void*); BAN::ErrorOr<BAN::String> (*m_callback)(void*);
void (*m_destructor)(void*);
void* m_data; void* m_data;
}; };
class ProcFDDirectoryInode final : public TmpInode
{
public:
static BAN::ErrorOr<BAN::RefPtr<ProcFDDirectoryInode>> create_new(Process&, TmpFileSystem&, mode_t);
~ProcFDDirectoryInode() = default;
virtual uid_t uid() const override { return m_process.credentials().ruid(); }
virtual gid_t gid() const override { return m_process.credentials().rgid(); }
protected:
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override { return BAN::Error::from_errno(EPERM); }
virtual BAN::ErrorOr<void> create_directory_impl(BAN::StringView, mode_t, uid_t, gid_t) override { return BAN::Error::from_errno(EPERM); }
virtual BAN::ErrorOr<void> link_inode_impl(BAN::StringView, BAN::RefPtr<Inode>) override { return BAN::Error::from_errno(EPERM); }
virtual BAN::ErrorOr<void> rename_inode_impl(BAN::RefPtr<Inode>, BAN::StringView, BAN::StringView) override { return BAN::Error::from_errno(EPERM); }
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override { return BAN::Error::from_errno(EPERM); }
virtual bool can_read_impl() const override { return false; }
virtual bool can_write_impl() const override { return false; }
virtual bool has_error_impl() const override { return false; }
virtual bool has_hungup_impl() const override { return false; }
private:
ProcFDDirectoryInode(Process&, TmpFileSystem&, const TmpInodeInfo&);
private:
Process& m_process;
};
} }

View File

@ -43,7 +43,7 @@ namespace Kernel
[](void*) -> BAN::ErrorOr<BAN::String> { [](void*) -> BAN::ErrorOr<BAN::String> {
return BAN::String::formatted("{}", Process::current().pid()); return BAN::String::formatted("{}", Process::current().pid());
}, },
nullptr, *s_instance, 0444, 0, 0) nullptr, nullptr, *s_instance, 0444, 0, 0)
); );
MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*self_inode, "self"_sv)); MUST(static_cast<TmpDirectoryInode*>(s_instance->root_inode().ptr())->link_inode(*self_inode, "self"_sv));
} }

View File

@ -1,5 +1,7 @@
#include <kernel/FS/ProcFS/Inode.h> #include <kernel/FS/ProcFS/Inode.h>
#include <ctype.h>
namespace Kernel namespace Kernel
{ {
@ -19,6 +21,7 @@ namespace Kernel
TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_environ, fs, 0400)), "environ"_sv)); TRY(inode->link_inode(*MUST(ProcROProcessInode::create_new(process, &Process::proc_environ, fs, 0400)), "environ"_sv));
TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_cwd, fs, 0777)), "cwd"_sv)); TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_cwd, fs, 0777)), "cwd"_sv));
TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_exe, fs, 0777)), "exe"_sv)); TRY(inode->link_inode(*MUST(ProcSymlinkProcessInode::create_new(process, &Process::proc_exe, fs, 0777)), "exe"_sv));
TRY(inode->link_inode(*MUST(ProcFDDirectoryInode::create_new(process, fs, 0777)), "fd"_sv));
return inode; return inode;
} }
@ -36,6 +39,7 @@ namespace Kernel
(void)TmpDirectoryInode::unlink_impl("environ"_sv); (void)TmpDirectoryInode::unlink_impl("environ"_sv);
(void)TmpDirectoryInode::unlink_impl("cwd"_sv); (void)TmpDirectoryInode::unlink_impl("cwd"_sv);
(void)TmpDirectoryInode::unlink_impl("exe"_sv); (void)TmpDirectoryInode::unlink_impl("exe"_sv);
(void)TmpDirectoryInode::unlink_impl("fd"_sv);
} }
BAN::ErrorOr<BAN::RefPtr<ProcROProcessInode>> ProcROProcessInode::create_new(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem& fs, mode_t mode) BAN::ErrorOr<BAN::RefPtr<ProcROProcessInode>> ProcROProcessInode::create_new(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem& fs, mode_t mode)
@ -110,27 +114,123 @@ namespace Kernel
return m_callback(offset, buffer); return m_callback(offset, buffer);
} }
BAN::ErrorOr<BAN::RefPtr<ProcSymlinkInode>> ProcSymlinkInode::create_new(BAN::ErrorOr<BAN::String> (*callback)(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)
{ {
auto inode_info = create_inode_info(Mode::IFLNK | mode, uid, gid); auto inode_info = create_inode_info(Mode::IFLNK | mode, uid, gid);
auto* inode_ptr = new ProcSymlinkInode(callback, data, fs, inode_info); auto* inode_ptr = new ProcSymlinkInode(callback, destructor, data, fs, 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<ProcSymlinkInode>::adopt(inode_ptr); return BAN::RefPtr<ProcSymlinkInode>::adopt(inode_ptr);
} }
ProcSymlinkInode::ProcSymlinkInode(BAN::ErrorOr<BAN::String> (*callback)(void*), void* data, TmpFileSystem& fs, const TmpInodeInfo& inode_info) ProcSymlinkInode::ProcSymlinkInode(BAN::ErrorOr<BAN::String> (*callback)(void*), void (*destructor)(void*), void* data, TmpFileSystem& fs, 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_destructor(destructor)
, m_data(data) , m_data(data)
{ {
m_inode_info.mode |= Inode::Mode::IFLNK; m_inode_info.mode |= Inode::Mode::IFLNK;
} }
ProcSymlinkInode::~ProcSymlinkInode()
{
if (m_destructor)
m_destructor(m_data);
}
BAN::ErrorOr<BAN::String> ProcSymlinkInode::link_target_impl() BAN::ErrorOr<BAN::String> ProcSymlinkInode::link_target_impl()
{ {
return m_callback(m_data); return m_callback(m_data);
} }
BAN::ErrorOr<BAN::RefPtr<ProcFDDirectoryInode>> ProcFDDirectoryInode::create_new(Process& process, TmpFileSystem& fs, mode_t mode)
{
auto inode_info = create_inode_info(Mode::IFDIR | mode, 0, 0);
auto* inode_ptr = new ProcFDDirectoryInode(process, fs, inode_info);
if (inode_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<ProcFDDirectoryInode>::adopt(inode_ptr);
}
ProcFDDirectoryInode::ProcFDDirectoryInode(Process& process, TmpFileSystem& fs, const TmpInodeInfo& inode_info)
: TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
, m_process(process)
{
ASSERT(mode().ifdir());
}
BAN::ErrorOr<BAN::RefPtr<Inode>> ProcFDDirectoryInode::find_inode_impl(BAN::StringView name)
{
int fd = 0;
for (char ch : name)
{
if (!isdigit(ch))
return BAN::Error::from_errno(ENOENT);
fd = (fd * 10) + (ch - '0');
if (fd > OPEN_MAX)
return BAN::Error::from_errno(ENOENT);
}
auto path_or_error = m_process.open_file_descriptor_set().path_of(fd);
if (path_or_error.is_error())
return BAN::Error::from_errno(ENOENT);
auto* data = new BAN::String();
if (data == nullptr)
return BAN::Error::from_errno(ENOMEM);
*data = BAN::move(path_or_error.release_value());
auto inode = TRY(ProcSymlinkInode::create_new(
[](void* data) -> BAN::ErrorOr<BAN::String>
{
BAN::String result;
TRY(result.append(*static_cast<BAN::String*>(data)));
return result;
},
[](void* data) -> void
{
delete static_cast<BAN::String*>(data);
},
data, m_fs, 0777, uid(), gid()
));
return BAN::RefPtr<Inode>(BAN::move(inode));
}
BAN::ErrorOr<size_t> ProcFDDirectoryInode::list_next_inodes_impl(off_t offset, struct dirent* list, size_t list_len)
{
if (list_len == 0)
return BAN::Error::from_errno(ENOBUFS);
auto& ofds = m_process.open_file_descriptor_set();
for (size_t fd = 0; fd < OPEN_MAX; fd++)
{
if (ofds.inode_of(fd).is_error())
continue;
if (offset-- > 0)
continue;
list[0] = {
.d_ino = 0,
.d_type = DT_LNK,
.d_name = {},
};
size_t index = 0;
BAN::Formatter::print(
[&](char ch) {
list[0].d_name[index++] = ch;
list[0].d_name[index] = '\0';
}, "{}", fd
);
return 1;
}
return 0;
}
} }