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.

308 lines
6.6 KiB

  1. #ifndef _SHLKMGR_H_
  2. #define _SHLKMGR_H_
  3. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  4. //
  5. // SHLKMGR.H
  6. //
  7. // Declaration of the CSharedLockMgr class which inherits from ILockCache
  8. // and is used in place of CLockCache for httpext. It wraps the shared
  9. // memory lock cache, needed to support recycling and multiple processes
  10. // handling dav requests.
  11. //
  12. // Copyright 2000 Microsoft Corporation, All Rights Reserved
  13. //
  14. #include <fhcache.h>
  15. #include <singlton.h>
  16. HRESULT HrGetUsableHandle(HANDLE hFile, DWORD dwProcessID, HANDLE * phFile);
  17. /**********************************
  18. * Class CSharedLockMgr
  19. *
  20. ***********************************/
  21. class CSharedLockMgr : private Singleton<CSharedLockMgr>
  22. {
  23. //
  24. // Friend declarations required by Singleton template
  25. //
  26. friend class Singleton<CSharedLockMgr>;
  27. //
  28. // The IFileHandleCache interface to the file handle cache
  29. //
  30. auto_ref_ptr<IFileHandleCache> m_pFileHandleCache;
  31. // CREATORS
  32. //
  33. CSharedLockMgr()
  34. {
  35. }
  36. ~CSharedLockMgr()
  37. {
  38. }
  39. // NOT IMPLEMENTED
  40. //
  41. CSharedLockMgr& operator=( const CSharedLockMgr& );
  42. CSharedLockMgr( const CSharedLockMgr& );
  43. HRESULT HrGetSIDFromHToken(HANDLE hit, BYTE ** ppSid, DWORD * pdwSid)
  44. {
  45. HRESULT hr = S_OK;
  46. enum { TOKENBUFFSIZE = (88) + sizeof(TOKEN_USER)};
  47. BYTE rgbTokenBuff[TOKENBUFFSIZE];
  48. DWORD dwTu = TOKENBUFFSIZE;
  49. TOKEN_USER * pTu = reinterpret_cast<TOKEN_USER *>(rgbTokenBuff);
  50. SID * psid;
  51. DWORD dwSid;
  52. auto_heap_ptr<BYTE> a_pSid;
  53. Assert(hit);
  54. Assert(ppSid);
  55. Assert(pdwSid);
  56. // Try to get the SID on this handle.
  57. //
  58. if (!GetTokenInformation(hit,
  59. TokenUser,
  60. pTu,
  61. dwTu,
  62. &dwTu))
  63. {
  64. hr = HRESULT_FROM_WIN32(GetLastError());
  65. goto ret;
  66. }
  67. psid = reinterpret_cast<SID *>(pTu->User.Sid);
  68. dwSid = GetSidLengthRequired(psid->SubAuthorityCount);
  69. Assert(dwSid);
  70. a_pSid = reinterpret_cast<BYTE *>(ExAlloc(dwSid));
  71. if (NULL == a_pSid.get())
  72. {
  73. hr = E_OUTOFMEMORY;
  74. goto ret;
  75. }
  76. if (!CopySid(dwSid, a_pSid.get(), pTu->User.Sid))
  77. {
  78. hr = HRESULT_FROM_WIN32(GetLastError());
  79. goto ret;
  80. }
  81. *ppSid = a_pSid.relinquish();
  82. *pdwSid = dwSid;
  83. ret:
  84. return hr;
  85. }
  86. public:
  87. // CREATORS
  88. //
  89. // Instance creating/destroying routines provided
  90. // by the Singleton template.
  91. //
  92. using Singleton<CSharedLockMgr>::CreateInstance;
  93. using Singleton<CSharedLockMgr>::DestroyInstance;
  94. using Singleton<CSharedLockMgr>::Instance;
  95. HRESULT HrInitialize()
  96. {
  97. HRESULT hr = S_OK;
  98. // Create an instance of the com-base file handle cache interface.
  99. //
  100. // Note that we do not init COM at any point. IIS should
  101. // have already done that for us.
  102. //
  103. hr = CoCreateInstance (CLSID_FileHandleCache,
  104. NULL,
  105. CLSCTX_LOCAL_SERVER | CLSCTX_ENABLE_AAA,
  106. IID_IFileHandleCache,
  107. reinterpret_cast<LPVOID*>(m_pFileHandleCache.load()));
  108. if (FAILED(hr))
  109. {
  110. DebugTrace( "CSharedLockMgr::HrInitialize() - CoCreateInstance(CLSID_FileHandleCache) failed 0x%08lX\n", hr );
  111. goto ret;
  112. }
  113. // Register this process with the file handle cache
  114. //
  115. hr = m_pFileHandleCache->HrRegisterWorkerProcess(GetCurrentProcessId());
  116. if (FAILED(hr))
  117. {
  118. DebugTrace( "CSharedLockMgr::HrInitialize() - IFileHandleCache::HrRegisterWorkerProcess() failed 0x%08lX\n", hr );
  119. goto ret;
  120. }
  121. ret:
  122. return hr;
  123. }
  124. BOOL FGetLockOnError(IMethUtil * pmu,
  125. LPCWSTR wszResource,
  126. DWORD dwLockType,
  127. BOOL fEmitXML = FALSE,
  128. CXMLEmitter * pemitter = NULL,
  129. CXNode * pxnParent = NULL);
  130. // CSharedLockMgr specific classes
  131. //=================================
  132. // Get the GUID string used by the locks
  133. //
  134. HRESULT HrGetGUIDString( HANDLE hit,
  135. UINT cchBufferLen,
  136. WCHAR * pwszGUIDString,
  137. UINT * pcchGUIDString)
  138. {
  139. safe_revert sr(hit);
  140. return m_pFileHandleCache->HrGetGUIDString(cchBufferLen,
  141. pwszGUIDString,
  142. pcchGUIDString);
  143. }
  144. // Used to generate a new shared data lock token with
  145. // the appropriate information stored it. Has to be
  146. // generated from here because of the need to get to the
  147. // new lock token ID, and to access the lock token guid.
  148. //
  149. HRESULT HrGetNewLockData(HANDLE hFile,
  150. HANDLE hit,
  151. SNewLockData * pnld,
  152. UINT cchBufferLen,
  153. WCHAR * pwszLockToken,
  154. UINT * pcchLockToken)
  155. {
  156. HRESULT hr = S_OK;
  157. auto_heap_ptr<BYTE> a_pSid;
  158. DWORD dwSid = 0;
  159. hr = HrGetSIDFromHToken(hit, a_pSid.load(), &dwSid);
  160. if (FAILED(hr))
  161. {
  162. goto ret;
  163. }
  164. {
  165. safe_revert sr(hit);
  166. hr = m_pFileHandleCache->HrGetNewLockData(reinterpret_cast<DWORD_PTR>(hFile),
  167. GetCurrentProcessId(),
  168. dwSid,
  169. a_pSid.get(),
  170. pnld,
  171. cchBufferLen,
  172. pwszLockToken,
  173. pcchLockToken);
  174. if (FAILED(hr))
  175. {
  176. goto ret;
  177. }
  178. }
  179. ret:
  180. return hr;
  181. }
  182. HRESULT HrGetLockData(LARGE_INTEGER liLockID,
  183. HANDLE hit,
  184. LPCWSTR pwszPath,
  185. DWORD dwTimeout,
  186. SNewLockData * pnld,
  187. SLockHandleData * plhd,
  188. UINT cchBufferLen,
  189. WCHAR rgwszLockToken[],
  190. UINT * pcchLockToken)
  191. {
  192. HRESULT hr = S_OK;
  193. auto_heap_ptr<BYTE> a_pSid;
  194. DWORD dwSid = 0;
  195. hr = HrGetSIDFromHToken(hit, a_pSid.load(), &dwSid);
  196. if (FAILED(hr))
  197. {
  198. goto ret;
  199. }
  200. {
  201. safe_revert sr(hit);
  202. hr = m_pFileHandleCache->HrGetLockData(liLockID,
  203. dwSid,
  204. a_pSid.get(),
  205. pwszPath,
  206. dwTimeout,
  207. pnld,
  208. plhd,
  209. cchBufferLen,
  210. rgwszLockToken,
  211. pcchLockToken);
  212. if (FAILED(hr))
  213. {
  214. goto ret;
  215. }
  216. }
  217. ret:
  218. return hr;
  219. }
  220. HRESULT HrCheckLockID(LARGE_INTEGER liLockID,
  221. HANDLE hit,
  222. LPCWSTR pwszPath)
  223. {
  224. HRESULT hr = S_OK;
  225. auto_heap_ptr<BYTE> a_pSid;
  226. DWORD dwSid = 0;
  227. hr = HrGetSIDFromHToken(hit, a_pSid.load(), &dwSid);
  228. if (FAILED(hr))
  229. {
  230. goto ret;
  231. }
  232. {
  233. safe_revert sr(hit);
  234. hr = m_pFileHandleCache->HrCheckLockID(liLockID,
  235. dwSid,
  236. a_pSid.get(),
  237. pwszPath);
  238. if (FAILED(hr))
  239. {
  240. goto ret;
  241. }
  242. }
  243. ret:
  244. return hr;
  245. }
  246. HRESULT HrDeleteLock(HANDLE hit,
  247. LARGE_INTEGER liLockID)
  248. {
  249. safe_revert sr(hit);
  250. return m_pFileHandleCache->HrDeleteLock(liLockID);
  251. }
  252. };
  253. #endif // end _SHLKMGR_H_ define