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.

183 lines
4.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // stats.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines functions for accessing the statistics in shared memory.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 09/10/1998 Original version.
  16. // 10/09/1998 Use a null DACL when creating mutex.
  17. // 09/28/1999 Only allow Administrators access to mutex.
  18. // 02/18/2000 Added proxy statistics.
  19. //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. #include <ias.h>
  22. #include <stats.h>
  23. //////////
  24. // Zero'ed out stats; used when the shared memory is unavailable.
  25. //////////
  26. RadiusStatistics defaultStats;
  27. RadiusProxyStatistics defaultProxyStats;
  28. //////////
  29. // Handles/pointers to the shared-memory statistics.
  30. //////////
  31. HANDLE theMonitor;
  32. HANDLE theMapping;
  33. HANDLE theProxyMapping;
  34. RadiusStatistics* theStats = &defaultStats;
  35. RadiusProxyStatistics* theProxy = &defaultProxyStats;
  36. BOOL
  37. WINAPI
  38. StatsOpen( VOID )
  39. {
  40. // Create the SID for local Administrators.
  41. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  42. PSID adminSid = (PSID)_alloca(GetSidLengthRequired(2));
  43. InitializeSid(
  44. adminSid,
  45. &sia,
  46. 2
  47. );
  48. *GetSidSubAuthority(adminSid, 0) = SECURITY_BUILTIN_DOMAIN_RID;
  49. *GetSidSubAuthority(adminSid, 1) = DOMAIN_ALIAS_RID_ADMINS;
  50. // Create an ACL giving Administrators all access.
  51. ULONG cbAcl = sizeof(ACL) +
  52. (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
  53. GetLengthSid(adminSid);
  54. PACL acl = (PACL)_alloca(cbAcl);
  55. InitializeAcl(
  56. acl,
  57. cbAcl,
  58. ACL_REVISION
  59. );
  60. AddAccessAllowedAce(
  61. acl,
  62. ACL_REVISION,
  63. MUTEX_ALL_ACCESS,
  64. adminSid
  65. );
  66. // Create a security descriptor with the above ACL.
  67. PSECURITY_DESCRIPTOR pSD;
  68. BYTE buffer[SECURITY_DESCRIPTOR_MIN_LENGTH];
  69. pSD = (PSECURITY_DESCRIPTOR)buffer;
  70. InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION);
  71. SetSecurityDescriptorDacl(pSD, TRUE, acl, FALSE);
  72. // Fill in the SECURITY_ATTRIBUTES struct.
  73. SECURITY_ATTRIBUTES sa;
  74. sa.nLength = sizeof(sa);
  75. sa.lpSecurityDescriptor = pSD;
  76. sa.bInheritHandle = TRUE;
  77. // Create the mutex.
  78. theMonitor = CreateMutex(
  79. &sa,
  80. FALSE,
  81. L"Global\\" RadiusStatisticsMutex
  82. );
  83. return theMonitor ? TRUE : FALSE;
  84. }
  85. VOID
  86. WINAPI
  87. StatsClose( VOID )
  88. {
  89. if (theStats != &defaultStats)
  90. {
  91. UnmapViewOfFile(theStats);
  92. theStats = &defaultStats;
  93. CloseHandle(theMapping);
  94. theMapping = NULL;
  95. }
  96. if (theProxy != &defaultProxyStats)
  97. {
  98. UnmapViewOfFile(theProxy);
  99. theProxy = &defaultProxyStats;
  100. CloseHandle(theProxyMapping);
  101. theProxyMapping = NULL;
  102. }
  103. CloseHandle(theMonitor);
  104. theMonitor = NULL;
  105. }
  106. VOID
  107. WINAPI
  108. StatsLock( VOID )
  109. {
  110. WaitForSingleObject(theMonitor, INFINITE);
  111. if (theStats == &defaultStats)
  112. {
  113. // Open the file mapping ...
  114. theMapping = OpenFileMappingW(
  115. FILE_MAP_READ,
  116. FALSE,
  117. L"Global\\" RadiusStatisticsName
  118. );
  119. if (theMapping)
  120. {
  121. // ... and map a view into our address space.
  122. PVOID view = MapViewOfFile(theMapping, FILE_MAP_READ, 0, 0, 0);
  123. if (view)
  124. {
  125. theStats = (RadiusStatistics*)view;
  126. }
  127. else
  128. {
  129. CloseHandle(theMapping);
  130. theMapping = NULL;
  131. }
  132. }
  133. }
  134. if (theProxy == &defaultProxyStats)
  135. {
  136. // Open the file mapping ...
  137. theProxyMapping = OpenFileMappingW(
  138. FILE_MAP_READ,
  139. FALSE,
  140. L"Global\\" RadiusProxyStatisticsName
  141. );
  142. if (theProxyMapping)
  143. {
  144. // ... and map a view into our address space.
  145. PVOID view = MapViewOfFile(theProxyMapping, FILE_MAP_READ, 0, 0, 0);
  146. if (view)
  147. {
  148. theProxy = (RadiusProxyStatistics*)view;
  149. }
  150. else
  151. {
  152. CloseHandle(theProxyMapping);
  153. theProxyMapping = NULL;
  154. }
  155. }
  156. }
  157. }
  158. VOID
  159. WINAPI
  160. StatsUnlock( VOID )
  161. {
  162. ReleaseMutex(theMonitor);
  163. }