xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/include/arch/win32/apr_arch_misc.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 struct apr_other_child_rec_t {
51     apr_pool_t *p;
52     struct apr_other_child_rec_t *next;
53     apr_proc_t *proc;
54     void (*maintenance) (int, void *, int);
55     void *data;
56     apr_os_file_t write_fd;
57 };
58 
59 #define WSAHighByte 2
60 #define WSALowByte 0
61 
62 /* start.c and apr_app.c helpers and communication within misc.c
63  *
64  * They are not for public consumption, although apr_app_init_complete
65  * must be an exported symbol to avoid reinitialization.
66  */
67 extern int APR_DECLARE_DATA apr_app_init_complete;
68 
69 int apr_wastrtoastr(char const * const * *retarr,
70                     wchar_t const * const *arr, int args);
71 
72 /* Platform specific designation of run time os version.
73  * Gaps allow for specific service pack levels that
74  * export new kernel or winsock functions or behavior.
75  */
76 typedef enum {
77     APR_WIN_UNK =       0,
78     APR_WIN_UNSUP =     1,
79     APR_WIN_95 =       10,
80     APR_WIN_95_B =     11,
81     APR_WIN_95_OSR2 =  12,
82     APR_WIN_98 =       14,
83     APR_WIN_98_SE =    16,
84     APR_WIN_ME =       18,
85 
86     APR_WIN_UNICODE =  20, /* Prior versions support only narrow chars */
87 
88     APR_WIN_CE_3 =     23, /* CE is an odd beast, not supporting */
89                            /* some pre-NT features, such as the    */
90     APR_WIN_NT =       30, /* narrow charset APIs (fooA fns), while  */
91     APR_WIN_NT_3_5 =   35, /* not supporting some NT-family features.  */
92     APR_WIN_NT_3_51 =  36,
93 
94     APR_WIN_NT_4 =     40,
95     APR_WIN_NT_4_SP2 = 42,
96     APR_WIN_NT_4_SP3 = 43,
97     APR_WIN_NT_4_SP4 = 44,
98     APR_WIN_NT_4_SP5 = 45,
99     APR_WIN_NT_4_SP6 = 46,
100 
101     APR_WIN_2000 =     50,
102     APR_WIN_2000_SP1 = 51,
103     APR_WIN_2000_SP2 = 52,
104     APR_WIN_XP =       60,
105     APR_WIN_XP_SP1 =   61,
106     APR_WIN_XP_SP2 =   62,
107     APR_WIN_2003 =     70,
108     APR_WIN_VISTA =    80,
109     APR_WIN_7 =        90
110 } apr_oslevel_e;
111 
112 extern APR_DECLARE_DATA apr_oslevel_e apr_os_level;
113 
114 apr_status_t apr_get_oslevel(apr_oslevel_e *);
115 
116 /* The APR_HAS_ANSI_FS symbol is PRIVATE, and internal to APR.
117  * APR only supports char data for filenames.  Like most applications,
118  * characters >127 are essentially undefined.  APR_HAS_UNICODE_FS lets
119  * the application know that utf-8 is the encoding method of APR, and
120  * only incidently hints that we have Wide OS calls.
121  *
122  * APR_HAS_ANSI_FS is simply an OS flag to tell us all calls must be
123  * the unicode eqivilant.
124  */
125 
126 #if defined(_WIN32_WCE) || defined(WINNT)
127 #define APR_HAS_ANSI_FS           0
128 #else
129 #define APR_HAS_ANSI_FS           1
130 #endif
131 
132 /* IF_WIN_OS_IS_UNICODE / ELSE_WIN_OS_IS_ANSI help us keep the code trivial
133  * where have runtime tests for unicode-ness, that aren't needed in any
134  * build which supports only WINNT or WCE.
135  */
136 #if APR_HAS_ANSI_FS && APR_HAS_UNICODE_FS
137 #define IF_WIN_OS_IS_UNICODE if (apr_os_level >= APR_WIN_UNICODE)
138 #define ELSE_WIN_OS_IS_ANSI else
139 #else /* APR_HAS_UNICODE_FS */
140 #define IF_WIN_OS_IS_UNICODE
141 #define ELSE_WIN_OS_IS_ANSI
142 #endif /* WINNT */
143 
144 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
145 #include "crtdbg.h"
146 
apr_malloc_dbg(size_t size,const char * filename,int linenumber)147 static APR_INLINE void* apr_malloc_dbg(size_t size, const char* filename,
148                                        int linenumber)
149 {
150     return _malloc_dbg(size, _CRT_BLOCK, filename, linenumber);
151 }
152 
apr_realloc_dbg(void * userData,size_t newSize,const char * filename,int linenumber)153 static APR_INLINE void* apr_realloc_dbg(void* userData, size_t newSize,
154                                         const char* filename, int linenumber)
155 {
156     return _realloc_dbg(userData, newSize, _CRT_BLOCK, filename, linenumber);
157 }
158 
159 #else
160 
apr_malloc_dbg(size_t size,const char * filename,int linenumber)161 static APR_INLINE void* apr_malloc_dbg(size_t size, const char* filename,
162                                        int linenumber)
163 {
164     return malloc(size);
165 }
166 
apr_realloc_dbg(void * userData,size_t newSize,const char * filename,int linenumber)167 static APR_INLINE void* apr_realloc_dbg(void* userData, size_t newSize,
168                                         const char* filename, int linenumber)
169 {
170     return realloc(userData, newSize);
171 }
172 
173 #endif  /* ! _MSC_VER */
174 
175 typedef enum {
176     DLL_WINBASEAPI = 0,    /* kernel32 From WinBase.h       */
177     DLL_WINADVAPI = 1,     /* advapi32 From WinBase.h       */
178     DLL_WINSOCKAPI = 2,    /* mswsock  From WinSock.h       */
179     DLL_WINSOCK2API = 3,   /* ws2_32   From WinSock2.h      */
180     DLL_SHSTDAPI = 4,      /* shell32  From ShellAPI.h      */
181     DLL_NTDLL = 5,         /* shell32  From our real kernel */
182     DLL_defined = 6        /* must define as last idx_ + 1  */
183 } apr_dlltoken_e;
184 
185 FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char *fnName, int ordinal);
186 
187 /* The apr_load_dll_func call WILL return 0 set error to
188  * ERROR_INVALID_FUNCTION if the function cannot be loaded
189  */
190 #define APR_DECLARE_LATE_DLL_FUNC(lib, rettype, calltype, fn, ord, args, names) \
191     typedef rettype (calltype *apr_winapi_fpt_##fn) args; \
192     static apr_winapi_fpt_##fn apr_winapi_pfn_##fn = NULL; \
193     static int apr_winapi_chk_##fn = 0; \
194     static APR_INLINE int apr_winapi_ld_##fn(void) \
195     {   if (apr_winapi_pfn_##fn) return 1; \
196         if (apr_winapi_chk_##fn ++) return 0; \
197         if (!apr_winapi_pfn_##fn) \
198             apr_winapi_pfn_##fn = (apr_winapi_fpt_##fn) \
199                                       apr_load_dll_func(lib, #fn, ord); \
200         if (apr_winapi_pfn_##fn) return 1; else return 0; }; \
201     static APR_INLINE rettype apr_winapi_##fn args \
202     {   if (apr_winapi_ld_##fn()) \
203             return (*(apr_winapi_pfn_##fn)) names; \
204         else { SetLastError(ERROR_INVALID_FUNCTION); return 0;} }; \
205 
206 #define APR_HAVE_LATE_DLL_FUNC(fn) apr_winapi_ld_##fn()
207 
208 /* Provide late bound declarations of every API function missing from
209  * one or more supported releases of the Win32 API
210  *
211  * lib is the enumerated token from apr_dlltoken_e, and must correspond
212  * to the string table entry in start.c used by the apr_load_dll_func().
213  * Token names (attempt to) follow Windows.h declarations prefixed by DLL_
214  * in order to facilitate comparison.  Use the exact declaration syntax
215  * and names from Windows.h to prevent ambigutity and bugs.
216  *
217  * rettype and calltype follow the original declaration in Windows.h
218  * fn is the true function name - beware Ansi/Unicode #defined macros
219  * ord is the ordinal within the library, use 0 if it varies between versions
220  * args is the parameter list following the original declaration, in parens
221  * names is the parameter list sans data types, enclosed in parens
222  *
223  * #undef/re#define the Ansi/Unicode generic name to abate confusion
224  * In the case of non-text functions, simply #define the original name
225  */
226 
227 #if !defined(_WIN32_WCE) && !defined(WINNT)
228 /* This group is available to all versions of WINNT 4.0 SP6 and later */
229 
230 #ifdef GetFileAttributesExA
231 #undef GetFileAttributesExA
232 #endif
233 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, GetFileAttributesExA, 0, (
234     IN LPCSTR lpFileName,
235     IN GET_FILEEX_INFO_LEVELS fInfoLevelId,
236     OUT LPVOID lpFileInformation),
237     (lpFileName, fInfoLevelId, lpFileInformation));
238 #define GetFileAttributesExA apr_winapi_GetFileAttributesExA
239 #undef GetFileAttributesEx
240 #define GetFileAttributesEx apr_winapi_GetFileAttributesExA
241 
242 #ifdef GetFileAttributesExW
243 #undef GetFileAttributesExW
244 #endif
245 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, GetFileAttributesExW, 0, (
246     IN LPCWSTR lpFileName,
247     IN GET_FILEEX_INFO_LEVELS fInfoLevelId,
248     OUT LPVOID lpFileInformation),
249     (lpFileName, fInfoLevelId, lpFileInformation));
250 #define GetFileAttributesExW apr_winapi_GetFileAttributesExW
251 
252 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, CancelIo, 0, (
253     IN HANDLE hFile),
254     (hFile));
255 #define CancelIo apr_winapi_CancelIo
256 
257 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, TryEnterCriticalSection, 0, (
258     LPCRITICAL_SECTION lpCriticalSection),
259     (lpCriticalSection));
260 #define TryEnterCriticalSection apr_winapi_TryEnterCriticalSection
261 
262 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, SwitchToThread, 0, (
263     void),
264     ());
265 #define SwitchToThread apr_winapi_SwitchToThread
266 
267 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetEffectiveRightsFromAclW, 0, (
268     IN PACL pacl,
269     IN PTRUSTEE_W pTrustee,
270     OUT PACCESS_MASK pAccessRights),
271     (pacl, pTrustee, pAccessRights));
272 #define GetEffectiveRightsFromAclW apr_winapi_GetEffectiveRightsFromAclW
273 
274 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetNamedSecurityInfoW, 0, (
275     IN LPWSTR pObjectName,
276     IN SE_OBJECT_TYPE ObjectType,
277     IN SECURITY_INFORMATION SecurityInfo,
278     OUT PSID *ppsidOwner,
279     OUT PSID *ppsidGroup,
280     OUT PACL *ppDacl,
281     OUT PACL *ppSacl,
282     OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
283     (pObjectName, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
284         ppDacl, ppSacl, ppSecurityDescriptor));
285 #define GetNamedSecurityInfoW apr_winapi_GetNamedSecurityInfoW
286 
287 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetNamedSecurityInfoA, 0, (
288     IN LPSTR pObjectName,
289     IN SE_OBJECT_TYPE ObjectType,
290     IN SECURITY_INFORMATION SecurityInfo,
291     OUT PSID *ppsidOwner,
292     OUT PSID *ppsidGroup,
293     OUT PACL *ppDacl,
294     OUT PACL *ppSacl,
295     OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
296     (pObjectName, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
297         ppDacl, ppSacl, ppSecurityDescriptor));
298 #define GetNamedSecurityInfoA apr_winapi_GetNamedSecurityInfoA
299 #undef GetNamedSecurityInfo
300 #define GetNamedSecurityInfo apr_winapi_GetNamedSecurityInfoA
301 
302 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetSecurityInfo, 0, (
303     IN HANDLE handle,
304     IN SE_OBJECT_TYPE ObjectType,
305     IN SECURITY_INFORMATION SecurityInfo,
306     OUT PSID *ppsidOwner,
307     OUT PSID *ppsidGroup,
308     OUT PACL *ppDacl,
309     OUT PACL *ppSacl,
310     OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
311     (handle, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
312         ppDacl, ppSacl, ppSecurityDescriptor));
313 #define GetSecurityInfo apr_winapi_GetSecurityInfo
314 
315 APR_DECLARE_LATE_DLL_FUNC(DLL_SHSTDAPI, LPWSTR *, WINAPI, CommandLineToArgvW, 0, (
316     LPCWSTR lpCmdLine,
317     int *pNumArgs),
318     (lpCmdLine, pNumArgs));
319 #define CommandLineToArgvW apr_winapi_CommandLineToArgvW
320 
321 #endif /* !defined(_WIN32_WCE) && !defined(WINNT) */
322 
323 #if !defined(_WIN32_WCE)
324 /* This group is NOT available to all versions of WinNT,
325  * these we must always look up
326  */
327 
328 #ifdef GetCompressedFileSizeA
329 #undef GetCompressedFileSizeA
330 #endif
331 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, DWORD, WINAPI, GetCompressedFileSizeA, 0, (
332     IN LPCSTR lpFileName,
333     OUT LPDWORD lpFileSizeHigh),
334     (lpFileName, lpFileSizeHigh));
335 #define GetCompressedFileSizeA apr_winapi_GetCompressedFileSizeA
336 #undef GetCompressedFileSize
337 #define GetCompressedFileSize apr_winapi_GetCompressedFileSizeA
338 
339 #ifdef GetCompressedFileSizeW
340 #undef GetCompressedFileSizeW
341 #endif
342 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, DWORD, WINAPI, GetCompressedFileSizeW, 0, (
343     IN LPCWSTR lpFileName,
344     OUT LPDWORD lpFileSizeHigh),
345     (lpFileName, lpFileSizeHigh));
346 #define GetCompressedFileSizeW apr_winapi_GetCompressedFileSizeW
347 
348 
349 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtQueryTimerResolution, 0, (
350     ULONG *pMaxRes,  /* Minimum NS Resolution */
351     ULONG *pMinRes,  /* Maximum NS Resolution */
352     ULONG *pCurRes), /* Current NS Resolution */
353     (pMaxRes, pMinRes, pCurRes));
354 #define QueryTimerResolution apr_winapi_NtQueryTimerResolution
355 
356 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtSetTimerResolution, 0, (
357     ULONG ReqRes,    /* Requested NS Clock Resolution */
358     BOOL  Acquire,   /* Aquire (1) or Release (0) our interest */
359     ULONG *pNewRes), /* The NS Clock Resolution granted */
360     (ReqRes, Acquire, pNewRes));
361 #define SetTimerResolution apr_winapi_NtSetTimerResolution
362 
363 typedef struct PBI {
364     LONG      ExitStatus;
365     PVOID     PebBaseAddress;
366     apr_uintptr_t AffinityMask;
367     LONG      BasePriority;
368     apr_uintptr_t UniqueProcessId;
369     apr_uintptr_t InheritedFromUniqueProcessId;
370 } PBI, *PPBI;
371 
372 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtQueryInformationProcess, 0, (
373     HANDLE hProcess,  /* Obvious */
374     INT   info,       /* Use 0 for PBI documented above */
375     PVOID pPI,        /* The PIB buffer */
376     ULONG LenPI,      /* Use sizeof(PBI) */
377     ULONG *pSizePI),  /* returns pPI buffer used (may pass NULL) */
378     (hProcess, info, pPI, LenPI, pSizePI));
379 #define QueryInformationProcess apr_winapi_NtQueryInformationProcess
380 
381 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtQueryObject, 0, (
382     HANDLE hObject,   /* Obvious */
383     INT   info,       /* Use 0 for PBI documented above */
384     PVOID pOI,        /* The PIB buffer */
385     ULONG LenOI,      /* Use sizeof(PBI) */
386     ULONG *pSizeOI),  /* returns pPI buffer used (may pass NULL) */
387     (hObject, info, pOI, LenOI, pSizeOI));
388 #define QueryObject apr_winapi_NtQueryObject
389 
390 typedef struct IOSB {
391     union {
392     UINT Status;
393     PVOID reserved;
394     };
395     apr_uintptr_t Information; /* Varies by op, consumed buffer size for FSI below */
396 } IOSB, *PIOSB;
397 
398 typedef struct FSI {
399     LONGLONG AllocationSize;
400     LONGLONG EndOfFile;
401     ULONG    NumberOfLinks;
402     BOOL     DeletePending;
403     BOOL     Directory;
404 } FSI, *PFSI;
405 
406 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, ZwQueryInformationFile, 0, (
407     HANDLE hObject,    /* Obvious */
408     PVOID  pIOSB,      /* Point to the IOSB buffer for detailed return results */
409     PVOID  pFI,        /* The buffer, using FIB above */
410     ULONG  LenFI,      /* Use sizeof(FI) */
411     ULONG  info),      /* Use 5 for FSI documented above*/
412     (hObject, pIOSB, pFI, LenFI, info));
413 #define ZwQueryInformationFile apr_winapi_ZwQueryInformationFile
414 
415 #ifdef CreateToolhelp32Snapshot
416 #undef CreateToolhelp32Snapshot
417 #endif
418 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, HANDLE, WINAPI, CreateToolhelp32Snapshot, 0, (
419     DWORD dwFlags,
420     DWORD th32ProcessID),
421     (dwFlags, th32ProcessID));
422 #define CreateToolhelp32Snapshot apr_winapi_CreateToolhelp32Snapshot
423 
424 #ifdef Process32FirstW
425 #undef Process32FirstW
426 #endif
427 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, Process32FirstW, 0, (
428     HANDLE hSnapshot,
429     LPPROCESSENTRY32W lppe),
430     (hSnapshot, lppe));
431 #define Process32FirstW apr_winapi_Process32FirstW
432 
433 #ifdef Process32NextW
434 #undef Process32NextW
435 #endif
436 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, Process32NextW, 0, (
437     HANDLE hSnapshot,
438     LPPROCESSENTRY32W lppe),
439     (hSnapshot, lppe));
440 #define Process32NextW apr_winapi_Process32NextW
441 
442 #if !defined(POLLERR)
443 /* Event flag definitions for WSAPoll(). */
444 #define POLLRDNORM  0x0100
445 #define POLLRDBAND  0x0200
446 #define POLLIN      (POLLRDNORM | POLLRDBAND)
447 #define POLLPRI     0x0400
448 
449 #define POLLWRNORM  0x0010
450 #define POLLOUT     (POLLWRNORM)
451 #define POLLWRBAND  0x0020
452 
453 #define POLLERR     0x0001
454 #define POLLHUP     0x0002
455 #define POLLNVAL    0x0004
456 
457 typedef struct pollfd {
458     SOCKET  fd;
459     SHORT   events;
460     SHORT   revents;
461 
462 } WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
463 
464 #endif /* !defined(POLLERR) */
465 #ifdef WSAPoll
466 #undef WSAPoll
467 #endif
468 APR_DECLARE_LATE_DLL_FUNC(DLL_WINSOCK2API, int, WSAAPI, WSAPoll, 0, (
469     IN OUT LPWSAPOLLFD fdArray,
470     IN ULONG fds,
471     IN INT timeout),
472     (fdArray, fds, timeout));
473 #define WSAPoll apr_winapi_WSAPoll
474 #define HAVE_POLL   1
475 
476 #ifdef SetDllDirectoryW
477 #undef SetDllDirectoryW
478 #endif
479 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, SetDllDirectoryW, 0, (
480     IN LPCWSTR lpPathName),
481     (lpPathName));
482 #define SetDllDirectoryW apr_winapi_SetDllDirectoryW
483 
484 #endif /* !defined(_WIN32_WCE) */
485 
486 #endif  /* ! MISC_H */
487 
488