Kernel: Add procfs that contains only pids

This commit is contained in:
Bananymous 2023-09-30 21:19:36 +03:00
parent 56bb419884
commit 8f630a97df
9 changed files with 135 additions and 0 deletions

Binary file not shown.

View File

@ -24,6 +24,8 @@ set(KERNEL_SOURCES
kernel/FS/Ext2/Inode.cpp kernel/FS/Ext2/Inode.cpp
kernel/FS/Inode.cpp kernel/FS/Inode.cpp
kernel/FS/Pipe.cpp kernel/FS/Pipe.cpp
kernel/FS/ProcFS/FileSystem.cpp
kernel/FS/ProcFS/Inode.cpp
kernel/FS/RamFS/FileSystem.cpp kernel/FS/RamFS/FileSystem.cpp
kernel/FS/RamFS/Inode.cpp kernel/FS/RamFS/Inode.cpp
kernel/FS/VirtualFileSystem.cpp kernel/FS/VirtualFileSystem.cpp

View File

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

View File

@ -0,0 +1,23 @@
#pragma once
#include <kernel/FS/RamFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/Process.h>
namespace Kernel
{
class ProcPidInode final : public RamDirectoryInode
{
public:
static BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> create(Process&, RamFileSystem&, mode_t, uid_t, gid_t);
~ProcPidInode() = default;
private:
ProcPidInode(Process&, RamFileSystem&, const FullInodeInfo&);
private:
Process& m_process;
};
}

View File

@ -0,0 +1,46 @@
#include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/ProcFS/Inode.h>
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/LockGuard.h>
namespace Kernel
{
static ProcFileSystem* s_instance = nullptr;
void ProcFileSystem::initialize()
{
ASSERT(s_instance == nullptr);
s_instance = new ProcFileSystem(1024 * 1024);
ASSERT(s_instance);
s_instance->m_root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, 0555, 0, 0));
MUST(s_instance->set_root_inode(s_instance->m_root_inode));
}
ProcFileSystem& ProcFileSystem::get()
{
ASSERT(s_instance);
return *s_instance;
}
ProcFileSystem::ProcFileSystem(size_t size)
: RamFileSystem(size)
{
}
BAN::ErrorOr<void> ProcFileSystem::on_process_create(Process& process)
{
auto path = BAN::String::formatted("{}", process.pid());
auto inode = TRY(ProcPidInode::create(process, *this, 0555, 0, 0));
TRY(m_root_inode->add_inode(path, inode));
return {};
}
void ProcFileSystem::on_process_delete(Process& process)
{
auto path = BAN::String::formatted("{}", process.pid());
MUST(m_root_inode->delete_inode(path));
}
}

View File

@ -0,0 +1,26 @@
#include <kernel/FS/ProcFS/Inode.h>
namespace Kernel
{
BAN::ErrorOr<BAN::RefPtr<ProcPidInode>> ProcPidInode::create(Process& process, RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid)
{
FullInodeInfo inode_info(fs, mode, uid, gid);
auto* inode_ptr = new ProcPidInode(process, fs, inode_info);
if (inode_ptr == nullptr)
return BAN::Error::from_errno(ENOMEM);
auto inode = BAN::RefPtr<ProcPidInode>::adopt(inode_ptr);
TRY(inode->add_inode("meminfo"sv, MUST(ProcMemInode::create(process, fs, 0755, 0, 0))));
return inode;
}
ProcPidInode::ProcPidInode(Process& process, RamFileSystem& fs, const FullInodeInfo& inode_info)
: RamDirectoryInode(fs, inode_info, fs.root_inode()->ino())
, m_process(process)
{
}
}

View File

@ -2,6 +2,7 @@
#include <BAN/StringView.h> #include <BAN/StringView.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/Ext2/FileSystem.h> #include <kernel/FS/Ext2/FileSystem.h>
#include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/RamFS/FileSystem.h> #include <kernel/FS/RamFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h> #include <kernel/FS/RamFS/Inode.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
@ -28,6 +29,8 @@ namespace Kernel
Credentials root_creds { 0, 0, 0, 0 }; Credentials root_creds { 0, 0, 0, 0 };
MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev"sv)); MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev"sv));
MUST(s_instance->mount(root_creds, &ProcFileSystem::get(), "/proc"sv));
auto* tmpfs = MUST(RamFileSystem::create(1024 * 1024, 0777, 0, 0)); auto* tmpfs = MUST(RamFileSystem::create(1024 * 1024, 0777, 0, 0));
MUST(s_instance->mount(root_creds, tmpfs, "/tmp"sv)); MUST(s_instance->mount(root_creds, tmpfs, "/tmp"sv));
} }

View File

@ -2,6 +2,7 @@
#include <BAN/StringView.h> #include <BAN/StringView.h>
#include <kernel/CriticalScope.h> #include <kernel/CriticalScope.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
@ -81,6 +82,8 @@ namespace Kernel
auto* process = new Process(credentials, pid, parent, sid, pgrp); auto* process = new Process(credentials, pid, parent, sid, pgrp);
ASSERT(process); ASSERT(process);
MUST(ProcFileSystem::get().on_process_create(*process));
return process; return process;
} }
@ -194,6 +197,8 @@ namespace Kernel
s_processes.remove(i); s_processes.remove(i);
s_process_lock.unlock(); s_process_lock.unlock();
ProcFileSystem::get().on_process_delete(*this);
m_lock.lock(); m_lock.lock();
m_exit_status.exited = true; m_exit_status.exited = true;
while (m_exit_status.waiting > 0) while (m_exit_status.waiting > 0)

View File

@ -2,6 +2,7 @@
#include <kernel/Arch.h> #include <kernel/Arch.h>
#include <kernel/Debug.h> #include <kernel/Debug.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/ProcFS/FileSystem.h>
#include <kernel/FS/VirtualFileSystem.h> #include <kernel/FS/VirtualFileSystem.h>
#include <kernel/GDT.h> #include <kernel/GDT.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
@ -131,6 +132,9 @@ extern "C" void kernel_main()
DevFileSystem::initialize(); DevFileSystem::initialize();
dprintln("devfs initialized"); dprintln("devfs initialized");
ProcFileSystem::initialize();
dprintln("procfs initialized");
if (Serial::has_devices()) if (Serial::has_devices())
{ {
Serial::initialize_devices(); Serial::initialize_devices();