mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-19 15:32:31 +00:00
LibIPC: Port to Windows
The Linux IPC uses SCM_RIGHTS to transfer fds to another process (see TransportSocket::transfer, which calls LocalSocket::send_message). File descriptors are handled separately from regular data. On Windows handles are embedded in regular data. They are duplicated in the sender process. Socket handles need special code both on sender side (because they require using WSADuplicateSocket instead of DuplicateHandle, see TransportSocketWindows::duplicate_handles) and on receiver side (because they require WSASocket, see FileWindows.cpp). TransportSocketWindows::ReadResult::fds vector is always empty, it is kept the same as Linux version to avoid OS #ifdefs in Connection.h/.cpp and Web::HTML::MessagePort::read_from_transport. Separate handling of fds permeates all IPC code, it doesn't make sense to #ifdef out all this code on Windows. In other words, the Linux code is more generic - it handles both regular data and fds. On Windows, we need only the regular data portion of it, and we just use that. Duplicating handles on Windows requires pid of target (receiver) process (see TransportSocketWindows::m_peer_pid). This pid is received during special TransportSocketWindows initialization, which is performed only on Windows. It is handled in a separate PR #3179. Note: ChatGPT and [stackoverflow](https://stackoverflow.com/questions/25429887/getting-pid-of-peer-socket-on-windows) suggest using GetExtendedTcpTable/GetTcpTable2 to get peer pid, but this doesn't work because [MIB_TCPROW2::dwOwningPid](https://learn.microsoft.com/en-us/windows/win32/api/tcpmib/ns-tcpmib-mib_tcprow2) is "The PID of the process that issued a context bind for this TCP connection.", so for both ends it will return the pid of the process that called socketpair. Co-Authored-By: Andrew Kaster <andrew@ladybird.org>
This commit is contained in:
parent
d41e577ba1
commit
652af318db
Notes:
github-actions[bot]
2025-02-13 05:32:46 +00:00
Author: https://github.com/stasoid
Commit: 652af318db
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2643
Reviewed-by: https://github.com/ADKaster
Reviewed-by: https://github.com/R-Goc
Reviewed-by: https://github.com/konradekk
12 changed files with 416 additions and 29 deletions
41
Libraries/LibIPC/FileWindows.cpp
Normal file
41
Libraries/LibIPC/FileWindows.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2025, stasoid <stasoid@yahoo.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibIPC/Decoder.h>
|
||||
#include <LibIPC/File.h>
|
||||
#include <LibIPC/HandleType.h>
|
||||
|
||||
#include <AK/Windows.h>
|
||||
|
||||
namespace IPC {
|
||||
|
||||
ErrorOr<void> File::clear_close_on_exec()
|
||||
{
|
||||
if (!SetHandleInformation(to_handle(m_fd), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
|
||||
return Error::from_windows_error();
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
ErrorOr<File> decode(Decoder& decoder)
|
||||
{
|
||||
auto handle_type = TRY(decoder.decode<HandleType>());
|
||||
int handle = -1;
|
||||
if (handle_type == HandleType::Generic) {
|
||||
TRY(decoder.decode_into(handle));
|
||||
} else if (handle_type == HandleType::Socket) {
|
||||
WSAPROTOCOL_INFO pi = {};
|
||||
TRY(decoder.decode_into({ reinterpret_cast<u8*>(&pi), sizeof(pi) }));
|
||||
handle = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
|
||||
if (handle == -1)
|
||||
return Error::from_windows_error();
|
||||
} else {
|
||||
return Error::from_string_literal("Invalid handle type");
|
||||
}
|
||||
return File::adopt_fd(handle);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue