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.

323 lines
6.6 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2000, Microsoft Corporation.
  4. //
  5. // All rights reserved.
  6. //
  7. // Module Name:
  8. //
  9. // WMIAdapter_Stuff.cpp
  10. //
  11. // Abstract:
  12. //
  13. // module for application stuff ( security, event logging ... )
  14. //
  15. // History:
  16. //
  17. // initial a-marius
  18. //
  19. ////////////////////////////////////////////////////////////////////////////////////
  20. #include "PreComp.h"
  21. // debuging features
  22. #ifndef _INC_CRTDBG
  23. #include <crtdbg.h>
  24. #endif _INC_CRTDBG
  25. // new stores file/line info
  26. #ifdef _DEBUG
  27. #ifndef NEW
  28. #define NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
  29. #define new NEW
  30. #endif NEW
  31. #endif _DEBUG
  32. // application
  33. #include "WMIAdapter_App.h"
  34. extern WmiAdapterApp _App;
  35. #include "WMIAdapter_Stuff.h"
  36. #include "WMIAdapter_Stuff_Refresh.cpp"
  37. #include "RefresherUtils.h"
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // IsValid
  40. ///////////////////////////////////////////////////////////////////////////////
  41. BOOL WmiAdapterStuff::IsValidBasePerfRegistry ( )
  42. {
  43. return ( m_data.IsValidGenerate () );
  44. }
  45. BOOL WmiAdapterStuff::IsValidInternalRegistry ( )
  46. {
  47. return ( m_data.GetPerformanceData() != NULL );
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // init
  51. ///////////////////////////////////////////////////////////////////////////////
  52. HRESULT WmiAdapterStuff::Init ( )
  53. {
  54. return m_Stuff.Init();
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // uninit
  58. ///////////////////////////////////////////////////////////////////////////////
  59. HRESULT WmiAdapterStuff::Uninit ( )
  60. {
  61. return m_Stuff.Uninit();
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // construction
  65. ///////////////////////////////////////////////////////////////////////////////
  66. WmiAdapterStuff::WmiAdapterStuff() :
  67. m_pWMIRefresh ( NULL )
  68. {
  69. try
  70. {
  71. if ( ( m_pWMIRefresh = new WmiRefresh < WmiAdapterStuff > ( this ) ) == NULL )
  72. {
  73. return;
  74. }
  75. }
  76. catch ( ... )
  77. {
  78. return;
  79. }
  80. }
  81. ///////////////////////////////////////////////////////////////////////////////
  82. // destruction
  83. ///////////////////////////////////////////////////////////////////////////////
  84. WmiAdapterStuff::~WmiAdapterStuff()
  85. {
  86. try
  87. {
  88. if ( m_pWMIRefresh )
  89. {
  90. delete m_pWMIRefresh;
  91. m_pWMIRefresh = NULL;
  92. }
  93. }
  94. catch ( ... )
  95. {
  96. }
  97. }
  98. ///////////////////////////////////////////////////////////////////////////////
  99. // generate requested ?
  100. ///////////////////////////////////////////////////////////////////////////////
  101. extern LPCWSTR g_szKey;
  102. extern LPCWSTR g_szKeyRefresh;
  103. BOOL WmiAdapterStuff::RequestGet ()
  104. {
  105. DWORD dwValue = 0;
  106. GetRegistry ( g_szKey, g_szKeyRefresh, &dwValue );
  107. return ( ( dwValue ) ? TRUE : FALSE );
  108. }
  109. BOOL WmiAdapterStuff::RequestSet ()
  110. {
  111. BOOL bResult = FALSE;
  112. if ( ! ( bResult = RequestGet () ) )
  113. {
  114. bResult = SUCCEEDED ( SetRegistry ( g_szKey, g_szKeyRefresh, 1 ) );
  115. }
  116. return bResult;
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. // generate
  120. ///////////////////////////////////////////////////////////////////////////////
  121. extern __SmartHANDLE g_hRefreshMutex;
  122. extern __SmartHANDLE g_hRefreshMutexLib;
  123. extern __SmartHANDLE g_hRefreshFlag;
  124. HRESULT WmiAdapterStuff::Generate ( BOOL bInitialize, GenerateEnum type )
  125. {
  126. HRESULT hRes = E_FAIL;
  127. BOOL bInit = FALSE;
  128. BOOL bOwnFlag = FALSE;
  129. BOOL bOwnMutex = FALSE;
  130. BOOL bLocked = FALSE;
  131. DWORD dwWaitResult = 0L;
  132. DWORD dwHandles = 2;
  133. HANDLE hHandles[] =
  134. {
  135. g_hRefreshMutex,
  136. g_hRefreshMutexLib
  137. };
  138. dwWaitResult = ::WaitForMultipleObjects ( dwHandles, hHandles, TRUE, 0 );
  139. if ( dwWaitResult == WAIT_TIMEOUT )
  140. {
  141. bLocked = TRUE;
  142. }
  143. else
  144. {
  145. if ( dwWaitResult == WAIT_OBJECT_0 )
  146. {
  147. bOwnMutex = TRUE;
  148. hRes = S_OK;
  149. }
  150. }
  151. if ( bLocked )
  152. {
  153. DWORD dwHandles = 3;
  154. HANDLE hHandles[] =
  155. {
  156. _App.m_hKill,
  157. g_hRefreshMutex,
  158. g_hRefreshMutexLib
  159. };
  160. dwWaitResult = ::WaitForMultipleObjects (
  161. dwHandles,
  162. hHandles,
  163. FALSE,
  164. INFINITE
  165. );
  166. switch ( dwWaitResult )
  167. {
  168. case WAIT_OBJECT_0 + 2:
  169. {
  170. dwWaitResult = ::WaitForMultipleObjects (
  171. dwHandles - 1,
  172. hHandles,
  173. FALSE,
  174. INFINITE
  175. );
  176. if ( dwWaitResult != WAIT_OBJECT_0 + 1 )
  177. {
  178. hRes = E_UNEXPECTED;
  179. ::ReleaseMutex ( g_hRefreshMutexLib );
  180. break;
  181. }
  182. }
  183. case WAIT_OBJECT_0 + 1:
  184. {
  185. // we got a mutex so must reinit because registry might changed
  186. bOwnMutex = TRUE;
  187. bInit = TRUE;
  188. hRes = S_OK;
  189. }
  190. break;
  191. case WAIT_OBJECT_0:
  192. {
  193. hRes = S_FALSE;
  194. }
  195. break;
  196. default:
  197. {
  198. hRes = E_UNEXPECTED;
  199. }
  200. break;
  201. }
  202. }
  203. if ( hRes == S_OK )
  204. {
  205. dwWaitResult = ::WaitForSingleObject ( g_hRefreshFlag, INFINITE );
  206. if ( dwWaitResult == WAIT_OBJECT_0 )
  207. {
  208. // mutex guarding registry has to be cleared
  209. bOwnFlag = TRUE;
  210. // call refresh procedure
  211. hRes = m_Stuff.Generate ( FALSE, type );
  212. if SUCCEEDED ( hRes )
  213. {
  214. // reinit because registry might changed
  215. bInit = TRUE;
  216. }
  217. }
  218. else
  219. {
  220. hRes = E_UNEXPECTED;
  221. }
  222. }
  223. // if we got a mutex and are supposed to refresh our selves
  224. if ( bInitialize && bInit )
  225. {
  226. ///////////////////////////////////////////////////////////////////////
  227. // clear first
  228. ///////////////////////////////////////////////////////////////////////
  229. try
  230. {
  231. // clear internal structure ( obtained from registry )
  232. m_data.ClearPerformanceData();
  233. }
  234. catch ( ... )
  235. {
  236. }
  237. try
  238. {
  239. if ( m_pWMIRefresh )
  240. {
  241. // remove enums :))
  242. m_pWMIRefresh->RemoveHandles();
  243. }
  244. }
  245. catch ( ... )
  246. {
  247. }
  248. ///////////////////////////////////////////////////////////////////////
  249. // obtain registry structure and make arrays
  250. ///////////////////////////////////////////////////////////////////////
  251. if ( ( hRes = m_data.InitializePerformance () ) == S_OK )
  252. {
  253. m_data.Generate ();
  254. if ( m_data.IsValidGenerate () )
  255. {
  256. if ( m_pWMIRefresh )
  257. {
  258. // add handles :))
  259. m_pWMIRefresh->AddHandles ( m_data.GetPerformanceData() );
  260. }
  261. }
  262. else
  263. {
  264. hRes = E_FAIL;
  265. }
  266. }
  267. }
  268. if ( bOwnFlag )
  269. {
  270. ::ReleaseMutex ( g_hRefreshFlag );
  271. }
  272. if ( bOwnMutex )
  273. {
  274. ::ReleaseMutex ( g_hRefreshMutexLib );
  275. ::ReleaseMutex ( g_hRefreshMutex );
  276. }
  277. return hRes;
  278. }