handle_workaround.hpp 8.7 KB

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