1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDebuggerPosixPipeConnection.h"
#include <cerrno>
#include <cstring>
#include <stdexcept>
#include <utility>
#include <unistd.h>
#include <sys/socket.h>
namespace cmDebugger {
#ifndef _WIN32
cmDebuggerPipeConnection_POSIX::cmDebuggerPipeConnection_POSIX(
std::string name)
: PipeName(std::move(name))
{
addr.sun_path[0] = '\0';
}
cmDebuggerPipeConnection_POSIX::~cmDebuggerPipeConnection_POSIX()
{
if (isOpen()) {
close();
}
}
bool cmDebuggerPipeConnection_POSIX::StartListening(std::string& errorMessage)
{
listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (listen_fd < 0) {
errorMessage = "Failed to create socket: ";
errorMessage += strerror(errno);
return false;
}
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, PipeName.c_str(), sizeof(addr.sun_path));
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
if (bind(listen_fd, (sockaddr*)&addr, sizeof(addr)) == -1) {
errorMessage = "Failed to bind name '";
errorMessage += addr.sun_path;
errorMessage += "' to socket: ";
errorMessage += strerror(errno);
close_listen();
return false;
}
if (listen(listen_fd, 1) == -1) {
errorMessage = "Failed to listen on socket: ";
errorMessage += strerror(errno);
close_listen();
return false;
}
StartedListening.set_value();
return true;
}
std::shared_ptr<dap::Reader> cmDebuggerPipeConnection_POSIX::GetReader()
{
return std::static_pointer_cast<dap::Reader>(shared_from_this());
}
std::shared_ptr<dap::Writer> cmDebuggerPipeConnection_POSIX::GetWriter()
{
return std::static_pointer_cast<dap::Writer>(shared_from_this());
}
bool cmDebuggerPipeConnection_POSIX::isOpen()
{
return rw_pipe >= 0;
}
void cmDebuggerPipeConnection_POSIX::close()
{
close_listen();
::close(rw_pipe);
rw_pipe = -1;
}
void cmDebuggerPipeConnection_POSIX::close_listen()
{
if (strlen(addr.sun_path) > 0) {
unlink(addr.sun_path);
addr.sun_path[0] = '\0';
}
::close(listen_fd);
listen_fd = -1;
}
void cmDebuggerPipeConnection_POSIX::WaitForConnection()
{
sockaddr_un laddr;
socklen_t len = sizeof(laddr);
rw_pipe = accept(listen_fd, (sockaddr*)&laddr, &len);
if (rw_pipe < 0) {
close();
return;
}
close_listen(); // no longer need the listen resources
}
size_t cmDebuggerPipeConnection_POSIX::read(void* buffer, size_t n)
{
size_t result = 0;
if (rw_pipe >= 0) {
result = ::read(rw_pipe, buffer, n);
if (result == 0) {
close();
}
}
return result;
}
bool cmDebuggerPipeConnection_POSIX::write(void const* buffer, size_t n)
{
bool result = false;
if (rw_pipe >= 0) {
result = ::write(rw_pipe, buffer, n) >= 0;
if (!result) {
close();
}
}
return result;
}
cmDebuggerPipeClient_POSIX::cmDebuggerPipeClient_POSIX(std::string name)
: PipeName(std::move(name))
{
}
cmDebuggerPipeClient_POSIX::~cmDebuggerPipeClient_POSIX()
{
close();
}
void cmDebuggerPipeClient_POSIX::WaitForConnection()
{
rw_pipe = socket(AF_UNIX, SOCK_STREAM, 0);
if (rw_pipe < 0) {
throw std::runtime_error(std::string("Failed to create socket: ") +
strerror(errno));
}
sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, PipeName.c_str(), sizeof(addr.sun_path));
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
if (connect(rw_pipe, (sockaddr*)&addr, sizeof(addr)) == -1) {
close();
throw std::runtime_error(
std::string("Failed to connect path to socket: ") + strerror(errno));
}
}
bool cmDebuggerPipeClient_POSIX::isOpen()
{
return rw_pipe >= 0;
}
void cmDebuggerPipeClient_POSIX::close()
{
if (isOpen()) {
::close(rw_pipe);
rw_pipe = -1;
}
}
size_t cmDebuggerPipeClient_POSIX::read(void* buffer, size_t n)
{
int count = 0;
if (isOpen()) {
count = static_cast<int>(::read(rw_pipe, buffer, n));
if (count == 0) {
close();
}
}
return count;
}
bool cmDebuggerPipeClient_POSIX::write(void const* buffer, size_t n)
{
int count = 0;
if (isOpen()) {
count = static_cast<int>(::write(rw_pipe, buffer, n));
if (count < 0) {
close();
}
}
return count > 0;
}
#endif // !_WIN32
} // namespace cmDebugger
|