1 karl 1.5 //%2006////////////////////////////////////////////////////////////////////////
|
2 kumpf 1.1 //
|
3 karl 1.2 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 kumpf 1.1 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.2 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.4 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.5 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 kumpf 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Carol Ann Krug Graves, Hewlett-Packard Company
33 // (carolann_graves@hp.com)
34 kumpf 1.1 //
|
35 david.dillard 1.3 // Modified By: David Dillard, VERITAS Software Corp.
36 // (david.dillard@veritas.com)
|
37 kumpf 1.1 //
38 //%/////////////////////////////////////////////////////////////////////////////
39
40
41 #include "AnonymousPipe.h"
42 #include <Pegasus/Common/Signal.h>
|
43 mike 1.6 #include "Network.h"
|
44 kumpf 1.1 #include <windows.h>
45 #include <stdio.h>
46
47
48 PEGASUS_NAMESPACE_BEGIN
49
50 AnonymousPipe::AnonymousPipe ()
51 {
52 PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::AnonymousPipe ()");
53
54 AnonymousPipeHandle thePipe [2];
55
56 SECURITY_ATTRIBUTES saAttr;
57 saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
58 saAttr.bInheritHandle = TRUE;
59 saAttr.lpSecurityDescriptor = NULL;
60
61 if (!CreatePipe (&thePipe [0], &thePipe [1], &saAttr, 0))
62 {
63 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
64 "Failed to create pipe. Error code: %d", GetLastError ());
65 kumpf 1.1 PEG_METHOD_EXIT ();
66
67 MessageLoaderParms mlp ("Common.AnonymousPipe.CREATE_PIPE_FAILED",
68 "Failed to create pipe.");
69 throw Exception (mlp);
70 }
71
72 _readHandle = thePipe [0];
73 _writeHandle = thePipe [1];
74 _readOpen = true;
75 _writeOpen = true;
76
77 PEG_METHOD_EXIT ();
78 }
79
80 AnonymousPipe::AnonymousPipe (
81 const char * readHandle,
82 const char * writeHandle)
83 {
84 PEG_METHOD_ENTER (TRC_OS_ABSTRACTION,
85 "AnonymousPipe::AnonymousPipe (const char *, const char *)");
86 kumpf 1.1
87 _readHandle = 0;
88 _writeHandle = 0;
89 _readOpen = false;
90 _writeOpen = false;
91
92 if (readHandle != NULL)
93 {
94 if (sscanf (readHandle, "%p", &_readHandle) != 1)
95 {
96 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
97 "Failed to create pipe: invalid read handle %s", readHandle);
98 PEG_METHOD_EXIT ();
99
100 MessageLoaderParms mlp ("Common.AnonymousPipe.CREATE_PIPE_FAILED",
101 "Failed to create pipe.");
102 throw Exception (mlp);
103 }
104 _readOpen = true;
105 }
106
107 kumpf 1.1 if (writeHandle != NULL)
108 {
109 if (sscanf (writeHandle, "%p", &_writeHandle) != 1)
110 {
111 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
112 "Failed to create pipe: invalid write handle %s", writeHandle);
113 PEG_METHOD_EXIT ();
114
115 MessageLoaderParms mlp ("Common.AnonymousPipe.CREATE_PIPE_FAILED",
116 "Failed to create pipe.");
117 throw Exception (mlp);
118 }
119 _writeOpen = true;
120 }
121
122 PEG_METHOD_EXIT ();
123 }
124
125 AnonymousPipe::~AnonymousPipe ()
126 {
127 PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::~AnonymousPipe");
128 kumpf 1.1
129 if (_readOpen)
130 {
131 closeReadHandle ();
132 }
133
134 if (_writeOpen)
135 {
136 closeWriteHandle ();
137 }
138
139 PEG_METHOD_EXIT ();
140 }
141
142 AnonymousPipe::Status AnonymousPipe::writeBuffer (
|
143 david.dillard 1.3 const void * buffer,
|
144 kumpf 1.1 Uint32 bytesToWrite)
145 {
146 //
147 // Treat invalid handle as connection closed
148 //
149 if (!_writeOpen)
150 {
151 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
152 "Attempted to write to pipe whose write handle is not open");
153 return STATUS_CLOSED;
154 }
155
156 //
157 // Ignore SIGPIPE signals
158 //
159 SignalHandler::ignore (PEGASUS_SIGPIPE);
160
|
161 david.dillard 1.3 const char * writeBuffer = reinterpret_cast<const char *>(buffer);
|
162 kumpf 1.1 DWORD expectedBytes = bytesToWrite;
163 do
164 {
165 BOOL returnValue;
166 DWORD bytesWritten = 0;
167 returnValue = WriteFile (_writeHandle, writeBuffer, expectedBytes,
168 &bytesWritten, NULL);
169
170 if (!returnValue)
171 {
172 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
173 "Failed to write buffer to pipe. Error code: %d",
174 GetLastError ());
175 return STATUS_ERROR;
176 }
177
178 if (bytesWritten < 0)
179 {
180 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
181 "Failed to write buffer to pipe. Error code: %d",
182 GetLastError ());
183 kumpf 1.1
184 if ((GetLastError () == ERROR_PIPE_NOT_CONNECTED) ||
185 (GetLastError () == ERROR_BROKEN_PIPE))
186 {
187 return STATUS_CLOSED;
188 }
189 else
190 {
191 return STATUS_ERROR;
192 }
193 }
194
195 expectedBytes -= bytesWritten;
196 writeBuffer += bytesWritten;
197 } while (expectedBytes > 0);
198
199 return STATUS_SUCCESS;
200 }
201
202 AnonymousPipe::Status AnonymousPipe::readBuffer (
|
203 david.dillard 1.3 void * buffer,
|
204 kumpf 1.1 Uint32 bytesToRead)
205 {
206 //
207 // Treat invalid handle as connection closed
208 //
209 if (!_readOpen)
210 {
211 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
212 "Attempted to read from pipe whose read handle is not open");
213 return STATUS_CLOSED;
214 }
215
216 DWORD expectedBytes = bytesToRead;
217
218 do
219 {
220 BOOL returnValue;
221 DWORD bytesRead;
222 returnValue = ReadFile (_readHandle, buffer, bytesToRead, &bytesRead,
223 NULL);
224
225 kumpf 1.1 if (!returnValue)
226 {
227 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
228 "Failed to read buffer from pipe. Error code: %d",
229 GetLastError ());
230 if ((GetLastError () == ERROR_PIPE_NOT_CONNECTED) ||
231 (GetLastError () == ERROR_BROKEN_PIPE))
232 {
233 return STATUS_CLOSED;
234 }
235
236 return STATUS_ERROR;
237 }
238
239 if (bytesRead == 0)
240 {
241 //
242 // Connection closed
243 //
244 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
245 "Failed to read buffer from pipe: connection closed");
246 kumpf 1.1 return STATUS_CLOSED;
247 }
248
249 if (bytesRead < 0)
250 {
251 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
252 "Failed to read buffer from pipe. Error code: %d",
253 GetLastError ());
254
255 //
256 // Error reading from pipe
257 //
258 return STATUS_ERROR;
259 }
260
|
261 david.dillard 1.3 buffer = reinterpret_cast<char *>(buffer) + bytesRead;
|
262 kumpf 1.1 bytesToRead -= bytesRead;
263 } while (bytesToRead > 0);
264
265 return STATUS_SUCCESS;
266 }
267
268 void AnonymousPipe::exportReadHandle (char * buffer) const
269 {
270 PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::exportReadHandle");
271
272 sprintf (buffer, "%p", _readHandle);
273
274 PEG_METHOD_EXIT ();
275 }
276
277 void AnonymousPipe::exportWriteHandle (char * buffer) const
278 {
279 PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::exportWriteHandle");
280
281 sprintf (buffer, "%p", _writeHandle);
282
283 kumpf 1.1 PEG_METHOD_EXIT ();
284 }
285
286 void AnonymousPipe::closeReadHandle ()
287 {
288 PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::closeReadHandle");
289
290 if (_readOpen)
291 {
292 if (!CloseHandle (_readHandle))
293 {
294 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
295 "Failed to close read handle. Error code: %d",
296 GetLastError ());
297 }
298 else
299 {
300 _readOpen = false;
301 }
302 }
303 else
304 kumpf 1.1 {
305 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
306 "Attempted to close read handle that was not open");
307 }
308
309 PEG_METHOD_EXIT ();
310 }
311
312 void AnonymousPipe::closeWriteHandle ()
313 {
314 PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::closeWriteHandle");
315
316 if (_writeOpen)
317 {
318 if (!CloseHandle (_writeHandle))
319 {
320 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
321 "Failed to close write handle. Error code: %d",
322 GetLastError ());
323 }
324 else
325 kumpf 1.1 {
326 _writeOpen = false;
327 }
328 }
329 else
330 {
331 Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
332 "Attempted to close write handle that was not open");
333 }
334
335 PEG_METHOD_EXIT ();
336 }
337
338 PEGASUS_NAMESPACE_END
|