From b02c486ad0d1a862c78b99ac3b9ac5a914891778 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 20 Jul 2023 00:06:22 +0300 Subject: [PATCH] Kernel: add NullDevice to /dev/null --- kernel/CMakeLists.txt | 1 + kernel/include/kernel/Device/NullDevice.h | 27 +++++++++++++++++++++++ kernel/kernel/Device/NullDevice.cpp | 15 +++++++++++++ kernel/kernel/FS/DevFS/FileSystem.cpp | 3 +++ 4 files changed, 46 insertions(+) create mode 100644 kernel/include/kernel/Device/NullDevice.h create mode 100644 kernel/kernel/Device/NullDevice.cpp diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index f70bd36091..91af117d64 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -15,6 +15,7 @@ set(KERNEL_SOURCES kernel/CPUID.cpp kernel/Debug.cpp kernel/Device/Device.cpp + kernel/Device/NullDevice.cpp kernel/Errors.cpp kernel/Font.cpp kernel/FS/DevFS/FileSystem.cpp diff --git a/kernel/include/kernel/Device/NullDevice.h b/kernel/include/kernel/Device/NullDevice.h new file mode 100644 index 0000000000..60abe67d6f --- /dev/null +++ b/kernel/include/kernel/Device/NullDevice.h @@ -0,0 +1,27 @@ +#include +#include + +namespace Kernel +{ + + class NullDevice : public CharacterDevice + { + public: + static BAN::ErrorOr> create(mode_t, uid_t, gid_t); + + virtual dev_t rdev() const override { return m_rdev; } + + virtual BAN::ErrorOr read(size_t, void*, size_t) override { return 0; } + virtual BAN::ErrorOr 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; + }; + +} \ No newline at end of file diff --git a/kernel/kernel/Device/NullDevice.cpp b/kernel/kernel/Device/NullDevice.cpp new file mode 100644 index 0000000000..a31d2777cc --- /dev/null +++ b/kernel/kernel/Device/NullDevice.cpp @@ -0,0 +1,15 @@ +#include +#include + +namespace Kernel +{ + + BAN::ErrorOr> 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::adopt(result); + } + +} \ No newline at end of file diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index c8b0b1cd10..5ec1db3ff6 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -17,6 +18,8 @@ namespace Kernel auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, Inode::Mode::IFDIR | 0755, 0, 0)); MUST(s_instance->set_root_inode(root_inode)); + + s_instance->add_device("null", MUST(NullDevice::create(0666, 0, 0))); } DevFileSystem& DevFileSystem::get()