Compare commits

..

2 Commits

Author SHA1 Message Date
51f0bdff0e
kernel+libc: Add O_NONBLOCK
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-19 11:25:14 +01:00
41514d9ad2
kernel: Add support for blocking reads 2023-03-19 11:21:50 +01:00
11 changed files with 46 additions and 1 deletions

View File

@ -32,6 +32,8 @@ namespace VFS
virtual Result<void> truncate(usize size) = 0;
virtual bool blocking() const = 0;
// Metadata accessors
virtual usize size() = 0;
@ -73,6 +75,11 @@ namespace VFS
return err(ENOTDIR);
}
bool blocking() const override
{
return false;
}
InodeType type() const override
{
return InodeType::RegularFile;

View File

@ -17,3 +17,8 @@ Result<usize> ConsoleDevice::write(const u8* buf, usize, usize length)
TextConsole::write((const char*)buf, length);
return length;
}
bool ConsoleDevice::blocking() const
{
return false;
}

View File

@ -11,5 +11,7 @@ class ConsoleDevice : public Device
Result<usize> write(const u8*, usize, usize) override;
bool blocking() const override;
virtual ~ConsoleDevice() = default;
};

View File

@ -8,5 +8,7 @@ class Device
virtual Result<usize> write(const u8* buf, usize offset, usize length) = 0;
virtual bool blocking() const = 0;
virtual ~Device() = default;
};

View File

@ -17,5 +17,10 @@ class NullDevice : public Device
return 0;
}
bool blocking() const override
{
return false;
}
virtual ~NullDevice() = default;
};

View File

@ -134,6 +134,11 @@ namespace TmpFS
return err(EINVAL);
}
bool blocking() const override
{
return m_device->blocking();
}
usize size() override
{
return 0;
@ -196,6 +201,11 @@ namespace TmpFS
return err(EISDIR);
}
bool blocking() const override
{
return false;
}
usize size() override
{
return 0;

View File

@ -21,6 +21,13 @@ Result<u64> sys_read(Registers*, SyscallArgs args)
if (!descriptor.is_readable()) return err(EBADF);
while (descriptor.inode->blocking())
{
if (descriptor.should_block()) kernel_sleep(10);
else
return err(EAGAIN);
}
usize nread = TRY(descriptor.inode->read(buf, descriptor.offset, size));
descriptor.offset += nread;

View File

@ -7,7 +7,7 @@
#include <bits/open-flags.h>
// These flags are needed after open(), the rest only affect open().
constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND;
constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK;
Result<u64> sys_open(Registers*, SyscallArgs args)
{

View File

@ -50,6 +50,11 @@ bool FileDescriptor::should_append()
return flags & O_APPEND;
}
bool FileDescriptor::should_block()
{
return flags & O_NONBLOCK;
}
bool FileDescriptor::is_readable()
{
return flags & O_RDONLY;

View File

@ -29,6 +29,7 @@ struct FileDescriptor
int flags { 0 };
bool should_append();
bool should_block();
bool is_writable();
bool is_readable();
};

View File

@ -10,5 +10,6 @@
#define O_CREAT 8
#define O_EXCL 16
#define O_TRUNC 32
#define O_NONBLOCK 64
#endif