[Lldb-commits] [lldb] r251034 - Add support for abstract domain sockets.

Oleksiy Vyalov via lldb-commits lldb-commits at lists.llvm.org
Thu Oct 22 10:50:34 PDT 2015


Author: ovyalov
Date: Thu Oct 22 12:50:33 2015
New Revision: 251034

URL: http://llvm.org/viewvc/llvm-project?rev=251034&view=rev
Log:
Add support for abstract domain sockets.

http://reviews.llvm.org/D13970


Added:
    lldb/trunk/include/lldb/Host/linux/AbstractSocket.h
    lldb/trunk/source/Host/linux/AbstractSocket.cpp
Modified:
    lldb/trunk/include/lldb/Host/Socket.h
    lldb/trunk/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h
    lldb/trunk/include/lldb/Host/posix/DomainSocket.h
    lldb/trunk/source/Host/CMakeLists.txt
    lldb/trunk/source/Host/common/Socket.cpp
    lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp
    lldb/trunk/source/Host/posix/DomainSocket.cpp

Modified: lldb/trunk/include/lldb/Host/Socket.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Socket.h?rev=251034&r1=251033&r2=251034&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Socket.h (original)
+++ lldb/trunk/include/lldb/Host/Socket.h Thu Oct 22 12:50:33 2015
@@ -45,7 +45,8 @@ public:
     {
         ProtocolTcp,
         ProtocolUdp,
-        ProtocolUnixDomain
+        ProtocolUnixDomain,
+        ProtocolUnixAbstract
     } SocketProtocol;
 
     static const NativeSocket kInvalidSocketValue;
@@ -69,6 +70,8 @@ public:
     static Error UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket);
     static Error UnixDomainConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
     static Error UnixDomainAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
+    static Error UnixAbstractConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
+    static Error UnixAbstractAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket);
 
     int GetOption (int level, int option_name, int &option_value);
     int SetOption (int level, int option_name, int option_value);

Added: lldb/trunk/include/lldb/Host/linux/AbstractSocket.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/linux/AbstractSocket.h?rev=251034&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Host/linux/AbstractSocket.h (added)
+++ lldb/trunk/include/lldb/Host/linux/AbstractSocket.h Thu Oct 22 12:50:33 2015
@@ -0,0 +1,28 @@
+//===-- AbstractSocket.h ----------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_AbstractSocket_h_
+#define liblldb_AbstractSocket_h_
+
+#include "lldb/Host/posix/DomainSocket.h"
+
+namespace lldb_private
+{
+    class AbstractSocket: public DomainSocket
+    {
+    public:
+        AbstractSocket(bool child_processes_inherit, Error &error);
+
+    protected:
+        size_t GetNameOffset() const override;
+        void DeleteSocketFile(llvm::StringRef name) override;
+    };
+}
+
+#endif // ifndef liblldb_AbstractSocket_h_

Modified: lldb/trunk/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h?rev=251034&r1=251033&r2=251034&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h (original)
+++ lldb/trunk/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h Thu Oct 22 12:50:33 2015
@@ -84,6 +84,8 @@ class ConnectionFileDescriptor : public
 
     lldb::ConnectionStatus NamedSocketAccept(const char *socket_name, Error *error_ptr);
 
+    lldb::ConnectionStatus UnixAbstractSocketConnect(const char *socket_name, Error *error_ptr);
+
     lldb::IOObjectSP m_read_sp;
     lldb::IOObjectSP m_write_sp;
 

Modified: lldb/trunk/include/lldb/Host/posix/DomainSocket.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/posix/DomainSocket.h?rev=251034&r1=251033&r2=251034&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/posix/DomainSocket.h (original)
+++ lldb/trunk/include/lldb/Host/posix/DomainSocket.h Thu Oct 22 12:50:33 2015
@@ -22,6 +22,13 @@ namespace lldb_private
         Error Connect(llvm::StringRef name) override;
         Error Listen(llvm::StringRef name, int backlog) override;
         Error Accept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket) override;
+
+    protected:
+        DomainSocket(SocketProtocol protocol, bool child_processes_inherit, Error &error);
+
+        virtual size_t GetNameOffset() const;
+        virtual void DeleteSocketFile(llvm::StringRef name);
+
     private:
         DomainSocket(NativeSocket socket);
     };

Modified: lldb/trunk/source/Host/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/CMakeLists.txt?rev=251034&r1=251033&r2=251034&view=diff
==============================================================================
--- lldb/trunk/source/Host/CMakeLists.txt (original)
+++ lldb/trunk/source/Host/CMakeLists.txt Thu Oct 22 12:50:33 2015
@@ -121,6 +121,7 @@ else()
         android/HostInfoAndroid.cpp
         android/LibcGlue.cpp
         android/ProcessLauncherAndroid.cpp
+        linux/AbstractSocket.cpp
         linux/Host.cpp
         linux/HostInfoLinux.cpp
         linux/HostThreadLinux.cpp
@@ -129,6 +130,7 @@ else()
         )
     else()
       add_host_subdirectory(linux
+        linux/AbstractSocket.cpp
         linux/Host.cpp
         linux/HostInfoLinux.cpp
         linux/HostThreadLinux.cpp

Modified: lldb/trunk/source/Host/common/Socket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Socket.cpp?rev=251034&r1=251033&r2=251034&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Socket.cpp (original)
+++ lldb/trunk/source/Host/common/Socket.cpp Thu Oct 22 12:50:33 2015
@@ -30,6 +30,10 @@
 #include <sys/un.h>
 #endif
 
+#ifdef __linux__
+#include "lldb/Host/linux/AbstractSocket.h"
+#endif
+
 #ifdef __ANDROID_NDK__
 #include <linux/tcp.h>
 #include <bits/error_constants.h>
@@ -189,6 +193,44 @@ Error Socket::UnixDomainAccept(llvm::Str
 #endif
     return error;
 }
+
+Error
+Socket::UnixAbstractConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
+{
+    Error error;
+#ifdef __linux__
+    std::unique_ptr<Socket> connect_socket(new AbstractSocket(child_processes_inherit, error));
+    if (error.Fail())
+        return error;
+
+    error = connect_socket->Connect(name);
+    if (error.Success())
+      socket = connect_socket.release();
+#else
+    error.SetErrorString("Abstract domain sockets are not supported on this platform.");
+#endif
+    return error;
+}
+
+Error
+Socket::UnixAbstractAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
+{
+    Error error;
+#ifdef __linux__
+    std::unique_ptr<Socket> listen_socket(new AbstractSocket(child_processes_inherit, error));
+    if (error.Fail())
+        return error;
+
+    error = listen_socket->Listen(name, 5);
+    if (error.Fail())
+        return error;
+
+    error = listen_socket->Accept(name, child_processes_inherit, socket);
+#else
+    error.SetErrorString("Abstract domain sockets are not supported on this platform.");
+#endif
+    return error;
+}
 
 bool
 Socket::DecodeHostAndPort(llvm::StringRef host_and_port,

Added: lldb/trunk/source/Host/linux/AbstractSocket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/linux/AbstractSocket.cpp?rev=251034&view=auto
==============================================================================
--- lldb/trunk/source/Host/linux/AbstractSocket.cpp (added)
+++ lldb/trunk/source/Host/linux/AbstractSocket.cpp Thu Oct 22 12:50:33 2015
@@ -0,0 +1,31 @@
+//===-- AbstractSocket.cpp --------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/linux/AbstractSocket.h"
+
+#include "llvm/ADT/StringRef.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+AbstractSocket::AbstractSocket(bool child_processes_inherit, Error &error)
+    : DomainSocket(ProtocolUnixAbstract, child_processes_inherit, error)
+{
+}
+
+size_t
+AbstractSocket::GetNameOffset() const
+{
+    return 1;
+}
+
+void
+AbstractSocket::DeleteSocketFile(llvm::StringRef name)
+{
+}

Modified: lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp?rev=251034&r1=251033&r2=251034&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp (original)
+++ lldb/trunk/source/Host/posix/ConnectionFileDescriptorPosix.cpp Thu Oct 22 12:50:33 2015
@@ -186,6 +186,11 @@ ConnectionFileDescriptor::Connect(const
             // unix-connect://SOCKNAME
             return NamedSocketConnect(s + strlen("unix-connect://"), error_ptr);
         }
+        else if (strstr(s, "unix-abstract-connect://") == s)
+        {
+            // unix-abstract-connect://SOCKNAME
+            return UnixAbstractSocketConnect(s + strlen("unix-abstract-connect://"), error_ptr);
+        }
 #ifndef LLDB_DISABLE_POSIX
         else if (strstr(s, "fd://") == s)
         {
@@ -754,6 +759,23 @@ ConnectionFileDescriptor::NamedSocketCon
     if (error_ptr)
         *error_ptr = error;
     m_write_sp.reset(socket);
+    m_read_sp = m_write_sp;
+    if (error.Fail())
+    {
+        return eConnectionStatusError;
+    }
+    m_uri.assign(socket_name);
+    return eConnectionStatusSuccess;
+}
+
+lldb::ConnectionStatus
+ConnectionFileDescriptor::UnixAbstractSocketConnect(const char *socket_name, Error *error_ptr)
+{
+    Socket *socket = nullptr;
+    Error error = Socket::UnixAbstractConnect(socket_name, m_child_processes_inherit, socket);
+    if (error_ptr)
+        *error_ptr = error;
+    m_write_sp.reset(socket);
     m_read_sp = m_write_sp;
     if (error.Fail())
     {

Modified: lldb/trunk/source/Host/posix/DomainSocket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/DomainSocket.cpp?rev=251034&r1=251033&r2=251034&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/DomainSocket.cpp (original)
+++ lldb/trunk/source/Host/posix/DomainSocket.cpp Thu Oct 22 12:50:33 2015
@@ -29,14 +29,19 @@ namespace {
 const int kDomain = AF_UNIX;
 const int kType   = SOCK_STREAM;
 
-void SetSockAddr(llvm::StringRef name, sockaddr_un* saddr_un)
+bool SetSockAddr(llvm::StringRef name, const size_t name_offset, sockaddr_un* saddr_un)
 {
+    if (name.size() + name_offset > sizeof(saddr_un->sun_path))
+        return false;
+
     saddr_un->sun_family = kDomain;
-    ::strncpy(saddr_un->sun_path, name.data(), sizeof(saddr_un->sun_path) - 1);
-    saddr_un->sun_path[sizeof(saddr_un->sun_path) - 1] = '\0';
+    memset(saddr_un->sun_path, 0, sizeof(saddr_un->sun_path));
+
+    strncpy(&saddr_un->sun_path[name_offset], name.data(), name.size());
 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
     saddr_un->sun_len = SUN_LEN (saddr_un);
 #endif
+    return true;
 }
 
 }
@@ -51,14 +56,20 @@ DomainSocket::DomainSocket(bool child_pr
 {
 }
 
+DomainSocket::DomainSocket(SocketProtocol protocol, bool child_processes_inherit, Error &error)
+    : Socket(CreateSocket(kDomain, kType, 0, child_processes_inherit, error), protocol, true)
+{
+}
+
 Error
 DomainSocket::Connect(llvm::StringRef name)
 {
     sockaddr_un saddr_un;
-    SetSockAddr(name, &saddr_un);
+    if (!SetSockAddr(name, GetNameOffset(), &saddr_un))
+        return Error("Failed to set socket address");
 
     Error error;
-    if (::connect(GetNativeSocket(), (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0)
+    if (::connect(GetNativeSocket(), (struct sockaddr *)&saddr_un, sizeof(saddr_un)) < 0)
         SetLastError (error);
 
     return error;
@@ -68,12 +79,13 @@ Error
 DomainSocket::Listen(llvm::StringRef name, int backlog)
 {
     sockaddr_un saddr_un;
-    SetSockAddr(name, &saddr_un);
+    if (!SetSockAddr(name, GetNameOffset(), &saddr_un))
+        return Error("Failed to set socket address");
 
-    FileSystem::Unlink(FileSpec{name, true});
+    DeleteSocketFile(name);
 
     Error error;
-    if (::bind(GetNativeSocket(), (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0)
+    if (::bind(GetNativeSocket(), (struct sockaddr *)&saddr_un, sizeof(saddr_un)) == 0)
         if (::listen(GetNativeSocket(), backlog) == 0)
             return error;
 
@@ -91,3 +103,15 @@ DomainSocket::Accept(llvm::StringRef nam
 
     return error;
 }
+
+size_t
+DomainSocket::GetNameOffset() const
+{
+    return 0;
+}
+
+void
+DomainSocket::DeleteSocketFile(llvm::StringRef name)
+{
+    FileSystem::Unlink(FileSpec{name, true});
+}




More information about the lldb-commits mailing list