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.

180 lines
3.6 KiB

  1. /*
  2. - PERFUTIL.C
  3. -
  4. * Purpose:
  5. * Contain utility functions used by both the CNTR objects and the
  6. * DMI PerfMon DLL. Mainly the shared memory routines used to
  7. * convey the perf data across the process boundry.
  8. *
  9. *
  10. */
  11. #include "perfutil.h"
  12. #include <winerror.h>
  13. #include <sddl.h>
  14. #include <Aclapi.h>
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. /*
  20. - HrOpenSharedMemory
  21. -
  22. * Purpose:
  23. * Opens the shared memory data block that is used by the PerfMon
  24. * counter. To be called by either the App or PerfDll to initialize
  25. * the data block.
  26. *
  27. * Parameters:
  28. * szName Name of the shared file mapping
  29. * dwSize Size of shared memory block requested
  30. * psz Pointer to SECURITY_ATTRIBUTES for Shared Memory
  31. * ph Pointer to the returned shared memory handle
  32. * ppv Pointer to the returned shared memory pointer
  33. * pfExist Pointer to a BOOL indicating this named shared
  34. * block has already been opened.
  35. *
  36. * Errors:
  37. * hr Indicating success or failure
  38. *
  39. */
  40. #define Align4K(cb) ((cb + (4 << 10) - 1) & ~((4 << 10) - 1))
  41. HRESULT
  42. HrOpenSharedMemory(LPWSTR szName,
  43. DWORD dwSize,
  44. SECURITY_ATTRIBUTES * psa,
  45. HANDLE * ph,
  46. LPVOID * ppv,
  47. BOOL * pfExist)
  48. {
  49. HRESULT hr = NOERROR;
  50. // Validate params
  51. if (!szName || !ph || !ppv || !pfExist || !psa)
  52. return E_INVALIDARG;
  53. // Do we really need to create a mapping?
  54. if (0 == dwSize)
  55. return E_INVALIDARG;
  56. *ppv = NULL;
  57. *ph = CreateFileMapping(INVALID_HANDLE_VALUE,
  58. psa,
  59. PAGE_READWRITE,
  60. 0,
  61. Align4K(dwSize),
  62. szName);
  63. if (*ph)
  64. {
  65. *pfExist = !!(GetLastError() == ERROR_ALREADY_EXISTS);
  66. *ppv = MapViewOfFile(*ph, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  67. }
  68. if (*ppv == NULL)
  69. hr = E_OUTOFMEMORY;
  70. return hr;
  71. }
  72. /*
  73. - HrInitializeSecurityAttribute
  74. -
  75. * Purpose:
  76. * Initalize the SECURITY_ATTRIBUTES data structure for later use.
  77. *
  78. * Parameters:
  79. * psa pointer to caller allocated SECURITY_ATTRIBUTES struct
  80. * psd pointer to caller allocated SECURITY_DESCRIPTOR struct
  81. *
  82. * Errors:
  83. * hr Indicates success or failure
  84. */
  85. HRESULT
  86. HrInitializeSecurityAttribute(SECURITY_ATTRIBUTES * psa)
  87. {
  88. HRESULT hr = S_OK;
  89. WCHAR wszSDL[MAX_PATH]=L"D:(A;OICI;GA;;;BA)(A;OICIIO;GA;;;CO)(A;OICI;GA;;;NS)(A;OICI;GA;;;SY)";
  90. SECURITY_DESCRIPTOR *pSD=NULL;
  91. ULONG lSize=0;
  92. if (!psa )
  93. {
  94. hr = E_INVALIDARG;
  95. goto ret;
  96. }
  97. if(!ConvertStringSecurityDescriptorToSecurityDescriptorW(
  98. wszSDL,
  99. SDDL_REVISION_1,
  100. &pSD,
  101. &lSize))
  102. {
  103. return HRESULT_FROM_WIN32(GetLastError());
  104. }
  105. psa->nLength = sizeof(SECURITY_ATTRIBUTES);
  106. psa->lpSecurityDescriptor = pSD;
  107. psa->bInheritHandle = TRUE;
  108. ret:
  109. return hr;
  110. }
  111. /*
  112. - HrCreatePerfMutex
  113. -
  114. * Purpose:
  115. * Initializes the mutex object used to protect access to the per-instance
  116. * PerfMon data. This is used by methods of the INSTCNTR class and the
  117. * _perfapp.lib. Upon successful return from the function, the caller owns
  118. * the mutex and must release it as appropriate.
  119. *
  120. * Parameters:
  121. * phmtx Pointer to a handle to the returned mutex
  122. *
  123. * Errors:
  124. * hr Indicating success or failure
  125. */
  126. HRESULT
  127. HrCreatePerfMutex(SECURITY_ATTRIBUTES * psa,
  128. LPWSTR szMutexName,
  129. HANDLE * phmtx)
  130. {
  131. HRESULT hr = S_OK;
  132. if (!psa || !szMutexName || !phmtx)
  133. {
  134. return E_INVALIDARG;
  135. }
  136. *phmtx = CreateMutex(psa, TRUE, szMutexName);
  137. if (*phmtx == NULL)
  138. {
  139. hr = E_FAIL;
  140. }
  141. return hr;
  142. }
  143. #ifdef __cplusplus
  144. }
  145. #endif