LibCore: Implement LocalSocket::adopt_fd

Similar to File::adopt_fd, this function creates a new LocalSocket with
an existing fd. The main use of this function is to create LocalSocket
objects from fds that have been passed to us by SystemServer to take
over.
This commit is contained in:
sin-ack 2022-01-14 13:24:01 +00:00 committed by Ali Mohammad Pur
parent 27b49a60d0
commit 92be52fd9f
Notes: sideshowbarker 2024-07-17 20:52:27 +09:00
2 changed files with 13 additions and 0 deletions
Userland/Libraries/LibCore

View file

@ -536,6 +536,18 @@ ErrorOr<NonnullOwnPtr<LocalSocket>> LocalSocket::connect(String const& path)
return socket;
}
ErrorOr<NonnullOwnPtr<LocalSocket>> LocalSocket::adopt_fd(int fd)
{
if (fd < 0) {
return Error::from_errno(EBADF);
}
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) LocalSocket()));
socket->m_helper.set_fd(fd);
socket->setup_notifier();
return socket;
}
ErrorOr<int> LocalSocket::receive_fd(int flags)
{
#ifdef __serenity__

View file

@ -379,6 +379,7 @@ private:
class LocalSocket final : public Socket {
public:
static ErrorOr<NonnullOwnPtr<LocalSocket>> connect(String const& path);
static ErrorOr<NonnullOwnPtr<LocalSocket>> adopt_fd(int fd);
LocalSocket(LocalSocket&& other)
: Socket(static_cast<Socket&&>(other))