1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:27:45 +00:00

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.
This commit is contained in:
sin-ack 2022-01-15 12:10:04 +00:00 committed by Andreas Kling
parent 89d9a1afc0
commit 259ed04087
2 changed files with 17 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -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: