Source code of Windows XP (NT5)
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.

437 lines
16 KiB

  1. //=============================================================================
  2. //
  3. // Copyright (c) 1996-1999, Microsoft Corporation, All rights reserved
  4. //
  5. // NSREP.H
  6. //
  7. // Represents the ESS functionality for a given namespace
  8. //
  9. // Classes defined:
  10. //
  11. // CEssNamespace
  12. //
  13. // History:
  14. //
  15. // 11/27/96 a-levn Compiles.
  16. // 1/6/97 a-levn Updated to initialize TSS.
  17. //
  18. //=============================================================================
  19. #ifndef __NSREP_ESS__H_
  20. #define __NSREP_ESS__H_
  21. #include "pragmas.h"
  22. #include "binding.h"
  23. #include "permfilt.h"
  24. #include "permcons.h"
  25. #include "tempfilt.h"
  26. #include "tempcons.h"
  27. #include "corefind.h"
  28. #include "consprov.h"
  29. #include "provreg.h"
  30. #include "wbemtss.h"
  31. #include "poller.h"
  32. #include "essutils.h"
  33. #include "clscache.h"
  34. #include "moniprov.h"
  35. #include <map>
  36. #include <set>
  37. class CEss;
  38. class CEssNamespace : public CUpdateLockable
  39. {
  40. protected:
  41. CEss* m_pEss;
  42. long m_lRef;
  43. //
  44. // protects level 1 members. These are members that can be used even
  45. // when level2 members are locked.
  46. //
  47. CCritSec m_csLevel1;
  48. //
  49. // protects level 2 members. When both a level1 and level2 lock need to
  50. // be aquired, the level2 lock MUST be obtained first. This is a wbem cs
  51. // because we hold this lock across calls to core ( which can conceivably
  52. // take longer than 2 minutes - the deadline for normal critical sections )
  53. //
  54. CWbemCriticalSection m_csLevel2;
  55. //
  56. // level 1 members
  57. //
  58. HANDLE m_hInitComplete;
  59. //
  60. // If events are signaled when we are in the unitialized state, then
  61. // they are temporarily stored here.
  62. //
  63. CPointerArray<CEventRepresentation> m_aDeferredEvents;
  64. LPWSTR m_wszName;
  65. _IWmiProviderFactory* m_pProviderFactory;
  66. IWbemServices* m_pCoreSvc;
  67. IWbemServices* m_pFullSvc;
  68. IWbemInternalServices* m_pInternalCoreSvc;
  69. IWbemInternalServices* m_pInternalFullSvc;
  70. //
  71. // Level 2 members.
  72. //
  73. BOOL m_bInResync;
  74. BOOL m_bStage1Complete;
  75. int m_cActive;
  76. CBindingTable m_Bindings;
  77. CConsumerProviderCache m_ConsumerProviderCache;
  78. CEventProviderCache m_EventProviderCache;
  79. CPoller m_Poller;
  80. CEssClassCache m_ClassCache;
  81. CMonitorProvider* m_pMonitorProvider;
  82. CCoreEventProvider* m_pCoreEventProvider;
  83. CNtSid m_sidAdministrators;
  84. //
  85. // this structure maps tells us if we need to do anything for a provider
  86. // when a class changes.
  87. //
  88. typedef std::set<WString,WSiless,wbem_allocator<WString> > ProviderSet;
  89. typedef std::map<WString,ProviderSet,WSiless,wbem_allocator<ProviderSet> >
  90. ClassToProviderMap;
  91. ClassToProviderMap m_mapProviderInterestClasses;
  92. //
  93. // the state and init members are both level 1 and level2. They can be
  94. // read when the level1 lock is held. They can only be modified when the
  95. // level2 and level1 locks are held.
  96. //
  97. HRESULT m_hresInit;
  98. enum {
  99. //
  100. // Initialization is Pending. Can service
  101. // events from core in this state (though will be defferred ).
  102. // it is expected that Initialize() will be called
  103. // sometime in the near future. We also can support limited ops
  104. // while in this state. Any operations that deal with event
  105. // subsciptions or provider objects can be serviced. Any ops that
  106. // deal with event provider registrations must wait for initialization.
  107. //
  108. e_InitializePending,
  109. //
  110. // Quiet - Initialization is not pending. The namespace is known to
  111. // be empty of any ess related onjects. Can service events in this
  112. // state, but they are simply discarded.
  113. //
  114. e_Quiet,
  115. //
  116. // We have loaded subscription objects. All ess operations can be
  117. // performed. Events from core now can be processed.
  118. //
  119. e_Initialized,
  120. //
  121. // Shutdown has been called. All operations return error.
  122. //
  123. e_Shutdown
  124. } m_eState;
  125. protected:
  126. class CConsumerClassDeletionSink : public CEmbeddedObjectSink<CEssNamespace>
  127. {
  128. public:
  129. CConsumerClassDeletionSink(CEssNamespace* pNamespace) :
  130. CEmbeddedObjectSink<CEssNamespace>(pNamespace){}
  131. STDMETHOD(Indicate)(long lNumObjects, IWbemClassObject** apObjects);
  132. } m_ClassDeletionSink;
  133. friend CConsumerClassDeletionSink;
  134. protected:
  135. inline void LogOp( LPCWSTR wszOp, IWbemClassObject* pObj );
  136. HRESULT EnsureInitPending();
  137. HRESULT CheckMonitor(IWbemClassObject* pPrevMonitorObj,
  138. IWbemClassObject* pMonitorObj);
  139. HRESULT CheckEventFilter(IWbemClassObject* pPrevFilterObj,
  140. IWbemClassObject* pFilterObj);
  141. HRESULT CheckEventConsumer(IWbemClassObject* pPrevConsumerObj,
  142. IWbemClassObject* pConsumerObj);
  143. HRESULT CheckBinding(IWbemClassObject* pPrevBindingObj,
  144. IWbemClassObject* pBindingObj);
  145. HRESULT CheckEventProviderRegistration(IWbemClassObject* pReg);
  146. HRESULT CheckTimerInstruction(IWbemClassObject* pInst);
  147. HRESULT ActOnSystemEvent(CEventRepresentation& Event, long lFlags);
  148. HRESULT HandleClassChange(LPCWSTR wszClassName, IWbemClassObject* pClass);
  149. HRESULT HandleClassCreation(LPCWSTR wszClassName,IWbemClassObject* pClass);
  150. HRESULT HandleConsumerClassDeletion(LPCWSTR wszClassName);
  151. HRESULT PrepareForResync();
  152. HRESULT ReactivateAllFilters();
  153. HRESULT CommitResync();
  154. HRESULT ReloadMonitor(ADDREF IWbemClassObject* pEventMonitorObj);
  155. HRESULT ReloadEventFilter(ADDREF IWbemClassObject* pEventFilterObj);
  156. HRESULT ReloadEventConsumer(READ_ONLY IWbemClassObject* pConsumerObj,
  157. long lFlags);
  158. HRESULT ReloadBinding(READ_ONLY IWbemClassObject* pBindingObj);
  159. HRESULT ReloadTimerInstruction(READ_ONLY IWbemClassObject* pInstObj);
  160. HRESULT ReloadProvider(READ_ONLY IWbemClassObject* pInstObj);
  161. HRESULT ReloadEventProviderRegistration(IWbemClassObject* pInstObj);
  162. HRESULT ReloadConsumerProviderRegistration(IWbemClassObject* pInstObj);
  163. HRESULT AddMonitor(ADDREF IWbemClassObject* pEventMonitorObj);
  164. HRESULT AddEventFilter(ADDREF IWbemClassObject* pEventFilterObj,
  165. BOOL bInRestart = FALSE);
  166. HRESULT AddEventConsumer(READ_ONLY IWbemClassObject* pConsumerObj,
  167. long lFlags,
  168. BOOL bInRestart = FALSE);
  169. HRESULT AddBinding(LPCWSTR wszFilterKey, LPCWSTR wszConsumerKey,
  170. READ_ONLY IWbemClassObject* pBindingObj);
  171. HRESULT AddBinding(IWbemClassObject* pBindingObj);
  172. HRESULT AddTimerInstruction(READ_ONLY IWbemClassObject* pInstObj);
  173. HRESULT AddProvider(READ_ONLY IWbemClassObject* pInstObj);
  174. HRESULT AddEventProviderRegistration(READ_ONLY IWbemClassObject* pInstObj);
  175. HRESULT RemoveMonitor(IWbemClassObject* pEventMonitorObj);
  176. HRESULT RemoveEventFilter(IWbemClassObject* pEventFilterObj);
  177. HRESULT RemoveEventConsumer(IWbemClassObject* pConsumerObj);
  178. HRESULT RemoveBinding(LPCWSTR wszFilterKey, LPCWSTR wszConsumerKey);
  179. HRESULT RemoveTimerInstruction(IWbemClassObject* pInstObj);
  180. HRESULT RemoveProvider(IWbemClassObject* pInstObj);
  181. HRESULT RemoveEventProviderRegistration(IWbemClassObject* pInstObj);
  182. HRESULT RemoveConsumerProviderRegistration(IWbemClassObject* pInstObj);
  183. HRESULT AssertBindings(IWbemClassObject* pEndpoint);
  184. HRESULT DeleteConsumerProvider(IWbemClassObject* pReg);
  185. HRESULT PerformSubscriptionInitialization();
  186. HRESULT PerformProviderInitialization();
  187. BOOL IsNeededOnStartup();
  188. HRESULT GetCurrentState(IWbemClassObject* pTemplate,
  189. IWbemClassObject** ppObj);
  190. HRESULT CheckSecurity(IWbemClassObject* pPrevObj,
  191. IWbemClassObject* pObj);
  192. HRESULT EnsureSessionSid(IWbemClassObject* pPrevObj, CNtSid& ActingSid);
  193. HRESULT CheckOverwriteSecurity( IWbemClassObject* pPrevObj,
  194. CNtSid& ActingSid);
  195. HRESULT PutSidInObject(IWbemClassObject* pObj, CNtSid& Sid);
  196. HRESULT IsCallerAdministrator();
  197. HRESULT AttemptToActivateFilter(READ_ONLY CEventFilter* pFilter);
  198. HRESULT GetFilterEventNamespace(CEventFilter* pFilter,
  199. RELEASE_ME CEssNamespace** ppNamespace);
  200. void FireNCFilterEvent(DWORD dwIndex, CEventFilter *pFilter);
  201. CQueueingEventSink* GetQueueingEventSink( LPCWSTR wszSinkName );
  202. ~CEssNamespace();
  203. public:
  204. CEssNamespace(CEss* pEss);
  205. ULONG AddRef();
  206. ULONG Release();
  207. CEss* GetEss() { return m_pEss; }
  208. //
  209. // On return, namespace can be used for limited operations. Events
  210. // can be signaled ( though they may be defferred internally ) and
  211. // operations dealing with subscriptions can be performed.
  212. //
  213. HRESULT PreInitialize( LPCWSTR wszName );
  214. //
  215. // Performs initialization but does NOT transition state to Initialized.
  216. // This is done by calling MarkAsInitialized(). This allows a caller to
  217. // atomically perform initalization of multiple namespaces.
  218. //
  219. HRESULT Initialize();
  220. //
  221. // Finishes loading event provider registrations and processes
  222. // subcriptions. Transitions to FullyInitialized() state.
  223. //
  224. HRESULT CompleteInitialization();
  225. //
  226. // Transitions state to Initialized.
  227. //
  228. void MarkAsInitialized( HRESULT hres );
  229. //
  230. // Transitions state to Initialize Pending if previously in the Quiet
  231. // state. Returns TRUE if transition was made.
  232. //
  233. BOOL MarkAsInitPendingIfQuiet();
  234. //
  235. // Waits for Initialization to complete.
  236. //
  237. HRESULT WaitForInitialization();
  238. HRESULT Park();
  239. HRESULT Shutdown();
  240. LPCWSTR GetName() {return m_wszName;}
  241. HRESULT GetNamespacePointer(RELEASE_ME IWbemServices** ppNamespace);
  242. HRESULT LoadEventProvider(LPCWSTR wszProviderName,
  243. IWbemEventProvider** ppProv);
  244. HRESULT LoadConsumerProvider(LPCWSTR wszProviderName,
  245. IUnknown** ppProv);
  246. HRESULT DecorateObject(IWbemClassObject* pObject);
  247. HRESULT ProcessEvent(CEventRepresentation& Event, long lFlags);
  248. HRESULT ProcessQueryObjectSinkEvent( READ_ONLY CEventRepresentation& Event );
  249. HRESULT SignalEvent(CEventRepresentation& Event, long lFlags);
  250. HRESULT ValidateSystemEvent(CEventRepresentation& Event);
  251. void SetActive();
  252. void SetInactive();
  253. HRESULT ActivateFilter(READ_ONLY CEventFilter* pFilter);
  254. HRESULT DeactivateFilter(READ_ONLY CEventFilter* pFilter);
  255. //
  256. // public versions of register/remove notification sink. Do NOT use
  257. // these versions if calling from within ESS. The reason is that these
  258. // versions wait for initialization and lock the namespace which might
  259. // cause deadlocks if called from within ESS. We also don't want to
  260. // generate self instrumentation events for internal calls.
  261. //
  262. HRESULT RegisterNotificationSink(
  263. WBEM_CWSTR wszQueryLanguage, WBEM_CWSTR wszQuery,
  264. long lFlags, WMIMSG_QOS_FLAG lQosFlags,
  265. IWbemContext* pContext,
  266. IWbemObjectSink* pSink );
  267. HRESULT RemoveNotificationSink( IWbemObjectSink* pSink );
  268. HRESULT ReloadProvider( long lFlags, LPCWSTR wszProvider );
  269. //
  270. // Internal versions of register/remove notification sink. They do
  271. // not lock, wait for initialization, or fire self instrumentation events.
  272. // If calling these methods from within ess, specify bInternal as TRUE.
  273. // The pOwnerSid is used when access checks for the subscription should
  274. // be performed based on a particular SID. currently this is only used
  275. // for cross-namespace subscriptions.
  276. //
  277. HRESULT InternalRegisterNotificationSink(
  278. WBEM_CWSTR wszQueryLanguage, WBEM_CWSTR wszQuery,
  279. long lFlags, WMIMSG_QOS_FLAG lQosFlags,
  280. IWbemContext* pContext, IWbemObjectSink* pSink,
  281. bool bInternal, PSID pOwnerSid );
  282. HRESULT InternalRemoveNotificationSink(IWbemObjectSink* pSink);
  283. CWinMgmtTimerGenerator& GetTimerGenerator();
  284. CConsumerProviderCache& GetConsumerProviderCache()
  285. {return m_ConsumerProviderCache;}
  286. DWORD GetProvidedEventMask(IWbemClassObject* pClass);
  287. HRESULT EnsureConsumerWatchInstruction();
  288. HRESULT InitializeTimerGenerator();
  289. HRESULT ScheduleDelivery(CQueueingEventSink* pDest);
  290. HRESULT RaiseErrorEvent(IWbemEvent* pEvent);
  291. void IncrementObjectCount();
  292. void DecrementObjectCount();
  293. HRESULT AddSleepCharge(DWORD dwSleep);
  294. HRESULT AddCache();
  295. HRESULT RemoveCache();
  296. HRESULT AddToCache(DWORD dwAdd, DWORD dwMemberTotal,
  297. DWORD* pdwSleep = NULL);
  298. HRESULT RemoveFromCache(DWORD dwRemove);
  299. HRESULT LockForUpdate();
  300. HRESULT UnlockForUpdate();
  301. bool IsShutdown() { return m_eState == e_Shutdown; }
  302. HRESULT GetProviderNamespacePointer(IWbemServices** ppServices);
  303. HRESULT GetClass( LPCWSTR wszClassName, _IWmiObject** ppClass)
  304. { return m_ClassCache.GetClass(wszClassName, GetCurrentEssContext(),
  305. ppClass);}
  306. HRESULT GetClassFromCore(LPCWSTR wszClassName, _IWmiObject** ppClass);
  307. HRESULT GetInstance(LPCWSTR wszPath, _IWmiObject** ppInstance);
  308. HRESULT GetDbInstance(LPCWSTR wszDbKey, _IWmiObject** ppInstance);
  309. HRESULT CreateInstanceEnum(LPCWSTR wszClass, long lFlags,
  310. IWbemObjectSink* pSink);
  311. HRESULT ExecQuery(LPCWSTR wszQuery, long lFlags, IWbemObjectSink* pSink);
  312. CNtSid& GetAdministratorsSid() {return m_sidAdministrators;}
  313. HRESULT GetToken(PSID pSid, IWbemToken** ppToken);
  314. HRESULT RegisterFilterForAllClassChanges(CEventFilter* pFilter,
  315. QL_LEVEL_1_RPN_EXPRESSION* pExpr);
  316. HRESULT RegisterSinkForAllClassChanges(IWbemObjectSink* pSink,
  317. QL_LEVEL_1_RPN_EXPRESSION* pExpr);
  318. HRESULT RegisterSinkForClassChanges(IWbemObjectSink* pSink,
  319. LPCWSTR wszClassName);
  320. HRESULT UnregisterFilterFromAllClassChanges(CEventFilter* pFilter);
  321. HRESULT UnregisterSinkFromAllClassChanges(IWbemObjectSink* pSink);
  322. HRESULT RegisterProviderForClassChanges( LPCWSTR wszClassName,
  323. LPCWSTR wszProvName );
  324. HRESULT FirePostponedOperations();
  325. HRESULT ScheduleFirePostponed();
  326. HRESULT PostponeRelease(IUnknown* pUnk);
  327. static PSID GetSidFromObject(IWbemClassObject* pObj);
  328. BOOL DoesThreadOwnNamespaceLock();
  329. void DumpStatistics(FILE* f, long lFlags);
  330. friend class CEss;
  331. friend class CEssMetaData;
  332. friend class CFilterEnumSink;
  333. friend class CConsumerEnumSink;
  334. friend class CBindingEnumSink;
  335. friend class CMonitorEnumSink;
  336. friend class CInResync;
  337. friend class CAssertBindingsSink;
  338. friend class CFirePostponed;
  339. };
  340. class CInResync
  341. {
  342. protected:
  343. CEssNamespace* m_pNamespace;
  344. public:
  345. CInResync(CEssNamespace* pNamespace) : m_pNamespace(pNamespace)
  346. {
  347. m_pNamespace->PrepareForResync();
  348. }
  349. ~CInResync()
  350. {
  351. m_pNamespace->ReactivateAllFilters();
  352. m_pNamespace->CommitResync();
  353. }
  354. };
  355. class CEssMetaData : public CMetaData
  356. {
  357. protected:
  358. CEssNamespace* m_pNamespace;
  359. public:
  360. CEssMetaData(CEssNamespace* pNamespace) : m_pNamespace(pNamespace){}
  361. virtual HRESULT GetClass( LPCWSTR wszName,
  362. IWbemContext* pContext,
  363. _IWmiObject** ppClass );
  364. };
  365. #endif