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

« back to all changes in this revision

Viewing changes to srclib/apr/shmem/win32/shm.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_general.h"
 
18
#include "apr_errno.h"
 
19
#include "apr_file_io.h"
 
20
#include "apr_shm.h"
 
21
#include "apr_arch_file_io.h"
 
22
#include "limits.h"
 
23
 
 
24
typedef struct memblock_t {
 
25
    apr_size_t size;
 
26
    apr_size_t length;
 
27
} memblock_t;
 
28
 
 
29
struct apr_shm_t {
 
30
    apr_pool_t *pool;
 
31
    memblock_t *memblk;
 
32
    void       *usrmem;
 
33
    apr_size_t  size;
 
34
    apr_size_t  length;
 
35
    HANDLE      hMap;
 
36
};
 
37
 
 
38
static apr_status_t shm_cleanup(void* shm)
 
39
{
 
40
    apr_status_t rv = APR_SUCCESS;
 
41
    apr_shm_t *m = shm;
 
42
    
 
43
    if (UnmapViewOfFile(m->memblk)) {
 
44
        rv = apr_get_os_error();
 
45
    }
 
46
    if (CloseHandle(m->hMap)) {
 
47
        return (rv != APR_SUCCESS) ? rv : apr_get_os_error();
 
48
    }
 
49
    /* ### Do we want to make a point of unlinking m->file here? 
 
50
     * Need to add the fname to the apr_shm_t, in that case.
 
51
     */
 
52
    return rv;
 
53
}
 
54
 
 
55
APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
 
56
                                         apr_size_t reqsize,
 
57
                                         const char *file,
 
58
                                         apr_pool_t *pool)
 
59
{
 
60
    static apr_size_t memblock = 0;
 
61
    HANDLE hMap, hFile;
 
62
    apr_status_t rv;
 
63
    apr_size_t size;
 
64
    apr_file_t *f;
 
65
    void *base;
 
66
    void *mapkey;
 
67
    DWORD err, sizelo, sizehi;
 
68
 
 
69
    reqsize += sizeof(memblock_t);
 
70
 
 
71
    if (!memblock)
 
72
    {
 
73
        SYSTEM_INFO si;
 
74
        GetSystemInfo(&si);
 
75
        memblock = si.dwAllocationGranularity;
 
76
    }   
 
77
 
 
78
    /* Compute the granualar multiple of the pagesize */
 
79
    size = memblock * (1 + (reqsize - 1) / memblock);
 
80
    sizelo = (DWORD)size;
 
81
#ifdef WIN64
 
82
    sizehi = (DWORD)(size >> 32);
 
83
#else
 
84
    sizehi = 0;
 
85
#endif
 
86
 
 
87
    if (!file) {
 
88
        /* Do Anonymous, which must be passed as a duplicated handle */
 
89
#ifndef _WIN32_WCE
 
90
        hFile = INVALID_HANDLE_VALUE;
 
91
#endif
 
92
        mapkey = NULL;
 
93
    }
 
94
    else {
 
95
        /* Do file backed, which is not an inherited handle 
 
96
         * While we could open APR_EXCL, it doesn't seem that Unix
 
97
         * ever did.  Ignore that error here, but fail later when
 
98
         * we discover we aren't the creator of the file map object.
 
99
         */
 
100
        rv = apr_file_open(&f, file,
 
101
                           APR_READ | APR_WRITE | APR_BINARY | APR_CREATE,
 
102
                           APR_UREAD | APR_UWRITE, pool);
 
103
        if ((rv != APR_SUCCESS)
 
104
                || ((rv = apr_os_file_get(&hFile, f)) != APR_SUCCESS)) {
 
105
            return rv;
 
106
        }
 
107
        rv = apr_file_trunc(f, size);
 
108
 
 
109
        /* res_name_from_filename turns file into a pseudo-name
 
110
         * without slashes or backslashes, and prepends the \global
 
111
         * prefix on Win2K and later
 
112
         */
 
113
        mapkey = res_name_from_filename(file, 1, pool);
 
114
    }
 
115
 
 
116
#if APR_HAS_UNICODE_FS
 
117
    IF_WIN_OS_IS_UNICODE
 
118
    {
 
119
        hMap = CreateFileMappingW(hFile, NULL, PAGE_READWRITE, 
 
120
                                  sizehi, sizelo, mapkey);
 
121
    }
 
122
#endif
 
123
#if APR_HAS_ANSI_FS
 
124
    ELSE_WIN_OS_IS_ANSI
 
125
    {
 
126
        hMap = CreateFileMappingA(hFile, NULL, PAGE_READWRITE, 
 
127
                                  sizehi, sizelo, mapkey);
 
128
    }
 
129
#endif
 
130
    err = apr_get_os_error();
 
131
 
 
132
    if (file) {
 
133
        apr_file_close(f);
 
134
    }
 
135
 
 
136
    if (hMap && err == ERROR_ALREADY_EXISTS) {
 
137
        CloseHandle(hMap);
 
138
        return APR_EEXIST;
 
139
    }
 
140
    if (!hMap) {
 
141
        return err;
 
142
    }
 
143
    
 
144
    base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE,
 
145
                         0, 0, size);
 
146
    if (!base) {
 
147
        CloseHandle(hMap);
 
148
        return apr_get_os_error();
 
149
    }
 
150
    
 
151
    *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
 
152
    (*m)->pool = pool;
 
153
    (*m)->hMap = hMap;
 
154
    (*m)->memblk = base;
 
155
    (*m)->size = size;
 
156
 
 
157
    (*m)->usrmem = (char*)base + sizeof(memblock_t);
 
158
    (*m)->length = reqsize - sizeof(memblock_t);;
 
159
    
 
160
    (*m)->memblk->length = (*m)->length;
 
161
    (*m)->memblk->size = (*m)->size;
 
162
 
 
163
    apr_pool_cleanup_register((*m)->pool, *m, 
 
164
                              shm_cleanup, apr_pool_cleanup_null);
 
165
    return APR_SUCCESS;
 
166
}
 
167
 
 
168
APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m) 
 
169
{
 
170
    apr_status_t rv = shm_cleanup(m);
 
171
    apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
 
172
    return rv;
 
173
}
 
174
 
 
175
APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
 
176
                                         apr_pool_t *pool)
 
177
{
 
178
    return APR_ENOTIMPL;
 
179
}
 
180
 
 
181
APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
 
182
                                         const char *file,
 
183
                                         apr_pool_t *pool)
 
184
{
 
185
    HANDLE hMap;
 
186
    void *mapkey;
 
187
    void *base;
 
188
 
 
189
    if (!file) {
 
190
        return APR_EINVAL;
 
191
    }
 
192
    else {
 
193
        /* res_name_from_filename turns file into a pseudo-name
 
194
         * without slashes or backslashes, and prepends the \global
 
195
         * prefix on Win2K and later
 
196
         */
 
197
        mapkey = res_name_from_filename(file, 1, pool);
 
198
    }
 
199
 
 
200
#if APR_HAS_UNICODE_FS
 
201
    IF_WIN_OS_IS_UNICODE
 
202
    {
 
203
#ifndef _WIN32_WCE
 
204
        hMap = OpenFileMappingW(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
 
205
#else
 
206
        /* The WCE 3.0 lacks OpenFileMapping. So we emulate one with
 
207
         * opening the existing shmem and reading its size from the header 
 
208
         */
 
209
        hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, 
 
210
                                  PAGE_READWRITE, 0, sizeof(apr_shm_t), mapkey);
 
211
#endif
 
212
    }
 
213
#endif
 
214
#if APR_HAS_ANSI_FS
 
215
    ELSE_WIN_OS_IS_ANSI
 
216
    {
 
217
        hMap = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
 
218
    }
 
219
#endif
 
220
 
 
221
    if (!hMap) {
 
222
        return apr_get_os_error();
 
223
    }
 
224
    
 
225
    base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
 
226
    if (!base) {
 
227
        CloseHandle(hMap);
 
228
        return apr_get_os_error();
 
229
    }
 
230
    
 
231
    *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
 
232
    (*m)->pool = pool;
 
233
    (*m)->memblk = base;
 
234
    /* Real (*m)->mem->size could be recovered with VirtualQuery */
 
235
    (*m)->size = (*m)->memblk->size;
 
236
#if _WIN32_WCE
 
237
    /* Reopen with real size  */
 
238
    UnmapViewOfFile(base);
 
239
    CloseHandle(hMap);
 
240
 
 
241
    hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, 
 
242
                              PAGE_READWRITE, 0, (*m)->size, mapkey);
 
243
    if (!hMap) {
 
244
        return apr_get_os_error();
 
245
    }
 
246
    base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
 
247
    if (!base) {
 
248
        CloseHandle(hMap);
 
249
        return apr_get_os_error();
 
250
    }    
 
251
#endif
 
252
    (*m)->hMap = hMap;
 
253
    (*m)->length = (*m)->memblk->length;
 
254
    (*m)->usrmem = (char*)base + sizeof(memblock_t);
 
255
    apr_pool_cleanup_register((*m)->pool, *m, 
 
256
                              shm_cleanup, apr_pool_cleanup_null);
 
257
    return APR_SUCCESS;
 
258
}
 
259
 
 
260
APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
 
261
{
 
262
    apr_status_t rv = shm_cleanup(m);
 
263
    apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
 
264
    return rv;
 
265
}
 
266
 
 
267
APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
 
268
{
 
269
    return m->usrmem;
 
270
}
 
271
 
 
272
APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
 
273
{
 
274
    return m->length;
 
275
}
 
276
 
 
277
APR_POOL_IMPLEMENT_ACCESSOR(shm)
 
278
 
 
279
APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
 
280
                                         apr_shm_t *shm)
 
281
{
 
282
    *osshm = shm->hMap;
 
283
    return APR_SUCCESS;
 
284
}
 
285
 
 
286
APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
 
287
                                         apr_os_shm_t *osshm,
 
288
                                         apr_pool_t *pool)
 
289
{
 
290
    void* base;
 
291
    base = MapViewOfFile(*osshm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
 
292
    if (!base) {
 
293
        return apr_get_os_error();
 
294
    }
 
295
    
 
296
    *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
 
297
    (*m)->pool = pool;
 
298
    (*m)->hMap = *osshm;
 
299
    (*m)->memblk = base;
 
300
    (*m)->usrmem = (char*)base + sizeof(memblock_t);
 
301
    /* Real (*m)->mem->size could be recovered with VirtualQuery */
 
302
    (*m)->size = (*m)->memblk->size;
 
303
    (*m)->length = (*m)->memblk->length;
 
304
 
 
305
    apr_pool_cleanup_register((*m)->pool, *m, 
 
306
                              shm_cleanup, apr_pool_cleanup_null);
 
307
    return APR_SUCCESS;
 
308
}    
 
309