From 69e92f69a7909465820f26afc331375dbeaa68b2 Mon Sep 17 00:00:00 2001 From: ayeteadoe Date: Sat, 23 Aug 2025 20:58:43 -0700 Subject: [PATCH] LibCore: Implement SIGTERM-based kill() on Windows There is no direct Win32 API equivalent, but calling WM_CLOSE on the top-level windows allows for a graceful shutdown where resources are able to clean themselves up properly --- Libraries/LibCore/System.h | 2 +- Libraries/LibCore/SystemWindows.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Libraries/LibCore/System.h b/Libraries/LibCore/System.h index 2de5b798c35..cc84c5eedb5 100644 --- a/Libraries/LibCore/System.h +++ b/Libraries/LibCore/System.h @@ -128,8 +128,8 @@ ErrorOr access(StringView pathname, int mode, int flags = 0); ErrorOr readlink(StringView pathname); ErrorOr poll(Span, int timeout); -#if !defined(AK_OS_WINDOWS) ErrorOr kill(pid_t, int signal); +#if !defined(AK_OS_WINDOWS) ErrorOr chown(StringView pathname, uid_t uid, gid_t gid); ErrorOr posix_spawn(StringView path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const arguments[], char* const envp[]); ErrorOr posix_spawnp(StringView path, posix_spawn_file_actions_t* const file_actions, posix_spawnattr_t* const attr, char* const arguments[], char* const envp[]); diff --git a/Libraries/LibCore/SystemWindows.cpp b/Libraries/LibCore/SystemWindows.cpp index 98a202bb9b6..289551730c5 100644 --- a/Libraries/LibCore/SystemWindows.cpp +++ b/Libraries/LibCore/SystemWindows.cpp @@ -384,4 +384,23 @@ ErrorOr connect(int socket, struct sockaddr const* address, socklen_t addr return {}; } +ErrorOr kill(pid_t pid, int signal) +{ + if (signal == SIGTERM) { + if (!EnumWindows([](HWND hwnd, LPARAM l_param) -> BOOL { + DWORD window_pid = 0; + GetWindowThreadProcessId(hwnd, &window_pid); + if (window_pid == static_cast(l_param)) { + PostMessage(hwnd, WM_CLOSE, 0, 0); + } + return TRUE; + }, + pid)) + return Error::from_windows_error(); + } else { + return Error::from_string_literal("Unsupported signal value"); + } + return {}; +} + }