ladybird/Libraries/LibWebView/ProcessMonitor.cpp
ayeteadoe 433ff624b8 LibWebView: Abstract process exit monitoring into ProcessMonitor
There is no direct equivalent to SIGCHILD on Windows. The closest we
can get is monitoring a specific process, given a known pid. On Unix
there is no single solution to be able to do that in LibCore's
EventLoopImplementationUnix. For Linux there's a SYS_pidfd_open syscall
that can integrate into poll(), but on macOS a kqueue would be needed.
Given macOS uses EventLoopImplementationUnix for the headless view
implementation, we currently can't create a fully cross-platform
abstaction at the Event Loop level to match what Windows needs to do.

ProcessMonitor's purpose is to abstract away the Unix vs Windows
behaviour avoiding more inlined ifdef soup in ProcessManager.
2026-04-12 16:08:07 +02:00

52 lines
1.4 KiB
C++

/*
* Copyright (c) 2025, ayeteadoe <ayeteadoe@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/EventLoop.h>
#include <LibWebView/ProcessMonitor.h>
#if !defined(AK_OS_WINDOWS)
# include <LibCore/System.h>
#endif
namespace WebView {
ProcessMonitor::ProcessMonitor(Function<void(pid_t)> exit_handler)
: m_on_process_exit(move(exit_handler))
{
#if !defined(AK_OS_WINDOWS)
m_signal_handle = Core::EventLoop::register_signal(SIGCHLD, [this](int) {
auto result = Core::System::waitpid(-1, WNOHANG);
while (!result.is_error() && result.value().pid > 0) {
auto& [pid, status] = result.value();
if (m_monitored_processes.contains(pid)) {
if (WIFEXITED(status) || WIFSIGNALED(status)) {
m_monitored_processes.remove(pid);
m_on_process_exit(pid);
}
}
result = Core::System::waitpid(-1, WNOHANG);
}
});
#endif
}
ProcessMonitor::~ProcessMonitor()
{
#if defined(AK_OS_WINDOWS)
// FIXME: Unregister all processes from the event loop on Windows
#else
Core::EventLoop::unregister_signal(m_signal_handle);
#endif
}
void ProcessMonitor::add_process(pid_t pid)
{
m_monitored_processes.set(pid, AK::HashSetExistingEntryBehavior::Keep);
#if defined(AK_OS_WINDOWS)
// FIXME: Register the process with the event loop on Windows
#endif
}
}