1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 21:58:10 +00:00

Kernel: disown should unblock any potential waiters

This is necessary because if a process changes the state to Stopped
or resumes from that state, a wait entry is created in the parent
process. So, if a child process does this before disown is called,
we need to clear those entries to avoid leaking references/zombies
that won't be cleaned up until the former parent exits.

This also should solve an even more unlikely corner case where another
thread is waiting on a pid that is being disowned by another thread.
This commit is contained in:
Tom 2020-12-08 19:04:05 -07:00 committed by Andreas Kling
parent bcb9363a97
commit 4bbee00650
5 changed files with 55 additions and 1 deletions

View file

@ -412,8 +412,33 @@ void Thread::WaitBlockCondition::try_unblock(Thread::WaitBlocker& blocker)
}
}
void Thread::WaitBlockCondition::disowned_by_waiter(Process& process)
{
ScopedSpinLock lock(m_lock);
if (m_finalized)
return;
for (size_t i = 0; i < m_threads.size();) {
auto& info = m_threads[i];
if (&info.thread->process() == &process) {
do_unblock([&](Blocker& b, void*) {
ASSERT(b.blocker_type() == Blocker::Type::Wait);
auto& blocker = static_cast<WaitBlocker&>(b);
bool did_unblock = blocker.unblock(info.thread, WaitBlocker::UnblockFlags::Disowned, 0, false);
ASSERT(did_unblock); // disowning must unblock everyone
return true;
});
m_threads.remove(i);
continue;
}
i++;
}
}
bool Thread::WaitBlockCondition::unblock(Thread& thread, WaitBlocker::UnblockFlags flags, u8 signal)
{
ASSERT(flags != WaitBlocker::UnblockFlags::Disowned);
bool did_unblock_any = false;
bool did_wait = false;
bool was_waited_already = false;
@ -565,6 +590,13 @@ void Thread::WaitBlocker::was_unblocked(bool)
current_thread->try_dispatch_one_pending_signal(SIGCHLD);
}
void Thread::WaitBlocker::do_was_disowned()
{
ASSERT(!m_did_unblock);
m_did_unblock = true;
m_result = KResult(-ECHILD);
}
void Thread::WaitBlocker::do_set_result(const siginfo_t& result)
{
ASSERT(!m_did_unblock);
@ -599,6 +631,10 @@ bool Thread::WaitBlocker::unblock(Thread& thread, UnblockFlags flags, u8 signal,
return false;
break;
case P_ALL:
if (flags == UnblockFlags::Disowned) {
// Generic waiter won't be unblocked by disown
return false;
}
break;
default:
ASSERT_NOT_REACHED();
@ -621,6 +657,12 @@ bool Thread::WaitBlocker::unblock(Thread& thread, UnblockFlags flags, u8 signal,
if (!(m_wait_options & WUNTRACED) && !thread.is_traced())
return false;
break;
case UnblockFlags::Disowned:
ScopedSpinLock lock(m_lock);
// Disowning must unblock anyone waiting for this process explicitly
if (!m_did_unblock)
do_was_disowned();
return true;
}
if (flags == UnblockFlags::Terminated) {
@ -645,6 +687,7 @@ bool Thread::WaitBlocker::unblock(Thread& thread, UnblockFlags flags, u8 signal,
switch (flags) {
case UnblockFlags::Terminated:
case UnblockFlags::Disowned:
ASSERT_NOT_REACHED();
case UnblockFlags::Stopped:
siginfo.si_code = CLD_STOPPED;
@ -665,6 +708,7 @@ bool Thread::WaitBlocker::unblock(Thread& thread, UnblockFlags flags, u8 signal,
if (!from_add_blocker) {
// Only call unblock if we weren't called from within set_block_condition!
ASSERT(flags != UnblockFlags::Disowned);
unblock_from_blocker();
}
// Because this may be called from add_blocker, in which case we should