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
206
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <limits.h>
#include "windefs.h"
#include "twowaypipe.h"
#define PIPE_NAME_FORMAT_STR "/tmp/clr-debug-pipe-%d-%s"
static void GetPipeName(char *name, DWORD id, const char *suffix)
{
int chars = snprintf(name, PATH_MAX, PIPE_NAME_FORMAT_STR, id, suffix);
_ASSERTE(chars > 0 && chars < PATH_MAX);
}
// Creates a server side of the pipe.
// Id is used to create pipes names and uniquely identify the pipe on the machine.
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::CreateServer(DWORD id)
{
_ASSERTE(m_state == NotInitialized);
if (m_state != NotInitialized)
return false;
m_id = id;
char inPipeName[PATH_MAX];
char outPipeName[PATH_MAX];
GetPipeName(inPipeName, id, "in");
GetPipeName(outPipeName, id, "out");
if (mkfifo(inPipeName, S_IRWXU) == -1)
{
return false;
}
if (mkfifo(outPipeName, S_IRWXU) == -1)
{
remove(inPipeName);
return false;
}
m_state = Created;
return true;
}
// Connects to a previously opened server side of the pipe.
// Id is used to locate the pipe on the machine.
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::Connect(DWORD id)
{
_ASSERTE(m_state == NotInitialized);
if (m_state != NotInitialized)
return false;
m_id = id;
char inPipeName[PATH_MAX];
char outPipeName[PATH_MAX];
//"in" and "out" are switched deliberately, because we're on the client
GetPipeName(inPipeName, id, "out");
GetPipeName(outPipeName, id, "in");
// Pipe opening order is reversed compared to WaitForConnection()
// in order to avaid deadlock.
m_outboundPipe = open(outPipeName, O_WRONLY);
if (m_outboundPipe == INVALID_PIPE)
{
return false;
}
m_inboundPipe = open(inPipeName, O_RDONLY);
if (m_inboundPipe == INVALID_PIPE)
{
close(m_outboundPipe);
m_outboundPipe = INVALID_PIPE;
return false;
}
m_state = ClientConnected;
return true;
}
// Waits for incoming client connections, assumes GetState() == Created
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::WaitForConnection()
{
_ASSERTE(m_state == Created);
if (m_state != Created)
return false;
char inPipeName[PATH_MAX];
char outPipeName[PATH_MAX];
GetPipeName(inPipeName, m_id, "in");
GetPipeName(outPipeName, m_id, "out");
m_inboundPipe = open(inPipeName, O_RDONLY);
if (m_inboundPipe == INVALID_PIPE)
{
return false;
}
m_outboundPipe = open(outPipeName, O_WRONLY);
if (m_outboundPipe == INVALID_PIPE)
{
close(m_inboundPipe);
m_inboundPipe = INVALID_PIPE;
return false;
}
m_state = ServerConnected;
return true;
}
// Reads data from pipe. Returns number of bytes read or a negative number in case of an error.
// use GetLastError() for more details
// UNIXTODO - mjm 9/6/15 - does not set last error on failure
int TwoWayPipe::Read(void *buffer, DWORD bufferSize)
{
_ASSERTE(m_state == ServerConnected || m_state == ClientConnected);
int totalBytesRead = 0;
int bytesRead;
int cb = bufferSize;
while ((bytesRead = (int)read(m_inboundPipe, buffer, cb)) > 0)
{
totalBytesRead += bytesRead;
_ASSERTE(totalBytesRead <= bufferSize);
if (totalBytesRead >= bufferSize)
{
break;
}
buffer = (char*)buffer + bytesRead;
cb -= bytesRead;
}
return bytesRead == -1 ? -1 : totalBytesRead;
}
// Writes data to pipe. Returns number of bytes written or a negative number in case of an error.
// use GetLastError() for more details
// UNIXTODO - mjm 9/6/15 - does not set last error on failure
int TwoWayPipe::Write(const void *data, DWORD dataSize)
{
_ASSERTE(m_state == ServerConnected || m_state == ClientConnected);
int totalBytesWritten = 0;
int bytesWritten;
int cb = dataSize;
while ((bytesWritten = (int)write(m_outboundPipe, data, cb)) > 0)
{
totalBytesWritten += bytesWritten;
_ASSERTE(totalBytesWritten <= dataSize);
if (totalBytesWritten >= dataSize)
{
break;
}
data = (char*)data + bytesWritten;
cb -= bytesWritten;
}
return bytesWritten == -1 ? -1 : totalBytesWritten;
}
// Disconnect server or client side of the pipe.
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::Disconnect()
{
if (m_outboundPipe != INVALID_PIPE && m_outboundPipe != 0)
{
close(m_outboundPipe);
m_outboundPipe = INVALID_PIPE;
}
if (m_inboundPipe != INVALID_PIPE && m_inboundPipe != 0)
{
close(m_inboundPipe);
m_inboundPipe = INVALID_PIPE;
}
if (m_state == ServerConnected || m_state == Created)
{
char inPipeName[PATH_MAX];
GetPipeName(inPipeName, m_id, "in");
remove(inPipeName);
char outPipeName[PATH_MAX];
GetPipeName(outPipeName, m_id, "out");
remove(outPipeName);
}
m_state = NotInitialized;
return true;
}
|