-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LibIPC: Port to Windows #2643
Draft
stasoid
wants to merge
1
commit into
LadybirdBrowser:master
Choose a base branch
from
stasoid:ipc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+402
−56
Draft
LibIPC: Port to Windows #2643
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org> | ||
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org> | ||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <LibIPC/Decoder.h> | ||
#include <LibIPC/File.h> | ||
|
||
namespace IPC { | ||
|
||
// FIXME: IPC::Files transferred over the wire are always set O_CLOEXEC during decoding. | ||
// Perhaps we should add an option to IPC::File to allow the receiver to decide whether to | ||
// make it O_CLOEXEC or not. Or an attribute in the .ipc file? | ||
stasoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ErrorOr<void> File::clear_close_on_exec() | ||
{ | ||
auto fd_flags = TRY(Core::System::fcntl(m_fd, F_GETFD)); | ||
fd_flags &= ~FD_CLOEXEC; | ||
TRY(Core::System::fcntl(m_fd, F_SETFD, fd_flags)); | ||
return {}; | ||
} | ||
|
||
template<> | ||
ErrorOr<File> decode(Decoder& decoder) | ||
{ | ||
auto file = TRY(decoder.files().try_dequeue()); | ||
auto fd = file.fd(); | ||
|
||
auto fd_flags = TRY(Core::System::fcntl(fd, F_GETFD)); | ||
TRY(Core::System::fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC)); | ||
return file; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2024, stasoid <stasoid@yahoo.com> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <LibIPC/Decoder.h> | ||
#include <LibIPC/File.h> | ||
|
||
#include <AK/Windows.h> | ||
|
||
namespace IPC { | ||
|
||
ErrorOr<void> File::clear_close_on_exec() | ||
{ | ||
if (!SetHandleInformation(Core::System::fd_to_handle(m_fd), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) | ||
return Error::from_windows_error(); | ||
return {}; | ||
} | ||
|
||
template<> | ||
ErrorOr<File> decode(Decoder& decoder) | ||
{ | ||
using namespace Core::System; | ||
auto handle_type = TRY(decoder.decode<HandleType>()); | ||
intptr_t handle = 0; | ||
if (handle_type == FileMappingHandle) { | ||
TRY(decoder.decode_into(handle)); | ||
} else if (handle_type == SocketHandle) { | ||
WSAPROTOCOL_INFO pi = {}; | ||
TRY(decoder.decode_into({ (u8*)&pi, sizeof(pi) })); | ||
handle = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, &pi, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); | ||
stasoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (handle == -1) | ||
return Error::from_windows_error(); | ||
stasoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
return Error::from_string_literal("Invalid handle type"); | ||
} | ||
int fd = handle_to_fd(handle, handle_type); | ||
return File::adopt_fd(fd); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> | ||
* Copyright (c) 2024, stasoid <stasoid@yahoo.com> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <LibIPC/Message.h> | ||
|
||
#include <AK/Windows.h> | ||
|
||
namespace IPC { | ||
|
||
using MessageSizeType = u32; | ||
|
||
MessageBuffer::MessageBuffer() | ||
{ | ||
m_data.resize(sizeof(MessageSizeType)); | ||
} | ||
|
||
ErrorOr<void> MessageBuffer::extend_data_capacity(size_t capacity) | ||
{ | ||
TRY(m_data.try_ensure_capacity(m_data.size() + capacity)); | ||
return {}; | ||
} | ||
|
||
ErrorOr<void> MessageBuffer::append_data(u8 const* values, size_t count) | ||
{ | ||
TRY(m_data.try_append(values, count)); | ||
return {}; | ||
} | ||
|
||
ErrorOr<void> MessageBuffer::append_file_descriptor(int fd) | ||
{ | ||
using namespace Core::System; | ||
|
||
HANDLE handle = fd_to_handle(fd); | ||
if (handle == INVALID_HANDLE_VALUE) | ||
return Error::from_string_literal("Invalid file descriptor"); | ||
|
||
m_fds.append(adopt_ref(*new AutoCloseFileDescriptor(fd))); | ||
m_handle_offsets.append(m_data.size()); | ||
|
||
if (is_socket(fd)) { | ||
HandleType type = SocketHandle; | ||
m_data.append((u8*)&type, sizeof(type)); | ||
WSAPROTOCOL_INFO pi = {}; | ||
*(HANDLE*)&pi = handle; | ||
// the handle will be duplicated and WSAPROTOCOL_INFO will be filled later in TransportSocketWindows::transfer | ||
m_data.append((u8*)&pi, sizeof(pi)); | ||
} else { | ||
HandleType type = FileMappingHandle; | ||
m_data.append((u8*)&type, sizeof(type)); | ||
// the handle will be overwritten by a duplicate handle later in TransportSocketWindows::transfer | ||
m_data.append((u8*)&handle, sizeof(handle)); | ||
} | ||
return {}; | ||
} | ||
|
||
ErrorOr<void> MessageBuffer::transfer_message(Transport& transport) | ||
{ | ||
VERIFY(m_data.size() >= sizeof(MessageSizeType) && m_data.size() < NumericLimits<MessageSizeType>::max()); | ||
size_t message_size = m_data.size() - sizeof(MessageSizeType); | ||
*(MessageSizeType*)m_data.data() = message_size; | ||
|
||
TRY(transport.transfer(m_data.span(), m_handle_offsets)); | ||
return {}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the order of declarations, which makes the diff unreadable. This is a diff that shows changes of substance (without reordering or
inline
removing).