Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3435 lines
87 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. vrnmpipe.c
  5. Abstract:
  6. Contains Named Pipe function handlers for Vdm Redir support. This module
  7. contains the following Vr (VdmRedir) routines:
  8. Contents:
  9. VrGetNamedPipeInfo
  10. VrGetNamedPipeHandleState
  11. VrSetNamedPipeHandleState
  12. VrPeekNamedPipe
  13. VrTransactNamedPipe
  14. VrCallNamedPipe
  15. VrWaitNamedPipe
  16. VrNetHandleGetInfo
  17. VrNetHandleSetInfo
  18. VrReadWriteAsyncNmPipe
  19. VrNmPipeInterrupt
  20. VrTerminateNamedPipes
  21. VrCancelPipeIo
  22. There are a couple of extra routines which must be called on open and close.
  23. Because these routines (in Dos Emulator) are general purpose, our open
  24. and close routines will be called for every file open/handle close. We
  25. must check that the operation is being performed on a named pipe entity.
  26. The routines are:
  27. VrAddOpenNamedPipeInfo
  28. VrRemoveOpenNamedPipeInfo
  29. Because named pipes are now opened in overlapped I/O mode, in case an app
  30. wishes to perform an asynchronous read or write operation, we must provide
  31. our own read/write routines for synchronously reading a pipe. If we just
  32. left this to the standard read/write routines in DEM, they would return an
  33. error because the handles were opened with FLAG_FILE_OVERLAPPED and the
  34. operations are performed with the LPOVERLAPPED parameter set to NULL
  35. VrReadNamedPipe
  36. VrWriteNamedPipe
  37. A couple of helper routines which are callable from outside of this module:
  38. VrIsNamedPipeName
  39. VrIsNamedPipeHandle
  40. VrConvertLocalNtPipeName
  41. Private (Vrp) routines:
  42. VrpAsyncNmPipeThread
  43. VrpSnapshotEventList
  44. VrpSearchForRequestByEventHandle
  45. VrpCompleteAsyncRequest
  46. VrpQueueAsyncRequest
  47. VrpDequeueAsyncRequest
  48. VrpFindCompletedRequest
  49. VrpAddOpenNamedPipeInfo
  50. VrpGetOpenNamedPipeInfo
  51. VrpRemoveOpenNamedPipeInfo
  52. RememberPipeIo
  53. ForgetPipeIo
  54. Author:
  55. Richard L Firth (rfirth) 10-Sep-1991
  56. Environment:
  57. Any 32-bit flat address space
  58. Notes:
  59. This module implements client-side named pipe support for the VDM process.
  60. Client-side named pipes are opened using the standard DOS open call (INT 21/
  61. ah=3dh) from a DOS app. The actual open is performed in the 32-bit context
  62. where a 32-bit handle is returned. This is put in the DOS context SFT and
  63. DOS returns an 8-bit J(ob) F(ile) N(umber) which the app then uses in other
  64. named pipe calls. The redir, which handles named pipe requests apart from
  65. open and close, must map the 8-bit JFN to the original 32-bit handle using
  66. a routine exported from DOS. The handle is then stored in BP:BX and control
  67. passed here.
  68. When an open succeeds, we add an OPEN_NAMED_PIPE_INFO structure to a list
  69. of structures. This maps the handle and name (for DosQNmPipeInfo). We don't
  70. expect to have very many of these structures at any one time, so they are
  71. singly linked and sequentially traversed using the handle as a key
  72. This code assumes that only one process at a time will be updating the list
  73. of structures and that any non-stack data items in this module will be
  74. replicated to all processes which use these functions (Ie the data is NOT
  75. shared)
  76. Revision History:
  77. 10-Sep-1991 RFirth
  78. Created
  79. --*/
  80. #include <nt.h>
  81. #include <ntrtl.h> // ASSERT, DbgPrint
  82. #include <nturtl.h>
  83. #include <windows.h>
  84. #include <softpc.h> // x86 virtual machine definitions
  85. #include <vrdlctab.h>
  86. #include <vdmredir.h> // common Vdm Redir stuff
  87. #include <vrinit.h> // VrQueueCompletionHandler
  88. #include "vrdebug.h" // IF_DEBUG
  89. #include "vrputil.h" // private utility prototypes
  90. //#include <os2def.h>
  91. //#include <bsedos.h> // PIPEINFO structure
  92. #include <align.h>
  93. #include <lmcons.h> // LM20_PATHLEN
  94. #include <lmerr.h> // NERR_
  95. #include <string.h> // Dos still dealing with ASCII
  96. #include <dossvc.h> // PDEMEXTERR
  97. #include <exterr.h> // extended error info
  98. //
  99. // the following 2 #undef's required because without them, insignia.h gives
  100. // errors (BOOL previously typedef'd) when compiled for MIPS
  101. //
  102. #undef BOOL
  103. #undef NT_INCLUDED
  104. #include <insignia.h> // Insignia defines
  105. #include <xt.h> // half_word
  106. #include <ica.h> // ica_hw_interrupt
  107. #include <idetect.h> // WaitIfIdle
  108. #include <vrica.h> // call_ica_hw_interrupt
  109. #include <vrnmpipe.h> // routine prototypes
  110. #include <stdio.h>
  111. //
  112. // manifests
  113. //
  114. //#define NAMED_PIPE_TIMEOUT 300000 // 5 minutes
  115. #define NAMED_PIPE_TIMEOUT INFINITE
  116. //
  117. // private data types
  118. //
  119. //
  120. // OVERLAPPED_PIPE_IO - contains handle of thread issuing named pipe I/O request.
  121. // If the app is later killed, we need to cancel any pending named pipe I/O
  122. //
  123. typedef struct _OVERLAPPED_PIPE_IO {
  124. struct _OVERLAPPED_PIPE_IO* Next;
  125. DWORD Thread;
  126. BOOL Cancelled;
  127. OVERLAPPED Overlapped;
  128. } OVERLAPPED_PIPE_IO, *POVERLAPPED_PIPE_IO;
  129. //
  130. // private routine prototypes
  131. //
  132. #undef PRIVATE
  133. #define PRIVATE /* static */ // actually, want to see routines in FREE build
  134. PRIVATE
  135. DWORD
  136. VrpAsyncNmPipeThread(
  137. IN LPVOID Parameters
  138. );
  139. PRIVATE
  140. DWORD
  141. VrpSnapshotEventList(
  142. OUT LPHANDLE pList
  143. );
  144. PRIVATE
  145. PDOS_ASYNC_NAMED_PIPE_INFO
  146. VrpSearchForRequestByEventHandle(
  147. IN HANDLE EventHandle
  148. );
  149. PRIVATE
  150. VOID
  151. VrpCompleteAsyncRequest(
  152. IN PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo
  153. );
  154. PRIVATE
  155. VOID
  156. VrpQueueAsyncRequest(
  157. IN PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo
  158. );
  159. PRIVATE
  160. PDOS_ASYNC_NAMED_PIPE_INFO
  161. VrpDequeueAsyncRequest(
  162. IN PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo
  163. );
  164. PRIVATE
  165. PDOS_ASYNC_NAMED_PIPE_INFO
  166. VrpFindCompletedRequest(
  167. VOID
  168. );
  169. PRIVATE
  170. BOOL
  171. VrpAddOpenNamedPipeInfo(
  172. IN HANDLE Handle,
  173. IN LPSTR PipeName
  174. );
  175. PRIVATE
  176. POPEN_NAMED_PIPE_INFO
  177. VrpGetOpenNamedPipeInfo(
  178. IN HANDLE Handle
  179. );
  180. PRIVATE
  181. BOOL
  182. VrpRemoveOpenNamedPipeInfo(
  183. IN HANDLE Handle
  184. );
  185. PRIVATE
  186. VOID
  187. RememberPipeIo(
  188. IN POVERLAPPED_PIPE_IO PipeIo
  189. );
  190. PRIVATE
  191. VOID
  192. ForgetPipeIo(
  193. IN POVERLAPPED_PIPE_IO PipeIo
  194. );
  195. #if DBG
  196. VOID DumpOpenPipeList(VOID);
  197. VOID DumpRequestQueue(VOID);
  198. #endif
  199. //
  200. // global data
  201. //
  202. DWORD VrPeekNamedPipeTickCount;
  203. //
  204. // private data
  205. //
  206. CRITICAL_SECTION VrNamedPipeCancelCritSec;
  207. POVERLAPPED_PIPE_IO PipeIoQueue = NULL;
  208. //
  209. // Vdm Redir Named Pipe support routines
  210. //
  211. VOID
  212. VrGetNamedPipeInfo(
  213. VOID
  214. )
  215. /*++
  216. Routine Description:
  217. Performs GetNamedPipeInfo (DosQNmPipeInfo) request on behalf of VDM redir
  218. Arguments:
  219. Function = 5F32h
  220. ENTRY BP:BX = 32-bit Named Pipe handle
  221. CX = Buffer size
  222. DX = Info level
  223. DS:SI = Buffer
  224. EXIT CF = 1
  225. AX = Error code
  226. CF = 0
  227. no error
  228. AX = undefined
  229. Return Value:
  230. None. Returns values in VDM Ax and Flags registers
  231. --*/
  232. {
  233. HANDLE Handle;
  234. DWORD Flags, OutBufferSize, InBufferSize, MaxInstances, CurInstances, bufLen;
  235. PIPEINFO* PipeInfo;
  236. BOOL Ok;
  237. POPEN_NAMED_PIPE_INFO OpenNamedPipeInfo;
  238. #if DBG
  239. IF_DEBUG(NAMEPIPE) {
  240. DbgPrint("VrGetNamedPipeInfo(0x%08x, %d, %04x:%04x, %d)\n",
  241. HANDLE_FROM_WORDS(getBP(), getBX()),
  242. getDX(),
  243. getDS(),
  244. getSI(),
  245. getCX()
  246. );
  247. }
  248. #endif
  249. //
  250. // bp:bx is 32-bit named pipe handle. Mapped from 8-bit handle in redir
  251. //
  252. Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  253. //
  254. // we have to collect the info to put in the PIPEINFO structure from
  255. // various sources - we stored the name (& name length) in a
  256. // OPEN_NAMED_PIPE_INFO structure. The other stuff we get from
  257. // GetNamedPipeInfo and GetNamedPipeHandleState
  258. //
  259. OpenNamedPipeInfo = VrpGetOpenNamedPipeInfo(Handle);
  260. if (OpenNamedPipeInfo) {
  261. bufLen = getCX();
  262. if (bufLen >= sizeof(PIPEINFO)) {
  263. Ok = GetNamedPipeInfo(Handle,
  264. &Flags,
  265. &OutBufferSize,
  266. &InBufferSize,
  267. &MaxInstances
  268. );
  269. if (Ok) {
  270. //
  271. // we are only interested in the current # instances of the
  272. // named pipe from this next call
  273. //
  274. Ok = GetNamedPipeHandleState(Handle,
  275. NULL,
  276. &CurInstances,
  277. NULL,
  278. NULL,
  279. NULL,
  280. 0
  281. );
  282. if (Ok) {
  283. PipeInfo = (PIPEINFO*)POINTER_FROM_WORDS(getDS(), getSI());
  284. WRITE_WORD(&PipeInfo->cbOut, (OutBufferSize > 65535 ? 65535 : OutBufferSize));
  285. WRITE_WORD(&PipeInfo->cbIn, (InBufferSize > 65535 ? 65535 : InBufferSize));
  286. WRITE_BYTE(&PipeInfo->cbMaxInst, (MaxInstances > 255 ? 255 : MaxInstances));
  287. WRITE_BYTE(&PipeInfo->cbCurInst, (CurInstances > 255 ? 255 : CurInstances));
  288. WRITE_BYTE(&PipeInfo->cbName, OpenNamedPipeInfo->NameLength);
  289. //
  290. // copy name if enough space
  291. //
  292. if (bufLen - sizeof(PIPEINFO) >= OpenNamedPipeInfo->NameLength) {
  293. strcpy(PipeInfo->szName, OpenNamedPipeInfo->Name);
  294. }
  295. setCF(0);
  296. } else {
  297. SET_ERROR(VrpMapLastError());
  298. }
  299. } else {
  300. SET_ERROR(VrpMapLastError());
  301. }
  302. } else {
  303. SET_ERROR(ERROR_BUFFER_OVERFLOW);
  304. }
  305. } else {
  306. #if DBG
  307. IF_DEBUG(NAMEPIPE) {
  308. DbgPrint("VrGetNamedPipeInfo: Error: can't map handle 0x%08x\n", Handle);
  309. }
  310. #endif
  311. SET_ERROR(ERROR_INVALID_HANDLE);
  312. }
  313. #if DBG
  314. IF_DEBUG(NAMEPIPE) {
  315. if (getCF()) {
  316. DbgPrint("VrGetNamedPipeInfo: returning ERROR: %d\n", getAX());
  317. } else {
  318. DbgPrint("VrGetNamedPipeInfo: returning OK. PIPEINFO:\n"
  319. "cbOut %04x\n"
  320. "cbIn %04x\n"
  321. "cbMaxInst %02x\n"
  322. "cbCurInst %02x\n"
  323. "cbName %02x\n"
  324. "szName %s\n",
  325. READ_WORD(&PipeInfo->cbOut),
  326. READ_WORD(&PipeInfo->cbIn),
  327. READ_BYTE(&PipeInfo->cbMaxInst),
  328. READ_BYTE(&PipeInfo->cbCurInst),
  329. READ_BYTE(&PipeInfo->cbName),
  330. READ_BYTE(&PipeInfo->szName)
  331. );
  332. }
  333. }
  334. #endif
  335. }
  336. VOID
  337. VrGetNamedPipeHandleState(
  338. VOID
  339. )
  340. /*++
  341. Routine Description:
  342. Performs GetNamedPipeHandleState request on behalf of VDM redir
  343. Arguments:
  344. Function = 5F33h
  345. ENTRY BP:BX = 32-bit Named Pipe handle
  346. EXIT CF = 1
  347. AX = Error code
  348. CF = 0
  349. AX = Pipe mode:
  350. BSxxxWxRIIIIIIII
  351. where:
  352. B = Blocking mode. If B=1 the pipe is non blocking
  353. S = Server end of pipe if 1
  354. W = Pipe is written in message mode if 1 (else byte mode)
  355. R = Pipe is read in message mode if 1 (else byte mode)
  356. I = Pipe instances. Unlimited if 0xFF
  357. Return Value:
  358. None. Returns values in VDM Ax and Flags registers
  359. --*/
  360. {
  361. HANDLE Handle;
  362. DWORD State, CurInstances, Flags, MaxInstances;
  363. BOOL Ok;
  364. WORD PipeHandleState;
  365. #if DBG
  366. IF_DEBUG(NAMEPIPE) {
  367. DbgPrint("VrGetNamedPipeHandleState\n");
  368. }
  369. #endif
  370. Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  371. Ok = GetNamedPipeHandleState(Handle,
  372. &State,
  373. &CurInstances,
  374. NULL,
  375. NULL,
  376. NULL,
  377. 0
  378. );
  379. if (Ok) {
  380. Ok = GetNamedPipeInfo(Handle, &Flags, NULL, NULL, &MaxInstances);
  381. if (Ok) {
  382. //
  383. // Create the Dos pipe handle state from the information gathered
  384. //
  385. PipeHandleState = (WORD)((MaxInstances > 255) ? 255 : (MaxInstances & 0xff))
  386. | (WORD)((State & PIPE_NOWAIT) ? NP_NBLK : 0)
  387. | (WORD)((State & PIPE_READMODE_MESSAGE) ? NP_RMESG : 0)
  388. //
  389. // BUGBUG - can't possibly be server end????
  390. //
  391. | (WORD)((Flags & PIPE_SERVER_END) ? NP_SERVER : 0)
  392. | (WORD)((Flags & PIPE_TYPE_MESSAGE) ? NP_WMESG : 0)
  393. ;
  394. setAX((WORD)PipeHandleState);
  395. setCF(0);
  396. } else {
  397. SET_ERROR(VrpMapLastError());
  398. }
  399. } else {
  400. SET_ERROR(VrpMapLastError());
  401. }
  402. }
  403. VOID
  404. VrSetNamedPipeHandleState(
  405. VOID
  406. )
  407. /*++
  408. Routine Description:
  409. Performs SetNamedPipeHandleState request on behalf of VDM redir
  410. Arguments:
  411. Function = 5F34h
  412. ENTRY BP:BX = 32-bit Named Pipe handle
  413. CX = Pipe mode to set
  414. EXIT CF = 1
  415. AX = Error code
  416. CF = 0
  417. AX = Pipe mode set
  418. Return Value:
  419. None. Returns values in VDM Ax and Flags registers
  420. --*/
  421. {
  422. HANDLE Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  423. BOOL Ok;
  424. WORD DosPipeMode;
  425. DWORD WinPipeMode;
  426. #define ILLEGAL_NMP_SETMODE_BITS ~(NP_NBLK | NP_RMESG | NP_WMESG)
  427. #if DBG
  428. IF_DEBUG(NAMEPIPE) {
  429. DbgPrint("VrSetNamedPipeHandleState(0x%08x, %04x)\n", Handle, getCX());
  430. }
  431. #endif
  432. //
  433. // Convert the Dos pipe mode bits to Win32 pipe mode bits. We can only
  434. // change the wait/no-wait status and the read mode of the pipe (byte
  435. // or message)
  436. //
  437. DosPipeMode = getCX();
  438. //
  439. // catch disallowed flags
  440. //
  441. if (DosPipeMode & ILLEGAL_NMP_SETMODE_BITS) {
  442. SET_ERROR(ERROR_INVALID_PARAMETER);
  443. return;
  444. }
  445. WinPipeMode = ((DosPipeMode & NP_NBLK)
  446. ? PIPE_NOWAIT
  447. : PIPE_WAIT)
  448. | ((DosPipeMode & NP_RMESG)
  449. ? PIPE_READMODE_MESSAGE
  450. : PIPE_READMODE_BYTE);
  451. if (!(Ok = SetNamedPipeHandleState(Handle, &WinPipeMode, NULL, NULL))) {
  452. #if DBG
  453. IF_DEBUG(NAMEPIPE) {
  454. DbgPrint("Error: VrSetNamedPipeHandleState: returning %d\n", GetLastError());
  455. }
  456. #endif
  457. SET_ERROR(VrpMapLastError());
  458. } else {
  459. setCF(0);
  460. }
  461. }
  462. VOID
  463. VrPeekNamedPipe(
  464. VOID
  465. )
  466. /*++
  467. Routine Description:
  468. Performs PeekNamedPipe request on behalf of VDM redir
  469. Arguments:
  470. Function = 5F35h
  471. ENTRY BP:BX = 32-bit Named Pipe handle
  472. CX = Size of buffer for peek
  473. DS:SI = Buffer address
  474. EXIT CF = 1
  475. AX = Error code
  476. CF = 0
  477. AX = Pipe status
  478. BX = Number of bytes peeked into buffer
  479. CX = Number of bytes in pipe
  480. DX = Number of bytes in message
  481. DI = Pipe status
  482. DS:SI = Data peeked
  483. Return Value:
  484. None. Returns values in VDM Ax and Flags registers
  485. --*/
  486. {
  487. HANDLE Handle;
  488. LPBYTE lpBuffer;
  489. DWORD nBufferSize, BytesRead, BytesAvailable, BytesLeftInMessage;
  490. BOOL Ok;
  491. #if DBG
  492. IF_DEBUG(NAMEPIPE) {
  493. DbgPrint("VrPeekNamedPipe(0x%08x, %04x:%04x, %d)\n",
  494. HANDLE_FROM_WORDS(getBP(), getBX()),
  495. getDS(),
  496. getSI(),
  497. getCX()
  498. );
  499. }
  500. #endif
  501. Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  502. lpBuffer = (LPBYTE)POINTER_FROM_WORDS(getDS(), getSI());
  503. nBufferSize = (DWORD)getCX();
  504. Ok = PeekNamedPipe(Handle,
  505. lpBuffer,
  506. nBufferSize,
  507. &BytesRead,
  508. &BytesAvailable,
  509. &BytesLeftInMessage
  510. );
  511. if (Ok) {
  512. //
  513. // Since we gave a 16-bit quantity for the buffer size, BytesRead
  514. // cannot be >64K
  515. //
  516. setBX((WORD)BytesRead);
  517. setCX((WORD)BytesAvailable);
  518. //
  519. // if message mode pipe, return total bytes in message (as opposed to
  520. // NT's bytes LEFT in message)
  521. //
  522. setDX((WORD)(BytesLeftInMessage ? ((WORD)BytesLeftInMessage + (WORD)BytesRead) : 0));
  523. //
  524. // Not sure what this means. According to NETPIAPI.ASM, a 3 is returned
  525. // on success, meaning status = connected. The named pipe statuses are
  526. // (according to BSEDOS.H):
  527. //
  528. // NP_DISCONNECTED 1
  529. // NP_LISTENING 2
  530. // NP_CONNECTED 3
  531. // NP_CLOSING 4
  532. //
  533. // Presumably, a client-side pipe can only be connected or the pipe is
  534. // closed
  535. //
  536. setDI(NP_CONNECTED);
  537. setCF(0);
  538. #if DBG
  539. IF_DEBUG(NAMEPIPE) {
  540. DbgPrint("VrPeekNamedPipe: Ok: %d bytes peeked, %d avail, %d left in message\n",
  541. BytesRead,
  542. BytesAvailable,
  543. BytesLeftInMessage
  544. );
  545. }
  546. #endif
  547. } else {
  548. SET_ERROR(VrpMapLastError());
  549. #if DBG
  550. IF_DEBUG(NAMEPIPE) {
  551. DbgPrint("VrPeekNamedPipe: Error %d\n", getAX());
  552. }
  553. #endif
  554. BytesRead = 0;
  555. }
  556. //
  557. // idle processing - only idle if there is nothing to return (including
  558. // an error occurred)
  559. //
  560. // For now, allow 10(!) peeks per second - on ALL pipe handles
  561. //
  562. if (!BytesRead) {
  563. if (GetTickCount() - VrPeekNamedPipeTickCount < 100) {
  564. WaitIfIdle();
  565. }
  566. }
  567. VrPeekNamedPipeTickCount = GetTickCount();
  568. }
  569. VOID
  570. VrTransactNamedPipe(
  571. VOID
  572. )
  573. /*++
  574. Routine Description:
  575. Performs TransactNamedPipe request on behalf of VDM redir
  576. Arguments:
  577. Function = 5F36h
  578. ENTRY BP:BX = 32-bit Named Pipe handle
  579. CX = Transmit buffer length
  580. DX = Receive buffer length
  581. DS:SI = Transmit buffer
  582. ES:DI = Receive buffer
  583. EXIT CF = 1
  584. AX = Error code
  585. CF = 0
  586. CX = Number of bytes in Receive buffer
  587. Return Value:
  588. None. Returns values in VDM Ax and Flags registers
  589. --*/
  590. {
  591. DWORD BytesRead;
  592. BOOL Ok;
  593. OVERLAPPED_PIPE_IO pipeio;
  594. DWORD Error;
  595. HANDLE Handle;
  596. #if DBG
  597. IF_DEBUG(NAMEPIPE) {
  598. DbgPrint("VrTransactNamedPipe(0x%08x, TxLen=%d, TxBuf=%04x:%04x, RxLen=%d, RxBuf=%04x:%04x)\n",
  599. HANDLE_FROM_WORDS(getBP(), getBX()),
  600. getCX(),
  601. getDS(),
  602. getSI(),
  603. getDX(),
  604. getES(),
  605. getDI()
  606. );
  607. }
  608. #endif
  609. //
  610. // now that we are opening named pipes with FLAG_FILE_OVERLAPPED, we have
  611. // to perform every I/O operation with an OVERLAPPED structure. We are only
  612. // interested in the event handle. We create a new event for synchronous
  613. // operation which requires an OVERLAPPED structure. Create the event to
  614. // be manually reset - this way, if we wait on it & the read has already
  615. // completed, the wait completes immediately. If we create an auto-reset
  616. // event, then it may go back into the not-signalled state, causing us to
  617. // wait forever for an event that has already occurred
  618. //
  619. RtlZeroMemory(&pipeio, sizeof(pipeio));
  620. pipeio.Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  621. if (pipeio.Overlapped.hEvent != NULL) {
  622. //
  623. // collect arguments from registers and perform transact named pipe call
  624. //
  625. Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  626. RememberPipeIo(&pipeio);
  627. Ok = TransactNamedPipe(Handle,
  628. (LPVOID)POINTER_FROM_WORDS(getDS(), getSI()),
  629. (DWORD)getCX(),
  630. (LPVOID)POINTER_FROM_WORDS(getES(), getDI()),
  631. (DWORD)getDX(),
  632. &BytesRead,
  633. &pipeio.Overlapped
  634. );
  635. Error = Ok ? NO_ERROR : GetLastError();
  636. if (Error == ERROR_IO_PENDING) {
  637. #if DBG
  638. IF_DEBUG(NAMEPIPE) {
  639. DbgPrint("VrTransactNamedPipe: Ok, Waiting on hEvent...\n");
  640. }
  641. #endif
  642. Error = WaitForSingleObject(pipeio.Overlapped.hEvent, NAMED_PIPE_TIMEOUT);
  643. }
  644. ForgetPipeIo(&pipeio);
  645. if (pipeio.Cancelled) {
  646. Error = WAIT_TIMEOUT;
  647. }
  648. if (Error == NO_ERROR || Error == ERROR_MORE_DATA) {
  649. GetOverlappedResult(Handle, &pipeio.Overlapped, &BytesRead, TRUE);
  650. #if DBG
  651. IF_DEBUG(NAMEPIPE) {
  652. DbgPrint("WaitForSingleObject completed. BytesRead=%d\n", BytesRead);
  653. }
  654. #endif
  655. setCX((WORD)BytesRead);
  656. setAX((WORD)Error);
  657. //
  658. // if we are returning NO_ERROR then carry flag is clear, else we
  659. // are returning ERROR_MORE_DATA: set carry flag
  660. //
  661. setCF(Error == ERROR_MORE_DATA);
  662. } else {
  663. //
  664. // if we timed-out then close the pipe handle
  665. //
  666. if (Error == WAIT_TIMEOUT) {
  667. #if DBG
  668. IF_DEBUG(NAMEPIPE) {
  669. DbgPrint("VrTransactNamedPipe: Wait timed out: closing handle %08x\n", Handle);
  670. }
  671. #endif
  672. CloseHandle(Handle);
  673. VrpRemoveOpenNamedPipeInfo(Handle);
  674. } else {
  675. Error = VrpMapDosError(Error);
  676. }
  677. SET_ERROR((WORD)Error);
  678. #if DBG
  679. IF_DEBUG(NAMEPIPE) {
  680. DbgPrint("VrTransactNamedPipe: Error: %d\n", getAX());
  681. }
  682. #endif
  683. }
  684. //
  685. // kill the event handle
  686. //
  687. CloseHandle(pipeio.Overlapped.hEvent);
  688. } else {
  689. //
  690. // failed to create event handle
  691. //
  692. #if DBG
  693. IF_DEBUG(NAMEPIPE) {
  694. DbgPrint("vdmredir: DosTransactNamedPipe couldn't create event error %d\n", GetLastError());
  695. }
  696. #endif
  697. SET_ERROR(VrpMapLastError());
  698. }
  699. }
  700. VOID
  701. VrCallNamedPipe(
  702. VOID
  703. )
  704. /*++
  705. Routine Description:
  706. Performs CallNamedPipe request on behalf of VDM redir
  707. Arguments:
  708. Function = 5F37h
  709. ENTRY DS:SI = Pointer to CallNmPipe structure:
  710. DWORD Timeout; +0
  711. LPWORD lpBytesRead; +4
  712. WORD OutputBufferLen; +8
  713. LPBYTE OutputBuffer; +10
  714. WORD InputBufferLength; +14
  715. LPBYTE InputBuffer; +16
  716. LPSTR PipeName; +20
  717. EXIT CF = 1
  718. AX = Error code
  719. CF = 0
  720. CX = Bytes received
  721. Return Value:
  722. None. Returns values in VDM Ax and Flags registers
  723. --*/
  724. {
  725. BOOL Ok;
  726. DWORD BytesRead;
  727. PDOS_CALL_NAMED_PIPE_STRUCT StructurePointer;
  728. StructurePointer = (PDOS_CALL_NAMED_PIPE_STRUCT)POINTER_FROM_WORDS(getDS(), getSI());
  729. #if DBG
  730. IF_DEBUG(NAMEPIPE) {
  731. DbgPrint("VrCallNamedPipe(%s)\n", (LPSTR)READ_FAR_POINTER(&StructurePointer->lpPipeName));
  732. }
  733. #endif
  734. Ok = CallNamedPipe((LPSTR)READ_FAR_POINTER(&StructurePointer->lpPipeName),
  735. READ_FAR_POINTER(&StructurePointer->lpInBuffer),
  736. READ_WORD(&StructurePointer->nInBufferLen),
  737. READ_FAR_POINTER(&StructurePointer->lpOutBuffer),
  738. READ_WORD(&StructurePointer->nOutBufferLen),
  739. &BytesRead,
  740. READ_DWORD(&StructurePointer->Timeout)
  741. );
  742. if (!Ok) {
  743. SET_ERROR(VrpMapLastError());
  744. #if DBG
  745. IF_DEBUG(NAMEPIPE) {
  746. DbgPrint("VrCallNamedPipe: Error: CallNamedPipe returns %u\n", getAX());
  747. }
  748. #endif
  749. } else {
  750. WRITE_WORD(READ_FAR_POINTER(&StructurePointer->lpBytesRead), (WORD)BytesRead);
  751. setCX((WORD)BytesRead);
  752. setCF(0);
  753. #if DBG
  754. IF_DEBUG(NAMEPIPE) {
  755. DbgPrint("VrCallNamedPipe: Ok\n");
  756. }
  757. #endif
  758. }
  759. }
  760. VOID
  761. VrWaitNamedPipe(
  762. VOID
  763. )
  764. /*++
  765. Routine Description:
  766. Performs WaitNamedPipe request on behalf of VDM redir. We assume that the
  767. name we are getting is \\computer\pipe\name, anything else is invalid
  768. Arguments:
  769. Function = 5F38h
  770. ENTRY BX:CX = Timeout
  771. DS:DX = Pipe name
  772. EXIT CF = 1
  773. AX = Error code
  774. CF = 0
  775. No error
  776. Return Value:
  777. None. Returns values in VDM Ax and Flags registers
  778. --*/
  779. {
  780. BOOL Ok;
  781. //
  782. // BUGBUG - should really perform DosPathCanonicalization on input string -
  783. // DOS redir would convert eg //server/pipe\foo.bar into \\SERVER\PIPE\FOO.BAR
  784. // if it makes any difference
  785. //
  786. #if DBG
  787. IF_DEBUG(NAMEPIPE) {
  788. DbgPrint("VrWaitNamedPipe(%s, %d)\n",
  789. LPSTR_FROM_WORDS(getDS(), getDX()),
  790. DWORD_FROM_WORDS(getBX(), getCX())
  791. );
  792. }
  793. #endif
  794. Ok = WaitNamedPipe(LPSTR_FROM_WORDS(getDS(), getDX()),
  795. DWORD_FROM_WORDS(getBX(), getCX())
  796. );
  797. if (!Ok) {
  798. SET_ERROR(VrpMapLastError());
  799. } else {
  800. #if DBG
  801. IF_DEBUG(NAMEPIPE) {
  802. DbgPrint("WaitNamedPipe returns TRUE\n");
  803. }
  804. #endif
  805. setAX(0);
  806. setCF(0);
  807. }
  808. }
  809. VOID
  810. VrNetHandleGetInfo(
  811. VOID
  812. )
  813. /*++
  814. Routine Description:
  815. Performs local NetHandleGetInfo on behalf of the Vdm client
  816. Arguments:
  817. Function = 5F3Ch
  818. ENTRY BP:BX = 32-bit Named Pipe handle
  819. CX = Buffer length
  820. SI = Level (1)
  821. DS:DX = Buffer
  822. EXIT CX = size of required buffer (whether we got it or not)
  823. CF = 1
  824. AX = Error code
  825. CF = 0
  826. indicated stuff put in buffer
  827. Return Value:
  828. None. Results returned via VDM registers or in VDM memory, according to
  829. request
  830. --*/
  831. {
  832. HANDLE Handle;
  833. DWORD Level;
  834. DWORD BufLen;
  835. BOOL Ok;
  836. DWORD CollectCount;
  837. DWORD CollectTime;
  838. LPVDM_HANDLE_INFO_1 StructurePointer;
  839. #if DBG
  840. IF_DEBUG(NAMEPIPE) {
  841. DbgPrint("VrNetHandleGetInfo\n");
  842. }
  843. #endif
  844. Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  845. Level = (DWORD)getSI();
  846. if (Level == 1) {
  847. BufLen = (DWORD)getCX();
  848. if (BufLen >= sizeof(VDM_HANDLE_INFO_1)) {
  849. //
  850. // BUGBUG - the information we are interested in cannot be returned
  851. // if the client and server are on the same machine, or if this is
  852. // the server end of the pipe???
  853. //
  854. Ok = GetNamedPipeHandleState(Handle,
  855. NULL, // not interested in state
  856. NULL, // ditto curInstances
  857. &CollectCount,
  858. &CollectTime,
  859. NULL, // not interested in client app name
  860. 0
  861. );
  862. if (!Ok) {
  863. SET_ERROR(VrpMapLastError());
  864. } else {
  865. StructurePointer = (LPVDM_HANDLE_INFO_1)POINTER_FROM_WORDS(getDS(), getDX());
  866. StructurePointer->CharTime = CollectTime;
  867. StructurePointer->CharCount = (WORD)CollectCount;
  868. setCF(0);
  869. }
  870. } else {
  871. SET_ERROR(NERR_BufTooSmall);
  872. }
  873. } else {
  874. SET_ERROR(ERROR_INVALID_LEVEL);
  875. }
  876. }
  877. VOID
  878. VrNetHandleSetInfo(
  879. VOID
  880. )
  881. /*++
  882. Routine Description:
  883. Performs local NetHandleSetInfo on behalf of the Vdm client
  884. Arguments:
  885. Function = 5F3Bh
  886. ENTRY BP:BX = 32-bit Named Pipe handle
  887. CX = Buffer length
  888. SI = Level (1)
  889. DI = Parmnum
  890. DS:DX = Buffer
  891. EXIT CF = 1
  892. AX = Error code
  893. CF = 0
  894. Stuff from buffer set
  895. Return Value:
  896. None. Results returned via VDM registers or in VDM memory, according to
  897. request
  898. --*/
  899. {
  900. HANDLE Handle;
  901. DWORD Level;
  902. DWORD BufLen;
  903. BOOL Ok;
  904. DWORD Data;
  905. DWORD ParmNum;
  906. LPBYTE Buffer;
  907. #if DBG
  908. IF_DEBUG(NAMEPIPE) {
  909. DbgPrint("VrNetHandleGetInfo\n");
  910. DbgBreakPoint();
  911. }
  912. #endif
  913. Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  914. Level = (DWORD)getSI();
  915. Buffer = LPBYTE_FROM_WORDS(getDS(), getDX());
  916. if (Level == 1) {
  917. BufLen = (DWORD)getCX();
  918. //
  919. // ParmNum can be 1 (CharTime) or 2 (CharCount), Can't be 0 (set
  920. // everything)
  921. //
  922. ParmNum = (DWORD)getDI();
  923. if (!--ParmNum) {
  924. if (BufLen < sizeof(((LPVDM_HANDLE_INFO_1)0)->CharTime)) {
  925. SET_ERROR(NERR_BufTooSmall);
  926. return ;
  927. }
  928. Data = (DWORD)*(LPDWORD)Buffer;
  929. } else if (!--ParmNum) {
  930. if (BufLen < sizeof(((LPVDM_HANDLE_INFO_1)0)->CharCount)) {
  931. SET_ERROR(NERR_BufTooSmall);
  932. return ;
  933. }
  934. Data = (DWORD)*(LPWORD)Buffer;
  935. } else {
  936. SET_ERROR(ERROR_INVALID_PARAMETER);
  937. return ;
  938. }
  939. //
  940. // BUGBUG - the information we are interested in cannot be set
  941. // if the client and server are on the same machine, or if this is
  942. // the server end of the pipe???
  943. //
  944. Ok = SetNamedPipeHandleState(Handle,
  945. NULL, // not interested in mode
  946. (LPDWORD)((ParmNum == 1) ? &Data : NULL),
  947. (LPDWORD)((ParmNum == 2) ? &Data : NULL)
  948. );
  949. if (!Ok) {
  950. SET_ERROR(VrpMapLastError());
  951. } else {
  952. setCF(0);
  953. }
  954. } else {
  955. SET_ERROR(ERROR_INVALID_LEVEL);
  956. }
  957. }
  958. //
  959. // Request Queue. This queue holds a singly linked list of async named pipe
  960. // read/write requests. The async thread will search this list when an async
  961. // read or write completes (the event is signalled). It then sets up the
  962. // information for the call back to the VDM and dequeues the request info.
  963. // Because we can have the async thread and the request thread simultaneously
  964. // accessing the queue, it is protected by a critical section
  965. //
  966. CRITICAL_SECTION VrNmpRequestQueueCritSec;
  967. PDOS_ASYNC_NAMED_PIPE_INFO RequestQueueHead = NULL;
  968. PDOS_ASYNC_NAMED_PIPE_INFO RequestQueueTail = NULL;
  969. HANDLE VrpNmpSomethingToDo;
  970. VOID
  971. VrReadWriteAsyncNmPipe(
  972. VOID
  973. )
  974. /*++
  975. Routine Description:
  976. Performs asynchronous read or write of a message mode named pipe on behalf
  977. of the VDM DOS application
  978. Arguments:
  979. None. All arguments are extracted from DOS registers/memory.
  980. These calls are made through int 2fh/ax=function code, not int 21h/ah=5fh
  981. AX = 1186h DosReadAsyncNmPipe
  982. 118Fh DosWriteAsyncNmPipe
  983. 1190h DosReadAsyncNmPipe2
  984. 1191h DosWriteAsyncNmPipe2
  985. BP:BX = 32-bit Named Pipe Handle
  986. DS:SI = DOS_ASYNC_NAMED_PIPE_STRUCT
  987. DD address of returned bytes read
  988. DW size of caller's buffer
  989. DD address of caller's buffer
  990. DD address of returned error code
  991. DD address of Asynchronous Notification Routine
  992. DW named pipe handle
  993. DD address of caller's 'semaphore'
  994. Return Value:
  995. None.
  996. --*/
  997. {
  998. HANDLE Handle;
  999. //
  1000. // Type is type of request - read or write, standard or 2 (meaning the
  1001. // request has an associated 'semaphore' which must be cleared)
  1002. //
  1003. DWORD Type;
  1004. //
  1005. // StructurePointer is 32-bit flat pointer to structure in DOS memory
  1006. // containing request parameters
  1007. //
  1008. PDOS_ASYNC_NAMED_PIPE_STRUCT StructurePointer;
  1009. //
  1010. // pAsyncInfo is a pointer to the request packet we stick on the request
  1011. // queue
  1012. //
  1013. PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo;
  1014. //
  1015. // pipeInfo is a pointer to the information we created/stored when the
  1016. // named pipe was opened. We just need this to check the handle's valid
  1017. //
  1018. POPEN_NAMED_PIPE_INFO pipeInfo;
  1019. WORD length;
  1020. LPBYTE buffer;
  1021. DWORD error;
  1022. BOOL ok;
  1023. HANDLE hEvent;
  1024. DWORD bytesTransferred;
  1025. //
  1026. // hThread and tid: these must be kept alive so long as the async named
  1027. // pipe (completion) thread exists. tid can be used with ResumeThread and
  1028. // SuspendThread as we may see fit
  1029. //
  1030. static HANDLE hThread = NULL;
  1031. static DWORD tid;
  1032. //
  1033. // get info from registers and the async named pipe structure
  1034. //
  1035. Handle = HANDLE_FROM_WORDS(getBP(), getBX());
  1036. pipeInfo = VrpGetOpenNamedPipeInfo(Handle);
  1037. Type = (DWORD)getAX() & 0xff; // 0x86, 0x8f, 0x90 or 0x91
  1038. StructurePointer = (PDOS_ASYNC_NAMED_PIPE_STRUCT)POINTER_FROM_WORDS(getDS(), getSI());
  1039. length = READ_WORD(&StructurePointer->BufferLength);
  1040. buffer = READ_FAR_POINTER(&StructurePointer->lpBuffer);
  1041. #if DBG
  1042. IF_DEBUG(NAMEPIPE) {
  1043. DbgPrint( "\n"
  1044. "VrReadWriteAsyncNmPipe (%04x) [%s]:\n"
  1045. "DOS_READ_ASYNC_NAMED_PIPE_STRUCT @ %08x:\n"
  1046. "32-bit named pipe handle . . . . %08x\n"
  1047. "Address of returned bytes read . %04x:%04x\n"
  1048. "Size of caller's buffer. . . . . %04x\n"
  1049. "Address of caller's buffer . . . %04x:%04x\n"
  1050. "Address of returned error code . %04x:%04x\n"
  1051. "Address of ANR . . . . . . . . . %04x:%04x\n"
  1052. "Named pipe handle. . . . . . . . %04x\n"
  1053. "Address of caller's semaphore. . %04x:%04x\n"
  1054. "\n",
  1055. (DWORD)getAX(), // type of read/write request
  1056. Type == ANP_READ
  1057. ? "READ"
  1058. : Type == ANP_WRITE
  1059. ? "WRITE"
  1060. : Type == ANP_READ2
  1061. ? "READ2"
  1062. : Type == ANP_WRITE2
  1063. ? "WRITE2"
  1064. : "?????",
  1065. StructurePointer,
  1066. Handle,
  1067. (DWORD)GET_SELECTOR(&StructurePointer->lpBytesRead),
  1068. (DWORD)GET_OFFSET(&StructurePointer->lpBytesRead),
  1069. (DWORD)StructurePointer->BufferLength,
  1070. (DWORD)GET_SELECTOR(&StructurePointer->lpBuffer),
  1071. (DWORD)GET_OFFSET(&StructurePointer->lpBuffer),
  1072. (DWORD)GET_SELECTOR(&StructurePointer->lpErrorCode),
  1073. (DWORD)GET_OFFSET(&StructurePointer->lpErrorCode),
  1074. (DWORD)GET_SELECTOR(&StructurePointer->lpANR),
  1075. (DWORD)GET_OFFSET(&StructurePointer->lpANR),
  1076. (DWORD)StructurePointer->PipeHandle,
  1077. (DWORD)GET_SELECTOR(&StructurePointer->lpSemaphore),
  1078. (DWORD)GET_OFFSET(&StructurePointer->lpSemaphore)
  1079. );
  1080. }
  1081. #endif
  1082. //
  1083. // if we can't find this handle in our list of opened named pipes, return
  1084. // an error
  1085. //
  1086. if (!pipeInfo) {
  1087. #if DBG
  1088. IF_DEBUG(NAMEPIPE) {
  1089. DbgPrint("VrReadWriteAsyncNmPipe: Handle 0x%08x is invalid\n", Handle);
  1090. }
  1091. #endif
  1092. SET_ERROR(ERROR_INVALID_HANDLE);
  1093. return;
  1094. }
  1095. //
  1096. // looks like we're going to make an async read/write request. Create the
  1097. // async thread if it doesn't already exist. Create also the "something to
  1098. // do" event. Create this as an auto reset event which is initially in the
  1099. // not-signalled state
  1100. //
  1101. if (hThread == NULL) {
  1102. VrpNmpSomethingToDo = CreateEvent(NULL, FALSE, FALSE, NULL);
  1103. if (VrpNmpSomethingToDo == NULL) {
  1104. #if DBG
  1105. IF_DEBUG(NAMEPIPE) {
  1106. DbgPrint("VrReadWriteAsyncNmPipe: Error: Couldn't create something-to-do event: %d\n",
  1107. GetLastError()
  1108. );
  1109. }
  1110. #endif
  1111. //
  1112. // return an out-of-resources error
  1113. //
  1114. SET_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  1115. return;
  1116. }
  1117. //
  1118. // we have the "something to do" event. Now create the thread
  1119. //
  1120. hThread = CreateThread(NULL,
  1121. 0,
  1122. VrpAsyncNmPipeThread,
  1123. NULL,
  1124. 0,
  1125. &tid
  1126. );
  1127. if (hThread == NULL) {
  1128. #if DBG
  1129. IF_DEBUG(NAMEPIPE) {
  1130. DbgPrint("VrReadWriteAsyncNmPipe: Error: Couldn't create thread: %d\n",
  1131. GetLastError()
  1132. );
  1133. }
  1134. #endif
  1135. CloseHandle(VrpNmpSomethingToDo);
  1136. SET_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  1137. return;
  1138. }
  1139. }
  1140. //
  1141. // allocate a structure in which to store the information required to
  1142. // complete the request (in the VDM)
  1143. //
  1144. pAsyncInfo = (PDOS_ASYNC_NAMED_PIPE_INFO)LocalAlloc(LMEM_FIXED, sizeof(DOS_ASYNC_NAMED_PIPE_INFO));
  1145. if (pAsyncInfo == NULL) {
  1146. #if DBG
  1147. IF_DEBUG(NAMEPIPE) {
  1148. DbgPrint("VrReadWriteAsyncNmPipe: Error: Couldn't allocate structure\n");
  1149. }
  1150. #endif
  1151. SET_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  1152. return;
  1153. }
  1154. RtlZeroMemory(&pAsyncInfo->Overlapped, sizeof(pAsyncInfo->Overlapped));
  1155. //
  1156. // create a new event for this request - there can be multiple simultaneous
  1157. // requests per named pipe. The event is manual reset so that if the request
  1158. // completes before the WaitForMultipleObjects snaps the list, the event
  1159. // will stay reset and hence the wait will complete. If we created the event
  1160. // as auto-reset, it may get signalled, and go not-signalled before we wait
  1161. // on it, potentially causing an infinite wait
  1162. //
  1163. hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  1164. if (hEvent == NULL) {
  1165. #if DBG
  1166. IF_DEBUG(NAMEPIPE) {
  1167. DbgPrint("VrReadWriteAsyncNmPipe: Error: Couldn't create event: %d\n", GetLastError());
  1168. }
  1169. #endif
  1170. LocalFree((HLOCAL)pAsyncInfo);
  1171. //
  1172. // return approximation out-of-resources error
  1173. //
  1174. SET_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  1175. return;
  1176. } else {
  1177. pAsyncInfo->Overlapped.hEvent = hEvent;
  1178. }
  1179. //
  1180. // set up rest of async operation info structure
  1181. //
  1182. pAsyncInfo->Completed = FALSE;
  1183. pAsyncInfo->Handle = Handle;
  1184. pAsyncInfo->Buffer = (DWORD)StructurePointer->lpBuffer;
  1185. pAsyncInfo->pBytesTransferred = READ_FAR_POINTER(&StructurePointer->lpBytesRead);
  1186. pAsyncInfo->pErrorCode = READ_FAR_POINTER(&StructurePointer->lpErrorCode);
  1187. pAsyncInfo->ANR = READ_DWORD(&StructurePointer->lpANR);
  1188. //
  1189. // if this is an AsyncNmPipe2 call then it has an associated semaphore
  1190. // handle. Earlier versions don't have a semaphore
  1191. //
  1192. if (Type == ANP_READ2 || Type == ANP_WRITE2) {
  1193. pAsyncInfo->Type2 = TRUE;
  1194. pAsyncInfo->Semaphore = READ_DWORD(&StructurePointer->lpSemaphore);
  1195. } else {
  1196. pAsyncInfo->Type2 = FALSE;
  1197. pAsyncInfo->Semaphore = (DWORD)NULL;
  1198. }
  1199. #if DBG
  1200. pAsyncInfo->RequestType = Type;
  1201. #endif
  1202. //
  1203. // add the completion info structure to the async thread's work queue
  1204. //
  1205. VrpQueueAsyncRequest(pAsyncInfo);
  1206. //
  1207. // Q: what happens if the request completes asynchronously before we finish
  1208. // this routine?
  1209. //
  1210. if (Type == ANP_READ || Type == ANP_READ2) {
  1211. ok = ReadFile(Handle,
  1212. buffer,
  1213. length,
  1214. &bytesTransferred,
  1215. &pAsyncInfo->Overlapped
  1216. );
  1217. #if DBG
  1218. IF_DEBUG(NAMEPIPE) {
  1219. DbgPrint("VrReadWriteAsyncNmPipe: ReadFile(%x, %x, %d, ...): %d\n",
  1220. Handle,
  1221. buffer,
  1222. length,
  1223. ok
  1224. );
  1225. }
  1226. #endif
  1227. } else {
  1228. ok = WriteFile(Handle,
  1229. buffer,
  1230. length,
  1231. &bytesTransferred,
  1232. &pAsyncInfo->Overlapped
  1233. );
  1234. #if DBG
  1235. IF_DEBUG(NAMEPIPE) {
  1236. DbgPrint("VrReadWriteAsyncNmPipe: WriteFile(%x, %x, %d, ...): %d\n",
  1237. Handle,
  1238. buffer,
  1239. length,
  1240. ok
  1241. );
  1242. }
  1243. #endif
  1244. }
  1245. error = ok ? NO_ERROR : GetLastError();
  1246. //
  1247. // if we get ERROR_MORE_DATA then treat it as an error. GetOverlappedResult
  1248. // will give us the same error which we will return asynchronously
  1249. //
  1250. if (error != NO_ERROR && error != ERROR_IO_PENDING && error != ERROR_MORE_DATA) {
  1251. //
  1252. // we didn't get to start the I/O operation successfully, therefore
  1253. // we won't get called back, so we dequeue and free the completion
  1254. // structure and return the error
  1255. //
  1256. #if DBG
  1257. IF_DEBUG(NAMEPIPE) {
  1258. DbgPrint("VrReadWriteAsyncNmPipe: Error: IO operation returns %d\n", error);
  1259. }
  1260. #endif
  1261. VrpDequeueAsyncRequest(pAsyncInfo);
  1262. CloseHandle(hEvent);
  1263. LocalFree(pAsyncInfo);
  1264. SET_ERROR((WORD)error);
  1265. } else {
  1266. #if DBG
  1267. IF_DEBUG(NAMEPIPE) {
  1268. DbgPrint("VrReadWriteAsyncNmPipe: IO operation started: returns %s\n",
  1269. error == ERROR_IO_PENDING ? "ERROR_IO_PENDING" : "NO_ERROR"
  1270. );
  1271. }
  1272. #endif
  1273. setCF(0);
  1274. }
  1275. #if DBG
  1276. IF_DEBUG(NAMEPIPE) {
  1277. DbgPrint("VrReadWriteAsyncNmPipe: returning CF=%d, AX=%d\n", getCF(), getAX());
  1278. }
  1279. #endif
  1280. }
  1281. BOOLEAN
  1282. VrNmPipeInterrupt(
  1283. VOID
  1284. )
  1285. /*++
  1286. Routine Description:
  1287. Called from hardware interrupt BOP processing to check if there are any
  1288. async named pipe ANRs to call
  1289. Arguments:
  1290. None.
  1291. Return Value:
  1292. BOOLEAN
  1293. TRUE - there was an async named pipe operation to complete. The
  1294. VDM registers & data areas have been modified to indicate
  1295. that the named pipe ANR must be called
  1296. FALSE - no async named pipe processing to do. Interrupt must have
  1297. been generated by NetBios or DLC
  1298. --*/
  1299. {
  1300. PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo;
  1301. #if DBG
  1302. IF_DEBUG(NAMEPIPE) {
  1303. DbgPrint("VrNmPipeInterrupt\n");
  1304. }
  1305. #endif
  1306. //
  1307. // locate the first async named pipe request packet that has completed and
  1308. // is waiting for interrupt processing (ie its ANR to be called)
  1309. //
  1310. pAsyncInfo = VrpFindCompletedRequest();
  1311. if (!pAsyncInfo) {
  1312. #if DBG
  1313. IF_DEBUG(NAMEPIPE) {
  1314. DbgPrint("VrNmPipeInterrupt - nothing to do\n");
  1315. }
  1316. #endif
  1317. //
  1318. // returning FALSE indicates that the hardware interrupt callback was
  1319. // not generated by async named pipe request completing
  1320. //
  1321. return FALSE;
  1322. } else {
  1323. //
  1324. // set the VDM registers to indicate a named pipe callback
  1325. //
  1326. setDS(HIWORD(pAsyncInfo->Buffer));
  1327. setSI(LOWORD(pAsyncInfo->Buffer));
  1328. setES(HIWORD(pAsyncInfo->Semaphore));
  1329. setDI(LOWORD(pAsyncInfo->Semaphore));
  1330. setCX(HIWORD(pAsyncInfo->ANR));
  1331. setBX(LOWORD(pAsyncInfo->ANR));
  1332. setAL((BYTE)pAsyncInfo->Type2);
  1333. SET_CALLBACK_NAMEPIPE();
  1334. //
  1335. // finished with this request packet, so dequeue and deallocate it
  1336. //
  1337. VrpDequeueAsyncRequest(pAsyncInfo);
  1338. CloseHandle(pAsyncInfo->Overlapped.hEvent);
  1339. LocalFree(pAsyncInfo);
  1340. #if DBG
  1341. IF_DEBUG(NAMEPIPE) {
  1342. DbgPrint("VrNmPipeInterrupt: Setting DOS Registers:\n"
  1343. "DS:SI=%04x:%04x, ES:DI=%04x:%04x, CX:BX=%04x:%04x, AL=%02x\n",
  1344. getDS(), getSI(),
  1345. getES(), getDI(),
  1346. getCX(), getBX(),
  1347. getAL()
  1348. );
  1349. }
  1350. #endif
  1351. //
  1352. // returning TRUE indicates that we have accepted a named pipe
  1353. // completion request
  1354. //
  1355. //VrDismissInterrupt();
  1356. return TRUE;
  1357. }
  1358. }
  1359. VOID
  1360. VrTerminateNamedPipes(
  1361. IN WORD DosPdb
  1362. )
  1363. /*++
  1364. Routine Description:
  1365. Cleans out all open named pipe info and pending async named pipe requests
  1366. when the owning DOS app terminates
  1367. Arguments:
  1368. DosPdb - PDB (DOS 'process' identifier) of terminating DOS process
  1369. Return Value:
  1370. None.
  1371. --*/
  1372. {
  1373. #if DBG
  1374. IF_DEBUG(NAMEPIPE) {
  1375. DbgPrint("VrTerminateNamedPipes\n");
  1376. }
  1377. #endif
  1378. }
  1379. VOID
  1380. VrCancelPipeIo(
  1381. IN DWORD Thread
  1382. )
  1383. /*++
  1384. Routine Description:
  1385. For all pending named pipe I/Os owned by Thread, mark them as cancelled
  1386. and signal the event in the OVERLAPPED structure, causing the wait to
  1387. terminate.
  1388. This thread may not have any outstanding named pipe I/O
  1389. Arguments:
  1390. Thread - pseudo-handle of thread owning named pipe I/O
  1391. Return Value:
  1392. None.
  1393. --*/
  1394. {
  1395. POVERLAPPED_PIPE_IO ptr;
  1396. EnterCriticalSection(&VrNamedPipeCancelCritSec);
  1397. for (ptr = PipeIoQueue; ptr; ptr = ptr->Next) {
  1398. if (ptr->Thread == Thread) {
  1399. ptr->Cancelled = TRUE;
  1400. SetEvent(ptr->Overlapped.hEvent);
  1401. }
  1402. }
  1403. LeaveCriticalSection(&VrNamedPipeCancelCritSec);
  1404. }
  1405. #if _MSC_FULL_VER >= 13008827
  1406. #pragma warning(push)
  1407. #pragma warning(disable:4715) // Not all control paths return (due to infinite loop)
  1408. #endif
  1409. PRIVATE
  1410. DWORD
  1411. VrpAsyncNmPipeThread(
  1412. IN LPVOID Parameters
  1413. )
  1414. /*++
  1415. Routine Description:
  1416. Waits for an asynchronous named pipe read or write operation to complete.
  1417. Loops forever, waiting on list of pending async (overlapped) named pipe
  1418. operations. If there are no more outstanding named pipe read/writes then
  1419. waits on VrpNmpSomethingToDo which is reset (put in not-signalled state)
  1420. when there are no packets left on the request queue
  1421. Arguments:
  1422. Parameters - unused parameter block
  1423. Return Value:
  1424. DWORD
  1425. 0
  1426. --*/
  1427. {
  1428. DWORD numberOfHandles;
  1429. DWORD index;
  1430. HANDLE eventList[MAXIMUM_ASYNC_PIPES + 1];
  1431. PDOS_ASYNC_NAMED_PIPE_INFO pRequest;
  1432. UNREFERENCED_PARAMETER(Parameters);
  1433. #if DBG
  1434. IF_DEBUG(NAMEPIPE) {
  1435. DbgPrint("VrAsyncNamedPipeThread: *** Started ***\n");
  1436. }
  1437. #endif
  1438. while (TRUE) {
  1439. //
  1440. // create an array of event handles. The first handle in the array is
  1441. // the "something to do" event. This will only be reset when the queue
  1442. // of requests changes from the empty set
  1443. //
  1444. numberOfHandles = VrpSnapshotEventList(eventList);
  1445. index = WaitForMultipleObjects(numberOfHandles, eventList, FALSE, INFINITE);
  1446. //
  1447. // if the index is 0, then the "something to do" event has been signalled,
  1448. // meaning that we have to snapshot a new event list and re-wait
  1449. //
  1450. if (index > 0 && index < numberOfHandles) {
  1451. #if DBG
  1452. IF_DEBUG(NAMEPIPE) {
  1453. DbgPrint("VrpAsyncNmPipeThread: event #%d fired\n", index);
  1454. }
  1455. #endif
  1456. pRequest = VrpSearchForRequestByEventHandle(eventList[index]);
  1457. if (pRequest != NULL) {
  1458. VrpCompleteAsyncRequest(pRequest);
  1459. }
  1460. #if DBG
  1461. else {
  1462. IF_DEBUG(NAMEPIPE) {
  1463. DbgPrint("VrpAsyncNmPipeThread: Couldn't find request for handle 0x%08x\n",
  1464. eventList[index]
  1465. );
  1466. }
  1467. }
  1468. #endif
  1469. } else if (index) {
  1470. //
  1471. // an error occurred
  1472. //
  1473. #if DBG
  1474. IF_DEBUG(NAMEPIPE) {
  1475. DbgPrint("VrpAsyncNmPipeThread: Error: WaitForMultipleObjects returns %d (%d)\n",
  1476. index,
  1477. GetLastError()
  1478. );
  1479. }
  1480. #endif
  1481. }
  1482. #if DBG
  1483. else {
  1484. IF_DEBUG(NAMEPIPE) {
  1485. DbgPrint("VrpAsyncNmPipeThread: Something-to-do event fired\n");
  1486. }
  1487. }
  1488. #endif
  1489. }
  1490. #if DBG
  1491. IF_DEBUG(NAMEPIPE) {
  1492. DbgPrint("VrpAsyncNmPipeThread: *** Terminated ***\n");
  1493. }
  1494. #endif
  1495. return 0; // appease the compiler-god
  1496. }
  1497. #if _MSC_FULL_VER >= 13008827
  1498. #pragma warning(pop)
  1499. #endif
  1500. PRIVATE
  1501. DWORD
  1502. VrpSnapshotEventList(
  1503. OUT LPHANDLE pList
  1504. )
  1505. /*++
  1506. Routine Description:
  1507. Builds an array of event handles for those asynchronous named pipe I/O
  1508. requests which are still not completed (the Completed flag is FALSE).
  1509. The first event handle is always the "something to do" event
  1510. Arguments:
  1511. pList - pointer to callers list to build
  1512. Return Value:
  1513. DWORD
  1514. --*/
  1515. {
  1516. DWORD count = 1;
  1517. PDOS_ASYNC_NAMED_PIPE_INFO ptr;
  1518. #if DBG
  1519. IF_DEBUG(NAMEPIPE) {
  1520. DbgPrint("VrpSnapshotEventList\n");
  1521. }
  1522. #endif
  1523. pList[0] = VrpNmpSomethingToDo;
  1524. EnterCriticalSection(&VrNmpRequestQueueCritSec);
  1525. for (ptr = RequestQueueHead; ptr; ptr = ptr->Next) {
  1526. if (ptr->Completed == FALSE) {
  1527. pList[count++] = ptr->Overlapped.hEvent;
  1528. }
  1529. }
  1530. LeaveCriticalSection(&VrNmpRequestQueueCritSec);
  1531. #if DBG
  1532. IF_DEBUG(NAMEPIPE) {
  1533. DbgPrint("VrpSnapshotEventList: returning %d events\n", count);
  1534. }
  1535. #endif
  1536. return count;
  1537. }
  1538. PRIVATE
  1539. PDOS_ASYNC_NAMED_PIPE_INFO
  1540. VrpSearchForRequestByEventHandle(
  1541. IN HANDLE EventHandle
  1542. )
  1543. /*++
  1544. Routine Description:
  1545. Searches the async request queue for the structure with this event handle.
  1546. If the structure is found, it is marked as Completed. The required structure
  1547. may NOT be located: this might occur when an item was removed from the list
  1548. due to an error in VrReadWriteAsyncNmPipe
  1549. Arguments:
  1550. EventHandle - to search for
  1551. Return Value:
  1552. PDOS_ASYNC_NAMED_PIPE_INFO
  1553. Success - the located structure
  1554. Failure - NULL
  1555. --*/
  1556. {
  1557. PDOS_ASYNC_NAMED_PIPE_INFO ptr;
  1558. #if DBG
  1559. IF_DEBUG(NAMEPIPE) {
  1560. DbgPrint("VrpSearchForRequestByEventHandle(0x%08x)\n", EventHandle);
  1561. }
  1562. #endif
  1563. EnterCriticalSection(&VrNmpRequestQueueCritSec);
  1564. for (ptr = RequestQueueHead; ptr; ptr = ptr->Next) {
  1565. if (ptr->Overlapped.hEvent == EventHandle) {
  1566. ptr->Completed = TRUE;
  1567. break;
  1568. }
  1569. }
  1570. LeaveCriticalSection(&VrNmpRequestQueueCritSec);
  1571. #if DBG
  1572. IF_DEBUG(NAMEPIPE) {
  1573. DbgPrint("VrpLocateAsyncRequestByEventHandle returning 0x%08x: Request is %s\n",
  1574. ptr,
  1575. !ptr
  1576. ? "NO REQUEST!!"
  1577. : ptr->RequestType == ANP_READ
  1578. ? "READ"
  1579. : ptr->RequestType == ANP_WRITE
  1580. ? "WRITE"
  1581. : ptr->RequestType == ANP_READ2
  1582. ? "READ2"
  1583. : ptr->RequestType == ANP_WRITE2
  1584. ? "WRITE2"
  1585. : "UNKNOWN REQUEST!!"
  1586. );
  1587. }
  1588. #endif
  1589. return ptr;
  1590. }
  1591. PRIVATE
  1592. VOID
  1593. VrpCompleteAsyncRequest(
  1594. IN PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo
  1595. )
  1596. /*++
  1597. Routine Description:
  1598. Completes the asynchronous named pipe I/O request by getting the results
  1599. of the transfer and filling-in the error & bytes transferred fields of
  1600. the async named pipe info structure. If there is an ANR to be called, then
  1601. a simulated hardware interrupt request is generated to the VDM. If there
  1602. is no ANR to call then the async named pipe info structure is cleared out.
  1603. If there is an ANR, the request will be completed finally when it is
  1604. dequeued & freed by VrNmPipeInterrupt
  1605. Arguments:
  1606. pAsyncInfo - pointer to DOS_ASYNC_NAMED_PIPE_INFO structure to complete
  1607. Return Value:
  1608. None.
  1609. --*/
  1610. {
  1611. BOOL ok;
  1612. DWORD bytesTransferred=0;
  1613. DWORD error;
  1614. #if DBG
  1615. IF_DEBUG(NAMEPIPE) {
  1616. DbgPrint("VrpCompleteAsyncRequest(0x%08x)\n", pAsyncInfo);
  1617. }
  1618. #endif
  1619. ok = GetOverlappedResult(pAsyncInfo->Handle,
  1620. &pAsyncInfo->Overlapped,
  1621. &bytesTransferred,
  1622. FALSE
  1623. );
  1624. error = ok ? NO_ERROR : GetLastError();
  1625. //
  1626. // update the VDM variables
  1627. //
  1628. WRITE_WORD(pAsyncInfo->pErrorCode, error);
  1629. WRITE_WORD(pAsyncInfo->pBytesTransferred, bytesTransferred);
  1630. #if DBG
  1631. IF_DEBUG(NAMEPIPE) {
  1632. DbgPrint("VrpCompleteAsyncRequest: error=%d, bytesTransferred=%d\n",
  1633. error,
  1634. bytesTransferred
  1635. );
  1636. }
  1637. #endif
  1638. //
  1639. // if there is no ANR then we cannot make a call-back to DOS (error? DOS
  1640. // app polls 'semaphore'?) so close the event handle, dequeue the request
  1641. // packet and free it
  1642. //
  1643. if (!pAsyncInfo->ANR) {
  1644. #if DBG
  1645. PDOS_ASYNC_NAMED_PIPE_INFO ptr;
  1646. ptr = VrpDequeueAsyncRequest(pAsyncInfo);
  1647. if (ptr != pAsyncInfo) {
  1648. DbgPrint("*** Error: incorrect request packet dequeued ***\n");
  1649. DbgBreakPoint();
  1650. }
  1651. #else
  1652. VrpDequeueAsyncRequest(pAsyncInfo);
  1653. #endif
  1654. CloseHandle(pAsyncInfo->Overlapped.hEvent);
  1655. LocalFree(pAsyncInfo);
  1656. } else {
  1657. //
  1658. // interrupt the VDM. It must call back to find out what asynchronous
  1659. // processing there is to do
  1660. //
  1661. #if DBG
  1662. IF_DEBUG(NAMEPIPE) {
  1663. DbgPrint("VrpCompleteAsyncRequest: *** INTERRUPTING VDM ***\n");
  1664. }
  1665. #endif
  1666. VrQueueCompletionHandler(VrNmPipeInterrupt);
  1667. VrRaiseInterrupt();
  1668. }
  1669. }
  1670. PRIVATE
  1671. VOID
  1672. VrpQueueAsyncRequest(
  1673. IN PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo
  1674. )
  1675. /*++
  1676. Routine Description:
  1677. Adds a DOS_ASYNC_NAMED_PIPE_INFO structure to the end of the request queue.
  1678. The queue is protected by a critical section
  1679. Arguments:
  1680. pAsyncInfo - pointer to structure to add
  1681. Return Value:
  1682. None.
  1683. --*/
  1684. {
  1685. #if DBG
  1686. IF_DEBUG(NAMEPIPE) {
  1687. DbgPrint("VrpQueueAsyncRequest\n");
  1688. }
  1689. #endif
  1690. EnterCriticalSection(&VrNmpRequestQueueCritSec);
  1691. if (!RequestQueueHead) {
  1692. RequestQueueHead = pAsyncInfo;
  1693. //
  1694. // the set is changing state from empty set to not empty set. Set the
  1695. // "something to do" event. Note: it is OK to do this here (before we
  1696. // have finished updating the queue info): because the async thread
  1697. // must gain this critical section before it can access the request
  1698. // queue
  1699. //
  1700. // PulseEvent(VrpNmpSomethingToDo);
  1701. } else {
  1702. RequestQueueTail->Next = pAsyncInfo;
  1703. }
  1704. pAsyncInfo->Next = NULL;
  1705. RequestQueueTail = pAsyncInfo;
  1706. SetEvent(VrpNmpSomethingToDo);
  1707. LeaveCriticalSection(&VrNmpRequestQueueCritSec);
  1708. }
  1709. PRIVATE
  1710. PDOS_ASYNC_NAMED_PIPE_INFO
  1711. VrpDequeueAsyncRequest(
  1712. IN PDOS_ASYNC_NAMED_PIPE_INFO pAsyncInfo
  1713. )
  1714. /*++
  1715. Routine Description:
  1716. Removes the DOS_ASYNC_NAMED_PIPE_INFO structure pointed at by pAsyncInfo
  1717. from the request queue. Protected by critical section
  1718. Arguments:
  1719. pAsyncInfo - pointer to DOS_ASYNC_NAMED_PIPE_INFO to remove
  1720. Return Value:
  1721. PDOS_ASYNC_NAMED_PIPE_INFO
  1722. Success - pAsyncInfo is returned
  1723. Failure - NULL - AsyncInfo wasn't found on the queue
  1724. --*/
  1725. {
  1726. PDOS_ASYNC_NAMED_PIPE_INFO ptr;
  1727. PDOS_ASYNC_NAMED_PIPE_INFO prev = (PDOS_ASYNC_NAMED_PIPE_INFO)&RequestQueueHead;
  1728. #if DBG
  1729. IF_DEBUG(NAMEPIPE) {
  1730. DbgPrint("VrpDequeueAsyncRequest(0x%08x)\n", pAsyncInfo);
  1731. }
  1732. #endif
  1733. EnterCriticalSection(&VrNmpRequestQueueCritSec);
  1734. for (ptr = RequestQueueHead; ptr; ptr = ptr->Next) {
  1735. if (ptr == pAsyncInfo) {
  1736. break;
  1737. } else {
  1738. prev = ptr;
  1739. }
  1740. }
  1741. if (ptr) {
  1742. prev->Next = ptr->Next;
  1743. if (RequestQueueTail == ptr) {
  1744. RequestQueueTail = prev;
  1745. }
  1746. }
  1747. //
  1748. // if this was the last item on the queue (in the set) then the set has
  1749. // changed state from not empty to empty set. Reset the "something to do"
  1750. // event to stop the async thread until another request arrives. Note: it
  1751. // is safe to reset the event here
  1752. //
  1753. // ResetEvent(VrpNmpSomethingToDo);
  1754. LeaveCriticalSection(&VrNmpRequestQueueCritSec);
  1755. #if DBG
  1756. IF_DEBUG(NAMEPIPE) {
  1757. DbgPrint("VrpDequeueAsyncRequest returning %08x\n", ptr);
  1758. }
  1759. #endif
  1760. return ptr;
  1761. }
  1762. PRIVATE
  1763. PDOS_ASYNC_NAMED_PIPE_INFO
  1764. VrpFindCompletedRequest(
  1765. VOID
  1766. )
  1767. /*++
  1768. Routine Description:
  1769. Tries to locate the first request packet in the queue with the Completed
  1770. field set, meaning the I/O request has completed and is waiting to generate
  1771. a callback
  1772. Arguments:
  1773. None.
  1774. Return Value:
  1775. PDOS_ASYNC_NAMED_PIPE_INFO
  1776. Success - pointer to request packet to complete
  1777. Failure - NULL
  1778. --*/
  1779. {
  1780. PDOS_ASYNC_NAMED_PIPE_INFO ptr;
  1781. #if DBG
  1782. IF_DEBUG(NAMEPIPE) {
  1783. DbgPrint("VrpFindCompletedRequest\n");
  1784. }
  1785. #endif
  1786. EnterCriticalSection(&VrNmpRequestQueueCritSec);
  1787. for (ptr = RequestQueueHead; ptr; ptr = ptr->Next) {
  1788. if (ptr->Completed) {
  1789. break;
  1790. }
  1791. }
  1792. LeaveCriticalSection(&VrNmpRequestQueueCritSec);
  1793. #if DBG
  1794. IF_DEBUG(NAMEPIPE) {
  1795. DbgPrint("VrpFindCompletedRequest returning 0x%08x: Request is %s\n",
  1796. ptr,
  1797. !ptr
  1798. ? "NO REQUEST!!"
  1799. : ptr->RequestType == ANP_READ
  1800. ? "READ"
  1801. : ptr->RequestType == ANP_WRITE
  1802. ? "WRITE"
  1803. : ptr->RequestType == ANP_READ2
  1804. ? "READ2"
  1805. : ptr->RequestType == ANP_WRITE2
  1806. ? "WRITE2"
  1807. : "UNKNOWN REQUEST!!"
  1808. );
  1809. }
  1810. #endif
  1811. return ptr;
  1812. }
  1813. //
  1814. // externally callable interceptors
  1815. //
  1816. BOOL
  1817. VrAddOpenNamedPipeInfo(
  1818. IN HANDLE Handle,
  1819. IN LPSTR lpFileName
  1820. )
  1821. /*++
  1822. Routine Description:
  1823. This routine is called whenever DEM (Dos Emulator) successfully opens a
  1824. handle to a file. We check if the file just opened was a named pipe (based
  1825. on the name) and if so create an association between name and handle
  1826. Arguments:
  1827. Handle - of just opened file/pipe/device
  1828. lpFileName - symbolic name of what was just opened
  1829. Return Value:
  1830. BOOL
  1831. TRUE - created/added open named pipe structure
  1832. FALSE - couldn't allocate structure memory or create event
  1833. --*/
  1834. {
  1835. BOOL ok;
  1836. #if DBG
  1837. IF_DEBUG(NAMEPIPE) {
  1838. DbgPrint("VrAddOpenNamedPipeInfo\n");
  1839. }
  1840. #endif
  1841. if (VrIsNamedPipeName(lpFileName)) {
  1842. #if DBG
  1843. IF_DEBUG(NAMEPIPE) {
  1844. DbgPrint("Adding %s as named pipe\n", lpFileName);
  1845. }
  1846. #endif
  1847. //
  1848. // if we can't create the named pipe info structure, or the async
  1849. // read/write event, return FALSE which results in an out-of-resources
  1850. // error (not enough memory) since DOS doesn't understand about events
  1851. //
  1852. ok = VrpAddOpenNamedPipeInfo(Handle, lpFileName);
  1853. } else {
  1854. #if DBG
  1855. IF_DEBUG(NAMEPIPE) {
  1856. DbgPrint("VrAddOpenNamedPipeInfo: Error: not named pipe: %s\n", lpFileName);
  1857. }
  1858. #endif
  1859. ok = FALSE;
  1860. }
  1861. return ok;
  1862. }
  1863. BOOL
  1864. VrRemoveOpenNamedPipeInfo(
  1865. IN HANDLE Handle
  1866. )
  1867. /*++
  1868. Routine Description:
  1869. This is the companion routine to VrAddOpenNamedPipeInfo. When a handle is
  1870. successfully closed for a DOS app, we must check if it referenced a named
  1871. pipe, and if so remove the info structure we created when the pipe was
  1872. opened
  1873. Arguments:
  1874. Handle - to file/pipe/device just closed for Dos app
  1875. Return Value:
  1876. BOOL
  1877. TRUE
  1878. FALSE
  1879. --*/
  1880. {
  1881. #if DBG
  1882. IF_DEBUG(NAMEPIPE) {
  1883. DbgPrint("VrRemoveOpenNamedPipeInfo\n");
  1884. }
  1885. if (!VrpRemoveOpenNamedPipeInfo(Handle)) {
  1886. IF_DEBUG(NAMEPIPE) {
  1887. DbgPrint("Handle 0x%08x is not a named pipe\n", Handle);
  1888. }
  1889. return FALSE;
  1890. } else {
  1891. IF_DEBUG(NAMEPIPE) {
  1892. DbgPrint("VrRemoveOpenNamedPipeInfo - Handle 0x%08x has been removed\n", Handle);
  1893. }
  1894. return TRUE;
  1895. }
  1896. #else
  1897. VrpRemoveOpenNamedPipeInfo(Handle);
  1898. #endif
  1899. return TRUE;
  1900. }
  1901. BOOL
  1902. VrReadNamedPipe(
  1903. IN HANDLE Handle,
  1904. IN LPBYTE Buffer,
  1905. IN DWORD Buflen,
  1906. OUT LPDWORD BytesRead,
  1907. OUT LPDWORD Error
  1908. )
  1909. /*++
  1910. Routine Description:
  1911. Performs ReadFile on a named pipe handle. All named pipes are opened in
  1912. overlapped-IO mode because async read/writes cannot be performed otherwise
  1913. Arguments:
  1914. Handle - of opened NamedPipe
  1915. Buffer - client (VDM) data buffer
  1916. Buflen - size of read buffer
  1917. BytesRead - where actual bytes read is returned
  1918. Error - pointer to returned error in case of failure or more data
  1919. Return Value:
  1920. BOOL
  1921. TRUE - handle was successfully written
  1922. FALSE - an error occurred, use GetLastError
  1923. --*/
  1924. {
  1925. OVERLAPPED_PIPE_IO pipeio;
  1926. BOOL success;
  1927. DWORD error;
  1928. DWORD dwBytesRead = 0;
  1929. BOOL bWaited = FALSE;
  1930. #if DBG
  1931. IF_DEBUG(NAMEPIPE) {
  1932. DbgPrint("VrReadNamePipe(0x%08x, %x, %d)\n", Handle, Buffer, Buflen);
  1933. }
  1934. #endif
  1935. //
  1936. // create an event to wait on. This goes in the overlapped structure - it
  1937. // is the only thing in the overlapped structure we are interested in.
  1938. // Create the event with manual reset. This is so that if the I/O operation
  1939. // completes immediately, we don't wait on the event. If we create the
  1940. // event as auto-reset, it can go into the signalled state, and back to the
  1941. // not-signalled state before we prime the wait, causing us to wait forever
  1942. // for an event that has already occurred
  1943. //
  1944. RtlZeroMemory(&pipeio, sizeof(pipeio));
  1945. if ((pipeio.Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
  1946. *Error = ERROR_NOT_ENOUGH_MEMORY; // really want out-of-resources (71?)
  1947. return FALSE;
  1948. }
  1949. //
  1950. // event handle created ok
  1951. //
  1952. RememberPipeIo(&pipeio);
  1953. success = ReadFile(Handle, Buffer, Buflen, BytesRead, &pipeio.Overlapped);
  1954. if (!success) {
  1955. error = GetLastError();
  1956. if (error == ERROR_IO_PENDING) {
  1957. error = WaitForSingleObject(pipeio.Overlapped.hEvent, NAMED_PIPE_TIMEOUT);
  1958. bWaited = TRUE;
  1959. if (error == 0xffffffff) {
  1960. error = GetLastError();
  1961. } else {
  1962. success = (error == WAIT_OBJECT_0);
  1963. }
  1964. } else {
  1965. #if DBG
  1966. IF_DEBUG(NAMEPIPE) {
  1967. DbgPrint("VrReadNamedPipe: ReadFile failed: %d\n", GetLastError());
  1968. }
  1969. #endif
  1970. //
  1971. // if we got ERROR_MORE_DATA, then this is actually success(!). In this case
  1972. // we don't want to SetLastError, but we do want to set the extended error
  1973. // info in DOS data segment. This is done by demRead
  1974. //
  1975. if (error == ERROR_MORE_DATA) {
  1976. success = TRUE;
  1977. }
  1978. }
  1979. } else {
  1980. error = NO_ERROR;
  1981. dwBytesRead = *BytesRead;
  1982. }
  1983. ForgetPipeIo(&pipeio);
  1984. if (pipeio.Cancelled) {
  1985. error = WAIT_TIMEOUT;
  1986. success = FALSE;
  1987. }
  1988. if (success && bWaited) {
  1989. //
  1990. // get the real bytes read. If GetOverlappedResult returns FALSE,
  1991. // check for ERROR_MORE_DATA
  1992. //
  1993. success = GetOverlappedResult(Handle, &pipeio.Overlapped, &dwBytesRead, FALSE);
  1994. error = success ? NO_ERROR : GetLastError();
  1995. //
  1996. // if we got ERROR_MORE_DATA, then this is actually success(!). In this case
  1997. // we don't want to SetLastError, but we do want to set the extended error
  1998. // info in DOS data segment. This is done by demRead
  1999. //
  2000. if (error == ERROR_MORE_DATA) {
  2001. success = TRUE;
  2002. }
  2003. } else if (error == WAIT_TIMEOUT) {
  2004. CloseHandle(Handle);
  2005. VrpRemoveOpenNamedPipeInfo(Handle);
  2006. }
  2007. CloseHandle(pipeio.Overlapped.hEvent);
  2008. //
  2009. // if no bytes were read and success was returned then treat this as an
  2010. // error - this is what the DOS Redir does
  2011. //
  2012. if (error == NO_ERROR && dwBytesRead == 0) {
  2013. error = ERROR_NO_DATA;
  2014. success = FALSE;
  2015. }
  2016. if (!success) {
  2017. #if DBG
  2018. IF_DEBUG(NAMEPIPE) {
  2019. DbgPrint("VrReadNamePipe: Error: Returning %d\n", error);
  2020. }
  2021. #endif
  2022. SetLastError(error);
  2023. } else {
  2024. *BytesRead = dwBytesRead;
  2025. #if DBG
  2026. IF_DEBUG(NAMEPIPE) {
  2027. DbgPrint("VrReadNamePipe: Ok: %d bytes read from pipe\n", *BytesRead);
  2028. }
  2029. #endif
  2030. }
  2031. //
  2032. // set the error code so that we can set the extended error code info
  2033. // from demRead and return the success/failure indication
  2034. //
  2035. *Error = error;
  2036. return success;
  2037. }
  2038. BOOL
  2039. VrWriteNamedPipe(
  2040. IN HANDLE Handle,
  2041. IN LPBYTE Buffer,
  2042. IN DWORD Buflen,
  2043. OUT LPDWORD BytesWritten
  2044. )
  2045. /*++
  2046. Routine Description:
  2047. Performs WriteFile on a named pipe handle. All named pipes are opened in
  2048. overlapped-IO mode because async read/writes cannot be performed otherwise
  2049. Arguments:
  2050. Handle - of opened NamedPipe
  2051. Buffer - client (VDM) data buffer
  2052. Buflen - size of write
  2053. BytesWritten - where actual bytes written is returned
  2054. Return Value:
  2055. BOOL
  2056. TRUE - handle was successfully written
  2057. FALSE - an error occurred, use GetLastError
  2058. --*/
  2059. {
  2060. OVERLAPPED_PIPE_IO pipeio;
  2061. BOOL success;
  2062. DWORD error;
  2063. #if DBG
  2064. IF_DEBUG(NAMEPIPE) {
  2065. DbgPrint("VrWriteNamePipe(0x%08x, %x, %d)\n", Handle, Buffer, Buflen);
  2066. }
  2067. #endif
  2068. //
  2069. // create an event to wait on. This goes in the overlapped structure - it
  2070. // is the only thing in the overlapped structure we are interested in.
  2071. // Create the event with manual reset. This is so that if the I/O operation
  2072. // completes immediately, we don't wait on the event. If we create the
  2073. // event as auto-reset, it can go into the signalled state, and back to the
  2074. // not-signalled state before we prime the wait, causing us to wait forever
  2075. // for an event that has already occurred
  2076. //
  2077. RtlZeroMemory(&pipeio, sizeof(pipeio));
  2078. if ((pipeio.Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
  2079. error = ERROR_NOT_ENOUGH_MEMORY;
  2080. success = FALSE;
  2081. } else {
  2082. RememberPipeIo(&pipeio);
  2083. success = WriteFile(Handle, Buffer, Buflen, BytesWritten, &pipeio.Overlapped);
  2084. error = success ? NO_ERROR : GetLastError();
  2085. if (error == ERROR_IO_PENDING) {
  2086. error = WaitForSingleObject(pipeio.Overlapped.hEvent, NAMED_PIPE_TIMEOUT);
  2087. if (error == 0xffffffff) {
  2088. error = GetLastError();
  2089. } else {
  2090. success = (error == WAIT_OBJECT_0);
  2091. }
  2092. }
  2093. ForgetPipeIo(&pipeio);
  2094. if (pipeio.Cancelled) {
  2095. error = WAIT_TIMEOUT;
  2096. success = FALSE;
  2097. }
  2098. }
  2099. if (success) {
  2100. //
  2101. // get the real bytes written
  2102. //
  2103. GetOverlappedResult(Handle, &pipeio.Overlapped, BytesWritten, FALSE);
  2104. }
  2105. CloseHandle(pipeio.Overlapped.hEvent);
  2106. if (!success) {
  2107. #if DBG
  2108. IF_DEBUG(NAMEPIPE) {
  2109. DbgPrint("VrWriteNamePipe: Error: Returning %d\n", error);
  2110. }
  2111. #endif
  2112. SetLastError(error);
  2113. if (error == WAIT_TIMEOUT) {
  2114. CloseHandle(Handle);
  2115. VrpRemoveOpenNamedPipeInfo(Handle);
  2116. }
  2117. }
  2118. #if DBG
  2119. else {
  2120. IF_DEBUG(NAMEPIPE) {
  2121. DbgPrint("VrWriteNamePipe: Ok: %d bytes written to pipe\n", *BytesWritten);
  2122. }
  2123. }
  2124. #endif
  2125. return success;
  2126. }
  2127. //
  2128. // externally callable helpers
  2129. //
  2130. BOOL
  2131. VrIsNamedPipeName(
  2132. IN LPSTR Name
  2133. )
  2134. /*++
  2135. Routine Description:
  2136. Checks if a string designates a named pipe. As criteria for the decision
  2137. we use:
  2138. \\computername\PIPE\...
  2139. DOS (client-side) can only open a named pipe which is created at a server
  2140. and must therefore be prefixed by a computername
  2141. We *know* that Name has just been used to successfully open a handle to
  2142. a named <something>, so it should at least be semi-sensible. We can
  2143. assume the following:
  2144. * ASCIZ string
  2145. * an LPSTR points at a single byte (& therefore ++ will add 1)
  2146. But we can't assume the following:
  2147. * Canonicalized name
  2148. Arguments:
  2149. Name - to check for (Dos) named pipe syntax
  2150. Return Value:
  2151. BOOL
  2152. TRUE - Name refers to (local or remote) named pipe
  2153. FALSE - Name doesn't look like name of pipe
  2154. --*/
  2155. {
  2156. int CharCount;
  2157. #if DBG
  2158. LPSTR OriginalName = Name;
  2159. #endif
  2160. if (IS_ASCII_PATH_SEPARATOR(*Name)) {
  2161. ++Name;
  2162. if (IS_ASCII_PATH_SEPARATOR(*Name)) {
  2163. ++Name;
  2164. CharCount = 0;
  2165. while (*Name && !IS_ASCII_PATH_SEPARATOR(*Name)) {
  2166. ++Name;
  2167. ++CharCount;
  2168. }
  2169. if (!CharCount || !*Name) {
  2170. //
  2171. // Name is \\ or \\\ or just \\name which I don't understand,
  2172. // so its not a named pipe - fail it
  2173. //
  2174. #if DBG
  2175. IF_DEBUG(NAMEPIPE) {
  2176. DbgPrint("VrIsNamedPipeName - returning FALSE for %s\n", OriginalName);
  2177. }
  2178. #endif
  2179. return FALSE;
  2180. }
  2181. //
  2182. // bump name past next path separator. Note that we don't have to
  2183. // check CharCount for max. length of a computername, because this
  2184. // function is called only after the (presumed) named pipe has been
  2185. // successfully opened, therefore we know that the name has been
  2186. // validated
  2187. //
  2188. ++Name;
  2189. } else {
  2190. #if DBG
  2191. IF_DEBUG(NAMEPIPE) {
  2192. DbgPrint("VrIsNamedPipeName - returning FALSE for %s\n", OriginalName);
  2193. }
  2194. #endif
  2195. return FALSE;
  2196. }
  2197. //
  2198. // We are at <something> (after \ or \\<name>\). Check if <something>
  2199. // is [Pp][Ii][Pp][Ee][\\/]
  2200. //
  2201. if (!_strnicmp(Name, "PIPE", 4)) {
  2202. Name += 4;
  2203. if (IS_ASCII_PATH_SEPARATOR(*Name)) {
  2204. #if DBG
  2205. IF_DEBUG(NAMEPIPE) {
  2206. DbgPrint("VrIsNamedPipeName - returning TRUE for %s\n", OriginalName);
  2207. }
  2208. #endif
  2209. return TRUE;
  2210. }
  2211. }
  2212. }
  2213. #if DBG
  2214. IF_DEBUG(NAMEPIPE) {
  2215. DbgPrint("VrIsNamedPipeName - returning FALSE for %s\n", OriginalName);
  2216. }
  2217. #endif
  2218. return FALSE;
  2219. }
  2220. BOOL
  2221. VrIsNamedPipeHandle(
  2222. IN HANDLE Handle
  2223. )
  2224. /*++
  2225. Routine Description:
  2226. Checks if Handle appears in the list of known named pipe handles. Callable
  2227. from outside this module
  2228. Arguments:
  2229. Handle - of suspected name pipe
  2230. Return Value:
  2231. BOOL
  2232. TRUE Handle refers to an open named pipe
  2233. FALSE Don't know what Handle refers to
  2234. --*/
  2235. {
  2236. return VrpGetOpenNamedPipeInfo(Handle) != NULL;
  2237. }
  2238. LPSTR
  2239. VrConvertLocalNtPipeName(
  2240. OUT LPSTR Buffer OPTIONAL,
  2241. IN LPSTR Name
  2242. )
  2243. /*++
  2244. Routine Description:
  2245. Converts a pipe name of the form \\<local-machine-name>\pipe\name to
  2246. \\.\pipe\name
  2247. If non-NULL pointer is returned, the buffer contains a canonicalized
  2248. name - any forward-slash characters (/) are converted to backward-slash
  2249. characters (\). In the interest of future-proofing, the name is not
  2250. upper-cased
  2251. Assumes: Name points to a named pipe specification (\\Server\PIPE\name)
  2252. Note: it is possible to supply the same input and output buffers and have
  2253. the conversion take place in situ. However, this is a side-effect
  2254. of the fact the input computername is replaced by effectively a
  2255. computername of length 1. Nevertheless, it is safe
  2256. Arguments:
  2257. Buffer - pointer to CHAR array where name is placed. If this parameter
  2258. is not present then this routine will allocate a buffer (using
  2259. LocalAlloc and return that
  2260. Name - pointer to ASCIZ pipe name
  2261. Return Value:
  2262. LPSTR - pointer to buffer containing name or NULL if failed
  2263. --*/
  2264. {
  2265. DWORD prefixLength; // length of \\computername
  2266. DWORD pipeLength; // length of pipe name without computername/device prefix
  2267. LPSTR pipeName; // \PIPE\name...
  2268. static char ThisComputerName[MAX_COMPUTERNAME_LENGTH+1] = {0};
  2269. static DWORD ThisComputerNameLength = 0xffffffff;
  2270. BOOLEAN mapped = FALSE;
  2271. ASSERT(Name);
  2272. ASSERT(IS_ASCII_PATH_SEPARATOR(Name[0]) && IS_ASCII_PATH_SEPARATOR(Name[1]));
  2273. //
  2274. // first time round, get the computername. If this fails assume there is no
  2275. // computername (i.e. no network)
  2276. //
  2277. if (ThisComputerNameLength == 0xffffffff) {
  2278. ThisComputerNameLength = sizeof(ThisComputerName);
  2279. if (!GetComputerName((LPTSTR)&ThisComputerName, &ThisComputerNameLength)) {
  2280. ThisComputerNameLength = 0;
  2281. }
  2282. }
  2283. if (!ARGUMENT_PRESENT(Buffer)) {
  2284. Buffer = (LPSTR)LocalAlloc(LMEM_FIXED, strlen(Name)+1);
  2285. }
  2286. if (Buffer) {
  2287. pipeName = strchr(Name+2, '\\'); // starts \pipe\...
  2288. if (!pipeName) {
  2289. pipeName = strchr(Name+2, '/');
  2290. }
  2291. ASSERT(pipeName);
  2292. if ( NULL == pipeName ) {
  2293. LocalFree ( (HLOCAL)Buffer );
  2294. return NULL;
  2295. }
  2296. pipeLength = strlen(pipeName);
  2297. prefixLength = (DWORD)pipeName - (DWORD)Name;
  2298. if (ThisComputerNameLength && (prefixLength - 2 == ThisComputerNameLength)) {
  2299. if (!_strnicmp(ThisComputerName, &Name[2], ThisComputerNameLength)) {
  2300. strcpy(Buffer, LOCAL_DEVICE_PREFIX);
  2301. mapped = TRUE;
  2302. }
  2303. }
  2304. if (!mapped) {
  2305. strncpy(Buffer, Name, prefixLength);
  2306. Buffer[prefixLength] = 0;
  2307. }
  2308. strcat(Buffer, pipeName);
  2309. //
  2310. // convert any forward-slashes to backward-slashes
  2311. //
  2312. do {
  2313. if (pipeName = strchr(Buffer, '/')) {
  2314. *pipeName++ = '\\';
  2315. }
  2316. } while (pipeName);
  2317. #if DBG
  2318. IF_DEBUG(NAMEPIPE) {
  2319. DbgPrint("VrConvertLocalNtPipeName - returning %s\n", Buffer);
  2320. }
  2321. #endif
  2322. }
  2323. return Buffer;
  2324. }
  2325. //
  2326. // Private utilities
  2327. //
  2328. //
  2329. // Private list of open named pipe info structures for this VDM process, and
  2330. // associated manipulation routines
  2331. //
  2332. PRIVATE
  2333. POPEN_NAMED_PIPE_INFO OpenNamedPipeInfoList = NULL;
  2334. PRIVATE
  2335. POPEN_NAMED_PIPE_INFO LastOpenNamedPipeInfo = NULL;
  2336. PRIVATE
  2337. BOOL
  2338. VrpAddOpenNamedPipeInfo(
  2339. IN HANDLE Handle,
  2340. IN LPSTR PipeName
  2341. )
  2342. /*++
  2343. Routine Description:
  2344. When a named pipe is successfully opened, we call this routine to
  2345. associate an open handle and a pipe name. This is required by
  2346. DosQNmPipeInfo (VrGetNamedPipeInfo)
  2347. Arguments:
  2348. Handle - The handle returned from CreateFile (in demOpen)
  2349. PipeName - Name of pipe being opened
  2350. Return Value:
  2351. BOOL
  2352. TRUE - created a OPEN_NAMED_PIPE_INFO structure and added to list
  2353. FALSE - couldn't get memory, or couldn't create event. Use GetLastError
  2354. if you really want to know why this failed
  2355. --*/
  2356. {
  2357. POPEN_NAMED_PIPE_INFO PipeInfo;
  2358. DWORD NameLength;
  2359. //
  2360. // grab a OPEN_NAMED_PIPE_INFO structure
  2361. //
  2362. NameLength = strlen(PipeName) + 1;
  2363. PipeInfo = (POPEN_NAMED_PIPE_INFO)
  2364. LocalAlloc(LMEM_FIXED,
  2365. ROUND_UP_COUNT((sizeof(OPEN_NAMED_PIPE_INFO) + NameLength),
  2366. sizeof(DWORD)
  2367. )
  2368. );
  2369. //
  2370. // if we cannot claim memory here, we should *really* close the pipe and
  2371. // return an insufficient memory error to the VDM. However, I don't expect
  2372. // us to run out of memory
  2373. //
  2374. if (PipeInfo == NULL) {
  2375. #if DBG
  2376. IF_DEBUG(NAMEPIPE) {
  2377. DbgPrint("VrpAddOpenNamedPipeInfo: couldn't allocate structure - returning FALSE\n");
  2378. }
  2379. #endif
  2380. return FALSE;
  2381. }
  2382. //
  2383. // fill it in
  2384. //
  2385. PipeInfo->Next = NULL;
  2386. PipeInfo->Handle = Handle;
  2387. PipeInfo->NameLength = NameLength;
  2388. strcpy(PipeInfo->Name, PipeName); // from DOS, so its old-fashioned ASCII
  2389. //
  2390. // put it at the end of the list
  2391. //
  2392. if (LastOpenNamedPipeInfo == NULL) {
  2393. OpenNamedPipeInfoList = PipeInfo;
  2394. } else {
  2395. LastOpenNamedPipeInfo->Next = PipeInfo;
  2396. }
  2397. LastOpenNamedPipeInfo = PipeInfo;
  2398. #if DBG
  2399. IF_DEBUG(NAMEPIPE) {
  2400. DbgPrint("VrpAddOpenNamedPipeInfo - adding structure @ %08x, Handle=0x%08x, Name=%s\n",
  2401. PipeInfo,
  2402. PipeInfo->Handle,
  2403. PipeInfo->Name
  2404. );
  2405. }
  2406. #endif
  2407. return TRUE;
  2408. }
  2409. PRIVATE
  2410. POPEN_NAMED_PIPE_INFO
  2411. VrpGetOpenNamedPipeInfo(
  2412. IN HANDLE Handle
  2413. )
  2414. /*++
  2415. Routine Description:
  2416. Linear search for an OPEN_NAMED_PIPE_INFO structure in OpenNamedPipeInfoList
  2417. using the handle as search criteria
  2418. Arguments:
  2419. Handle - to search for
  2420. Return Value:
  2421. POPEN_NAMED_PIPE_INFO
  2422. Success - Pointer to located structure
  2423. Failure - NULL
  2424. --*/
  2425. {
  2426. POPEN_NAMED_PIPE_INFO ptr;
  2427. for (ptr = OpenNamedPipeInfoList; ptr; ptr = ptr->Next) {
  2428. if (ptr->Handle == Handle) {
  2429. break;
  2430. }
  2431. }
  2432. return ptr;
  2433. }
  2434. PRIVATE
  2435. BOOL
  2436. VrpRemoveOpenNamedPipeInfo(
  2437. IN HANDLE Handle
  2438. )
  2439. /*++
  2440. Routine Description:
  2441. Unlinks and frees an OPEN_NAMED_PIPE_INFO structure from
  2442. OpenNamedPipeInfoList
  2443. Note: Assumes that the Handle is in the list (no action taken if not
  2444. found)
  2445. Arguments:
  2446. Handle - defining OPEN_NAMED_PIPE_INFO structure to remove from list
  2447. Return Value:
  2448. BOOL
  2449. TRUE - OPEN_NAMED_PIPE_INFO structure corresponding to Handle was
  2450. removed from list and freed
  2451. FALSE - OPEN_NAMED_PIPE_INFO structure corresponding to Handle was
  2452. not found
  2453. --*/
  2454. {
  2455. POPEN_NAMED_PIPE_INFO ptr, prev = NULL;
  2456. #if DBG
  2457. IF_DEBUG(NAMEPIPE) {
  2458. DbgPrint("VrpRemoveOpenNamedPipeInfo(0x%08x)\n", Handle);
  2459. DumpOpenPipeList();
  2460. DumpRequestQueue();
  2461. }
  2462. #endif
  2463. for (ptr = OpenNamedPipeInfoList; ptr; ) {
  2464. if (ptr->Handle == Handle) {
  2465. if (!prev) {
  2466. OpenNamedPipeInfoList = ptr->Next;
  2467. } else {
  2468. prev->Next = ptr->Next;
  2469. }
  2470. if (LastOpenNamedPipeInfo == ptr) {
  2471. LastOpenNamedPipeInfo = prev;
  2472. }
  2473. #if DBG
  2474. IF_DEBUG(NAMEPIPE) {
  2475. DbgPrint("VrpRemoveOpenNamedPipeInfo - freeing structure @ %08x, Handle=0x%08x, Name=%s\n",
  2476. ptr,
  2477. ptr->Handle,
  2478. ptr->Name
  2479. );
  2480. }
  2481. #endif
  2482. LocalFree(ptr);
  2483. return TRUE;
  2484. } else {
  2485. prev = ptr;
  2486. ptr = ptr->Next;
  2487. }
  2488. }
  2489. #if DBG
  2490. IF_DEBUG(NAMEPIPE) {
  2491. DbgPrint("VrpRemoveOpenNamedPipeInfo: Can't find 0x%08x in list\n", Handle);
  2492. }
  2493. #endif
  2494. return FALSE;
  2495. }
  2496. PRIVATE
  2497. VOID
  2498. RememberPipeIo(
  2499. IN POVERLAPPED_PIPE_IO PipeIo
  2500. )
  2501. /*++
  2502. Routine Description:
  2503. Adds an OVERLAPPED_PIPE_IO structure to the list of in-progress named pipe
  2504. I/Os
  2505. Arguments:
  2506. PipeIo - pointer to OVERLAPPED_PIPE_IO structure to add to list
  2507. Return Value:
  2508. None.
  2509. --*/
  2510. {
  2511. //
  2512. // just stick this at front of list; order is not important - this is just
  2513. // a stack of in-progress requests
  2514. //
  2515. PipeIo->Thread = GetCurrentThreadId();
  2516. EnterCriticalSection(&VrNamedPipeCancelCritSec);
  2517. PipeIo->Next = PipeIoQueue;
  2518. PipeIoQueue = PipeIo;
  2519. LeaveCriticalSection(&VrNamedPipeCancelCritSec);
  2520. }
  2521. PRIVATE
  2522. VOID
  2523. ForgetPipeIo(
  2524. IN POVERLAPPED_PIPE_IO PipeIo
  2525. )
  2526. /*++
  2527. Routine Description:
  2528. Removes an OVERLAPPED_PIPE_IO structure from the list of in-progress named
  2529. pipe I/Os
  2530. Arguments:
  2531. PipeIo - pointer to OVERLAPPED_PIPE_IO structure to remove
  2532. Return Value:
  2533. None.
  2534. --*/
  2535. {
  2536. POVERLAPPED_PIPE_IO prev, ptr;
  2537. EnterCriticalSection(&VrNamedPipeCancelCritSec);
  2538. for (ptr = PipeIoQueue, prev = (POVERLAPPED_PIPE_IO)&PipeIoQueue; ptr && ptr != PipeIo; ) {
  2539. prev = ptr;
  2540. ptr = ptr->Next;
  2541. }
  2542. if (ptr == PipeIo) {
  2543. prev->Next = ptr->Next;
  2544. }
  2545. LeaveCriticalSection(&VrNamedPipeCancelCritSec);
  2546. }
  2547. #if DBG
  2548. VOID DumpOpenPipeList()
  2549. {
  2550. POPEN_NAMED_PIPE_INFO ptr = OpenNamedPipeInfoList;
  2551. DbgPrint("DumpOpenPipeList\n");
  2552. if (!ptr) {
  2553. DbgPrint("DumpOpenPipeList: no open named pipe structures\n");
  2554. } else {
  2555. while (ptr) {
  2556. DbgPrint("\n"
  2557. "OPEN_NAMED_PIPE_INFO structure @%08x:\n"
  2558. "Next. . . . . . . . . . %08x\n"
  2559. "Handle. . . . . . . . . %08x\n"
  2560. "NameLength. . . . . . . %d\n"
  2561. "DosPdb. . . . . . . . . %04x\n"
  2562. "Name. . . . . . . . . . %s\n",
  2563. ptr,
  2564. ptr->Next,
  2565. ptr->Handle,
  2566. ptr->NameLength,
  2567. ptr->DosPdb,
  2568. ptr->Name
  2569. );
  2570. ptr = ptr->Next;
  2571. }
  2572. DbgPrint("\n");
  2573. }
  2574. }
  2575. VOID DumpRequestQueue()
  2576. {
  2577. PDOS_ASYNC_NAMED_PIPE_INFO ptr;
  2578. DbgPrint("DumpRequestQueue\n");
  2579. EnterCriticalSection(&VrNmpRequestQueueCritSec);
  2580. ptr = RequestQueueHead;
  2581. if (!ptr) {
  2582. DbgPrint("DumpRequestQueue: no request packets queued\n");
  2583. } else {
  2584. for (; ptr; ptr = ptr->Next) {
  2585. //
  2586. // NT (308c) can't handle all this being put on the stack - gets
  2587. // fault in KdpCopyDataToStack
  2588. //
  2589. DbgPrint("\n"
  2590. "DOS_ASYNC_NAMED_PIPE_INFO structure @%08x:\n"
  2591. "Next. . . . . . . . . . %08x\n"
  2592. "Overlapped.Internal . . %08x\n"
  2593. "Overlapped.InternalHigh %08x\n"
  2594. "Overlapped.Offset . . . %08x\n"
  2595. "Overlapped.OffsetHigh . %08x\n"
  2596. "Overlapped.hEvent . . . %08x\n",
  2597. ptr,
  2598. ptr->Next,
  2599. ptr->Overlapped.Internal,
  2600. ptr->Overlapped.InternalHigh,
  2601. ptr->Overlapped.Offset,
  2602. ptr->Overlapped.OffsetHigh,
  2603. ptr->Overlapped.hEvent
  2604. );
  2605. DbgPrint("Type2 . . . . . . . . . %d\n"
  2606. "Completed . . . . . . . %d\n"
  2607. "Handle. . . . . . . . . %08x\n"
  2608. "Buffer. . . . . . . . . %04x:%04x\n"
  2609. "BytesTransferred. . . . %d\n"
  2610. "pBytesTransferred . . . %08x\n"
  2611. "pErrorCode. . . . . . . %08x\n"
  2612. "ANR . . . . . . . . . . %04x:%04x\n"
  2613. "Semaphore . . . . . . . %04x:%04x\n"
  2614. "RequestType . . . . . . %04x [%s]\n",
  2615. ptr->Type2,
  2616. ptr->Completed,
  2617. ptr->Handle,
  2618. HIWORD(ptr->Buffer),
  2619. LOWORD(ptr->Buffer),
  2620. ptr->BytesTransferred,
  2621. ptr->pBytesTransferred,
  2622. ptr->pErrorCode,
  2623. HIWORD(ptr->ANR),
  2624. LOWORD(ptr->ANR),
  2625. HIWORD(ptr->Semaphore),
  2626. LOWORD(ptr->Semaphore),
  2627. ptr->RequestType,
  2628. ptr->RequestType == ANP_READ
  2629. ? "READ"
  2630. : ptr->RequestType == ANP_READ2
  2631. ? "READ2"
  2632. : ptr->RequestType == ANP_WRITE
  2633. ? "WRITE"
  2634. : ptr->RequestType == ANP_WRITE2
  2635. ? "WRITE2"
  2636. : "*** UNKNOWN REQUEST ***"
  2637. );
  2638. }
  2639. DbgPrint("\n");
  2640. }
  2641. LeaveCriticalSection(&VrNmpRequestQueueCritSec);
  2642. }
  2643. #endif