Kernel: Add basic ZeroDevice to /dev/zero

This commit is contained in:
Bananymous 2023-09-09 22:46:15 +03:00
parent 7e08f0fb66
commit d77f455065
4 changed files with 51 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,22 @@
#include <kernel/Device/ZeroDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
namespace Kernel
{
BAN::ErrorOr<BAN::RefPtr<ZeroDevice>> ZeroDevice::create(mode_t mode, uid_t uid, gid_t gid)
{
auto* result = new ZeroDevice(mode, uid, gid, DevFileSystem::get().get_next_dev());
if (result == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::RefPtr<ZeroDevice>::adopt(result);
}
BAN::ErrorOr<size_t> ZeroDevice::read_impl(off_t, void* buffer, size_t bytes)
{
memset(buffer, 0, bytes);
return bytes;
}
}

View File

@ -1,5 +1,6 @@
#include <BAN/ScopeGuard.h>
#include <kernel/Device/NullDevice.h>
#include <kernel/Device/ZeroDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/FS/RamFS/Inode.h>
#include <kernel/LockGuard.h>
@ -21,6 +22,7 @@ namespace Kernel
MUST(s_instance->set_root_inode(root_inode));
s_instance->add_device("null", MUST(NullDevice::create(0666, 0, 0)));
s_instance->add_device("zero", MUST(ZeroDevice::create(0666, 0, 0)));
}
DevFileSystem& DevFileSystem::get()