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.

308 lines
6.7 KiB

  1. //******************************************************************************
  2. //
  3. // EVSINK.CPP
  4. //
  5. // Copyright (C) 1996-1999 Microsoft Corporation
  6. //
  7. //******************************************************************************
  8. #include "precomp.h"
  9. #include <stdio.h>
  10. #include <genutils.h>
  11. #include <cominit.h>
  12. #include "ess.h"
  13. #include "evsink.h"
  14. //****************************** CONTEXT **********************************//
  15. CEventContext::~CEventContext()
  16. {
  17. if( m_bOwning )
  18. {
  19. delete [] m_pSD;
  20. }
  21. }
  22. BOOL CEventContext::SetSD( long lSDLength, BYTE* pSD, BOOL bMakeCopy )
  23. {
  24. BOOL bRes = TRUE;
  25. if ( m_bOwning )
  26. {
  27. delete [] m_pSD;
  28. m_bOwning = false;
  29. }
  30. m_lSDLength = lSDLength;
  31. if ( m_lSDLength > 0 )
  32. {
  33. if ( !bMakeCopy )
  34. {
  35. m_pSD = pSD;
  36. m_bOwning = false;
  37. }
  38. else
  39. {
  40. m_pSD = new BYTE[m_lSDLength];
  41. if ( m_pSD != NULL )
  42. {
  43. memcpy( m_pSD, pSD, m_lSDLength );
  44. m_bOwning = true;
  45. }
  46. else
  47. {
  48. bRes = FALSE;
  49. }
  50. }
  51. }
  52. else
  53. {
  54. m_pSD = NULL;
  55. }
  56. return bRes;
  57. }
  58. CReuseMemoryManager CEventContext::mstatic_Manager(sizeof CEventContext);
  59. void *CEventContext::operator new(size_t nBlock)
  60. {
  61. return mstatic_Manager.Allocate();
  62. }
  63. void CEventContext::operator delete(void* p)
  64. {
  65. mstatic_Manager.Free(p);
  66. }
  67. /*
  68. void* CEventContext::operator new(size_t nSize)
  69. {
  70. return CTemporaryHeap::Alloc(nSize);
  71. }
  72. void CEventContext::operator delete(void* p)
  73. {
  74. CTemporaryHeap::Free(p, sizeof(CEventContext));
  75. }
  76. */
  77. //*************************** ABSTRTACT EVENT SINK *************************//
  78. STDMETHODIMP CAbstractEventSink::QueryInterface(REFIID riid, void** ppv)
  79. {
  80. if(riid == IID_IUnknown || riid == IID_IWbemObjectSink )
  81. {
  82. *ppv = (IWbemObjectSink*)this;
  83. AddRef();
  84. return S_OK;
  85. }
  86. else
  87. return E_NOINTERFACE;
  88. }
  89. STDMETHODIMP CAbstractEventSink::SetStatus(long, long, BSTR, IWbemClassObject*)
  90. {
  91. return E_NOTIMPL;
  92. }
  93. HRESULT CAbstractEventSink::Indicate(long lNumEvemts, IWbemEvent** apEvents,
  94. CEventContext* pContext)
  95. {
  96. return WBEM_E_CRITICAL_ERROR; // if not implemented, but called
  97. }
  98. STDMETHODIMP CAbstractEventSink::Indicate(long lNumEvents,
  99. IWbemClassObject** apEvents)
  100. {
  101. //
  102. // Event is being raised without security --- send it along with an empty
  103. // context
  104. //
  105. return Indicate(lNumEvents, apEvents, NULL);
  106. }
  107. STDMETHODIMP CAbstractEventSink::IndicateWithSD(long lNumEvents,
  108. IUnknown** apEvents,
  109. long lSDLength, BYTE* pSD)
  110. {
  111. HRESULT hres;
  112. //
  113. // Event is being raised with security -- send it along with that SD in the
  114. // context
  115. //
  116. CEventContext Context;
  117. Context.SetSD( lSDLength, pSD, FALSE );
  118. //
  119. // Allocate a stack buffer to cast the pointers
  120. //
  121. CTempArray<IWbemClassObject*> apCast;
  122. if(!INIT_TEMP_ARRAY(apCast, lNumEvents))
  123. {
  124. return WBEM_E_OUT_OF_MEMORY;
  125. }
  126. for(int i = 0; i < lNumEvents; i++)
  127. {
  128. hres = apEvents[i]->QueryInterface( IID_IWbemClassObject,
  129. (void**)&apCast[i] );
  130. if ( FAILED(hres) )
  131. {
  132. return hres;
  133. }
  134. }
  135. return Indicate(lNumEvents, apCast, &Context);
  136. }
  137. //*************************** OBJECT SINK *************************//
  138. STDMETHODIMP CObjectSink::QueryInterface(REFIID riid, void** ppv)
  139. {
  140. if(riid == IID_IUnknown || riid == IID_IWbemObjectSink)
  141. {
  142. *ppv = (IWbemObjectSink*)this;
  143. AddRef();
  144. return S_OK;
  145. }
  146. // Hack to idenitfy ourselves to the core as a trusted component
  147. else if(riid == CLSID_WbemLocator)
  148. return S_OK;
  149. else
  150. return E_NOINTERFACE;
  151. }
  152. STDMETHODIMP CObjectSink::SetStatus(long, long, BSTR, IWbemClassObject*)
  153. {
  154. return E_NOTIMPL;
  155. }
  156. ULONG STDMETHODCALLTYPE CObjectSink::AddRef()
  157. {
  158. return InterlockedIncrement(&m_lRef);
  159. }
  160. ULONG STDMETHODCALLTYPE CObjectSink::Release()
  161. {
  162. long lRef = InterlockedDecrement(&m_lRef);
  163. if(lRef == 0)
  164. delete this;
  165. return lRef;
  166. }
  167. //*************************** EVENT SINK *************************//
  168. ULONG STDMETHODCALLTYPE CEventSink::AddRef()
  169. {
  170. return InterlockedIncrement(&m_lRef);
  171. }
  172. ULONG STDMETHODCALLTYPE CEventSink::Release()
  173. {
  174. long lRef = InterlockedDecrement(&m_lRef);
  175. if(lRef == 0)
  176. delete this;
  177. return lRef;
  178. }
  179. //*************************** OWNED EVENT SINK *************************//
  180. COwnedEventSink::COwnedEventSink(CAbstractEventSink* pOwner)
  181. : m_pOwner(pOwner), m_lRef(0), m_bReleasing(false)
  182. {
  183. }
  184. ULONG STDMETHODCALLTYPE COwnedEventSink::AddRef()
  185. {
  186. CInCritSec ics(&m_cs);
  187. //
  188. // Increment our ref count, as well as that of our putative owner
  189. //
  190. m_lRef++;
  191. if(m_pOwner)
  192. m_pOwner->AddRef();
  193. return m_lRef;
  194. }
  195. ULONG STDMETHODCALLTYPE COwnedEventSink::Release()
  196. {
  197. bool bDelete = false;
  198. {
  199. CInCritSec ics(&m_cs);
  200. m_bReleasing = true;
  201. m_lRef--;
  202. //
  203. // Propagate release to our owner. This may cause Disconnect to be
  204. // called, but it will know not to self-destruct because of
  205. // m_bReleasing
  206. //
  207. if(m_pOwner)
  208. m_pOwner->Release();
  209. //
  210. // Determine whether self-destruct is called for
  211. //
  212. if(m_lRef == 0 && m_pOwner == NULL)
  213. {
  214. bDelete = true;
  215. }
  216. m_bReleasing = false;
  217. }
  218. if(bDelete)
  219. delete this;
  220. return 1;
  221. }
  222. void COwnedEventSink::Disconnect()
  223. {
  224. bool bDelete = false;
  225. {
  226. CInCritSec ics(&m_cs);
  227. if(m_pOwner == NULL)
  228. return;
  229. //
  230. // Release all the ref-counts that the owner has received through us
  231. //
  232. for(int i = 0; i < m_lRef; i++)
  233. m_pOwner->Release();
  234. //
  235. // Forget about the owner. Once we have been released by externals,
  236. // we go away
  237. //
  238. m_pOwner = NULL;
  239. //
  240. // Check if we are already fully released by externals
  241. //
  242. if(m_lRef == 0 && !m_bReleasing)
  243. bDelete = true;
  244. }
  245. if(bDelete)
  246. delete this;
  247. }