fspr_arch_misc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MISC_H
  17. #define MISC_H
  18. #include "fspr.h"
  19. #include "fspr_portable.h"
  20. #include "fspr_private.h"
  21. #include "fspr_general.h"
  22. #include "fspr_pools.h"
  23. #include "fspr_getopt.h"
  24. #include "fspr_thread_proc.h"
  25. #include "fspr_file_io.h"
  26. #include "fspr_errno.h"
  27. #include "fspr_getopt.h"
  28. #if APR_HAVE_STDIO_H
  29. #include <stdio.h>
  30. #endif
  31. #if APR_HAVE_SIGNAL_H
  32. #include <signal.h>
  33. #endif
  34. #if APR_HAVE_PTHREAD_H
  35. #include <pthread.h>
  36. #endif
  37. /* ### create APR_HAVE_* macros for these? */
  38. #if APR_HAVE_STDLIB_H
  39. #include <stdlib.h>
  40. #endif
  41. #if APR_HAVE_STRING_H
  42. #include <string.h>
  43. #endif
  44. struct fspr_other_child_rec_t {
  45. fspr_pool_t *p;
  46. struct fspr_other_child_rec_t *next;
  47. fspr_proc_t *proc;
  48. void (*maintenance) (int, void *, int);
  49. void *data;
  50. fspr_os_file_t write_fd;
  51. };
  52. #define WSAHighByte 2
  53. #define WSALowByte 0
  54. /* start.c and fspr_app.c helpers and communication within misc.c
  55. *
  56. * They are not for public consumption, although fspr_app_init_complete
  57. * must be an exported symbol to avoid reinitialization.
  58. */
  59. extern int APR_DECLARE_DATA fspr_app_init_complete;
  60. int fspr_wastrtoastr(char const * const * *retarr,
  61. wchar_t const * const *arr, int args);
  62. /* Platform specific designation of run time os version.
  63. * Gaps allow for specific service pack levels that
  64. * export new kernel or winsock functions or behavior.
  65. */
  66. typedef enum {
  67. APR_WIN_UNK = 0,
  68. APR_WIN_UNSUP = 1,
  69. APR_WIN_95 = 10,
  70. APR_WIN_95_B = 11,
  71. APR_WIN_95_OSR2 = 12,
  72. APR_WIN_98 = 14,
  73. APR_WIN_98_SE = 16,
  74. APR_WIN_ME = 18,
  75. APR_WIN_UNICODE = 20, /* Prior versions support only narrow chars */
  76. APR_WIN_CE_3 = 23, /* CE is an odd beast, not supporting */
  77. /* some pre-NT features, such as the */
  78. APR_WIN_NT = 30, /* narrow charset APIs (fooA fns), while */
  79. APR_WIN_NT_3_5 = 35, /* not supporting some NT-family features. */
  80. APR_WIN_NT_3_51 = 36,
  81. APR_WIN_NT_4 = 40,
  82. APR_WIN_NT_4_SP2 = 42,
  83. APR_WIN_NT_4_SP3 = 43,
  84. APR_WIN_NT_4_SP4 = 44,
  85. APR_WIN_NT_4_SP5 = 45,
  86. APR_WIN_NT_4_SP6 = 46,
  87. APR_WIN_2000 = 50,
  88. APR_WIN_2000_SP1 = 51,
  89. APR_WIN_2000_SP2 = 52,
  90. APR_WIN_XP = 60,
  91. APR_WIN_XP_SP1 = 61,
  92. APR_WIN_XP_SP2 = 62,
  93. APR_WIN_2003 = 70
  94. } fspr_oslevel_e;
  95. extern APR_DECLARE_DATA fspr_oslevel_e fspr_os_level;
  96. fspr_status_t fspr_get_oslevel(fspr_oslevel_e *);
  97. /* The APR_HAS_ANSI_FS symbol is PRIVATE, and internal to APR.
  98. * APR only supports char data for filenames. Like most applications,
  99. * characters >127 are essentially undefined. APR_HAS_UNICODE_FS lets
  100. * the application know that utf-8 is the encoding method of APR, and
  101. * only incidently hints that we have Wide OS calls.
  102. *
  103. * APR_HAS_ANSI_FS is simply an OS flag to tell us all calls must be
  104. * the unicode eqivilant.
  105. */
  106. #if defined(_WIN32_WCE) || defined(WINNT)
  107. #define APR_HAS_ANSI_FS 0
  108. #else
  109. #define APR_HAS_ANSI_FS 1
  110. #endif
  111. /* IF_WIN_OS_IS_UNICODE / ELSE_WIN_OS_IS_ANSI help us keep the code trivial
  112. * where have runtime tests for unicode-ness, that aren't needed in any
  113. * build which supports only WINNT or WCE.
  114. */
  115. #if APR_HAS_ANSI_FS && APR_HAS_UNICODE_FS
  116. #define IF_WIN_OS_IS_UNICODE if (fspr_os_level >= APR_WIN_UNICODE)
  117. #define ELSE_WIN_OS_IS_ANSI else
  118. #else /* APR_HAS_UNICODE_FS */
  119. #define IF_WIN_OS_IS_UNICODE
  120. #define ELSE_WIN_OS_IS_ANSI
  121. #endif /* WINNT */
  122. typedef enum {
  123. DLL_WINBASEAPI = 0, // kernel32 From WinBase.h
  124. DLL_WINADVAPI = 1, // advapi32 From WinBase.h
  125. DLL_WINSOCKAPI = 2, // mswsock From WinSock.h
  126. DLL_WINSOCK2API = 3, // ws2_32 From WinSock2.h
  127. DLL_SHSTDAPI = 4, // shell32 From ShellAPI.h
  128. DLL_NTDLL = 5, // shell32 From our real kernel
  129. DLL_defined = 6 // must define as last idx_ + 1
  130. } fspr_dlltoken_e;
  131. FARPROC fspr_load_dll_func(fspr_dlltoken_e fnLib, char *fnName, int ordinal);
  132. /* The fspr_load_dll_func call WILL fault if the function cannot be loaded */
  133. #define APR_DECLARE_LATE_DLL_FUNC(lib, rettype, calltype, fn, ord, args, names) \
  134. typedef rettype (calltype *fspr_winapi_fpt_##fn) args; \
  135. static fspr_winapi_fpt_##fn fspr_winapi_pfn_##fn = NULL; \
  136. __inline rettype fspr_winapi_##fn args \
  137. { if (!fspr_winapi_pfn_##fn) \
  138. fspr_winapi_pfn_##fn = (fspr_winapi_fpt_##fn) \
  139. fspr_load_dll_func(lib, #fn, ord); \
  140. return (*(fspr_winapi_pfn_##fn)) names; }; \
  141. /* Provide late bound declarations of every API function missing from
  142. * one or more supported releases of the Win32 API
  143. *
  144. * lib is the enumerated token from fspr_dlltoken_e, and must correspond
  145. * to the string table entry in start.c used by the fspr_load_dll_func().
  146. * Token names (attempt to) follow Windows.h declarations prefixed by DLL_
  147. * in order to facilitate comparison. Use the exact declaration syntax
  148. * and names from Windows.h to prevent ambigutity and bugs.
  149. *
  150. * rettype and calltype follow the original declaration in Windows.h
  151. * fn is the true function name - beware Ansi/Unicode #defined macros
  152. * ord is the ordinal within the library, use 0 if it varies between versions
  153. * args is the parameter list following the original declaration, in parens
  154. * names is the parameter list sans data types, enclosed in parens
  155. *
  156. * #undef/re#define the Ansi/Unicode generic name to abate confusion
  157. * In the case of non-text functions, simply #define the original name
  158. */
  159. #if !defined(_WIN32_WCE) && !defined(WINNT)
  160. #ifdef GetFileAttributesExA
  161. #undef GetFileAttributesExA
  162. #endif
  163. APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, GetFileAttributesExA, 0, (
  164. IN LPCSTR lpFileName,
  165. IN GET_FILEEX_INFO_LEVELS fInfoLevelId,
  166. OUT LPVOID lpFileInformation),
  167. (lpFileName, fInfoLevelId, lpFileInformation));
  168. #define GetFileAttributesExA fspr_winapi_GetFileAttributesExA
  169. #undef GetFileAttributesEx
  170. #define GetFileAttributesEx fspr_winapi_GetFileAttributesExA
  171. #ifdef GetFileAttributesExW
  172. #undef GetFileAttributesExW
  173. #endif
  174. APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, GetFileAttributesExW, 0, (
  175. IN LPCWSTR lpFileName,
  176. IN GET_FILEEX_INFO_LEVELS fInfoLevelId,
  177. OUT LPVOID lpFileInformation),
  178. (lpFileName, fInfoLevelId, lpFileInformation));
  179. #define GetFileAttributesExW fspr_winapi_GetFileAttributesExW
  180. APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, CancelIo, 0, (
  181. IN HANDLE hFile),
  182. (hFile));
  183. #define CancelIo fspr_winapi_CancelIo
  184. APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, TryEnterCriticalSection, 0, (
  185. LPCRITICAL_SECTION lpCriticalSection),
  186. (lpCriticalSection));
  187. #define TryEnterCriticalSection fspr_winapi_TryEnterCriticalSection
  188. APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, SwitchToThread, 0, (
  189. void),
  190. ());
  191. #define SwitchToThread fspr_winapi_SwitchToThread
  192. APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetEffectiveRightsFromAclW, 0, (
  193. IN PACL pacl,
  194. IN PTRUSTEE_W pTrustee,
  195. OUT PACCESS_MASK pAccessRights),
  196. (pacl, pTrustee, pAccessRights));
  197. #define GetEffectiveRightsFromAclW fspr_winapi_GetEffectiveRightsFromAclW
  198. APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetNamedSecurityInfoW, 0, (
  199. IN LPWSTR pObjectName,
  200. IN SE_OBJECT_TYPE ObjectType,
  201. IN SECURITY_INFORMATION SecurityInfo,
  202. OUT PSID *ppsidOwner,
  203. OUT PSID *ppsidGroup,
  204. OUT PACL *ppDacl,
  205. OUT PACL *ppSacl,
  206. OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
  207. (pObjectName, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
  208. ppDacl, ppSacl, ppSecurityDescriptor));
  209. #define GetNamedSecurityInfoW fspr_winapi_GetNamedSecurityInfoW
  210. APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetNamedSecurityInfoA, 0, (
  211. IN LPSTR pObjectName,
  212. IN SE_OBJECT_TYPE ObjectType,
  213. IN SECURITY_INFORMATION SecurityInfo,
  214. OUT PSID *ppsidOwner,
  215. OUT PSID *ppsidGroup,
  216. OUT PACL *ppDacl,
  217. OUT PACL *ppSacl,
  218. OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
  219. (pObjectName, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
  220. ppDacl, ppSacl, ppSecurityDescriptor));
  221. #define GetNamedSecurityInfoA fspr_winapi_GetNamedSecurityInfoA
  222. #undef GetNamedSecurityInfo
  223. #define GetNamedSecurityInfo fspr_winapi_GetNamedSecurityInfoA
  224. APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetSecurityInfo, 0, (
  225. IN HANDLE handle,
  226. IN SE_OBJECT_TYPE ObjectType,
  227. IN SECURITY_INFORMATION SecurityInfo,
  228. OUT PSID *ppsidOwner,
  229. OUT PSID *ppsidGroup,
  230. OUT PACL *ppDacl,
  231. OUT PACL *ppSacl,
  232. OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
  233. (handle, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
  234. ppDacl, ppSacl, ppSecurityDescriptor));
  235. #define GetSecurityInfo fspr_winapi_GetSecurityInfo
  236. APR_DECLARE_LATE_DLL_FUNC(DLL_SHSTDAPI, LPWSTR *, WINAPI, CommandLineToArgvW, 0, (
  237. LPCWSTR lpCmdLine,
  238. int *pNumArgs),
  239. (lpCmdLine, pNumArgs));
  240. #define CommandLineToArgvW fspr_winapi_CommandLineToArgvW
  241. #endif /* !defined(_WIN32_WCE) && !defined(WINNT) */
  242. #if !defined(_WIN32_WCE)
  243. APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, DWORD, WINAPI, NtQueryTimerResolution, 0, (
  244. ULONG *pMaxRes, /* Minimum NS Resolution */
  245. ULONG *pMinRes, /* Maximum NS Resolution */
  246. ULONG *pCurRes), /* Current NS Resolution */
  247. (pMaxRes, pMinRes, pCurRes));
  248. #define QueryTimerResolution fspr_winapi_NtQueryTimerResolution
  249. APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, DWORD, WINAPI, NtSetTimerResolution, 0, (
  250. ULONG ReqRes, /* Requested NS Clock Resolution */
  251. BOOL Acquire, /* Aquire (1) or Release (0) our interest */
  252. ULONG *pNewRes), /* The NS Clock Resolution granted */
  253. (ReqRes, Acquire, pNewRes));
  254. #define SetTimerResolution fspr_winapi_NtSetTimerResolution
  255. /* ### These are ULONG_PTR values, but that's int32 for all we care
  256. * until the Win64 port is prepared.
  257. */
  258. typedef struct PBI {
  259. DWORD ExitStatus;
  260. PVOID PebBaseAddress;
  261. ULONG AffinityMask;
  262. LONG BasePriority;
  263. ULONG UniqueProcessId;
  264. ULONG InheritedFromUniqueProcessId;
  265. } PBI, *PPBI;
  266. APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, DWORD, WINAPI, NtQueryInformationProcess, 0, (
  267. HANDLE hProcess, /* Obvious */
  268. INT info, /* Use 0 for PBI documented above */
  269. PVOID pPI, /* The PIB buffer */
  270. ULONG LenPI, /* Use sizeof(PBI) */
  271. ULONG *pSizePI), /* returns pPI buffer used (may pass NULL) */
  272. (hProcess, info, pPI, LenPI, pSizePI));
  273. #define QueryInformationProcess fspr_winapi_NtQueryInformationProcess
  274. APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, DWORD, WINAPI, NtQueryObject, 0, (
  275. HANDLE hObject, /* Obvious */
  276. INT info, /* Use 0 for PBI documented above */
  277. PVOID pOI, /* The PIB buffer */
  278. ULONG LenOI, /* Use sizeof(PBI) */
  279. ULONG *pSizeOI), /* returns pPI buffer used (may pass NULL) */
  280. (hObject, info, pOI, LenOI, pSizeOI));
  281. #define QueryObject fspr_winapi_NtQueryObject
  282. #endif /* !defined(_WIN32_WCE) */
  283. #endif /* ! MISC_H */