diff options
author | sin-ack <sin-ack@users.noreply.github.com> | 2022-01-15 12:10:04 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-06 10:28:19 +0100 |
commit | 259ed0408791f42835aac7f0adce6c98151828ef (patch) | |
tree | 0ff23dc8bd70665d300f33b3ad7a281b6938eea0 /Userland/Libraries/LibCore | |
parent | 89d9a1afc05c57a8c2e11ab92d2ab2e9f8dda357 (diff) | |
download | serenity-259ed0408791f42835aac7f0adce6c98151828ef.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r-- | Userland/Libraries/LibCore/Stream.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Stream.h | 6 |
2 files changed, 17 insertions, 0 deletions
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<size_t> LocalSocket::read_without_waiting(Bytes buffer) return m_helper.read(buffer, MSG_DONTWAIT); } +ErrorOr<int> 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<pid_t> peer_pid() const; ErrorOr<size_t> 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<int> release_fd(); + virtual ~LocalSocket() { close(); } private: |