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