GME  13
apr_arch_misc.h
Go to the documentation of this file.
00001 /* Licensed to the Apache Software Foundation (ASF) under one or more
00002  * contributor license agreements.  See the NOTICE file distributed with
00003  * this work for additional information regarding copyright ownership.
00004  * The ASF licenses this file to You under the Apache License, Version 2.0
00005  * (the "License"); you may not use this file except in compliance with
00006  * the License.  You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef MISC_H
00018 #define MISC_H
00019 
00020 #include "apr.h"
00021 #include "apr_portable.h"
00022 #include "apr_private.h"
00023 #include "apr_general.h"
00024 #include "apr_pools.h"
00025 #include "apr_getopt.h"
00026 #include "apr_thread_proc.h"
00027 #include "apr_file_io.h"
00028 #include "apr_errno.h"
00029 #include "apr_getopt.h"
00030 
00031 #if APR_HAVE_STDIO_H
00032 #include <stdio.h>
00033 #endif
00034 #if APR_HAVE_SIGNAL_H
00035 #include <signal.h>
00036 #endif
00037 #if APR_HAVE_PTHREAD_H
00038 #include <pthread.h>
00039 #endif
00040 #if APR_HAVE_STDLIB_H
00041 #include <stdlib.h>
00042 #endif
00043 #if APR_HAVE_STRING_H
00044 #include <string.h>
00045 #endif
00046 #ifndef _WIN32_WCE
00047 #include <tlhelp32.h>
00048 #endif
00049 
00050 struct apr_other_child_rec_t {
00051     apr_pool_t *p;
00052     struct apr_other_child_rec_t *next;
00053     apr_proc_t *proc;
00054     void (*maintenance) (int, void *, int);
00055     void *data;
00056     apr_os_file_t write_fd;
00057 };
00058 
00059 #define WSAHighByte 2
00060 #define WSALowByte 0
00061 
00062 /* start.c and apr_app.c helpers and communication within misc.c
00063  *
00064  * They are not for public consumption, although apr_app_init_complete
00065  * must be an exported symbol to avoid reinitialization.
00066  */
00067 extern int APR_DECLARE_DATA apr_app_init_complete;
00068 
00069 int apr_wastrtoastr(char const * const * *retarr,
00070                     wchar_t const * const *arr, int args);
00071 
00072 /* Platform specific designation of run time os version.
00073  * Gaps allow for specific service pack levels that
00074  * export new kernel or winsock functions or behavior.
00075  */
00076 typedef enum {
00077     APR_WIN_UNK =       0,
00078     APR_WIN_UNSUP =     1,
00079     APR_WIN_95 =       10,
00080     APR_WIN_95_B =     11,
00081     APR_WIN_95_OSR2 =  12,
00082     APR_WIN_98 =       14,
00083     APR_WIN_98_SE =    16,
00084     APR_WIN_ME =       18,
00085 
00086     APR_WIN_UNICODE =  20, /* Prior versions support only narrow chars */
00087 
00088     APR_WIN_CE_3 =     23, /* CE is an odd beast, not supporting */
00089                            /* some pre-NT features, such as the    */
00090     APR_WIN_NT =       30, /* narrow charset APIs (fooA fns), while  */
00091     APR_WIN_NT_3_5 =   35, /* not supporting some NT-family features.  */
00092     APR_WIN_NT_3_51 =  36,
00093 
00094     APR_WIN_NT_4 =     40,
00095     APR_WIN_NT_4_SP2 = 42,
00096     APR_WIN_NT_4_SP3 = 43,
00097     APR_WIN_NT_4_SP4 = 44,
00098     APR_WIN_NT_4_SP5 = 45,
00099     APR_WIN_NT_4_SP6 = 46,
00100 
00101     APR_WIN_2000 =     50,
00102     APR_WIN_2000_SP1 = 51,
00103     APR_WIN_2000_SP2 = 52,
00104     APR_WIN_XP =       60,
00105     APR_WIN_XP_SP1 =   61,
00106     APR_WIN_XP_SP2 =   62,
00107     APR_WIN_2003 =     70,
00108     APR_WIN_VISTA =    80
00109 } apr_oslevel_e;
00110 
00111 extern APR_DECLARE_DATA apr_oslevel_e apr_os_level;
00112 
00113 apr_status_t apr_get_oslevel(apr_oslevel_e *);
00114 
00115 /* The APR_HAS_ANSI_FS symbol is PRIVATE, and internal to APR.
00116  * APR only supports char data for filenames.  Like most applications,
00117  * characters >127 are essentially undefined.  APR_HAS_UNICODE_FS lets
00118  * the application know that utf-8 is the encoding method of APR, and
00119  * only incidently hints that we have Wide OS calls.
00120  *
00121  * APR_HAS_ANSI_FS is simply an OS flag to tell us all calls must be
00122  * the unicode eqivilant.
00123  */
00124 
00125 #if defined(_WIN32_WCE) || defined(WINNT)
00126 #define APR_HAS_ANSI_FS           0
00127 #else
00128 #define APR_HAS_ANSI_FS           1
00129 #endif
00130 
00131 /* IF_WIN_OS_IS_UNICODE / ELSE_WIN_OS_IS_ANSI help us keep the code trivial
00132  * where have runtime tests for unicode-ness, that aren't needed in any
00133  * build which supports only WINNT or WCE.
00134  */
00135 #if APR_HAS_ANSI_FS && APR_HAS_UNICODE_FS
00136 #define IF_WIN_OS_IS_UNICODE if (apr_os_level >= APR_WIN_UNICODE)
00137 #define ELSE_WIN_OS_IS_ANSI else
00138 #else /* APR_HAS_UNICODE_FS */
00139 #define IF_WIN_OS_IS_UNICODE
00140 #define ELSE_WIN_OS_IS_ANSI
00141 #endif /* WINNT */
00142 
00143 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
00144 #include "crtdbg.h"
00145 
00146 static APR_INLINE void* apr_malloc_dbg(size_t size, const char* filename,
00147                                        int linenumber)
00148 {
00149     return _malloc_dbg(size, _CRT_BLOCK, filename, linenumber);
00150 }
00151 
00152 static APR_INLINE void* apr_realloc_dbg(void* userData, size_t newSize,
00153                                         const char* filename, int linenumber)
00154 {
00155     return _realloc_dbg(userData, newSize, _CRT_BLOCK, filename, linenumber);
00156 }
00157 
00158 #else
00159 
00160 static APR_INLINE void* apr_malloc_dbg(size_t size, const char* filename,
00161                                        int linenumber)
00162 {
00163     return malloc(size);
00164 }
00165 
00166 static APR_INLINE void* apr_realloc_dbg(void* userData, size_t newSize,
00167                                         const char* filename, int linenumber)
00168 {
00169     return realloc(userData, newSize);
00170 }
00171 
00172 #endif  /* ! _MSC_VER */
00173 
00174 typedef enum {
00175     DLL_WINBASEAPI = 0,    /* kernel32 From WinBase.h       */
00176     DLL_WINADVAPI = 1,     /* advapi32 From WinBase.h       */
00177     DLL_WINSOCKAPI = 2,    /* mswsock  From WinSock.h       */
00178     DLL_WINSOCK2API = 3,   /* ws2_32   From WinSock2.h      */
00179     DLL_SHSTDAPI = 4,      /* shell32  From ShellAPI.h      */
00180     DLL_NTDLL = 5,         /* shell32  From our real kernel */
00181     DLL_defined = 6        /* must define as last idx_ + 1  */
00182 } apr_dlltoken_e;
00183 
00184 FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char *fnName, int ordinal);
00185 
00186 /* The apr_load_dll_func call WILL return 0 set error to
00187  * ERROR_INVALID_FUNCTION if the function cannot be loaded
00188  */
00189 #define APR_DECLARE_LATE_DLL_FUNC(lib, rettype, calltype, fn, ord, args, names) \
00190     typedef rettype (calltype *apr_winapi_fpt_##fn) args; \
00191     static apr_winapi_fpt_##fn apr_winapi_pfn_##fn = NULL; \
00192     static int apr_winapi_chk_##fn = 0; \
00193     static APR_INLINE int apr_winapi_ld_##fn(void) \
00194     {   if (apr_winapi_pfn_##fn) return 1; \
00195         if (apr_winapi_chk_##fn ++) return 0; \
00196         if (!apr_winapi_pfn_##fn) \
00197             apr_winapi_pfn_##fn = (apr_winapi_fpt_##fn) \
00198                                       apr_load_dll_func(lib, #fn, ord); \
00199         if (apr_winapi_pfn_##fn) return 1; else return 0; }; \
00200     static APR_INLINE rettype apr_winapi_##fn args \
00201     {   if (apr_winapi_ld_##fn()) \
00202             return (*(apr_winapi_pfn_##fn)) names; \
00203         else { SetLastError(ERROR_INVALID_FUNCTION); return 0;} }; \
00204 
00205 #define APR_HAVE_LATE_DLL_FUNC(fn) apr_winapi_ld_##fn()
00206 
00207 /* Provide late bound declarations of every API function missing from
00208  * one or more supported releases of the Win32 API
00209  *
00210  * lib is the enumerated token from apr_dlltoken_e, and must correspond
00211  * to the string table entry in start.c used by the apr_load_dll_func().
00212  * Token names (attempt to) follow Windows.h declarations prefixed by DLL_
00213  * in order to facilitate comparison.  Use the exact declaration syntax
00214  * and names from Windows.h to prevent ambigutity and bugs.
00215  *
00216  * rettype and calltype follow the original declaration in Windows.h
00217  * fn is the true function name - beware Ansi/Unicode #defined macros
00218  * ord is the ordinal within the library, use 0 if it varies between versions
00219  * args is the parameter list following the original declaration, in parens
00220  * names is the parameter list sans data types, enclosed in parens
00221  *
00222  * #undef/re#define the Ansi/Unicode generic name to abate confusion
00223  * In the case of non-text functions, simply #define the original name
00224  */
00225 
00226 #if !defined(_WIN32_WCE) && !defined(WINNT)
00227 /* This group is available to all versions of WINNT 4.0 SP6 and later */
00228 
00229 #ifdef GetFileAttributesExA
00230 #undef GetFileAttributesExA
00231 #endif
00232 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, GetFileAttributesExA, 0, (
00233     IN LPCSTR lpFileName,
00234     IN GET_FILEEX_INFO_LEVELS fInfoLevelId,
00235     OUT LPVOID lpFileInformation),
00236     (lpFileName, fInfoLevelId, lpFileInformation));
00237 #define GetFileAttributesExA apr_winapi_GetFileAttributesExA
00238 #undef GetFileAttributesEx
00239 #define GetFileAttributesEx apr_winapi_GetFileAttributesExA
00240 
00241 #ifdef GetFileAttributesExW
00242 #undef GetFileAttributesExW
00243 #endif
00244 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, GetFileAttributesExW, 0, (
00245     IN LPCWSTR lpFileName,
00246     IN GET_FILEEX_INFO_LEVELS fInfoLevelId,
00247     OUT LPVOID lpFileInformation),
00248     (lpFileName, fInfoLevelId, lpFileInformation));
00249 #define GetFileAttributesExW apr_winapi_GetFileAttributesExW
00250 
00251 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, CancelIo, 0, (
00252     IN HANDLE hFile),
00253     (hFile));
00254 #define CancelIo apr_winapi_CancelIo
00255 
00256 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, TryEnterCriticalSection, 0, (
00257     LPCRITICAL_SECTION lpCriticalSection),
00258     (lpCriticalSection));
00259 #define TryEnterCriticalSection apr_winapi_TryEnterCriticalSection
00260 
00261 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, SwitchToThread, 0, (
00262     void),
00263     ());
00264 #define SwitchToThread apr_winapi_SwitchToThread
00265 
00266 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetEffectiveRightsFromAclW, 0, (
00267     IN PACL pacl,
00268     IN PTRUSTEE_W pTrustee,
00269     OUT PACCESS_MASK pAccessRights),
00270     (pacl, pTrustee, pAccessRights));
00271 #define GetEffectiveRightsFromAclW apr_winapi_GetEffectiveRightsFromAclW
00272 
00273 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetNamedSecurityInfoW, 0, (
00274     IN LPWSTR pObjectName,
00275     IN SE_OBJECT_TYPE ObjectType,
00276     IN SECURITY_INFORMATION SecurityInfo,
00277     OUT PSID *ppsidOwner,
00278     OUT PSID *ppsidGroup,
00279     OUT PACL *ppDacl,
00280     OUT PACL *ppSacl,
00281     OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
00282     (pObjectName, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
00283         ppDacl, ppSacl, ppSecurityDescriptor));
00284 #define GetNamedSecurityInfoW apr_winapi_GetNamedSecurityInfoW
00285 
00286 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetNamedSecurityInfoA, 0, (
00287     IN LPSTR pObjectName,
00288     IN SE_OBJECT_TYPE ObjectType,
00289     IN SECURITY_INFORMATION SecurityInfo,
00290     OUT PSID *ppsidOwner,
00291     OUT PSID *ppsidGroup,
00292     OUT PACL *ppDacl,
00293     OUT PACL *ppSacl,
00294     OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
00295     (pObjectName, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
00296         ppDacl, ppSacl, ppSecurityDescriptor));
00297 #define GetNamedSecurityInfoA apr_winapi_GetNamedSecurityInfoA
00298 #undef GetNamedSecurityInfo
00299 #define GetNamedSecurityInfo apr_winapi_GetNamedSecurityInfoA
00300 
00301 APR_DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, GetSecurityInfo, 0, (
00302     IN HANDLE handle,
00303     IN SE_OBJECT_TYPE ObjectType,
00304     IN SECURITY_INFORMATION SecurityInfo,
00305     OUT PSID *ppsidOwner,
00306     OUT PSID *ppsidGroup,
00307     OUT PACL *ppDacl,
00308     OUT PACL *ppSacl,
00309     OUT PSECURITY_DESCRIPTOR *ppSecurityDescriptor),
00310     (handle, ObjectType, SecurityInfo, ppsidOwner, ppsidGroup,
00311         ppDacl, ppSacl, ppSecurityDescriptor));
00312 #define GetSecurityInfo apr_winapi_GetSecurityInfo
00313 
00314 APR_DECLARE_LATE_DLL_FUNC(DLL_SHSTDAPI, LPWSTR *, WINAPI, CommandLineToArgvW, 0, (
00315     LPCWSTR lpCmdLine,
00316     int *pNumArgs),
00317     (lpCmdLine, pNumArgs));
00318 #define CommandLineToArgvW apr_winapi_CommandLineToArgvW
00319 
00320 #endif /* !defined(_WIN32_WCE) && !defined(WINNT) */
00321 
00322 #if !defined(_WIN32_WCE)
00323 /* This group is NOT available to all versions of WinNT,
00324  * these we must always look up
00325  */
00326 
00327 #ifdef GetCompressedFileSizeA
00328 #undef GetCompressedFileSizeA
00329 #endif
00330 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, DWORD, WINAPI, GetCompressedFileSizeA, 0, (
00331     IN LPCSTR lpFileName,
00332     OUT LPDWORD lpFileSizeHigh),
00333     (lpFileName, lpFileSizeHigh));
00334 #define GetCompressedFileSizeA apr_winapi_GetCompressedFileSizeA
00335 #undef GetCompressedFileSize
00336 #define GetCompressedFileSize apr_winapi_GetCompressedFileSizeA
00337 
00338 #ifdef GetCompressedFileSizeW
00339 #undef GetCompressedFileSizeW
00340 #endif
00341 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, DWORD, WINAPI, GetCompressedFileSizeW, 0, (
00342     IN LPCWSTR lpFileName,
00343     OUT LPDWORD lpFileSizeHigh),
00344     (lpFileName, lpFileSizeHigh));
00345 #define GetCompressedFileSizeW apr_winapi_GetCompressedFileSizeW
00346 
00347 
00348 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtQueryTimerResolution, 0, (
00349     ULONG *pMaxRes,  /* Minimum NS Resolution */
00350     ULONG *pMinRes,  /* Maximum NS Resolution */
00351     ULONG *pCurRes), /* Current NS Resolution */
00352     (pMaxRes, pMinRes, pCurRes));
00353 #define QueryTimerResolution apr_winapi_NtQueryTimerResolution
00354 
00355 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtSetTimerResolution, 0, (
00356     ULONG ReqRes,    /* Requested NS Clock Resolution */
00357     BOOL  Acquire,   /* Aquire (1) or Release (0) our interest */
00358     ULONG *pNewRes), /* The NS Clock Resolution granted */
00359     (ReqRes, Acquire, pNewRes));
00360 #define SetTimerResolution apr_winapi_NtSetTimerResolution
00361 
00362 typedef struct PBI {
00363     LONG      ExitStatus;
00364     PVOID     PebBaseAddress;
00365     apr_uintptr_t AffinityMask;
00366     LONG      BasePriority;
00367     apr_uintptr_t UniqueProcessId;
00368     apr_uintptr_t InheritedFromUniqueProcessId;
00369 } PBI, *PPBI;
00370 
00371 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtQueryInformationProcess, 0, (
00372     HANDLE hProcess,  /* Obvious */
00373     INT   info,       /* Use 0 for PBI documented above */
00374     PVOID pPI,        /* The PIB buffer */
00375     ULONG LenPI,      /* Use sizeof(PBI) */
00376     ULONG *pSizePI),  /* returns pPI buffer used (may pass NULL) */
00377     (hProcess, info, pPI, LenPI, pSizePI));
00378 #define QueryInformationProcess apr_winapi_NtQueryInformationProcess
00379 
00380 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, NtQueryObject, 0, (
00381     HANDLE hObject,   /* Obvious */
00382     INT   info,       /* Use 0 for PBI documented above */
00383     PVOID pOI,        /* The PIB buffer */
00384     ULONG LenOI,      /* Use sizeof(PBI) */
00385     ULONG *pSizeOI),  /* returns pPI buffer used (may pass NULL) */
00386     (hObject, info, pOI, LenOI, pSizeOI));
00387 #define QueryObject apr_winapi_NtQueryObject
00388 
00389 typedef struct IOSB {
00390     union {
00391     UINT Status;
00392     PVOID reserved;
00393     };
00394     apr_uintptr_t Information; /* Varies by op, consumed buffer size for FSI below */
00395 } IOSB, *PIOSB;
00396 
00397 typedef struct FSI {
00398     LONGLONG AllocationSize;
00399     LONGLONG EndOfFile;
00400     ULONG    NumberOfLinks;
00401     BOOL     DeletePending;
00402     BOOL     Directory;
00403 } FSI, *PFSI;
00404 
00405 APR_DECLARE_LATE_DLL_FUNC(DLL_NTDLL, LONG, WINAPI, ZwQueryInformationFile, 0, (
00406     HANDLE hObject,    /* Obvious */
00407     PVOID  pIOSB,      /* Point to the IOSB buffer for detailed return results */
00408     PVOID  pFI,        /* The buffer, using FIB above */
00409     ULONG  LenFI,      /* Use sizeof(FI) */
00410     ULONG  info),      /* Use 5 for FSI documented above*/
00411     (hObject, pIOSB, pFI, LenFI, info));
00412 #define ZwQueryInformationFile apr_winapi_ZwQueryInformationFile
00413 
00414 #ifdef CreateToolhelp32Snapshot
00415 #undef CreateToolhelp32Snapshot
00416 #endif
00417 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, HANDLE, WINAPI, CreateToolhelp32Snapshot, 0, (
00418     DWORD dwFlags,
00419     DWORD th32ProcessID),
00420     (dwFlags, th32ProcessID));
00421 #define CreateToolhelp32Snapshot apr_winapi_CreateToolhelp32Snapshot
00422 
00423 #ifdef Process32FirstW
00424 #undef Process32FirstW
00425 #endif
00426 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, Process32FirstW, 0, (
00427     HANDLE hSnapshot,
00428     LPPROCESSENTRY32W lppe),
00429     (hSnapshot, lppe));
00430 #define Process32FirstW apr_winapi_Process32FirstW
00431 
00432 #ifdef Process32NextW
00433 #undef Process32NextW
00434 #endif
00435 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, Process32NextW, 0, (
00436     HANDLE hSnapshot,
00437     LPPROCESSENTRY32W lppe),
00438     (hSnapshot, lppe));
00439 #define Process32NextW apr_winapi_Process32NextW
00440 
00441 #if !defined(POLLERR)
00442 /* Event flag definitions for WSAPoll(). */
00443 #define POLLRDNORM  0x0100
00444 #define POLLRDBAND  0x0200
00445 #define POLLIN      (POLLRDNORM | POLLRDBAND)
00446 #define POLLPRI     0x0400
00447 
00448 #define POLLWRNORM  0x0010
00449 #define POLLOUT     (POLLWRNORM)
00450 #define POLLWRBAND  0x0020
00451 
00452 #define POLLERR     0x0001
00453 #define POLLHUP     0x0002
00454 #define POLLNVAL    0x0004
00455 
00456 typedef struct pollfd {
00457     SOCKET  fd;
00458     SHORT   events;
00459     SHORT   revents;
00460 
00461 } WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
00462 
00463 #endif /* !defined(POLLERR) */
00464 #ifdef WSAPoll
00465 #undef WSAPoll
00466 #endif
00467 APR_DECLARE_LATE_DLL_FUNC(DLL_WINSOCK2API, int, WSAAPI, WSAPoll, 0, (
00468     IN OUT LPWSAPOLLFD fdArray,
00469     IN ULONG fds,
00470     IN INT timeout),
00471     (fdArray, fds, timeout));
00472 #define WSAPoll apr_winapi_WSAPoll
00473 #define HAVE_POLL   1
00474 
00475 #ifdef SetDllDirectoryW
00476 #undef SetDllDirectoryW
00477 #endif
00478 APR_DECLARE_LATE_DLL_FUNC(DLL_WINBASEAPI, BOOL, WINAPI, SetDllDirectoryW, 0, (
00479     IN LPCWSTR lpPathName),
00480     (lpPathName));
00481 #define SetDllDirectoryW apr_winapi_SetDllDirectoryW
00482 
00483 #endif /* !defined(_WIN32_WCE) */
00484 
00485 #endif  /* ! MISC_H */
00486