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