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.

656 lines
17 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. //========= Critical section for internal lock
  130. CRITICAL_SECTION m_csInternalLock;
  131. //========= External lock support
  132. CRITICAL_SECTION m_csApplnLock;
  133. DWORD m_dwLockThreadID; // thread which locked
  134. DWORD m_cLockRefCount; // lock count
  135. //========= Notification support
  136. // Identifiers stored by notification system
  137. CPtrArray m_rgpvDME; // list of directory monitor entries
  138. CPtrArray m_rgpvFileAppln; // list of entries relating files to applications
  139. // User impersonation handle for UNC change notification
  140. HANDLE m_hUserImpersonation;
  141. //========= Type Library wrapper from GLOBAL.ASA
  142. IDispatch *m_pdispGlobTypeLibWrapper;
  143. //========= SupportErrorInfo
  144. // Interface to indicate that we support ErrorInfo reporting
  145. CSupportErrorInfo m_ISuppErrImp;
  146. //========= Debugging Support
  147. // root node for browsing of running documents
  148. IDebugApplicationNode *m_pAppRoot;
  149. // proc used to asynchronously cleanup the app
  150. HRESULT ApplnCleanupProc();
  151. public:
  152. CAppln();
  153. ~CAppln();
  154. HRESULT Init
  155. (
  156. TCHAR *pszApplnKey,
  157. TCHAR *pszApplnPath,
  158. CIsapiReqInfo *pIReq,
  159. HANDLE hUserImpersonation
  160. );
  161. // cnvert to tombstone state
  162. HRESULT UnInit();
  163. // create application's activity as clone of param
  164. HRESULT BindToActivity(CViperActivity *pActivity = NULL);
  165. // set (and remember) global.asa for this app
  166. HRESULT SetGlobalAsa(const TCHAR *pszGlobalAsa);
  167. // make sure script didn't leave locks
  168. HRESULT UnLockAfterRequest();
  169. // Non-delegating object IUnknown
  170. STDMETHODIMP QueryInterface(REFIID, void **);
  171. STDMETHODIMP_(ULONG) AddRef();
  172. STDMETHODIMP_(ULONG) Release();
  173. // Tombstone stub
  174. HRESULT CheckForTombstone();
  175. // Restart an application (such as when global.asa changes)
  176. HRESULT Restart(BOOL fForceRestart = FALSE);
  177. // IApplicationObject functions
  178. STDMETHODIMP Lock();
  179. STDMETHODIMP UnLock();
  180. STDMETHODIMP get_Value(BSTR bstr, VARIANT *pvar);
  181. STDMETHODIMP put_Value(BSTR bstr, VARIANT var);
  182. STDMETHODIMP putref_Value(BSTR bstr, VARIANT var);
  183. STDMETHODIMP get_Contents(IVariantDictionary **ppDictReturn);
  184. STDMETHODIMP get_StaticObjects(IVariantDictionary **ppDictReturn);
  185. // Application config related methods
  186. CAppConfig *QueryAppConfig();
  187. BOOL FConfigNeedsUpdate();
  188. HRESULT UpdateConfig(CIsapiReqInfo *pIReq, BOOL *pfRestart = NULL, BOOL *pfFlushAll = NULL);
  189. // inline methods to access member properties
  190. CSessionMgr *PSessionMgr();
  191. CComponentCollection *PCompCol();
  192. CViperActivity *PActivity();
  193. IDebugApplicationNode *PAppRoot();
  194. CTemplate *PGlobalTemplate();
  195. void SetGlobalTemplate(CTemplate *);
  196. TCHAR *GetMetabaseKey();
  197. TCHAR *GetApplnPath(SOURCEPATHTYPE = SOURCEPATHTYPE_PHYSICAL);
  198. TCHAR *GetGlobalAsa();
  199. DWORD GetNumSessions();
  200. DWORD GetNumRequests();
  201. BOOL FGlobalChanged();
  202. BOOL FDebuggable();
  203. BOOL FTombstone();
  204. BOOL FHasGlobalAsa();
  205. BOOL FFirstRequestRun();
  206. IDispatch *PGlobTypeLibWrapper();
  207. void SetFirstRequestRan();
  208. void SetGlobTypeLibWrapper(IDispatch *);
  209. HRESULT AddDirMonitorEntry(CDirMonitorEntry *);
  210. HRESULT AddFileApplnEntry(CFileApplnList *pFileAppln);
  211. CASPDirMonitorEntry *FPathMonitored(LPCTSTR pszPath);
  212. // Misc inline methods
  213. void InternalLock();
  214. void InternalUnLock();
  215. void IncrementSessionCount();
  216. void DecrementSessionCount();
  217. void IncrementRequestCount();
  218. void DecrementRequestCount();
  219. // AssertValid()
  220. public:
  221. #ifdef DBG
  222. virtual void AssertValid() const;
  223. #else
  224. virtual void AssertValid() const {}
  225. #endif
  226. // Cache on per-class basis
  227. ACACHE_INCLASS_DEFINITIONS()
  228. // Trace Log info -- keep in both free & checked builds so that ntsd extension will work for both builds
  229. // for FREE build, trace log is always NULL. Checked builds, it must be enabled.
  230. static PTRACE_LOG gm_pTraceLog;
  231. };
  232. /*===================================================================
  233. C A p p l n inlines
  234. ===================================================================*/
  235. inline CSessionMgr *CAppln::PSessionMgr()
  236. {
  237. return m_pSessionMgr;
  238. }
  239. inline CComponentCollection *CAppln::PCompCol()
  240. {
  241. return m_pApplCompCol;
  242. }
  243. inline CViperActivity *CAppln::PActivity()
  244. {
  245. return m_pActivity;
  246. }
  247. inline IDebugApplicationNode *CAppln::PAppRoot()
  248. {
  249. return m_pAppRoot;
  250. }
  251. inline TCHAR *CAppln::GetMetabaseKey()
  252. {
  253. return m_pszMetabaseKey;
  254. }
  255. inline TCHAR *CAppln::GetApplnPath(SOURCEPATHTYPE pathtype)
  256. {
  257. return (pathtype == SOURCEPATHTYPE_VIRTUAL? m_pszApplnVRoot :
  258. (pathtype == SOURCEPATHTYPE_PHYSICAL? m_pszApplnPath : NULL));
  259. }
  260. inline CTemplate *CAppln::PGlobalTemplate()
  261. {
  262. return m_pGlobalTemplate;
  263. }
  264. inline void CAppln::SetGlobalTemplate(CTemplate *pTemplate)
  265. {
  266. pTemplate->AddRef();
  267. m_pGlobalTemplate = pTemplate;
  268. }
  269. inline TCHAR *CAppln::GetGlobalAsa()
  270. {
  271. return m_pszGlobalAsa;
  272. }
  273. inline DWORD CAppln::GetNumSessions()
  274. {
  275. return m_cSessions;
  276. }
  277. inline DWORD CAppln::GetNumRequests()
  278. {
  279. return m_cRequests;
  280. }
  281. inline BOOL CAppln::FGlobalChanged()
  282. {
  283. return m_fGlobalChanged;
  284. }
  285. inline BOOL CAppln::FDebuggable()
  286. {
  287. return m_fDebuggable;
  288. }
  289. inline BOOL CAppln::FTombstone()
  290. {
  291. return m_fTombstone;
  292. }
  293. inline BOOL CAppln::FHasGlobalAsa()
  294. {
  295. return (m_pszGlobalAsa != NULL);
  296. }
  297. inline BOOL CAppln::FFirstRequestRun()
  298. {
  299. return m_fFirstRequestRan;
  300. }
  301. inline void CAppln::SetFirstRequestRan()
  302. {
  303. Assert(m_fInited);
  304. m_fFirstRequestRan = TRUE;
  305. }
  306. inline IDispatch *CAppln::PGlobTypeLibWrapper()
  307. {
  308. return m_pdispGlobTypeLibWrapper;
  309. }
  310. inline void CAppln::SetGlobTypeLibWrapper(IDispatch *pdisp)
  311. {
  312. if (m_pdispGlobTypeLibWrapper)
  313. m_pdispGlobTypeLibWrapper->Release();
  314. m_pdispGlobTypeLibWrapper = pdisp;
  315. if (m_pdispGlobTypeLibWrapper)
  316. m_pdispGlobTypeLibWrapper->AddRef();
  317. }
  318. inline void CAppln::IncrementSessionCount()
  319. {
  320. Assert(m_fInited);
  321. InterlockedIncrement((LPLONG)&m_cSessions);
  322. }
  323. inline void CAppln::DecrementSessionCount()
  324. {
  325. Assert(m_fInited);
  326. InterlockedDecrement((LPLONG)&m_cSessions);
  327. }
  328. inline void CAppln::IncrementRequestCount()
  329. {
  330. Assert(m_fInited);
  331. InterlockedIncrement((LPLONG)&m_cRequests);
  332. }
  333. inline void CAppln::DecrementRequestCount()
  334. {
  335. Assert(m_fInited);
  336. InterlockedDecrement((LPLONG)&m_cRequests);
  337. }
  338. inline void CAppln::InternalLock()
  339. {
  340. Assert(m_fInited);
  341. EnterCriticalSection(&m_csInternalLock);
  342. }
  343. inline void CAppln::InternalUnLock()
  344. {
  345. Assert(m_fInited);
  346. LeaveCriticalSection(&m_csInternalLock);
  347. }
  348. inline CAppConfig * CAppln::QueryAppConfig()
  349. {
  350. return m_pAppConfig;
  351. }
  352. inline BOOL CAppln::FConfigNeedsUpdate()
  353. {
  354. return m_pAppConfig->fNeedUpdate();
  355. }
  356. /*===================================================================
  357. C A p p l n M g r
  358. ===================================================================*/
  359. class CApplnMgr : public CHashTable
  360. {
  361. private:
  362. // Flags
  363. DWORD m_fInited : 1; // Are we initialized?
  364. DWORD m_fHashTableInited : 1; // Need to UnInit hash table?
  365. DWORD m_fCriticalSectionInited : 1; // Need to delete CS?
  366. // Event handles
  367. HANDLE m_hDeleteApplnEvent; // Flush application object
  368. // Critical section for locking
  369. CRITICAL_SECTION m_csLock;
  370. // List of script engines that need to be closed on next request.
  371. // (See comments in code, esp. CApplnMgr::AddEngine)
  372. CDblLink m_listEngineCleanup;
  373. public:
  374. CApplnMgr();
  375. ~CApplnMgr();
  376. HRESULT Init();
  377. HRESULT UnInit();
  378. // CAppln manipulations
  379. HRESULT AddAppln
  380. (
  381. TCHAR *pszApplnKey,
  382. TCHAR *pszApplnPath,
  383. CIsapiReqInfo *pIReq,
  384. HANDLE hUserImpersonation,
  385. CAppln **ppAppln
  386. );
  387. HRESULT FindAppln
  388. (
  389. TCHAR *pszApplnKey,
  390. CAppln **ppAppln
  391. );
  392. HRESULT DeleteApplicationIfExpired(CAppln *pAppln);
  393. HRESULT DeleteAllApplications();
  394. HRESULT RestartApplications(BOOL fRestartAllApplications = FALSE);
  395. // Add an engine to the deferred cleanup list/release engines in the list
  396. HRESULT AddEngine(CActiveScriptEngine *pEng);
  397. void CleanupEngines();
  398. // inlines
  399. void Lock();
  400. void UnLock();
  401. HANDLE HDeleteEvent();
  402. void SetDeleteEvent(void);
  403. };
  404. /*===================================================================
  405. C A p p l n M g r inlines
  406. ===================================================================*/
  407. inline void CApplnMgr::Lock()
  408. {
  409. Assert(m_fInited);
  410. EnterCriticalSection(&m_csLock);
  411. }
  412. inline void CApplnMgr::UnLock()
  413. {
  414. Assert(m_fInited);
  415. LeaveCriticalSection( &m_csLock );
  416. }
  417. inline HANDLE CApplnMgr::HDeleteEvent()
  418. {
  419. return m_hDeleteApplnEvent;
  420. }
  421. inline void CApplnMgr::SetDeleteEvent()
  422. {
  423. DWORD error;
  424. Assert(m_hDeleteApplnEvent != INVALID_HANDLE_VALUE);
  425. BOOL flag = SetEvent(m_hDeleteApplnEvent);
  426. if (!flag)
  427. error = GetLastError();
  428. }
  429. /*===================================================================
  430. C A p p l n C l e a n u p M g r
  431. ===================================================================*/
  432. class CApplnCleanupMgr : public CHashTable
  433. {
  434. private:
  435. // Flags
  436. DWORD m_fInited : 1; // Are we initialized?
  437. DWORD m_fHashTableInited : 1; // Need to UnInit hash table?
  438. DWORD m_fCriticalSectionInited : 1; // Need to delete CS?
  439. DWORD m_fThreadAlive : 1; // worker thread alive?
  440. // Critical section for locking
  441. CRITICAL_SECTION m_csLock;
  442. HANDLE m_hAppToCleanup; // event to signal when there is an app to cleanup
  443. public:
  444. CApplnCleanupMgr();
  445. ~CApplnCleanupMgr();
  446. HRESULT Init();
  447. HRESULT UnInit();
  448. // CAppln manipulations
  449. HRESULT AddAppln
  450. (
  451. CAppln *ppAppln
  452. );
  453. private:
  454. // inlines
  455. void Lock();
  456. void UnLock();
  457. // thread proc used to cleanup deleted applications
  458. static DWORD __stdcall ApplnCleanupThread(VOID *pArg);
  459. void ApplnCleanupDoWork();
  460. };
  461. /*===================================================================
  462. C A p p l n C l e a n u p M g r inlines
  463. ===================================================================*/
  464. inline void CApplnCleanupMgr::Lock()
  465. {
  466. Assert(m_fCriticalSectionInited);
  467. EnterCriticalSection(&m_csLock);
  468. }
  469. inline void CApplnCleanupMgr::UnLock()
  470. {
  471. Assert(m_fCriticalSectionInited);
  472. LeaveCriticalSection( &m_csLock );
  473. }
  474. /*===================================================================
  475. C A p p l n M g r thread proc prototype
  476. ===================================================================*/
  477. void __cdecl RestartAppsThreadProc(VOID *arg);
  478. /*===================================================================
  479. Globals
  480. ===================================================================*/
  481. extern CApplnMgr g_ApplnMgr;
  482. extern DWORD g_nApplications;
  483. extern DWORD g_nApplicationsRestarting;
  484. /*===================================================================
  485. C A p p l n I t e r a t o r
  486. ===================================================================*/
  487. class CApplnIterator
  488. {
  489. private:
  490. CApplnMgr *m_pApplnMgr;
  491. CAppln *m_pCurr;
  492. BOOL m_fEnded; // iterator ended
  493. public:
  494. CApplnIterator(void);
  495. virtual ~CApplnIterator(void);
  496. public:
  497. HRESULT Start(CApplnMgr *pApplnMgr = NULL);
  498. HRESULT Stop(void);
  499. CAppln * Next(void);
  500. };
  501. #endif // APPLMGR_H