diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index e8f26757b2..5a8ba56e05 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -535,4 +535,15 @@ ErrorOr LocalSocket::read_without_waiting(Bytes buffer) return m_helper.read(buffer, MSG_DONTWAIT); } +ErrorOr LocalSocket::release_fd() +{ + if (!is_open()) { + return Error::from_errno(ENOTCONN); + } + + auto fd = m_helper.fd(); + m_helper.set_fd(-1); + return fd; +} + } diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index 9bfe2c4b81..3af5633653 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -416,6 +416,12 @@ public: ErrorOr peer_pid() const; ErrorOr read_without_waiting(Bytes buffer); + /// Release the fd associated with this LocalSocket. After the fd is + /// released, the socket will be considered "closed" and all operations done + /// on it will fail with ENOTCONN. Fails with ENOTCONN if the socket is + /// already closed. + ErrorOr release_fd(); + virtual ~LocalSocket() { close(); } private: