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.

280 lines
7.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: df32.hxx
  7. //
  8. // Contents: Docfile generic header for 32-bit functions
  9. //
  10. // Classes: CGlobalSecurity
  11. // CDfMutex
  12. //
  13. // History: 09-Oct-93 DrewB Created
  14. //
  15. //----------------------------------------------------------------------------
  16. #ifndef __DF32_HXX__
  17. #define __DF32_HXX__
  18. #ifdef WIN32
  19. #include <dfexcept.hxx>
  20. // Make an scode out of the last Win32 error
  21. // Error that may map to STG_* scodes should go through Win32ErrorToScode
  22. #define WIN32_SCODE(err) HRESULT_FROM_WIN32(err)
  23. #define LAST_SCODE WIN32_SCODE(GetLastError())
  24. #define LAST_STG_SCODE Win32ErrorToScode(GetLastError())
  25. //+---------------------------------------------------------------------------
  26. //
  27. // Class: CGlobalSecurity (gs)
  28. //
  29. // Purpose: Encapsulates a global SECURITY_DESCRIPTOR and
  30. // SECURITY_ATTRIBUTES
  31. //
  32. // Interface: See below
  33. //
  34. // History: 18-Jun-93 DrewB Created
  35. //
  36. // Notes: Only active for Win32 platforms which support security
  37. // Init MUST be called before this is used
  38. //
  39. //----------------------------------------------------------------------------
  40. #if WIN32 == 100 || WIN32 > 200
  41. // This leaves space for 8 sub authorities. Currently NT only uses 6
  42. const DWORD SIZEOF_SID = 44;
  43. // This leaves space for 1 access allowed ACEs in the ACL.
  44. const DWORD SIZEOF_ACL = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + SIZEOF_SID;
  45. const DWORD SIZEOF_TOKEN_USER = sizeof(TOKEN_USER) + SIZEOF_SID;
  46. class CGlobalSecurity
  47. {
  48. private:
  49. BYTE _acl[SIZEOF_ACL];
  50. SECURITY_DESCRIPTOR _sd;
  51. BYTE _sdExt[SIZEOF_SID*2 + SIZEOF_ACL];
  52. SECURITY_ATTRIBUTES _sa;
  53. #if DBG == 1
  54. BOOL _fInit;
  55. #endif
  56. public:
  57. #if DBG == 1
  58. CGlobalSecurity(void) { _fInit = FALSE; }
  59. #endif
  60. SCODE Init(BOOL fAcl)
  61. {
  62. #ifdef MULTIHEAP
  63. ACL *pacl = fAcl ? (ACL *) &_acl : NULL;
  64. BYTE pTokenUser[SIZEOF_TOKEN_USER];
  65. if (pacl != NULL)
  66. {
  67. BOOL fToken = TRUE;
  68. HANDLE hToken;
  69. DWORD lIgnore;
  70. // Initialize a new ACL.
  71. if (!InitializeAcl( pacl, SIZEOF_ACL, ACL_REVISION))
  72. return LAST_SCODE;
  73. if (!OpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &hToken))
  74. {
  75. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken))
  76. fToken = FALSE;
  77. }
  78. if (fToken)
  79. {
  80. if (!GetTokenInformation( hToken, TokenUser,
  81. (TOKEN_USER*)pTokenUser, SIZEOF_TOKEN_USER, &lIgnore ))
  82. {
  83. CloseHandle (hToken);
  84. return LAST_SCODE;
  85. }
  86. CloseHandle (hToken);
  87. // Allow current user access.
  88. if (!AddAccessAllowedAce( pacl, ACL_REVISION,
  89. STANDARD_RIGHTS_ALL | GENERIC_ALL,
  90. ((TOKEN_USER *)pTokenUser)->User.Sid ))
  91. return LAST_SCODE;
  92. }
  93. }
  94. #else
  95. ACL *pacl = NULL;
  96. #endif
  97. if (!InitializeSecurityDescriptor(&_sd, SECURITY_DESCRIPTOR_REVISION))
  98. return LAST_SCODE;
  99. if (!SetSecurityDescriptorDacl(&_sd, TRUE, pacl, FALSE))
  100. return LAST_SCODE;
  101. _sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  102. _sa.lpSecurityDescriptor = &_sd;
  103. _sa.bInheritHandle = FALSE;
  104. #if DBG == 1
  105. _fInit = TRUE;
  106. #endif
  107. return S_OK;
  108. }
  109. operator SECURITY_DESCRIPTOR *(void) { olAssert(_fInit); return &_sd; }
  110. operator SECURITY_ATTRIBUTES *(void) { olAssert(_fInit); return &_sa; }
  111. };
  112. #endif
  113. //
  114. // Global Critical Sections have two components. One piece is shared between
  115. // all applications using the global lock. This portion will typically reside
  116. // in some sort of shared memory. The second piece is per-process. This
  117. // contains a per-process handle to the shared critical section lock semaphore.
  118. // The semaphore is itself shared, but each process may have a different handle
  119. // value to the semaphore.
  120. //
  121. // Global critical sections are attached to by name. The application wishing to
  122. // attach must know the name of the critical section (actually the name of the
  123. // shared lock semaphore, and must know the address of the global portion of
  124. // the critical section
  125. //
  126. #define SUPPORT_RECURSIVE_LOCK
  127. typedef struct _GLOBAL_SHARED_CRITICAL_SECTION {
  128. LONG LockCount;
  129. #ifdef SUPPORT_RECURSIVE_LOCK
  130. LONG RecursionCount;
  131. DWORD OwningThread;
  132. #else
  133. #if DBG == 1
  134. DWORD OwningThread;
  135. #endif
  136. #endif
  137. DWORD Reserved;
  138. } GLOBAL_SHARED_CRITICAL_SECTION, *PGLOBAL_SHARED_CRITICAL_SECTION;
  139. //+---------------------------------------------------------------------------
  140. //
  141. // Class: CDfMutex (dmtx)
  142. //
  143. // Purpose: A multi-process synchronization object
  144. //
  145. // Interface: See below
  146. //
  147. // History: 05-Apr-93 DrewB Created
  148. // 19-Jul-95 SusiA Added HaveMutex
  149. //
  150. // Notes: Only active for Win32 implementations which support threads
  151. // For platforms with security, a global security descriptor is
  152. // used
  153. //
  154. //----------------------------------------------------------------------------
  155. // Default timeout of twenty minutes
  156. #define DFM_TIMEOUT 1200000
  157. class CDfMutex
  158. {
  159. public:
  160. inline CDfMutex(void);
  161. SCODE Init(TCHAR *ptcsName);
  162. ~CDfMutex(void);
  163. SCODE Take(DWORD dwTimeout);
  164. void Release(void);
  165. BOOL IsHandleValid (TCHAR *ptcsName);
  166. #if DBG == 1
  167. //check to see if the current thread already has the mutex
  168. inline BOOL HaveMutex(void);
  169. #endif
  170. private:
  171. PGLOBAL_SHARED_CRITICAL_SECTION _pGlobalPortion;
  172. HANDLE _hLockSemaphore;
  173. HANDLE _hSharedMapping;
  174. };
  175. inline CDfMutex::CDfMutex(void)
  176. {
  177. _pGlobalPortion = NULL;
  178. _hLockSemaphore = NULL;
  179. _hSharedMapping = NULL;
  180. }
  181. #if DBG == 1
  182. //+--------------------------------------------------------------
  183. //
  184. // Member: CDfMutex::HaveMutex, public
  185. //
  186. // Synopsis: This routine checks to see if the current thread
  187. // already has the mutex
  188. //
  189. // History: 19-Jul-95 SusiA Created
  190. //
  191. // Algorithm: Checks the current thread to see if it already owns
  192. // the mutex. Returns TRUE if it does, FALSE otherwise
  193. //
  194. //
  195. //---------------------------------------------------------------
  196. inline BOOL
  197. CDfMutex::HaveMutex(
  198. void
  199. )
  200. {
  201. if ( _pGlobalPortion->OwningThread == GetCurrentThreadId())
  202. return TRUE;
  203. else
  204. return FALSE;
  205. }
  206. #endif
  207. //+---------------------------------------------------------------------------
  208. //
  209. // Class: CStaticDfMutex (sdmtx)
  210. //
  211. // Purpose: Static version of CDfMutex
  212. //
  213. // Interface: CDfMutex
  214. //
  215. // History: 10-Oct-93 DrewB Created
  216. //
  217. // Notes: Throws exceptions on initialization failures
  218. //
  219. //----------------------------------------------------------------------------
  220. class CStaticDfMutex : public CDfMutex
  221. {
  222. public:
  223. inline CStaticDfMutex(TCHAR *ptcsName);
  224. };
  225. inline CStaticDfMutex::CStaticDfMutex(TCHAR *ptcsName)
  226. : CDfMutex()
  227. {
  228. SCODE sc;
  229. sc = Init(ptcsName);
  230. if (FAILED(sc))
  231. THROW_SC(sc);
  232. }
  233. #ifdef ONETHREAD
  234. //Mutex used to control access for based pointers.
  235. extern CStaticDfMutex s_dmtxProcess;
  236. #endif
  237. #endif // WIN32
  238. #endif // #ifndef __DF32_HXX__