From 259ed0408791f42835aac7f0adce6c98151828ef Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sat, 15 Jan 2022 12:10:04 +0000 Subject: [PATCH] LibCore: Implement LocalSocket::release_fd release_fd() releases the fd associated with the LocalSocket it is called on. This is analogous to release_value() on container objects in AK, after which the object does not contain the value. --- Userland/Libraries/LibCore/Stream.cpp | 11 +++++++++++ Userland/Libraries/LibCore/Stream.h | 6 ++++++ 2 files changed, 17 insertions(+) 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: