Source code of Windows XP (NT5)
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.

812 lines
22 KiB

  1. /*++
  2. Copyright (c) 1990 - 1995 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. portredn.c
  6. Abstract:
  7. This module contains functions to handle port redirection.
  8. Earlier this was done by localmon, the code is a modified version of
  9. localmon code.
  10. Author:
  11. Muhunthan Sivapragasam (MuhuntS) 10-Sep-1995
  12. Revision History:
  13. --*/
  14. #include <precomp.h>
  15. WCHAR szDeviceNameHeader[] = L"\\Device\\NamedPipe\\Spooler\\";
  16. WCHAR szCOM[] = L"COM";
  17. WCHAR szLPT[] = L"LPT";
  18. //
  19. // Definitions for MonitorThread:
  20. //
  21. #define TRANSMISSION_DATA_SIZE 0x400
  22. #define NUMBER_OF_PIPE_INSTANCES 10
  23. typedef struct _TRANSMISSION {
  24. HANDLE hPipe;
  25. BYTE Data[TRANSMISSION_DATA_SIZE];
  26. LPOVERLAPPED pOverlapped;
  27. HANDLE hPrinter;
  28. DWORD JobId;
  29. PINIPORT pIniPort;
  30. } TRANSMISSION, *PTRANSMISSION;
  31. typedef struct _REDIRECT_INFO {
  32. PINIPORT pIniPort;
  33. HANDLE hEvent;
  34. } REDIRECT_INFO, *PREDIRECT_INFO;
  35. VOID
  36. FreeRedirectInfo(
  37. PREDIRECT_INFO pRedirectInfo
  38. )
  39. {
  40. SplInSem();
  41. //
  42. // This is to handle the case when Redirection thread did not initialize
  43. // correctly and is terminating abnormally
  44. // Since CloseHandle has not been called it is ok to do this
  45. //
  46. if ( pRedirectInfo->pIniPort->hEvent == pRedirectInfo->hEvent )
  47. pRedirectInfo->pIniPort->hEvent = NULL;
  48. DECPORTREF(pRedirectInfo->pIniPort);
  49. CloseHandle(pRedirectInfo->hEvent);
  50. FreeSplMem(pRedirectInfo);
  51. }
  52. VOID
  53. RemoveColon(
  54. LPWSTR pName)
  55. {
  56. DWORD Length;
  57. Length = wcslen(pName);
  58. if (pName[Length-1] == L':')
  59. pName[Length-1] = 0;
  60. }
  61. VOID
  62. RemoveDeviceName(
  63. PINIPORT pIniPort
  64. )
  65. {
  66. SplInSem();
  67. if ( pIniPort->hEvent ) {
  68. //
  69. // Redirection thread is told to terminate here; It will close the
  70. // handle. If it has already terminated then this call will fail
  71. //
  72. SetEvent(pIniPort->hEvent);
  73. pIniPort->hEvent = NULL;
  74. }
  75. }
  76. #define MAX_ACE 6
  77. PSECURITY_DESCRIPTOR
  78. CreateNamedPipeSecurityDescriptor(
  79. VOID)
  80. /*++
  81. Routine Description:
  82. Creates a security descriptor giving everyone access.
  83. Arguments:
  84. Return Value:
  85. The security descriptor returned by BuildPrintObjectProtection.
  86. --*/
  87. {
  88. UCHAR AceType[MAX_ACE];
  89. PSID AceSid[MAX_ACE];
  90. BYTE InheritFlags[MAX_ACE];
  91. DWORD AceCount;
  92. PSECURITY_DESCRIPTOR ServerSD = NULL;
  93. //
  94. // For Code optimization we replace 5 individaul
  95. // SID_IDENTIFIER_AUTHORITY with an array of
  96. // SID_IDENTIFIER_AUTHORITYs
  97. // where
  98. // SidAuthority[0] = UserSidAuthority
  99. // SidAuthority[1] = PowerSidAuthority
  100. // SidAuthority[2] = EveryOneSidAuthority
  101. // SidAuthority[3] = CreatorSidAuthority
  102. // SidAuthority[4] = SystemSidAuthority
  103. // SidAuthority[5] = AdminSidAuthority
  104. //
  105. SID_IDENTIFIER_AUTHORITY SidAuthority[MAX_ACE] = {
  106. SECURITY_NT_AUTHORITY,
  107. SECURITY_NT_AUTHORITY,
  108. SECURITY_WORLD_SID_AUTHORITY,
  109. SECURITY_CREATOR_SID_AUTHORITY,
  110. SECURITY_NT_AUTHORITY,
  111. SECURITY_NT_AUTHORITY
  112. };
  113. //
  114. // For code optimization we replace 5 individual Sids with
  115. // an array of Sids
  116. // where
  117. // Sid[0] = UserSid
  118. // Sid[1] = PowerSid
  119. // Sid[2] = EveryOne
  120. // Sid[3] = CreatorSid
  121. // Sid[4] = SystemSid
  122. // Sid[5] = AdminSid
  123. //
  124. PSID Sids[MAX_ACE] = {NULL,NULL,NULL,NULL,NULL,NULL};
  125. ACCESS_MASK AceMask[MAX_ACE] = {
  126. FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE ,
  127. FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE ,
  128. (FILE_GENERIC_READ | FILE_WRITE_DATA | FILE_ALL_ACCESS) &
  129. ~WRITE_DAC &~WRITE_OWNER & ~DELETE & ~FILE_CREATE_PIPE_INSTANCE,
  130. STANDARD_RIGHTS_ALL | FILE_GENERIC_WRITE | FILE_GENERIC_READ | FILE_ALL_ACCESS,
  131. STANDARD_RIGHTS_ALL | FILE_GENERIC_WRITE | FILE_GENERIC_READ | FILE_ALL_ACCESS,
  132. STANDARD_RIGHTS_ALL | FILE_GENERIC_WRITE | FILE_GENERIC_READ | FILE_ALL_ACCESS
  133. };
  134. DWORD SubAuthorities[3*MAX_ACE] = {
  135. 2 , SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_USERS ,
  136. 2 , SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_POWER_USERS ,
  137. 1 , SECURITY_WORLD_RID , 0 ,
  138. 1 , SECURITY_CREATOR_OWNER_RID , 0 ,
  139. 1 , SECURITY_LOCAL_SYSTEM_RID , 0 ,
  140. 2 , SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_ADMINS
  141. };
  142. //
  143. // Name Pipe SD
  144. //
  145. for(AceCount = 0;
  146. ( (AceCount < MAX_ACE) &&
  147. AllocateAndInitializeSid(&SidAuthority[AceCount],
  148. (BYTE)SubAuthorities[AceCount*3],
  149. SubAuthorities[AceCount*3+1],
  150. SubAuthorities[AceCount*3+2],
  151. 0, 0, 0, 0, 0, 0,
  152. &Sids[AceCount]));
  153. AceCount++)
  154. {
  155. AceType[AceCount] = ACCESS_ALLOWED_ACE_TYPE;
  156. AceSid[AceCount] = Sids[AceCount];
  157. InheritFlags[AceCount] = 0;
  158. }
  159. if(AceCount == MAX_ACE)
  160. {
  161. if(!BuildPrintObjectProtection(AceType,
  162. AceCount,
  163. AceSid,
  164. AceMask,
  165. InheritFlags,
  166. NULL,
  167. NULL,
  168. NULL,
  169. &ServerSD ) )
  170. {
  171. DBGMSG( DBG_WARNING,( "Couldn't buidl Named Pipe protection" ) );
  172. }
  173. }
  174. else
  175. {
  176. DBGMSG( DBG_WARNING,( "Couldn't Allocate and initialize SIDs" ) );
  177. }
  178. for(AceCount=0;AceCount<MAX_ACE;AceCount++)
  179. {
  180. if(Sids[AceCount])
  181. FreeSid( Sids[AceCount] );
  182. }
  183. return ServerSD;
  184. }
  185. LPWSTR
  186. SetupDosDev(
  187. PINIPORT pIniPort,
  188. LPWSTR szPipeName,
  189. DWORD cchPipeName,
  190. PSECURITY_ATTRIBUTES pSecurityAttributes,
  191. PSECURITY_ATTRIBUTES* ppSecurityAttributes
  192. )
  193. {
  194. WCHAR NewNtDeviceName[MAX_PATH];
  195. WCHAR OldNtDeviceName[MAX_PATH];
  196. WCHAR DosDeviceName[MAX_PATH];
  197. LPWSTR pszNewDeviceName = NULL;
  198. PSECURITY_DESCRIPTOR lpSecurityDescriptor = NULL;
  199. BOOL bRet = FALSE;
  200. wcscpy(DosDeviceName, pIniPort->pName);
  201. RemoveColon(DosDeviceName);
  202. if(StrNCatBuff(NewNtDeviceName,
  203. COUNTOF(NewNtDeviceName),
  204. szDeviceNameHeader,
  205. pIniPort->pName,
  206. NULL) != ERROR_SUCCESS ) {
  207. goto Cleanup;
  208. }
  209. RemoveColon(NewNtDeviceName);
  210. pszNewDeviceName = AllocSplStr(NewNtDeviceName);
  211. if ( !pszNewDeviceName ||
  212. !QueryDosDevice(DosDeviceName, OldNtDeviceName,
  213. sizeof(OldNtDeviceName)/sizeof(OldNtDeviceName[0]))) {
  214. goto Cleanup;
  215. }
  216. lpSecurityDescriptor = CreateNamedPipeSecurityDescriptor();
  217. if (lpSecurityDescriptor) {
  218. pSecurityAttributes->nLength = sizeof(SECURITY_ATTRIBUTES);
  219. pSecurityAttributes->lpSecurityDescriptor = lpSecurityDescriptor;
  220. pSecurityAttributes->bInheritHandle = FALSE;
  221. } else {
  222. pSecurityAttributes = NULL;
  223. }
  224. //
  225. // If clause added to preclude multiple entries of the same named pipe in the device
  226. // name definition.
  227. // Ram 1\16
  228. //
  229. if (lstrcmp(NewNtDeviceName, OldNtDeviceName) != 0) {
  230. DefineDosDevice(DDD_RAW_TARGET_PATH, DosDeviceName, NewNtDeviceName);
  231. }
  232. if (StrNCatBuff( szPipeName,
  233. cchPipeName,
  234. L"\\\\.\\Pipe\\Spooler\\",
  235. pIniPort->pName,
  236. NULL) != ERROR_SUCCESS) {
  237. goto Cleanup;
  238. }
  239. RemoveColon(szPipeName);
  240. *ppSecurityAttributes = pSecurityAttributes;
  241. bRet = TRUE;
  242. Cleanup:
  243. if ( !bRet ) {
  244. FreeSplStr(pszNewDeviceName);
  245. pszNewDeviceName = NULL;
  246. }
  247. return pszNewDeviceName;
  248. }
  249. VOID
  250. ReadThread(
  251. PTRANSMISSION pTransmission)
  252. {
  253. DOC_INFO_1W DocInfo;
  254. DWORD BytesRead;
  255. DWORD BytesWritten;
  256. BOOL bStartDocPrinterResult = FALSE;
  257. BOOL bReadResult;
  258. LPWSTR pszPrinter=NULL;
  259. //
  260. // ImpersonateNamedPipeClient requires that some data is read before
  261. // the impersonation is done.
  262. //
  263. bReadResult = ReadFile(pTransmission->hPipe,
  264. pTransmission->Data,
  265. sizeof(pTransmission->Data),
  266. &BytesRead,
  267. NULL);
  268. if (!bReadResult)
  269. goto Fail;
  270. if (!ImpersonateNamedPipeClient(pTransmission->hPipe)) {
  271. DBGMSG(DBG_ERROR,("ImpersonateNamedPipeClient failed %d\n",
  272. GetLastError()));
  273. goto Fail;
  274. }
  275. SPLASSERT(pTransmission->pIniPort->cPrinters);
  276. pszPrinter = AllocSplStr(pTransmission->pIniPort->ppIniPrinter[0]->pName);
  277. if ( !pszPrinter ) {
  278. goto Fail;
  279. }
  280. //
  281. // Open the printer.
  282. //
  283. if (!OpenPrinter(pszPrinter, &pTransmission->hPrinter, NULL)) {
  284. DBGMSG(DBG_WARN, ("OpenPrinter(%ws) failed: Error %d\n",
  285. pszPrinter,
  286. GetLastError()));
  287. goto Fail;
  288. }
  289. memset(&DocInfo, 0, sizeof(DOC_INFO_1W));
  290. if (StartDocPrinter(pTransmission->hPrinter, 1, (LPBYTE)&DocInfo)) {
  291. DBGMSG(DBG_INFO, ("StartDocPrinter succeeded\n"));
  292. bStartDocPrinterResult = TRUE;
  293. } else {
  294. DBGMSG(DBG_WARN, ("StartDocPrinter failed: Error %d\n",
  295. GetLastError()));
  296. goto Fail;
  297. }
  298. while (bReadResult && BytesRead) {
  299. if (!WritePrinter(pTransmission->hPrinter,
  300. pTransmission->Data,
  301. BytesRead,
  302. &BytesWritten))
  303. {
  304. DBGMSG(DBG_WARN, ("WritePrinter failed: Error %d\n",
  305. GetLastError()));
  306. goto Fail;
  307. }
  308. bReadResult = ReadFile(pTransmission->hPipe,
  309. pTransmission->Data,
  310. sizeof(pTransmission->Data),
  311. &BytesRead,
  312. NULL);
  313. }
  314. DBGMSG(DBG_INFO, ("bool %d BytesRead 0x%x (Error = %d) EOT\n",
  315. bReadResult,
  316. BytesRead,
  317. GetLastError()));
  318. Fail:
  319. if (bStartDocPrinterResult) {
  320. if (!EndDocPrinter(pTransmission->hPrinter)) {
  321. DBGMSG(DBG_WARN, ("EndDocPrinter failed: Error %d\n",
  322. GetLastError()));
  323. }
  324. }
  325. FreeSplStr(pszPrinter);
  326. if (pTransmission->hPrinter)
  327. ClosePrinter(pTransmission->hPrinter);
  328. if ( !SetEvent(pTransmission->pOverlapped->hEvent)) {
  329. DBGMSG(DBG_ERROR, ("SetEvent failed %d\n", GetLastError()));
  330. }
  331. FreeSplMem(pTransmission);
  332. }
  333. BOOL
  334. ReconnectNamedPipe(
  335. HANDLE hPipe,
  336. LPOVERLAPPED pOverlapped)
  337. {
  338. DWORD Error;
  339. BOOL bIOPending = FALSE;
  340. DisconnectNamedPipe(hPipe);
  341. if (!ConnectNamedPipe(hPipe,
  342. pOverlapped)) {
  343. Error = GetLastError( );
  344. if (Error == ERROR_IO_PENDING) {
  345. DBGMSG(DBG_INFO, ("re-ConnectNamedPipe 0x%x IO pending\n", hPipe));
  346. bIOPending = TRUE;
  347. } else {
  348. DBGMSG(DBG_ERROR, ("re-ConnectNamedPipe 0x%x failed. Error %d\n",
  349. hPipe,
  350. Error));
  351. }
  352. } else {
  353. DBGMSG(DBG_INFO, ("re-ConnectNamedPipe successful 0x%x\n", hPipe));
  354. }
  355. return bIOPending;
  356. }
  357. BOOL
  358. RedirectionThread(
  359. PREDIRECT_INFO pRedirectInfo
  360. )
  361. /*++
  362. Redirection thread is responsible for freeing pRedirectInfo. Since
  363. the ref count on port thread is incremented before this is called we
  364. know that the IniPort will be valid till we decrement the ref count.
  365. We are also passed the event we should wait to die on.
  366. This is pIniPort->hEvent. But redirection thread should use the local
  367. copy passed and not the one in pIniPort. The reason is there could be a
  368. lag from the time this event is set and the redirection dies. In the
  369. meantime a new rediction thread could be spun off and in which case the
  370. pIniPort->hEvent will not be for this thread
  371. When redirection thread is told to die:
  372. a. it should decrement the ref count on the pIniPort object when it is
  373. done with it's reference to pIniPort
  374. b. it should call CloseHandle on pRedirectInfo->hEvent
  375. --*/
  376. {
  377. WCHAR szPipeName[MAX_PATH];
  378. HANDLE hPipe[NUMBER_OF_PIPE_INSTANCES];
  379. SECURITY_ATTRIBUTES SecurityAttributes;
  380. PSECURITY_ATTRIBUTES pSecurityAttributes;
  381. //
  382. // One extra event for our trigger (pIniPort->hEvent)
  383. //
  384. HANDLE ahEvent[NUMBER_OF_PIPE_INSTANCES+1];
  385. BOOL abReconnect[NUMBER_OF_PIPE_INSTANCES];
  386. OVERLAPPED Overlapped[NUMBER_OF_PIPE_INSTANCES];
  387. DWORD WaitResult, i, j, Error, dwThreadId;
  388. PTRANSMISSION pTransmission;
  389. HANDLE hThread;
  390. BOOL bTerminate = FALSE;
  391. LPWSTR pszNewDeviceName = NULL;
  392. WCHAR DosDeviceName[MAX_PATH];
  393. SecurityAttributes.lpSecurityDescriptor = NULL;
  394. //
  395. // Setup the redirection.
  396. //
  397. if ( !(pszNewDeviceName = SetupDosDev(pRedirectInfo->pIniPort,
  398. szPipeName,
  399. COUNTOF(szPipeName),
  400. &SecurityAttributes,
  401. &pSecurityAttributes)) ) {
  402. EnterSplSem();
  403. FreeRedirectInfo(pRedirectInfo);
  404. LeaveSplSem();
  405. return FALSE;
  406. }
  407. //
  408. // Initialization
  409. //
  410. for (i = 0; i < NUMBER_OF_PIPE_INSTANCES; i++) {
  411. hPipe[i] = INVALID_HANDLE_VALUE;
  412. Overlapped[i].hEvent = ahEvent[i] = NULL;
  413. }
  414. //
  415. // Put the event in the extra member of the event array.
  416. //
  417. ahEvent[NUMBER_OF_PIPE_INSTANCES] = pRedirectInfo->hEvent;
  418. //
  419. // Create several instances of a named pipe, create an event for each,
  420. // and connect to wait for a client:
  421. //
  422. for (i = 0; i < NUMBER_OF_PIPE_INSTANCES; i++) {
  423. abReconnect[i] = FALSE;
  424. hPipe[i] = CreateNamedPipe(szPipeName,
  425. PIPE_ACCESS_DUPLEX |
  426. FILE_FLAG_OVERLAPPED,
  427. PIPE_WAIT |
  428. PIPE_READMODE_BYTE |
  429. PIPE_TYPE_BYTE,
  430. PIPE_UNLIMITED_INSTANCES,
  431. 4096,
  432. 64*1024, // 64k
  433. 0,
  434. pSecurityAttributes);
  435. if ( hPipe[i] == INVALID_HANDLE_VALUE ) {
  436. DBGMSG(DBG_ERROR, ("CreateNamedPipe failed for %ws. Error %d\n",
  437. szPipeName, GetLastError()));
  438. goto Cleanup;
  439. }
  440. ahEvent[i] = Overlapped[i].hEvent = CreateEvent(NULL,
  441. FALSE,
  442. FALSE,
  443. NULL);
  444. if (!ahEvent[i]) {
  445. DBGMSG(DBG_ERROR, ("CreateEvent failed. Error %d\n",
  446. GetLastError()));
  447. goto Cleanup;
  448. }
  449. if (!ConnectNamedPipe(hPipe[i], &Overlapped[i])){
  450. Error = GetLastError();
  451. if (Error == ERROR_IO_PENDING) {
  452. DBGMSG(DBG_INFO, ("ConnectNamedPipe %d, IO pending\n",
  453. i));
  454. } else {
  455. DBGMSG(DBG_ERROR, ("ConnectNamedPipe failed. Error %d\n",
  456. GetLastError()));
  457. goto Cleanup;
  458. }
  459. }
  460. }
  461. while (TRUE) {
  462. DBGMSG(DBG_INFO, ("Waiting to connect...\n"));
  463. WaitResult = WaitForMultipleObjectsEx(NUMBER_OF_PIPE_INSTANCES + 1,
  464. ahEvent,
  465. FALSE,
  466. INFINITE,
  467. TRUE);
  468. DBGMSG(DBG_INFO, ("WaitForMultipleObjectsEx returned %d\n",
  469. WaitResult));
  470. if ((WaitResult >= NUMBER_OF_PIPE_INSTANCES)
  471. && (WaitResult != WAIT_IO_COMPLETION)) {
  472. DBGMSG(DBG_INFO, ("WaitForMultipleObjects returned %d; Last error = %d\n",
  473. WaitResult,
  474. GetLastError( ) ) );
  475. //
  476. // We need to terminate. But wait for any read thread that is spun
  477. // off by this redirection thread
  478. //
  479. for ( i = 0 ; i < NUMBER_OF_PIPE_INSTANCES ; ++i )
  480. if ( abReconnect[i] ) {
  481. bTerminate = TRUE;
  482. break; // the for loop
  483. }
  484. if ( i < NUMBER_OF_PIPE_INSTANCES )
  485. continue; // for the while loop
  486. else
  487. goto Cleanup;
  488. }
  489. i = WaitResult;
  490. //
  491. // If disco and reconnect was pending, do it again here.
  492. //
  493. if (abReconnect[i]) {
  494. abReconnect[i] = FALSE;
  495. //
  496. // If redirection thread has been told to quit check for termination
  497. //
  498. if ( bTerminate ) {
  499. for ( j = 0 ; j < NUMBER_OF_PIPE_INSTANCES ; ++j )
  500. if ( abReconnect[j] )
  501. break; // the for loop
  502. if ( j < NUMBER_OF_PIPE_INSTANCES )
  503. continue; // for while loop
  504. else
  505. goto Cleanup;
  506. } else {
  507. ReconnectNamedPipe(hPipe[i], &Overlapped[i]);
  508. continue;
  509. }
  510. }
  511. //
  512. // If we have been told to terminate do not spin a read thread
  513. //
  514. if ( bTerminate )
  515. continue;
  516. //
  517. // Set up the transmission structure with the handles etc. needed by
  518. // the completion callback routine:
  519. //
  520. pTransmission = (PTRANSMISSION)AllocSplMem(sizeof(TRANSMISSION));
  521. if (pTransmission) {
  522. pTransmission->hPipe = hPipe[i];
  523. pTransmission->pOverlapped = &Overlapped[i];
  524. pTransmission->hPrinter = NULL;
  525. pTransmission->pIniPort = pRedirectInfo->pIniPort;
  526. abReconnect[i] = TRUE;
  527. hThread = CreateThread(NULL,
  528. INITIAL_STACK_COMMIT,
  529. (LPTHREAD_START_ROUTINE)ReadThread,
  530. pTransmission,
  531. 0,
  532. &dwThreadId);
  533. if (hThread) {
  534. CloseHandle(hThread);
  535. } else {
  536. abReconnect[i] = FALSE;
  537. FreeSplMem(pTransmission);
  538. DBGMSG(DBG_WARN, ("CreateThread failed. Error %d\n",
  539. GetLastError()));
  540. }
  541. } else {
  542. DBGMSG(DBG_WARN, ("Alloc failed. Error %d\n",
  543. GetLastError()));
  544. }
  545. }
  546. Cleanup:
  547. if ( pszNewDeviceName ) {
  548. wcscpy(DosDeviceName, pRedirectInfo->pIniPort->pName);
  549. RemoveColon(DosDeviceName);
  550. DefineDosDevice(DDD_REMOVE_DEFINITION |
  551. DDD_EXACT_MATCH_ON_REMOVE |
  552. DDD_RAW_TARGET_PATH,
  553. DosDeviceName,
  554. pszNewDeviceName);
  555. FreeSplStr(pszNewDeviceName);
  556. }
  557. EnterSplSem();
  558. FreeRedirectInfo(pRedirectInfo);
  559. LeaveSplSem();
  560. for (i = 0; i < NUMBER_OF_PIPE_INSTANCES; i++) {
  561. if ( hPipe[i] != INVALID_HANDLE_VALUE ) {
  562. CloseHandle(hPipe[i]);
  563. hPipe[i] = INVALID_HANDLE_VALUE;
  564. }
  565. if ( ahEvent[i] ) {
  566. CloseHandle(ahEvent[i]);
  567. ahEvent[i] = NULL;
  568. Overlapped[i].hEvent = NULL;
  569. }
  570. }
  571. if (SecurityAttributes.lpSecurityDescriptor)
  572. DestroyPrivateObjectSecurity(&SecurityAttributes.lpSecurityDescriptor);
  573. return TRUE;
  574. }
  575. BOOL
  576. CreateRedirectionThread(
  577. PINIPORT pIniPort)
  578. {
  579. HANDLE hThread;
  580. DWORD dwThreadId;
  581. PREDIRECT_INFO pRedirectInfo = NULL;
  582. SplInSem();
  583. SPLASSERT(pIniPort->hEvent == NULL);
  584. //
  585. // Create redirection thread only once and only for LPT and COM ports
  586. //
  587. if ( !IsPortType(pIniPort->pName, szLPT) &&
  588. !IsPortType(pIniPort->pName, szCOM) ) {
  589. return TRUE;
  590. }
  591. pIniPort->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  592. pRedirectInfo = (PREDIRECT_INFO) AllocSplMem(sizeof(REDIRECT_INFO));
  593. if ( !pIniPort->hEvent || !pRedirectInfo ) {
  594. FreeSplMem(pRedirectInfo);
  595. if ( pIniPort->hEvent ) {
  596. CloseHandle(pIniPort->hEvent);
  597. pIniPort->hEvent = NULL;
  598. }
  599. return FALSE;
  600. }
  601. INCPORTREF(pIniPort);
  602. pRedirectInfo->pIniPort = pIniPort;
  603. pRedirectInfo->hEvent = pIniPort->hEvent;
  604. hThread = CreateThread(NULL,
  605. INITIAL_STACK_COMMIT,
  606. (LPTHREAD_START_ROUTINE)RedirectionThread,
  607. pRedirectInfo,
  608. 0,
  609. &dwThreadId);
  610. if (hThread) {
  611. CloseHandle(hThread);
  612. } else {
  613. pIniPort->hEvent = NULL;
  614. FreeRedirectInfo(pRedirectInfo);
  615. return FALSE;
  616. }
  617. return TRUE;
  618. }