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.

342 lines
12 KiB

  1. /*----------------------------------------------------------------------------
  2. Cpsmon.cpp
  3. Implementation of pbsmon.dll -- the perfmon DLL for counting the number of
  4. times the phone book server was accessed
  5. since it started
  6. Copyright (c) 1997-1998 Microsoft Corporation
  7. All rights reserved.
  8. Authors:
  9. t-geetat Geeta Tarachandani
  10. History:
  11. 6/2/97 t-geetat Created
  12. --------------------------------------------------------------------------*/
  13. #include "pbsmaster.h"
  14. BOOL g_bOpenOK =FALSE; // TRUE if Open went OK, FALSE otherwise
  15. DWORD g_dwNumOpens =0; // Active "opens" reference counts
  16. CPSMON_DATA_DEFINITION g_CpsMonDataDef;
  17. HANDLE g_hSharedFileMapping = NULL; // Handle to shared file map
  18. PERF_COUNTERS * g_pCpsCounter = NULL; // Pointer to the shared object
  19. //----------------------------------------------------------------------------
  20. //
  21. // Function: OpenPerfMon
  22. //
  23. // Synopsis: This function opens & maps the shared memory used to pass
  24. // counter-values between the phone-book server & perfmon.dll
  25. // It also initializes the data-structures used to pass data back
  26. // to the registry
  27. //
  28. // Arguments: lpDeviceName -- Pointer to object ID of the device to be opened
  29. // --> Should be NULL.
  30. //
  31. // Returns: ERROR_SUCCESS if succeeds, GetLastError() if fails.
  32. //
  33. // History: 06/02/97 t-geetat Created
  34. //
  35. //----------------------------------------------------------------------------
  36. DWORD OpenPerfMon( LPWSTR lpDeviceName )
  37. {
  38. OutputDebugString("OpenPerfMon - entering\n");
  39. if ( g_bOpenOK )
  40. {
  41. g_dwNumOpens ++;
  42. OutputDebugString("OpenPerfMon - already open\n");
  43. return ERROR_SUCCESS;
  44. }
  45. // -------------------------------------
  46. // Validate parameter (should be NULL)
  47. // -------------------------------------
  48. if (lpDeviceName)
  49. {
  50. OutputDebugString("OpenPerfMon - param is not null, should be null");
  51. return ERROR_INVALID_PARAMETER;
  52. }
  53. // -------------------------------------
  54. // Open shared memory ( if exists )
  55. // -------------------------------------
  56. g_hSharedFileMapping = OpenFileMapping(
  57. FILE_MAP_READ, // Read only access desired
  58. FALSE, // Don't want to inherit
  59. SHARED_OBJECT); // from "cpsmon.h"
  60. if ( NULL == g_hSharedFileMapping )
  61. {
  62. //
  63. // the phone book server DLL should create this Shared File. So we can assume
  64. // the server has not been loaded yet. Just return silently.
  65. //
  66. OutputDebugString("OpenPerfMon - OpenFileMapping returned NULL\n");
  67. return ERROR_SUCCESS;
  68. }
  69. // -------------------------------------
  70. // Map the shared-file into memory
  71. // -------------------------------------
  72. g_pCpsCounter = (PERF_COUNTERS *)MapViewOfFileEx(
  73. g_hSharedFileMapping, // File mapping handle
  74. FILE_MAP_READ, // Read only access desired
  75. 0, // |_ File Offset
  76. 0, // |
  77. sizeof(PERF_COUNTERS), // no. of bytes to map
  78. NULL ); // Any address
  79. if ( NULL == g_pCpsCounter )
  80. {
  81. OutputDebugString("OpenPerfMon - MapViewOfFileEx is null\n");
  82. goto CleanUp;
  83. }
  84. // ------------------------------------------------
  85. // Initialize the data-structure g_CpsMonDataDef
  86. // ------------------------------------------------
  87. InitializeDataDef();
  88. OutputDebugString("OpenPerfMon - past IntializeDataDef\n");
  89. // -----------------------------------------------------------------
  90. // Update static data structures g_CpsMonDataDef by adding base to
  91. // the offset value in the structure.
  92. // -----------------------------------------------------------------
  93. if (!UpdateDataDefFromRegistry())
  94. {
  95. goto CleanUp;
  96. }
  97. // -------------
  98. // Success :)
  99. // -------------
  100. g_bOpenOK = TRUE;
  101. g_dwNumOpens ++;
  102. OutputDebugString("OpenPerfMon - exit success\n");
  103. return ERROR_SUCCESS;
  104. CleanUp :
  105. // -------------
  106. // Failure :(
  107. // -------------
  108. if ( NULL != g_hSharedFileMapping )
  109. {
  110. CloseHandle( g_hSharedFileMapping );
  111. g_hSharedFileMapping = NULL;
  112. }
  113. if ( NULL != g_pCpsCounter )
  114. {
  115. g_pCpsCounter = NULL;
  116. }
  117. OutputDebugString("OpenPerfMon - exit failure\n");
  118. return GetLastError();
  119. }
  120. //----------------------------------------------------------------------------
  121. //
  122. // Function: CollectPerfMon
  123. //
  124. // Synopsis: This function opens & maps the shared memory used to pass
  125. // counter-values between the phone-book server & perfmon.dll
  126. // It also initializes the data-structures used to pass data back
  127. // to the registry
  128. //
  129. // Arguments:
  130. // lpwszValue Pointer to wide character string passed by registry
  131. //
  132. // lppData IN : Pointer to address of buffer to receive completed
  133. // PerfDataBlock and subordinate structures This routine
  134. // appends its data to the buffer starting at *lppData.
  135. // OUT : Points to the first byte after the data structure added
  136. // by this routine.
  137. //
  138. // lpcbTotalBytes IN : Address of DWORD that tells the size in bytes
  139. // of the buffer *lppData.
  140. // OUT : The number of bytes added by this routine is
  141. // written to the DWORD pointed to by this arg.
  142. //
  143. // lpcObjectTypes IN : Address of DWORD to receive the number of
  144. // objects added by this routine
  145. // OUT : The number of objs. added by this routine.
  146. //
  147. // Returns: ERROR_SUCCESS if succeeds, GetLastError() if fails.
  148. //
  149. // History: 06/02/97 t-geetat Created
  150. //
  151. //----------------------------------------------------------------------------
  152. DWORD CollectPerfMon(
  153. IN LPWSTR lpwszValue,
  154. IN OUT LPVOID *lppData,
  155. IN OUT LPDWORD lpcbTotalBytes,
  156. IN OUT LPDWORD lpcObjectTypes
  157. )
  158. {
  159. DWORD dwQueryType;
  160. CPSMON_DATA_DEFINITION *pCpsMonDataDef;
  161. CPSMON_COUNTERS *pCpsMonCounters;
  162. DWORD SpaceNeeded = 0;
  163. OutputDebugString("CollectPerfMon - entering\n");
  164. // parameter validation:
  165. // lpwszValue can be NULL.
  166. // other params must be non null at least
  167. if (!lppData || !lpcbTotalBytes || !lpcObjectTypes)
  168. {
  169. return ERROR_INVALID_PARAMETER;
  170. }
  171. // ------------------------
  172. // Check if Open went OK
  173. //-------------------------
  174. if ( !g_bOpenOK )
  175. {
  176. // -----------------------------------------
  177. // Unable to continue because open failed
  178. // -----------------------------------------
  179. *lpcbTotalBytes = (DWORD) 0;
  180. *lpcObjectTypes = (DWORD) 0;
  181. OutputDebugString("CollectPerfMon - exit success actually open failed\n");
  182. return ERROR_SUCCESS;
  183. }
  184. // ------------------------------------
  185. // Retrieve the TYPE of the request
  186. // ------------------------------------
  187. dwQueryType = GetQueryType( lpwszValue );
  188. if ( QUERY_FOREIGN == dwQueryType )
  189. {
  190. // -------------------------------------
  191. // Unable to service non-NT requests
  192. // -------------------------------------
  193. *lpcbTotalBytes = (DWORD) 0;
  194. *lpcObjectTypes = (DWORD) 0;
  195. OutputDebugString("CollectPerfMon - exit success query_foreign\n");
  196. return ERROR_SUCCESS;
  197. }
  198. if ( QUERY_ITEMS == dwQueryType )
  199. {
  200. // -----------------------------------------------
  201. // The registry is asking for specifis objects.
  202. // Check if we're one of the chosen
  203. // -----------------------------------------------
  204. if ( !IsNumberInUnicodeList(
  205. g_CpsMonDataDef.m_CpsMonObjectType.ObjectNameTitleIndex,
  206. lpwszValue ) )
  207. {
  208. *lpcbTotalBytes = (DWORD) 0;
  209. *lpcObjectTypes = (DWORD) 0;
  210. OutputDebugString("CollectPerfMon - exit success not our objects\n");
  211. return ERROR_SUCCESS;
  212. }
  213. }
  214. // -------------------------------------------
  215. // We need space for header and the counters
  216. // Let's see if there's enough space
  217. // -------------------------------------------
  218. SpaceNeeded = sizeof(CPSMON_DATA_DEFINITION) + sizeof( CPSMON_COUNTERS );
  219. if ( SpaceNeeded > *lpcbTotalBytes )
  220. {
  221. *lpcbTotalBytes = (DWORD) 0;
  222. *lpcObjectTypes = (DWORD) 0;
  223. OutputDebugString("CollectPerfMon - ran out of space\n");
  224. return ERROR_MORE_DATA;
  225. }
  226. // -------------------------------------------------------------
  227. // Copy the initialized Object Type & the Counter Definitions
  228. // into the caller's data buffer
  229. // -------------------------------------------------------------
  230. pCpsMonDataDef = (CPSMON_DATA_DEFINITION *) *lppData;
  231. memmove( pCpsMonDataDef, &g_CpsMonDataDef, sizeof(CPSMON_DATA_DEFINITION) );
  232. // --------------------------------
  233. // Now try to retrieve the data
  234. // --------------------------------
  235. pCpsMonCounters = (CPSMON_COUNTERS *)(pCpsMonDataDef + 1);
  236. CPSMON_COUNTERS CpsMonCounters = {
  237. // The PERF_COUNTER_BLOCK structure
  238. { { sizeof( CPSMON_COUNTERS )}, 0},
  239. // The RAW counters
  240. g_pCpsCounter->dwTotalHits,
  241. g_pCpsCounter->dwNoUpgradeHits,
  242. g_pCpsCounter->dwDeltaUpgradeHits,
  243. g_pCpsCounter->dwFullUpgradeHits,
  244. g_pCpsCounter->dwErrors,
  245. // The RATE counters
  246. g_pCpsCounter->dwTotalHits,
  247. g_pCpsCounter->dwNoUpgradeHits,
  248. g_pCpsCounter->dwDeltaUpgradeHits,
  249. g_pCpsCounter->dwFullUpgradeHits,
  250. g_pCpsCounter->dwErrors,
  251. };
  252. memmove( pCpsMonCounters, &CpsMonCounters, sizeof(CPSMON_COUNTERS) );
  253. // -------------------------------
  254. // Update arguements for return
  255. // -------------------------------
  256. *lppData = (LPBYTE)(*lppData) + SpaceNeeded;
  257. *lpcObjectTypes = 1;
  258. *lpcbTotalBytes = SpaceNeeded;
  259. // --------------------
  260. // Success at last :)
  261. // --------------------
  262. OutputDebugString("CollectPerfMon - exit success\n");
  263. return ERROR_SUCCESS;
  264. }
  265. //----------------------------------------------------------------------------
  266. //
  267. // Function: ClosePerfMon
  268. //
  269. // Synopsis: This function closes the open handles to the shared file
  270. //
  271. // Arguments: None
  272. //
  273. // Returns: ERROR_SUCCESS
  274. //
  275. // History: 06/03/97 t-geetat Created
  276. //
  277. //----------------------------------------------------------------------------
  278. DWORD ClosePerfMon()
  279. {
  280. g_dwNumOpens --;
  281. OutputDebugString("ClosePerfMon - entering\n");
  282. if ( NULL != g_hSharedFileMapping )
  283. {
  284. CloseHandle( g_hSharedFileMapping );
  285. g_hSharedFileMapping = NULL;
  286. if ( g_dwNumOpens != 0 )
  287. {
  288. OutputDebugString("ClosePerfMon - strange, g_dwNumOpens should be zero...\n");
  289. }
  290. }
  291. OutputDebugString("ClosePerfMon - exit success\n");
  292. return ERROR_SUCCESS;
  293. }