diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:23:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:23:09 +0000 |
commit | 30d479c28c831a0d4f1fdb54a9e346b0fc176be1 (patch) | |
tree | aa35d7414ce9f1326abf6f723f6dfa5b0aa08b1d /shmem | |
parent | Initial commit. (diff) | |
download | apr-30d479c28c831a0d4f1fdb54a9e346b0fc176be1.tar.xz apr-30d479c28c831a0d4f1fdb54a9e346b0fc176be1.zip |
Adding upstream version 1.7.2.upstream/1.7.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'shmem')
-rw-r--r-- | shmem/beos/shm.c | 195 | ||||
-rw-r--r-- | shmem/os2/shm.c | 168 | ||||
-rw-r--r-- | shmem/unix/shm.c | 731 | ||||
-rw-r--r-- | shmem/win32/shm.c | 452 |
4 files changed, 1546 insertions, 0 deletions
diff --git a/shmem/beos/shm.c b/shmem/beos/shm.c new file mode 100644 index 0000000..fcd7c84 --- /dev/null +++ b/shmem/beos/shm.c @@ -0,0 +1,195 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr_general.h" +#include "apr_shm.h" +#include "apr_errno.h" +#include "apr_lib.h" +#include "apr_strings.h" +#include <stdio.h> +#include <stdlib.h> +#include <kernel/OS.h> +#include "apr_portable.h" + +struct apr_shm_t { + apr_pool_t *pool; + void *memblock; + void *ptr; + apr_size_t reqsize; + apr_size_t avail; + area_id aid; +}; + +APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m, + apr_size_t reqsize, + const char *filename, + apr_pool_t *p) +{ + apr_size_t pagesize; + area_id newid; + char *addr; + char shname[B_OS_NAME_LENGTH]; + + (*m) = (apr_shm_t *)apr_pcalloc(p, sizeof(apr_shm_t)); + /* we MUST allocate in pages, so calculate how big an area we need... */ + pagesize = ((reqsize + B_PAGE_SIZE - 1) / B_PAGE_SIZE) * B_PAGE_SIZE; + + if (!filename) { + int num = 0; + snprintf(shname, B_OS_NAME_LENGTH, "apr_shmem_%ld", find_thread(NULL)); + while (find_area(shname) >= 0) + snprintf(shname, B_OS_NAME_LENGTH, "apr_shmem_%ld_%d", + find_thread(NULL), num++); + } + newid = create_area(filename ? filename : shname, + (void*)&addr, B_ANY_ADDRESS, + pagesize, B_LAZY_LOCK, B_READ_AREA|B_WRITE_AREA); + + if (newid < 0) + return errno; + + (*m)->pool = p; + (*m)->aid = newid; + (*m)->memblock = addr; + (*m)->ptr = (void*)addr; + (*m)->avail = pagesize; /* record how big an area we actually created... */ + (*m)->reqsize = reqsize; + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m, + apr_size_t reqsize, + const char *filename, + apr_pool_t *p, + apr_int32_t flags) +{ + return apr_shm_create(m, reqsize, filename, p); +} + +APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m) +{ + delete_area(m->aid); + m->avail = 0; + m->memblock = NULL; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename, + apr_pool_t *pool) +{ + area_id deleteme = find_area(filename); + + if (deleteme == B_NAME_NOT_FOUND) + return APR_EINVAL; + + delete_area(deleteme); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_delete(apr_shm_t *m) +{ + if (m->filename) { + return apr_shm_remove(m->filename, m->pool); + } + else { + return APR_ENOTIMPL; + } +} + +APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m, + const char *filename, + apr_pool_t *pool) +{ + area_info ai; + thread_info ti; + apr_shm_t *new_m; + area_id deleteme = find_area(filename); + + if (deleteme == B_NAME_NOT_FOUND) + return APR_EINVAL; + + new_m = (apr_shm_t*)apr_palloc(pool, sizeof(apr_shm_t*)); + if (new_m == NULL) + return APR_ENOMEM; + new_m->pool = pool; + + get_area_info(deleteme, &ai); + get_thread_info(find_thread(NULL), &ti); + + if (ti.team != ai.team) { + area_id narea; + + narea = clone_area(ai.name, &(ai.address), B_CLONE_ADDRESS, + B_READ_AREA|B_WRITE_AREA, ai.area); + + if (narea < B_OK) + return narea; + + get_area_info(narea, &ai); + new_m->aid = narea; + new_m->memblock = ai.address; + new_m->ptr = (void*)ai.address; + new_m->avail = ai.size; + new_m->reqsize = ai.size; + } + + (*m) = new_m; + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m, + const char *filename, + apr_pool_t *pool, + apr_int32_t flags) +{ + return apr_shm_attach(m, filename, pool); +} + +APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m) +{ + delete_area(m->aid); + return APR_SUCCESS; +} + +APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m) +{ + return m->memblock; +} + +APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m) +{ + return m->reqsize; +} + +APR_PERMS_SET_ENOTIMPL(shm) + +APR_POOL_IMPLEMENT_ACCESSOR(shm) + +APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm, + apr_shm_t *shm) +{ + return APR_ENOTIMPL; +} + +APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m, + apr_os_shm_t *osshm, + apr_pool_t *pool) +{ + return APR_ENOTIMPL; +} + diff --git a/shmem/os2/shm.c b/shmem/os2/shm.c new file mode 100644 index 0000000..091fe51 --- /dev/null +++ b/shmem/os2/shm.c @@ -0,0 +1,168 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr_general.h" +#include "apr_shm.h" +#include "apr_errno.h" +#include "apr_lib.h" +#include "apr_strings.h" +#include "apr_portable.h" + +struct apr_shm_t { + apr_pool_t *pool; + void *memblock; +}; + +APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m, + apr_size_t reqsize, + const char *filename, + apr_pool_t *pool) +{ + int rc; + apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t)); + char *name = NULL; + ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE; + + newm->pool = pool; + + if (filename) { + name = apr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL); + } + + if (name == NULL) { + flags |= OBJ_GETTABLE; + } + + rc = DosAllocSharedMem(&(newm->memblock), name, reqsize, flags); + + if (rc) { + return APR_OS2_STATUS(rc); + } + + *m = newm; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m, + apr_size_t reqsize, + const char *filename, + apr_pool_t *p, + apr_int32_t flags) +{ + return apr_shm_create(m, reqsize, filename, p); +} + +APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m) +{ + DosFreeMem(m->memblock); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename, + apr_pool_t *pool) +{ + return APR_ENOTIMPL; +} + +APR_DECLARE(apr_status_t) apr_shm_delete(apr_shm_t *m) +{ + return APR_ENOTIMPL; +} + +APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m, + const char *filename, + apr_pool_t *pool) +{ + int rc; + apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t)); + char *name = NULL; + ULONG flags = PAG_READ|PAG_WRITE; + + newm->pool = pool; + name = apr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL); + + rc = DosGetNamedSharedMem(&(newm->memblock), name, flags); + + if (rc) { + return APR_FROM_OS_ERROR(rc); + } + + *m = newm; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m, + const char *filename, + apr_pool_t *pool, + apr_int32_t flags) +{ + return apr_shm_attach(m, filename, pool); +} + +APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m) +{ + int rc = 0; + + if (m->memblock) { + rc = DosFreeMem(m->memblock); + } + + return APR_FROM_OS_ERROR(rc); +} + +APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m) +{ + return m->memblock; +} + +APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m) +{ + ULONG flags, size = 0x1000000; + DosQueryMem(m->memblock, &size, &flags); + return size; +} + +APR_PERMS_SET_ENOTIMPL(shm) + +APR_POOL_IMPLEMENT_ACCESSOR(shm) + +APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm, + apr_shm_t *shm) +{ + *osshm = shm->memblock; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m, + apr_os_shm_t *osshm, + apr_pool_t *pool) +{ + int rc; + apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t)); + ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE; + + newm->pool = pool; + + rc = DosGetSharedMem(&(newm->memblock), flags); + + if (rc) { + return APR_FROM_OS_ERROR(rc); + } + + *m = newm; + return APR_SUCCESS; +} + diff --git a/shmem/unix/shm.c b/shmem/unix/shm.c new file mode 100644 index 0000000..f99afec --- /dev/null +++ b/shmem/unix/shm.c @@ -0,0 +1,731 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr_arch_shm.h" +#include "apr_arch_file_io.h" + +#include "apr_general.h" +#include "apr_errno.h" +#include "apr_user.h" +#include "apr_strings.h" +#include "apr_hash.h" + +#if APR_USE_SHMEM_MMAP_SHM +/* + * For portable use, a shared memory object should be identified by a name of + * the form /somename; that is, a null-terminated string of up to NAME_MAX + * (i.e., 255) characters consisting of an initial slash, followed by one or + * more characters, none of which are slashes. + */ +#ifndef NAME_MAX +#define NAME_MAX 255 +#endif + +/* See proc_mutex.c and sem_open for the reason for all this! */ +static unsigned int rshash (const char *p) { + /* hash function from Robert Sedgwicks 'Algorithms in C' book */ + unsigned int b = 378551; + unsigned int a = 63689; + unsigned int retval = 0; + + for( ; *p; p++) { + retval = retval * a + (*p); + a *= b; + } + + return retval; +} + +static const char *make_shm_open_safe_name(const char *filename, + apr_pool_t *pool) +{ + apr_ssize_t flen; + unsigned int h1, h2; + + if (filename == NULL) { + return NULL; + } + + flen = strlen(filename); + h1 = (apr_hashfunc_default(filename, &flen) & 0xffffffff); + h2 = (rshash(filename) & 0xffffffff); + return apr_psprintf(pool, "/ShM.%xH%x", h1, h2); + +} +#endif + +#if APR_USE_SHMEM_SHMGET +static key_t our_ftok(const char *filename) +{ + /* to help avoid collisions while still using + * an easily recreated proj_id */ + apr_ssize_t slen = strlen(filename); + return ftok(filename, + (int)apr_hashfunc_default(filename, &slen)); +} +#endif + +static apr_status_t shm_cleanup_owner(void *m_) +{ + apr_shm_t *m = (apr_shm_t *)m_; + + /* anonymous shared memory */ + if (m->filename == NULL) { +#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON + if (munmap(m->base, m->realsize) == -1) { + return errno; + } + return APR_SUCCESS; +#elif APR_USE_SHMEM_SHMGET_ANON + if (shmdt(m->base) == -1) { + return errno; + } + /* This segment will automatically remove itself after all + * references have detached. */ + return APR_SUCCESS; +#endif + } + + /* name-based shared memory */ + else { +#if APR_USE_SHMEM_MMAP_TMP + if (munmap(m->base, m->realsize) == -1) { + return errno; + } + if (access(m->filename, F_OK)) { + return APR_SUCCESS; + } + else { + return apr_file_remove(m->filename, m->pool); + } +#elif APR_USE_SHMEM_MMAP_SHM + if (munmap(m->base, m->realsize) == -1) { + return errno; + } + if (shm_unlink(make_shm_open_safe_name(m->filename, m->pool)) == -1 && errno != ENOENT) { + return errno; + } + return APR_SUCCESS; +#elif APR_USE_SHMEM_SHMGET + /* Indicate that the segment is to be destroyed as soon + * as all processes have detached. This also disallows any + * new attachments to the segment. */ + if (shmctl(m->shmid, IPC_RMID, NULL) == -1 && errno != EINVAL) { + return errno; + } + if (shmdt(m->base) == -1) { + return errno; + } + if (access(m->filename, F_OK)) { + return APR_SUCCESS; + } + else { + return apr_file_remove(m->filename, m->pool); + } +#else + return APR_ENOTIMPL; +#endif + } +} + +APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m, + apr_size_t reqsize, + const char *filename, + apr_pool_t *pool) +{ + apr_shm_t *new_m; + apr_status_t status; +#if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON + struct shmid_ds shmbuf; + apr_uid_t uid; + apr_gid_t gid; +#endif +#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || \ + APR_USE_SHMEM_MMAP_ZERO + int tmpfd; +#endif +#if APR_USE_SHMEM_SHMGET + apr_size_t nbytes; +#endif +#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_SHMGET || \ + APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM + apr_file_t *file; /* file where metadata is stored */ +#endif + + /* Check if they want anonymous or name-based shared memory */ + if (filename == NULL) { +#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON + new_m = apr_palloc(pool, sizeof(apr_shm_t)); + new_m->pool = pool; + new_m->reqsize = reqsize; + new_m->realsize = reqsize + + APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */ + new_m->filename = NULL; + +#if APR_USE_SHMEM_MMAP_ZERO + status = apr_file_open(&file, "/dev/zero", APR_READ | APR_WRITE, + APR_OS_DEFAULT, pool); + if (status != APR_SUCCESS) { + return status; + } + status = apr_os_file_get(&tmpfd, file); + if (status != APR_SUCCESS) { + return status; + } + + new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE, + MAP_SHARED, tmpfd, 0); + if (new_m->base == (void *)MAP_FAILED) { + return errno; + } + + status = apr_file_close(file); + if (status != APR_SUCCESS) { + return status; + } + + /* store the real size in the metadata */ + *(apr_size_t*)(new_m->base) = new_m->realsize; + /* metadata isn't usable */ + new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t)); + + apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner, + apr_pool_cleanup_null); + *m = new_m; + return APR_SUCCESS; + +#elif APR_USE_SHMEM_MMAP_ANON + new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE, + MAP_ANON|MAP_SHARED, -1, 0); + if (new_m->base == (void *)MAP_FAILED) { + return errno; + } + + /* store the real size in the metadata */ + *(apr_size_t*)(new_m->base) = new_m->realsize; + /* metadata isn't usable */ + new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t)); + + apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner, + apr_pool_cleanup_null); + *m = new_m; + return APR_SUCCESS; + +#endif /* APR_USE_SHMEM_MMAP_ZERO */ +#elif APR_USE_SHMEM_SHMGET_ANON + new_m = apr_palloc(pool, sizeof(apr_shm_t)); + new_m->pool = pool; + new_m->reqsize = reqsize; + new_m->realsize = reqsize; + new_m->filename = NULL; + new_m->shmkey = IPC_PRIVATE; + if ((new_m->shmid = shmget(new_m->shmkey, new_m->realsize, + SHM_R | SHM_W | IPC_CREAT)) < 0) { + return errno; + } + + if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) { + return errno; + } + new_m->usable = new_m->base; + + if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) { + return errno; + } + apr_uid_current(&uid, &gid, pool); + shmbuf.shm_perm.uid = uid; + shmbuf.shm_perm.gid = gid; + if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) { + return errno; + } + + /* Remove the segment once use count hits zero. + * We will not attach to this segment again, since it is + * anonymous memory, so it is ok to mark it for deletion. + */ + if (shmctl(new_m->shmid, IPC_RMID, NULL) == -1) { + return errno; + } + + apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner, + apr_pool_cleanup_null); + *m = new_m; + return APR_SUCCESS; +#else + /* It is an error if they want anonymous memory but we don't have it. */ + return APR_ENOTIMPL; /* requested anonymous but we don't have it */ +#endif + } + + /* Name-based shared memory */ + else { + new_m = apr_palloc(pool, sizeof(apr_shm_t)); + new_m->pool = pool; + new_m->reqsize = reqsize; + new_m->filename = apr_pstrdup(pool, filename); +#if APR_USE_SHMEM_MMAP_SHM + const char *shm_name = make_shm_open_safe_name(filename, pool); +#endif +#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM + new_m->realsize = reqsize + + APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */ + /* FIXME: Ignore error for now. * + * status = apr_file_remove(file, pool);*/ + status = APR_SUCCESS; + +#if APR_USE_SHMEM_MMAP_TMP + /* FIXME: Is APR_OS_DEFAULT sufficient? */ + status = apr_file_open(&file, filename, + APR_READ | APR_WRITE | APR_CREATE | APR_EXCL, + APR_OS_DEFAULT, pool); + if (status != APR_SUCCESS) { + return status; + } + + status = apr_os_file_get(&tmpfd, file); + if (status != APR_SUCCESS) { + apr_file_close(file); /* ignore errors, we're failing */ + apr_file_remove(new_m->filename, new_m->pool); + return status; + } + + status = apr_file_trunc(file, new_m->realsize); + if (status != APR_SUCCESS && status != APR_ESPIPE) { + apr_file_close(file); /* ignore errors, we're failing */ + apr_file_remove(new_m->filename, new_m->pool); + return status; + } + + new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE, + MAP_SHARED, tmpfd, 0); + /* FIXME: check for errors */ + + status = apr_file_close(file); + if (status != APR_SUCCESS) { + return status; + } +#endif /* APR_USE_SHMEM_MMAP_TMP */ +#if APR_USE_SHMEM_MMAP_SHM + /* FIXME: SysV uses 0600... should we? */ + tmpfd = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, 0644); + if (tmpfd == -1) { + return errno; + } + + status = apr_os_file_put(&file, &tmpfd, + APR_READ | APR_WRITE | APR_CREATE | APR_EXCL, + pool); + if (status != APR_SUCCESS) { + return status; + } + + status = apr_file_trunc(file, new_m->realsize); + if (status != APR_SUCCESS && status != APR_ESPIPE) { + shm_unlink(shm_name); /* we're failing, remove the object */ + return status; + } + new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE, + MAP_SHARED, tmpfd, 0); + + /* FIXME: check for errors */ + + status = apr_file_close(file); + if (status != APR_SUCCESS) { + return status; + } +#endif /* APR_USE_SHMEM_MMAP_SHM */ + + /* store the real size in the metadata */ + *(apr_size_t*)(new_m->base) = new_m->realsize; + /* metadata isn't usable */ + new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t)); + + apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner, + apr_pool_cleanup_null); + *m = new_m; + return APR_SUCCESS; + +#elif APR_USE_SHMEM_SHMGET + new_m->realsize = reqsize; + + /* FIXME: APR_OS_DEFAULT is too permissive, switch to 600 I think. */ + status = apr_file_open(&file, filename, + APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL, + APR_OS_DEFAULT, pool); + if (status != APR_SUCCESS) { + return status; + } + + /* ftok() (on solaris at least) requires that the file actually + * exist before calling ftok(). */ + new_m->shmkey = our_ftok(filename); + if (new_m->shmkey == (key_t)-1) { + apr_file_close(file); + return errno; + } + + if ((new_m->shmid = shmget(new_m->shmkey, new_m->realsize, + SHM_R | SHM_W | IPC_CREAT | IPC_EXCL)) < 0) { + apr_file_close(file); + return errno; + } + + if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) { + apr_file_close(file); + return errno; + } + new_m->usable = new_m->base; + + if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) { + apr_file_close(file); + return errno; + } + apr_uid_current(&uid, &gid, pool); + shmbuf.shm_perm.uid = uid; + shmbuf.shm_perm.gid = gid; + if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) { + apr_file_close(file); + return errno; + } + + nbytes = sizeof(reqsize); + status = apr_file_write(file, (const void *)&reqsize, + &nbytes); + if (status != APR_SUCCESS) { + apr_file_close(file); + return status; + } + status = apr_file_close(file); + if (status != APR_SUCCESS) { + return status; + } + + apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner, + apr_pool_cleanup_null); + *m = new_m; + return APR_SUCCESS; + +#else + return APR_ENOTIMPL; +#endif + } +} + +APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m, + apr_size_t reqsize, + const char *filename, + apr_pool_t *p, + apr_int32_t flags) +{ + return apr_shm_create(m, reqsize, filename, p); +} + +APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename, + apr_pool_t *pool) +{ +#if APR_USE_SHMEM_SHMGET + apr_status_t status; + apr_file_t *file; + key_t shmkey; + int shmid; +#endif + +#if APR_USE_SHMEM_MMAP_TMP + return apr_file_remove(filename, pool); +#elif APR_USE_SHMEM_MMAP_SHM + const char *shm_name = make_shm_open_safe_name(filename, pool); + if (shm_unlink(shm_name) == -1) { + return errno; + } + return APR_SUCCESS; +#elif APR_USE_SHMEM_SHMGET + /* Presume that the file already exists; just open for writing */ + status = apr_file_open(&file, filename, APR_FOPEN_WRITE, + APR_OS_DEFAULT, pool); + if (status) { + return status; + } + + /* ftok() (on solaris at least) requires that the file actually + * exist before calling ftok(). */ + shmkey = our_ftok(filename); + if (shmkey == (key_t)-1) { + goto shm_remove_failed; + } + + apr_file_close(file); + + if ((shmid = shmget(shmkey, 0, SHM_R | SHM_W)) < 0) { + goto shm_remove_failed; + } + + /* Indicate that the segment is to be destroyed as soon + * as all processes have detached. This also disallows any + * new attachments to the segment. */ + if (shmctl(shmid, IPC_RMID, NULL) == -1) { + goto shm_remove_failed; + } + return apr_file_remove(filename, pool); + +shm_remove_failed: + status = errno; + /* ensure the file has been removed anyway. */ + apr_file_remove(filename, pool); + return status; +#else + + /* No support for anonymous shm */ + return APR_ENOTIMPL; +#endif +} + +APR_DECLARE(apr_status_t) apr_shm_delete(apr_shm_t *m) +{ + if (m->filename) { + return apr_shm_remove(m->filename, m->pool); + } + else { + return APR_ENOTIMPL; + } +} + +APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m) +{ + return apr_pool_cleanup_run(m->pool, m, shm_cleanup_owner); +} + +static apr_status_t shm_cleanup_attach(void *m_) +{ + apr_shm_t *m = (apr_shm_t *)m_; + + if (m->filename == NULL) { + /* It doesn't make sense to detach from an anonymous memory segment. */ + return APR_EINVAL; + } + else { +#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM + if (munmap(m->base, m->realsize) == -1) { + return errno; + } + return APR_SUCCESS; +#elif APR_USE_SHMEM_SHMGET + if (shmdt(m->base) == -1) { + return errno; + } + return APR_SUCCESS; +#else + return APR_ENOTIMPL; +#endif + } +} + +APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m, + const char *filename, + apr_pool_t *pool) +{ + if (filename == NULL) { + /* It doesn't make sense to attach to a segment if you don't know + * the filename. */ + return APR_EINVAL; + } + else { +#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM + apr_shm_t *new_m; + apr_status_t status; + int tmpfd; + apr_file_t *file; /* file where metadata is stored */ + apr_size_t nbytes; + + new_m = apr_palloc(pool, sizeof(apr_shm_t)); + new_m->pool = pool; + new_m->filename = apr_pstrdup(pool, filename); +#if APR_USE_SHMEM_MMAP_SHM + const char *shm_name = make_shm_open_safe_name(filename, pool); + + /* FIXME: SysV uses 0600... should we? */ + tmpfd = shm_open(shm_name, O_RDWR, 0644); + if (tmpfd == -1) { + return errno; + } + + status = apr_os_file_put(&file, &tmpfd, + APR_READ | APR_WRITE, + pool); + if (status != APR_SUCCESS) { + return status; + } + +#elif APR_USE_SHMEM_MMAP_TMP + status = apr_file_open(&file, filename, + APR_READ | APR_WRITE, + APR_OS_DEFAULT, pool); + if (status != APR_SUCCESS) { + return status; + } + status = apr_os_file_get(&tmpfd, file); + if (status != APR_SUCCESS) { + return status; + } +#else + return APR_ENOTIMPL; +#endif + + nbytes = sizeof(new_m->realsize); + status = apr_file_read(file, (void *)&(new_m->realsize), + &nbytes); + if (status != APR_SUCCESS) { + return status; + } + + status = apr_os_file_get(&tmpfd, file); + if (status != APR_SUCCESS) { + apr_file_close(file); /* ignore errors, we're failing */ + apr_file_remove(new_m->filename, new_m->pool); + return status; + } + + new_m->reqsize = new_m->realsize - sizeof(apr_size_t); + + new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE, + MAP_SHARED, tmpfd, 0); + /* FIXME: check for errors */ + + status = apr_file_close(file); + if (status != APR_SUCCESS) { + return status; + } + + /* metadata isn't part of the usable segment */ + new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t)); + + apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach, + apr_pool_cleanup_null); + *m = new_m; + return APR_SUCCESS; + +#elif APR_USE_SHMEM_SHMGET + apr_shm_t *new_m; + apr_status_t status; + apr_file_t *file; /* file where metadata is stored */ + apr_size_t nbytes; + + new_m = apr_palloc(pool, sizeof(apr_shm_t)); + + status = apr_file_open(&file, filename, + APR_FOPEN_READ, APR_OS_DEFAULT, pool); + if (status != APR_SUCCESS) { + return status; + } + + nbytes = sizeof(new_m->reqsize); + status = apr_file_read(file, (void *)&(new_m->reqsize), + &nbytes); + if (status != APR_SUCCESS) { + return status; + } + status = apr_file_close(file); + if (status != APR_SUCCESS) { + return status; + } + + new_m->filename = apr_pstrdup(pool, filename); + new_m->pool = pool; + new_m->shmkey = our_ftok(filename); + if (new_m->shmkey == (key_t)-1) { + return errno; + } + if ((new_m->shmid = shmget(new_m->shmkey, 0, SHM_R | SHM_W)) == -1) { + return errno; + } + if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) { + return errno; + } + new_m->usable = new_m->base; + new_m->realsize = new_m->reqsize; + + apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach, + apr_pool_cleanup_null); + *m = new_m; + return APR_SUCCESS; + +#else + return APR_ENOTIMPL; +#endif + } +} + +APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m, + const char *filename, + apr_pool_t *pool, + apr_int32_t flags) +{ + return apr_shm_attach(m, filename, pool); +} + +APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m) +{ + apr_status_t rv = shm_cleanup_attach(m); + apr_pool_cleanup_kill(m->pool, m, shm_cleanup_attach); + return rv; +} + +APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m) +{ + return m->usable; +} + +APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m) +{ + return m->reqsize; +} + +APR_PERMS_SET_IMPLEMENT(shm) +{ +#if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON + struct shmid_ds shmbuf; + int shmid; + apr_shm_t *m = (apr_shm_t *)theshm; + + if ((shmid = shmget(m->shmkey, 0, SHM_R | SHM_W)) == -1) { + return errno; + } + shmbuf.shm_perm.uid = uid; + shmbuf.shm_perm.gid = gid; + shmbuf.shm_perm.mode = apr_unix_perms2mode(perms); + if (shmctl(shmid, IPC_SET, &shmbuf) == -1) { + return errno; + } + return APR_SUCCESS; +#else + return APR_ENOTIMPL; +#endif +} + +APR_POOL_IMPLEMENT_ACCESSOR(shm) + +APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm, + apr_shm_t *shm) +{ + return APR_ENOTIMPL; +} + +APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m, + apr_os_shm_t *osshm, + apr_pool_t *pool) +{ + return APR_ENOTIMPL; +} + diff --git a/shmem/win32/shm.c b/shmem/win32/shm.c new file mode 100644 index 0000000..8d8848e --- /dev/null +++ b/shmem/win32/shm.c @@ -0,0 +1,452 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "apr_general.h" +#include "apr_errno.h" +#include "apr_file_io.h" +#include "apr_shm.h" +#include "apr_strings.h" +#include "apr_arch_file_io.h" +#include "limits.h" + +typedef struct memblock_t { + apr_size_t size; + apr_size_t length; +} memblock_t; + +struct apr_shm_t { + apr_pool_t *pool; + memblock_t *memblk; + void *usrmem; + apr_size_t size; + apr_size_t length; + HANDLE hMap; + const char *filename; +}; + +static apr_status_t shm_cleanup(void* shm) +{ + apr_status_t rv = APR_SUCCESS; + apr_shm_t *m = shm; + + if (!UnmapViewOfFile(m->memblk)) { + rv = apr_get_os_error(); + } + if (!CloseHandle(m->hMap)) { + rv = rv != APR_SUCCESS ? rv : apr_get_os_error(); + } + if (m->filename) { + /* Remove file if file backed */ + apr_status_t rc = apr_file_remove(m->filename, m->pool); + rv = rv != APR_SUCCESS ? rv : rc; + } + return rv; +} + +/* See if the caller is able to create a map in the global namespace by + * checking if the SE_CREATE_GLOBAL_NAME privilege is enabled. + * + * Prior to APR 1.5.0, named shared memory segments were always created + * in the global segment. However, with recent versions of Windows this + * fails for unprivileged processes. Thus, with older APR, named shared + * memory segments can't be created by unprivileged processes on newer + * Windows. + * + * By checking if the caller has the privilege, shm APIs can decide + * whether to use the Global or Local namespace. + * + * If running on an SDK without the required API definitions *OR* + * some processing failure occurs trying to check the privilege, fall + * back to earlier behavior -- always try to use the Global namespace. + */ +#ifdef SE_CREATE_GLOBAL_NAME +static int can_create_global_maps(void) +{ + BOOL ok, has_priv; + LUID priv_id; + PRIVILEGE_SET privs; + HANDLE hToken; + + ok = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken); + if (!ok && GetLastError() == ERROR_NO_TOKEN) { + /* no thread-specific access token, so try to get process access token + */ + ok = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken); + } + + if (ok) { + ok = LookupPrivilegeValue(NULL, SE_CREATE_GLOBAL_NAME, &priv_id); + } + + if (ok) { + privs.PrivilegeCount = 1; + privs.Control = PRIVILEGE_SET_ALL_NECESSARY; + privs.Privilege[0].Luid = priv_id; + privs.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED; + ok = PrivilegeCheck(hToken, &privs, &has_priv); + } + + if (ok && !has_priv) { + return 0; + } + else { + return 1; + } +} +#else /* SE_CREATE_GLOBAL_NAME */ +/* SDK definitions missing */ +static int can_create_global_maps(void) +{ + return 1; +} +#endif /* SE_CREATE_GLOBAL_NAME */ + +APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m, + apr_size_t reqsize, + const char *file, + apr_pool_t *pool, + apr_int32_t flags) +{ + static apr_size_t memblock = 0; + HANDLE hMap, hFile; + apr_status_t rv; + apr_size_t size; + apr_file_t *f; + void *base; + void *mapkey; + DWORD err, sizelo, sizehi; + + reqsize += sizeof(memblock_t); + + if (!memblock) + { + SYSTEM_INFO si; + GetSystemInfo(&si); + memblock = si.dwAllocationGranularity; + } + + /* Compute the granualar multiple of the pagesize */ + size = memblock * (1 + (reqsize - 1) / memblock); + sizelo = (DWORD)size; +#ifdef _WIN64 + sizehi = (DWORD)(size >> 32); +#else + sizehi = 0; +#endif + + if (!file) { + /* Do Anonymous, which must be passed as a duplicated handle */ +#ifndef _WIN32_WCE + hFile = INVALID_HANDLE_VALUE; +#endif + mapkey = NULL; + } + else { + int global; + + /* Do file backed, which is not an inherited handle + * While we could open APR_EXCL, it doesn't seem that Unix + * ever did. Ignore that error here, but fail later when + * we discover we aren't the creator of the file map object. + */ + rv = apr_file_open(&f, file, + APR_READ | APR_WRITE | APR_BINARY | APR_CREATE, + APR_UREAD | APR_UWRITE, pool); + if ((rv != APR_SUCCESS) + || ((rv = apr_os_file_get(&hFile, f)) != APR_SUCCESS)) { + return rv; + } + rv = apr_file_trunc(f, size); + + /* res_name_from_filename turns file into a pseudo-name + * without slashes or backslashes, and prepends the \global + * or \local prefix on Win2K and later + */ + if (flags & APR_SHM_NS_GLOBAL) { + global = 1; + } + else if (flags & APR_SHM_NS_LOCAL) { + global = 0; + } + else { + global = can_create_global_maps(); + } + mapkey = res_name_from_filename(file, global, pool); + } + +#if APR_HAS_UNICODE_FS + IF_WIN_OS_IS_UNICODE + { + hMap = CreateFileMappingW(hFile, NULL, PAGE_READWRITE, + sizehi, sizelo, mapkey); + } +#endif +#if APR_HAS_ANSI_FS + ELSE_WIN_OS_IS_ANSI + { + hMap = CreateFileMappingA(hFile, NULL, PAGE_READWRITE, + sizehi, sizelo, mapkey); + } +#endif + err = apr_get_os_error(); + + if (file) { + apr_file_close(f); + } + + if (hMap && APR_STATUS_IS_EEXIST(err)) { + CloseHandle(hMap); + return APR_EEXIST; + } + if (!hMap) { + return err; + } + + base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, + 0, 0, size); + if (!base) { + CloseHandle(hMap); + return apr_get_os_error(); + } + + *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t)); + (*m)->pool = pool; + (*m)->hMap = hMap; + (*m)->memblk = base; + (*m)->size = size; + + (*m)->usrmem = (char*)base + sizeof(memblock_t); + (*m)->length = reqsize - sizeof(memblock_t);; + + (*m)->memblk->length = (*m)->length; + (*m)->memblk->size = (*m)->size; + (*m)->filename = file ? apr_pstrdup(pool, file) : NULL; + + apr_pool_cleanup_register((*m)->pool, *m, + shm_cleanup, apr_pool_cleanup_null); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m, + apr_size_t reqsize, + const char *file, + apr_pool_t *pool) +{ + return apr_shm_create_ex(m, reqsize, file, pool, 0); +} + +APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m) +{ + apr_status_t rv = shm_cleanup(m); + apr_pool_cleanup_kill(m->pool, m, shm_cleanup); + return rv; +} + +APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename, + apr_pool_t *pool) +{ + return apr_file_remove(filename, pool); +} + +APR_DECLARE(apr_status_t) apr_shm_delete(apr_shm_t *m) +{ + if (m->filename) { + return apr_shm_remove(m->filename, m->pool); + } + else { + return APR_ENOTIMPL; + } +} + +static apr_status_t shm_attach_internal(apr_shm_t **m, + const char *file, + apr_pool_t *pool, + int global) +{ + HANDLE hMap; + void *mapkey; + void *base; + + /* res_name_from_filename turns file into a pseudo-name + * without slashes or backslashes, and prepends the \global + * or local prefix on Win2K and later + */ + mapkey = res_name_from_filename(file, global, pool); + +#if APR_HAS_UNICODE_FS + IF_WIN_OS_IS_UNICODE + { +#ifndef _WIN32_WCE + hMap = OpenFileMappingW(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey); +#else + /* The WCE 3.0 lacks OpenFileMapping. So we emulate one with + * opening the existing shmem and reading its size from the header + */ + hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, + PAGE_READWRITE, 0, sizeof(apr_shm_t), mapkey); +#endif + } +#endif +#if APR_HAS_ANSI_FS + ELSE_WIN_OS_IS_ANSI + { + hMap = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey); + } +#endif + + if (!hMap) { + return apr_get_os_error(); + } + + base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); + if (!base) { + CloseHandle(hMap); + return apr_get_os_error(); + } + + *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t)); + (*m)->pool = pool; + (*m)->memblk = base; + /* Real (*m)->mem->size could be recovered with VirtualQuery */ + (*m)->size = (*m)->memblk->size; +#if _WIN32_WCE + /* Reopen with real size */ + UnmapViewOfFile(base); + CloseHandle(hMap); + + hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, + PAGE_READWRITE, 0, (*m)->size, mapkey); + if (!hMap) { + return apr_get_os_error(); + } + base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); + if (!base) { + CloseHandle(hMap); + return apr_get_os_error(); + } +#endif + (*m)->hMap = hMap; + (*m)->length = (*m)->memblk->length; + (*m)->usrmem = (char*)base + sizeof(memblock_t); + (*m)->filename = NULL; + + apr_pool_cleanup_register((*m)->pool, *m, + shm_cleanup, apr_pool_cleanup_null); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m, + const char *file, + apr_pool_t *pool, + apr_int32_t flags) +{ + apr_status_t rv; + int can_create_global; + int try_global_local[3] = {-1, -1, -1}; + int cur; + + if (!file) { + return APR_EINVAL; + } + + if (flags & APR_SHM_NS_LOCAL) { + try_global_local[0] = 0; /* only search local */ + } + else if (flags & APR_SHM_NS_GLOBAL) { + try_global_local[0] = 1; /* only search global */ + } + else { + can_create_global = can_create_global_maps(); + if (!can_create_global) { /* unprivileged process */ + try_global_local[0] = 0; /* search local before global */ + try_global_local[1] = 1; + } + else { + try_global_local[0] = 1; /* search global before local */ + try_global_local[1] = 0; + } + } + + for (cur = 0; try_global_local[cur] != -1; cur++) { + rv = shm_attach_internal(m, file, pool, try_global_local[cur]); + if (!APR_STATUS_IS_ENOENT(rv)) { + break; + } + } + + return rv; +} + +APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m, + const char *file, + apr_pool_t *pool) +{ + return apr_shm_attach_ex(m, file, pool, 0); +} + +APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m) +{ + apr_status_t rv = shm_cleanup(m); + apr_pool_cleanup_kill(m->pool, m, shm_cleanup); + return rv; +} + +APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m) +{ + return m->usrmem; +} + +APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m) +{ + return m->length; +} + +APR_PERMS_SET_ENOTIMPL(shm) + +APR_POOL_IMPLEMENT_ACCESSOR(shm) + +APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm, + apr_shm_t *shm) +{ + *osshm = shm->hMap; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m, + apr_os_shm_t *osshm, + apr_pool_t *pool) +{ + void* base; + base = MapViewOfFile(*osshm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); + if (!base) { + return apr_get_os_error(); + } + + *m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t)); + (*m)->pool = pool; + (*m)->hMap = *osshm; + (*m)->memblk = base; + (*m)->usrmem = (char*)base + sizeof(memblock_t); + /* Real (*m)->mem->size could be recovered with VirtualQuery */ + (*m)->size = (*m)->memblk->size; + (*m)->length = (*m)->memblk->length; + (*m)->filename = NULL; + + apr_pool_cleanup_register((*m)->pool, *m, + shm_cleanup, apr_pool_cleanup_null); + return APR_SUCCESS; +} + |