From b2139c0b1ef7b39bc6bb7bcd7666675b41b94afb Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 4 Sep 2023 12:57:52 +0300 Subject: [PATCH] Kernel: Add basic support for O_NONBLOCK (only for tty) --- kernel/include/kernel/FS/Inode.h | 1 + kernel/include/kernel/Terminal/TTY.h | 3 ++- kernel/kernel/OpenFileDescriptorSet.cpp | 2 ++ kernel/kernel/Terminal/TTY.cpp | 6 ++++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/kernel/include/kernel/FS/Inode.h b/kernel/include/kernel/FS/Inode.h index a51258b96c..c159a64246 100644 --- a/kernel/include/kernel/FS/Inode.h +++ b/kernel/include/kernel/FS/Inode.h @@ -87,6 +87,7 @@ namespace Kernel virtual BAN::ErrorOr read(size_t, void*, size_t) { if (mode().ifdir()) return BAN::Error::from_errno(EISDIR); ASSERT_NOT_REACHED(); } virtual BAN::ErrorOr write(size_t, const void*, size_t) { if (mode().ifdir()) return BAN::Error::from_errno(EISDIR); ASSERT_NOT_REACHED(); } + virtual bool has_data() const { dwarnln("nonblock not supported"); return true; } virtual BAN::ErrorOr truncate(size_t) { if (mode().ifdir()) return BAN::Error::from_errno(EISDIR); return BAN::Error::from_errno(ENOTSUP); } diff --git a/kernel/include/kernel/Terminal/TTY.h b/kernel/include/kernel/Terminal/TTY.h index fb5e68082d..5891df09c2 100644 --- a/kernel/include/kernel/Terminal/TTY.h +++ b/kernel/include/kernel/Terminal/TTY.h @@ -35,6 +35,7 @@ namespace Kernel virtual BAN::ErrorOr read(size_t, void*, size_t) override; virtual BAN::ErrorOr write(size_t, const void*, size_t) override; + virtual bool has_data() const override; private: void clear(); @@ -78,7 +79,7 @@ namespace Kernel }; private: - Kernel::SpinLock m_lock; + mutable Kernel::SpinLock m_lock; State m_state { State::Normal }; AnsiState m_ansi_state { }; diff --git a/kernel/kernel/OpenFileDescriptorSet.cpp b/kernel/kernel/OpenFileDescriptorSet.cpp index 46e6f79425..bf3c19c741 100644 --- a/kernel/kernel/OpenFileDescriptorSet.cpp +++ b/kernel/kernel/OpenFileDescriptorSet.cpp @@ -234,6 +234,8 @@ namespace Kernel { TRY(validate_fd(fd)); auto& open_file = m_open_files[fd]; + if ((open_file->flags & O_NONBLOCK) && !open_file->inode->has_data()) + return 0; size_t nread = TRY(open_file->inode->read(open_file->offset, buffer, count)); open_file->offset += nread; return nread; diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index 9cf26cad15..906503ccc2 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -679,6 +679,12 @@ flush: return count; } + bool TTY::has_data() const + { + LockGuard _(m_lock); + return m_output.flush; + } + void TTY::putchar_current(uint8_t ch) { ASSERT(s_tty);