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.

482 lines
12 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2000-2002, Microsoft Corporation.
  4. //
  5. // All rights reserved.
  6. //
  7. // Module Name:
  8. //
  9. // WMIAdapter_App.cpp
  10. //
  11. // Abstract:
  12. //
  13. // module for application
  14. //
  15. // History:
  16. //
  17. // initial a-marius
  18. //
  19. ////////////////////////////////////////////////////////////////////////////////////
  20. #include "PreComp.h"
  21. #include "RefresherUtils.h"
  22. // debuging features
  23. #ifndef _INC_CRTDBG
  24. #include <crtdbg.h>
  25. #endif _INC_CRTDBG
  26. // new stores file/line info
  27. #ifdef _DEBUG
  28. #ifndef NEW
  29. #define NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
  30. #define new NEW
  31. #endif NEW
  32. #endif _DEBUG
  33. // application
  34. #include "WMIAdapter_App.h"
  35. extern WmiAdapterApp _App;
  36. ///////////////////////////////////////////////////////////////////////////
  37. // GLOBAL STUFF
  38. ///////////////////////////////////////////////////////////////////////////
  39. extern LPCWSTR g_szRefreshMutex; // name of mutex for refresh
  40. extern __SmartHANDLE g_hRefreshMutex; // mutex to find out refersh request
  41. extern LPCWSTR g_szRefreshMutexLib;// name of mutex for refresh
  42. extern __SmartHANDLE g_hRefreshMutexLib; // mutex to find out refersh request
  43. extern LPCWSTR g_szRefreshFlag;
  44. extern __SmartHANDLE g_hRefreshFlag;
  45. extern __SmartHANDLE g_hDoneWorkEvt; // event for COM init/uninit ( nonsignaled )
  46. extern __SmartHANDLE g_hDoneWorkEvtCIM; // event for CIM connect/release ( nonsignaled )
  47. extern __SmartHANDLE g_hDoneWorkEvtWMI; // event for WMI connect/release ( nonsignaled )
  48. extern __SmartHANDLE g_hDoneLibEvt; // event for lib connect/disconnect ( nonsignaled )
  49. extern __SmartHANDLE g_hDoneInitEvt; // event for init is finished ( nonsignaled )
  50. extern LPCWSTR g_szAppName;
  51. extern LPCWSTR g_szAppNameGlobal;
  52. ///////////////////////////////////////////////////////////////////////////
  53. // construction & destruction
  54. ///////////////////////////////////////////////////////////////////////////
  55. WmiAdapterApp::WmiAdapterApp( ):
  56. #ifdef __SUPPORT_EVENTVWR
  57. m_hResources ( NULL ),
  58. #endif __SUPPORT_EVENTVWR
  59. m_bInUse ( FALSE ),
  60. m_bManual ( FALSE )
  61. {
  62. ATLTRACE ( L"*************************************************************\n"
  63. L"WmiAdapterApp construction\n"
  64. L"*************************************************************\n" );
  65. ::InitializeCriticalSection ( &m_cs );
  66. }
  67. WmiAdapterApp::~WmiAdapterApp()
  68. {
  69. ATLTRACE ( L"*************************************************************\n"
  70. L"WmiAdapterApp destruction\n"
  71. L"*************************************************************\n" );
  72. ////////////////////////////////////////////////////////////////////////
  73. // release mutex ( previous instance checker :)) )
  74. ////////////////////////////////////////////////////////////////////////
  75. if ( m_hInstance.GetHANDLE() )
  76. {
  77. ::ReleaseMutex ( m_hInstance );
  78. m_hInstance.CloseHandle();
  79. }
  80. ////////////////////////////////////////////////////////////////////////
  81. // release security attributtes
  82. ////////////////////////////////////////////////////////////////////////
  83. try
  84. {
  85. if ( ! pStuff.IsEmpty() )
  86. {
  87. delete pStuff.Detach();
  88. }
  89. }
  90. catch ( ... )
  91. {
  92. pStuff.Detach();
  93. }
  94. #ifdef __SUPPORT_EVENTVWR
  95. ////////////////////////////////////////////////////////////////////////
  96. // release event log
  97. ////////////////////////////////////////////////////////////////////////
  98. try
  99. {
  100. if ( ! pEventLog.IsEmpty() )
  101. {
  102. delete pEventLog.Detach();
  103. }
  104. }
  105. catch ( ... )
  106. {
  107. pEventLog.Detach();
  108. }
  109. #endif __SUPPORT_EVENTVWR
  110. ////////////////////////////////////////////////////////////////////////
  111. // release security attributtes
  112. ////////////////////////////////////////////////////////////////////////
  113. try
  114. {
  115. if ( ! pSA.IsEmpty() )
  116. {
  117. delete pSA.Detach();
  118. }
  119. }
  120. catch ( ... )
  121. {
  122. pSA.Detach();
  123. }
  124. #ifdef __SUPPORT_EVENTVWR
  125. ////////////////////////////////////////////////////////////////////////
  126. // close resources
  127. ////////////////////////////////////////////////////////////////////////
  128. if ( m_hResources )
  129. {
  130. ::FreeLibrary ( m_hResources );
  131. m_hResources = NULL;
  132. }
  133. #endif __SUPPORT_EVENTVWR
  134. ::DeleteCriticalSection ( &m_cs );
  135. #ifdef _DEBUG
  136. _CrtDumpMemoryLeaks();
  137. #endif _DEBUG
  138. }
  139. ///////////////////////////////////////////////////////////////////////////
  140. // exists instance ?
  141. ///////////////////////////////////////////////////////////////////////////
  142. BOOL WmiAdapterApp::Exists ( void )
  143. {
  144. ATLTRACE ( L"*************************************************************\n"
  145. L"WmiAdapterApp exists application\n"
  146. L"*************************************************************\n" );
  147. ////////////////////////////////////////////////////////////////////////
  148. // smart locking/unlocking
  149. ////////////////////////////////////////////////////////////////////////
  150. __Smart_CRITICAL_SECTION scs ( const_cast < LPCRITICAL_SECTION> ( &m_cs ) );
  151. // check instance
  152. if ( m_hInstance.GetHANDLE() == NULL )
  153. {
  154. if ( !pSA.IsEmpty() )
  155. {
  156. if ( m_hInstance.SetHANDLE ( ::CreateMutexW ( pSA->GetSecurityAttributtes(), FALSE, g_szAppNameGlobal ) ), m_hInstance.GetHANDLE() != NULL )
  157. {
  158. if ( ::GetLastError () == ERROR_ALREADY_EXISTS )
  159. {
  160. return TRUE;
  161. }
  162. }
  163. else
  164. {
  165. // m_hInstance.GetHANDLE() == NULL
  166. // something's is very bad
  167. // return we already exists :))
  168. return TRUE;
  169. }
  170. }
  171. else
  172. {
  173. // security is not initialized
  174. // something's is very bad
  175. // return we already exists :))
  176. return TRUE;
  177. }
  178. }
  179. else
  180. {
  181. // something's is very bad
  182. // we should not really be here
  183. return TRUE;
  184. }
  185. return FALSE;
  186. }
  187. ///////////////////////////////////////////////////////////////////////////
  188. // INITIALIZATION
  189. ///////////////////////////////////////////////////////////////////////////
  190. HRESULT WmiAdapterApp::InitKill ( void )
  191. {
  192. HRESULT hRes = S_FALSE;
  193. try
  194. {
  195. if (_App.m_hKill.GetHANDLE() == NULL)
  196. {
  197. if ( ( _App.m_hKill =
  198. ::CreateEvent ( ((WmiSecurityAttributes*)_App)->GetSecurityAttributtes(),
  199. TRUE,
  200. FALSE,
  201. NULL )
  202. ) == NULL )
  203. {
  204. // get error
  205. HRESULT hr = HRESULT_FROM_WIN32 ( ::GetLastError() );
  206. if FAILED ( hr )
  207. {
  208. hRes = hr;
  209. }
  210. else
  211. {
  212. hRes = E_OUTOFMEMORY;
  213. }
  214. }
  215. else
  216. {
  217. hRes = S_OK;
  218. }
  219. }
  220. }
  221. catch ( ... )
  222. {
  223. hRes = HRESULT_FROM_WIN32 ( ERROR_NOT_READY );
  224. }
  225. return hRes;
  226. }
  227. HRESULT WmiAdapterApp::InitAttributes ( void )
  228. {
  229. ATLTRACE ( L"*************************************************************\n"
  230. L"WmiAdapterApp initialization of neccessary attributes\n"
  231. L"*************************************************************\n" );
  232. ////////////////////////////////////////////////////////////////////////
  233. // smart locking/unlocking
  234. ////////////////////////////////////////////////////////////////////////
  235. __Smart_CRITICAL_SECTION scs ( &m_cs );
  236. #ifdef __SUPPORT_EVENTVWR
  237. ////////////////////////////////////////////////////////////////////////
  238. // load resource library
  239. ////////////////////////////////////////////////////////////////////////
  240. if ( ! m_hResources )
  241. {
  242. m_hResources = GetResourceDll();
  243. }
  244. #endif __SUPPORT_EVENTVWR
  245. try
  246. {
  247. ////////////////////////////////////////////////////////////////////////
  248. // create Security descriptor
  249. ////////////////////////////////////////////////////////////////////////
  250. if ( pSA.IsEmpty() && ( pSA.SetData ( new WmiSecurityAttributes() ), pSA.IsEmpty() ) )
  251. {
  252. return E_OUTOFMEMORY;
  253. }
  254. //
  255. // check to see if security is initialized
  256. //
  257. if ( FALSE == pSA->m_bInitialized )
  258. {
  259. return E_FAIL;
  260. }
  261. #ifdef __SUPPORT_EVENTVWR
  262. ////////////////////////////////////////////////////////////////////////
  263. // create event log
  264. ////////////////////////////////////////////////////////////////////////
  265. if ( pEventLog.IsEmpty() && ( pEventLog.SetData( new CPerformanceEventLogBase( L"WMIAdapter" ) ), pEventLog.IsEmpty() ) )
  266. {
  267. return E_OUTOFMEMORY;
  268. }
  269. #endif __SUPPORT_EVENTVWR
  270. }
  271. catch ( ... )
  272. {
  273. return E_FAIL;
  274. }
  275. return S_OK;
  276. }
  277. HRESULT WmiAdapterApp::Init ( void )
  278. {
  279. ATLTRACE ( L"*************************************************************\n"
  280. L"WmiAdapterApp initialization\n"
  281. L"*************************************************************\n" );
  282. ////////////////////////////////////////////////////////////////////////
  283. // smart locking/unlocking
  284. ////////////////////////////////////////////////////////////////////////
  285. __Smart_CRITICAL_SECTION scs ( &m_cs );
  286. #ifdef __SUPPORT_EVENTVWR
  287. ////////////////////////////////////////////////////////////////////////
  288. // load resource library
  289. ////////////////////////////////////////////////////////////////////////
  290. if ( ! m_hResources )
  291. {
  292. m_hResources = GetResourceDll();
  293. }
  294. #endif __SUPPORT_EVENTVWR
  295. try
  296. {
  297. ////////////////////////////////////////////////////////////////////////
  298. // create stuff
  299. ////////////////////////////////////////////////////////////////////////
  300. if ( pStuff.IsEmpty() && ( pStuff.SetData( new WmiAdapterStuff( ) ), pStuff.IsEmpty() ) )
  301. {
  302. return E_OUTOFMEMORY;
  303. }
  304. }
  305. catch ( ... )
  306. {
  307. return E_FAIL;
  308. }
  309. #ifdef __SUPPORT_WAIT
  310. m_hData = ::CreateEventW( pSA->GetSecurityAttributtes(),
  311. TRUE,
  312. FALSE,
  313. L"Global\\WmiAdapterDataReady"
  314. );
  315. #endif __SUPPORT_WAIT
  316. if ( ( m_hInit = ::CreateSemaphoreW( pSA->GetSecurityAttributtes(),
  317. 0L,
  318. 100L,
  319. L"Global\\WmiAdapterInit"
  320. )
  321. ) == NULL )
  322. {
  323. // this is really important to have
  324. return E_OUTOFMEMORY;
  325. }
  326. if ( ( m_hUninit= ::CreateSemaphoreW( pSA->GetSecurityAttributtes(),
  327. 0L,
  328. 100L,
  329. L"Global\\WmiAdapterUninit"
  330. )
  331. ) == NULL )
  332. {
  333. // this is really important to have
  334. return E_OUTOFMEMORY;
  335. }
  336. ///////////////////////////////////////////////////////////////////////////
  337. // GLOBAL STUFF
  338. ///////////////////////////////////////////////////////////////////////////
  339. if ( ! g_hRefreshMutex )
  340. {
  341. if ( ( g_hRefreshMutex = ::CreateMutex (
  342. pSA->GetSecurityAttributtes(),
  343. FALSE,
  344. g_szRefreshMutex
  345. )
  346. ) == NULL )
  347. {
  348. // this is really important to have
  349. return E_OUTOFMEMORY;
  350. }
  351. }
  352. if ( ! g_hRefreshMutexLib )
  353. {
  354. if ( ( g_hRefreshMutexLib = ::CreateMutex (
  355. pSA->GetSecurityAttributtes(),
  356. FALSE,
  357. g_szRefreshMutexLib
  358. )
  359. ) == NULL )
  360. {
  361. // this is really important to have
  362. return E_OUTOFMEMORY;
  363. }
  364. }
  365. if ( ! g_hRefreshFlag )
  366. {
  367. if ( ( g_hRefreshFlag = ::CreateMutex (
  368. pSA->GetSecurityAttributtes(),
  369. FALSE,
  370. g_szRefreshFlag
  371. )
  372. ) == NULL )
  373. {
  374. // this is really important to have
  375. return E_OUTOFMEMORY;
  376. }
  377. }
  378. if ( ! g_hDoneWorkEvt )
  379. {
  380. if ( ( g_hDoneWorkEvt = ::CreateEvent ( NULL, TRUE, FALSE, NULL ) ) == NULL )
  381. {
  382. // this is really important to have
  383. return E_OUTOFMEMORY;
  384. }
  385. }
  386. if ( ! g_hDoneWorkEvtCIM )
  387. {
  388. if ( ( g_hDoneWorkEvtCIM = ::CreateEvent ( NULL, TRUE, FALSE, NULL ) ) == NULL )
  389. {
  390. // this is really important to have
  391. return E_OUTOFMEMORY;
  392. }
  393. }
  394. if ( ! g_hDoneWorkEvtWMI )
  395. {
  396. if ( ( g_hDoneWorkEvtWMI = ::CreateEvent ( NULL, TRUE, FALSE, NULL ) ) == NULL )
  397. {
  398. // this is really important to have
  399. return E_OUTOFMEMORY;
  400. }
  401. }
  402. if ( ! g_hDoneInitEvt )
  403. {
  404. if ( ( g_hDoneInitEvt = ::CreateEvent ( NULL, TRUE, FALSE, NULL ) ) == NULL )
  405. {
  406. // this is really important to have
  407. return E_OUTOFMEMORY;
  408. }
  409. }
  410. if ( ! g_hDoneLibEvt )
  411. {
  412. if ( ( g_hDoneLibEvt = ::CreateEvent ( NULL, TRUE, FALSE, NULL ) ) == NULL )
  413. {
  414. // this is really important to have
  415. return E_OUTOFMEMORY;
  416. }
  417. }
  418. return S_OK;
  419. }
  420. void WmiAdapterApp::Term ( void )
  421. {
  422. }