Kernel: add NullDevice to /dev/null

This commit is contained in:
Bananymous 2023-07-20 00:06:22 +03:00
parent adbe13938e
commit f8a1a10897
4 changed files with 46 additions and 0 deletions

View File

@ -15,6 +15,7 @@ set(KERNEL_SOURCES
kernel/CPUID.cpp kernel/CPUID.cpp
kernel/Debug.cpp kernel/Debug.cpp
kernel/Device/Device.cpp kernel/Device/Device.cpp
kernel/Device/NullDevice.cpp
kernel/Errors.cpp kernel/Errors.cpp
kernel/Font.cpp kernel/Font.cpp
kernel/FS/DevFS/FileSystem.cpp kernel/FS/DevFS/FileSystem.cpp

View File

@ -0,0 +1,27 @@
#include <BAN/UniqPtr.h>
#include <kernel/Device/Device.h>
namespace Kernel
{
class NullDevice : public CharacterDevice
{
public:
static BAN::ErrorOr<BAN::RefPtr<NullDevice>> create(mode_t, uid_t, gid_t);
virtual dev_t rdev() const override { return m_rdev; }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override { return 0; }
virtual BAN::ErrorOr<size_t> write(size_t, const void*, size_t size) override { return size; };
protected:
NullDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev)
: CharacterDevice(mode, uid, gid)
, m_rdev(rdev)
{ }
private:
const dev_t m_rdev;
};
}

View File

@ -0,0 +1,15 @@
#include <kernel/Device/NullDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
namespace Kernel
{
BAN::ErrorOr<BAN::RefPtr<NullDevice>> NullDevice::create(mode_t mode, uid_t uid, gid_t gid)
{
auto* result = new NullDevice(mode, uid, gid, DevFileSystem::get().get_next_rdev());
if (result == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<NullDevice>::adopt(result);
}
}

View File

@ -1,4 +1,5 @@
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
#include <kernel/Device/NullDevice.h>
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h> #include <kernel/FS/RamFS/Inode.h>
#include <kernel/LockGuard.h> #include <kernel/LockGuard.h>
@ -17,6 +18,8 @@ namespace Kernel
auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, Inode::Mode::IFDIR | 0755, 0, 0)); auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, Inode::Mode::IFDIR | 0755, 0, 0));
MUST(s_instance->set_root_inode(root_inode)); MUST(s_instance->set_root_inode(root_inode));
s_instance->add_device("null", MUST(NullDevice::create(0666, 0, 0)));
} }
DevFileSystem& DevFileSystem::get() DevFileSystem& DevFileSystem::get()