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.

473 lines
8.8 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include <provexpt.h>
  14. #include <snmptempl.h>
  15. #include <snmpmt.h>
  16. #include <typeinfo.h>
  17. #include <process.h>
  18. #include <objbase.h>
  19. #include <stdio.h>
  20. #include <wbemidl.h>
  21. #include <snmplog.h>
  22. #include <snmpcl.h>
  23. #include <snmpcont.h>
  24. #include <snmptype.h>
  25. #include <snmpauto.h>
  26. #include <snmpevt.h>
  27. #include <snmpthrd.h>
  28. #include <snmpobj.h>
  29. #include <classfac.h>
  30. #include <smir.h>
  31. #include <notify.h>
  32. #include <evtdefs.h>
  33. #include <evtthrd.h>
  34. #include <evtmap.h>
  35. #include <evtprov.h>
  36. extern CRITICAL_SECTION g_CacheCriticalSection;
  37. extern CEventProviderWorkerThread* g_pWorkerThread;
  38. CWbemServerWrap::CWbemServerWrap(IWbemServices *pServ) : m_ref ( 0 ), m_Serv(NULL)
  39. {
  40. m_Serv = pServ;
  41. if (m_Serv)
  42. {
  43. m_Serv->AddRef();
  44. }
  45. g_pWorkerThread->AddClassesToCache((DWORD_PTR)(&m_ClassMap), &m_ClassMap);
  46. }
  47. CWbemServerWrap::~CWbemServerWrap()
  48. {
  49. if (m_Serv)
  50. {
  51. m_Serv->Release();
  52. }
  53. g_pWorkerThread->RemoveClassesFromCache((DWORD_PTR)(&m_ClassMap));
  54. m_ClassMap.RemoveAll();
  55. }
  56. ULONG CWbemServerWrap::AddRef()
  57. {
  58. return (ULONG)(InterlockedIncrement(&m_ref));
  59. }
  60. ULONG CWbemServerWrap::Release()
  61. {
  62. ULONG i = (ULONG)(InterlockedDecrement(&m_ref));
  63. if (i == 0)
  64. {
  65. delete this;
  66. }
  67. return i;
  68. }
  69. HRESULT CWbemServerWrap::GetMapperObject(BSTR a_path, IWbemContext *a_pCtx, IWbemClassObject **a_ppObj)
  70. {
  71. if (a_ppObj == NULL)
  72. {
  73. return WBEM_E_FAILED;
  74. }
  75. *a_ppObj = NULL;
  76. HRESULT result = WBEM_NO_ERROR;
  77. SCacheEntry *t_CacheEntry = NULL;
  78. EnterCriticalSection ( & g_CacheCriticalSection ) ;
  79. if (!m_ClassMap.Lookup(a_path, t_CacheEntry))
  80. {
  81. DebugMacro14(
  82. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  83. __FILE__,__LINE__,
  84. L"Cache Miss [%s]\r\n" , a_path
  85. );
  86. )
  87. LPUNKNOWN pInterrogativeInt = NULL;
  88. result = CoCreateInstance (
  89. CLSID_SMIR_Database,
  90. NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
  91. IID_ISMIR_Interrogative,
  92. (void**)&pInterrogativeInt
  93. );
  94. if ((result != S_OK) || (NULL == pInterrogativeInt))
  95. {
  96. DebugMacro9(
  97. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  98. __FILE__,__LINE__,
  99. L"CEncapMapper::GetSpecificClass failed to connect to SMIR\r\n");
  100. )
  101. return result ;
  102. }
  103. result = ((ISmirInterrogator*)pInterrogativeInt)->GetWBEMClass(a_ppObj, a_path);
  104. pInterrogativeInt->Release();
  105. if ( SUCCEEDED ( result ) )
  106. {
  107. t_CacheEntry = new SCacheEntry(*a_ppObj);
  108. m_ClassMap[a_path] = t_CacheEntry;
  109. (*a_ppObj)->AddRef();
  110. }
  111. else
  112. {
  113. t_CacheEntry = new SCacheEntry(NULL);
  114. m_ClassMap[a_path] = t_CacheEntry;
  115. }
  116. }
  117. else
  118. {
  119. if ( t_CacheEntry->m_Class )
  120. {
  121. *a_ppObj = t_CacheEntry->m_Class;
  122. (*a_ppObj)->AddRef();
  123. }
  124. else
  125. {
  126. result = WBEM_E_NOT_FOUND ;
  127. }
  128. }
  129. LeaveCriticalSection ( & g_CacheCriticalSection ) ;
  130. return result ;
  131. }
  132. HRESULT CWbemServerWrap::GetObject(BSTR a_path, IWbemContext *a_pCtx, IWbemClassObject **a_ppObj)
  133. {
  134. if ((a_ppObj == NULL) || (m_Serv == NULL))
  135. {
  136. return WBEM_E_FAILED;
  137. }
  138. *a_ppObj = NULL;
  139. HRESULT result = WBEM_NO_ERROR;
  140. SCacheEntry *t_CacheEntry = NULL;
  141. EnterCriticalSection ( & g_CacheCriticalSection ) ;
  142. if (!m_ClassMap.Lookup(a_path, t_CacheEntry))
  143. {
  144. DebugMacro14(
  145. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  146. __FILE__,__LINE__,
  147. L"Cache Miss [%s]\r\n" , a_path
  148. );
  149. )
  150. result = m_Serv->GetObject(a_path, 0, a_pCtx, a_ppObj, NULL);
  151. if ( SUCCEEDED(result) )
  152. {
  153. t_CacheEntry = new SCacheEntry(*a_ppObj);
  154. m_ClassMap[a_path] = t_CacheEntry;
  155. (*a_ppObj)->AddRef();
  156. }
  157. }
  158. else
  159. {
  160. *a_ppObj = t_CacheEntry->m_Class;
  161. (*a_ppObj)->AddRef();
  162. }
  163. LeaveCriticalSection ( & g_CacheCriticalSection ) ;
  164. return result;
  165. }
  166. STDMETHODIMP CTrapEventProvider::Initialize (
  167. LPWSTR pszUser,
  168. LONG lFlags,
  169. LPWSTR pszNamespace,
  170. LPWSTR pszLocale,
  171. IWbemServices *pCIMOM, // For anybody
  172. IWbemContext *pCtx,
  173. IWbemProviderInitSink *pInitSink // For init signals
  174. )
  175. {
  176. SetStructuredExceptionHandler seh;
  177. try
  178. {
  179. DebugMacro9(
  180. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  181. __FILE__,__LINE__,
  182. L"Entering CTrapEventProvider::Initialize\n");
  183. )
  184. m_pNamespace = new CWbemServerWrap(pCIMOM);
  185. m_pNamespace->AddRef();
  186. pInitSink->SetStatus ( WBEM_S_INITIALIZED , 0 );
  187. DebugMacro9(
  188. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  189. __FILE__,__LINE__,
  190. L"Leaving CTrapEventProvider::Initialize with SUCCEEDED\n");
  191. )
  192. return WBEM_NO_ERROR;
  193. }
  194. catch(Structured_Exception e_SE)
  195. {
  196. return WBEM_E_UNEXPECTED;
  197. }
  198. catch(Heap_Exception e_HE)
  199. {
  200. return WBEM_E_OUT_OF_MEMORY;
  201. }
  202. catch(...)
  203. {
  204. return WBEM_E_UNEXPECTED;
  205. }
  206. }
  207. STDMETHODIMP CTrapEventProvider::ProvideEvents(IWbemObjectSink* pSink, LONG lFlags)
  208. {
  209. SetStructuredExceptionHandler seh;
  210. try
  211. {
  212. DebugMacro9(
  213. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  214. __FILE__,__LINE__,
  215. L"Entering CTrapEventProvider::ProvideEvents\n");
  216. )
  217. m_pEventSink = pSink;
  218. m_pEventSink->AddRef();
  219. if (!m_thrd->Register(this))
  220. {
  221. DebugMacro9(
  222. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  223. __FILE__,__LINE__,
  224. L"Leaving CTrapEventProvider::ProvideEvents with FAILED\n");
  225. )
  226. return WBEM_E_FAILED;
  227. }
  228. DebugMacro9(
  229. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  230. __FILE__,__LINE__,
  231. L"Leaving CNTEventProvider::ProvideEvents with SUCCEEDED\n");
  232. )
  233. return WBEM_NO_ERROR;
  234. }
  235. catch(Structured_Exception e_SE)
  236. {
  237. return WBEM_E_UNEXPECTED;
  238. }
  239. catch(Heap_Exception e_HE)
  240. {
  241. return WBEM_E_OUT_OF_MEMORY;
  242. }
  243. catch(...)
  244. {
  245. return WBEM_E_UNEXPECTED;
  246. }
  247. }
  248. CTrapEventProvider::~CTrapEventProvider()
  249. {
  250. if (m_thrd)
  251. {
  252. m_thrd->UnRegister(this);
  253. }
  254. if ( m_pEventSink )
  255. {
  256. m_pEventSink->Release () ;
  257. }
  258. if ( m_pNamespace )
  259. {
  260. m_pNamespace->Release () ;
  261. }
  262. }
  263. CTrapEventProvider::CTrapEventProvider(DWORD mapperType, CEventProviderThread* thrd)
  264. : m_thrd (NULL),
  265. m_pNamespace (NULL),
  266. m_pEventSink (NULL)
  267. {
  268. m_thrd = thrd;
  269. m_MapType = mapperType;
  270. m_ref = 0;
  271. }
  272. CWbemServerWrap* CTrapEventProvider::GetNamespace()
  273. {
  274. m_pNamespace->AddRef();
  275. return m_pNamespace;
  276. }
  277. IWbemObjectSink* CTrapEventProvider::GetEventSink()
  278. {
  279. m_pEventSink->AddRef();
  280. return m_pEventSink;
  281. }
  282. void CTrapEventProvider::ReleaseAll()
  283. {
  284. //release dependencies
  285. m_pNamespace->Release();
  286. m_pEventSink->Release();
  287. if ( 0 != InterlockedDecrement(&m_ref) )
  288. {
  289. InterlockedDecrement(&(CSNMPEventProviderClassFactory::objectsInProgress));
  290. return;
  291. }
  292. delete this;
  293. InterlockedDecrement(&(CSNMPEventProviderClassFactory::objectsInProgress));
  294. return;
  295. }
  296. void CTrapEventProvider::AddRefAll()
  297. {
  298. //addref dependencies
  299. m_pNamespace->AddRef();
  300. m_pEventSink->AddRef();
  301. InterlockedIncrement(&(CSNMPEventProviderClassFactory::objectsInProgress));
  302. InterlockedIncrement ( &m_ref ) ;
  303. }
  304. STDMETHODIMP_( ULONG ) CTrapEventProvider::AddRef()
  305. {
  306. SetStructuredExceptionHandler seh;
  307. try
  308. {
  309. InterlockedIncrement(&(CSNMPEventProviderClassFactory::objectsInProgress));
  310. return InterlockedIncrement ( &m_ref ) ;
  311. }
  312. catch(Structured_Exception e_SE)
  313. {
  314. return 0;
  315. }
  316. catch(Heap_Exception e_HE)
  317. {
  318. return 0;
  319. }
  320. catch(...)
  321. {
  322. return 0;
  323. }
  324. }
  325. STDMETHODIMP_(ULONG) CTrapEventProvider::Release()
  326. {
  327. SetStructuredExceptionHandler seh;
  328. try
  329. {
  330. long ret;
  331. if ( 0 != (ret = InterlockedDecrement(&m_ref)) )
  332. {
  333. InterlockedDecrement(&(CSNMPEventProviderClassFactory::objectsInProgress));
  334. return ret;
  335. }
  336. delete this;
  337. InterlockedDecrement(&(CSNMPEventProviderClassFactory::objectsInProgress));
  338. return 0;
  339. }
  340. catch(Structured_Exception e_SE)
  341. {
  342. return 0;
  343. }
  344. catch(Heap_Exception e_HE)
  345. {
  346. return 0;
  347. }
  348. catch(...)
  349. {
  350. return 0;
  351. }
  352. }
  353. STDMETHODIMP CTrapEventProvider::QueryInterface(REFIID riid, PVOID* ppv)
  354. {
  355. SetStructuredExceptionHandler seh;
  356. try
  357. {
  358. *ppv = NULL;
  359. if (IID_IUnknown == riid)
  360. {
  361. *ppv= (IWbemEventProvider*)this;
  362. }
  363. else if (IID_IWbemEventProvider == riid)
  364. {
  365. *ppv=(IWbemEventProvider*)this;
  366. }
  367. else if (IID_IWbemProviderInit == riid)
  368. {
  369. *ppv=(IWbemProviderInit*)this;
  370. }
  371. if (NULL==*ppv)
  372. {
  373. return E_NOINTERFACE;
  374. }
  375. //AddRef any interface we'll return.
  376. ((LPUNKNOWN)*ppv)->AddRef();
  377. return NOERROR;
  378. }
  379. catch(Structured_Exception e_SE)
  380. {
  381. return E_UNEXPECTED;
  382. }
  383. catch(Heap_Exception e_HE)
  384. {
  385. return E_OUTOFMEMORY;
  386. }
  387. catch(...)
  388. {
  389. return E_UNEXPECTED;
  390. }
  391. }