-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnpipe.cpp
More file actions
340 lines (286 loc) · 6.66 KB
/
npipe.cpp
File metadata and controls
340 lines (286 loc) · 6.66 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Named Pipe facility somewhat similar to local named pipes
// available under Win32
// This is implemented for Unix SysV where named pipes are available
// the package could be implemented using BSD UNIX Domain sockets
// if SysV named pipes were not available
// 20050111 mvh Blocked out sys/conf.h; compiles with linux but gives
// linker warning: fattach not implemented and will always fail
// 20051217 mvh Return (unsigned) 0xFFFFFFFF instead of signed -1
// 20070308 bcb Change things for BSD and DARWIN.
// 20070316 bcb Some more fixes
#include "npipe.hpp"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#ifndef BSD
#include <stropts.h>
#endif //BSD
//#include <sys/conf.h>
#include <string.h>
NamedPipe::NamedPipe(char *theName) :
itsCreatedHere(FALSE),
itsControlReadFd(-1),
itsControlWriteFd(-1),
#ifndef BSD
itsConnectionFd(-1)
#else //BSD
itsConnectionFd(NULL)
#endif //BSD
{
int aLength = theName ? strlen(theName) : 0;
itsName = theName ? new char[aLength + 1] : NULL;
if(theName)
{
strncpy(itsName, theName, aLength + 1);
}
}
NamedPipe::~NamedPipe()
{
Destroy();
}
void
NamedPipe::Destroy()
{
if(itsName)
{
if(itsCreatedHere)
{
(void)unlink(itsName);
}
delete [] itsName;
itsName = NULL;
if(itsControlReadFd >= 0)
{
#ifndef BSD //Doesn't have a read field
close(itsControlReadFd);
#endif //BSD
itsControlReadFd = 0;
}
if(itsControlWriteFd >= 0)
{
#ifndef BSD //Doesn't have a write field
close(itsControlWriteFd);
#endif //BSD
close(itsControlWriteFd);
itsControlWriteFd = 0;
}
}
#ifndef BSD
if(itsConnectionFd >= 0)
{
close(itsConnectionFd);
itsConnectionFd = 0;
}
#else //BSD uses fclose for a stream
if(itsConnectionFd != NULL)
{
fclose(itsConnectionFd);
itsConnectionFd = NULL;
}
#endif //BSD
itsCreatedHere = FALSE;
}
BOOL
NamedPipe::Create()
{
#ifndef BSD //Not used by dgate, no need to port
if(itsCreatedHere)
{
// already created
return TRUE;
}
if(!itsName)
{
// call it invalid argument...
errno = EINVAL;
return FALSE;
}
// see if the name exists in the file system
// if not, create it error checking done via fattach() below...
int aNamedFd = creat(itsName, 0770);
if(aNamedFd >= 0)
{
// just needed the name in the file system...
close(aNamedFd);
}
int anFdList[2];
if(pipe(anFdList))
{
// can't create a pipe
return FALSE;
}
itsControlReadFd = anFdList[0];
itsControlWriteFd = anFdList[1];
itsConnectionFd = -1;
// bind the desired name to the end of pipe used by writer
// fails if name doesn't exist or is already bound to a stream
if(fattach(itsControlWriteFd, itsName))
{
// error binding to desired name -- name probably in use already
// as a named pipe
int anError = errno;
close(itsControlWriteFd);
itsControlWriteFd = -1;
close(itsControlReadFd);
itsControlReadFd = -1;
errno = anError;
return FALSE;
}
// push the connection-oriented line discipline module onto the
// writer's end of the stream -- this will have the effect of
// creating a new stream each time an open is done on the named
// object, and sending a file descriptor to the new stream to us
// via the original stream we created -- thus yielding a separate
// stream for each connection established (like mulitple instances
// of an NT named pipe)
if(ioctl(itsControlWriteFd, I_PUSH, "connld"))
{
int anError = errno;
close(itsControlWriteFd);
itsControlWriteFd = -1;
close(itsControlReadFd);
itsControlReadFd = -1;
errno = anError;
return FALSE;
}
itsCreatedHere = TRUE;
#endif //BSD
return TRUE;
}
size_t
NamedPipe::Read(unsigned char *theBuffer, size_t theLength)
{
if(!itsName || !Connect())
{
errno = EBADF;
return 0xFFFFFFFF;
}
int aReadCount;
#ifndef BSD
aReadCount = read(itsConnectionFd, theBuffer, theLength);
#else //BSD uses fread to read streams
rewind(itsConnectionFd);
aReadCount = fread(theBuffer,1, theLength, itsConnectionFd);
#endif //BSD
return aReadCount;
}
size_t
NamedPipe::Write(unsigned char *theBuffer, size_t theLength)
{
if(!itsName || !Connect())
{
errno = EBADF;
return 0xFFFFFFFF;
}
int aWriteCount;
void (*aPreviousDisposal)(int) = signal(SIGPIPE, SIG_IGN);
#ifndef BSD
aWriteCount = write(itsConnectionFd, theBuffer, theLength);
#else //BSD uses fwrite for streams.
fseek(itsConnectionFd, 0, SEEK_END);
aWriteCount = fwrite(theBuffer,1, theLength, itsConnectionFd);
#endif //BSD
signal(SIGPIPE, aPreviousDisposal);
return aWriteCount;
}
BOOL
NamedPipe::Wait(size_t theWaitSeconds)
{
BOOL aWaitForeverFlag = (theWaitSeconds == 0 ? TRUE : FALSE);
unsigned char aBuffer;
do
{
// remain in loop when waiting until it happens...
if(aWaitForeverFlag)
{
++theWaitSeconds;
}
if(Connect() && Write(&aBuffer, 0) == 0)
{
// connected and succeeded in writing 0 bytes
// which actually do get transmitted as a 0 byte message
Disconnect();
return TRUE;
}
// sleep 1 second then try again...
// unless time is up
if(theWaitSeconds > 0)
{
sleep(1);
}
}
while(theWaitSeconds-- > 0);
// times up...
return FALSE;
}
BOOL
NamedPipe::Connect()
{
#ifndef BSD //Darwin uses fopen to open a stream.
if(itsConnectionFd >= 0)
{
// already connected...
return TRUE;
}
// client or server depending upon whether or not we created it
if(itsCreatedHere)
{
// server style -- wait to receive a connection via our
// control read fd (this ioctl returns a new fd for a
// connection when one occurs)
if(ioctl(itsControlReadFd, I_RECVFD, &itsConnectionFd))
{
return FALSE;
}
}
else
{
// client style
itsConnectionFd = open(itsName, O_RDWR | O_NONBLOCK);
if(itsConnectionFd == -1)
{
return FALSE;
}
// ensure that its a stream...
if(!isastream(itsConnectionFd))
{
close(itsConnectionFd);
itsConnectionFd = -1;
return FALSE;
}
}
#else //Darwin uses fopen to open a stream.
if(itsConnectionFd != NULL)
{
// already connected...
return TRUE;
}
itsConnectionFd = fopen(itsName, "w+");
if(itsConnectionFd == NULL)
{
return FALSE;
}
#endif //BSD
return TRUE;
}
void
NamedPipe::Disconnect()
{
#ifndef BSD
if(itsConnectionFd >= 0)
{
close(itsConnectionFd);
itsConnectionFd = -1;
}
#else //BSD uses fclose.
if(itsConnectionFd != NULL)
{
fclose(itsConnectionFd);
itsConnectionFd = NULL;
}
#endif //BSD
}