(file) Return to AnonymousPipeWindows.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  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                   #include <windows.h>
 44                   #include <stdio.h>
 45                   
 46                   
 47                   PEGASUS_NAMESPACE_BEGIN
 48                   
 49                   AnonymousPipe::AnonymousPipe ()
 50                   {
 51                       PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::AnonymousPipe ()");
 52                   
 53                       AnonymousPipeHandle thePipe [2];
 54                   
 55                       SECURITY_ATTRIBUTES saAttr;
 56                       saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
 57                       saAttr.bInheritHandle = TRUE;
 58 kumpf         1.1     saAttr.lpSecurityDescriptor = NULL;
 59                   
 60                       if (!CreatePipe (&thePipe [0], &thePipe [1], &saAttr, 0))
 61                       {
 62                           Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
 63                               "Failed to create pipe.  Error code: %d", GetLastError ());
 64                           PEG_METHOD_EXIT ();
 65                   
 66                           MessageLoaderParms mlp ("Common.AnonymousPipe.CREATE_PIPE_FAILED",
 67                               "Failed to create pipe.");
 68                           throw Exception (mlp);
 69                       }
 70                   
 71                       _readHandle = thePipe [0];
 72                       _writeHandle = thePipe [1];
 73                       _readOpen = true;
 74                       _writeOpen = true;
 75                   
 76                       PEG_METHOD_EXIT ();
 77                   }
 78                   
 79 kumpf         1.1 AnonymousPipe::AnonymousPipe (
 80                       const char * readHandle,
 81                       const char * writeHandle)
 82                   {
 83                       PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, 
 84                           "AnonymousPipe::AnonymousPipe (const char *, const char *)");
 85                   
 86                       _readHandle = 0;
 87                       _writeHandle = 0;
 88                       _readOpen = false;
 89                       _writeOpen = false;
 90                   
 91                       if (readHandle != NULL)
 92                       {
 93                           if (sscanf (readHandle, "%p", &_readHandle) != 1)
 94                           {
 95                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
 96                                   "Failed to create pipe: invalid read handle %s", readHandle);
 97                               PEG_METHOD_EXIT ();
 98                   
 99                               MessageLoaderParms mlp ("Common.AnonymousPipe.CREATE_PIPE_FAILED",
100 kumpf         1.1                 "Failed to create pipe.");
101                               throw Exception (mlp);
102                           }
103                           _readOpen = true;
104                       }
105                   
106                       if (writeHandle != NULL)
107                       {
108                           if (sscanf (writeHandle, "%p", &_writeHandle) != 1)
109                           {
110                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
111                                   "Failed to create pipe: invalid write handle %s", writeHandle);
112                               PEG_METHOD_EXIT ();
113                   
114                               MessageLoaderParms mlp ("Common.AnonymousPipe.CREATE_PIPE_FAILED",
115                                   "Failed to create pipe.");
116                               throw Exception (mlp);
117                           }
118                           _writeOpen = true;
119                       }
120                   
121 kumpf         1.1     PEG_METHOD_EXIT ();
122                   }
123                   
124                   AnonymousPipe::~AnonymousPipe ()
125                   {
126                       PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::~AnonymousPipe");
127                   
128                       if (_readOpen)
129                       {
130                           closeReadHandle ();
131                       }
132                   
133                       if (_writeOpen)
134                       {
135                           closeWriteHandle ();
136                       }
137                   
138                       PEG_METHOD_EXIT ();
139                   }
140                   
141                   AnonymousPipe::Status AnonymousPipe::writeBuffer (
142 david.dillard 1.3     const void * buffer,
143 kumpf         1.1     Uint32 bytesToWrite)
144                   {
145                       //
146                       //  Treat invalid handle as connection closed
147                       //
148                       if (!_writeOpen)
149                       {
150                           Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
151                               "Attempted to write to pipe whose write handle is not open");
152                           return STATUS_CLOSED;
153                       }
154                   
155                       //
156                       //  Ignore SIGPIPE signals
157                       //
158                       SignalHandler::ignore (PEGASUS_SIGPIPE);
159                   
160 david.dillard 1.3     const char * writeBuffer = reinterpret_cast<const char *>(buffer);
161 kumpf         1.1     DWORD expectedBytes = bytesToWrite;
162                       do
163                       {
164                           BOOL returnValue;
165                           DWORD bytesWritten = 0;
166                           returnValue = WriteFile (_writeHandle, writeBuffer, expectedBytes,
167                               &bytesWritten, NULL);
168                   
169                           if (!returnValue)
170                           {
171                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
172                                   "Failed to write buffer to pipe.  Error code: %d", 
173                                   GetLastError ());
174                               return STATUS_ERROR;
175                           }
176                   
177                           if (bytesWritten < 0)
178                           {
179                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
180                                   "Failed to write buffer to pipe.  Error code: %d", 
181                                   GetLastError ());
182 kumpf         1.1 
183                               if ((GetLastError () == ERROR_PIPE_NOT_CONNECTED) ||
184                                   (GetLastError () == ERROR_BROKEN_PIPE))
185                               {
186                                   return STATUS_CLOSED;
187                               }
188                               else
189                               {
190                                   return STATUS_ERROR;
191                               }
192                           }
193                   
194                           expectedBytes -= bytesWritten;
195                           writeBuffer += bytesWritten;
196                       } while (expectedBytes > 0);
197                   
198                       return STATUS_SUCCESS;
199                   }
200                   
201                   AnonymousPipe::Status AnonymousPipe::readBuffer (
202 david.dillard 1.3     void * buffer,
203 kumpf         1.1     Uint32 bytesToRead)
204                   {
205                       //
206                       //  Treat invalid handle as connection closed
207                       //
208                       if (!_readOpen)
209                       {
210                           Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
211                               "Attempted to read from pipe whose read handle is not open");
212                           return STATUS_CLOSED;
213                       }
214                   
215                       DWORD expectedBytes = bytesToRead;
216                   
217                       do
218                       {
219                           BOOL returnValue;
220                           DWORD bytesRead;
221                           returnValue = ReadFile (_readHandle, buffer, bytesToRead, &bytesRead,
222                               NULL);
223                   
224 kumpf         1.1         if (!returnValue)
225                           {
226                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
227                                   "Failed to read buffer from pipe.  Error code: %d", 
228                                   GetLastError ());
229                               if ((GetLastError () == ERROR_PIPE_NOT_CONNECTED) ||
230                                   (GetLastError () == ERROR_BROKEN_PIPE))
231                               {
232                                   return STATUS_CLOSED;
233                               }
234                   
235                               return STATUS_ERROR;
236                           }
237                   
238                           if (bytesRead == 0)
239                           {
240                               //
241                               //  Connection closed
242                               //
243                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
244                                   "Failed to read buffer from pipe: connection closed");
245 kumpf         1.1             return STATUS_CLOSED;
246                           }
247                   
248                           if (bytesRead < 0)
249                           {
250                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
251                                   "Failed to read buffer from pipe.  Error code: %d", 
252                                   GetLastError ());
253                   
254                               //
255                               //  Error reading from pipe
256                               //
257                               return STATUS_ERROR;
258                           }
259                   
260 david.dillard 1.3         buffer = reinterpret_cast<char *>(buffer) + bytesRead;
261 kumpf         1.1         bytesToRead -= bytesRead;
262                       } while (bytesToRead > 0);
263                   
264                       return STATUS_SUCCESS;
265                   }
266                   
267                   void AnonymousPipe::exportReadHandle (char * buffer) const
268                   {
269                       PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::exportReadHandle");
270                   
271                       sprintf (buffer, "%p", _readHandle);
272                   
273                       PEG_METHOD_EXIT ();
274                   }
275                   
276                   void AnonymousPipe::exportWriteHandle (char * buffer) const
277                   {
278                       PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::exportWriteHandle");
279                   
280                       sprintf (buffer, "%p", _writeHandle);
281                   
282 kumpf         1.1     PEG_METHOD_EXIT ();
283                   }
284                   
285                   void AnonymousPipe::closeReadHandle ()
286                   {
287                       PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::closeReadHandle");
288                   
289                       if (_readOpen)
290                       {
291                           if (!CloseHandle (_readHandle))
292                           {
293                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
294                                   "Failed to close read handle.  Error code: %d", 
295                                   GetLastError ());
296                           }
297                           else
298                           {
299                               _readOpen = false;
300                           }
301                       }
302                       else
303 kumpf         1.1     {
304                           Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
305                               "Attempted to close read handle that was not open");
306                       }
307                   
308                       PEG_METHOD_EXIT ();
309                   }
310                   
311                   void AnonymousPipe::closeWriteHandle ()
312                   {
313                       PEG_METHOD_ENTER (TRC_OS_ABSTRACTION, "AnonymousPipe::closeWriteHandle");
314                   
315                       if (_writeOpen)
316                       {
317                           if (!CloseHandle (_writeHandle))
318                           {
319                               Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
320                                   "Failed to close write handle.  Error code: %d", 
321                                   GetLastError ());
322                           }
323                           else
324 kumpf         1.1         {
325                               _writeOpen = false;
326                           }
327                       }
328                       else
329                       {
330                           Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL2,
331                               "Attempted to close write handle that was not open");
332                       }
333                   
334                       PEG_METHOD_EXIT ();
335                   }
336                   
337                   PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2