Kernel: Reorder sys$pipe() to fail more nicely

Try to do both FD allocations up front instead of interleaved between
assigning them to the descriptor table. This prevents us from failing
in the middle of setting up the pipes.
This commit is contained in:
Andreas Kling 2021-09-05 16:22:52 +02:00
parent 4483110990
commit c1e18befe8
Notes: sideshowbarker 2024-07-18 04:41:58 +09:00

View file

@ -24,30 +24,21 @@ KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
if (!fifo)
return ENOMEM;
auto open_reader_result = fifo->open_direction(FIFO::Direction::Reader);
if (open_reader_result.is_error())
return open_reader_result.error();
auto open_writer_result = fifo->open_direction(FIFO::Direction::Writer);
if (open_writer_result.is_error())
return open_writer_result.error();
auto reader_fd_allocation = TRY(m_fds.allocate());
auto writer_fd_allocation = TRY(m_fds.allocate());
auto reader_fd_or_error = m_fds.allocate();
if (reader_fd_or_error.is_error())
return reader_fd_or_error.error();
auto reader_fd = reader_fd_or_error.release_value();
m_fds[reader_fd.fd].set(open_reader_result.release_value(), fd_flags);
m_fds[reader_fd.fd].description()->set_readable(true);
if (!copy_to_user(&pipefd[0], &reader_fd.fd))
auto reader_description = TRY(fifo->open_direction(FIFO::Direction::Reader));
auto writer_description = TRY(fifo->open_direction(FIFO::Direction::Writer));
reader_description->set_readable(true);
writer_description->set_writable(true);
m_fds[reader_fd_allocation.fd].set(move(reader_description), fd_flags);
m_fds[writer_fd_allocation.fd].set(move(writer_description), fd_flags);
if (!copy_to_user(&pipefd[0], &reader_fd_allocation.fd))
return EFAULT;
auto writer_fd_or_error = m_fds.allocate();
if (writer_fd_or_error.is_error())
return writer_fd_or_error.error();
auto writer_fd = writer_fd_or_error.release_value();
m_fds[writer_fd.fd].set(open_writer_result.release_value(), fd_flags);
m_fds[writer_fd.fd].description()->set_writable(true);
if (!copy_to_user(&pipefd[1], &writer_fd.fd))
if (!copy_to_user(&pipefd[1], &writer_fd_allocation.fd))
return EFAULT;
return 0;