1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmDebuggerPosixPipeConnection.h"
12 #include <sys/socket.h>
14 namespace cmDebugger
{
18 cmDebuggerPipeConnection_POSIX::cmDebuggerPipeConnection_POSIX(
20 : PipeName(std::move(name
))
22 addr
.sun_path
[0] = '\0';
25 cmDebuggerPipeConnection_POSIX::~cmDebuggerPipeConnection_POSIX()
32 bool cmDebuggerPipeConnection_POSIX::StartListening(std::string
& errorMessage
)
34 listen_fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
36 errorMessage
= "Failed to create socket: ";
37 errorMessage
+= strerror(errno
);
41 addr
.sun_family
= AF_UNIX
;
42 strncpy(addr
.sun_path
, PipeName
.c_str(), sizeof(addr
.sun_path
));
43 addr
.sun_path
[sizeof(addr
.sun_path
) - 1] = '\0';
44 if (bind(listen_fd
, (sockaddr
*)&addr
, sizeof(addr
)) == -1) {
45 errorMessage
= "Failed to bind name '";
46 errorMessage
+= addr
.sun_path
;
47 errorMessage
+= "' to socket: ";
48 errorMessage
+= strerror(errno
);
53 if (listen(listen_fd
, 1) == -1) {
54 errorMessage
= "Failed to listen on socket: ";
55 errorMessage
+= strerror(errno
);
60 StartedListening
.set_value();
64 std::shared_ptr
<dap::Reader
> cmDebuggerPipeConnection_POSIX::GetReader()
66 return std::static_pointer_cast
<dap::Reader
>(shared_from_this());
69 std::shared_ptr
<dap::Writer
> cmDebuggerPipeConnection_POSIX::GetWriter()
71 return std::static_pointer_cast
<dap::Writer
>(shared_from_this());
74 bool cmDebuggerPipeConnection_POSIX::isOpen()
79 void cmDebuggerPipeConnection_POSIX::close()
86 void cmDebuggerPipeConnection_POSIX::close_listen()
88 if (strlen(addr
.sun_path
) > 0) {
89 unlink(addr
.sun_path
);
90 addr
.sun_path
[0] = '\0';
96 void cmDebuggerPipeConnection_POSIX::WaitForConnection()
99 socklen_t len
= sizeof(laddr
);
100 rw_pipe
= accept(listen_fd
, (sockaddr
*)&laddr
, &len
);
106 close_listen(); // no longer need the listen resources
109 size_t cmDebuggerPipeConnection_POSIX::read(void* buffer
, size_t n
)
113 result
= ::read(rw_pipe
, buffer
, n
);
122 bool cmDebuggerPipeConnection_POSIX::write(void const* buffer
, size_t n
)
126 result
= ::write(rw_pipe
, buffer
, n
) >= 0;
135 cmDebuggerPipeClient_POSIX::cmDebuggerPipeClient_POSIX(std::string name
)
136 : PipeName(std::move(name
))
140 cmDebuggerPipeClient_POSIX::~cmDebuggerPipeClient_POSIX()
145 void cmDebuggerPipeClient_POSIX::WaitForConnection()
147 rw_pipe
= socket(AF_UNIX
, SOCK_STREAM
, 0);
149 throw std::runtime_error(std::string("Failed to create socket: ") +
154 addr
.sun_family
= AF_UNIX
;
155 strncpy(addr
.sun_path
, PipeName
.c_str(), sizeof(addr
.sun_path
));
156 addr
.sun_path
[sizeof(addr
.sun_path
) - 1] = '\0';
157 if (connect(rw_pipe
, (sockaddr
*)&addr
, sizeof(addr
)) == -1) {
159 throw std::runtime_error(
160 std::string("Failed to connect path to socket: ") + strerror(errno
));
164 bool cmDebuggerPipeClient_POSIX::isOpen()
169 void cmDebuggerPipeClient_POSIX::close()
177 size_t cmDebuggerPipeClient_POSIX::read(void* buffer
, size_t n
)
181 count
= static_cast<int>(::read(rw_pipe
, buffer
, n
));
190 bool cmDebuggerPipeClient_POSIX::write(void const* buffer
, size_t n
)
194 count
= static_cast<int>(::write(rw_pipe
, buffer
, n
));
205 } // namespace cmDebugger