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.

949 lines
20 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 <cominit.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. #include <evtencap.h>
  37. #include <evtreft.h>
  38. extern CEventProviderWorkerThread *g_pWorkerThread ;
  39. extern CRITICAL_SECTION g_CacheCriticalSection;
  40. CTrapListener::CTrapListener(CEventProviderThread* parentptr)
  41. {
  42. m_pParent = parentptr;
  43. m_Ref = 1;
  44. }
  45. void CTrapListener::Destroy()
  46. {
  47. if (InterlockedDecrement(&m_Ref) == 0)
  48. {
  49. DestroyReceiver();
  50. }
  51. }
  52. void CTrapListener::Receive (SnmpTransportAddress &sender_addr,
  53. SnmpSecurity &security_context,
  54. SnmpVarBindList &vbList)
  55. {
  56. InterlockedIncrement(&m_Ref);
  57. MySnmpV1Security context((const SnmpV1Security&)security_context);
  58. const char *security = context.GetName();
  59. const char *addr = sender_addr.GetAddress();
  60. if ((NULL == security) || (NULL == addr))
  61. {
  62. DebugMacro9(
  63. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  64. __FILE__,__LINE__,
  65. L"CTrapListener::Receive invalid community or address\r\n");
  66. )
  67. return;
  68. }
  69. const char *transport = NULL;
  70. if(typeid(SnmpTransportIpAddress) == typeid(sender_addr))
  71. {
  72. transport = "IP";
  73. }
  74. else if(typeid(SnmpTransportIpxAddress) == typeid(sender_addr))
  75. {
  76. transport = "IPX";
  77. }
  78. else
  79. {
  80. transport = "UNKNOWN";
  81. }
  82. char *oid = NULL;
  83. // reset the list
  84. vbList.Reset();
  85. UINT x = 0;
  86. // Get the SnmpTrapOid call process trap.
  87. vbList.Next(); //the timestamp
  88. vbList.Next(); //the snmpTrapOID
  89. const SnmpVarBind *var_bind = vbList.Get();
  90. const SnmpObjectIdentifier &id = var_bind->GetInstance();
  91. if (id != SNMP_TRAP_OID)
  92. {
  93. DebugMacro9(
  94. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  95. __FILE__,__LINE__,
  96. L"CTrapListener::Receive invalid trap oid varbind\r\n");
  97. )
  98. return;
  99. }
  100. const SnmpValue &val = var_bind->GetValue();
  101. if(typeid(SnmpObjectIdentifier) == typeid(val))
  102. {
  103. oid = ((const SnmpObjectIdentifier&)val).GetAllocatedString();
  104. }
  105. if (NULL == oid)
  106. {
  107. DebugMacro9(
  108. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  109. __FILE__,__LINE__,
  110. L"CTrapListener::Receive invalid oid\r\n");
  111. )
  112. return;
  113. }
  114. DebugMacro9(
  115. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  116. __FILE__,__LINE__,
  117. L"CTrapListener::Receive trap to process\r\n");
  118. )
  119. m_pParent->ProcessTrap(addr, security, oid, transport, vbList);
  120. delete [] oid;
  121. Destroy();
  122. }
  123. void CEventProviderThread::UnRegister(CTrapEventProvider* prov)
  124. {
  125. DebugMacro9(
  126. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  127. __FILE__,__LINE__,
  128. L"CEventProviderThread::UnRegister\r\n");
  129. )
  130. if (m_ControlObjects.Lock())
  131. {
  132. if (m_ControlObjects.RemoveKey((UINT_PTR)prov)
  133. && m_ControlObjects.IsEmpty() && (NULL != m_Ear))
  134. {
  135. m_Ear->Destroy();
  136. m_Ear = NULL;
  137. }
  138. m_ControlObjects.Unlock();
  139. }
  140. }
  141. BOOL CEventProviderThread::Register(CTrapEventProvider* prov)
  142. {
  143. if (m_ControlObjects.Lock())
  144. {
  145. m_ControlObjects.SetAt((UINT_PTR)prov, prov);
  146. m_ControlObjects.Unlock();
  147. if (NULL == m_Ear)
  148. {
  149. m_Ear = new CTrapListener(this);
  150. }
  151. if (m_Ear->IsRegistered())
  152. {
  153. DebugMacro9(
  154. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  155. __FILE__,__LINE__,
  156. L"CEventProviderThread::Register returning TRUE\r\n");
  157. )
  158. return TRUE;
  159. }
  160. else
  161. {
  162. delete m_Ear;
  163. m_Ear = NULL;
  164. }
  165. }
  166. DebugMacro9(
  167. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  168. __FILE__,__LINE__,
  169. L"CEventProviderThread::Register returning FALSE\r\n");
  170. )
  171. return FALSE;
  172. }
  173. void CEventProviderThread::ProcessTrap(const char *sender_addr, const char *security_Context,
  174. const char *snmpTrapOid, const char *trnsp,
  175. SnmpVarBindList &vbList)
  176. {
  177. DebugMacro9(
  178. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  179. __FILE__,__LINE__,
  180. L"Entering CEventProviderThread::ProcessTrap\r\n");
  181. )
  182. CList<CTrapEventProvider *, CTrapEventProvider *> controlObjects;
  183. if (m_ControlObjects.Lock())
  184. {
  185. POSITION pos = m_ControlObjects.GetStartPosition();
  186. while (NULL != pos)
  187. {
  188. CTrapEventProvider *pCntrl;
  189. UINT key;
  190. m_ControlObjects.GetNextAssoc(pos, key, pCntrl);
  191. pCntrl->AddRefAll();
  192. controlObjects.AddTail(pCntrl);
  193. }
  194. m_ControlObjects.Unlock();
  195. }
  196. else
  197. {
  198. DebugMacro9(
  199. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  200. __FILE__,__LINE__,
  201. L"CEventProviderThread::ProcessTrap Failed to lock Control objects\r\n");
  202. )
  203. return;
  204. }
  205. //loop through the different control objects to see if an event class
  206. //should be sent. if yes, then generate the event class. if generating
  207. //the specific class fails try the generic case.
  208. CTrapData TrapData (sender_addr, security_Context, snmpTrapOid, trnsp, vbList);
  209. while (!controlObjects.IsEmpty())
  210. {
  211. CTrapEventProvider *pCntrl = controlObjects.RemoveTail();
  212. CTrapProcessTaskObject asyncTrapTask ( & TrapData, pCntrl);
  213. asyncTrapTask.Process();
  214. pCntrl->ReleaseAll();
  215. }
  216. DebugMacro9(
  217. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  218. __FILE__,__LINE__,
  219. L"CEventProviderThread::ProcessTrap done!\r\n");
  220. )
  221. }
  222. CTrapData::CTrapData (
  223. const char *sender_addr,
  224. const char *security_Context,
  225. const char *snmpTrapOid,
  226. const char *trnsp,
  227. SnmpVarBindList &vbList
  228. ) : m_variable_bindings(vbList),
  229. m_Ref(0),
  230. m_sender_addr(NULL),
  231. m_security_context(NULL),
  232. m_trap_oid(NULL),
  233. m_transport(NULL)
  234. {
  235. if (sender_addr)
  236. {
  237. int t_sender_len = strlen(sender_addr) + 1 ;
  238. if ( t_sender_len > 32 )
  239. {
  240. m_sender_addr = new char[strlen(sender_addr) + 1];
  241. strcpy(m_sender_addr, sender_addr);
  242. }
  243. else
  244. {
  245. m_sender_addr = NULL ;
  246. strcpy(m_static_sender_addr, sender_addr);
  247. }
  248. }
  249. if (security_Context)
  250. {
  251. int t_security_Context_len = strlen(security_Context) + 1 ;
  252. if ( t_security_Context_len > 32 )
  253. {
  254. m_security_context = new char[strlen(security_Context) + 1];
  255. strcpy(m_security_context, security_Context);
  256. }
  257. else
  258. {
  259. m_security_context = NULL ;
  260. strcpy(m_static_security_context, security_Context);
  261. }
  262. }
  263. if (snmpTrapOid)
  264. {
  265. int t_trap_oid_len = strlen(snmpTrapOid) + 1 ;
  266. if ( t_trap_oid_len > 32 )
  267. {
  268. m_trap_oid = new char[strlen(snmpTrapOid) + 1];
  269. strcpy(m_trap_oid, snmpTrapOid);
  270. }
  271. else
  272. {
  273. m_trap_oid = NULL ;
  274. strcpy(m_static_trap_oid, snmpTrapOid);
  275. }
  276. }
  277. if (trnsp)
  278. {
  279. int t_trnsp_len = strlen(trnsp) + 1 ;
  280. if ( t_trnsp_len > 32 )
  281. {
  282. m_transport = new char[strlen(trnsp) + 1];
  283. strcpy(m_transport, trnsp);
  284. }
  285. else
  286. {
  287. m_transport = NULL ;
  288. strcpy(m_static_transport, trnsp);
  289. }
  290. }
  291. }
  292. CTrapData::~CTrapData()
  293. {
  294. if (m_sender_addr)
  295. {
  296. delete [] m_sender_addr;
  297. }
  298. if (m_security_context)
  299. {
  300. delete [] m_security_context;
  301. }
  302. if (m_trap_oid)
  303. {
  304. delete [] m_trap_oid;
  305. }
  306. if (m_transport)
  307. {
  308. delete [] m_transport;
  309. }
  310. }
  311. LONG CTrapData::AddRef()
  312. {
  313. return InterlockedIncrement ( &m_Ref ) ;
  314. }
  315. LONG CTrapData::Release()
  316. {
  317. long ret;
  318. if ( 0 != (ret = InterlockedDecrement(&m_Ref)) )
  319. {
  320. return ret;
  321. }
  322. delete this;
  323. return 0;
  324. }
  325. CTrapProcessTaskObject::CTrapProcessTaskObject (CTrapData *pTData, CTrapEventProvider* pCntrl) : m_Cntrl (NULL)
  326. {
  327. if (pCntrl)
  328. {
  329. m_Cntrl = pCntrl;
  330. m_Cntrl->AddRefAll();
  331. }
  332. if (pTData)
  333. {
  334. m_trap_data = pTData;
  335. }
  336. else
  337. {
  338. m_trap_data = NULL;
  339. }
  340. }
  341. CTrapProcessTaskObject::~CTrapProcessTaskObject()
  342. {
  343. if (m_Cntrl)
  344. {
  345. m_Cntrl->ReleaseAll();
  346. }
  347. }
  348. void CTrapProcessTaskObject::Process()
  349. {
  350. if (m_Cntrl->m_MapType == CMapToEvent::EMappingType::ENCAPSULATED_MAPPER)
  351. {
  352. ProcessEncapsulated () ;
  353. }
  354. else //must be referent
  355. {
  356. ProcessReferent () ;
  357. }
  358. }
  359. void CTrapProcessTaskObject::ProcessReferent ()
  360. {
  361. CWbemServerWrap *ns = m_Cntrl->GetNamespace();
  362. IWbemObjectSink *es = m_Cntrl->GetEventSink();
  363. CReferentMapper mapper ;
  364. mapper.SetData (
  365. m_trap_data->m_sender_addr ? m_trap_data->m_sender_addr : m_trap_data->m_static_sender_addr ,
  366. m_trap_data->m_security_context ? m_trap_data->m_security_context : m_trap_data->m_static_security_context ,
  367. m_trap_data->m_trap_oid ? m_trap_data->m_trap_oid : m_trap_data->m_static_trap_oid ,
  368. m_trap_data->m_transport ? m_trap_data->m_transport : m_trap_data->m_static_transport ,
  369. m_trap_data->m_variable_bindings,
  370. ns
  371. );
  372. #ifdef FILTERING
  373. //is the specific filter set?
  374. if (SUCCEEDED(es->CheckObject(mapper, NULL, NULL)))
  375. #endif //FILTERING
  376. {
  377. IWbemClassObject *Evt = NULL;
  378. DebugMacro9(
  379. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  380. __FILE__,__LINE__,
  381. L"CTrapProcessTaskObject::Process generating specific instance\r\n");
  382. )
  383. mapper.GenerateInstance(&Evt);
  384. //only indicate if specific worked
  385. if (Evt != NULL)
  386. {
  387. DebugMacro9(
  388. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  389. __FILE__,__LINE__,
  390. L"CTrapProcessTaskObject::Process indicating specific instance\r\n");
  391. )
  392. es->Indicate(1, &Evt);
  393. Evt->Release();
  394. }
  395. else if (!mapper.TriedGeneric()) //have we tried the generic filter
  396. {
  397. mapper.ResetData();
  398. mapper.SetTryGeneric();
  399. mapper.SetData (
  400. m_trap_data->m_sender_addr ? m_trap_data->m_sender_addr : m_trap_data->m_static_sender_addr ,
  401. m_trap_data->m_security_context ? m_trap_data->m_security_context : m_trap_data->m_static_security_context ,
  402. m_trap_data->m_trap_oid ? m_trap_data->m_trap_oid : m_trap_data->m_static_trap_oid ,
  403. m_trap_data->m_transport ? m_trap_data->m_transport : m_trap_data->m_static_transport ,
  404. m_trap_data->m_variable_bindings,
  405. ns
  406. );
  407. //is the generic filter set?
  408. #ifdef FILTERING
  409. if (SUCCEEDED(es->CheckObject(m_Map, NULL, NULL)))
  410. #endif //FILTERING
  411. {
  412. IWbemClassObject *stdEvt = NULL;
  413. DebugMacro9(
  414. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  415. __FILE__,__LINE__,
  416. L"CTrapProcessTaskObject::Process generating general instance\r\n");
  417. )
  418. mapper.GenerateInstance(&stdEvt);
  419. //if we generated the class indicate
  420. if (NULL != stdEvt)
  421. {
  422. DebugMacro9(
  423. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  424. __FILE__,__LINE__,
  425. L"CTrapProcessTaskObject::Process indicating general instance\r\n");
  426. )
  427. es->Indicate(1, &stdEvt);
  428. stdEvt->Release();
  429. }
  430. else
  431. {
  432. DebugMacro9(
  433. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  434. __FILE__,__LINE__,
  435. L"CTrapProcessTaskObject::Process generating generic instance failed\r\n");
  436. )
  437. }
  438. }
  439. }
  440. else
  441. {
  442. //the specific case was the generic case
  443. }
  444. }
  445. mapper.ResetData();
  446. es->Release();
  447. ns->Release();
  448. }
  449. void CTrapProcessTaskObject::ProcessEncapsulated ()
  450. {
  451. CWbemServerWrap *ns = m_Cntrl->GetNamespace();
  452. IWbemObjectSink *es = m_Cntrl->GetEventSink();
  453. CEncapMapper mapper ;
  454. mapper.SetData (
  455. m_trap_data->m_sender_addr ? m_trap_data->m_sender_addr : m_trap_data->m_static_sender_addr ,
  456. m_trap_data->m_security_context ? m_trap_data->m_security_context : m_trap_data->m_static_security_context ,
  457. m_trap_data->m_trap_oid ? m_trap_data->m_trap_oid : m_trap_data->m_static_trap_oid ,
  458. m_trap_data->m_transport ? m_trap_data->m_transport : m_trap_data->m_static_transport ,
  459. m_trap_data->m_variable_bindings,
  460. ns
  461. );
  462. #ifdef FILTERING
  463. //is the specific filter set?
  464. if (SUCCEEDED(es->CheckObject(mapper, NULL, NULL)))
  465. #endif //FILTERING
  466. {
  467. IWbemClassObject *Evt = NULL;
  468. DebugMacro9(
  469. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  470. __FILE__,__LINE__,
  471. L"CTrapProcessTaskObject::Process generating specific instance\r\n");
  472. )
  473. mapper.GenerateInstance(&Evt);
  474. //only indicate if specific worked
  475. if (Evt != NULL)
  476. {
  477. DebugMacro9(
  478. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  479. __FILE__,__LINE__,
  480. L"CTrapProcessTaskObject::Process indicating specific instance\r\n");
  481. )
  482. es->Indicate(1, &Evt);
  483. Evt->Release();
  484. }
  485. else if (!mapper.TriedGeneric()) //have we tried the generic filter
  486. {
  487. mapper.ResetData();
  488. mapper.SetTryGeneric();
  489. mapper.SetData (
  490. m_trap_data->m_sender_addr ? m_trap_data->m_sender_addr : m_trap_data->m_static_sender_addr ,
  491. m_trap_data->m_security_context ? m_trap_data->m_security_context : m_trap_data->m_static_security_context ,
  492. m_trap_data->m_trap_oid ? m_trap_data->m_trap_oid : m_trap_data->m_static_trap_oid ,
  493. m_trap_data->m_transport ? m_trap_data->m_transport : m_trap_data->m_static_transport ,
  494. m_trap_data->m_variable_bindings,
  495. ns
  496. );
  497. //is the generic filter set?
  498. #ifdef FILTERING
  499. if (SUCCEEDED(es->CheckObject(m_Map, NULL, NULL)))
  500. #endif //FILTERING
  501. {
  502. IWbemClassObject* stdEvt = NULL;
  503. DebugMacro9(
  504. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  505. __FILE__,__LINE__,
  506. L"CTrapProcessTaskObject::Process generating general instance\r\n");
  507. )
  508. mapper.GenerateInstance(&stdEvt);
  509. //if we generated the class indicate
  510. if (NULL != stdEvt)
  511. {
  512. DebugMacro9(
  513. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  514. __FILE__,__LINE__,
  515. L"CTrapProcessTaskObject::Process indicating general instance\r\n");
  516. )
  517. es->Indicate(1, &stdEvt);
  518. stdEvt->Release();
  519. }
  520. else
  521. {
  522. DebugMacro9(
  523. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  524. __FILE__,__LINE__,
  525. L"CTrapProcessTaskObject::Process generating generic instance failed\r\n");
  526. )
  527. }
  528. }
  529. }
  530. else
  531. {
  532. //the specific case was the generic case
  533. }
  534. }
  535. mapper.ResetData();
  536. es->Release();
  537. ns->Release();
  538. }
  539. TimerRegistryTaskObject :: TimerRegistryTaskObject (CEventProviderWorkerThread *parent) : m_LogKey ( NULL )
  540. {
  541. m_parent = parent;
  542. ReadRegistry();
  543. }
  544. TimerRegistryTaskObject :: ~TimerRegistryTaskObject ()
  545. {
  546. if ( m_LogKey )
  547. RegCloseKey ( m_LogKey ) ;
  548. }
  549. void TimerRegistryTaskObject :: Process ()
  550. {
  551. ReadRegistry();
  552. SetRegistryNotification () ;
  553. }
  554. void TimerRegistryTaskObject :: ReadRegistry ()
  555. {
  556. if (m_LogKey == NULL)
  557. {
  558. LONG t_Status = RegCreateKeyEx (
  559. HKEY_LOCAL_MACHINE,
  560. THREAD_REG_KEY,
  561. 0,
  562. NULL,
  563. REG_OPTION_NON_VOLATILE,
  564. KEY_ALL_ACCESS,
  565. NULL,
  566. &m_LogKey,
  567. NULL
  568. ) ;
  569. if ( t_Status != ERROR_SUCCESS )
  570. {
  571. return;
  572. }
  573. }
  574. DWORD t_Count = 0;
  575. BOOL bWrite = TRUE;
  576. DWORD t_ValueType = REG_DWORD ;
  577. DWORD t_ValueLength = sizeof ( DWORD ) ;
  578. LONG t_Status = RegQueryValueEx (
  579. m_LogKey ,
  580. THREAD_MARKS_VAL ,
  581. 0,
  582. &t_ValueType ,
  583. ( LPBYTE ) &t_Count ,
  584. &t_ValueLength
  585. ) ;
  586. if (t_Status == ERROR_SUCCESS)
  587. {
  588. if (t_Count == 0)
  589. {
  590. t_Count = 1;
  591. }
  592. else if (t_Count > THREAD_MARKS_MAX)
  593. {
  594. t_Count = THREAD_MARKS_MAX;
  595. }
  596. else
  597. {
  598. t_Count = t_Count;
  599. bWrite = FALSE;
  600. }
  601. }
  602. else
  603. {
  604. t_Count = THREAD_MARKS_DEF;
  605. }
  606. if (bWrite)
  607. {
  608. t_ValueType = REG_DWORD ;
  609. RegSetValueEx (
  610. m_LogKey ,
  611. THREAD_MARKS_VAL ,
  612. 0,
  613. t_ValueType ,
  614. ( LPBYTE ) &t_Count ,
  615. t_ValueLength
  616. ) ;
  617. }
  618. m_parent->SetMaxMarks(t_Count);
  619. }
  620. void TimerRegistryTaskObject :: SetRegistryNotification ()
  621. {
  622. if ( m_LogKey )
  623. RegCloseKey ( m_LogKey ) ;
  624. LONG t_Status = RegCreateKeyEx (
  625. HKEY_LOCAL_MACHINE,
  626. THREAD_REG_KEY,
  627. 0,
  628. NULL,
  629. REG_OPTION_NON_VOLATILE,
  630. KEY_ALL_ACCESS,
  631. NULL,
  632. &m_LogKey,
  633. NULL
  634. ) ;
  635. if ( t_Status == ERROR_SUCCESS )
  636. {
  637. t_Status = RegNotifyChangeKeyValue (
  638. m_LogKey ,
  639. TRUE ,
  640. REG_NOTIFY_CHANGE_LAST_SET ,
  641. GetHandle () ,
  642. TRUE
  643. ) ;
  644. }
  645. }
  646. CEventProviderWorkerThread::CEventProviderWorkerThread()
  647. : SnmpThreadObject (THREAD_NAME, THREAD_INTERVAL),
  648. m_pNotifyInt(NULL),
  649. m_notify(NULL),
  650. m_RegTaskObject(NULL)
  651. {
  652. m_RegTaskObject = new TimerRegistryTaskObject(this) ;
  653. ScheduleTask ( *m_RegTaskObject ) ;
  654. HRESULT hr = CoCreateInstance (CLSID_SMIR_Database,
  655. NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
  656. IID_ISMIR_Database, (void**)&m_pNotifyInt);
  657. if(SUCCEEDED(hr))
  658. {
  659. m_notify = new CEventCacheNotify();
  660. m_notify->AddRef();
  661. DWORD dw = 0;
  662. hr = m_pNotifyInt->AddNotify(m_notify, &dw);
  663. if(SUCCEEDED(hr))
  664. {
  665. m_notify->SetCookie(dw);
  666. }
  667. else
  668. {
  669. m_notify->Release();
  670. m_notify = NULL;
  671. }
  672. }
  673. else
  674. {
  675. m_pNotifyInt = NULL;
  676. }
  677. }
  678. CEventProviderWorkerThread::~CEventProviderWorkerThread()
  679. {
  680. if (m_RegTaskObject)
  681. {
  682. ReapTask ( *m_RegTaskObject ) ;
  683. delete m_RegTaskObject ;
  684. }
  685. }
  686. void CEventProviderWorkerThread::GetDeleteNotifyParams(ISmirDatabase** a_ppNotifyInt, CEventCacheNotify** a_ppNotify)
  687. {
  688. if (m_notify != NULL)
  689. {
  690. m_notify->Detach();
  691. *a_ppNotify = m_notify;
  692. m_notify = NULL;
  693. }
  694. if (m_pNotifyInt != NULL)
  695. {
  696. *a_ppNotifyInt = m_pNotifyInt;
  697. m_pNotifyInt = NULL;
  698. }
  699. }
  700. void CEventProviderWorkerThread::CreateServerWrap ()
  701. {
  702. m_pSmirNamespace = new CWbemServerWrap ( NULL ) ;
  703. }
  704. void CEventProviderWorkerThread::Initialise()
  705. {
  706. InitializeCom();
  707. DebugMacro9(
  708. SnmpDebugLog :: s_SnmpDebugLog->Write (
  709. L"\r\n");
  710. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  711. __FILE__,__LINE__,
  712. L"CEventProviderWorkerThread::Initialise ()\r\n");
  713. )
  714. }
  715. void CEventProviderWorkerThread::Uninitialise()
  716. {
  717. DebugMacro9(
  718. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  719. __FILE__,__LINE__,
  720. L"CEventProviderWorkerThread::Uninitialise ()\r\n");
  721. )
  722. if ( m_pSmirNamespace )
  723. {
  724. m_pSmirNamespace->Release () ;
  725. }
  726. delete this;
  727. CoUninitialize();
  728. }
  729. void CEventProviderWorkerThread::SetMaxMarks(DWORD dwM)
  730. {
  731. EnterCriticalSection(&g_CacheCriticalSection);
  732. m_MaxMarks = dwM;
  733. LeaveCriticalSection(&g_CacheCriticalSection);
  734. }
  735. void CEventProviderWorkerThread::TimedOut()
  736. {
  737. EnterCriticalSection(&g_CacheCriticalSection);
  738. //loop cache elements delete old entries...
  739. POSITION pos = m_Classes.GetStartPosition();
  740. while (NULL != pos)
  741. {
  742. CMap<CString, LPCWSTR, SCacheEntry*, SCacheEntry*>* pClassMap;
  743. DWORD key;
  744. m_Classes.GetNextAssoc(pos, key, pClassMap);
  745. CList<CString, CString> delList;
  746. POSITION subpos = pClassMap->GetStartPosition();
  747. while (NULL != subpos)
  748. {
  749. SCacheEntry* pEntry;
  750. CString subkey;
  751. pClassMap->GetNextAssoc(subpos, subkey, pEntry);
  752. pEntry->m_Marker++;
  753. if (pEntry->m_Marker > m_MaxMarks)
  754. {
  755. delList.AddTail(subkey);
  756. }
  757. }
  758. if (!delList.IsEmpty())
  759. {
  760. if (delList.GetCount() == pClassMap->GetCount())
  761. {
  762. pClassMap->RemoveAll();
  763. }
  764. else
  765. {
  766. while (!delList.IsEmpty())
  767. {
  768. pClassMap->RemoveKey(delList.RemoveTail());
  769. }
  770. }
  771. }
  772. }
  773. LeaveCriticalSection(&g_CacheCriticalSection);
  774. }
  775. void CEventProviderWorkerThread::Clear()
  776. {
  777. EnterCriticalSection(&g_CacheCriticalSection);
  778. //loop cache elements delete all entries...
  779. POSITION pos = m_Classes.GetStartPosition();
  780. while (NULL != pos)
  781. {
  782. CMap<CString, LPCWSTR, SCacheEntry*, SCacheEntry*>* pClassMap;
  783. DWORD key;
  784. m_Classes.GetNextAssoc(pos, key, pClassMap);
  785. pClassMap->RemoveAll();
  786. }
  787. LeaveCriticalSection(&g_CacheCriticalSection);
  788. }
  789. void CEventProviderWorkerThread::AddClassesToCache(DWORD_PTR key, CMap<CString, LPCWSTR, SCacheEntry*, SCacheEntry*>* item)
  790. {
  791. EnterCriticalSection ( & g_CacheCriticalSection ) ;
  792. m_Classes.SetAt(key, item);
  793. LeaveCriticalSection ( & g_CacheCriticalSection ) ;
  794. }
  795. void CEventProviderWorkerThread::RemoveClassesFromCache(DWORD_PTR key)
  796. {
  797. EnterCriticalSection ( & g_CacheCriticalSection ) ;
  798. g_pWorkerThread->m_Classes.RemoveKey(key);
  799. LeaveCriticalSection ( & g_CacheCriticalSection ) ;
  800. }