mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-15 21:41:58 +00:00
LibCore: Implement System::set_close_on_exec
This commit is contained in:
parent
2e200489c8
commit
2abc792938
Notes:
github-actions[bot]
2025-03-20 02:26:29 +00:00
Author: https://github.com/stasoid
Commit: 2abc792938
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3002
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/AtkinsSJ
8 changed files with 31 additions and 35 deletions
|
@ -193,15 +193,7 @@ ErrorOr<void> PosixSocketHelper::set_blocking(bool enabled)
|
||||||
|
|
||||||
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
|
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
|
||||||
{
|
{
|
||||||
int flags = TRY(System::fcntl(m_fd, F_GETFD));
|
return System::set_close_on_exec(m_fd, enabled);
|
||||||
|
|
||||||
if (enabled)
|
|
||||||
flags |= FD_CLOEXEC;
|
|
||||||
else
|
|
||||||
flags &= ~FD_CLOEXEC;
|
|
||||||
|
|
||||||
TRY(System::fcntl(m_fd, F_SETFD, flags));
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<void> PosixSocketHelper::set_receive_timeout(AK::Duration timeout)
|
ErrorOr<void> PosixSocketHelper::set_receive_timeout(AK::Duration timeout)
|
||||||
|
|
|
@ -106,9 +106,7 @@ ErrorOr<void> PosixSocketHelper::set_blocking(bool)
|
||||||
|
|
||||||
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
|
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
|
||||||
{
|
{
|
||||||
if (!SetHandleInformation(to_handle(m_fd), HANDLE_FLAG_INHERIT, enabled ? 0 : HANDLE_FLAG_INHERIT))
|
return System::set_close_on_exec(m_fd, enabled);
|
||||||
return Error::from_windows_error();
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<size_t> PosixSocketHelper::pending_bytes() const
|
ErrorOr<size_t> PosixSocketHelper::pending_bytes() const
|
||||||
|
|
|
@ -1017,4 +1017,17 @@ ErrorOr<void> sleep_ms(u32 milliseconds)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> set_close_on_exec(int fd, bool enabled)
|
||||||
|
{
|
||||||
|
int flags = TRY(fcntl(fd, F_GETFD));
|
||||||
|
|
||||||
|
if (enabled)
|
||||||
|
flags |= FD_CLOEXEC;
|
||||||
|
else
|
||||||
|
flags &= ~FD_CLOEXEC;
|
||||||
|
|
||||||
|
TRY(fcntl(fd, F_SETFD, flags));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -184,5 +184,6 @@ ErrorOr<void> set_resource_limits(int resource, rlim_t limit);
|
||||||
int getpid();
|
int getpid();
|
||||||
bool is_socket(int fd);
|
bool is_socket(int fd);
|
||||||
ErrorOr<void> sleep_ms(u32 milliseconds);
|
ErrorOr<void> sleep_ms(u32 milliseconds);
|
||||||
|
ErrorOr<void> set_close_on_exec(int fd, bool enabled);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -269,4 +269,11 @@ ErrorOr<ByteString> current_executable_path()
|
||||||
return TRY(Process::get_name()).to_byte_string();
|
return TRY(Process::get_name()).to_byte_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> set_close_on_exec(int handle, bool enabled)
|
||||||
|
{
|
||||||
|
if (!SetHandleInformation(to_handle(handle), HANDLE_FLAG_INHERIT, enabled ? 0 : HANDLE_FLAG_INHERIT))
|
||||||
|
return Error::from_windows_error();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,25 +11,11 @@
|
||||||
|
|
||||||
namespace IPC {
|
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<>
|
template<>
|
||||||
ErrorOr<File> decode(Decoder& decoder)
|
ErrorOr<File> decode(Decoder& decoder)
|
||||||
{
|
{
|
||||||
auto file = TRY(decoder.files().try_dequeue());
|
auto file = TRY(decoder.files().try_dequeue());
|
||||||
auto fd = file.fd();
|
TRY(Core::System::set_close_on_exec(file.fd(), true));
|
||||||
|
|
||||||
auto fd_flags = TRY(Core::System::fcntl(fd, F_GETFD));
|
|
||||||
TRY(Core::System::fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC));
|
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,13 @@ public:
|
||||||
return exchange(m_fd, -1);
|
return exchange(m_fd, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<void> clear_close_on_exec();
|
// 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> clear_close_on_exec()
|
||||||
|
{
|
||||||
|
return Core::System::set_close_on_exec(m_fd, false);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit File(int fd)
|
explicit File(int fd)
|
||||||
|
|
|
@ -12,13 +12,6 @@
|
||||||
|
|
||||||
namespace IPC {
|
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<>
|
template<>
|
||||||
ErrorOr<File> decode(Decoder& decoder)
|
ErrorOr<File> decode(Decoder& decoder)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue