VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/pipe-win.cpp@ 33540

最後變更 在這個檔案從33540是 33540,由 vboxsync 提交於 14 年 前

*: spelling fixes, thanks Timeless!

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 48.1 KB
 
1/* $Id: pipe-win.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
2/** @file
3 * IPRT - Anonymous Pipes, Windows Implementation.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <Windows.h>
32
33#include <iprt/pipe.h>
34#include "internal/iprt.h"
35
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/critsect.h>
39#include <iprt/err.h>
40#include <iprt/mem.h>
41#include <iprt/string.h>
42#include <iprt/poll.h>
43#include <iprt/process.h>
44#include <iprt/thread.h>
45#include <iprt/time.h>
46#include "internal/pipe.h"
47#include "internal/magics.h"
48
49
50/*******************************************************************************
51* Defined Constants And Macros *
52*******************************************************************************/
53/** The pipe buffer size we prefer. */
54#define RTPIPE_NT_SIZE _64K
55
56
57/*******************************************************************************
58* Structures and Typedefs *
59*******************************************************************************/
60typedef struct RTPIPEINTERNAL
61{
62 /** Magic value (RTPIPE_MAGIC). */
63 uint32_t u32Magic;
64 /** The pipe handle. */
65 HANDLE hPipe;
66 /** Set if this is the read end, clear if it's the write end. */
67 bool fRead;
68 /** Set if there is already pending I/O. */
69 bool fIOPending;
70 /** Set if the zero byte read that the poll code using is pending. */
71 bool fZeroByteRead;
72 /** Set if the pipe is broken. */
73 bool fBrokenPipe;
74 /** Set if we've promised that the handle is writable. */
75 bool fPromisedWritable;
76 /** Usage counter. */
77 uint32_t cUsers;
78 /** The overlapped I/O structure we use. */
79 OVERLAPPED Overlapped;
80 /** Bounce buffer for writes. */
81 uint8_t *pbBounceBuf;
82 /** Amount of used buffer space. */
83 size_t cbBounceBufUsed;
84 /** Amount of allocated buffer space. */
85 size_t cbBounceBufAlloc;
86 /** The handle of the poll set currently polling on this pipe.
87 * We can only have one poller at the time (lazy bird). */
88 RTPOLLSET hPollSet;
89 /** Critical section protecting the above members.
90 * (Taking the lazy/simple approach.) */
91 RTCRITSECT CritSect;
92 /** Buffer for the zero byte read. */
93 uint8_t abBuf[8];
94} RTPIPEINTERNAL;
95
96
97/* from ntdef.h */
98typedef LONG NTSTATUS;
99
100/* from ntddk.h */
101typedef struct _IO_STATUS_BLOCK {
102 union {
103 NTSTATUS Status;
104 PVOID Pointer;
105 };
106 ULONG_PTR Information;
107} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
108
109typedef enum _FILE_INFORMATION_CLASS {
110 FilePipeInformation = 23,
111 FilePipeLocalInformation = 24,
112 FilePipeRemoteInformation = 25,
113} FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;
114
115/* from ntifs.h */
116typedef struct _FILE_PIPE_LOCAL_INFORMATION {
117 ULONG NamedPipeType;
118 ULONG NamedPipeConfiguration;
119 ULONG MaximumInstances;
120 ULONG CurrentInstances;
121 ULONG InboundQuota;
122 ULONG ReadDataAvailable;
123 ULONG OutboundQuota;
124 ULONG WriteQuotaAvailable;
125 ULONG NamedPipeState;
126 ULONG NamedPipeEnd;
127} FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION;
128
129#define FILE_PIPE_DISCONNECTED_STATE 0x00000001
130#define FILE_PIPE_LISTENING_STATE 0x00000002
131#define FILE_PIPE_CONNECTED_STATE 0x00000003
132#define FILE_PIPE_CLOSING_STATE 0x00000004
133
134#define FILE_PIPE_INBOUND 0x00000000
135#define FILE_PIPE_OUTBOUND 0x00000001
136#define FILE_PIPE_FULL_DUPLEX 0x00000002
137
138#define FILE_PIPE_CLIENT_END 0x00000000
139#define FILE_PIPE_SERVER_END 0x00000001
140
141extern "C" NTSYSAPI NTSTATUS WINAPI NtQueryInformationFile(HANDLE, PIO_STATUS_BLOCK, PVOID, LONG, FILE_INFORMATION_CLASS);
142
143
144/**
145 * Wrapper for getting FILE_PIPE_LOCAL_INFORMATION via the NT API.
146 *
147 * @returns Success indicator (true/false).
148 * @param pThis The pipe.
149 * @param pInfo The info structure.
150 */
151static bool rtPipeQueryInfo(RTPIPEINTERNAL *pThis, FILE_PIPE_LOCAL_INFORMATION *pInfo)
152{
153 IO_STATUS_BLOCK Ios;
154 RT_ZERO(Ios);
155 RT_ZERO(*pInfo);
156 NTSTATUS rcNt = NtQueryInformationFile(pThis->hPipe, &Ios, pInfo, sizeof(*pInfo), FilePipeLocalInformation);
157 return rcNt >= 0;
158}
159
160
161RTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags)
162{
163 AssertPtrReturn(phPipeRead, VERR_INVALID_POINTER);
164 AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
165 AssertReturn(!(fFlags & ~RTPIPE_C_VALID_MASK), VERR_INVALID_PARAMETER);
166
167 /*
168 * Create the read end of the pipe.
169 */
170 DWORD dwErr;
171 HANDLE hPipeR;
172 HANDLE hPipeW;
173 int rc;
174 for (;;)
175 {
176 static volatile uint32_t g_iNextPipe = 0;
177 char szName[128];
178 RTStrPrintf(szName, sizeof(szName), "\\\\.\\pipe\\iprt-pipe-%u-%u", RTProcSelf(), ASMAtomicIncU32(&g_iNextPipe));
179
180 SECURITY_ATTRIBUTES SecurityAttributes;
181 PSECURITY_ATTRIBUTES pSecurityAttributes = NULL;
182 if (fFlags & RTPIPE_C_INHERIT_READ)
183 {
184 SecurityAttributes.nLength = sizeof(SecurityAttributes);
185 SecurityAttributes.lpSecurityDescriptor = NULL;
186 SecurityAttributes.bInheritHandle = TRUE;
187 pSecurityAttributes = &SecurityAttributes;
188 }
189
190 DWORD dwOpenMode = PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED;
191#ifdef FILE_FLAG_FIRST_PIPE_INSTANCE
192 dwOpenMode |= FILE_FLAG_FIRST_PIPE_INSTANCE;
193#endif
194
195 DWORD dwPipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT;
196#ifdef PIPE_REJECT_REMOTE_CLIENTS
197 dwPipeMode |= PIPE_REJECT_REMOTE_CLIENTS;
198#endif
199
200 hPipeR = CreateNamedPipeA(szName, dwOpenMode, dwPipeMode, 1 /*nMaxInstances*/, RTPIPE_NT_SIZE, RTPIPE_NT_SIZE,
201 NMPWAIT_USE_DEFAULT_WAIT, pSecurityAttributes);
202#ifdef PIPE_REJECT_REMOTE_CLIENTS
203 if (hPipeR == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
204 {
205 dwPipeMode &= ~PIPE_REJECT_REMOTE_CLIENTS;
206 hPipeR = CreateNamedPipeA(szName, dwOpenMode, dwPipeMode, 1 /*nMaxInstances*/, RTPIPE_NT_SIZE, RTPIPE_NT_SIZE,
207 NMPWAIT_USE_DEFAULT_WAIT, pSecurityAttributes);
208 }
209#endif
210#ifdef FILE_FLAG_FIRST_PIPE_INSTANCE
211 if (hPipeR == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_PARAMETER)
212 {
213 dwOpenMode &= ~FILE_FLAG_FIRST_PIPE_INSTANCE;
214 hPipeR = CreateNamedPipeA(szName, dwOpenMode, dwPipeMode, 1 /*nMaxInstances*/, RTPIPE_NT_SIZE, RTPIPE_NT_SIZE,
215 NMPWAIT_USE_DEFAULT_WAIT, pSecurityAttributes);
216 }
217#endif
218 if (hPipeR != INVALID_HANDLE_VALUE)
219 {
220 /*
221 * Connect to the pipe (the write end).
222 * We add FILE_READ_ATTRIBUTES here to make sure we can query the
223 * pipe state later on.
224 */
225 pSecurityAttributes = NULL;
226 if (fFlags & RTPIPE_C_INHERIT_WRITE)
227 {
228 SecurityAttributes.nLength = sizeof(SecurityAttributes);
229 SecurityAttributes.lpSecurityDescriptor = NULL;
230 SecurityAttributes.bInheritHandle = TRUE;
231 pSecurityAttributes = &SecurityAttributes;
232 }
233
234 hPipeW = CreateFileA(szName,
235 GENERIC_WRITE | FILE_READ_ATTRIBUTES /*dwDesiredAccess*/,
236 0 /*dwShareMode*/,
237 pSecurityAttributes,
238 OPEN_EXISTING /* dwCreationDisposition */,
239 FILE_FLAG_OVERLAPPED /*dwFlagsAndAttributes*/,
240 NULL /*hTemplateFile*/);
241 if (hPipeW != INVALID_HANDLE_VALUE)
242 break;
243 dwErr = GetLastError();
244 CloseHandle(hPipeR);
245 }
246 else
247 dwErr = GetLastError();
248 if ( dwErr != ERROR_PIPE_BUSY /* already exist - compatible */
249 && dwErr != ERROR_ACCESS_DENIED /* already exist - incompatible */)
250 return RTErrConvertFromWin32(dwErr);
251 /* else: try again with a new name */
252 }
253
254 /*
255 * Create the two handles.
256 */
257 RTPIPEINTERNAL *pThisR = (RTPIPEINTERNAL *)RTMemAllocZ(sizeof(RTPIPEINTERNAL));
258 if (pThisR)
259 {
260 RTPIPEINTERNAL *pThisW = (RTPIPEINTERNAL *)RTMemAllocZ(sizeof(RTPIPEINTERNAL));
261 if (pThisW)
262 {
263 rc = RTCritSectInit(&pThisR->CritSect);
264 if (RT_SUCCESS(rc))
265 {
266 rc = RTCritSectInit(&pThisW->CritSect);
267 if (RT_SUCCESS(rc))
268 {
269 pThisR->Overlapped.hEvent = CreateEvent(NULL, TRUE /*fManualReset*/,
270 TRUE /*fInitialState*/, NULL /*pName*/);
271 if (pThisR->Overlapped.hEvent != NULL)
272 {
273 pThisW->Overlapped.hEvent = CreateEvent(NULL, TRUE /*fManualReset*/,
274 TRUE /*fInitialState*/, NULL /*pName*/);
275 if (pThisW->Overlapped.hEvent != NULL)
276 {
277 pThisR->u32Magic = RTPIPE_MAGIC;
278 pThisW->u32Magic = RTPIPE_MAGIC;
279 pThisR->hPipe = hPipeR;
280 pThisW->hPipe = hPipeW;
281 pThisR->fRead = true;
282 pThisW->fRead = false;
283 //pThisR->fIOPending = false;
284 //pThisW->fIOPending = false;
285 //pThisR->fZeroByteRead = false;
286 //pThisW->fZeroByteRead = false;
287 //pThisR->fBrokenPipe = false;
288 //pThisW->fBrokenPipe = false;
289 //pThisW->fPromisedWritable= false;
290 //pThisR->fPromisedWritable= false;
291 //pThisR->cUsers = 0;
292 //pThisW->cUsers = 0;
293 //pThisR->pbBounceBuf = NULL;
294 //pThisW->pbBounceBuf = NULL;
295 //pThisR->cbBounceBufUsed = 0;
296 //pThisW->cbBounceBufUsed = 0;
297 //pThisR->cbBounceBufAlloc= 0;
298 //pThisW->cbBounceBufAlloc= 0;
299 pThisR->hPollSet = NIL_RTPOLLSET;
300 pThisW->hPollSet = NIL_RTPOLLSET;
301
302 *phPipeRead = pThisR;
303 *phPipeWrite = pThisW;
304 return VINF_SUCCESS;
305 }
306 CloseHandle(pThisR->Overlapped.hEvent);
307 }
308 RTCritSectDelete(&pThisW->CritSect);
309 }
310 RTCritSectDelete(&pThisR->CritSect);
311 }
312 RTMemFree(pThisW);
313 }
314 else
315 rc = VERR_NO_MEMORY;
316 RTMemFree(pThisR);
317 }
318 else
319 rc = VERR_NO_MEMORY;
320
321 CloseHandle(hPipeR);
322 CloseHandle(hPipeW);
323 return rc;
324}
325
326
327/**
328 * Common worker for handling I/O completion.
329 *
330 * This is used by RTPipeClose, RTPipeWrite and RTPipeWriteBlocking.
331 *
332 * @returns IPRT status code.
333 * @param pThis The pipe instance handle.
334 */
335static int rtPipeWriteCheckCompletion(RTPIPEINTERNAL *pThis)
336{
337 int rc;
338 DWORD dwRc = WaitForSingleObject(pThis->Overlapped.hEvent, 0);
339 if (dwRc == WAIT_OBJECT_0)
340 {
341 DWORD cbWritten = 0;
342 if (GetOverlappedResult(pThis->hPipe, &pThis->Overlapped, &cbWritten, TRUE))
343 {
344 for (;;)
345 {
346 if (cbWritten >= pThis->cbBounceBufUsed)
347 {
348 pThis->fIOPending = false;
349 rc = VINF_SUCCESS;
350 break;
351 }
352
353 /* resubmit the remainder of the buffer - can this actually happen? */
354 memmove(&pThis->pbBounceBuf[0], &pThis->pbBounceBuf[cbWritten], pThis->cbBounceBufUsed - cbWritten);
355 rc = ResetEvent(pThis->Overlapped.hEvent); Assert(rc == TRUE);
356 if (!WriteFile(pThis->hPipe, pThis->pbBounceBuf, (DWORD)pThis->cbBounceBufUsed,
357 &cbWritten, &pThis->Overlapped))
358 {
359 if (GetLastError() == ERROR_IO_PENDING)
360 rc = VINF_TRY_AGAIN;
361 else
362 {
363 pThis->fIOPending = false;
364 if (GetLastError() == ERROR_NO_DATA)
365 rc = VERR_BROKEN_PIPE;
366 else
367 rc = RTErrConvertFromWin32(GetLastError());
368 if (rc == VERR_BROKEN_PIPE)
369 pThis->fBrokenPipe = true;
370 }
371 break;
372 }
373 Assert(cbWritten > 0);
374 }
375 }
376 else
377 {
378 pThis->fIOPending = false;
379 rc = RTErrConvertFromWin32(GetLastError());
380 }
381 }
382 else if (dwRc == WAIT_TIMEOUT)
383 rc = VINF_TRY_AGAIN;
384 else
385 {
386 pThis->fIOPending = false;
387 if (dwRc == WAIT_ABANDONED)
388 rc = VERR_INVALID_HANDLE;
389 else
390 rc = RTErrConvertFromWin32(GetLastError());
391 }
392 return rc;
393}
394
395
396
397RTDECL(int) RTPipeClose(RTPIPE hPipe)
398{
399 RTPIPEINTERNAL *pThis = hPipe;
400 if (pThis == NIL_RTPIPE)
401 return VINF_SUCCESS;
402 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
403 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
404
405 /*
406 * Do the cleanup.
407 */
408 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTPIPE_MAGIC, RTPIPE_MAGIC), VERR_INVALID_HANDLE);
409 RTCritSectEnter(&pThis->CritSect);
410 Assert(pThis->cUsers == 0);
411
412 if (!pThis->fRead && pThis->fIOPending)
413 rtPipeWriteCheckCompletion(pThis);
414
415 CloseHandle(pThis->hPipe);
416 pThis->hPipe = INVALID_HANDLE_VALUE;
417
418 CloseHandle(pThis->Overlapped.hEvent);
419 pThis->Overlapped.hEvent = NULL;
420
421 RTMemFree(pThis->pbBounceBuf);
422 pThis->pbBounceBuf = NULL;
423
424 RTCritSectLeave(&pThis->CritSect);
425 RTCritSectDelete(&pThis->CritSect);
426
427 RTMemFree(pThis);
428
429 return VINF_SUCCESS;
430}
431
432
433RTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
434{
435 AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
436 AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
437 AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
438
439 /*
440 * Get and validate the pipe handle info.
441 */
442 HANDLE hNative = (HANDLE)hNativePipe;
443 AssertReturn(GetFileType(hNative) == FILE_TYPE_PIPE, VERR_INVALID_HANDLE);
444
445 DWORD cMaxInstances;
446 DWORD fInfo;
447 if (!GetNamedPipeInfo(hNative, &fInfo, NULL, NULL, &cMaxInstances))
448 return RTErrConvertFromWin32(GetLastError());
449 AssertReturn(!(fInfo & PIPE_TYPE_MESSAGE), VERR_INVALID_HANDLE);
450 AssertReturn(cMaxInstances == 1, VERR_INVALID_HANDLE);
451
452 DWORD cInstances;
453 DWORD fState;
454 if (!GetNamedPipeHandleState(hNative, &fState, &cInstances, NULL, NULL, NULL, 0))
455 return RTErrConvertFromWin32(GetLastError());
456 AssertReturn(!(fState & PIPE_NOWAIT), VERR_INVALID_HANDLE);
457 AssertReturn(!(fState & PIPE_READMODE_MESSAGE), VERR_INVALID_HANDLE);
458 AssertReturn(cInstances <= 1, VERR_INVALID_HANDLE);
459
460 /*
461 * Looks kind of OK, create a handle so we can try rtPipeQueryInfo on it
462 * and see if we need to duplicate it to make that call work.
463 */
464 RTPIPEINTERNAL *pThis = (RTPIPEINTERNAL *)RTMemAllocZ(sizeof(RTPIPEINTERNAL));
465 if (!pThis)
466 return VERR_NO_MEMORY;
467 int rc = RTCritSectInit(&pThis->CritSect);
468 if (RT_SUCCESS(rc))
469 {
470 pThis->Overlapped.hEvent = CreateEvent(NULL, TRUE /*fManualReset*/,
471 TRUE /*fInitialState*/, NULL /*pName*/);
472 if (pThis->Overlapped.hEvent != NULL)
473 {
474 pThis->u32Magic = RTPIPE_MAGIC;
475 pThis->hPipe = hNative;
476 pThis->fRead = !!(fFlags & RTPIPE_N_READ);
477 //pThis->fIOPending = false;
478 //pThis->fZeroByteRead = false;
479 //pThis->fBrokenPipe = false;
480 //pThisR->fPromisedWritable= false;
481 //pThis->cUsers = 0;
482 //pThis->pbBounceBuf = NULL;
483 //pThis->cbBounceBufUsed = 0;
484 //pThis->cbBounceBufAlloc= 0;
485 pThis->hPollSet = NIL_RTPOLLSET;
486
487 HANDLE hNative2 = INVALID_HANDLE_VALUE;
488 FILE_PIPE_LOCAL_INFORMATION Info;
489 if (rtPipeQueryInfo(pThis, &Info))
490 rc = VINF_SUCCESS;
491 else
492 {
493 if (DuplicateHandle(GetCurrentProcess() /*hSrcProcess*/, hNative /*hSrcHandle*/,
494 GetCurrentProcess() /*hDstProcess*/, &hNative2 /*phDstHandle*/,
495 pThis->fRead ? GENERIC_READ : GENERIC_WRITE | FILE_READ_ATTRIBUTES /*dwDesiredAccess*/,
496 !!(fFlags & RTPIPE_N_INHERIT) /*fInheritHandle*/,
497 0 /*dwOptions*/))
498 {
499 pThis->hPipe = hNative2;
500 if (rtPipeQueryInfo(pThis, &Info))
501 rc = VINF_SUCCESS;
502 else
503 {
504 rc = VERR_ACCESS_DENIED;
505 CloseHandle(hNative2);
506 }
507 }
508 else
509 hNative2 = INVALID_HANDLE_VALUE;
510 }
511 if (RT_SUCCESS(rc))
512 {
513 /*
514 * Verify the pipe state and correct the inheritability.
515 */
516 AssertStmt( Info.NamedPipeState == FILE_PIPE_CONNECTED_STATE
517 || Info.NamedPipeState == FILE_PIPE_CLOSING_STATE
518 || Info.NamedPipeState == FILE_PIPE_DISCONNECTED_STATE,
519 VERR_INVALID_HANDLE);
520 AssertStmt( Info.NamedPipeConfiguration
521 == ( Info.NamedPipeEnd == FILE_PIPE_SERVER_END
522 ? (pThis->fRead ? FILE_PIPE_INBOUND : FILE_PIPE_OUTBOUND)
523 : (pThis->fRead ? FILE_PIPE_OUTBOUND : FILE_PIPE_INBOUND) ),
524 VERR_INVALID_HANDLE);
525 if ( RT_SUCCESS(rc)
526 && hNative2 == INVALID_HANDLE_VALUE
527 && !SetHandleInformation(hNative,
528 HANDLE_FLAG_INHERIT /*dwMask*/,
529 fFlags & RTPIPE_N_INHERIT ? HANDLE_FLAG_INHERIT : 0))
530 {
531 rc = RTErrConvertFromWin32(GetLastError());
532 AssertMsgFailed(("%Rrc\n", rc));
533 }
534 if (RT_SUCCESS(rc))
535 {
536 /*
537 * Ok, we're good!
538 */
539 if (hNative2 != INVALID_HANDLE_VALUE)
540 CloseHandle(hNative);
541 *phPipe = pThis;
542 return VINF_SUCCESS;
543 }
544 }
545
546 /* Bail out. */
547 if (hNative2 != INVALID_HANDLE_VALUE)
548 CloseHandle(hNative2);
549 CloseHandle(pThis->Overlapped.hEvent);
550 }
551 RTCritSectDelete(&pThis->CritSect);
552 }
553 RTMemFree(pThis);
554 return rc;
555}
556
557
558RTDECL(RTHCINTPTR) RTPipeToNative(RTPIPE hPipe)
559{
560 RTPIPEINTERNAL *pThis = hPipe;
561 AssertPtrReturn(pThis, -1);
562 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, -1);
563
564 return (RTHCINTPTR)pThis->hPipe;
565}
566
567
568RTDECL(int) RTPipeRead(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
569{
570 RTPIPEINTERNAL *pThis = hPipe;
571 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
572 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
573 AssertReturn(pThis->fRead, VERR_ACCESS_DENIED);
574 AssertPtr(pcbRead);
575 AssertPtr(pvBuf);
576
577 int rc = RTCritSectEnter(&pThis->CritSect);
578 if (RT_SUCCESS(rc))
579 {
580 /* No concurrent readers, sorry. */
581 if (pThis->cUsers == 0)
582 {
583 pThis->cUsers++;
584
585 /*
586 * Kick of a an overlapped read. It should return immediately if
587 * there is bytes in the buffer. If not, we'll cancel it and see
588 * what we get back.
589 */
590 rc = ResetEvent(pThis->Overlapped.hEvent); Assert(rc == TRUE);
591 DWORD cbRead = 0;
592 if ( cbToRead == 0
593 || ReadFile(pThis->hPipe, pvBuf,
594 cbToRead <= ~(DWORD)0 ? (DWORD)cbToRead : ~(DWORD)0,
595 &cbRead, &pThis->Overlapped))
596 {
597 *pcbRead = cbRead;
598 rc = VINF_SUCCESS;
599 }
600 else if (GetLastError() == ERROR_IO_PENDING)
601 {
602 pThis->fIOPending = true;
603 RTCritSectLeave(&pThis->CritSect);
604
605 if (!CancelIo(pThis->hPipe))
606 WaitForSingleObject(pThis->Overlapped.hEvent, INFINITE);
607 if (GetOverlappedResult(pThis->hPipe, &pThis->Overlapped, &cbRead, TRUE /*fWait*/))
608 {
609 *pcbRead = cbRead;
610 rc = VINF_SUCCESS;
611 }
612 else if (GetLastError() == ERROR_OPERATION_ABORTED)
613 {
614 *pcbRead = 0;
615 rc = VINF_TRY_AGAIN;
616 }
617 else
618 rc = RTErrConvertFromWin32(GetLastError());
619
620 RTCritSectEnter(&pThis->CritSect);
621 pThis->fIOPending = false;
622 }
623 else
624 rc = RTErrConvertFromWin32(GetLastError());
625 if (rc == VERR_BROKEN_PIPE)
626 pThis->fBrokenPipe = true;
627
628 pThis->cUsers--;
629 }
630 else
631 rc = VERR_WRONG_ORDER;
632 RTCritSectLeave(&pThis->CritSect);
633 }
634 return rc;
635}
636
637
638RTDECL(int) RTPipeReadBlocking(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
639{
640 RTPIPEINTERNAL *pThis = hPipe;
641 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
642 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
643 AssertReturn(pThis->fRead, VERR_ACCESS_DENIED);
644 AssertPtr(pvBuf);
645
646 int rc = RTCritSectEnter(&pThis->CritSect);
647 if (RT_SUCCESS(rc))
648 {
649 /* No concurrent readers, sorry. */
650 if (pThis->cUsers == 0)
651 {
652 pThis->cUsers++;
653
654 size_t cbTotalRead = 0;
655 while (cbToRead > 0)
656 {
657 /*
658 * Kick of a an overlapped read. It should return immediately if
659 * there is bytes in the buffer. If not, we'll cancel it and see
660 * what we get back.
661 */
662 rc = ResetEvent(pThis->Overlapped.hEvent); Assert(rc == TRUE);
663 DWORD cbRead = 0;
664 pThis->fIOPending = true;
665 RTCritSectLeave(&pThis->CritSect);
666
667 if (ReadFile(pThis->hPipe, pvBuf,
668 cbToRead <= ~(DWORD)0 ? (DWORD)cbToRead : ~(DWORD)0,
669 &cbRead, &pThis->Overlapped))
670 rc = VINF_SUCCESS;
671 else if (GetLastError() == ERROR_IO_PENDING)
672 {
673 WaitForSingleObject(pThis->Overlapped.hEvent, INFINITE);
674 if (GetOverlappedResult(pThis->hPipe, &pThis->Overlapped, &cbRead, TRUE /*fWait*/))
675 rc = VINF_SUCCESS;
676 else
677 rc = RTErrConvertFromWin32(GetLastError());
678 }
679 else
680 rc = RTErrConvertFromWin32(GetLastError());
681
682 RTCritSectEnter(&pThis->CritSect);
683 pThis->fIOPending = false;
684 if (RT_FAILURE(rc))
685 break;
686
687 /* advance */
688 cbToRead -= cbRead;
689 cbTotalRead += cbRead;
690 pvBuf = (uint8_t *)pvBuf + cbRead;
691 }
692
693 if (rc == VERR_BROKEN_PIPE)
694 pThis->fBrokenPipe = true;
695
696 if (pcbRead)
697 {
698 *pcbRead = cbTotalRead;
699 if ( RT_FAILURE(rc)
700 && cbTotalRead
701 && rc != VERR_INVALID_POINTER)
702 rc = VINF_SUCCESS;
703 }
704
705 pThis->cUsers--;
706 }
707 else
708 rc = VERR_WRONG_ORDER;
709 RTCritSectLeave(&pThis->CritSect);
710 }
711 return rc;
712}
713
714
715RTDECL(int) RTPipeWrite(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
716{
717 RTPIPEINTERNAL *pThis = hPipe;
718 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
719 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
720 AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
721 AssertPtr(pcbWritten);
722 AssertPtr(pvBuf);
723
724 int rc = RTCritSectEnter(&pThis->CritSect);
725 if (RT_SUCCESS(rc))
726 {
727 /* No concurrent readers, sorry. */
728 if (pThis->cUsers == 0)
729 {
730 pThis->cUsers++;
731
732 /* If I/O is pending, check if it has completed. */
733 if (pThis->fIOPending)
734 rc = rtPipeWriteCheckCompletion(pThis);
735 else
736 rc = VINF_SUCCESS;
737 if (rc == VINF_SUCCESS)
738 {
739 Assert(!pThis->fIOPending);
740
741 /* Adjust the number of bytes to write to fit into the current
742 buffer quota, unless we've promised stuff in RTPipeSelectOne.
743 WriteQuotaAvailable better not be zero when it shouldn't!! */
744 FILE_PIPE_LOCAL_INFORMATION Info;
745 if ( !pThis->fPromisedWritable
746 && cbToWrite > 0
747 && rtPipeQueryInfo(pThis, &Info))
748 {
749 if (Info.NamedPipeState == FILE_PIPE_CLOSING_STATE)
750 rc = VERR_BROKEN_PIPE;
751 else if ( cbToWrite >= Info.WriteQuotaAvailable
752 && Info.OutboundQuota != 0
753 && (Info.WriteQuotaAvailable || pThis->cbBounceBufAlloc)
754 )
755 {
756 cbToWrite = Info.WriteQuotaAvailable;
757 if (!cbToWrite)
758 rc = VINF_TRY_AGAIN;
759 }
760 }
761 pThis->fPromisedWritable = false;
762
763 /* Do the bounce buffering. */
764 if ( pThis->cbBounceBufAlloc < cbToWrite
765 && pThis->cbBounceBufAlloc < RTPIPE_NT_SIZE)
766 {
767 if (cbToWrite > RTPIPE_NT_SIZE)
768 cbToWrite = RTPIPE_NT_SIZE;
769 void *pv = RTMemRealloc(pThis->pbBounceBuf, RT_ALIGN_Z(cbToWrite, _1K));
770 if (pv)
771 {
772 pThis->pbBounceBuf = (uint8_t *)pv;
773 pThis->cbBounceBufAlloc = RT_ALIGN_Z(cbToWrite, _1K);
774 }
775 else
776 rc = VERR_NO_MEMORY;
777 }
778 else if (cbToWrite > RTPIPE_NT_SIZE)
779 cbToWrite = RTPIPE_NT_SIZE;
780 if (RT_SUCCESS(rc) && cbToWrite)
781 {
782 memcpy(pThis->pbBounceBuf, pvBuf, cbToWrite);
783 pThis->cbBounceBufUsed = (uint32_t)cbToWrite;
784
785 /* Submit the write. */
786 rc = ResetEvent(pThis->Overlapped.hEvent); Assert(rc == TRUE);
787 DWORD cbWritten = 0;
788 if (WriteFile(pThis->hPipe, pThis->pbBounceBuf, (DWORD)pThis->cbBounceBufUsed,
789 &cbWritten, &pThis->Overlapped))
790 {
791 *pcbWritten = cbWritten;
792 rc = VINF_SUCCESS;
793 }
794 else if (GetLastError() == ERROR_IO_PENDING)
795 {
796 *pcbWritten = cbToWrite;
797 pThis->fIOPending = true;
798 rc = VINF_SUCCESS;
799 }
800 else if (GetLastError() == ERROR_NO_DATA)
801 rc = VERR_BROKEN_PIPE;
802 else
803 rc = RTErrConvertFromWin32(GetLastError());
804 }
805 else if (RT_SUCCESS(rc))
806 *pcbWritten = 0;
807 }
808 else if (RT_SUCCESS(rc))
809 *pcbWritten = 0;
810
811 if (rc == VERR_BROKEN_PIPE)
812 pThis->fBrokenPipe = true;
813
814 pThis->cUsers--;
815 }
816 else
817 rc = VERR_WRONG_ORDER;
818 RTCritSectLeave(&pThis->CritSect);
819 }
820 return rc;
821}
822
823
824RTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
825{
826 RTPIPEINTERNAL *pThis = hPipe;
827 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
828 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
829 AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
830 AssertPtr(pvBuf);
831 AssertPtrNull(pcbWritten);
832
833 int rc = RTCritSectEnter(&pThis->CritSect);
834 if (RT_SUCCESS(rc))
835 {
836 /* No concurrent readers, sorry. */
837 if (pThis->cUsers == 0)
838 {
839 pThis->cUsers++;
840
841 /*
842 * If I/O is pending, wait for it to complete.
843 */
844 if (pThis->fIOPending)
845 {
846 rc = rtPipeWriteCheckCompletion(pThis);
847 while (rc == VINF_TRY_AGAIN)
848 {
849 Assert(pThis->fIOPending);
850 HANDLE hEvent = pThis->Overlapped.hEvent;
851 RTCritSectLeave(&pThis->CritSect);
852 WaitForSingleObject(pThis->Overlapped.hEvent, INFINITE);
853 RTCritSectEnter(&pThis->CritSect);
854 }
855 }
856 if (RT_SUCCESS(rc))
857 {
858 Assert(!pThis->fIOPending);
859 pThis->fPromisedWritable = false;
860
861 /*
862 * Try write everything.
863 * No bounce buffering, cUsers protects us.
864 */
865 size_t cbTotalWritten = 0;
866 while (cbToWrite > 0)
867 {
868 rc = ResetEvent(pThis->Overlapped.hEvent); Assert(rc == TRUE);
869 pThis->fIOPending = true;
870 RTCritSectLeave(&pThis->CritSect);
871
872 DWORD cbWritten = 0;
873 if (WriteFile(pThis->hPipe, pvBuf,
874 cbToWrite <= ~(DWORD)0 ? (DWORD)cbToWrite : ~(DWORD)0,
875 &cbWritten, &pThis->Overlapped))
876 rc = VINF_SUCCESS;
877 else if (GetLastError() == ERROR_IO_PENDING)
878 {
879 WaitForSingleObject(pThis->Overlapped.hEvent, INFINITE);
880 if (GetOverlappedResult(pThis->hPipe, &pThis->Overlapped, &cbWritten, TRUE /*fWait*/))
881 rc = VINF_SUCCESS;
882 else
883 rc = RTErrConvertFromWin32(GetLastError());
884 }
885 else if (GetLastError() == ERROR_NO_DATA)
886 rc = VERR_BROKEN_PIPE;
887 else
888 rc = RTErrConvertFromWin32(GetLastError());
889
890 RTCritSectEnter(&pThis->CritSect);
891 pThis->fIOPending = false;
892 if (RT_FAILURE(rc))
893 break;
894
895 /* advance */
896 pvBuf = (char const *)pvBuf + cbWritten;
897 cbTotalWritten += cbWritten;
898 cbToWrite -= cbWritten;
899 }
900
901 if (pcbWritten)
902 {
903 *pcbWritten = cbTotalWritten;
904 if ( RT_FAILURE(rc)
905 && cbTotalWritten
906 && rc != VERR_INVALID_POINTER)
907 rc = VINF_SUCCESS;
908 }
909 }
910
911 if (rc == VERR_BROKEN_PIPE)
912 pThis->fBrokenPipe = true;
913
914 pThis->cUsers--;
915 }
916 else
917 rc = VERR_WRONG_ORDER;
918 RTCritSectLeave(&pThis->CritSect);
919 }
920 return rc;
921
922#if 1
923 return VERR_NOT_IMPLEMENTED;
924#else
925 int rc = rtPipeTryBlocking(pThis);
926 if (RT_SUCCESS(rc))
927 {
928 size_t cbTotalWritten = 0;
929 while (cbToWrite > 0)
930 {
931 ssize_t cbWritten = write(pThis->fd, pvBuf, RT_MIN(cbToWrite, SSIZE_MAX));
932 if (cbWritten < 0)
933 {
934 rc = RTErrConvertFromErrno(errno);
935 break;
936 }
937
938 /* advance */
939 pvBuf = (char const *)pvBuf + cbWritten;
940 cbTotalWritten += cbWritten;
941 cbToWrite -= cbWritten;
942 }
943
944 if (pcbWritten)
945 {
946 *pcbWritten = cbTotalWritten;
947 if ( RT_FAILURE(rc)
948 && cbTotalWritten
949 && rc != VERR_INVALID_POINTER)
950 rc = VINF_SUCCESS;
951 }
952
953 ASMAtomicDecU32(&pThis->u32State);
954 }
955 return rc;
956#endif
957}
958
959
960RTDECL(int) RTPipeFlush(RTPIPE hPipe)
961{
962 RTPIPEINTERNAL *pThis = hPipe;
963 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
964 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
965 AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
966
967 if (!FlushFileBuffers(pThis->hPipe))
968 {
969 int rc = RTErrConvertFromWin32(GetLastError());
970 if (rc == VERR_BROKEN_PIPE)
971 pThis->fBrokenPipe = true;
972 return rc;
973 }
974 return VINF_SUCCESS;
975}
976
977
978RTDECL(int) RTPipeSelectOne(RTPIPE hPipe, RTMSINTERVAL cMillies)
979{
980 RTPIPEINTERNAL *pThis = hPipe;
981 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
982 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
983
984 uint64_t const StartMsTS = RTTimeMilliTS();
985
986 int rc = RTCritSectEnter(&pThis->CritSect);
987 if (RT_FAILURE(rc))
988 return rc;
989 for (unsigned iLoop = 0;; iLoop++)
990 {
991 HANDLE hWait = INVALID_HANDLE_VALUE;
992 if (pThis->fRead)
993 {
994 if (pThis->fIOPending)
995 hWait = pThis->Overlapped.hEvent;
996 else
997 {
998 /* Peek at the pipe buffer and see how many bytes it contains. */
999 DWORD cbAvailable;
1000 if ( PeekNamedPipe(pThis->hPipe, NULL, 0, NULL, &cbAvailable, NULL)
1001 && cbAvailable > 0)
1002 {
1003 rc = VINF_SUCCESS;
1004 break;
1005 }
1006
1007 /* Start a zero byte read operation that we can wait on. */
1008 if (cMillies == 0)
1009 {
1010 rc = VERR_TIMEOUT;
1011 break;
1012 }
1013 AssertBreakStmt(pThis->cUsers == 0, rc = VERR_INTERNAL_ERROR_5);
1014 rc = ResetEvent(pThis->Overlapped.hEvent); Assert(rc == TRUE);
1015 DWORD cbRead = 0;
1016 if (ReadFile(pThis->hPipe, pThis->abBuf, 0, &cbRead, &pThis->Overlapped))
1017 {
1018 rc = VINF_SUCCESS;
1019 if (iLoop > 10)
1020 RTThreadYield();
1021 }
1022 else if (GetLastError() == ERROR_IO_PENDING)
1023 {
1024 pThis->cUsers++;
1025 pThis->fIOPending = true;
1026 pThis->fZeroByteRead = true;
1027 hWait = pThis->Overlapped.hEvent;
1028 }
1029 else
1030 rc = RTErrConvertFromWin32(GetLastError());
1031 }
1032 }
1033 else
1034 {
1035 if (pThis->fIOPending)
1036 {
1037 rc = rtPipeWriteCheckCompletion(pThis);
1038 if (RT_FAILURE(rc))
1039 break;
1040 }
1041 if (pThis->fIOPending)
1042 hWait = pThis->Overlapped.hEvent;
1043 else
1044 {
1045 FILE_PIPE_LOCAL_INFORMATION Info;
1046 if (rtPipeQueryInfo(pThis, &Info))
1047 {
1048 /* Check for broken pipe. */
1049 if (Info.NamedPipeState == FILE_PIPE_CLOSING_STATE)
1050 {
1051 rc = VERR_BROKEN_PIPE;
1052 break;
1053 }
1054 /* Check for available write buffer space. */
1055 else if (Info.WriteQuotaAvailable > 0)
1056 {
1057 pThis->fPromisedWritable = false;
1058 rc = VINF_SUCCESS;
1059 break;
1060 }
1061 /* delayed buffer alloc or timeout: phony promise
1062 later: See if we still can associate a semaphore with
1063 the pipe, like on OS/2. */
1064 else if ( Info.OutboundQuota == 0
1065 || cMillies)
1066 {
1067 pThis->fPromisedWritable = true;
1068 rc = VINF_SUCCESS;
1069 break;
1070 }
1071 }
1072 else
1073 {
1074 pThis->fPromisedWritable = true;
1075 rc = VINF_SUCCESS;
1076 break;
1077 }
1078 }
1079 }
1080 if (RT_FAILURE(rc))
1081 break;
1082
1083 /*
1084 * Check for timeout.
1085 */
1086 DWORD cMsMaxWait = INFINITE;
1087 if ( cMillies != RT_INDEFINITE_WAIT
1088 && ( hWait != INVALID_HANDLE_VALUE
1089 || iLoop > 10)
1090 )
1091 {
1092 uint64_t cElapsed = RTTimeMilliTS() - StartMsTS;
1093 if (cElapsed >= cMillies)
1094 {
1095 rc = VERR_TIMEOUT;
1096 break;
1097 }
1098 cMsMaxWait = cMillies - (uint32_t)cElapsed;
1099 }
1100
1101 /*
1102 * Wait.
1103 */
1104 if (hWait != INVALID_HANDLE_VALUE)
1105 {
1106 RTCritSectLeave(&pThis->CritSect);
1107
1108 DWORD dwRc = WaitForSingleObject(hWait, cMsMaxWait);
1109 if (dwRc == WAIT_OBJECT_0)
1110 rc = VINF_SUCCESS;
1111 else if (dwRc == WAIT_TIMEOUT)
1112 rc = VERR_TIMEOUT;
1113 else if (dwRc == WAIT_ABANDONED)
1114 rc = VERR_INVALID_HANDLE;
1115 else
1116 rc = RTErrConvertFromWin32(GetLastError());
1117 if ( RT_FAILURE(rc)
1118 && pThis->u32Magic != RTPIPE_MAGIC)
1119 return rc;
1120
1121 RTCritSectEnter(&pThis->CritSect);
1122 if (pThis->fZeroByteRead)
1123 {
1124 pThis->cUsers--;
1125 pThis->fIOPending = false;
1126 if (rc != VINF_SUCCESS)
1127 CancelIo(pThis->hPipe);
1128 DWORD cbRead = 0;
1129 GetOverlappedResult(pThis->hPipe, &pThis->Overlapped, &cbRead, TRUE /*fWait*/);
1130 }
1131 if (RT_FAILURE(rc))
1132 break;
1133 }
1134 }
1135
1136 if (rc == VERR_BROKEN_PIPE)
1137 pThis->fBrokenPipe = true;
1138
1139 RTCritSectLeave(&pThis->CritSect);
1140 return rc;
1141}
1142
1143
1144/**
1145 * Internal RTPollSetAdd helper that returns the handle that should be added to
1146 * the pollset.
1147 *
1148 * @returns Valid handle on success, INVALID_HANDLE_VALUE on failure.
1149 * @param hPipe The pipe handle.
1150 * @param fEvents The events we're polling for.
1151 * @param ph where to put the primary handle.
1152 */
1153int rtPipePollGetHandle(RTPIPE hPipe, uint32_t fEvents, PHANDLE ph)
1154{
1155 RTPIPEINTERNAL *pThis = hPipe;
1156 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1157 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
1158
1159 AssertReturn(!(fEvents & RTPOLL_EVT_READ) || pThis->fRead, VERR_INVALID_PARAMETER);
1160 AssertReturn(!(fEvents & RTPOLL_EVT_WRITE) || !pThis->fRead, VERR_INVALID_PARAMETER);
1161
1162 /* Later: Try register an event handle with the pipe like on OS/2, there is
1163 a file control for doing this obviously intended for the OS/2 subsys.
1164 The question is whether this still exists on Vista and W7. */
1165 *ph = pThis->Overlapped.hEvent;
1166 return VINF_SUCCESS;
1167}
1168
1169
1170/**
1171 * Checks for pending events.
1172 *
1173 * @returns Event mask or 0.
1174 * @param pThis The pipe handle.
1175 * @param fEvents The desired events.
1176 */
1177static uint32_t rtPipePollCheck(RTPIPEINTERNAL *pThis, uint32_t fEvents)
1178{
1179 uint32_t fRetEvents = 0;
1180 if (pThis->fBrokenPipe)
1181 fRetEvents |= RTPOLL_EVT_ERROR;
1182 else if (pThis->fRead)
1183 {
1184 if (!pThis->fIOPending)
1185 {
1186 DWORD cbAvailable;
1187 if (PeekNamedPipe(pThis->hPipe, NULL, 0, NULL, &cbAvailable, NULL))
1188 {
1189 if ( (fEvents & RTPOLL_EVT_READ)
1190 && cbAvailable > 0)
1191 fRetEvents |= RTPOLL_EVT_READ;
1192 }
1193 else
1194 {
1195 if (GetLastError() == ERROR_BROKEN_PIPE)
1196 pThis->fBrokenPipe = true;
1197 fRetEvents |= RTPOLL_EVT_ERROR;
1198 }
1199 }
1200 }
1201 else
1202 {
1203 if (pThis->fIOPending)
1204 {
1205 rtPipeWriteCheckCompletion(pThis);
1206 if (pThis->fBrokenPipe)
1207 fRetEvents |= RTPOLL_EVT_ERROR;
1208 }
1209 if ( !pThis->fIOPending
1210 && !fRetEvents)
1211 {
1212 FILE_PIPE_LOCAL_INFORMATION Info;
1213 if (rtPipeQueryInfo(pThis, &Info))
1214 {
1215 /* Check for broken pipe. */
1216 if (Info.NamedPipeState == FILE_PIPE_CLOSING_STATE)
1217 {
1218 fRetEvents = RTPOLL_EVT_ERROR;
1219 pThis->fBrokenPipe = true;
1220 }
1221
1222 /* Check if there is available buffer space. */
1223 if ( !fRetEvents
1224 && (fEvents & RTPOLL_EVT_WRITE)
1225 && ( Info.WriteQuotaAvailable > 0
1226 || Info.OutboundQuota == 0)
1227 )
1228 fRetEvents |= RTPOLL_EVT_WRITE;
1229 }
1230 else if (fEvents & RTPOLL_EVT_WRITE)
1231 fRetEvents |= RTPOLL_EVT_WRITE;
1232 }
1233 }
1234
1235 return fRetEvents;
1236}
1237
1238
1239/**
1240 * Internal RTPoll helper that polls the pipe handle and, if @a fNoWait is
1241 * clear, starts whatever actions we've got running during the poll call.
1242 *
1243 * @returns 0 if no pending events, actions initiated if @a fNoWait is clear.
1244 * Event mask (in @a fEvents) and no actions if the handle is ready
1245 * already.
1246 * UINT32_MAX (asserted) if the pipe handle is busy in I/O or a
1247 * different poll set.
1248 *
1249 * @param hPipe The pipe handle.
1250 * @param hPollSet The poll set handle (for access checks).
1251 * @param fEvents The events we're polling for.
1252 * @param fFinalEntry Set if this is the final entry for this handle
1253 * in this poll set. This can be used for dealing
1254 * with duplicate entries.
1255 * @param fNoWait Set if it's a zero-wait poll call. Clear if
1256 * we'll wait for an event to occur.
1257 */
1258uint32_t rtPipePollStart(RTPIPE hPipe, RTPOLLSET hPollSet, uint32_t fEvents, bool fFinalEntry, bool fNoWait)
1259{
1260 /** @todo All this polling code could be optimized to make fewer system
1261 * calls; like for instance the ResetEvent calls. */
1262 RTPIPEINTERNAL *pThis = hPipe;
1263 AssertPtrReturn(pThis, UINT32_MAX);
1264 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, UINT32_MAX);
1265
1266 int rc = RTCritSectEnter(&pThis->CritSect);
1267 AssertRCReturn(rc, UINT32_MAX);
1268
1269 /* Check that this is the only current use of this pipe. */
1270 uint32_t fRetEvents;
1271 if ( pThis->cUsers == 0
1272 || pThis->hPollSet == hPollSet)
1273 {
1274 /* Check what the current events are. */
1275 fRetEvents = rtPipePollCheck(pThis, fEvents);
1276 if ( !fRetEvents
1277 && !fNoWait)
1278 {
1279 /* Make sure the event semaphore has been reset. */
1280 if (!pThis->fIOPending)
1281 {
1282 rc = ResetEvent(pThis->Overlapped.hEvent);
1283 Assert(rc == TRUE);
1284 }
1285
1286 /* Kick off the zero byte read thing if applicable. */
1287 if ( !pThis->fIOPending
1288 && pThis->fRead
1289 && (fEvents & RTPOLL_EVT_READ)
1290 )
1291 {
1292 DWORD cbRead = 0;
1293 if (ReadFile(pThis->hPipe, pThis->abBuf, 0, &cbRead, &pThis->Overlapped))
1294 fRetEvents = rtPipePollCheck(pThis, fEvents);
1295 else if (GetLastError() == ERROR_IO_PENDING)
1296 {
1297 pThis->fIOPending = true;
1298 pThis->fZeroByteRead = true;
1299 }
1300 else
1301 fRetEvents = RTPOLL_EVT_ERROR;
1302 }
1303
1304 /* If we're still set for the waiting, record the poll set and
1305 mark the pipe used. */
1306 if (!fRetEvents)
1307 {
1308 pThis->cUsers++;
1309 pThis->hPollSet = hPollSet;
1310 }
1311 }
1312 }
1313 else
1314 {
1315 AssertFailed();
1316 fRetEvents = UINT32_MAX;
1317 }
1318
1319 RTCritSectLeave(&pThis->CritSect);
1320 return fRetEvents;
1321}
1322
1323
1324/**
1325 * Called after a WaitForMultipleObjects returned in order to check for pending
1326 * events and stop whatever actions that rtPipePollStart() initiated.
1327 *
1328 * @returns Event mask or 0.
1329 *
1330 * @param hPipe The pipe handle.
1331 * @param fEvents The events we're polling for.
1332 * @param fFinalEntry Set if this is the final entry for this handle
1333 * in this poll set. This can be used for dealing
1334 * with duplicate entries. Only keep in mind that
1335 * this method is called in reverse order, so the
1336 * first call will have this set (when the entire
1337 * set was processed).
1338 * @param fHarvestEvents Set if we should check for pending events.
1339 */
1340uint32_t rtPipePollDone(RTPIPE hPipe, uint32_t fEvents, bool fFinalEntry, bool fHarvestEvents)
1341{
1342 RTPIPEINTERNAL *pThis = hPipe;
1343 AssertPtrReturn(pThis, 0);
1344 AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, 0);
1345
1346 int rc = RTCritSectEnter(&pThis->CritSect);
1347 AssertRCReturn(rc, 0);
1348
1349 Assert(pThis->cUsers > 0);
1350
1351
1352 /* Cancel the zero byte read. */
1353 uint32_t fRetEvents = 0;
1354 if (pThis->fZeroByteRead)
1355 {
1356 CancelIo(pThis->hPipe);
1357 DWORD cbRead = 0;
1358 if ( !GetOverlappedResult(pThis->hPipe, &pThis->Overlapped, &cbRead, TRUE /*fWait*/)
1359 && GetLastError() != ERROR_OPERATION_ABORTED)
1360 fRetEvents = RTPOLL_EVT_ERROR;
1361
1362 pThis->fIOPending = false;
1363 pThis->fZeroByteRead = false;
1364 }
1365
1366 /* harvest events. */
1367 fRetEvents |= rtPipePollCheck(pThis, fEvents);
1368
1369 /* update counters. */
1370 pThis->cUsers--;
1371 if (!pThis->cUsers)
1372 pThis->hPollSet = NIL_RTPOLLSET;
1373
1374 RTCritSectLeave(&pThis->CritSect);
1375 return fRetEvents;
1376}
1377
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette