diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index f0824a9c..02a1019a 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -220,20 +220,24 @@ namespace Kernel BAN::ErrorOr Process::write(int fd, const void* buffer, size_t count) { - m_lock.lock(); - TRY(validate_fd(fd)); - auto open_fd_copy = open_file_description(fd); - m_lock.unlock(); + OpenFileDescription open_fd_copy; + + { + LockGuard _(m_lock); + TRY(validate_fd(fd)); + open_fd_copy = open_file_description(fd); + } if (!(open_fd_copy.flags & O_WRONLY)) return BAN::Error::from_errno(EBADF); size_t n_written = TRY(open_fd_copy.inode->write(open_fd_copy.offset, buffer, count)); open_fd_copy.offset += n_written; - m_lock.lock(); - MUST(validate_fd(fd)); - open_file_description(fd) = open_fd_copy; - m_lock.unlock(); + { + LockGuard _(m_lock); + MUST(validate_fd(fd)); + open_file_description(fd) = open_fd_copy; + } return n_written; } @@ -266,10 +270,13 @@ namespace Kernel BAN::ErrorOr Process::fstat(int fd, struct stat* out) { - m_lock.lock(); - TRY(validate_fd(fd)); - auto open_fd_copy = open_file_description(fd); - m_lock.unlock(); + OpenFileDescription open_fd_copy; + + { + LockGuard _(m_lock); + TRY(validate_fd(fd)); + open_fd_copy = open_file_description(fd); + } out->st_dev = open_fd_copy.inode->dev(); out->st_ino = open_fd_copy.inode->ino(); @@ -298,10 +305,13 @@ namespace Kernel BAN::ErrorOr> Process::read_directory_entries(int fd) { - m_lock.lock(); - TRY(validate_fd(fd)); - auto open_fd_copy = open_file_description(fd); - m_lock.unlock(); + OpenFileDescription open_fd_copy; + + { + LockGuard _(m_lock); + TRY(validate_fd(fd)); + open_fd_copy = open_file_description(fd); + } auto result = TRY(open_fd_copy.inode->read_directory_entries(open_fd_copy.offset)); open_fd_copy.offset++;