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.

681 lines
18 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1996 Microsoft Corporation. All Rights Reserved.
  5. Component: Appln Manager
  6. File: Applmgr.h
  7. Owner: PramodD
  8. This is the application manager header file.
  9. ===================================================================*/
  10. #ifndef APPLMGR_H
  11. #define APPLMGR_H
  12. #include "debug.h"
  13. #include "hashing.h"
  14. #include "cachemgr.h"
  15. #include "appcnfg.h"
  16. #include "compcol.h"
  17. #include "fileapp.h"
  18. #include "idhash.h"
  19. #include "memcls.h"
  20. #include "ftm.h"
  21. #include "disptch2.h"
  22. /*===================================================================
  23. #defines
  24. ===================================================================*/
  25. #define NUM_APPLMGR_HASHING_BUCKETS 17
  26. #define NOTIFICATION_BUFFER_SIZE 4096
  27. #define INVALID_THREADID 0xFFFFFFFF
  28. #include "asptlb.h"
  29. // Use to specify which source file name you want (pathInfo or pathTranslated)
  30. #ifndef _SRCPATHTYPE_DEFINED
  31. #define _SRCPATHTYPE_DEFINED
  32. enum SOURCEPATHTYPE
  33. {
  34. SOURCEPATHTYPE_VIRTUAL = 0,
  35. SOURCEPATHTYPE_PHYSICAL = 1
  36. };
  37. #endif
  38. /*===================================================================
  39. Forward declarations
  40. ===================================================================*/
  41. class CComponentCollection;
  42. class CSessionMgr;
  43. class CViperActivity;
  44. class CActiveScriptEngine;
  45. struct IDebugApplication;
  46. struct IDebugApplicationNode;
  47. /*===================================================================
  48. C A p p l n V a r i a n t s
  49. ===================================================================*/
  50. class CApplnVariants : public IVariantDictionaryImpl
  51. {
  52. private:
  53. ULONG m_cRefs; // ref count
  54. CAppln * m_pAppln; // pointer to parent object
  55. CompType m_ctColType; // type of components in collection
  56. CSupportErrorInfo m_ISupportErrImp; // implementation of ISupportErr
  57. HRESULT ObjectNameFromVariant(VARIANT &vKey, WCHAR **ppwszName,
  58. BOOL fVerify = FALSE);
  59. public:
  60. CApplnVariants();
  61. ~CApplnVariants();
  62. HRESULT Init(CAppln *pAppln, CompType ctColType);
  63. HRESULT UnInit();
  64. // The Big Three
  65. STDMETHODIMP QueryInterface(const GUID &, void **);
  66. STDMETHODIMP_(ULONG) AddRef();
  67. STDMETHODIMP_(ULONG) Release();
  68. // OLE Automation Interface
  69. STDMETHODIMP get_Item(VARIANT Var, VARIANT *pvar);
  70. STDMETHODIMP put_Item(VARIANT varKey, VARIANT var);
  71. STDMETHODIMP putref_Item(VARIANT varKey, VARIANT var);
  72. STDMETHODIMP get_Key(VARIANT Var, VARIANT *pvar);
  73. STDMETHODIMP get__NewEnum(IUnknown **ppEnumReturn);
  74. STDMETHODIMP get_Count(int *pcValues);
  75. STDMETHODIMP Remove(VARIANT VarKey);
  76. STDMETHODIMP RemoveAll();
  77. // Cache on per-class basis
  78. ACACHE_INCLASS_DEFINITIONS()
  79. };
  80. /*===================================================================
  81. C A p p l n
  82. ===================================================================*/
  83. class CAppln : public IApplicationObjectImpl, public CLinkElem, public CFTMImplementation
  84. {
  85. friend class CApplnMgr;
  86. friend class CApplnCleanupMgr;
  87. friend class CDirMonitorEntry;
  88. friend class CApplnVariants;
  89. private:
  90. //========= Misc flags
  91. DWORD m_fInited : 1; // Are we initialized?
  92. DWORD m_fFirstRequestRan : 1; // 1st request for this app ran?
  93. DWORD m_fGlobalChanged : 1; // Global.asa has changed?
  94. DWORD m_fDeleteInProgress : 1; // Delete event posted?
  95. DWORD m_fTombstone : 1; // ASP is done with the app?
  96. DWORD m_fDebuggable : 1; // Debugging enabled for this app?
  97. //========= Notification flags
  98. // ReadDirectoryChangesW done?
  99. DWORD m_fNotificationAdded : 1;
  100. // change notification should use impersonation?
  101. DWORD m_fUseImpersonationHandle : 1;
  102. //========= Ref counts
  103. DWORD m_cRefs;
  104. DWORD m_cRequests; // Active requests count
  105. DWORD m_cSessions; // Session count
  106. //========= Application's key, path, global.asa
  107. // metabase key (unique app id)
  108. TCHAR *m_pszMetabaseKey;
  109. // physical application directory path
  110. TCHAR *m_pszApplnPath;
  111. // virtual application directory path
  112. TCHAR *m_pszApplnVRoot;
  113. // Path to global.asa for application
  114. TCHAR *m_pszGlobalAsa;
  115. // Pointer to compliled template for global.asa
  116. CTemplate *m_pGlobalTemplate;
  117. //========= Application's Session Manager
  118. CSessionMgr *m_pSessionMgr; // Session manager for this app
  119. //========= Application's Configuration Settings
  120. CAppConfig *m_pAppConfig; // Application Configuration object
  121. //========= Application's Component Collection
  122. CComponentCollection *m_pApplCompCol; // Application scope objects
  123. //========= Application's dictionaries for presenting component collection
  124. CApplnVariants *m_pProperties;
  125. CApplnVariants *m_pTaggedObjects;
  126. //========= Viper Activity
  127. // Application's activity (for thread-locked appls)
  128. CViperActivity *m_pActivity;
  129. // ======== COM+ Services Config Object
  130. IUnknown *m_pServicesConfig;
  131. //========= Critical section for internal lock
  132. CRITICAL_SECTION m_csInternalLock;
  133. //========= External lock support
  134. CRITICAL_SECTION m_csApplnLock;
  135. DWORD m_dwLockThreadID; // thread which locked
  136. DWORD m_cLockRefCount; // lock count
  137. //========= Notification support
  138. // Identifiers stored by notification system
  139. CPtrArray m_rgpvDME; // list of directory monitor entries
  140. CPtrArray m_rgpvFileAppln; // list of entries relating files to applications
  141. // User impersonation handle for UNC change notification
  142. HANDLE m_hUserImpersonation;
  143. //========= Type Library wrapper from GLOBAL.ASA
  144. IDispatch *m_pdispGlobTypeLibWrapper;
  145. //========= SupportErrorInfo
  146. // Interface to indicate that we support ErrorInfo reporting
  147. CSupportErrorInfo m_ISuppErrImp;
  148. //========= Debugging Support
  149. // root node for browsing of running documents
  150. IDebugApplicationNode *m_pAppRoot;
  151. HRESULT InitServicesConfig();
  152. // proc used to asynchronously cleanup the app
  153. static DWORD __stdcall ApplnCleanupProc(VOID *pArg);
  154. public:
  155. CAppln();
  156. ~CAppln();
  157. HRESULT Init
  158. (
  159. TCHAR *pszApplnKey,
  160. TCHAR *pszApplnPath,
  161. CIsapiReqInfo *pIReq,
  162. HANDLE hUserImpersonation
  163. );
  164. // cnvert to tombstone state
  165. HRESULT UnInit();
  166. // create application's activity as clone of param
  167. HRESULT BindToActivity(CViperActivity *pActivity = NULL);
  168. // set (and remember) global.asa for this app
  169. HRESULT SetGlobalAsa(const TCHAR *pszGlobalAsa);
  170. // make sure script didn't leave locks
  171. HRESULT UnLockAfterRequest();
  172. // Non-delegating object IUnknown
  173. STDMETHODIMP QueryInterface(REFIID, void **);
  174. STDMETHODIMP_(ULONG) AddRef();
  175. STDMETHODIMP_(ULONG) Release();
  176. // Tombstone stub
  177. HRESULT CheckForTombstone();
  178. // Restart an application (such as when global.asa changes)
  179. HRESULT Restart(BOOL fForceRestart = FALSE);
  180. // IApplicationObject functions
  181. STDMETHODIMP Lock();
  182. STDMETHODIMP UnLock();
  183. STDMETHODIMP get_Value(BSTR bstr, VARIANT *pvar);
  184. STDMETHODIMP put_Value(BSTR bstr, VARIANT var);
  185. STDMETHODIMP putref_Value(BSTR bstr, VARIANT var);
  186. STDMETHODIMP get_Contents(IVariantDictionary **ppDictReturn);
  187. STDMETHODIMP get_StaticObjects(IVariantDictionary **ppDictReturn);
  188. // Application config related methods
  189. CAppConfig *QueryAppConfig();
  190. BOOL FConfigNeedsUpdate();
  191. HRESULT UpdateConfig(CIsapiReqInfo *pIReq, BOOL *pfRestart = NULL, BOOL *pfFlushAll = NULL);
  192. // inline methods to access member properties
  193. CSessionMgr *PSessionMgr();
  194. CComponentCollection *PCompCol();
  195. CViperActivity *PActivity();
  196. IDebugApplicationNode *PAppRoot();
  197. CTemplate *PGlobalTemplate();
  198. void SetGlobalTemplate(CTemplate *);
  199. TCHAR *GetMetabaseKey();
  200. TCHAR *GetApplnPath(SOURCEPATHTYPE = SOURCEPATHTYPE_PHYSICAL);
  201. TCHAR *GetGlobalAsa();
  202. DWORD GetNumSessions();
  203. DWORD GetNumRequests();
  204. BOOL FGlobalChanged();
  205. BOOL FDebuggable();
  206. BOOL FTombstone();
  207. BOOL FHasGlobalAsa();
  208. BOOL FFirstRequestRun();
  209. IDispatch *PGlobTypeLibWrapper();
  210. IUnknown *PServicesConfig();
  211. void SetFirstRequestRan();
  212. void SetGlobTypeLibWrapper(IDispatch *);
  213. HRESULT AddDirMonitorEntry(CDirMonitorEntry *);
  214. HRESULT AddFileApplnEntry(CFileApplnList *pFileAppln);
  215. CASPDirMonitorEntry *FPathMonitored(LPCTSTR pszPath);
  216. // Misc inline methods
  217. void InternalLock();
  218. void InternalUnLock();
  219. void IncrementSessionCount();
  220. void DecrementSessionCount();
  221. void IncrementRequestCount();
  222. void DecrementRequestCount();
  223. // AssertValid()
  224. public:
  225. #ifdef DBG
  226. virtual void AssertValid() const;
  227. #else
  228. virtual void AssertValid() const {}
  229. #endif
  230. // Cache on per-class basis
  231. ACACHE_INCLASS_DEFINITIONS()
  232. // Trace Log info -- keep in both free & checked builds so that ntsd extension will work for both builds
  233. // for FREE build, trace log is always NULL. Checked builds, it must be enabled.
  234. static PTRACE_LOG gm_pTraceLog;
  235. };
  236. /*===================================================================
  237. C A p p l n inlines
  238. ===================================================================*/
  239. inline CSessionMgr *CAppln::PSessionMgr()
  240. {
  241. return m_pSessionMgr;
  242. }
  243. inline CComponentCollection *CAppln::PCompCol()
  244. {
  245. return m_pApplCompCol;
  246. }
  247. inline CViperActivity *CAppln::PActivity()
  248. {
  249. return m_pActivity;
  250. }
  251. inline IDebugApplicationNode *CAppln::PAppRoot()
  252. {
  253. return m_pAppRoot;
  254. }
  255. inline TCHAR *CAppln::GetMetabaseKey()
  256. {
  257. return m_pszMetabaseKey;
  258. }
  259. inline TCHAR *CAppln::GetApplnPath(SOURCEPATHTYPE pathtype)
  260. {
  261. return (pathtype == SOURCEPATHTYPE_VIRTUAL? m_pszApplnVRoot :
  262. (pathtype == SOURCEPATHTYPE_PHYSICAL? m_pszApplnPath : NULL));
  263. }
  264. inline CTemplate *CAppln::PGlobalTemplate()
  265. {
  266. return m_pGlobalTemplate;
  267. }
  268. inline void CAppln::SetGlobalTemplate(CTemplate *pTemplate)
  269. {
  270. pTemplate->AddRef();
  271. m_pGlobalTemplate = pTemplate;
  272. }
  273. inline TCHAR *CAppln::GetGlobalAsa()
  274. {
  275. return m_pszGlobalAsa;
  276. }
  277. inline DWORD CAppln::GetNumSessions()
  278. {
  279. return m_cSessions;
  280. }
  281. inline DWORD CAppln::GetNumRequests()
  282. {
  283. return m_cRequests;
  284. }
  285. inline BOOL CAppln::FGlobalChanged()
  286. {
  287. return m_fGlobalChanged;
  288. }
  289. inline BOOL CAppln::FDebuggable()
  290. {
  291. return m_fDebuggable;
  292. }
  293. inline BOOL CAppln::FTombstone()
  294. {
  295. return m_fTombstone;
  296. }
  297. inline BOOL CAppln::FHasGlobalAsa()
  298. {
  299. return (m_pszGlobalAsa != NULL);
  300. }
  301. inline BOOL CAppln::FFirstRequestRun()
  302. {
  303. return m_fFirstRequestRan;
  304. }
  305. inline void CAppln::SetFirstRequestRan()
  306. {
  307. Assert(m_fInited);
  308. m_fFirstRequestRan = TRUE;
  309. }
  310. inline IDispatch *CAppln::PGlobTypeLibWrapper()
  311. {
  312. return m_pdispGlobTypeLibWrapper;
  313. }
  314. inline IUnknown *CAppln::PServicesConfig() {
  315. return m_pServicesConfig;
  316. }
  317. inline void CAppln::SetGlobTypeLibWrapper(IDispatch *pdisp)
  318. {
  319. if (m_pdispGlobTypeLibWrapper)
  320. m_pdispGlobTypeLibWrapper->Release();
  321. m_pdispGlobTypeLibWrapper = pdisp;
  322. if (m_pdispGlobTypeLibWrapper)
  323. m_pdispGlobTypeLibWrapper->AddRef();
  324. }
  325. inline void CAppln::IncrementSessionCount()
  326. {
  327. Assert(m_fInited);
  328. InterlockedIncrement((LPLONG)&m_cSessions);
  329. }
  330. inline void CAppln::DecrementSessionCount()
  331. {
  332. Assert(m_fInited);
  333. InterlockedDecrement((LPLONG)&m_cSessions);
  334. }
  335. inline void CAppln::IncrementRequestCount()
  336. {
  337. Assert(m_fInited);
  338. InterlockedIncrement((LPLONG)&m_cRequests);
  339. }
  340. inline void CAppln::DecrementRequestCount()
  341. {
  342. Assert(m_fInited);
  343. InterlockedDecrement((LPLONG)&m_cRequests);
  344. }
  345. inline void CAppln::InternalLock()
  346. {
  347. Assert(m_fInited);
  348. EnterCriticalSection(&m_csInternalLock);
  349. }
  350. inline void CAppln::InternalUnLock()
  351. {
  352. Assert(m_fInited);
  353. LeaveCriticalSection(&m_csInternalLock);
  354. }
  355. inline CAppConfig * CAppln::QueryAppConfig()
  356. {
  357. return m_pAppConfig;
  358. }
  359. inline BOOL CAppln::FConfigNeedsUpdate()
  360. {
  361. return m_pAppConfig->fNeedUpdate();
  362. }
  363. /*===================================================================
  364. C A p p l n M g r
  365. ===================================================================*/
  366. class CApplnMgr : public CHashTable
  367. {
  368. private:
  369. // Flags
  370. DWORD m_fInited : 1; // Are we initialized?
  371. DWORD m_fHashTableInited : 1; // Need to UnInit hash table?
  372. DWORD m_fCriticalSectionInited : 1; // Need to delete CS?
  373. // Critical section for locking
  374. CRITICAL_SECTION m_csLock;
  375. // List of script engines that need to be closed on next request.
  376. // (See comments in code, esp. CApplnMgr::AddEngine)
  377. CDblLink m_listEngineCleanup;
  378. public:
  379. CApplnMgr();
  380. ~CApplnMgr();
  381. HRESULT Init();
  382. HRESULT UnInit();
  383. // CAppln manipulations
  384. HRESULT AddAppln
  385. (
  386. TCHAR *pszApplnKey,
  387. TCHAR *pszApplnPath,
  388. CIsapiReqInfo *pIReq,
  389. HANDLE hUserImpersonation,
  390. CAppln **ppAppln
  391. );
  392. HRESULT FindAppln
  393. (
  394. TCHAR *pszApplnKey,
  395. CAppln **ppAppln
  396. );
  397. HRESULT DeleteApplicationIfExpired(CAppln *pAppln);
  398. HRESULT DeleteAllApplications();
  399. HRESULT RestartApplications(BOOL fRestartAllApplications = FALSE);
  400. // Add an engine to the deferred cleanup list/release engines in the list
  401. HRESULT AddEngine(CActiveScriptEngine *pEng);
  402. void CleanupEngines();
  403. // inlines
  404. void Lock();
  405. void UnLock();
  406. HANDLE HDeleteEvent();
  407. void SetDeleteEvent(void);
  408. };
  409. /*===================================================================
  410. C A p p l n M g r inlines
  411. ===================================================================*/
  412. inline void CApplnMgr::Lock()
  413. {
  414. Assert(m_fInited);
  415. EnterCriticalSection(&m_csLock);
  416. }
  417. inline void CApplnMgr::UnLock()
  418. {
  419. Assert(m_fInited);
  420. LeaveCriticalSection( &m_csLock );
  421. }
  422. /*===================================================================
  423. C A p p l n C l e a n u p M g r
  424. ===================================================================*/
  425. class CApplnCleanupMgr
  426. {
  427. private:
  428. // Flags
  429. DWORD m_fInited : 1; // Are we initialized?
  430. DWORD m_fCriticalSectionInited : 1; // Need to delete CS?
  431. DWORD m_fThreadAlive : 1; // worker thread alive?
  432. // Critical section for locking
  433. CRITICAL_SECTION m_csLock;
  434. HANDLE m_hAppToCleanup; // event to signal when there is an app to cleanup
  435. CLinkElem m_List;
  436. CAppln *Head();
  437. void AddElem(CAppln *pAppln);
  438. void RemoveElem(CAppln *pAppln);
  439. public:
  440. CApplnCleanupMgr();
  441. ~CApplnCleanupMgr();
  442. HRESULT Init();
  443. HRESULT UnInit();
  444. // CAppln manipulations
  445. HRESULT AddAppln
  446. (
  447. CAppln *ppAppln
  448. );
  449. void Wakeup();
  450. private:
  451. // inlines
  452. void Lock();
  453. void UnLock();
  454. // thread proc used to cleanup deleted applications
  455. static DWORD __stdcall ApplnCleanupThread(VOID *pArg);
  456. void ApplnCleanupDoWork();
  457. };
  458. /*===================================================================
  459. C A p p l n C l e a n u p M g r inlines
  460. ===================================================================*/
  461. inline void CApplnCleanupMgr::Lock()
  462. {
  463. Assert(m_fCriticalSectionInited);
  464. EnterCriticalSection(&m_csLock);
  465. }
  466. inline void CApplnCleanupMgr::UnLock()
  467. {
  468. Assert(m_fCriticalSectionInited);
  469. LeaveCriticalSection( &m_csLock );
  470. }
  471. inline CAppln *CApplnCleanupMgr::Head()
  472. {
  473. return ((m_List.m_pNext == &m_List) ? NULL : (CAppln *)m_List.m_pNext);
  474. }
  475. inline void CApplnCleanupMgr::AddElem(CAppln *pAppln)
  476. {
  477. pAppln->m_pNext = &m_List;
  478. pAppln->m_pPrev = m_List.m_pPrev;
  479. m_List.m_pPrev->m_pNext = pAppln;
  480. m_List.m_pPrev = pAppln;
  481. }
  482. inline void CApplnCleanupMgr::RemoveElem(CAppln *pAppln)
  483. {
  484. pAppln->m_pPrev->m_pNext = pAppln->m_pNext;
  485. pAppln->m_pNext->m_pPrev = pAppln->m_pPrev;
  486. }
  487. inline void CApplnCleanupMgr::Wakeup()
  488. {
  489. SetEvent(m_hAppToCleanup);
  490. }
  491. /*===================================================================
  492. C A p p l n M g r thread proc prototype
  493. ===================================================================*/
  494. void __cdecl RestartAppsThreadProc(VOID *arg);
  495. /*===================================================================
  496. Globals
  497. ===================================================================*/
  498. extern CApplnMgr g_ApplnMgr;
  499. extern DWORD g_nApplications;
  500. extern DWORD g_nApplicationsRestarting;
  501. /*===================================================================
  502. C A p p l n I t e r a t o r
  503. ===================================================================*/
  504. class CApplnIterator
  505. {
  506. private:
  507. CApplnMgr *m_pApplnMgr;
  508. CAppln *m_pCurr;
  509. BOOL m_fEnded; // iterator ended
  510. public:
  511. CApplnIterator(void);
  512. virtual ~CApplnIterator(void);
  513. public:
  514. HRESULT Start(CApplnMgr *pApplnMgr = NULL);
  515. HRESULT Stop(void);
  516. CAppln * Next(void);
  517. };
  518. #endif // APPLMGR_H