~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/misc/win32/misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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 if (oslev.dwMinorVersion == 2) {
 
84
                    apr_os_level = APR_WIN_2003;                    
 
85
                }
 
86
                else {
 
87
                    if (servpack < 1)
 
88
                        apr_os_level = APR_WIN_XP;
 
89
                    else if (servpack == 1)
 
90
                        apr_os_level = APR_WIN_XP_SP1;
 
91
                    else
 
92
                        apr_os_level = APR_WIN_XP_SP2;
 
93
                }
 
94
            }
 
95
            else {
 
96
                apr_os_level = APR_WIN_XP;
 
97
            }
 
98
        }
 
99
#ifndef WINNT
 
100
        else if (oslev.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
 
101
            char *prevision;
 
102
            if (prevision = oslev.szCSDVersion) {
 
103
                while (*prevision && !apr_isupper(*prevision)) {
 
104
                     prevision++;
 
105
                }
 
106
            }
 
107
            else prevision = "";
 
108
 
 
109
            if (oslev.dwMinorVersion < 10) {
 
110
                if (*prevision < 'C')
 
111
                    apr_os_level = APR_WIN_95;
 
112
                else
 
113
                    apr_os_level = APR_WIN_95_OSR2;
 
114
            }
 
115
            else if (oslev.dwMinorVersion < 90) {
 
116
                if (*prevision < 'A')
 
117
                    apr_os_level = APR_WIN_98;
 
118
                else
 
119
                    apr_os_level = APR_WIN_98_SE;
 
120
            }
 
121
            else {
 
122
                apr_os_level = APR_WIN_ME;
 
123
            }
 
124
        }
 
125
#endif
 
126
#ifdef _WIN32_WCE
 
127
        else if (oslev.dwPlatformId == VER_PLATFORM_WIN32_CE) 
 
128
        {
 
129
            if (oslev.dwMajorVersion < 3) {
 
130
                apr_os_level = APR_WIN_UNSUP;
 
131
            }
 
132
            else {
 
133
                apr_os_level = APR_WIN_CE_3;
 
134
            }
 
135
        }
 
136
#endif
 
137
        else {
 
138
            apr_os_level = APR_WIN_UNSUP;
 
139
        }
 
140
    }
 
141
 
 
142
    *level = apr_os_level;
 
143
 
 
144
    if (apr_os_level < APR_WIN_UNSUP) {
 
145
        return APR_EGENERAL;
 
146
    }
 
147
 
 
148
    return APR_SUCCESS;
 
149
}
 
150
 
 
151
 
 
152
/* This is the helper code to resolve late bound entry points 
 
153
 * missing from one or more releases of the Win32 API
 
154
 */
 
155
 
 
156
static const char* const lateDllName[DLL_defined] = {
 
157
    "kernel32", "advapi32", "mswsock",  "ws2_32", "shell32", "ntdll.dll"  };
 
158
static HMODULE lateDllHandle[DLL_defined] = {
 
159
     NULL,       NULL,       NULL,       NULL,     NULL,       NULL       };
 
160
 
 
161
FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char* fnName, int ordinal)
 
162
{
 
163
    if (!lateDllHandle[fnLib]) { 
 
164
        lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]);
 
165
        if (!lateDllHandle[fnLib])
 
166
            return NULL;
 
167
    }
 
168
    if (ordinal)
 
169
        return GetProcAddress(lateDllHandle[fnLib], (char *) ordinal);
 
170
    else
 
171
        return GetProcAddress(lateDllHandle[fnLib], fnName);
 
172
}
 
173
 
 
174
/* Declared in include/arch/win32/apr_dbg_win32_handles.h
 
175
 */
 
176
APR_DECLARE_NONSTD(HANDLE) apr_dbg_log(char* fn, HANDLE ha, char* fl, int ln, 
 
177
                                       int nh, /* HANDLE hv, char *dsc */...)
 
178
{
 
179
    static DWORD tlsid = 0xFFFFFFFF;
 
180
    static HANDLE fh = NULL;
 
181
    static long ctr = 0;
 
182
    static CRITICAL_SECTION cs;
 
183
    long seq;
 
184
    DWORD wrote;
 
185
    char *sbuf;
 
186
    
 
187
    seq = (InterlockedIncrement)(&ctr);
 
188
 
 
189
    if (tlsid == 0xFFFFFFFF) {
 
190
        tlsid = (TlsAlloc)();
 
191
    }
 
192
 
 
193
    sbuf = (TlsGetValue)(tlsid);
 
194
    if (!fh || !sbuf) {
 
195
        sbuf = (malloc)(1024);
 
196
        (TlsSetValue)(tlsid, sbuf);
 
197
        sbuf[1023] = '\0';
 
198
        if (!fh) {
 
199
            (GetModuleFileName)(NULL, sbuf, 250);
 
200
            sprintf(strchr(sbuf, '\0'), ".%d",
 
201
                    (GetCurrentProcessId)());
 
202
            fh = (CreateFile)(sbuf, GENERIC_WRITE, 0, NULL, 
 
203
                            CREATE_ALWAYS, 0, NULL);
 
204
            (InitializeCriticalSection)(&cs);
 
205
        }
 
206
    }
 
207
 
 
208
    if (!nh) {
 
209
        (sprintf)(sbuf, "%08x %08x %08x %s() %s:%d\n",
 
210
                  (DWORD)ha, seq, GetCurrentThreadId(), fn, fl, ln);
 
211
        (EnterCriticalSection)(&cs);
 
212
        (WriteFile)(fh, sbuf, (DWORD)strlen(sbuf), &wrote, NULL);
 
213
        (LeaveCriticalSection)(&cs);
 
214
    } 
 
215
    else {
 
216
        va_list a;
 
217
        va_start(a,nh);
 
218
        (EnterCriticalSection)(&cs);
 
219
        do {
 
220
            HANDLE *hv = va_arg(a, HANDLE*);
 
221
            char *dsc = va_arg(a, char*);
 
222
            if (strcmp(dsc, "Signaled") == 0) {
 
223
                if ((DWORD)ha >= STATUS_WAIT_0 
 
224
                       && (DWORD)ha < STATUS_ABANDONED_WAIT_0) {
 
225
                    hv += (DWORD)ha;
 
226
                }
 
227
                else if ((DWORD)ha >= STATUS_ABANDONED_WAIT_0
 
228
                            && (DWORD)ha < STATUS_USER_APC) {
 
229
                    hv += (DWORD)ha - STATUS_ABANDONED_WAIT_0;
 
230
                    dsc = "Abandoned";
 
231
                }
 
232
                else if ((DWORD)ha == WAIT_TIMEOUT) {
 
233
                    dsc = "Timed Out";
 
234
                }
 
235
            }
 
236
            (sprintf)(sbuf, "%08x %08x %08x %s(%s) %s:%d\n",
 
237
                      (DWORD*)*hv, seq, GetCurrentThreadId(), 
 
238
                      fn, dsc, fl, ln);
 
239
            (WriteFile)(fh, sbuf, (DWORD)strlen(sbuf), &wrote, NULL);
 
240
        } while (--nh);
 
241
        (LeaveCriticalSection)(&cs);
 
242
        va_end(a);
 
243
    }
 
244
    return ha;
 
245
}