1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

Implement /proc/PID/vm.

Refactored SyntheticFileSystem to maintain an arbitrary directory structure.
ProcFileSystem creates a directory entry in /proc for each new process.
This commit is contained in:
Andreas Kling 2018-10-26 17:42:12 +02:00
parent 10347b9ae8
commit a32b3a3ddf
15 changed files with 217 additions and 39 deletions

View file

@ -1,6 +1,14 @@
#include "ProcFileSystem.h"
#include "Task.h"
static ProcFileSystem* s_the;
ProcFileSystem& ProcFileSystem::the()
{
ASSERT(s_the);
return *s_the;
}
RetainPtr<ProcFileSystem> ProcFileSystem::create()
{
return adopt(*new ProcFileSystem);
@ -8,15 +16,56 @@ RetainPtr<ProcFileSystem> ProcFileSystem::create()
ProcFileSystem::ProcFileSystem()
{
s_the = this;
}
ProcFileSystem::~ProcFileSystem()
{
}
void ProcFileSystem::addProcess(Task& task)
{
ASSERT_INTERRUPTS_DISABLED();
char buf[16];
ksprintf(buf, "%d", task.pid());
auto dir = addFile(createDirectory(buf));
m_pid2inode.set(task.pid(), dir.index());
addFile(createGeneratedFile("vm", [&task] {
InterruptDisabler disabler;
char* buffer;
auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer);
memset(buffer, 0, stringImpl->length());
char* ptr = buffer;
ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
for (auto& region : task.regions()) {
ptr += ksprintf(ptr, "%x -- %x %x %s\n",
region->linearAddress.get(),
region->linearAddress.offset(region->size - 1).get(),
region->size,
region->name.characters());
}
*ptr = '\0';
return ByteBuffer::copy((byte*)buffer, ptr - buffer);
}), dir.index());
}
void ProcFileSystem::removeProcess(Task& task)
{
ASSERT_INTERRUPTS_DISABLED();
auto pid = task.pid();
auto it = m_pid2inode.find(pid);
ASSERT(it != m_pid2inode.end());
bool success = removeFile((*it).value);
ASSERT(success);
m_pid2inode.remove(pid);
}
bool ProcFileSystem::initialize()
{
SyntheticFileSystem::initialize();
auto d = addFile(createDirectory("sys"));
addFile(createGeneratedFile("summary", [] {
InterruptDisabler disabler;
auto tasks = Task::allTasks();

View file

@ -3,15 +3,24 @@
#include <AK/Types.h>
#include <VirtualFileSystem/SyntheticFileSystem.h>
class Task;
class ProcFileSystem final : public SyntheticFileSystem {
public:
static ProcFileSystem& the();
virtual ~ProcFileSystem() override;
static RetainPtr<ProcFileSystem> create();
virtual bool initialize() override;
virtual const char* className() const override;
void addProcess(Task&);
void removeProcess(Task&);
private:
ProcFileSystem();
HashMap<pid_t, InodeIndex> m_pid2inode;
};

View file

@ -12,6 +12,7 @@
#include "errno.h"
#include "i8253.h"
#include "RTC.h"
#include "ProcFileSystem.h"
//#define DEBUG_IO
//#define TASK_DEBUG
@ -397,11 +398,14 @@ Task::Task(String&& name, uid_t uid, gid_t gid, pid_t parentPID, RingLevel ring)
// HACK: Ring2 SS in the TSS is the current PID.
m_tss.ss2 = m_pid;
m_farPtr.offset = 0x98765432;
ProcFileSystem::the().addProcess(*this);
}
Task::~Task()
{
InterruptDisabler disabler;
ProcFileSystem::the().removeProcess(*this);
system.nprocess--;
delete [] m_ldtEntries;
m_ldtEntries = nullptr;

View file

@ -15,6 +15,7 @@ class Zone;
class Task : public InlineLinkedListNode<Task> {
friend class InlineLinkedListNode<Task>;
class Region;
public:
static Task* createKernelTask(void (*entry)(), String&& name);
static Task* createUserTask(const String& path, uid_t, gid_t, pid_t parentPID, int& error, const char** args = nullptr);
@ -110,6 +111,8 @@ public:
static void taskDidCrash(Task*);
size_t regionCount() const { return m_regions.size(); }
const Vector<OwnPtr<Region>>& regions() const { return m_regions; }
void dumpRegions();
void didSchedule() { ++m_timesScheduled; }

View file

@ -108,9 +108,7 @@ static void init_stage2()
vfs->mountRoot(e2fs.copyRef());
auto procfs = ProcFileSystem::create();
procfs->initialize();
vfs->mount(procfs.copyRef(), "/proc");
vfs->mount(ProcFileSystem::the(), "/proc");
#endif
@ -200,6 +198,9 @@ void init()
kprintf("%u kB base memory\n", base_memory);
kprintf("%u kB extended memory\n", ext_memory);
auto procfs = ProcFileSystem::create();
procfs->initialize();
Task::initialize();
Task::createKernelTask(undertaker_main, "undertaker");