Kernel: Fix some deadlocks in the Process

This commit is contained in:
Bananymous 2023-04-22 15:46:23 +03:00
parent 8c1f5bfe1e
commit 8001493df3
1 changed files with 26 additions and 16 deletions

View File

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