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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_general.h"
18 #include "apr_errno.h"
19 #include "apr_file_io.h"
21 #include "apr_arch_file_io.h"
23 typedef struct memblock_t {
37 static apr_status_t shm_cleanup(void* shm)
39 apr_status_t rv = APR_SUCCESS;
42 if (UnmapViewOfFile(m->memblk)) {
43 rv = apr_get_os_error();
45 if (CloseHandle(m->hMap)) {
46 return (rv != APR_SUCCESS) ? rv : apr_get_os_error();
48 /* ### Do we want to make a point of unlinking m->file here?
49 * Need to add the fname to the apr_shm_t, in that case.
54 APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
59 static apr_size_t memblock = 0;
68 reqsize += sizeof(memblock_t);
74 memblock = si.dwAllocationGranularity;
77 /* Compute the granualar multiple of the pagesize */
78 size = memblock * (1 + (reqsize - 1) / memblock);
81 /* Do Anonymous, which must be passed as a duplicated handle */
83 hFile = INVALID_HANDLE_VALUE;
88 /* Do file backed, which is not an inherited handle
89 * While we could open APR_EXCL, it doesn't seem that Unix
90 * ever did. Ignore that error here, but fail later when
91 * we discover we aren't the creator of the file map object.
93 rv = apr_file_open(&f, file,
94 APR_READ | APR_WRITE | APR_BINARY | APR_CREATE,
95 APR_UREAD | APR_UWRITE, pool);
96 if ((rv != APR_SUCCESS)
97 || ((rv = apr_os_file_get(&hFile, f)) != APR_SUCCESS)) {
100 rv = apr_file_trunc(f, size);
102 /* res_name_from_filename turns file into a pseudo-name
103 * without slashes or backslashes, and prepends the \global
104 * prefix on Win2K and later
106 mapkey = res_name_from_filename(file, 1, pool);
109 #if APR_HAS_UNICODE_FS
112 hMap = CreateFileMappingW(hFile, NULL, PAGE_READWRITE, 0, size, mapkey);
118 hMap = CreateFileMappingA(hFile, NULL, PAGE_READWRITE, 0, size, mapkey);
121 err = apr_get_os_error();
127 if (hMap && APR_STATUS_IS_EEXIST(err)) {
135 base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE,
139 return apr_get_os_error();
142 *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
148 (*m)->usrmem = (char*)base + sizeof(memblock_t);
149 (*m)->length = reqsize - sizeof(memblock_t);;
151 (*m)->memblk->length = (*m)->length;
152 (*m)->memblk->size = (*m)->size;
154 apr_pool_cleanup_register((*m)->pool, *m,
155 shm_cleanup, apr_pool_cleanup_null);
159 APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
161 apr_status_t rv = shm_cleanup(m);
162 apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
166 APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
178 /* res_name_from_filename turns file into a pseudo-name
179 * without slashes or backslashes, and prepends the \global
180 * prefix on Win2K and later
182 mapkey = res_name_from_filename(file, 1, pool);
185 #if APR_HAS_UNICODE_FS
189 hMap = OpenFileMappingW(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
191 /* The WCE 3.0 lacks OpenFileMapping. So we emulate one with
192 * opening the existing shmem and reading its size from the header
194 hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
195 PAGE_READWRITE, 0, sizeof(apr_shm_t), mapkey);
202 hMap = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
207 return apr_get_os_error();
210 base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
213 return apr_get_os_error();
216 *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
219 /* Real (*m)->mem->size could be recovered with VirtualQuery */
220 (*m)->size = (*m)->memblk->size;
222 /* Reopen with real size */
223 UnmapViewOfFile(base);
226 hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
227 PAGE_READWRITE, 0, (*m)->size, mapkey);
229 return apr_get_os_error();
231 base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
234 return apr_get_os_error();
238 (*m)->length = (*m)->memblk->length;
239 (*m)->usrmem = (char*)base + sizeof(memblock_t);
240 apr_pool_cleanup_register((*m)->pool, *m,
241 shm_cleanup, apr_pool_cleanup_null);
245 APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
247 apr_status_t rv = shm_cleanup(m);
248 apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
252 APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
257 APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
262 APR_POOL_IMPLEMENT_ACCESSOR(shm)
264 APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
271 APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
276 base = MapViewOfFile(*osshm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
278 return apr_get_os_error();
281 *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
285 (*m)->usrmem = (char*)base + sizeof(memblock_t);
286 /* Real (*m)->mem->size could be recovered with VirtualQuery */
287 (*m)->size = (*m)->memblk->size;
288 (*m)->length = (*m)->memblk->length;
290 apr_pool_cleanup_register((*m)->pool, *m,
291 shm_cleanup, apr_pool_cleanup_null);