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.

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