diff --git a/Libraries/LibCore/Socket.cpp b/Libraries/LibCore/Socket.cpp index 1d1b9eb6919..dc5af41edc3 100644 --- a/Libraries/LibCore/Socket.cpp +++ b/Libraries/LibCore/Socket.cpp @@ -193,15 +193,7 @@ ErrorOr PosixSocketHelper::set_blocking(bool enabled) ErrorOr 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 PosixSocketHelper::set_receive_timeout(AK::Duration timeout) diff --git a/Libraries/LibCore/SocketWindows.cpp b/Libraries/LibCore/SocketWindows.cpp index 33b83b2f79e..65d9217ec42 100644 --- a/Libraries/LibCore/SocketWindows.cpp +++ b/Libraries/LibCore/SocketWindows.cpp @@ -106,9 +106,7 @@ ErrorOr PosixSocketHelper::set_blocking(bool) ErrorOr 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 PosixSocketHelper::pending_bytes() const diff --git a/Libraries/LibCore/System.cpp b/Libraries/LibCore/System.cpp index 586a3a9070c..1d37a7041f1 100644 --- a/Libraries/LibCore/System.cpp +++ b/Libraries/LibCore/System.cpp @@ -1017,4 +1017,17 @@ ErrorOr sleep_ms(u32 milliseconds) return {}; } +ErrorOr 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 {}; +} + } diff --git a/Libraries/LibCore/System.h b/Libraries/LibCore/System.h index 323982dd880..0a0b3eb122b 100644 --- a/Libraries/LibCore/System.h +++ b/Libraries/LibCore/System.h @@ -184,5 +184,6 @@ ErrorOr set_resource_limits(int resource, rlim_t limit); int getpid(); bool is_socket(int fd); ErrorOr sleep_ms(u32 milliseconds); +ErrorOr set_close_on_exec(int fd, bool enabled); } diff --git a/Libraries/LibCore/SystemWindows.cpp b/Libraries/LibCore/SystemWindows.cpp index f46508f7a37..d5b021bf9cf 100644 --- a/Libraries/LibCore/SystemWindows.cpp +++ b/Libraries/LibCore/SystemWindows.cpp @@ -269,4 +269,11 @@ ErrorOr current_executable_path() return TRY(Process::get_name()).to_byte_string(); } +ErrorOr 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 {}; +} + } diff --git a/Libraries/LibIPC/File.cpp b/Libraries/LibIPC/File.cpp index 148a9c7d849..6f664564059 100644 --- a/Libraries/LibIPC/File.cpp +++ b/Libraries/LibIPC/File.cpp @@ -11,25 +11,11 @@ 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 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 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)); + TRY(Core::System::set_close_on_exec(file.fd(), true)); return file; } diff --git a/Libraries/LibIPC/File.h b/Libraries/LibIPC/File.h index 77c5f3d86be..5aa1001010e 100644 --- a/Libraries/LibIPC/File.h +++ b/Libraries/LibIPC/File.h @@ -63,7 +63,13 @@ public: return exchange(m_fd, -1); } - ErrorOr 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 clear_close_on_exec() + { + return Core::System::set_close_on_exec(m_fd, false); + } private: explicit File(int fd) diff --git a/Libraries/LibIPC/FileWindows.cpp b/Libraries/LibIPC/FileWindows.cpp index 2999e1f1847..dfc7be512de 100644 --- a/Libraries/LibIPC/FileWindows.cpp +++ b/Libraries/LibIPC/FileWindows.cpp @@ -12,13 +12,6 @@ namespace IPC { -ErrorOr 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 decode(Decoder& decoder) {