-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
401 additions
and
57 deletions.
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 <[email protected]> | ||
* Copyright (c) 2021, Andreas Kling <[email protected]> | ||
* Copyright (c) 2023, Tim Flynn <[email protected]> | ||
* | ||
* 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? | ||
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,43 @@ | ||
/* | ||
* Copyright (c) 2024, stasoid <[email protected]> | ||
* | ||
* 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) | ||
{ | ||
auto handle_type = TRY(decoder.decode<Core::System::HandleType>()); | ||
intptr_t handle = 0; | ||
if (handle_type == Core::System::FileMappingHandle) { | ||
TRY(decoder.decode_into(handle)); | ||
} else if (handle_type == Core::System::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); | ||
if (handle == -1) | ||
return Error::from_windows_error(); | ||
} else { | ||
return Error::from_string_literal("Invalid handle type"); | ||
} | ||
int fd = Core::System::handle_to_fd(handle, handle_type); | ||
if (fd == -1) | ||
return Error::from_string_literal("Invalid handle"); | ||
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 <[email protected]> | ||
* Copyright (c) 2024, stasoid <[email protected]> | ||
* | ||
* 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 (fd < 0) { | ||
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)); | ||
} else { | ||
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)); | ||
} | ||
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.