"Fossies" - the Fresh Open Source Software Archive

Member "apr-1.7.0/misc/win32/misc.c" (19 Mar 2019, 8998 Bytes) of package /linux/www/apr-1.7.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "misc.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.6.5_vs_1.7.0.

    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 #include "apr_private.h"
   18 #include "apr_arch_misc.h"
   19 #include "apr_arch_file_io.h"
   20 #include "assert.h"
   21 #include "apr_lib.h"
   22 #include "tchar.h"
   23 
   24 APR_DECLARE_DATA apr_oslevel_e apr_os_level = APR_WIN_UNK;
   25 
   26 apr_status_t apr_get_oslevel(apr_oslevel_e *level)
   27 {
   28     if (apr_os_level == APR_WIN_UNK) 
   29     {
   30         static OSVERSIONINFO oslev;
   31         oslev.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
   32         GetVersionEx(&oslev);
   33 
   34         if (oslev.dwPlatformId == VER_PLATFORM_WIN32_NT) 
   35         {
   36             static unsigned int servpack = 0;
   37             TCHAR *pservpack;
   38             if ((pservpack = oslev.szCSDVersion)) {
   39                 while (*pservpack && !apr_isdigit(*pservpack)) {
   40                     pservpack++;
   41                 }
   42                 if (*pservpack)
   43 #ifdef _UNICODE
   44                     servpack = _wtoi(pservpack);
   45 #else
   46                     servpack = atoi(pservpack);
   47 #endif
   48             }
   49 
   50             if (oslev.dwMajorVersion < 3) {
   51                 apr_os_level = APR_WIN_UNSUP;
   52             }
   53             else if (oslev.dwMajorVersion == 3) {
   54                 if (oslev.dwMajorVersion < 50) {
   55                     apr_os_level = APR_WIN_UNSUP;
   56                 }
   57                 else if (oslev.dwMajorVersion == 50) {
   58                     apr_os_level = APR_WIN_NT_3_5;
   59                 }
   60                 else {
   61                     apr_os_level = APR_WIN_NT_3_51;
   62                 }
   63             }
   64             else if (oslev.dwMajorVersion == 4) {
   65                 if (servpack < 2)
   66                     apr_os_level = APR_WIN_NT_4;
   67                 else if (servpack <= 2)
   68                     apr_os_level = APR_WIN_NT_4_SP2;
   69                 else if (servpack <= 3)
   70                     apr_os_level = APR_WIN_NT_4_SP3;
   71                 else if (servpack <= 4)
   72                     apr_os_level = APR_WIN_NT_4_SP4;
   73                 else if (servpack <= 5)
   74                     apr_os_level = APR_WIN_NT_4_SP5;
   75                 else 
   76                     apr_os_level = APR_WIN_NT_4_SP6;
   77             }
   78             else if (oslev.dwMajorVersion == 5) {
   79                 if (oslev.dwMinorVersion == 0) {
   80                     if (servpack == 0)
   81                         apr_os_level = APR_WIN_2000;
   82                     else if (servpack == 1)
   83                         apr_os_level = APR_WIN_2000_SP1;
   84                     else
   85                         apr_os_level = APR_WIN_2000_SP2;
   86                 }
   87                 else if (oslev.dwMinorVersion == 2) {
   88                     apr_os_level = APR_WIN_2003;
   89                 }
   90                 else {
   91                     if (servpack < 1)
   92                         apr_os_level = APR_WIN_XP;
   93                     else if (servpack == 1)
   94                         apr_os_level = APR_WIN_XP_SP1;
   95                     else
   96                         apr_os_level = APR_WIN_XP_SP2;
   97                 }
   98             }
   99             else if (oslev.dwMajorVersion == 6) {
  100                 if (oslev.dwMinorVersion == 0)
  101                     apr_os_level = APR_WIN_VISTA;
  102                 else
  103                     apr_os_level = APR_WIN_7;
  104             }
  105             else {
  106                 apr_os_level = APR_WIN_XP;
  107             }
  108         }
  109 #ifndef WINNT
  110         else if (oslev.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
  111             TCHAR *prevision;
  112             if (prevision = oslev.szCSDVersion) {
  113                 while (*prevision && !apr_isupper(*prevision)) {
  114                      prevision++;
  115                 }
  116             }
  117             else prevision = _T("");
  118 
  119             if (oslev.dwMinorVersion < 10) {
  120                 if (*prevision < _T('C'))
  121                     apr_os_level = APR_WIN_95;
  122                 else
  123                     apr_os_level = APR_WIN_95_OSR2;
  124             }
  125             else if (oslev.dwMinorVersion < 90) {
  126                 if (*prevision < _T('A'))
  127                     apr_os_level = APR_WIN_98;
  128                 else
  129                     apr_os_level = APR_WIN_98_SE;
  130             }
  131             else {
  132                 apr_os_level = APR_WIN_ME;
  133             }
  134         }
  135 #endif
  136 #ifdef _WIN32_WCE
  137         else if (oslev.dwPlatformId == VER_PLATFORM_WIN32_CE) 
  138         {
  139             if (oslev.dwMajorVersion < 3) {
  140                 apr_os_level = APR_WIN_UNSUP;
  141             }
  142             else {
  143                 apr_os_level = APR_WIN_CE_3;
  144             }
  145         }
  146 #endif
  147         else {
  148             apr_os_level = APR_WIN_UNSUP;
  149         }
  150     }
  151 
  152     *level = apr_os_level;
  153 
  154     if (apr_os_level < APR_WIN_UNSUP) {
  155         return APR_EGENERAL;
  156     }
  157 
  158     return APR_SUCCESS;
  159 }
  160 
  161 
  162 /* This is the helper code to resolve late bound entry points 
  163  * missing from one or more releases of the Win32 API
  164  */
  165 
  166 static const char* const lateDllName[DLL_defined] = {
  167     "kernel32", "advapi32", "mswsock",  "ws2_32", "shell32", "ntdll.dll",
  168     "Iphplapi"  };
  169 static HMODULE lateDllHandle[DLL_defined] = {
  170      NULL,       NULL,       NULL,       NULL,     NULL,      NULL,
  171      NULL       };
  172 
  173 FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char* fnName, int ordinal)
  174 {
  175     if (!lateDllHandle[fnLib]) { 
  176         lateDllHandle[fnLib] = LoadLibraryA(lateDllName[fnLib]);
  177         if (!lateDllHandle[fnLib])
  178             return NULL;
  179     }
  180 #if defined(_WIN32_WCE)
  181     if (ordinal)
  182         return GetProcAddressA(lateDllHandle[fnLib], (const char *)
  183                                                      (apr_ssize_t)ordinal);
  184     else
  185         return GetProcAddressA(lateDllHandle[fnLib], fnName);
  186 #else
  187     if (ordinal)
  188         return GetProcAddress(lateDllHandle[fnLib], (const char *)
  189                                                     (apr_ssize_t)ordinal);
  190     else
  191         return GetProcAddress(lateDllHandle[fnLib], fnName);
  192 #endif
  193 }
  194 
  195 /* Declared in include/arch/win32/apr_dbg_win32_handles.h
  196  */
  197 APR_DECLARE_NONSTD(HANDLE) apr_dbg_log(char* fn, HANDLE ha, char* fl, int ln, 
  198                                        int nh, /* HANDLE hv, char *dsc */...)
  199 {
  200     static DWORD tlsid = 0xFFFFFFFF;
  201     static HANDLE fh = NULL;
  202     static long ctr = 0;
  203     static CRITICAL_SECTION cs;
  204     long seq;
  205     DWORD wrote;
  206     char *sbuf;
  207     
  208     seq = (InterlockedIncrement)(&ctr);
  209 
  210     if (tlsid == 0xFFFFFFFF) {
  211         tlsid = (TlsAlloc)();
  212     }
  213 
  214     sbuf = (TlsGetValue)(tlsid);
  215     if (!fh || !sbuf) {
  216         sbuf = (malloc)(1024);
  217         (TlsSetValue)(tlsid, sbuf);
  218         sbuf[1023] = '\0';
  219         if (!fh) {
  220             (GetModuleFileNameA)(NULL, sbuf, 250);
  221             sprintf(strchr(sbuf, '\0'), ".%u",
  222                     (unsigned int)(GetCurrentProcessId)());
  223             fh = (CreateFileA)(sbuf, GENERIC_WRITE, 0, NULL, 
  224                             CREATE_ALWAYS, 0, NULL);
  225             (InitializeCriticalSection)(&cs);
  226         }
  227     }
  228 
  229     if (!nh) {
  230         (sprintf)(sbuf, "%p %08x %08x %s() %s:%d\n",
  231                   ha, (unsigned int)seq, (unsigned int)GetCurrentThreadId(),
  232                   fn, fl, ln);
  233         (EnterCriticalSection)(&cs);
  234         (WriteFile)(fh, sbuf, (DWORD)strlen(sbuf), &wrote, NULL);
  235         (LeaveCriticalSection)(&cs);
  236     } 
  237     else {
  238         va_list a;
  239         va_start(a,nh);
  240         (EnterCriticalSection)(&cs);
  241         do {
  242             HANDLE *hv = va_arg(a, HANDLE*);
  243             char *dsc = va_arg(a, char*);
  244             if (strcmp(dsc, "Signaled") == 0) {
  245                 if ((apr_ssize_t)ha >= STATUS_WAIT_0 
  246                        && (apr_ssize_t)ha < STATUS_ABANDONED_WAIT_0) {
  247                     hv += (apr_ssize_t)ha;
  248                 }
  249                 else if ((apr_ssize_t)ha >= STATUS_ABANDONED_WAIT_0
  250                             && (apr_ssize_t)ha < STATUS_USER_APC) {
  251                     hv += (apr_ssize_t)ha - STATUS_ABANDONED_WAIT_0;
  252                     dsc = "Abandoned";
  253                 }
  254                 else if ((apr_ssize_t)ha == WAIT_TIMEOUT) {
  255                     dsc = "Timed Out";
  256                 }
  257             }
  258             (sprintf)(sbuf, "%p %08x %08x %s(%s) %s:%d\n",
  259                       *hv, (unsigned int)seq,
  260                       (unsigned int)GetCurrentThreadId(), 
  261                       fn, dsc, fl, ln);
  262             (WriteFile)(fh, sbuf, (DWORD)strlen(sbuf), &wrote, NULL);
  263         } while (--nh);
  264         (LeaveCriticalSection)(&cs);
  265         va_end(a);
  266     }
  267     return ha;
  268 }