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.

2189 lines
50 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. winsmsc.c
  5. Abstract:
  6. This module contains miscellanous functions that in general are used by
  7. more than one component of WINS. Some of the functions in this module are
  8. wrappers for WIN32 api functions. These wrappers serve to isolate
  9. WINS code from changes in the WIN32 api.
  10. Functions:
  11. WinsMscAlloc
  12. WinsMscDealloc
  13. WinsMscFreeMem
  14. WinsMscWaitInfinite
  15. WinsMscWaitTimed
  16. WinsMscCreateEvt
  17. WinsMscSetUpThd
  18. WinsMscWaitUntilSignaled
  19. WinsMscWaitTimedUntilSignaled
  20. WinsMscHeapAlloc
  21. WinsMscHeapFree
  22. WinsMscHeapCreate
  23. WinsMscHeapDestroy
  24. WinsMscTermThd
  25. WinsMscSignalHdl
  26. WinsMscResetHdl
  27. WinsMscCloseHdl
  28. WinsMscCreateThd
  29. WinsMscSetThdPriority
  30. WinsMscOpenFile
  31. WinsMscMapFile
  32. Portability:
  33. This module is portable
  34. Author:
  35. Pradeep Bahl (PradeepB) Dec-1992
  36. Revision History:
  37. Modification date Person Description of modification
  38. ----------------- ------- ----------------------------
  39. --*/
  40. #include <string.h>
  41. #if 0
  42. //
  43. // The following is just for the system call
  44. //
  45. #ifdef WINSDBG
  46. #include <process.h>
  47. #include <stdlib.h>
  48. #endif
  49. #endif
  50. #include "wins.h"
  51. #include "nms.h"
  52. #include "nmsdb.h"
  53. #include "winsmsc.h"
  54. #include "winscnf.h"
  55. #include "winstmm.h"
  56. #include "winsevt.h"
  57. #include "winsque.h"
  58. #include "winsprs.h"
  59. #include "winsdbg.h"
  60. /*
  61. * Local Macro Declarations
  62. */
  63. #define PERCENT_CHAR TEXT('%')
  64. /*
  65. * Local Typedef Declarations
  66. */
  67. /*
  68. * Global Variable Definitions
  69. */
  70. /*
  71. * Local Variable Definitions
  72. */
  73. /*
  74. * Local Function Prototype Declarations
  75. */
  76. /* prototypes for functions local to this module go here */
  77. __inline
  78. VOID
  79. WinsMscAlloc(
  80. IN DWORD BuffSize,
  81. OUT LPVOID *ppRspBuff
  82. )
  83. /*++
  84. Routine Description:
  85. This function is called to allocate memory.
  86. Arguments:
  87. BuffSize - Size of buffer to allocate
  88. ppRspBuff - Buffer allocated
  89. Externals Used:
  90. None
  91. Return Value:
  92. None
  93. Error Handling:
  94. Called by:
  95. NmsDbGetDataRecs, GetGroupMembers
  96. Side Effects:
  97. Comments:
  98. None
  99. --*/
  100. {
  101. FUTURES("Change this function into a macro")
  102. *ppRspBuff = WinsMscHeapAlloc(GenBuffHeapHdl, BuffSize);
  103. return;
  104. }
  105. __inline
  106. VOID
  107. WinsMscDealloc(
  108. IN LPVOID pBuff
  109. )
  110. /*++
  111. Routine Description:
  112. This function frees memory allocated via NmsDbAlloc
  113. Arguments:
  114. pBuff -- Buffer to deallocate
  115. Externals Used:
  116. None
  117. Return Value:
  118. None
  119. Error Handling:
  120. Called by:
  121. Side Effects:
  122. Comments:
  123. None
  124. --*/
  125. {
  126. FUTURES("Change this function into a macro")
  127. WinsMscHeapFree(GenBuffHeapHdl, pBuff);
  128. return;
  129. }
  130. VOID
  131. WinsMscFreeMem(
  132. IN PWINS_MEM_T pWinsMem
  133. )
  134. /*++
  135. Routine Description:
  136. This function is called to free memory that is pointed to by one
  137. or more pointers in the pWinsMem array
  138. This function is called from all those functions that allocate
  139. memory or that acquire memory allocated by called function via
  140. OUT args of those "memory allocating" called functions.
  141. Arguments:
  142. pWinsMem - ptr to an array of buffers to deallocate
  143. Externals Used:
  144. None
  145. Return Value:
  146. Success status codes --
  147. Error status codes --
  148. Error Handling:
  149. Called by:
  150. Side Effects:
  151. Comments:
  152. pWinsMem array should end with a NULL pointer
  153. --*/
  154. {
  155. if (pWinsMem != NULL)
  156. {
  157. for (; pWinsMem->pMem != NULL; pWinsMem++)
  158. {
  159. WinsMscDealloc(pWinsMem->pMem);
  160. }
  161. }
  162. return;
  163. }
  164. VOID
  165. WinsMscWaitInfinite(
  166. IN HANDLE Hdl
  167. )
  168. /*++
  169. Routine Description:
  170. The function is called to wait on a handle until signalled.
  171. Arguments:
  172. Hdl -- handle to wait on until signaled
  173. Externals Used:
  174. None
  175. Return Value:
  176. Success status codes --
  177. Error status codes --
  178. Error Handling:
  179. Called by:
  180. Side Effects:
  181. Comments:
  182. None
  183. --*/
  184. {
  185. DWORD RetVal = WAIT_OBJECT_0;
  186. /*
  187. The function should return only if the handle is in the signaled state
  188. */
  189. RetVal = WaitForSingleObject(Hdl, INFINITE);
  190. if (RetVal != WAIT_OBJECT_0)
  191. {
  192. WINS_RAISE_EXC_M(WINS_EXC_ABNORMAL_TERM);
  193. }
  194. return;
  195. }
  196. VOID
  197. WinsMscWaitTimed(
  198. IN HANDLE Hdl,
  199. IN DWORD TimeOut,
  200. OUT LPBOOL pfSignaled
  201. )
  202. /*++
  203. Routine Description:
  204. The function is called to wait on a handle until signalled.
  205. Arguments:
  206. Hdl - handle to wait on until signaled
  207. TimeOut - TIme for which the wait has to be done
  208. pfSIgnaled - Indicates whether the hdl got signaled.
  209. Externals Used:
  210. None
  211. Return Value:
  212. None
  213. Error Handling:
  214. Called by:
  215. Side Effects:
  216. Comments:
  217. None
  218. --*/
  219. {
  220. DWORD RetVal = WAIT_OBJECT_0;
  221. *pfSignaled = TRUE;
  222. /*
  223. The function should return only if the handle is in the signaled state
  224. */
  225. RetVal = WaitForSingleObject(Hdl, TimeOut);
  226. if (RetVal == WAIT_ABANDONED)
  227. {
  228. WINS_RAISE_EXC_M(WINS_EXC_ABNORMAL_TERM);
  229. }
  230. if (RetVal == WAIT_TIMEOUT)
  231. {
  232. if (TimeOut == INFINITE)
  233. {
  234. WINS_RAISE_EXC_M(WINS_EXC_ABNORMAL_TERM);
  235. }
  236. else
  237. {
  238. *pfSignaled = FALSE;
  239. }
  240. }
  241. return;
  242. }
  243. VOID
  244. WinsMscCreateEvt(
  245. IN LPTSTR pName,
  246. IN BOOL fManualReset,
  247. IN PHANDLE pHdl
  248. )
  249. /*++
  250. Routine Description:
  251. This function creates an event with the specified name
  252. Arguments:
  253. pName - Name of Event to create
  254. fManualReset - Flag indicating whether it is a manual reset event
  255. pHdl - Handle to Event created
  256. Externals Used:
  257. None
  258. Return Value:
  259. None
  260. Error Handling:
  261. Called by:
  262. Init in nms.c
  263. Side Effects:
  264. Comments:
  265. None
  266. --*/
  267. {
  268. DWORD Error;
  269. *pHdl = CreateEvent(
  270. NULL, //default security attributes
  271. fManualReset, //auto reset event
  272. FALSE, //Not signalled initially
  273. pName // name
  274. );
  275. if (*pHdl == NULL)
  276. {
  277. Error = GetLastError();
  278. WINSEVT_LOG_M(WINS_FATAL_ERR, WINS_EVT_CANT_CREATE_EVT);
  279. WINS_RAISE_EXC_M(WINS_EXC_OUT_OF_RSRCS);
  280. }
  281. return;
  282. }
  283. STATUS
  284. WinsMscSetUpThd(
  285. PQUE_HD_T pQueHd,
  286. LPTHREAD_START_ROUTINE pThdInitFn,
  287. LPVOID pParam,
  288. PHANDLE pThdHdl,
  289. LPDWORD pThdId
  290. )
  291. /*++
  292. Routine Description:
  293. This function initializes a queue and its critical section, creates
  294. an event and a thread to wait on that event. The event is signaled
  295. whenever a work item is put on the queue.
  296. Arguments:
  297. pQueHd - Head of queue to be monitored by the thread
  298. pThdInitFn - Startup function of the thread
  299. pParam - param to be passed to the startup function
  300. pThdhdl - Hdl of thread created by this function
  301. pThdId - Id of thread created by this function
  302. Externals Used:
  303. None
  304. Return Value:
  305. Success status codes -- WINS_SUCCESS
  306. Error status codes -- none currently
  307. Error Handling:
  308. Called by:
  309. WinsTmmInit, RplInit, NmsChlInit
  310. Side Effects:
  311. Comments:
  312. None
  313. --*/
  314. {
  315. DWORD ThdId;
  316. /*
  317. * Initialize the critical section that protects the work queue of
  318. * the Pull thread
  319. */
  320. InitializeCriticalSection(&pQueHd->CrtSec);
  321. /*
  322. * Initialize the listhead for the pull thread's queue
  323. */
  324. InitializeListHead(&pQueHd->Head);
  325. /*
  326. * Create an auto-reset event for the above queue
  327. */
  328. WinsMscCreateEvt(
  329. NULL, //create without name
  330. FALSE, //auto-reser var
  331. &pQueHd->EvtHdl
  332. );
  333. /*
  334. Create the thread
  335. */
  336. *pThdHdl = WinsMscCreateThd(
  337. pThdInitFn,
  338. pParam,
  339. &ThdId
  340. );
  341. if (pThdId != NULL)
  342. {
  343. *pThdId = ThdId;
  344. }
  345. return(WINS_SUCCESS);
  346. }
  347. VOID
  348. WinsMscWaitUntilSignaled(
  349. LPHANDLE pHdlArray,
  350. DWORD NoOfHdls,
  351. LPDWORD pIndexOfHdlSignaled,
  352. BOOL fAlertable
  353. )
  354. /*++
  355. Routine Description:
  356. This function is called to wait on multiple handles, one of which
  357. is the handle which is signaled at termination time
  358. Arguments:
  359. pHdlArray - Array of handles to wait on
  360. NoOfHdls - No of hdls in the array
  361. pIndexOfHdlSignaled - Index of the hdl signaled
  362. Externals Used:
  363. None
  364. Return Value:
  365. None
  366. Error Handling:
  367. Called by:
  368. Side Effects:
  369. Comments:
  370. --*/
  371. {
  372. DWORD RetHdl;
  373. do {
  374. RetHdl = WaitForMultipleObjectsEx(
  375. NoOfHdls, //# of handles in the array
  376. pHdlArray, //array of handles
  377. FALSE, //return when any of the events
  378. //gets signaled
  379. INFINITE, //Infinite timeout
  380. fAlertable
  381. );
  382. DBGPRINT1(DET, "WinsMscWaitUntilSignaled. WaitForMultipleObjects returned (%d)\n", RetHdl);
  383. // if we got signaled due to IO completion queued on the thread
  384. // just go back and wait again
  385. } while (fAlertable && WAIT_IO_COMPLETION == RetHdl);
  386. if (RetHdl == 0xFFFFFFFF)
  387. {
  388. DBGPRINT1(EXC, "WinsMscWaitUntilSignaled. WaitForMultipleObjects returned error. Error = (%d)\n", GetLastError());
  389. WINS_RAISE_EXC_M(WINS_EXC_FATAL_ERR);
  390. }
  391. *pIndexOfHdlSignaled = RetHdl - WAIT_OBJECT_0;
  392. if (*pIndexOfHdlSignaled >= NoOfHdls)
  393. {
  394. WINS_RAISE_EXC_M(WINS_EXC_FATAL_ERR);
  395. }
  396. return;
  397. }
  398. VOID
  399. WinsMscWaitTimedUntilSignaled(
  400. LPHANDLE pHdlArray,
  401. DWORD NoOfHdls,
  402. LPDWORD pIndexOfHdlSignaled,
  403. DWORD TimeOut,
  404. LPBOOL pfSignaled
  405. )
  406. /*++
  407. Routine Description:
  408. This function is called to wait on multiple handles, one of which
  409. is the handle which is signaled at termination time
  410. Arguments:
  411. pHdlArray - Array of handles to wait on
  412. NoOfHdls - No of handles in the array
  413. pIndexOfHdlSignaled - Index of handle signaled
  414. Timeout - Max time for which to do the wait
  415. pfSignaled - indicates whether a hdl was signaled
  416. Externals Used:
  417. None
  418. Return Value:
  419. None
  420. Error Handling:
  421. Called by:
  422. Side Effects:
  423. Comments:
  424. --*/
  425. {
  426. DWORD RetHdl = 0xFFFFFFFF;
  427. DWORD Error;
  428. int Index;
  429. *pfSignaled = TRUE;
  430. RetHdl = WaitForMultipleObjects(
  431. NoOfHdls, //# of handles in the array
  432. pHdlArray, //array of handles
  433. FALSE, //return when either event gets
  434. //signaled
  435. TimeOut //Infinite timeout
  436. );
  437. if (RetHdl == 0xFFFFFFFF)
  438. {
  439. Error = GetLastError();
  440. WINS_RAISE_EXC_M(WINS_EXC_FATAL_ERR);
  441. }
  442. if (RetHdl == WAIT_TIMEOUT)
  443. {
  444. *pfSignaled = FALSE;
  445. return;
  446. }
  447. Index = RetHdl - WAIT_OBJECT_0;
  448. if ((Index >= (int)NoOfHdls) || (Index < 0))
  449. {
  450. DBGPRINT1(EXC, "WinsMscWaitTimedUntilSignaled: Index of handle signaled (%d) is INVALID\n", Index);
  451. Index = RetHdl - WAIT_ABANDONED_0 ;
  452. if ((Index > 0) && (Index < (int)NoOfHdls))
  453. {
  454. DBGPRINT1(EXC, "WinsMscWaitTimedUntilSignaled: Index of handle in the abandoned state (%d)\n", Index);
  455. }
  456. WINS_RAISE_EXC_M(WINS_EXC_FATAL_ERR);
  457. }
  458. else
  459. {
  460. *pIndexOfHdlSignaled = Index;
  461. }
  462. return;
  463. }
  464. __inline
  465. LPVOID
  466. WinsMscHeapAlloc(
  467. IN HANDLE HeapHdl,
  468. IN DWORD Size
  469. )
  470. /*++
  471. Routine Description:
  472. The function returns with a buffer allocated from the specified heap
  473. Arguments:
  474. HeapHdl - Handle to the heap
  475. ppBuff - Buffer allocated
  476. Size - Size of Buffer
  477. Externals Used:
  478. None
  479. Return Value:
  480. Success status codes -- ptr to the allocated memory
  481. Error status codes -- NULL
  482. Error Handling:
  483. Called by:
  484. Side Effects:
  485. Comments:
  486. None
  487. --*/
  488. {
  489. LPVOID pBuff;
  490. #ifdef WINSDBG
  491. LPDWORD pHeapCntr;
  492. #endif
  493. //
  494. // Note: It is very important that the memory be initialized to zero
  495. // (for example, until we have longlong (LARGE INTEGER) support
  496. // in the db engine - JET, we will retrieve the version number
  497. // as a long data type and store it in the LowPart field of the
  498. // large integer storing the version number in our in-memory
  499. // data structure. The HighPart will be 0 by default due to
  500. // the initialization done at allocation time. This is what
  501. // we want.
  502. //
  503. //
  504. // if you pass a very large value for the size, HeapAlloc returns NULL
  505. // instead of raising an exception.
  506. //
  507. pBuff = (MSG_T)HeapAlloc(
  508. HeapHdl,
  509. HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,
  510. Size
  511. );
  512. DBGPRINT2(HEAP, "HeapAlloc: HeapHandle = (%p), pBuff = (%p)\n",
  513. HeapHdl, pBuff);
  514. #ifdef WINSDBG
  515. if (Size == 0)
  516. {
  517. DBGPRINT2(ERR, "WinsMscHeapAlloc: Size = 0; pBuff returned = (%p); HeapHdl = (%p)\n", pBuff, HeapHdl);
  518. }
  519. IF_DBG(HEAP_CNTRS)
  520. {
  521. if (HeapHdl == CommUdpBuffHeapHdl)
  522. {
  523. pHeapCntr = &NmsUdpHeapAlloc;
  524. } else if (HeapHdl == CommUdpDlgHeapHdl)
  525. {
  526. pHeapCntr = &NmsUdpDlgHeapAlloc;
  527. } else if (HeapHdl == CommAssocDlgHeapHdl)
  528. {
  529. pHeapCntr = &NmsDlgHeapAlloc;
  530. } else if (HeapHdl == CommAssocTcpMsgHeapHdl)
  531. {
  532. pHeapCntr = &NmsTcpMsgHeapAlloc;
  533. } else if (HeapHdl == GenBuffHeapHdl)
  534. {
  535. pHeapCntr = &NmsGenHeapAlloc;
  536. } else if (HeapHdl == QueBuffHeapHdl)
  537. {
  538. pHeapCntr = &NmsQueHeapAlloc;
  539. } else if (HeapHdl == NmsChlHeapHdl)
  540. {
  541. pHeapCntr = &NmsChlHeapAlloc;
  542. } else if (HeapHdl == CommAssocAssocHeapHdl)
  543. {
  544. pHeapCntr = &NmsAssocHeapAlloc;
  545. } else if (HeapHdl == RplWrkItmHeapHdl)
  546. {
  547. pHeapCntr = &NmsRplWrkItmHeapAlloc;
  548. } else if (HeapHdl == NmsRpcHeapHdl)
  549. {
  550. pHeapCntr = &NmsRpcHeapAlloc;
  551. } else if (HeapHdl == WinsTmmHeapHdl)
  552. {
  553. pHeapCntr = &NmsTmmHeapAlloc;
  554. } else
  555. {
  556. DBGPRINT1(HEAP, "WinsMscHeapAlloc: HeapHdl = (%p)\n", HeapHdl);
  557. pHeapCntr = &NmsCatchAllHeapAlloc;
  558. }
  559. EnterCriticalSection(&NmsHeapCrtSec);
  560. (*pHeapCntr)++;
  561. LeaveCriticalSection(&NmsHeapCrtSec);
  562. }
  563. #endif
  564. return(pBuff);
  565. }
  566. __inline
  567. VOID
  568. WinsMscHeapFree(
  569. IN HANDLE HeapHdl,
  570. IN LPVOID pBuff
  571. )
  572. /*++
  573. Routine Description:
  574. This function deallocates the memory pointed to by pBuff from the
  575. specified heap
  576. Arguments:
  577. HeapHdl - Handle to the heap
  578. pBuff - Buffer to deallocate
  579. Externals Used:
  580. Return Value:
  581. None
  582. Error Handling:
  583. Called by:
  584. Side Effects:
  585. Comments:
  586. None
  587. --*/
  588. {
  589. DWORD Error;
  590. BOOL fStatus;
  591. #ifdef WINSDBG
  592. LPDWORD pHeapCntr;
  593. #endif
  594. DBGPRINT2(HEAP, "HeapFree: HeapHandle = (%p), pBuff = (%p)\n",
  595. HeapHdl, pBuff);
  596. fStatus = HeapFree(
  597. HeapHdl,
  598. 0, //we want mutual exclusion
  599. pBuff
  600. );
  601. if (!fStatus)
  602. {
  603. Error = GetLastError();
  604. WINSEVT_LOG_M(Error, WINS_EVT_HEAP_ERROR);
  605. WINS_RAISE_EXC_M(WINS_EXC_HEAP_FREE_ERR);
  606. }
  607. #ifdef WINSDBG
  608. IF_DBG(HEAP_CNTRS)
  609. {
  610. if (HeapHdl == CommUdpBuffHeapHdl)
  611. {
  612. pHeapCntr = &NmsUdpHeapFree;
  613. } else if (HeapHdl == CommUdpDlgHeapHdl)
  614. {
  615. pHeapCntr = &NmsUdpDlgHeapFree;
  616. } else if (HeapHdl == CommAssocDlgHeapHdl)
  617. {
  618. pHeapCntr = &NmsDlgHeapFree;
  619. } else if (HeapHdl == CommAssocTcpMsgHeapHdl)
  620. {
  621. pHeapCntr = &NmsTcpMsgHeapFree;
  622. } else if (HeapHdl == GenBuffHeapHdl)
  623. {
  624. pHeapCntr = &NmsGenHeapFree;
  625. } else if (HeapHdl == QueBuffHeapHdl)
  626. {
  627. pHeapCntr = &NmsQueHeapFree;
  628. } else if (HeapHdl == NmsChlHeapHdl)
  629. {
  630. pHeapCntr = &NmsChlHeapFree;
  631. } else if (HeapHdl == CommAssocAssocHeapHdl)
  632. {
  633. pHeapCntr = &NmsAssocHeapFree;
  634. } else if (HeapHdl == RplWrkItmHeapHdl)
  635. {
  636. pHeapCntr = &NmsRplWrkItmHeapFree;
  637. } else if (HeapHdl == NmsRpcHeapHdl)
  638. {
  639. pHeapCntr = &NmsRpcHeapFree;
  640. } else if (HeapHdl == WinsTmmHeapHdl)
  641. {
  642. pHeapCntr = &NmsTmmHeapFree;
  643. } else
  644. {
  645. DBGPRINT1(HEAP, "WinsMscHeapFree: HeapHdl = (%p)\n", HeapHdl);
  646. pHeapCntr = &NmsCatchAllHeapFree;
  647. }
  648. EnterCriticalSection(&NmsHeapCrtSec);
  649. (*pHeapCntr)++;
  650. LeaveCriticalSection(&NmsHeapCrtSec);
  651. }
  652. #endif
  653. return;
  654. }
  655. HANDLE
  656. WinsMscHeapCreate(
  657. IN DWORD Options,
  658. IN DWORD InitSize
  659. )
  660. /*++
  661. Routine Description:
  662. This function creates a heap with the specified options
  663. Arguments:
  664. Options -- Options for the HeapCreate function (Example: whether or
  665. not to enable mutual exclusion)
  666. InitSize -- Initial Size of the heap (committed memory size)
  667. Externals Used:
  668. None
  669. Return Value:
  670. Success status codes -- Hdl to heap
  671. Error status codes --
  672. Error Handling:
  673. Called by:
  674. Side Effects:
  675. Comments:
  676. None
  677. --*/
  678. {
  679. DWORD Error;
  680. HANDLE HeapHdl;
  681. HeapHdl = HeapCreate(
  682. Options,
  683. InitSize,
  684. 0 //limited only by available memory
  685. );
  686. if (HeapHdl == NULL)
  687. {
  688. Error = GetLastError();
  689. DBGPRINT0(HEAP, "Cant create heap\n");
  690. WINSEVT_LOG_M(Error, WINS_EVT_CANT_CREATE_HEAP);
  691. WINS_RAISE_EXC_M(WINS_EXC_HEAP_CREATE_ERR);
  692. }
  693. #ifdef WINSDBG
  694. IF_DBG(HEAP_CNTRS)
  695. {
  696. DBGPRINT1(HEAP_CRDL, "HeapCreate: HeapHandle = (%p)\n", HeapHdl);
  697. EnterCriticalSection(&NmsHeapCrtSec);
  698. NmsHeapCreate++;
  699. LeaveCriticalSection(&NmsHeapCrtSec);
  700. }
  701. #endif
  702. return(HeapHdl);
  703. }
  704. VOID
  705. WinsMscHeapDestroy(
  706. HANDLE HeapHdl
  707. )
  708. /*++
  709. Routine Description:
  710. This is a wrapper for the HeapDestroy function
  711. Arguments:
  712. HeapHdl - Handle to heap to destroy
  713. Externals Used:
  714. None
  715. Return Value:
  716. None
  717. Error Handling:
  718. Called by:
  719. WrapUp() in nms.c
  720. Side Effects:
  721. Comments:
  722. None
  723. --*/
  724. {
  725. BOOL fRetVal;
  726. fRetVal = HeapDestroy(HeapHdl);
  727. ASSERT(fRetVal);
  728. #ifdef WINSDBG
  729. if (!fRetVal)
  730. {
  731. DBGPRINT1(ERR, "HeapDestroy: FAILED -- HeapHandle used = (%p)\n", HeapHdl);
  732. }
  733. else
  734. {
  735. IF_DBG(HEAP_CNTRS)
  736. {
  737. if (HeapHdl == CommUdpBuffHeapHdl)
  738. {
  739. DBGPRINT0(HEAP_CRDL, "Udp Buff heap\n");
  740. } else if (HeapHdl == CommAssocDlgHeapHdl)
  741. {
  742. DBGPRINT0(HEAP_CRDL, "Dlg Buff heap\n");
  743. } else if (HeapHdl == GenBuffHeapHdl)
  744. {
  745. DBGPRINT0(HEAP_CRDL, "Gen Buff heap\n");
  746. } else if (HeapHdl == QueBuffHeapHdl)
  747. {
  748. DBGPRINT0(HEAP_CRDL, "Que Buff heap\n");
  749. } else if (HeapHdl == NmsChlHeapHdl)
  750. {
  751. DBGPRINT0(HEAP_CRDL, "Chl Buff heap\n");
  752. } else if (HeapHdl == CommAssocAssocHeapHdl)
  753. {
  754. DBGPRINT0(HEAP_CRDL, "Assoc Buff heap\n");
  755. } else if (HeapHdl == RplWrkItmHeapHdl)
  756. {
  757. DBGPRINT0(HEAP_CRDL, "Rpl Work Item heap\n");
  758. } else if (HeapHdl == NmsRpcHeapHdl)
  759. {
  760. DBGPRINT0(HEAP_CRDL, "Rpc Work Item heap\n");
  761. } else if (HeapHdl == WinsTmmHeapHdl)
  762. {
  763. DBGPRINT0(HEAP_CRDL, "Tmm Work Item heap\n");
  764. } else
  765. {
  766. static DWORD sAdjust = 0;
  767. DBGPRINT0(HEAP_CRDL, "Catchall Work Item heap\n");
  768. EnterCriticalSection(&NmsHeapCrtSec);
  769. if (((NmsHeapCreate - NmsHeapDestroy) == 12) && (NmsCatchAllHeapAlloc > (NmsCatchAllHeapFree + sAdjust)))
  770. {
  771. PWINSTHD_TLS_T pTls;
  772. pTls = TlsGetValue(WinsTlsIndex);
  773. if (pTls == NULL)
  774. {
  775. DBGPRINT1(ERR, "WinsMscHeapDestroy: Could not get TLS. GetLastError() = (%d)\n", GetLastError());
  776. }
  777. else
  778. {
  779. DBGPRINT4(ERR, "WinsMscHeapDestroy: %s thd noticed a mismatch between allocs (%d) and frees (%d). Free count was adjusted by (%d)\n", pTls->ThdName, NmsCatchAllHeapAlloc, NmsCatchAllHeapFree, sAdjust);
  780. sAdjust = NmsCatchAllHeapAlloc - NmsCatchAllHeapFree;
  781. //system("net send pradeepb MISMATCH");
  782. }
  783. }
  784. LeaveCriticalSection(&NmsHeapCrtSec);
  785. }
  786. DBGPRINT1(HEAP_CRDL, "HeapDestroy: HeapHandle = (%p)\n", HeapHdl);
  787. EnterCriticalSection(&NmsHeapCrtSec);
  788. NmsHeapDestroy++;
  789. LeaveCriticalSection(&NmsHeapCrtSec);
  790. }
  791. }
  792. #endif
  793. return;
  794. } //WinsMscHeapDestroy
  795. VOID
  796. WinsMscTermThd(
  797. IN STATUS ExitStatus,
  798. IN DWORD DbSessionExistent
  799. )
  800. /*++
  801. Routine Description:
  802. This function is called to terminate the thread.
  803. The function does the necessary cleanup and exit.
  804. Arguments:
  805. ExitStatus - Status to exit with
  806. DbSessionExistent - indicates whether DB session is existent
  807. Externals Used:
  808. None
  809. Return Value:
  810. Thread is exited
  811. Error Handling:
  812. Called by:
  813. Side Effects:
  814. Comments:
  815. None
  816. --*/
  817. {
  818. DBGPRINT0(FLOW, "Enter: WinsMscTermThd\n");
  819. /*
  820. * End the database session if it exists. Decrement the count of
  821. * threads. If it reaches 1, signal the main thread so that it can
  822. * terminate itself. At the end terminate yourself.
  823. *
  824. */
  825. //
  826. // I could enter the critical section after the if block but it is
  827. // not important. This way, I get the prints in the right
  828. // order.
  829. //
  830. EnterCriticalSection(&NmsTermCrtSec);
  831. if (DbSessionExistent == WINS_DB_SESSION_EXISTS)
  832. {
  833. try {
  834. if (ExitStatus == WINS_SUCCESS)
  835. {
  836. DBGPRINT0(FLOW, "Ending the db session for thd -- ");
  837. }
  838. else
  839. {
  840. DBGPRINT0(ERR, "Ending the db session for thd -- ");
  841. }
  842. DBGPRINTNAME;
  843. DBGPRINT0(FLOW,"\n");
  844. //for now, we don't check the return value
  845. NmsDbEndSession();
  846. }
  847. except(EXCEPTION_EXECUTE_HANDLER) {
  848. DBGPRINTEXC("WinsMscTermThd");
  849. }
  850. } // end of if block
  851. //
  852. // If the total thread count after decrementing is 1, it means
  853. // that after we exit from this thread, only the main thread
  854. // will be left. Let us signal it to inform it of this situation.
  855. //
  856. // If the exit status is not success, it means that we have enetered
  857. // this function as a result of a fatal error/exception. We need
  858. // to signal the main thread to kick off the process termination
  859. //
  860. if ((--NmsTotalTrmThdCnt == 1) || (ExitStatus != WINS_SUCCESS))
  861. {
  862. DBGPRINT1(FLOW, "Signaling the main thread. Exit status = (%x)\n",
  863. ExitStatus);
  864. if (!SetEvent(NmsMainTermEvt))
  865. {
  866. WINSEVT_LOG_M(WINS_FATAL_ERR, WINS_EVT_CANT_SIGNAL_MAIN_THD);
  867. }
  868. }
  869. //
  870. // If NmsTotalTrmThdCnt reached 1 above, then the main thread will
  871. // exit invalidating the NmsTermCrtSec. We may get an INVALID_HANDLE
  872. // exception. If we get it, it is ok.
  873. //
  874. try {
  875. LeaveCriticalSection(&NmsTermCrtSec);
  876. }
  877. except(EXCEPTION_EXECUTE_HANDLER) {
  878. if (GetExceptionCode() == STATUS_INVALID_HANDLE)
  879. {
  880. DBGPRINT1(FLOW, "WinsMscTermThd: LAST THREAD. NmsTotalTrmThdCnt = (%d)\n", NmsTotalTrmThdCnt);
  881. }
  882. else
  883. {
  884. WINS_RERAISE_EXC_M();
  885. }
  886. }
  887. DBGPRINT0(FLOW, "EXITING the thread\n");
  888. ExitThread(ExitStatus);
  889. return;
  890. }
  891. VOID
  892. WinsMscSignalHdl(
  893. IN HANDLE Hdl
  894. )
  895. /*++
  896. Routine Description:
  897. This function is a wrapper for the WIN32 SignalEvent function
  898. Arguments:
  899. Hdl - Handle to signal
  900. Externals Used:
  901. None
  902. Return Value:
  903. None
  904. Error Handling:
  905. Called by:
  906. Side Effects:
  907. Comments:
  908. None
  909. --*/
  910. {
  911. if (!SetEvent(Hdl))
  912. {
  913. WINSEVT_LOG_M(WINS_FATAL_ERR, WINS_EVT_CANT_SIGNAL_HDL);
  914. WINS_RAISE_EXC_M(WINS_EXC_FATAL_ERR);
  915. }
  916. return;
  917. }
  918. VOID
  919. WinsMscResetHdl(
  920. IN HANDLE Hdl
  921. )
  922. /*++
  923. Routine Description:
  924. This function is a wrapper for the WIN32 ResetEvent function
  925. Arguments:
  926. Hdl - Handle to signal
  927. Externals Used:
  928. None
  929. Return Value:
  930. None
  931. Error Handling:
  932. Called by:
  933. Side Effects:
  934. Comments:
  935. Change to a macro
  936. --*/
  937. {
  938. if (!ResetEvent(Hdl))
  939. {
  940. WINSEVT_LOG_M(WINS_FATAL_ERR, WINS_EVT_CANT_RESET_HDL);
  941. WINS_RAISE_EXC_M(WINS_EXC_FATAL_ERR);
  942. }
  943. return;
  944. }
  945. VOID
  946. WinsMscCloseHdl (
  947. HANDLE Hdl
  948. )
  949. /*++
  950. Routine Description:
  951. This function is a wrapper for the WIN32 CloseHandle function
  952. Arguments:
  953. Hdl - Handle to close
  954. Externals Used:
  955. None
  956. Return Value:
  957. None
  958. Error Handling:
  959. Called by:
  960. Side Effects:
  961. Comments:
  962. change to a macro
  963. --*/
  964. {
  965. BOOL fRet;
  966. fRet = CloseHandle(Hdl);
  967. if(!fRet)
  968. {
  969. DBGPRINT0(ERR, "WinsMscCloseHdl:Could not close handle\n");
  970. WINS_RAISE_EXC_M(WINS_EXC_FATAL_ERR);
  971. }
  972. return;
  973. }
  974. HANDLE
  975. WinsMscCreateThd(
  976. IN LPTHREAD_START_ROUTINE pThdInitFn,
  977. IN LPVOID pParam,
  978. OUT LPDWORD pThdId
  979. )
  980. /*++
  981. Routine Description:
  982. This function is a wrapper around the WIN32 Create Thread function
  983. Arguments:
  984. pThdInitFn - Thread startup function
  985. pParam - Param to be passed to the startup function
  986. pThdId - Thd Id
  987. Externals Used:
  988. None
  989. Return Value:
  990. None
  991. Error Handling:
  992. Called by:
  993. Side Effects:
  994. Comments:
  995. None
  996. --*/
  997. {
  998. HANDLE ThdHdl; //Thread handle
  999. DWORD Error;
  1000. /*
  1001. * Create a thread with no sec attributes (i.e. it will take the
  1002. * security attributes of the process), and default stack size
  1003. */
  1004. ThdHdl = CreateThread(
  1005. NULL, /*no sec. attrbutes*/
  1006. 0, /*default stack size*/
  1007. pThdInitFn,
  1008. pParam, /*arg*/
  1009. 0, /*run it now*/
  1010. pThdId
  1011. );
  1012. if (ThdHdl == NULL)
  1013. {
  1014. Error = GetLastError();
  1015. DBGPRINT1(ERR, "WinsMscCreateThd: Can not create thread. Error = (%d)\n",
  1016. Error);
  1017. WINSEVT_LOG_M( Error, WINS_EVT_CANT_CREATE_THD);
  1018. WINS_RAISE_EXC_M(WINS_EXC_OUT_OF_RSRCS);
  1019. }
  1020. return(ThdHdl);
  1021. }
  1022. VOID
  1023. WinsMscSetThreadPriority(
  1024. HANDLE ThdHdl,
  1025. int PrLvl
  1026. )
  1027. /*++
  1028. Routine Description:
  1029. This function is a wrapper for the "set thread priority" function
  1030. Arguments:
  1031. ThdHdl - Handle of thread whose priority needs to be set
  1032. PrLvl - New Prirority level
  1033. Externals Used:
  1034. None
  1035. Return Value:
  1036. None
  1037. Error Handling:
  1038. Called by:
  1039. DoScavenging in nmsscv.c
  1040. Side Effects:
  1041. Comments:
  1042. None
  1043. --*/
  1044. {
  1045. BOOL fRet;
  1046. DWORD Error;
  1047. //
  1048. // Set the priority
  1049. //
  1050. fRet = SetThreadPriority(
  1051. ThdHdl,
  1052. PrLvl
  1053. );
  1054. if (!fRet)
  1055. {
  1056. Error = GetLastError();
  1057. DBGPRINT1(ERR, "NmsScvInit: Could not lower the priority of the scavanmger thread. Error = (%d)\n", Error);
  1058. WINSEVT_LOG_M(WINS_FAILURE, WINS_EVT_UNABLE_TO_CHG_PRIORITY);
  1059. WINS_RAISE_EXC_M(WINS_EXC_FAILURE);
  1060. }
  1061. return;
  1062. }
  1063. BOOL
  1064. WinsMscOpenFile(
  1065. IN LPTCH pFileName,
  1066. IN DWORD StrType,
  1067. OUT LPHANDLE pFileHdl
  1068. )
  1069. /*++
  1070. Routine Description:
  1071. This is a wrapper for the WIN32 function to open an existing file
  1072. Arguments:
  1073. pFileName - Name of file
  1074. StrType - Indicates REG_EXPAND_SZ or REG_SZ
  1075. pFileHdl - handle to file if it could be opened
  1076. Externals Used:
  1077. None
  1078. Return Value:
  1079. Success status codes -- TRUE
  1080. Error status codes -- FALSE
  1081. Error Handling:
  1082. Called by:
  1083. Side Effects:
  1084. Comments:
  1085. None
  1086. --*/
  1087. {
  1088. DWORD Error;
  1089. SECURITY_ATTRIBUTES SecAtt;
  1090. TCHAR ExpandedFileName[WINS_MAX_FILENAME_SZ];
  1091. LPTCH pHoldFileName;
  1092. SecAtt.nLength = sizeof(SecAtt);
  1093. SecAtt.lpSecurityDescriptor = NULL; //use default security descriptor
  1094. SecAtt.bInheritHandle = FALSE; //actually don't care
  1095. if (!WinsMscGetName(StrType, pFileName, ExpandedFileName,
  1096. WINS_MAX_FILENAME_SZ, &pHoldFileName))
  1097. {
  1098. return(FALSE);
  1099. }
  1100. //
  1101. // Open the file for reading and position self to start of the
  1102. // file
  1103. //
  1104. *pFileHdl = CreateFile(
  1105. pHoldFileName,
  1106. GENERIC_READ,
  1107. FILE_SHARE_READ,
  1108. &SecAtt,
  1109. OPEN_EXISTING,
  1110. FILE_ATTRIBUTE_NORMAL,
  1111. 0 //ignored ?? check
  1112. );
  1113. if (*pFileHdl == INVALID_HANDLE_VALUE)
  1114. {
  1115. WINSEVT_STRS_T EvtStr;
  1116. EvtStr.NoOfStrs = 1;
  1117. EvtStr.pStr[0] = pHoldFileName;
  1118. Error = GetLastError();
  1119. DBGPRINT1(ERR, "WinsMscOpenFile: Could not open the file (Error = %d)\n", Error);
  1120. FUTURES("Use WINSEVT_LOG_STR_M. Make sure it takes TCHAR instead of CHAR")
  1121. WINSEVT_LOG_STR_M(WINS_EVT_FILE_ERR, &EvtStr);
  1122. return(FALSE);
  1123. }
  1124. return(TRUE);
  1125. }
  1126. BOOL
  1127. WinsMscMapFile(
  1128. IN OUT PWINSPRS_FILE_INFO_T pFileInfo
  1129. )
  1130. /*++
  1131. Routine Description:
  1132. This function maps a file into allocated memory
  1133. Arguments:
  1134. FileHdl - Handle to the file
  1135. pFileInfo - Address of buffer into which the file was mapped
  1136. Externals Used:
  1137. None
  1138. Return Value:
  1139. Success status codes -- TRUE
  1140. Error status codes -- FALSE
  1141. Error Handling:
  1142. Called by:
  1143. Side Effects:
  1144. Comments:
  1145. Note: The function returns an error if te file is more than
  1146. 2**32 bytes in size
  1147. --*/
  1148. {
  1149. DWORD HighWordOfFSz = 0;
  1150. DWORD Error;
  1151. DWORD cBytesRead;
  1152. BOOL fRetVal = FALSE;
  1153. try {
  1154. //
  1155. // get the size of the file so that we can allocate enough memory
  1156. // to read the file in
  1157. //
  1158. pFileInfo->FileSize = GetFileSize(pFileInfo->FileHdl, &HighWordOfFSz);
  1159. if (HighWordOfFSz)
  1160. {
  1161. DBGPRINT1(ERR, "WinsMscMapFile: File too big. High word of size is (%x)\n", HighWordOfFSz);
  1162. WINSEVT_LOG_M(WINS_FAILURE, WINS_EVT_FILE_TOO_BIG);
  1163. fRetVal = FALSE;
  1164. }
  1165. else
  1166. {
  1167. //
  1168. // if the low word of size is 0xFFFFFFFF either it is a valid
  1169. // size or it is an error. Check it
  1170. //
  1171. if (pFileInfo->FileSize == 0xFFFFFFFF)
  1172. {
  1173. Error = GetLastError();
  1174. if (Error != NO_ERROR)
  1175. {
  1176. DBGPRINT1(ERR, "WinsMscMapFile: Error from GetFileSz = (%d)\n", Error);
  1177. WINSEVT_LOG_M(Error, WINS_EVT_FILE_ERR);
  1178. fRetVal = FALSE;
  1179. }
  1180. }
  1181. else
  1182. {
  1183. //
  1184. // Allocate a buffer to hold the contents of the file
  1185. //
  1186. WinsMscAlloc(
  1187. pFileInfo->FileSize,
  1188. &pFileInfo->pFileBuff
  1189. );
  1190. pFileInfo->pLimit = pFileInfo->pFileBuff +
  1191. pFileInfo->FileSize;
  1192. fRetVal = ReadFile(
  1193. pFileInfo->FileHdl,
  1194. pFileInfo->pFileBuff,
  1195. pFileInfo->FileSize,
  1196. &cBytesRead,
  1197. NULL
  1198. );
  1199. if (!fRetVal)
  1200. {
  1201. DBGPRINT1(ERR,
  1202. "WinsMscMapFile: Error reading file (Error = %d)\n", GetLastError());
  1203. WinsMscDealloc(pFileInfo->pFileBuff);
  1204. }
  1205. }
  1206. }
  1207. } // end of try ...
  1208. except(EXCEPTION_EXECUTE_HANDLER) {
  1209. DBGPRINTEXC("WinsMscParse");
  1210. }
  1211. //
  1212. // close the file
  1213. //
  1214. if (!CloseHandle(pFileInfo->FileHdl))
  1215. {
  1216. Error = GetLastError();
  1217. DBGPRINT1(ERR, "WinsMscMapFile: Could not close the file (Error = %d)\n", Error);
  1218. }
  1219. #ifdef WINSDBG
  1220. else
  1221. {
  1222. DBGPRINT0(DET, "WinsMscMapFile: Closed handle to open file\n");
  1223. }
  1224. #endif
  1225. return(fRetVal);
  1226. }
  1227. VOID
  1228. WinsMscLogEvtStrs(
  1229. LPBYTE pAscii,
  1230. DWORD Evt,
  1231. BOOL fInfo
  1232. )
  1233. {
  1234. WINSEVT_STRS_T EvtStrs;
  1235. WCHAR String[NMSDB_MAX_NAM_LEN];
  1236. EvtStrs.NoOfStrs = 1;
  1237. (VOID)WinsMscConvertAsciiStringToUnicode(
  1238. pAscii,
  1239. (LPBYTE)String,
  1240. NMSDB_MAX_NAM_LEN);
  1241. EvtStrs.pStr[0] = String;
  1242. if (!fInfo)
  1243. {
  1244. WINSEVT_LOG_STR_M(Evt, &EvtStrs);
  1245. }
  1246. else
  1247. {
  1248. WINSEVT_LOG_INFO_STR_D_M(Evt, &EvtStrs);
  1249. }
  1250. return;
  1251. }
  1252. VOID
  1253. WinsMscConvertUnicodeStringToAscii(
  1254. LPBYTE pUnicodeString,
  1255. LPBYTE pAsciiString,
  1256. DWORD MaxSz
  1257. )
  1258. {
  1259. WideCharToMultiByte(CP_ACP, 0, (LPWSTR)pUnicodeString, -1,
  1260. pAsciiString, MaxSz, NULL,
  1261. NULL);
  1262. return;
  1263. }
  1264. VOID
  1265. WinsMscConvertAsciiStringToUnicode(
  1266. LPBYTE pAsciiString,
  1267. LPBYTE pUnicodeString,
  1268. DWORD MaxSz
  1269. )
  1270. {
  1271. MultiByteToWideChar(CP_ACP, 0, pAsciiString, -1,
  1272. (LPWSTR)pUnicodeString, MaxSz);
  1273. return;
  1274. }
  1275. BOOL
  1276. WinsMscGetName(
  1277. DWORD StrType,
  1278. LPTSTR pFileName,
  1279. LPTSTR pExpandedFileName,
  1280. DWORD ExpandedFileNameBuffLen,
  1281. LPTSTR *ppHoldFileName
  1282. )
  1283. {
  1284. DWORD ChInDest;
  1285. if (StrType == REG_EXPAND_SZ)
  1286. {
  1287. ChInDest = ExpandEnvironmentStrings(
  1288. pFileName,
  1289. pExpandedFileName,
  1290. ExpandedFileNameBuffLen);
  1291. if (ChInDest == 0)
  1292. {
  1293. WINSEVT_STRS_T EvtStr;
  1294. EvtStr.NoOfStrs = 1;
  1295. EvtStr.pStr[0] = pFileName;
  1296. DBGPRINT2(ERR, "WinsPrsDoStaticInit: Could not expand environment strings in (%s). Error is (%d)\n", pFileName, (DWORD)GetLastError());
  1297. WINSEVT_LOG_STR_M(WINS_EVT_FILE_ERR, &EvtStr);
  1298. return(FALSE);
  1299. }
  1300. //
  1301. // If only part of the expanded name could be stored, log error
  1302. //
  1303. if (ChInDest > ExpandedFileNameBuffLen)
  1304. {
  1305. WINSEVT_STRS_T EvtStr;
  1306. EvtStr.NoOfStrs = 1;
  1307. EvtStr.pStr[0] = pFileName;
  1308. DBGPRINT2(ERR, "WinsPrsDoStaticInit: File name after expansion is just too big (%d> 255).\nThe name to be expanded is (%s))", ChInDest, pFileName);
  1309. WINSEVT_LOG_STR_M(WINS_EVT_FILE_NAME_TOO_BIG, &EvtStr);
  1310. return(FALSE);
  1311. }
  1312. *ppHoldFileName = pExpandedFileName;
  1313. }
  1314. else
  1315. {
  1316. //
  1317. // There were no env. var. to expand
  1318. //
  1319. *ppHoldFileName = pFileName;
  1320. }
  1321. return(TRUE);
  1322. }
  1323. VOID
  1324. WinsMscSendControlToSc(
  1325. DWORD ControlCode
  1326. )
  1327. /*++
  1328. Routine Description:
  1329. Arguments:
  1330. Externals Used:
  1331. None
  1332. Return Value:
  1333. Success status codes --
  1334. Error status codes --
  1335. Error Handling:
  1336. Called by:
  1337. Side Effects:
  1338. Comments:
  1339. None
  1340. --*/
  1341. {
  1342. SERVICE_STATUS ServiceStatus;
  1343. BOOL fStatus;
  1344. SC_HANDLE ScHdl;
  1345. SC_HANDLE SvcHdl;
  1346. BOOL sCalled = FALSE;
  1347. try {
  1348. ScHdl = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  1349. if (ScHdl == NULL)
  1350. {
  1351. DBGPRINT1(ERR, "WinsMscSendControlToSc: Error (%d) from OpenSCManager\n", GetLastError());
  1352. return;
  1353. }
  1354. SvcHdl = OpenService(ScHdl, WINS_SERVER, SERVICE_ALL_ACCESS);
  1355. if (SvcHdl == NULL)
  1356. {
  1357. DBGPRINT1(ERR, "WinsMscSendControlToSc: Error (%d) from OpenService\n", GetLastError());
  1358. goto CLOSE_SC;
  1359. }
  1360. fStatus = ControlService(SvcHdl, ControlCode, &ServiceStatus);
  1361. if (!fStatus)
  1362. {
  1363. DBGPRINT1(ERR, "WinsMscSendControlToSc: Error (%d) from ControlService\n", GetLastError());
  1364. goto CLOSE_SERVICE;
  1365. }
  1366. else
  1367. {
  1368. DBGPRINT1(FLOW, "WinsMscSendControlToSc: Current State is (%d)\n",
  1369. ServiceStatus.dwCurrentState);
  1370. }
  1371. CLOSE_SERVICE:
  1372. fStatus = CloseServiceHandle(SvcHdl);
  1373. if (!fStatus)
  1374. {
  1375. DBGPRINT1(ERR, "WinsMscSendControlToSc: Error (%d) from CloseServiceHandle called for service\n", GetLastError());
  1376. }
  1377. CLOSE_SC:
  1378. fStatus = CloseServiceHandle(ScHdl);
  1379. if (!fStatus)
  1380. {
  1381. DBGPRINT1(ERR, "WinsMscSendControlToSc: Error (%d) from CloseServiceHandle called for SC\n", GetLastError());
  1382. }
  1383. }
  1384. except(EXCEPTION_EXECUTE_HANDLER) {
  1385. DBGPRINTEXC("WinsMscSendControlToSc");
  1386. }
  1387. return;
  1388. }
  1389. unsigned
  1390. WinsMscPutMsg(
  1391. unsigned usMsgNum,
  1392. ... )
  1393. /*++
  1394. Routine Description:
  1395. Displays a message
  1396. Arguments:
  1397. Externals Used:
  1398. None
  1399. Return Value:
  1400. Success status codes --
  1401. Error status codes --
  1402. Error Handling:
  1403. Called by:
  1404. Side Effects:
  1405. Comments:
  1406. None
  1407. --*/
  1408. {
  1409. //unsigned msglen;
  1410. //va_list arglist;
  1411. //LPVOID pMsg;
  1412. //HINSTANCE hModule;
  1413. DBGENTER("WinsMscPutMsg\n");
  1414. //--ft: #106568 - WINS is a service and it shouldn't pop up message boxes.
  1415. //--this is mostly annoying for cluster: in case the db. is corrupted WINS is popping the message wnd
  1416. //--and does not terminate (at least the process wins.exe will be there for as long as the dialog is
  1417. //--on the screen. This will prevent the cluster from bringing up WINS resource on the same node.
  1418. //--In case of such a failure, the event being logged (in system log) should suffice.
  1419. //
  1420. //if ((hModule = LoadLibrary(TEXT("winsevnt.dll")))==NULL)
  1421. //{
  1422. // DBGPRINT1(ERR, "WinsMscPutMsg: LoadLibrary(\"winsevnt.dll\") failed with error = (%d)\n.", GetLastError());
  1423. // return 0;
  1424. //}
  1425. //va_start(arglist, usMsgNum);
  1426. //if (!(msglen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1427. // FORMAT_MESSAGE_FROM_HMODULE,
  1428. // hModule,
  1429. // usMsgNum,
  1430. // 0L, // Default country ID.
  1431. // (LPTSTR)&pMsg,
  1432. // 0,
  1433. // &arglist)))
  1434. //{
  1435. // DBGPRINT1(ERR, "WinsMscPutMsg: FormatMessage failed with error = (%d)\n",
  1436. // GetLastError());
  1437. //}
  1438. //else
  1439. //{
  1440. // DBGPRINT0(DET, "WinsMscPutMsg: Putting up the message box\n");
  1441. // if(MessageBoxEx(NULL, pMsg, WINS_SERVER_FULL_NAME, MB_SYSTEMMODAL | MB_OK | MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION | MB_ICONSTOP, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)) == 0)
  1442. // {
  1443. // DBGPRINT1(ERR, "WinsMscPutMsg: MessageBoxEx failed with error = (%d)\n", GetLastError());
  1444. // }
  1445. // LocalFree(pMsg);
  1446. //}
  1447. //FreeLibrary(hModule);
  1448. WINSEVT_LOG_M(WINS_FAILURE, usMsgNum);
  1449. DBGLEAVE("WinsMscPutMsg\n");
  1450. //return(msglen);
  1451. return 0;
  1452. }
  1453. LPTSTR
  1454. WinsMscGetString(
  1455. DWORD StrId
  1456. )
  1457. /*++
  1458. Routine Description:
  1459. This routine retrieves string corresponding to strid from the resource file.
  1460. Arguments:
  1461. StrId - The unique id of the string.
  1462. Externals Used:
  1463. None
  1464. Return Value:
  1465. Success status codes --
  1466. Error status codes --
  1467. Error Handling:
  1468. Called by:
  1469. Side Effects:
  1470. Comments:
  1471. None
  1472. --*/
  1473. {
  1474. unsigned msglen;
  1475. va_list arglist;
  1476. LPTSTR pMsg = NULL;
  1477. HINSTANCE hModule;
  1478. DBGENTER("WinsMscPutMsg\n");
  1479. if ((hModule = LoadLibrary(TEXT("winsevnt.dll"))) == NULL)
  1480. {
  1481. DBGPRINT1(ERR, "LoadLibrary(\"winsevnt.dll\") failed with error = (%d)\n",
  1482. GetLastError());
  1483. return NULL;
  1484. }
  1485. if (!(msglen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1486. FORMAT_MESSAGE_FROM_HMODULE,
  1487. hModule,
  1488. StrId,
  1489. 0L, // Default country ID.
  1490. (LPTSTR)&pMsg,
  1491. 0,
  1492. NULL)))
  1493. {
  1494. DBGPRINT1(ERR, "WinsMscPutMsg: FormatMessage failed with error = (%d)\n",
  1495. GetLastError());
  1496. }
  1497. FreeLibrary(hModule);
  1498. DBGLEAVE("WinsMscPutMsg\n");
  1499. return(pMsg);
  1500. }
  1501. VOID
  1502. WinsMscChkTermEvt(
  1503. #ifdef WINSDBG
  1504. WINS_CLIENT_E Client_e,
  1505. #endif
  1506. BOOL fTermTrans
  1507. )
  1508. /*++
  1509. Routine Description:
  1510. Arguments:
  1511. Externals Used:
  1512. None
  1513. Return Value:
  1514. Success status codes --
  1515. Error status codes --
  1516. Error Handling:
  1517. Called by:
  1518. Side Effects:
  1519. Comments:
  1520. Currently (8/6/94), fTermTrans is set only by the scavenger thread
  1521. --*/
  1522. {
  1523. DWORD fSignaled;
  1524. /*
  1525. * We may have been signaled by the main thread
  1526. * Check it.
  1527. */
  1528. WinsMscWaitTimed(
  1529. NmsTermEvt,
  1530. 0, //timeout is 0
  1531. &fSignaled
  1532. );
  1533. if (fSignaled)
  1534. {
  1535. DBGPRINT1(DET, "WinsCnfChkTermEvt: %s thread got termination signal\n", Client_e == WINS_E_RPLPULL ? "PULL" : "SCV");
  1536. if (fTermTrans)
  1537. {
  1538. NmsDbEndTransaction(); //ignore return code
  1539. }
  1540. WinsMscTermThd(WINS_SUCCESS, WINS_DB_SESSION_EXISTS);
  1541. }
  1542. return;
  1543. }
  1544. VOID
  1545. WinsMscDelFiles(
  1546. BOOL fMultiple,
  1547. LPCTSTR pFilePattern,
  1548. LPTSTR pFilePath
  1549. )
  1550. {
  1551. DWORD ErrCode;
  1552. #ifdef WINSDBG
  1553. BYTE FileNameAscii[WINS_MAX_FILENAME_SZ];
  1554. #endif
  1555. DBGENTER("WinsMscDelFiles\n");
  1556. if (fMultiple)
  1557. {
  1558. WIN32_FIND_DATA FileInfo;
  1559. HANDLE SearchHandle;
  1560. TCHAR FullFilePath[WINS_MAX_FILENAME_SZ + MAX_PATH + 2];
  1561. //
  1562. // Construct the full file pattern
  1563. //
  1564. lstrcpy(FullFilePath, pFilePath);
  1565. lstrcat(FullFilePath, L"\\");
  1566. lstrcat(FullFilePath, pFilePattern);
  1567. SearchHandle = FindFirstFile(FullFilePath, &FileInfo);
  1568. if (SearchHandle == INVALID_HANDLE_VALUE)
  1569. {
  1570. DBGPRINT1(ERR, "WinsMscDelFiles: FindFirstFile returned error = (%d)\n", GetLastError());
  1571. return;
  1572. }
  1573. do {
  1574. //
  1575. // construct the full file path
  1576. //
  1577. lstrcpy(FullFilePath, pFilePath);
  1578. lstrcat(FullFilePath, L"\\");
  1579. lstrcat(FullFilePath, FileInfo.cFileName);
  1580. #ifdef WINSDBG
  1581. WinsMscConvertUnicodeStringToAscii((LPBYTE)FullFilePath, FileNameAscii, sizeof(FileNameAscii));
  1582. DBGPRINT1(DET, "WinsMscDelFiles: Deleting %s ..\n", FileNameAscii);
  1583. #endif
  1584. if (!DeleteFile(FullFilePath))
  1585. {
  1586. DBGPRINT1(ERR, "WinsMscDelFiles: DeleteFile returned error = (%d)\n", GetLastError());
  1587. FindClose(SearchHandle);
  1588. return;
  1589. }
  1590. } while(FindNextFile(SearchHandle, &FileInfo));
  1591. if ((ErrCode = GetLastError()) != ERROR_NO_MORE_FILES)
  1592. {
  1593. DBGPRINT1(ERR, "WinsMscDelFiles: FindNextFile returned error = (%d)\n", ErrCode);
  1594. }
  1595. if (!FindClose(SearchHandle))
  1596. {
  1597. DBGPRINT1(ERR, "WinsMscDelFiles: FindClose returned error = (%d)\n", ErrCode);
  1598. }
  1599. }
  1600. else
  1601. {
  1602. if (!DeleteFile(pFilePattern))
  1603. {
  1604. DBGPRINT1(ERR, "WinsMscDelFiles: DeleteFile returned error = (%d)\n", GetLastError());
  1605. return;
  1606. }
  1607. }
  1608. DBGLEAVE("WinsMscDelFiles\n");
  1609. return;
  1610. }
  1611. VOID
  1612. WinsMscHeapReAlloc(
  1613. IN HANDLE HeapHdl,
  1614. IN DWORD BuffSize,
  1615. OUT LPVOID *ppRspBuff
  1616. )
  1617. /*++
  1618. Routine Description:
  1619. This function is called to allocate memory.
  1620. Arguments:
  1621. BuffSize - Size of buffer to allocate
  1622. ppRspBuff - Buffer allocated
  1623. Externals Used:
  1624. None
  1625. Return Value:
  1626. None
  1627. Error Handling:
  1628. Called by:
  1629. NmsDbGetDataRecs, GetGroupMembers
  1630. Side Effects:
  1631. Comments:
  1632. None
  1633. --*/
  1634. {
  1635. *ppRspBuff = HeapReAlloc(
  1636. HeapHdl,
  1637. HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,
  1638. *ppRspBuff,
  1639. BuffSize
  1640. );
  1641. DBGPRINT3(HEAP, "WinsMscHeapReAlloc: HeapHdl = (%p), pStartBuff = (%p), BuffSize = (%d)\n", HeapHdl, *ppRspBuff, BuffSize);
  1642. return;
  1643. }