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.

234 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. perfmain.c
  5. Abstract:
  6. This file contains the DllMain function for the NTFRSPRF.dll.
  7. Author:
  8. Rohan Kumar [rohank] 15-Feb-1999
  9. Environment:
  10. User Mode Service
  11. Revision History:
  12. --*/
  13. //
  14. // The common header file which leads to the definition of the CRITICAL_SECTION
  15. // data structure and declares the globals FRS_ThrdCounter and FRC_ThrdCounter.
  16. //
  17. #include <perrepsr.h>
  18. //
  19. // If InitializeCriticalSectionAndSpinCount returns an error, set the global boolean
  20. // (below) to FALSE.
  21. //
  22. BOOLEAN ShouldPerfmonCollectData = TRUE;
  23. BOOLEAN FRS_ThrdCounter_Initialized = FALSE;
  24. BOOLEAN FRC_ThrdCounter_Initialized = FALSE;
  25. HANDLE hEventLog;
  26. //BOOLEAN DoLogging = TRUE;
  27. //
  28. // Default to no Event Log reporting.
  29. //
  30. DWORD PerfEventLogLevel = WINPERF_LOG_NONE;
  31. #define NTFRSPERF L"SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\NTFRSPerf"
  32. #define EVENTLOGDLL L"%SystemRoot%\\System32\\ntfrsres.dll"
  33. #define PERFLIB_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib"
  34. BOOL
  35. WINAPI
  36. DllMain(
  37. HINSTANCE hinstDLL,
  38. DWORD fdwReason,
  39. LPVOID fImpLoad
  40. )
  41. /*++
  42. Routine Description:
  43. The DllMain routine for the NTFRSPRF.dll.
  44. Arguments:
  45. hinstDLL - Instance handle of the DLL.
  46. fdwReason - The reason for this function to be called by the system.
  47. fImpLoad - Indicated whether the DLL was implicitly or explicitly loaded.
  48. Return Value:
  49. TRUE.
  50. --*/
  51. {
  52. DWORD flag, WStatus;
  53. DWORD size, type;
  54. DWORD TypesSupported = 7; // Types of EventLog messages supported.
  55. HKEY Key = INVALID_HANDLE_VALUE;
  56. switch(fdwReason) {
  57. case DLL_PROCESS_ATTACH:
  58. //
  59. // THe DLL is being mapped into the process's address space. When this
  60. // happens, initialize the CRITICAL_SECTION objects being used for
  61. // synchronization. InitializeCriticalSectionAndSpinCount returns
  62. // an error in low memory condition.
  63. //
  64. if(!InitializeCriticalSectionAndSpinCount(&FRS_ThrdCounter,
  65. NTFRS_CRITSEC_SPIN_COUNT)) {
  66. ShouldPerfmonCollectData = FALSE;
  67. return(TRUE);
  68. }
  69. FRS_ThrdCounter_Initialized = TRUE;
  70. if(!InitializeCriticalSectionAndSpinCount(&FRC_ThrdCounter,
  71. NTFRS_CRITSEC_SPIN_COUNT)) {
  72. ShouldPerfmonCollectData = FALSE;
  73. return(TRUE);
  74. }
  75. FRC_ThrdCounter_Initialized = TRUE;
  76. //
  77. // Create/Open a Key under the Application key for logging purposes.
  78. // Even if we fail, we return TRUE. EventLogging is not critically important.
  79. // Returning FALSE will cause the process loading this DLL to terminate.
  80. //
  81. WStatus = RegCreateKeyEx (HKEY_LOCAL_MACHINE,
  82. NTFRSPERF,
  83. 0L,
  84. NULL,
  85. REG_OPTION_NON_VOLATILE,
  86. KEY_ALL_ACCESS,
  87. NULL,
  88. &Key,
  89. &flag);
  90. if (WStatus != ERROR_SUCCESS) {
  91. //DoLogging = FALSE;
  92. break;
  93. }
  94. //
  95. // Set the values EventMessageFile and TypesSupported. Return value is
  96. // intentionally not checked (see above).
  97. //
  98. WStatus = RegSetValueEx(Key,
  99. L"EventMessageFile",
  100. 0L,
  101. REG_EXPAND_SZ,
  102. (BYTE *)EVENTLOGDLL,
  103. (1 + wcslen(EVENTLOGDLL)) * sizeof(WCHAR));
  104. if (WStatus != ERROR_SUCCESS) {
  105. //DoLogging = FALSE;
  106. FRS_REG_CLOSE(Key);
  107. break;
  108. }
  109. WStatus = RegSetValueEx(Key,
  110. L"TypesSupported",
  111. 0L,
  112. REG_DWORD,
  113. (BYTE *)&TypesSupported,
  114. sizeof(DWORD));
  115. if (WStatus != ERROR_SUCCESS) {
  116. //DoLogging = FALSE;
  117. FRS_REG_CLOSE(Key);
  118. break;
  119. }
  120. //
  121. // Close the key
  122. //
  123. FRS_REG_CLOSE(Key);
  124. //
  125. // Get the handle used to report errors in the event log. Return value
  126. // is intentionally not checked (see above).
  127. //
  128. hEventLog = RegisterEventSource((LPCTSTR)NULL, (LPCTSTR)L"NTFRSPerf");
  129. if (hEventLog == NULL) {
  130. //DoLogging = FALSE;
  131. break;
  132. }
  133. //
  134. // Read the Perflib Event log level from the registry.
  135. // "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\EventLogLevel"
  136. //
  137. WStatus = RegOpenKey(HKEY_LOCAL_MACHINE, PERFLIB_KEY, &Key);
  138. if (WStatus != ERROR_SUCCESS) {
  139. //DoLogging = FALSE;
  140. break;
  141. }
  142. size = sizeof(DWORD);
  143. WStatus = RegQueryValueEx (Key,
  144. L"EventLogLevel",
  145. 0L,
  146. &type,
  147. (LPBYTE)&PerfEventLogLevel,
  148. &size);
  149. if (WStatus != ERROR_SUCCESS || type != REG_DWORD) {
  150. //DoLogging = FALSE;
  151. PerfEventLogLevel = WINPERF_LOG_NONE;
  152. FRS_REG_CLOSE(Key);
  153. break;
  154. }
  155. FRS_REG_CLOSE(Key);
  156. break;
  157. case DLL_THREAD_ATTACH:
  158. //
  159. // A thread is being created. Nothing to do.
  160. //
  161. break;
  162. case DLL_THREAD_DETACH:
  163. //
  164. // A thread is exiting cleanly. Nothing to do.
  165. //
  166. break;
  167. case DLL_PROCESS_DETACH:
  168. //
  169. // The DLL is being unmapped from the process's address space. Free up
  170. // the resources.
  171. //
  172. if (FRS_ThrdCounter_Initialized) {
  173. DeleteCriticalSection(&FRS_ThrdCounter);
  174. }
  175. if (FRC_ThrdCounter_Initialized) {
  176. DeleteCriticalSection(&FRC_ThrdCounter);
  177. }
  178. if (hEventLog) {
  179. DeregisterEventSource(hEventLog);
  180. }
  181. break;
  182. }
  183. return(TRUE);
  184. }