handle_workaround.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright (c) 2018 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_DETAIL_WINDOWS_HANDLE_WORKAROUND_HPP_
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_HANDLE_WORKAROUND_HPP_
  7. #include <boost/winapi/basic_types.hpp>
  8. #include <boost/winapi/dll.hpp>
  9. #include <boost/winapi/access_rights.hpp>
  10. //#define BOOST_USE_WINDOWS_H 1
  11. #if defined( BOOST_USE_WINDOWS_H )
  12. #include <Winternl.h>
  13. #endif
  14. namespace boost { namespace process { namespace detail { namespace windows { namespace workaround
  15. {
  16. typedef struct _SYSTEM_HANDLE_ENTRY_
  17. {
  18. ::boost::winapi::ULONG_ OwnerPid;
  19. ::boost::winapi::BYTE_ ObjectType;
  20. ::boost::winapi::BYTE_ HandleFlags;
  21. ::boost::winapi::USHORT_ HandleValue;
  22. ::boost::winapi::PVOID_ ObjectPointer;
  23. ::boost::winapi::ULONG_ AccessMask;
  24. } SYSTEM_HANDLE_ENTRY_, *PSYSTEM_HANDLE_ENTRY_;
  25. typedef struct _SYSTEM_HANDLE_INFORMATION_
  26. {
  27. ::boost::winapi::ULONG_ Count;
  28. SYSTEM_HANDLE_ENTRY_ Handle[1];
  29. } SYSTEM_HANDLE_INFORMATION_, *PSYSTEM_HANDLE_INFORMATION_;
  30. #if defined( BOOST_USE_WINDOWS_H )
  31. using UNICODE_STRING_ = ::UNICODE_STRING;
  32. using GENERIC_MAPPING_ = ::GENERIC_MAPPING;
  33. using OBJECT_INFORMATION_CLASS_ = ::OBJECT_INFORMATION_CLASS;
  34. constexpr static OBJECT_INFORMATION_CLASS_ ObjectTypeInformation = ::OBJECT_INFORMATION_CLASS::ObjectTypeInformation;
  35. typedef struct _OBJECT_TYPE_INFORMATION_ {
  36. UNICODE_STRING TypeName;
  37. ULONG TotalNumberOfObjects;
  38. ULONG TotalNumberOfHandles;
  39. ULONG TotalPagedPoolUsage;
  40. ULONG TotalNonPagedPoolUsage;
  41. ULONG TotalNamePoolUsage;
  42. ULONG TotalHandleTableUsage;
  43. ULONG HighWaterNumberOfObjects;
  44. ULONG HighWaterNumberOfHandles;
  45. ULONG HighWaterPagedPoolUsage;
  46. ULONG HighWaterNonPagedPoolUsage;
  47. ULONG HighWaterNamePoolUsage;
  48. ULONG HighWaterHandleTableUsage;
  49. ULONG InvalidAttributes;
  50. GENERIC_MAPPING GenericMapping;
  51. ULONG ValidAccessMask;
  52. BOOLEAN SecurityRequired;
  53. BOOLEAN MaintainHandleCount;
  54. UCHAR TypeIndex;
  55. CHAR ReservedByte;
  56. ULONG PoolType;
  57. ULONG DefaultPagedPoolCharge;
  58. ULONG DefaultNonPagedPoolCharge;
  59. } OBJECT_TYPE_INFORMATION_, *POBJECT_TYPE_INFORMATION_;
  60. #else
  61. typedef enum _OBJECT_INFORMATION_CLASS_
  62. {
  63. ObjectBasicInformation,
  64. ObjectNameInformation,
  65. ObjectTypeInformation,
  66. ObjectAllInformation,
  67. ObjectDataInformation
  68. } OBJECT_INFORMATION_CLASS_, *POBJECT_INFORMATION_CLASS_;
  69. typedef struct _UNICODE_STRING_ {
  70. ::boost::winapi::USHORT_ Length;
  71. ::boost::winapi::USHORT_ MaximumLength;
  72. ::boost::winapi::LPWSTR_ Buffer;
  73. } UNICODE_STRING_, *PUNICODE_STRING_;
  74. typedef struct _GENERIC_MAPPING_ {
  75. ::boost::winapi::ACCESS_MASK_ GenericRead;
  76. ::boost::winapi::ACCESS_MASK_ GenericWrite;
  77. ::boost::winapi::ACCESS_MASK_ GenericExecute;
  78. ::boost::winapi::ACCESS_MASK_ GenericAll;
  79. } GENERIC_MAPPING_;
  80. #endif
  81. typedef struct _OBJECT_BASIC_INFORMATION {
  82. ::boost::winapi::ULONG_ Attributes;
  83. ::boost::winapi::ACCESS_MASK_ GrantedAccess;
  84. ::boost::winapi::ULONG_ HandleCount;
  85. ::boost::winapi::ULONG_ PointerCount;
  86. ::boost::winapi::ULONG_ PagedPoolUsage;
  87. ::boost::winapi::ULONG_ NonPagedPoolUsage;
  88. ::boost::winapi::ULONG_ Reserved[3];
  89. ::boost::winapi::ULONG_ NameInformationLength;
  90. ::boost::winapi::ULONG_ TypeInformationLength;
  91. ::boost::winapi::ULONG_ SecurityDescriptorLength;
  92. ::boost::winapi::LARGE_INTEGER_ CreateTime;
  93. } OBJECT_BASIC_INFORMATION_, *POBJECT_BASIC_INFORMATION_;
  94. typedef struct _OBJECT_NAME_INFORMATION {
  95. UNICODE_STRING_ Name;
  96. } OBJECT_NAME_INFORMATION_, *POBJECT_NAME_INFORMATION_;
  97. #if defined( BOOST_USE_WINDOWS_H )
  98. extern "C"
  99. {
  100. using SYSTEM_INFORMATION_CLASS_ = ::SYSTEM_INFORMATION_CLASS;
  101. constexpr static SYSTEM_INFORMATION_CLASS_ SystemHandleInformation_ = static_cast<SYSTEM_INFORMATION_CLASS_>(16);
  102. inline ::boost::winapi::NTSTATUS_ nt_system_query_information(
  103. SYSTEM_INFORMATION_CLASS SystemInformationClass,
  104. void * SystemInformation,
  105. ::boost::winapi::ULONG_ SystemInformationLength,
  106. ::boost::winapi::PULONG_ ReturnLength)
  107. {
  108. return ::NtQuerySystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
  109. }
  110. inline ::boost::winapi::NTSTATUS_ nt_query_object(
  111. ::boost::winapi::HANDLE_ Handle,
  112. OBJECT_INFORMATION_CLASS_ ObjectInformationClass,
  113. ::boost::winapi::PVOID_ ObjectInformation,
  114. ::boost::winapi::ULONG_ ObjectInformationLength,
  115. ::boost::winapi::PULONG_ ReturnLength
  116. )
  117. {
  118. return ::NtQueryObject(Handle, ObjectInformationClass, ObjectInformation, ObjectInformationLength, ReturnLength);
  119. }
  120. }
  121. #else
  122. //this import workaround is to keep it a header-only library. and enums cannot be imported from the winapi.
  123. extern "C"
  124. {
  125. typedef enum _SYSTEM_INFORMATION_CLASS_
  126. {
  127. SystemBasicInformation_ = 0,
  128. SystemProcessorInformation_ = 1,
  129. SystemPerformanceInformation_ = 2,
  130. SystemTimeOfDayInformation_ = 3,
  131. SystemProcessInformation_ = 5,
  132. SystemProcessorPerformanceInformation_ = 8,
  133. SystemHandleInformation_ = 16,
  134. SystemPagefileInformation_ = 18,
  135. SystemInterruptInformation_ = 23,
  136. SystemExceptionInformation_ = 33,
  137. SystemRegistryQuotaInformation_ = 37,
  138. SystemLookasideInformation_ = 45
  139. } SYSTEM_INFORMATION_CLASS_;
  140. typedef struct _OBJECT_TYPE_INFORMATION_ {
  141. UNICODE_STRING_ TypeName;
  142. ::boost::winapi::ULONG_ TotalNumberOfObjects;
  143. ::boost::winapi::ULONG_ TotalNumberOfHandles;
  144. ::boost::winapi::ULONG_ TotalPagedPoolUsage;
  145. ::boost::winapi::ULONG_ TotalNonPagedPoolUsage;
  146. ::boost::winapi::ULONG_ TotalNamePoolUsage;
  147. ::boost::winapi::ULONG_ TotalHandleTableUsage;
  148. ::boost::winapi::ULONG_ HighWaterNumberOfObjects;
  149. ::boost::winapi::ULONG_ HighWaterNumberOfHandles;
  150. ::boost::winapi::ULONG_ HighWaterPagedPoolUsage;
  151. ::boost::winapi::ULONG_ HighWaterNonPagedPoolUsage;
  152. ::boost::winapi::ULONG_ HighWaterNamePoolUsage;
  153. ::boost::winapi::ULONG_ HighWaterHandleTableUsage;
  154. ::boost::winapi::ULONG_ InvalidAttributes;
  155. GENERIC_MAPPING_ GenericMapping;
  156. ::boost::winapi::ULONG_ ValidAccessMask;
  157. ::boost::winapi::BOOLEAN_ SecurityRequired;
  158. ::boost::winapi::BOOLEAN_ MaintainHandleCount;
  159. ::boost::winapi::UCHAR_ TypeIndex;
  160. ::boost::winapi::CHAR_ ReservedByte;
  161. ::boost::winapi::ULONG_ PoolType;
  162. ::boost::winapi::ULONG_ DefaultPagedPoolCharge;
  163. ::boost::winapi::ULONG_ DefaultNonPagedPoolCharge;
  164. } OBJECT_TYPE_INFORMATION_, *POBJECT_TYPE_INFORMATION_;
  165. /*
  166. __kernel_entry NTSTATUS NtQuerySystemInformation(
  167. IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
  168. OUT PVOID SystemInformation,
  169. IN ULONG SystemInformationLength,
  170. OUT PULONG ReturnLength
  171. );
  172. */
  173. typedef ::boost::winapi::NTSTATUS_ (__kernel_entry *nt_system_query_information_p )(
  174. SYSTEM_INFORMATION_CLASS_,
  175. void *,
  176. ::boost::winapi::ULONG_,
  177. ::boost::winapi::PULONG_);
  178. /*
  179. __kernel_entry NTSYSCALLAPI NTSTATUS NtQueryObject(
  180. HANDLE Handle,
  181. OBJECT_INFORMATION_CLASS ObjectInformationClass,
  182. PVOID ObjectInformation,
  183. ULONG ObjectInformationLength,
  184. PULONG ReturnLength
  185. );
  186. */
  187. typedef ::boost::winapi::NTSTATUS_ (__kernel_entry *nt_query_object_p )(
  188. ::boost::winapi::HANDLE_,
  189. OBJECT_INFORMATION_CLASS_,
  190. void *,
  191. ::boost::winapi::ULONG_,
  192. ::boost::winapi::PULONG_);
  193. }
  194. inline ::boost::winapi::NTSTATUS_ nt_system_query_information(
  195. SYSTEM_INFORMATION_CLASS_ SystemInformationClass,
  196. void *SystemInformation,
  197. ::boost::winapi::ULONG_ SystemInformationLength,
  198. ::boost::winapi::PULONG_ ReturnLength)
  199. {
  200. static ::boost::winapi::HMODULE_ h = ::boost::winapi::get_module_handle(L"Ntdll.dll");
  201. static nt_system_query_information_p f = reinterpret_cast<nt_system_query_information_p>(::boost::winapi::get_proc_address(h, "NtQuerySystemInformation"));
  202. return (*f)(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
  203. }
  204. inline ::boost::winapi::BOOL_ nt_query_object(
  205. ::boost::winapi::HANDLE_ Handle,
  206. OBJECT_INFORMATION_CLASS_ ObjectInformationClass,
  207. void *ObjectInformation,
  208. ::boost::winapi::ULONG_ ObjectInformationLength,
  209. ::boost::winapi::PULONG_ ReturnLength)
  210. {
  211. static ::boost::winapi::HMODULE_ h = ::boost::winapi::get_module_handle(L"Ntdll.dll");
  212. static nt_query_object_p f = reinterpret_cast<nt_query_object_p>(::boost::winapi::get_proc_address(h, "NtQueryObject"));
  213. return (*f)(Handle, ObjectInformationClass, ObjectInformation, ObjectInformationLength, ReturnLength);
  214. }
  215. #endif
  216. }}}}}
  217. #endif /* BOOST_PROCESS_DETAIL_WINDOWS_JOB_WORKAROUND_HPP_ */