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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2