1 //===-- ConnectionFileDescriptorPosix.h -------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_Host_posix_ConnectionFileDescriptorPosix_h_
11 #define liblldb_Host_posix_ConnectionFileDescriptorPosix_h_
12 
13 #include <atomic>
14 #include <memory>
15 #include <mutex>
16 
17 #include "lldb/lldb-forward.h"
18 
19 #include "lldb/Host/Pipe.h"
20 #include "lldb/Utility/Connection.h"
21 #include "lldb/Utility/IOObject.h"
22 #include "lldb/Utility/Predicate.h"
23 
24 namespace lldb_private {
25 
26 class Status;
27 class Socket;
28 class SocketAddress;
29 
30 class ConnectionFileDescriptor : public Connection {
31 public:
32   static const char *LISTEN_SCHEME;
33   static const char *ACCEPT_SCHEME;
34   static const char *UNIX_ACCEPT_SCHEME;
35   static const char *CONNECT_SCHEME;
36   static const char *TCP_CONNECT_SCHEME;
37   static const char *UDP_SCHEME;
38   static const char *UNIX_CONNECT_SCHEME;
39   static const char *UNIX_ABSTRACT_CONNECT_SCHEME;
40   static const char *FD_SCHEME;
41   static const char *FILE_SCHEME;
42 
43   ConnectionFileDescriptor(bool child_processes_inherit = false);
44 
45   ConnectionFileDescriptor(int fd, bool owns_fd);
46 
47   ConnectionFileDescriptor(Socket *socket);
48 
49   ~ConnectionFileDescriptor() override;
50 
51   bool IsConnected() const override;
52 
53   lldb::ConnectionStatus Connect(llvm::StringRef s, Status *error_ptr) override;
54 
55   lldb::ConnectionStatus Disconnect(Status *error_ptr) override;
56 
57   size_t Read(void *dst, size_t dst_len, const Timeout<std::micro> &timeout,
58               lldb::ConnectionStatus &status, Status *error_ptr) override;
59 
60   size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status,
61                Status *error_ptr) override;
62 
63   std::string GetURI() override;
64 
65   lldb::ConnectionStatus BytesAvailable(const Timeout<std::micro> &timeout,
66                                         Status *error_ptr);
67 
68   bool InterruptRead() override;
69 
GetReadObject()70   lldb::IOObjectSP GetReadObject() override { return m_read_sp; }
71 
72   uint16_t GetListeningPort(const Timeout<std::micro> &timeout);
73 
74   bool GetChildProcessesInherit() const;
75   void SetChildProcessesInherit(bool child_processes_inherit);
76 
77 protected:
78   void OpenCommandPipe();
79 
80   void CloseCommandPipe();
81 
82   lldb::ConnectionStatus SocketListenAndAccept(llvm::StringRef host_and_port,
83                                                Status *error_ptr);
84 
85   lldb::ConnectionStatus ConnectTCP(llvm::StringRef host_and_port,
86                                     Status *error_ptr);
87 
88   lldb::ConnectionStatus ConnectUDP(llvm::StringRef args, Status *error_ptr);
89 
90   lldb::ConnectionStatus NamedSocketConnect(llvm::StringRef socket_name,
91                                             Status *error_ptr);
92 
93   lldb::ConnectionStatus NamedSocketAccept(llvm::StringRef socket_name,
94                                            Status *error_ptr);
95 
96   lldb::ConnectionStatus UnixAbstractSocketConnect(llvm::StringRef socket_name,
97                                                    Status *error_ptr);
98 
99   lldb::IOObjectSP m_read_sp;
100   lldb::IOObjectSP m_write_sp;
101 
102   Predicate<uint16_t>
103       m_port_predicate; // Used when binding to port zero to wait for the thread
104                         // that creates the socket, binds and listens to
105                         // resolve the port number.
106 
107   Pipe m_pipe;
108   std::recursive_mutex m_mutex;
109   std::atomic<bool> m_shutting_down; // This marks that we are shutting down so
110                                      // if we get woken up from
111   // BytesAvailable to disconnect, we won't try to read again.
112   bool m_waiting_for_accept;
113   bool m_child_processes_inherit;
114 
115   std::string m_uri;
116 
117 private:
118   void InitializeSocket(Socket *socket);
119 
120   DISALLOW_COPY_AND_ASSIGN(ConnectionFileDescriptor);
121 };
122 
123 } // namespace lldb_private
124 
125 #endif // liblldb_ConnectionFileDescriptor_h_
126