1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:37:34 +00:00

Thread: Return a result from block() indicating why the block terminated

And use this to return EINTR in various places; some of which we were
not handling properly before.

This might expose a few bugs in userspace, but should be more compatible
with other POSIX systems, and is certainly a little cleaner.
This commit is contained in:
Robin Burchell 2019-07-20 11:05:52 +02:00 committed by Andreas Kling
parent 56217c7432
commit 833d444cd8
9 changed files with 73 additions and 38 deletions

View file

@ -878,9 +878,10 @@ ssize_t Process::sys$writev(int fd, const struct iovec* iov, int iov_count)
}
if (current->has_unmasked_pending_signals()) {
current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
if (nwritten == 0)
return -EINTR;
if (current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal) == Thread::BlockResult::InterruptedBySignal) {
if (nwritten == 0)
return -EINTR;
}
}
return nwritten;
@ -909,7 +910,10 @@ ssize_t Process::do_write(FileDescription& description, const u8* data, int data
#ifdef IO_DEBUG
dbgprintf("block write on %d\n", fd);
#endif
current->block<Thread::WriteBlocker>(description);
if (current->block<Thread::WriteBlocker>(description) == Thread::BlockResult::InterruptedBySignal) {
if (nwritten == 0)
return -EINTR;
}
}
ssize_t rc = description.write(data + nwritten, data_size - nwritten);
#ifdef IO_DEBUG
@ -923,9 +927,10 @@ ssize_t Process::do_write(FileDescription& description, const u8* data, int data
if (rc == 0)
break;
if (current->has_unmasked_pending_signals()) {
current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
if (nwritten == 0)
return -EINTR;
if (current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal) == Thread::BlockResult::InterruptedBySignal) {
if (nwritten == 0)
return -EINTR;
}
}
nwritten += rc;
}
@ -948,9 +953,10 @@ ssize_t Process::sys$write(int fd, const u8* data, ssize_t size)
return -EBADF;
auto nwritten = do_write(*description, data, size);
if (current->has_unmasked_pending_signals()) {
current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
if (nwritten == 0)
return -EINTR;
if (current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal) == Thread::BlockResult::InterruptedBySignal) {
if (nwritten == 0)
return -EINTR;
}
}
return nwritten;
}
@ -971,8 +977,7 @@ ssize_t Process::sys$read(int fd, u8* buffer, ssize_t size)
return -EBADF;
if (description->is_blocking()) {
if (!description->can_read()) {
current->block<Thread::ReadBlocker>(*description);
if (current->m_was_interrupted_while_blocked)
if (current->block<Thread::ReadBlocker>(*description) == Thread::BlockResult::InterruptedBySignal)
return -EINTR;
}
}
@ -1274,7 +1279,7 @@ int Process::sys$kill(pid_t pid, int signal)
}
if (pid == m_pid) {
current->send_signal(signal, this);
current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
(void)current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Signal);
return 0;
}
InterruptDisabler disabler;
@ -1300,7 +1305,6 @@ int Process::sys$usleep(useconds_t usec)
u64 wakeup_time = current->sleep(usec / 1000);
if (wakeup_time > g_uptime) {
ASSERT(current->m_was_interrupted_while_blocked);
u32 ticks_left_until_original_wakeup_time = wakeup_time - g_uptime;
return ticks_left_until_original_wakeup_time / TICKS_PER_SECOND;
}
@ -1313,7 +1317,6 @@ int Process::sys$sleep(unsigned seconds)
return 0;
u64 wakeup_time = current->sleep(seconds * TICKS_PER_SECOND);
if (wakeup_time > g_uptime) {
ASSERT(current->m_was_interrupted_while_blocked);
u32 ticks_left_until_original_wakeup_time = wakeup_time - g_uptime;
return ticks_left_until_original_wakeup_time / TICKS_PER_SECOND;
}
@ -1451,8 +1454,7 @@ pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options)
}
pid_t waitee_pid = waitee;
current->block<Thread::WaitBlocker>(options, waitee_pid);
if (current->m_was_interrupted_while_blocked)
if (current->block<Thread::WaitBlocker>(options, waitee_pid) == Thread::BlockResult::InterruptedBySignal)
return -EINTR;
InterruptDisabler disabler;
@ -1835,8 +1837,9 @@ int Process::sys$select(const Syscall::SC_select_params* params)
dbgprintf("%s<%u> selecting on (read:%u, write:%u), timeout=%p\n", name().characters(), pid(), rfds.size(), wfds.size(), params->timeout);
#endif
Thread::BlockResult block_res = Thread::BlockResult::WokeNormally;
if (!params->timeout || select_has_timeout)
current->block<Thread::SelectBlocker>(timeout, select_has_timeout, rfds, wfds, efds);
block_res = current->block<Thread::SelectBlocker>(timeout, select_has_timeout, rfds, wfds, efds);
int marked_fd_count = 0;
auto mark_fds = [&](auto* fds, auto& vector, auto should_mark) {
@ -1853,6 +1856,12 @@ int Process::sys$select(const Syscall::SC_select_params* params)
mark_fds(params->readfds, rfds, [](auto& description) { return description.can_read(); });
mark_fds(params->writefds, wfds, [](auto& description) { return description.can_write(); });
// FIXME: We should also mark params->exceptfds as appropriate.
if (marked_fd_count == 0) {
if (block_res == Thread::BlockResult::InterruptedBySignal)
return -EINTR;
}
return marked_fd_count;
}
@ -1890,8 +1899,9 @@ int Process::sys$poll(pollfd* fds, int nfds, int timeout)
dbgprintf("%s<%u> polling on (read:%u, write:%u), timeout=%d\n", name().characters(), pid(), rfds.size(), wfds.size(), timeout);
#endif
Thread::BlockResult block_res = Thread::BlockResult::WokeNormally;
if (has_timeout|| timeout < 0)
current->block<Thread::SelectBlocker>(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector());
block_res = current->block<Thread::SelectBlocker>(actual_timeout, has_timeout, rfds, wfds, Thread::SelectBlocker::FDVector());
int fds_with_revents = 0;
@ -1911,6 +1921,11 @@ int Process::sys$poll(pollfd* fds, int nfds, int timeout)
++fds_with_revents;
}
if (fds_with_revents == 0) {
if (block_res != Thread::BlockResult::InterruptedBySignal)
return -EINTR;
}
return fds_with_revents;
}
@ -2134,8 +2149,7 @@ int Process::sys$accept(int accepting_socket_fd, sockaddr* address, socklen_t* a
auto& socket = *accepting_socket_description->socket();
if (!socket.can_accept()) {
if (accepting_socket_description->is_blocking()) {
current->block<Thread::AcceptBlocker>(*accepting_socket_description);
if (current->m_was_interrupted_while_blocked)
if (current->block<Thread::AcceptBlocker>(*accepting_socket_description) == Thread::BlockResult::InterruptedBySignal)
return -EINTR;
} else {
return -EAGAIN;