LibCore: Implement System::set_close_on_exec

This commit is contained in:
stasoid 2025-02-13 18:50:23 +05:00 committed by Andrew Kaster
commit 2abc792938
Notes: github-actions[bot] 2025-03-20 02:26:29 +00:00
8 changed files with 31 additions and 35 deletions

View file

@ -193,15 +193,7 @@ ErrorOr<void> PosixSocketHelper::set_blocking(bool enabled)
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
{
int flags = TRY(System::fcntl(m_fd, F_GETFD));
if (enabled)
flags |= FD_CLOEXEC;
else
flags &= ~FD_CLOEXEC;
TRY(System::fcntl(m_fd, F_SETFD, flags));
return {};
return System::set_close_on_exec(m_fd, enabled);
}
ErrorOr<void> PosixSocketHelper::set_receive_timeout(AK::Duration timeout)

View file

@ -106,9 +106,7 @@ ErrorOr<void> PosixSocketHelper::set_blocking(bool)
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
{
if (!SetHandleInformation(to_handle(m_fd), HANDLE_FLAG_INHERIT, enabled ? 0 : HANDLE_FLAG_INHERIT))
return Error::from_windows_error();
return {};
return System::set_close_on_exec(m_fd, enabled);
}
ErrorOr<size_t> PosixSocketHelper::pending_bytes() const

View file

@ -1017,4 +1017,17 @@ ErrorOr<void> sleep_ms(u32 milliseconds)
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 {};
}
}

View file

@ -184,5 +184,6 @@ ErrorOr<void> set_resource_limits(int resource, rlim_t limit);
int getpid();
bool is_socket(int fd);
ErrorOr<void> sleep_ms(u32 milliseconds);
ErrorOr<void> set_close_on_exec(int fd, bool enabled);
}

View file

@ -269,4 +269,11 @@ ErrorOr<ByteString> current_executable_path()
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 {};
}
}