Apache2
apr_arch_misc.h
Go to the documentation of this file.
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 
17 #ifndef MISC_H
18 #define MISC_H
19 
20 #include "apr.h"
21 #include "apr_portable.h"
22 #include "apr_private.h"
23 #include "apr_general.h"
24 #include "apr_pools.h"
25 #include "apr_getopt.h"
26 #include "apr_thread_proc.h"
27 #include "apr_file_io.h"
28 #include "apr_errno.h"
29 #include "apr_getopt.h"
30 
31 #if APR_HAVE_STDIO_H
32 #include <stdio.h>
33 #endif
34 #if APR_HAVE_SIGNAL_H
35 #include <signal.h>
36 #endif
37 #if APR_HAVE_PTHREAD_H
38 #include <pthread.h>
39 #endif
40 #if APR_HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43 #if APR_HAVE_STRING_H
44 #include <string.h>
45 #endif
46 #include <tlhelp32.h>
47 
48 #if defined(HAVE_IF_INDEXTONAME) && defined(_MSC_VER)
49 #include <Iphlpapi.h>
50 #endif
51 
52 struct apr_other_child_rec_t {
53  apr_pool_t *p;
56  void (*maintenance) (int, void *, int);
57  void *data;
58 };
59 
60 #define WSAHighByte 2
61 #define WSALowByte 0
62 
63 /* start.c and apr_app.c helpers and communication within misc.c
64  *
65  * They are not for public consumption, although apr_app_init_complete
66  * must be an exported symbol to avoid reinitialization.
67  */
69 
70 int apr_wastrtoastr(char const * const * *retarr,
71  wchar_t const * const *arr, int args);
72 
73 /* Platform specific designation of run time os version.
74  * Gaps allow for specific service pack levels that
75  * export new kernel or winsock functions or behavior.
76  */
77 typedef enum {
80  APR_WIN_95 = 10,
83  APR_WIN_98 = 14,
85  APR_WIN_ME = 18,
86 
87  APR_WIN_UNICODE = 20, /* Prior versions support only narrow chars */
88 
89  APR_WIN_CE_3 = 23, /* CE is an odd beast, not supporting */
90  /* some pre-NT features, such as the */
91  APR_WIN_NT = 30, /* narrow charset APIs (fooA fns), while */
92  APR_WIN_NT_3_5 = 35, /* not supporting some NT-family features. */
94 
101 
110  APR_WIN_7 = 90,
112  APR_WIN_8 = 100,
113  APR_WIN_8_1 = 110,
114  APR_WIN_10 = 120
116 
118 
120 
121 #if defined(_MSC_VER)
122 #include "crtdbg.h"
123 
124 static APR_INLINE void* apr_malloc_dbg(size_t size, const char* filename,
125  int linenumber)
126 {
127  return _malloc_dbg(size, _CRT_BLOCK, filename, linenumber);
128 }
129 
130 static APR_INLINE void* apr_realloc_dbg(void* userData, size_t newSize,
131  const char* filename, int linenumber)
132 {
133  return _realloc_dbg(userData, newSize, _CRT_BLOCK, filename, linenumber);
134 }
135 
136 #else
137 
138 static APR_INLINE void* apr_malloc_dbg(size_t size, const char* filename,
139  int linenumber)
140 {
141  return malloc(size);
142 }
143 
144 static APR_INLINE void* apr_realloc_dbg(void* userData, size_t newSize,
145  const char* filename, int linenumber)
146 {
147  return realloc(userData, newSize);
148 }
149 
150 #endif /* ! _MSC_VER */
151 
152 /* Wrapper around WaitForSingleObject() that accepts apr_interval_time_t
153  * in microseconds instead of milliseconds. Values < 0 mean wait
154  * forever, 0 means do not wait at all. */
155 DWORD apr_wait_for_single_object(HANDLE handle, apr_interval_time_t timeout);
156 
157 typedef enum {
158  DLL_WINBASEAPI = 0, /* kernel32 From WinBase.h */
159  DLL_WINADVAPI = 1, /* advapi32 From WinBase.h */
160  DLL_WINSOCKAPI = 2, /* mswsock From WinSock.h */
161  DLL_WINSOCK2API = 3, /* ws2_32 From WinSock2.h */
162  DLL_SHSTDAPI = 4, /* shell32 From ShellAPI.h */
163  DLL_NTDLL = 5, /* ntdll From our real kernel */
164  DLL_IPHLPAPI = 6, /* Iphlpapi From Iphlpapi.h */
166  /* api-ms-win-downlevel-shell32-l1-1-0.dll,
167  fallback to shell32.dll */
168  DLL_defined = 8 /* must define as last idx_ + 1 */
170 
171 FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char *fnName, int ordinal);
172 
173 /* The apr_load_dll_func call WILL return 0 set error to
174  * ERROR_INVALID_FUNCTION if the function cannot be loaded
175  */
176 #define APR_DECLARE_LATE_DLL_FUNC(lib, rettype, calltype, fn, ord, args, names) \
177  typedef rettype (calltype *apr_winapi_fpt_##fn) args; \
178  static volatile apr_winapi_fpt_##fn apr_winapi_pfn_##fn = (apr_winapi_fpt_##fn) (ULONG_PTR) (-1); \
179  static APR_INLINE int apr_winapi_ld_##fn(void) \
180  { \
181  apr_winapi_fpt_##fn cached_func = apr_winapi_pfn_##fn; \
182  if (cached_func == (apr_winapi_fpt_##fn) (ULONG_PTR) (-1)) { \
183  cached_func = (apr_winapi_fpt_##fn) apr_load_dll_func(lib, #fn, ord); \
184  /* Pointer-sized writes are atomic on Windows. */ \
185  apr_winapi_pfn_##fn = cached_func; \
186  } \
187  if (cached_func) return 1; else return 0; }; \
188  static APR_INLINE rettype apr_winapi_##fn args \
189  { if (apr_winapi_ld_##fn()) \
190  return (*(apr_winapi_pfn_##fn)) names; \
191  else { SetLastError(ERROR_INVALID_FUNCTION); return 0;} }; \
192 
193 #define APR_HAVE_LATE_DLL_FUNC(fn) apr_winapi_ld_##fn()
194 
195 /* Provide late bound declarations of every API function missing from
196  * one or more supported releases of the Win32 API
197  *
198  * lib is the enumerated token from apr_dlltoken_e, and must correspond
199  * to the string table entry in start.c used by the apr_load_dll_func().
200  * Token names (attempt to) follow Windows.h declarations prefixed by DLL_
201  * in order to facilitate comparison. Use the exact declaration syntax
202  * and names from Windows.h to prevent ambigutity and bugs.
203  *
204  * rettype and calltype follow the original declaration in Windows.h
205  * fn is the true function name - beware Ansi/Unicode #defined macros
206  * ord is the ordinal within the library, use 0 if it varies between versions
207  * args is the parameter list following the original declaration, in parens
208  * names is the parameter list sans data types, enclosed in parens
209  *
210  * #undef/re#define the Ansi/Unicode generic name to abate confusion
211  * In the case of non-text functions, simply #define the original name
212  */
213 
214 /* This group is NOT available to all versions of WinNT,
215  * these we must always look up
216  */
217 
218 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtQueryTimerResolution, 0, (
219  ULONG *pMaxRes, /* Minimum NS Resolution */
220  ULONG *pMinRes, /* Maximum NS Resolution */
221  ULONG *pCurRes), /* Current NS Resolution */
222  (pMaxRes, pMinRes, pCurRes));
223 #define QueryTimerResolution apr_winapi_NtQueryTimerResolution
224 
225 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtSetTimerResolution, 0, (
226  ULONG ReqRes, /* Requested NS Clock Resolution */
227  BOOL Acquire, /* Aquire (1) or Release (0) our interest */
228  ULONG *pNewRes), /* The NS Clock Resolution granted */
229  (ReqRes, Acquire, pNewRes));
230 #define SetTimerResolution apr_winapi_NtSetTimerResolution
231 
232 /* https://docs.microsoft.com/en-us/windows/desktop/api/winternl/nf-winternl-ntwaitforsingleobject */
233 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtWaitForSingleObject, 0, (
234  HANDLE Handle, /* The handle to the wait object. */
235  BOOLEAN Alertable, /* Specifies whether an alert can be delivered when
236  the object is waiting. */
237  PLARGE_INTEGER Timeout),/* A pointer to an absolute or relative time over
238  which the wait is to occur. */
239  (Handle, Alertable, Timeout));
240 
241 #ifdef CreateToolhelp32Snapshot
242 #undef CreateToolhelp32Snapshot
243 #endif
245  DWORD dwFlags,
246  DWORD th32ProcessID),
247  (dwFlags, th32ProcessID));
248 #define CreateToolhelp32Snapshot apr_winapi_CreateToolhelp32Snapshot
249 
250 #ifdef Process32FirstW
251 #undef Process32FirstW
252 #endif
254  HANDLE hSnapshot,
255  LPPROCESSENTRY32W lppe),
256  (hSnapshot, lppe));
257 #define Process32FirstW apr_winapi_Process32FirstW
258 
259 #ifdef Process32NextW
260 #undef Process32NextW
261 #endif
263  HANDLE hSnapshot,
264  LPPROCESSENTRY32W lppe),
265  (hSnapshot, lppe));
266 #define Process32NextW apr_winapi_Process32NextW
267 
268 #define HAVE_POLL 1
269 
270 #if HAVE_IF_NAMETOINDEX
271 #ifdef if_nametoindex
272 #undef if_nametoindex
273 #endif
274 APR_DECLARE_LATE_DLL_FUNC(DLL_IPHLPAPI, NET_IFINDEX, WINAPI, if_nametoindex, 0, (
275  IN PCSTR InterfaceName),
276  (InterfaceName));
277 #define if_nametoindex apr_winapi_if_nametoindex
278 #endif
279 
280 #if HAVE_IF_INDEXTONAME
281 #ifdef if_indextoname
282 #undef if_indextoname
283 #endif
284 APR_DECLARE_LATE_DLL_FUNC(DLL_IPHLPAPI, PCHAR, NETIOAPI_API_, if_indextoname, 0, (
285  NET_IFINDEX InterfaceIndex,
286  PCHAR InterfaceName),
287  (InterfaceIndex, InterfaceName));
288 #define if_indextoname apr_winapi_if_indextoname
289 #endif
290 
292  STDAPICALLTYPE, CommandLineToArgvW, 0,
293  (LPCWSTR lpCmdLine, int *pNumArgs),
294  (lpCmdLine, pNumArgs));
295 
296 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, GetThreadDescription, 0, (
297  HANDLE hThread,
298  PWSTR *ppszThreadDescription),
299  (hThread, ppszThreadDescription));
300 
301 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, SetThreadDescription, 0, (
302  HANDLE hThread,
303  PCWSTR lpThreadDescription),
304  (hThread, lpThreadDescription));
305 
306 #endif /* ! MISC_H */
APR Platform Definitions.
APR Error Codes.
APR File I/O Handling.
APR Miscellaneous library routines.
APR Command Arguments (getopt)
APR memory allocation.
APR Portability Routines.
APR Thread and Process Library.
dav_buffer apr_size_t size
Definition: mod_dav.h:461
int
Definition: mod_proxy.h:674
int apr_status_t
Definition: apr_errno.h:44
#define APR_INLINE
Definition: apr.h:65
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
apr_int64_t apr_interval_time_t
Definition: apr_time.h:55
#define APR_DECLARE_DATA
Definition: macros.h:17
#define BOOL
Definition: macros.h:25
#define malloc(x)
Definition: apr_private.h:170
Definition: apr_arch_misc.h:52
void * data
Definition: apr_arch_misc.h:57
void(* maintenance)(int, void *, int)
Definition: apr_arch_misc.h:56
apr_pool_t * p
Definition: apr_arch_misc.h:53
apr_proc_t * proc
Definition: apr_arch_misc.h:55
struct apr_other_child_rec_t * next
Definition: apr_arch_misc.h:54
Definition: apr_thread_proc.h:134
#define APR_DECLARE_LATE_DLL_FUNC(lib, rettype, calltype, fn, ord, args, names)
Definition: apr_arch_misc.h:176
apr_dlltoken_e
Definition: apr_arch_misc.h:157
@ DLL_NTDLL
Definition: apr_arch_misc.h:163
@ DLL_WINADVAPI
Definition: apr_arch_misc.h:159
@ DLL_WINSOCK2API
Definition: apr_arch_misc.h:161
@ DLL_WINSOCKAPI
Definition: apr_arch_misc.h:160
@ DLL_SHSTDAPI
Definition: apr_arch_misc.h:162
@ DLL_defined
Definition: apr_arch_misc.h:168
@ DLL_API_MS_WIN_DOWNLEVEL_SHELL32_L1_1_0
Definition: apr_arch_misc.h:165
@ DLL_IPHLPAPI
Definition: apr_arch_misc.h:164
@ DLL_WINBASEAPI
Definition: apr_arch_misc.h:158
APR_DECLARE_DATA apr_oslevel_e apr_os_level
apr_oslevel_e
Definition: apr_arch_misc.h:77
@ APR_WIN_ME
Definition: apr_arch_misc.h:85
@ APR_WIN_NT_4_SP4
Definition: apr_arch_misc.h:98
@ APR_WIN_95
Definition: apr_arch_misc.h:80
@ APR_WIN_NT
Definition: apr_arch_misc.h:91
@ APR_WIN_8
Definition: apr_arch_misc.h:112
@ APR_WIN_VISTA
Definition: apr_arch_misc.h:109
@ APR_WIN_2000_SP2
Definition: apr_arch_misc.h:104
@ APR_WIN_95_OSR2
Definition: apr_arch_misc.h:82
@ APR_WIN_8_1
Definition: apr_arch_misc.h:113
@ APR_WIN_NT_4_SP5
Definition: apr_arch_misc.h:99
@ APR_WIN_98
Definition: apr_arch_misc.h:83
@ APR_WIN_NT_4
Definition: apr_arch_misc.h:95
@ APR_WIN_CE_3
Definition: apr_arch_misc.h:89
@ APR_WIN_NT_3_5
Definition: apr_arch_misc.h:92
@ APR_WIN_10
Definition: apr_arch_misc.h:114
@ APR_WIN_7_SP1
Definition: apr_arch_misc.h:111
@ APR_WIN_NT_4_SP2
Definition: apr_arch_misc.h:96
@ APR_WIN_NT_4_SP3
Definition: apr_arch_misc.h:97
@ APR_WIN_2003
Definition: apr_arch_misc.h:108
@ APR_WIN_98_SE
Definition: apr_arch_misc.h:84
@ APR_WIN_2000_SP1
Definition: apr_arch_misc.h:103
@ APR_WIN_7
Definition: apr_arch_misc.h:110
@ APR_WIN_UNICODE
Definition: apr_arch_misc.h:87
@ APR_WIN_UNK
Definition: apr_arch_misc.h:78
@ APR_WIN_XP_SP1
Definition: apr_arch_misc.h:106
@ APR_WIN_2000
Definition: apr_arch_misc.h:102
@ APR_WIN_NT_4_SP6
Definition: apr_arch_misc.h:100
@ APR_WIN_UNSUP
Definition: apr_arch_misc.h:79
@ APR_WIN_95_B
Definition: apr_arch_misc.h:81
@ APR_WIN_XP
Definition: apr_arch_misc.h:105
@ APR_WIN_NT_3_51
Definition: apr_arch_misc.h:93
@ APR_WIN_XP_SP2
Definition: apr_arch_misc.h:107
#define CreateToolhelp32Snapshot
Definition: apr_arch_misc.h:248
DWORD apr_wait_for_single_object(HANDLE handle, apr_interval_time_t timeout)
#define Process32NextW
Definition: apr_arch_misc.h:266
FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char *fnName, int ordinal)
apr_status_t apr_get_oslevel(apr_oslevel_e *)
#define Process32FirstW
Definition: apr_arch_misc.h:257
int APR_DECLARE_DATA apr_app_init_complete
int apr_wastrtoastr(char const *const **retarr, wchar_t const *const *arr, int args)