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.

348 lines
8.3 KiB

  1. //***************************************************************************
  2. //
  3. // NTEVTCFAC.CPP
  4. //
  5. // Module: WBEM NT EVENT PROVIDER
  6. //
  7. // Purpose: Contains the class factory. This creates objects when
  8. // connections are requested.
  9. //
  10. // Copyright (c) 1996-2001 Microsoft Corporation, All Rights Reserved
  11. //
  12. //***************************************************************************
  13. #include "precomp.h"
  14. LONG CNTEventProviderClassFactory :: objectsInProgress = 0 ;
  15. LONG CNTEventProviderClassFactory :: locksInProgress = 0 ;
  16. BOOL CEventLogFile :: ms_bSetPrivilege = FALSE ;
  17. extern CEventProviderManager* g_pMgr;
  18. extern CCriticalSection g_ProvLock;
  19. //***************************************************************************
  20. //
  21. // CNTEventProviderClassFactory::CNTEventProviderClassFactory
  22. // CNTEventProviderClassFactory::~CNTEventProviderClassFactory
  23. //
  24. // Constructor Parameters:
  25. // None
  26. //***************************************************************************
  27. CNTEventProviderClassFactory::CNTEventProviderClassFactory ()
  28. {
  29. m_referenceCount = 0 ;
  30. }
  31. CNTEventProviderClassFactory::~CNTEventProviderClassFactory ()
  32. {
  33. }
  34. //***************************************************************************
  35. //
  36. // CNTEventProviderClassFactory::QueryInterface
  37. // CNTEventProviderClassFactory::AddRef
  38. // CNTEventProviderClassFactory::Release
  39. //
  40. // Purpose: Standard Ole routines needed for all interfaces
  41. //
  42. //***************************************************************************
  43. STDMETHODIMP CNTEventProviderClassFactory::QueryInterface (
  44. REFIID iid ,
  45. LPVOID FAR *iplpv
  46. )
  47. {
  48. *iplpv = NULL ;
  49. if ( iid == IID_IUnknown )
  50. {
  51. *iplpv = ( LPVOID ) this ;
  52. }
  53. else if ( iid == IID_IClassFactory )
  54. {
  55. *iplpv = ( LPVOID ) this ;
  56. }
  57. if ( *iplpv )
  58. {
  59. ( ( LPUNKNOWN ) *iplpv )->AddRef () ;
  60. return ResultFromScode ( S_OK ) ;
  61. }
  62. else
  63. {
  64. return ResultFromScode ( E_NOINTERFACE ) ;
  65. }
  66. }
  67. STDMETHODIMP_( ULONG ) CNTEventProviderClassFactory :: AddRef ()
  68. {
  69. InterlockedIncrement(&objectsInProgress);
  70. return InterlockedIncrement ( &m_referenceCount ) ;
  71. }
  72. STDMETHODIMP_(ULONG) CNTEventProviderClassFactory :: Release ()
  73. {
  74. LONG ref ;
  75. if ( ( ref = InterlockedDecrement ( & m_referenceCount ) ) == 0 )
  76. {
  77. delete this ;
  78. }
  79. InterlockedDecrement(&objectsInProgress);
  80. return ref ;
  81. }
  82. //***************************************************************************
  83. //
  84. // CNTEventProviderClassFactory::LockServer
  85. //
  86. // Purpose:
  87. // Increments or decrements the lock count of the DLL. If the
  88. // lock count goes to zero and there are no objects, the DLL
  89. // is allowed to unload. See DllCanUnloadNow.
  90. //
  91. // Parameters:
  92. // fLock BOOL specifying whether to increment or
  93. // decrement the lock count.
  94. //
  95. // Return Value:
  96. // HRESULT NOERROR always.
  97. //***************************************************************************
  98. STDMETHODIMP CNTEventProviderClassFactory :: LockServer ( BOOL fLock )
  99. {
  100. if ( fLock )
  101. {
  102. InterlockedIncrement ( & locksInProgress ) ;
  103. }
  104. else
  105. {
  106. InterlockedDecrement ( & locksInProgress ) ;
  107. }
  108. return S_OK ;
  109. }
  110. //***************************************************************************
  111. //
  112. // CNTEventlogEventProviderClassFactory::CreateInstance
  113. //
  114. // Purpose: Instantiates a Provider object returning an interface pointer.
  115. //
  116. // Parameters:
  117. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  118. // being used in an aggregation.
  119. // riid REFIID identifying the interface the caller
  120. // desires to have for the new object.
  121. // ppvObj PPVOID in which to store the desired
  122. // interface pointer for the new object.
  123. //
  124. // Return Value:
  125. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  126. // if we cannot support the requested interface.
  127. //***************************************************************************
  128. STDMETHODIMP CNTEventlogEventProviderClassFactory :: CreateInstance(LPUNKNOWN pUnkOuter ,
  129. REFIID riid,
  130. LPVOID FAR * ppvObject
  131. )
  132. {
  133. HRESULT status = E_FAIL;
  134. if ( pUnkOuter )
  135. {
  136. status = CLASS_E_NOAGGREGATION;
  137. }
  138. else
  139. {
  140. BOOL bAllocateMgr = FALSE;
  141. BOOL bLocked = FALSE;
  142. try
  143. {
  144. if (g_ProvLock.Lock())
  145. {
  146. bLocked = TRUE;
  147. if (NULL == CNTEventProvider::g_secMutex)
  148. {
  149. //only need the security mutex if not NT5
  150. DWORD dwVersion = GetVersion();
  151. if ( 5 > (DWORD)(LOBYTE(LOWORD(dwVersion))) )
  152. {
  153. CNTEventProvider::g_secMutex = new CMutex(FALSE, SECURITY_MUTEX_NAME, NULL);
  154. }
  155. }
  156. if (!CEventLogFile::ms_bSetPrivilege)
  157. {
  158. if (!CEventLogFile::SetSecurityLogPrivilege(TRUE))
  159. {
  160. DebugOut(
  161. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  162. L"CNTEventlogEventProviderClassFactory :: CreateInstance, CEventLogFile::SetSecurityLogPrivilege failed \r\n");
  163. )
  164. }
  165. else
  166. {
  167. CEventLogFile::ms_bSetPrivilege = TRUE;
  168. }
  169. }
  170. if (NULL == g_pMgr)
  171. {
  172. bAllocateMgr = TRUE;
  173. g_pMgr = new CEventProviderManager;
  174. CNTEventProvider::AllocateGlobalSIDs();
  175. }
  176. CNTEventProvider* prov = new CNTEventProvider(g_pMgr);
  177. status = prov->QueryInterface (riid, ppvObject);
  178. if (NOERROR != status)
  179. {
  180. delete prov;
  181. }
  182. g_ProvLock.Unlock();
  183. bLocked = FALSE;
  184. }
  185. }
  186. catch ( ... )
  187. {
  188. if ( bLocked )
  189. {
  190. if ( bAllocateMgr )
  191. {
  192. if ( g_pMgr )
  193. {
  194. delete g_pMgr;
  195. g_pMgr = NULL;
  196. }
  197. }
  198. g_ProvLock.Unlock();
  199. bLocked = FALSE;
  200. }
  201. if ( SUCCEEDED ( status ) )
  202. {
  203. status = E_UNEXPECTED;
  204. }
  205. }
  206. }
  207. return status ;
  208. }
  209. //***************************************************************************
  210. //
  211. // CNTEventlogInstanceProviderClassFactory::CreateInstance
  212. //
  213. // Purpose: Instantiates a Provider object returning an interface pointer.
  214. //
  215. // Parameters:
  216. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  217. // being used in an aggregation.
  218. // riid REFIID identifying the interface the caller
  219. // desires to have for the new object.
  220. // ppvObj PPVOID in which to store the desired
  221. // interface pointer for the new object.
  222. //
  223. // Return Value:
  224. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  225. // if we cannot support the requested interface.
  226. //***************************************************************************
  227. STDMETHODIMP CNTEventlogInstanceProviderClassFactory :: CreateInstance(LPUNKNOWN pUnkOuter ,
  228. REFIID riid,
  229. LPVOID FAR * ppvObject
  230. )
  231. {
  232. HRESULT status = E_FAIL;
  233. if ( pUnkOuter )
  234. {
  235. status = CLASS_E_NOAGGREGATION;
  236. }
  237. else
  238. {
  239. BOOL bLocked = FALSE;
  240. try
  241. {
  242. if (g_ProvLock.Lock())
  243. {
  244. bLocked = TRUE;
  245. if (NULL == CNTEventProvider::g_secMutex)
  246. {
  247. //only need the security mutex if not NT5
  248. DWORD dwVersion = GetVersion();
  249. if ( 5 > (DWORD)(LOBYTE(LOWORD(dwVersion))) )
  250. {
  251. CNTEventProvider::g_secMutex = new CMutex(FALSE, SECURITY_MUTEX_NAME, NULL);
  252. }
  253. }
  254. IWbemServices *lpunk = ( IWbemServices * ) new CImpNTEvtProv ;
  255. if ( lpunk == NULL )
  256. {
  257. status = E_OUTOFMEMORY ;
  258. }
  259. else
  260. {
  261. status = lpunk->QueryInterface ( riid , ppvObject ) ;
  262. if ( FAILED ( status ) )
  263. {
  264. delete lpunk ;
  265. }
  266. else
  267. {
  268. }
  269. }
  270. g_ProvLock.Unlock();
  271. bLocked = FALSE;
  272. }
  273. }
  274. catch ( ... )
  275. {
  276. if ( bLocked )
  277. {
  278. g_ProvLock.Unlock();
  279. bLocked = FALSE;
  280. }
  281. if ( SUCCEEDED ( status ) )
  282. {
  283. status = E_UNEXPECTED;
  284. }
  285. }
  286. }
  287. return status ;
  288. }