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.

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