Explorar o código

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.
sin-ack %!s(int64=3) %!d(string=hai) anos
pai
achega
92be52fd9f
Modificáronse 2 ficheiros con 13 adicións e 0 borrados
  1. 12 0
      Userland/Libraries/LibCore/Stream.cpp
  2. 1 0
      Userland/Libraries/LibCore/Stream.h

+ 12 - 0
Userland/Libraries/LibCore/Stream.cpp

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

+ 1 - 0
Userland/Libraries/LibCore/Stream.h

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