Kernel: Make ProcFS use the new TmpFS internally

This commit is contained in:
Bananymous 2023-11-07 02:35:44 +02:00
parent a20f8607de
commit a46b2f43d9
8 changed files with 56 additions and 42 deletions

View File

@ -1,13 +1,13 @@
#pragma once #pragma once
#include <kernel/FS/RamFS/FileSystem.h> #include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h> #include <kernel/FS/TmpFS/Inode.h>
#include <kernel/Process.h> #include <kernel/Process.h>
namespace Kernel namespace Kernel
{ {
class ProcFileSystem final : public RamFileSystem class ProcFileSystem final : public TmpFileSystem
{ {
public: public:
static void initialize(); static void initialize();
@ -17,10 +17,7 @@ namespace Kernel
void on_process_delete(Process&); void on_process_delete(Process&);
private: private:
ProcFileSystem(size_t size); ProcFileSystem();
private:
BAN::RefPtr<RamDirectoryInode> m_root_inode;
}; };
} }

View File

@ -1,29 +1,31 @@
#pragma once #pragma once
#include <kernel/FS/RamFS/FileSystem.h> #include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h> #include <kernel/FS/TmpFS/Inode.h>
#include <kernel/Process.h> #include <kernel/Process.h>
namespace Kernel namespace Kernel
{ {
class ProcPidInode final : public RamDirectoryInode class ProcPidInode final : public TmpDirectoryInode
{ {
public: public:
static BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> create(Process&, RamFileSystem&, mode_t, uid_t, gid_t); static BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> create_new(Process&, TmpFileSystem&, mode_t, uid_t, gid_t);
~ProcPidInode() = default; ~ProcPidInode() = default;
void cleanup();
private: private:
ProcPidInode(Process&, RamFileSystem&, const FullInodeInfo&); ProcPidInode(Process&, TmpFileSystem&, const TmpInodeInfo&);
private: private:
Process& m_process; Process& m_process;
}; };
class ProcROInode final : public RamInode class ProcROInode final : public TmpInode
{ {
public: public:
static BAN::ErrorOr<BAN::RefPtr<ProcROInode>> create(Process&, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, RamFileSystem&, mode_t, uid_t, gid_t); static BAN::ErrorOr<BAN::RefPtr<ProcROInode>> create_new(Process&, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem&, mode_t, uid_t, gid_t);
~ProcROInode() = default; ~ProcROInode() = default;
protected: protected:
@ -35,7 +37,7 @@ namespace Kernel
virtual bool has_data_impl() const override { return true; } virtual bool has_data_impl() const override { return true; }
private: private:
ProcROInode(Process&, size_t (Process::*)(off_t, BAN::ByteSpan) const, RamFileSystem&, const FullInodeInfo&); ProcROInode(Process&, size_t (Process::*)(off_t, BAN::ByteSpan) const, TmpFileSystem&, const TmpInodeInfo&);
private: private:
Process& m_process; Process& m_process;

View File

@ -88,10 +88,11 @@ namespace Kernel
size_t index; size_t index;
}; };
private: protected:
TmpFileSystem(size_t max_pages); TmpFileSystem(size_t max_pages);
BAN::ErrorOr<void> initialize(mode_t, uid_t, gid_t); BAN::ErrorOr<void> initialize(mode_t, uid_t, gid_t);
private:
InodeLocation find_inode(ino_t ino); InodeLocation find_inode(ino_t ino);
paddr_t find_block(size_t index); paddr_t find_block(size_t index);

View File

@ -97,7 +97,11 @@ namespace Kernel
~TmpDirectoryInode(); ~TmpDirectoryInode();
BAN::ErrorOr<void> link_inode(TmpInode&, BAN::StringView);
protected: protected:
TmpDirectoryInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
virtual BAN::ErrorOr<void> prepare_unlink() override; virtual BAN::ErrorOr<void> prepare_unlink() override;
protected: protected:
@ -108,14 +112,12 @@ namespace Kernel
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override final; virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override final;
private: private:
TmpDirectoryInode(TmpFileSystem&, ino_t, const TmpInodeInfo&);
BAN::ErrorOr<void> link_inode(TmpInode&, BAN::StringView);
template<TmpFuncs::for_each_valid_entry_callback F> template<TmpFuncs::for_each_valid_entry_callback F>
void for_each_valid_entry(F callback); void for_each_valid_entry(F callback);
friend class TmpInode; friend class TmpInode;
}; };
TmpInodeInfo create_inode_info(mode_t mode, uid_t uid, gid_t gid);
} }

View File

@ -60,6 +60,8 @@ namespace Kernel
bool is_session_leader() const { return pid() == sid(); } bool is_session_leader() const { return pid() == sid(); }
const Credentials& credentials() const { return m_credentials; }
BAN::ErrorOr<long> sys_exit(int status); BAN::ErrorOr<long> sys_exit(int status);
BAN::ErrorOr<long> sys_gettermios(::termios*); BAN::ErrorOr<long> sys_gettermios(::termios*);

View File

@ -1,6 +1,5 @@
#include <kernel/FS/ProcFS/FileSystem.h> #include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/ProcFS/Inode.h> #include <kernel/FS/ProcFS/Inode.h>
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/LockGuard.h> #include <kernel/LockGuard.h>
namespace Kernel namespace Kernel
@ -11,11 +10,10 @@ namespace Kernel
void ProcFileSystem::initialize() void ProcFileSystem::initialize()
{ {
ASSERT(s_instance == nullptr); ASSERT(s_instance == nullptr);
s_instance = new ProcFileSystem(1024 * 1024); s_instance = new ProcFileSystem();
ASSERT(s_instance); ASSERT(s_instance);
s_instance->m_root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, 0555, 0, 0)); MUST(s_instance->TmpFileSystem::initialize(0555, 0, 0));
MUST(s_instance->set_root_inode(s_instance->m_root_inode));
} }
ProcFileSystem& ProcFileSystem::get() ProcFileSystem& ProcFileSystem::get()
@ -24,23 +22,28 @@ namespace Kernel
return *s_instance; return *s_instance;
} }
ProcFileSystem::ProcFileSystem(size_t size) ProcFileSystem::ProcFileSystem()
: RamFileSystem(size) : TmpFileSystem(-1)
{ {
} }
BAN::ErrorOr<void> ProcFileSystem::on_process_create(Process& process) BAN::ErrorOr<void> ProcFileSystem::on_process_create(Process& process)
{ {
auto path = BAN::String::formatted("{}", process.pid()); auto path = BAN::String::formatted("{}", process.pid());
auto inode = TRY(ProcPidInode::create(process, *this, 0555, 0, 0)); auto inode = TRY(ProcPidInode::create_new(process, *this, 0555, process.credentials().ruid(), process.credentials().rgid()));
TRY(m_root_inode->add_inode(path, inode)); TRY(reinterpret_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
return {}; return {};
} }
void ProcFileSystem::on_process_delete(Process& process) void ProcFileSystem::on_process_delete(Process& process)
{ {
auto path = BAN::String::formatted("{}", process.pid()); auto path = BAN::String::formatted("{}", process.pid());
MUST(m_root_inode->unlink(path));
auto inode = MUST(root_inode()->find_inode(path));
reinterpret_cast<ProcPidInode*>(inode.ptr())->cleanup();
if (auto ret = root_inode()->unlink(path); ret.is_error())
dwarnln("{}", ret.error());
} }
} }

View File

@ -3,31 +3,38 @@
namespace Kernel namespace Kernel
{ {
BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> ProcPidInode::create(Process& process, RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> ProcPidInode::create_new(Process& process, TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
{ {
FullInodeInfo inode_info(fs, mode, uid, gid); auto inode_info = create_inode_info(Mode::IFDIR | mode, uid, gid);
auto* inode_ptr = new ProcPidInode(process, fs, inode_info); auto* inode_ptr = new ProcPidInode(process, fs, inode_info);
if (inode_ptr == nullptr) if (inode_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM); return BAN::Error::from_errno(ENOMEM);
auto inode = BAN::RefPtr<ProcPidInode>::adopt(inode_ptr); auto inode = BAN::RefPtr<ProcPidInode>::adopt(inode_ptr);
TRY(inode->add_inode("meminfo"sv, MUST(ProcROInode::create(process, &Process::proc_meminfo, fs, 0755, 0, 0)))); TRY(inode->link_inode(*MUST(ProcROInode::create_new(process, &Process::proc_meminfo, fs, 0400, uid, gid)), "meminfo"sv));
TRY(inode->add_inode("cmdline"sv, MUST(ProcROInode::create(process, &Process::proc_cmdline, fs, 0755, 0, 0)))); TRY(inode->link_inode(*MUST(ProcROInode::create_new(process, &Process::proc_cmdline, fs, 0400, uid, gid)), "cmdline"sv));
TRY(inode->add_inode("environ"sv, MUST(ProcROInode::create(process, &Process::proc_environ, fs, 0755, 0, 0)))); TRY(inode->link_inode(*MUST(ProcROInode::create_new(process, &Process::proc_environ, fs, 0400, uid, gid)), "environ"sv));
return inode; return inode;
} }
ProcPidInode::ProcPidInode(Process& process, RamFileSystem& fs, const FullInodeInfo& inode_info) ProcPidInode::ProcPidInode(Process& process, TmpFileSystem& fs, const TmpInodeInfo& inode_info)
: RamDirectoryInode(fs, inode_info, fs.root_inode()->ino()) : TmpDirectoryInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
, m_process(process) , m_process(process)
{ {
} }
BAN::ErrorOr<BAN::RefPtr<ProcROInode>> ProcROInode::create(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) void ProcPidInode::cleanup()
{ {
FullInodeInfo inode_info(fs, mode, uid, gid); (void)TmpDirectoryInode::unlink_impl("meminfo"sv);
(void)TmpDirectoryInode::unlink_impl("cmdline"sv);
(void)TmpDirectoryInode::unlink_impl("environ"sv);
}
BAN::ErrorOr<BAN::RefPtr<ProcROInode>> ProcROInode::create_new(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
{
auto inode_info = create_inode_info(Mode::IFREG | mode, uid, gid);
auto* inode_ptr = new ProcROInode(process, callback, fs, inode_info); auto* inode_ptr = new ProcROInode(process, callback, fs, inode_info);
if (inode_ptr == nullptr) if (inode_ptr == nullptr)
@ -35,8 +42,8 @@ namespace Kernel
return BAN::RefPtr<ProcROInode>::adopt(inode_ptr); return BAN::RefPtr<ProcROInode>::adopt(inode_ptr);
} }
ProcROInode::ProcROInode(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, RamFileSystem& fs, const FullInodeInfo& inode_info) ProcROInode::ProcROInode(Process& process, size_t (Process::*callback)(off_t, BAN::ByteSpan) const, TmpFileSystem& fs, const TmpInodeInfo& inode_info)
: RamInode(fs, inode_info) : TmpInode(fs, MUST(fs.allocate_inode(inode_info)), inode_info)
, m_process(process) , m_process(process)
, m_callback(callback) , m_callback(callback)
{ {

View File

@ -5,7 +5,7 @@
namespace Kernel namespace Kernel
{ {
static TmpInodeInfo create_inode_info(mode_t mode, uid_t uid, gid_t gid) TmpInodeInfo create_inode_info(mode_t mode, uid_t uid, gid_t gid)
{ {
auto current_time = SystemTimer::get().real_time(); auto current_time = SystemTimer::get().real_time();