1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-11 10:18:15 +09:00

Start adding a basic /proc filesystem and a "ps" utility.

This commit is contained in:
Andreas Kling 2018-10-23 11:57:38 +02:00
parent 98f76f0153
commit ed2422d7af
Notes: sideshowbarker 2024-07-19 18:44:50 +09:00
13 changed files with 139 additions and 23 deletions

View file

@ -51,7 +51,7 @@ public:
{ {
} }
Out operator()(In... in) Out operator()(In... in) const
{ {
ASSERT(m_callableWrapper); ASSERT(m_callableWrapper);
return m_callableWrapper->call(forward<In>(in)...); return m_callableWrapper->call(forward<In>(in)...);
@ -83,7 +83,7 @@ private:
class CallableWrapperBase { class CallableWrapperBase {
public: public:
virtual ~CallableWrapperBase() { } virtual ~CallableWrapperBase() { }
virtual Out call(In...) = 0; virtual Out call(In...) const = 0;
}; };
template<typename CallableType> template<typename CallableType>
@ -97,7 +97,7 @@ private:
CallableWrapper(const CallableWrapper&) = delete; CallableWrapper(const CallableWrapper&) = delete;
CallableWrapper& operator=(const CallableWrapper&) = delete; CallableWrapper& operator=(const CallableWrapper&) = delete;
Out call(In... in) final { return m_callable(forward<In>(in)...); } Out call(In... in) const final { return m_callable(forward<In>(in)...); }
private: private:
CallableType m_callable; CallableType m_callable;

View file

@ -21,7 +21,8 @@ KERNEL_OBJS = \
MemoryManager.o \ MemoryManager.o \
Console.o \ Console.o \
IRQHandler.o \ IRQHandler.o \
kprintf.o kprintf.o \
ProcFileSystem.o
VFS_OBJS = \ VFS_OBJS = \
../VirtualFileSystem/DiskDevice.o \ ../VirtualFileSystem/DiskDevice.o \
@ -35,7 +36,8 @@ VFS_OBJS = \
../VirtualFileSystem/Ext2FileSystem.o \ ../VirtualFileSystem/Ext2FileSystem.o \
../VirtualFileSystem/InodeIdentifier.o \ ../VirtualFileSystem/InodeIdentifier.o \
../VirtualFileSystem/VirtualFileSystem.o \ ../VirtualFileSystem/VirtualFileSystem.o \
../VirtualFileSystem/FileHandle.o ../VirtualFileSystem/FileHandle.o \
../VirtualFileSystem/SyntheticFileSystem.o
ELFLOADER_OBJS = \ ELFLOADER_OBJS = \
../ELFLoader/ELFImage.o \ ../ELFLoader/ELFImage.o \

29
Kernel/ProcFileSystem.cpp Normal file
View file

@ -0,0 +1,29 @@
#include "ProcFileSystem.h"
#include <AK/StdLib.h>
RetainPtr<ProcFileSystem> ProcFileSystem::create()
{
return adopt(*new ProcFileSystem);
}
ProcFileSystem::ProcFileSystem()
{
}
ProcFileSystem::~ProcFileSystem()
{
}
bool ProcFileSystem::initialize()
{
SyntheticFileSystem::initialize();
addFile(createGeneratedFile("summary", [] {
return String("Process summary!").toByteBuffer();
}));
return true;
}
const char* ProcFileSystem::className() const
{
return "procfs";
}

17
Kernel/ProcFileSystem.h Normal file
View file

@ -0,0 +1,17 @@
#pragma once
#include <AK/Types.h>
#include <VirtualFileSystem/SyntheticFileSystem.h>
class ProcFileSystem final : public SyntheticFileSystem {
public:
virtual ~ProcFileSystem() override;
static RetainPtr<ProcFileSystem> create();
virtual bool initialize() override;
virtual const char* className() const override;
private:
ProcFileSystem();
};

View file

@ -645,7 +645,9 @@ FileHandle* Task::openFile(String&& path)
{ {
auto handle = VirtualFileSystem::the().open(move(path)); auto handle = VirtualFileSystem::the().open(move(path));
if (!handle) { if (!handle) {
#ifdef DEBUG_IO
kprintf("vfs::open() failed\n"); kprintf("vfs::open() failed\n");
#endif
return nullptr; return nullptr;
} }
handle->setFD(m_fileHandles.size()); handle->setFD(m_fileHandles.size());

Binary file not shown.

View file

@ -25,6 +25,7 @@
#include "MemoryManager.h" #include "MemoryManager.h"
#include <ELFLoader/ELFLoader.h> #include <ELFLoader/ELFLoader.h>
#include "Console.h" #include "Console.h"
#include "ProcFileSystem.h"
#define TEST_VFS #define TEST_VFS
//#define TEST_ELF_LOADER //#define TEST_ELF_LOADER
@ -132,7 +133,9 @@ static void init_stage2()
vfs->mountRoot(e2fs.copyRef()); vfs->mountRoot(e2fs.copyRef());
//vfs->listDirectory("/"); auto procfs = ProcFileSystem::create();
procfs->initialize();
vfs->mount(procfs.copyRef(), "/proc");
{ {
auto motdFile = vfs->open("/motd.txt"); auto motdFile = vfs->open("/motd.txt");

View file

@ -1,5 +1,7 @@
mkdir mnt mkdir mnt
mount -o loop _fs_contents mnt/ mount -o loop _fs_contents mnt/
cp ../Userland/sh mnt/bin/sh cp ../Userland/sh mnt/bin/sh
cp ../Userland/id mnt/bin/id
cp ../Userland/ps mnt/bin/ps
umount mnt umount mnt
sync sync

1
Userland/.gitignore vendored
View file

@ -1,3 +1,4 @@
id id
sh sh
ps
*.o *.o

View file

@ -1,10 +1,12 @@
OBJS = \ OBJS = \
id.o \ id.o \
sh.o sh.o \
ps.o
APPS = \ APPS = \
id \ id \
sh sh \
ps
ARCH_FLAGS = ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib
@ -30,6 +32,9 @@ id: id.o
sh: sh.o sh: sh.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
ps: ps.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
.cpp.o: .cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $< @echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

25
Userland/ps.cpp Normal file
View file

@ -0,0 +1,25 @@
#include <LibC/stdio.h>
#include <LibC/unistd.h>
int main(int c, char** v)
{
int fd = open("/proc/summary");
if (fd == -1) {
printf("failed to open /proc/summary :(\n");
return 1;
}
for (;;) {
char buf[16];
ssize_t nread = read(fd, buf, sizeof(buf));
if (nread == 0)
break;
if (nread < 0) {
printf("failed to read :(\n");
return 2;
}
for (ssize_t i = 0; i < nread; ++i) {
putchar(buf[i]);
}
}
return 0;
}

View file

@ -1,6 +1,8 @@
#include "SyntheticFileSystem.h" #include "SyntheticFileSystem.h"
#include <AK/StdLib.h> #include <AK/StdLib.h>
//#define SYNTHFS_DEBUG
RetainPtr<SyntheticFileSystem> SyntheticFileSystem::create() RetainPtr<SyntheticFileSystem> SyntheticFileSystem::create()
{ {
return adopt(*new SyntheticFileSystem); return adopt(*new SyntheticFileSystem);
@ -25,10 +27,12 @@ bool SyntheticFileSystem::initialize()
rootDir->metadata.gid = 0; rootDir->metadata.gid = 0;
rootDir->metadata.size = 0; rootDir->metadata.size = 0;
rootDir->metadata.mtime = mepoch; rootDir->metadata.mtime = mepoch;
m_files.append(std::move(rootDir)); m_files.append(move(rootDir));
#ifndef SERENITY
addFile(createTextFile("file", "I'm a synthetic file!\n")); addFile(createTextFile("file", "I'm a synthetic file!\n"));
addFile(createTextFile("message", "Hey! This isn't my bottle!\n")); addFile(createTextFile("message", "Hey! This isn't my bottle!\n"));
#endif
return true; return true;
} }
@ -36,7 +40,7 @@ auto SyntheticFileSystem::createTextFile(String&& name, String&& text) -> OwnPtr
{ {
auto file = make<File>(); auto file = make<File>();
file->data = text.toByteBuffer(); file->data = text.toByteBuffer();
file->name = std::move(name); file->name = move(name);
file->metadata.size = file->data.size(); file->metadata.size = file->data.size();
file->metadata.uid = 100; file->metadata.uid = 100;
file->metadata.gid = 200; file->metadata.gid = 200;
@ -45,11 +49,24 @@ auto SyntheticFileSystem::createTextFile(String&& name, String&& text) -> OwnPtr
return file; return file;
} }
auto SyntheticFileSystem::createGeneratedFile(String&& name, Function<ByteBuffer()>&& generator) -> OwnPtr<File>
{
auto file = make<File>();
file->generator = move(generator);
file->name = move(name);
file->metadata.size = 0;
file->metadata.uid = 0;
file->metadata.gid = 0;
file->metadata.mode = 0100644;
file->metadata.mtime = mepoch;
return file;
}
void SyntheticFileSystem::addFile(OwnPtr<File>&& file) void SyntheticFileSystem::addFile(OwnPtr<File>&& file)
{ {
ASSERT(file); ASSERT(file);
file->metadata.inode = { id(), m_files.size() + 1 }; file->metadata.inode = { id(), m_files.size() + 1 };
m_files.append(std::move(file)); m_files.append(move(file));
} }
const char* SyntheticFileSystem::className() const const char* SyntheticFileSystem::className() const
@ -66,7 +83,7 @@ bool SyntheticFileSystem::enumerateDirectoryInode(InodeIdentifier inode, Functio
{ {
ASSERT(inode.fileSystemID() == id()); ASSERT(inode.fileSystemID() == id());
#ifdef SYNTHFS_DEBUG #ifdef SYNTHFS_DEBUG
printf("[synthfs] enumerateDirectoryInode %u\n", inode.index()); kprintf("[synthfs] enumerateDirectoryInode %u\n", inode.index());
#endif #endif
if (inode.index() != 1) if (inode.index() != 1)
return false; return false;
@ -83,17 +100,21 @@ InodeMetadata SyntheticFileSystem::inodeMetadata(InodeIdentifier inode) const
{ {
ASSERT(inode.fileSystemID() == id()); ASSERT(inode.fileSystemID() == id());
#ifdef SYNTHFS_DEBUG #ifdef SYNTHFS_DEBUG
printf("[synthfs] inodeMetadata(%u)\n", inode.index); kprintf("[synthfs] inodeMetadata(%u)\n", inode.index());
#endif #endif
if (inode.index() == 0 || inode.index() > m_files.size()) if (inode.index() == 0 || inode.index() > m_files.size())
return InodeMetadata(); return InodeMetadata();
return m_files[inode.index() - 1]->metadata; auto& file = *m_files[inode.index() - 1];
auto metadata = file.metadata;
if (file.generator)
metadata.size = file.generator().size();
return metadata;
} }
bool SyntheticFileSystem::setModificationTime(InodeIdentifier, dword timestamp) bool SyntheticFileSystem::setModificationTime(InodeIdentifier, dword timestamp)
{ {
(void) timestamp; (void) timestamp;
printf("FIXME: Implement SyntheticFileSystem::setModificationTime().\n"); kprintf("FIXME: Implement SyntheticFileSystem::setModificationTime().\n");
return false; return false;
} }
@ -102,13 +123,13 @@ InodeIdentifier SyntheticFileSystem::createInode(InodeIdentifier parentInode, co
(void) parentInode; (void) parentInode;
(void) name; (void) name;
(void) mode; (void) mode;
printf("FIXME: Implement SyntheticFileSystem::createDirectoryInode().\n"); kprintf("FIXME: Implement SyntheticFileSystem::createDirectoryInode().\n");
return { }; return { };
} }
bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&) bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&)
{ {
printf("FIXME: Implement SyntheticFileSystem::writeInode().\n"); kprintf("FIXME: Implement SyntheticFileSystem::writeInode().\n");
return false; return false;
} }
@ -116,7 +137,7 @@ Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::o
{ {
ASSERT(inode.fileSystemID() == id()); ASSERT(inode.fileSystemID() == id());
#ifdef SYNTHFS_DEBUG #ifdef SYNTHFS_DEBUG
printf("[synthfs] readInode %u\n", inode.index()); kprintf("[synthfs] readInode %u\n", inode.index());
#endif #endif
ASSERT(inode.index() != 1); ASSERT(inode.index() != 1);
ASSERT(inode.index() <= m_files.size()); ASSERT(inode.index() <= m_files.size());
@ -124,13 +145,18 @@ Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::o
ASSERT(buffer); ASSERT(buffer);
auto& file = *m_files[inode.index() - 1]; auto& file = *m_files[inode.index() - 1];
Unix::ssize_t nread = min(static_cast<Unix::off_t>(file.data.size() - offset), static_cast<Unix::off_t>(count)); ByteBuffer generatedData;
memcpy(buffer, file.data.pointer() + offset, nread); if (file.generator)
generatedData = file.generator();
auto* data = generatedData ? &generatedData : &file.data;
Unix::ssize_t nread = min(static_cast<Unix::off_t>(data->size() - offset), static_cast<Unix::off_t>(count));
memcpy(buffer, data->pointer() + offset, nread);
return nread; return nread;
} }
InodeIdentifier SyntheticFileSystem::makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) InodeIdentifier SyntheticFileSystem::makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t)
{ {
printf("FIXME: Implement SyntheticFileSystem::makeDirectory().\n"); kprintf("FIXME: Implement SyntheticFileSystem::makeDirectory().\n");
return { }; return { };
} }

View file

@ -4,7 +4,7 @@
#include "UnixTypes.h" #include "UnixTypes.h"
#include <AK/HashMap.h> #include <AK/HashMap.h>
class SyntheticFileSystem final : public FileSystem { class SyntheticFileSystem : public FileSystem {
public: public:
virtual ~SyntheticFileSystem() override; virtual ~SyntheticFileSystem() override;
static RetainPtr<SyntheticFileSystem> create(); static RetainPtr<SyntheticFileSystem> create();
@ -20,18 +20,22 @@ public:
virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const override; virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer) const override;
virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override; virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;
private: protected:
SyntheticFileSystem(); SyntheticFileSystem();
struct File { struct File {
String name; String name;
InodeMetadata metadata; InodeMetadata metadata;
ByteBuffer data; ByteBuffer data;
Function<ByteBuffer()> generator;
}; };
OwnPtr<File> createTextFile(String&& name, String&& text); OwnPtr<File> createTextFile(String&& name, String&& text);
OwnPtr<File> createGeneratedFile(String&& name, Function<ByteBuffer()>&&);
void addFile(OwnPtr<File>&&); void addFile(OwnPtr<File>&&);
private:
Vector<OwnPtr<File>> m_files; Vector<OwnPtr<File>> m_files;
}; };