mod_sharedmem.c revision 908fa24e93608edc48387d1348de5fd8ff9d60c4
083eacf86f9327835b824f9e6be987d56cd96e82slive/* Licensed to the Apache Software Foundation (ASF) under one or more
530eba85dbd41b8a0fa5255d3648d1440199a661slive * contributor license agreements. See the NOTICE file distributed with
083eacf86f9327835b824f9e6be987d56cd96e82slive * this work for additional information regarding copyright ownership.
083eacf86f9327835b824f9e6be987d56cd96e82slive * The ASF licenses this file to You under the Apache License, Version 2.0
083eacf86f9327835b824f9e6be987d56cd96e82slive * (the "License"); you may not use this file except in compliance with
083eacf86f9327835b824f9e6be987d56cd96e82slive * the License. You may obtain a copy of the License at
083eacf86f9327835b824f9e6be987d56cd96e82slive *
083eacf86f9327835b824f9e6be987d56cd96e82slive * http://www.apache.org/licenses/LICENSE-2.0
083eacf86f9327835b824f9e6be987d56cd96e82slive *
083eacf86f9327835b824f9e6be987d56cd96e82slive * Unless required by applicable law or agreed to in writing, software
083eacf86f9327835b824f9e6be987d56cd96e82slive * distributed under the License is distributed on an "AS IS" BASIS,
083eacf86f9327835b824f9e6be987d56cd96e82slive * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
083eacf86f9327835b824f9e6be987d56cd96e82slive * See the License for the specific language governing permissions and
083eacf86f9327835b824f9e6be987d56cd96e82slive * limitations under the License.
083eacf86f9327835b824f9e6be987d56cd96e82slive */
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/* Memory handler for a shared memory divided in slot.
083eacf86f9327835b824f9e6be987d56cd96e82slive * This one uses shared memory.
083eacf86f9327835b824f9e6be987d56cd96e82slive */
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive#include "ap_slotmem.h"
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive#include "httpd.h"
083eacf86f9327835b824f9e6be987d56cd96e82slive#ifdef AP_NEED_SET_MUTEX_PERMS
083eacf86f9327835b824f9e6be987d56cd96e82slive#include "unixd.h"
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive#if APR_HAVE_UNISTD_H
083eacf86f9327835b824f9e6be987d56cd96e82slive#include <unistd.h> /* for getpid() */
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive#if HAVE_SYS_SEM_H
083eacf86f9327835b824f9e6be987d56cd96e82slive#include <sys/shm.h>
083eacf86f9327835b824f9e6be987d56cd96e82slive#if !defined(SHM_R)
083eacf86f9327835b824f9e6be987d56cd96e82slive#define SHM_R 0400
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive#if !defined(SHM_W)
083eacf86f9327835b824f9e6be987d56cd96e82slive#define SHM_W 0200
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestruct ap_slotmem_t {
083eacf86f9327835b824f9e6be987d56cd96e82slive char *name; /* per segment name */
083eacf86f9327835b824f9e6be987d56cd96e82slive void *shm; /* ptr to memory segment (apr_shm_t *) */
083eacf86f9327835b824f9e6be987d56cd96e82slive void *base; /* data set start */
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_size_t size; /* size of each memory slot */
083eacf86f9327835b824f9e6be987d56cd96e82slive unsigned int num; /* number of mem slots */
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_t *gpool; /* per segment global pool */
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_global_mutex_t *smutex; /* mutex */
083eacf86f9327835b824f9e6be987d56cd96e82slive struct ap_slotmem_t *next; /* location of next allocated segment */
083eacf86f9327835b824f9e6be987d56cd96e82slive char *inuse; /* is-use flag table*/
083eacf86f9327835b824f9e6be987d56cd96e82slive};
530eba85dbd41b8a0fa5255d3648d1440199a661slive
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/* The description of the slots to reuse the slotmem */
083eacf86f9327835b824f9e6be987d56cd96e82slivestruct sharedslotdesc {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_size_t item_size;
083eacf86f9327835b824f9e6be987d56cd96e82slive unsigned int item_num;
083eacf86f9327835b824f9e6be987d56cd96e82slive};
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/*
083eacf86f9327835b824f9e6be987d56cd96e82slive * Memory layout:
083eacf86f9327835b824f9e6be987d56cd96e82slive * sharedslotdesc | slots | isuse array
083eacf86f9327835b824f9e6be987d56cd96e82slive */
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/* global pool and list of slotmem we are handling */
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic struct ap_slotmem_t *globallistmem = NULL;
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_pool_t *gpool = NULL;
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_global_mutex_t *smutex = NULL;
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic const char *mutex_fname = NULL;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive#define SLOTMEM_LOCK(s) do { \
083eacf86f9327835b824f9e6be987d56cd96e82slive if (s) \
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_global_mutex_lock(s); \
083eacf86f9327835b824f9e6be987d56cd96e82slive} while (0)
530eba85dbd41b8a0fa5255d3648d1440199a661slive
083eacf86f9327835b824f9e6be987d56cd96e82slive#define SLOTMEM_UNLOCK(s) do { \
530eba85dbd41b8a0fa5255d3648d1440199a661slive if (s) \
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_global_mutex_unlock(s); \
083eacf86f9327835b824f9e6be987d56cd96e82slive} while (0)
530eba85dbd41b8a0fa5255d3648d1440199a661slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/* apr:shmem/unix/shm.c */
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t unixd_set_shm_perms(const char *fname)
530eba85dbd41b8a0fa5255d3648d1440199a661slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive#ifdef AP_NEED_SET_MUTEX_PERMS
083eacf86f9327835b824f9e6be987d56cd96e82slive#if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON
083eacf86f9327835b824f9e6be987d56cd96e82slive struct shmid_ds shmbuf;
530eba85dbd41b8a0fa5255d3648d1440199a661slive key_t shmkey;
083eacf86f9327835b824f9e6be987d56cd96e82slive int shmid;
083eacf86f9327835b824f9e6be987d56cd96e82slive
530eba85dbd41b8a0fa5255d3648d1440199a661slive shmkey = ftok(fname, 1);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (shmkey == (key_t)-1) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return errno;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
530eba85dbd41b8a0fa5255d3648d1440199a661slive if ((shmid = shmget(shmkey, 0, SHM_R | SHM_W)) == -1) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return errno;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
530eba85dbd41b8a0fa5255d3648d1440199a661slive#if MODULE_MAGIC_NUMBER_MAJOR > 20081212
083eacf86f9327835b824f9e6be987d56cd96e82slive shmbuf.shm_perm.uid = ap_unixd_config.user_id;
083eacf86f9327835b824f9e6be987d56cd96e82slive shmbuf.shm_perm.gid = ap_unixd_config.group_id;
530eba85dbd41b8a0fa5255d3648d1440199a661slive#else
083eacf86f9327835b824f9e6be987d56cd96e82slive shmbuf.shm_perm.uid = unixd_config.user_id;
083eacf86f9327835b824f9e6be987d56cd96e82slive shmbuf.shm_perm.gid = unixd_config.group_id;
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
530eba85dbd41b8a0fa5255d3648d1440199a661slive shmbuf.shm_perm.mode = 0600;
083eacf86f9327835b824f9e6be987d56cd96e82slive if (shmctl(shmid, IPC_SET, &shmbuf) == -1) {
530eba85dbd41b8a0fa5255d3648d1440199a661slive return errno;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
530eba85dbd41b8a0fa5255d3648d1440199a661slive#else
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOTIMPL;
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive#else
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOTIMPL;
083eacf86f9327835b824f9e6be987d56cd96e82slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/*
083eacf86f9327835b824f9e6be987d56cd96e82slive * Persiste the slotmem in a file
083eacf86f9327835b824f9e6be987d56cd96e82slive * slotmem name and file name.
083eacf86f9327835b824f9e6be987d56cd96e82slive * anonymous : $server_root/logs/anonymous.slotmem
083eacf86f9327835b824f9e6be987d56cd96e82slive * :module.c : $server_root/logs/module.c.slotmem
083eacf86f9327835b824f9e6be987d56cd96e82slive * abs_name : $abs_name.slotmem
083eacf86f9327835b824f9e6be987d56cd96e82slive *
083eacf86f9327835b824f9e6be987d56cd96e82slive */
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic const char *store_filename(apr_pool_t *pool, const char *slotmemname)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *storename;
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *fname;
083eacf86f9327835b824f9e6be987d56cd96e82slive if (strcmp(slotmemname, "anonymous") == 0)
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = ap_server_root_relative(pool, "logs/anonymous");
083eacf86f9327835b824f9e6be987d56cd96e82slive else if (slotmemname[0] == ':') {
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *tmpname;
083eacf86f9327835b824f9e6be987d56cd96e82slive tmpname = apr_pstrcat(pool, "logs/", &slotmemname[1], NULL);
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = ap_server_root_relative(pool, tmpname);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = slotmemname;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive storename = apr_pstrcat(pool, fname, ".slotmem", NULL);
083eacf86f9327835b824f9e6be987d56cd96e82slive return storename;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic void store_slotmem(ap_slotmem_t *slotmem)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_t *fp;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_size_t nbytes;
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *storename;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive storename = store_filename(slotmem->gpool, slotmem->name);
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, slotmem->gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (APR_STATUS_IS_EEXIST(rv)) {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_remove(storename, slotmem->gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, slotmem->gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive nbytes = (slotmem->size * slotmem->num) + (slotmem->num * sizeof(char));
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_write(fp, slotmem->base, &nbytes);
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_close(fp);
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic void restore_slotmem(void *ptr, const char *name, apr_size_t size, apr_pool_t *pool)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *storename;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_t *fp;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_size_t nbytes = size;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive storename = store_filename(pool, name);
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_file_open(&fp, storename, APR_READ | APR_WRITE, APR_OS_DEFAULT, pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv == APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_finfo_t fi;
083eacf86f9327835b824f9e6be987d56cd96e82slive if (apr_file_info_get(&fi, APR_FINFO_SIZE, fp) == APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive if (fi.size == nbytes) {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_read(fp, ptr, &nbytes);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_close(fp);
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_remove(storename, pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive return;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_file_close(fp);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t cleanup_slotmem(void *param)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_slotmem_t **mem = param;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_t *pool = NULL;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive if (*mem) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_slotmem_t *next = *mem;
083eacf86f9327835b824f9e6be987d56cd96e82slive pool = next->gpool;
083eacf86f9327835b824f9e6be987d56cd96e82slive while (next) {
083eacf86f9327835b824f9e6be987d56cd96e82slive store_slotmem(next);
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_shm_destroy((apr_shm_t *)next->shm);
083eacf86f9327835b824f9e6be987d56cd96e82slive next = next->next;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_destroy(pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t slotmem_do(ap_slotmem_t *mem, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive unsigned int i;
083eacf86f9327835b824f9e6be987d56cd96e82slive void *ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive char *inuse;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive if (!mem) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOSHMAVAIL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = mem->base;
083eacf86f9327835b824f9e6be987d56cd96e82slive inuse = ptr + (mem->size * mem->num);
083eacf86f9327835b824f9e6be987d56cd96e82slive SLOTMEM_LOCK(mem->smutex);
083eacf86f9327835b824f9e6be987d56cd96e82slive for (i = 0; i < mem->num; i++, inuse++) {
083eacf86f9327835b824f9e6be987d56cd96e82slive if (*inuse) {
083eacf86f9327835b824f9e6be987d56cd96e82slive func((void *) ptr, data, pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr += mem->size;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive SLOTMEM_UNLOCK(mem->smutex);
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t slotmem_create(ap_slotmem_t **new, const char *name, apr_size_t item_size, unsigned int item_num, apslotmem_type type, apr_pool_t *pool)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive/* void *slotmem = NULL; */
083eacf86f9327835b824f9e6be987d56cd96e82slive void *ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive struct sharedslotdesc desc;
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_slotmem_t *res;
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_slotmem_t *next = globallistmem;
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *fname;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_shm_t *shm;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_size_t basesize = (item_size * item_num);
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_size_t size = sizeof(struct sharedslotdesc) + (item_num * sizeof(char)) + basesize;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive if (gpool == NULL)
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOSHMAVAIL;
083eacf86f9327835b824f9e6be987d56cd96e82slive if (name) {
083eacf86f9327835b824f9e6be987d56cd96e82slive if (name[0] == ':') {
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = name;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = ap_server_root_relative(pool, name);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive /* first try to attach to existing slotmem */
083eacf86f9327835b824f9e6be987d56cd96e82slive if (next) {
083eacf86f9327835b824f9e6be987d56cd96e82slive for (;;) {
083eacf86f9327835b824f9e6be987d56cd96e82slive if (strcmp(next->name, fname) == 0) {
083eacf86f9327835b824f9e6be987d56cd96e82slive /* we already have it */
083eacf86f9327835b824f9e6be987d56cd96e82slive *new = next;
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive if (!next->next) {
083eacf86f9327835b824f9e6be987d56cd96e82slive break;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive next = next->next;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = "anonymous";
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive /* first try to attach to existing shared memory */
083eacf86f9327835b824f9e6be987d56cd96e82slive if (name && name[0] != ':') {
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_shm_attach(&shm, fname, gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = APR_EINVAL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv == APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive /* check size */
083eacf86f9327835b824f9e6be987d56cd96e82slive if (apr_shm_size_get(shm) != size) {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_shm_detach(shm);
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_EINVAL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = apr_shm_baseaddr_get(shm);
083eacf86f9327835b824f9e6be987d56cd96e82slive memcpy(&desc, ptr, sizeof(desc));
083eacf86f9327835b824f9e6be987d56cd96e82slive if (desc.item_size != item_size || desc.item_num != item_num) {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_shm_detach(shm);
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_EINVAL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = ptr + sizeof(desc);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_size_t dsize = size - sizeof(struct sharedslotdesc);
083eacf86f9327835b824f9e6be987d56cd96e82slive SLOTMEM_LOCK(smutex);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (name && name[0] != ':') {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_shm_remove(fname, gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_shm_create(&shm, size, fname, gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_shm_create(&shm, size, NULL, gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive SLOTMEM_UNLOCK(smutex);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive if (name && name[0] != ':') {
083eacf86f9327835b824f9e6be987d56cd96e82slive /* Set permissions to shared memory
083eacf86f9327835b824f9e6be987d56cd96e82slive * so it can be attached by child process
083eacf86f9327835b824f9e6be987d56cd96e82slive * having different user credentials
083eacf86f9327835b824f9e6be987d56cd96e82slive *
083eacf86f9327835b824f9e6be987d56cd96e82slive * See apr:shmem/unix/shm.c
083eacf86f9327835b824f9e6be987d56cd96e82slive */
083eacf86f9327835b824f9e6be987d56cd96e82slive unixd_set_shm_perms(fname);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = apr_shm_baseaddr_get(shm);
083eacf86f9327835b824f9e6be987d56cd96e82slive desc.item_size = item_size;
083eacf86f9327835b824f9e6be987d56cd96e82slive desc.item_num = item_num;
083eacf86f9327835b824f9e6be987d56cd96e82slive memcpy(ptr, &desc, sizeof(desc));
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = ptr + sizeof(desc);
083eacf86f9327835b824f9e6be987d56cd96e82slive memset(ptr, 0, dsize);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (type == SLOTMEM_PERSIST)
083eacf86f9327835b824f9e6be987d56cd96e82slive restore_slotmem(ptr, fname, dsize, pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive /* For the chained slotmem stuff */
083eacf86f9327835b824f9e6be987d56cd96e82slive res = (ap_slotmem_t *) apr_pcalloc(gpool, sizeof(ap_slotmem_t));
083eacf86f9327835b824f9e6be987d56cd96e82slive res->name = apr_pstrdup(gpool, fname);
083eacf86f9327835b824f9e6be987d56cd96e82slive res->shm = shm;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->base = ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->size = item_size;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->num = item_num;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->gpool = gpool;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->smutex = smutex;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->next = NULL;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->inuse = ptr + basesize;
083eacf86f9327835b824f9e6be987d56cd96e82slive if (globallistmem == NULL) {
083eacf86f9327835b824f9e6be987d56cd96e82slive globallistmem = res;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive next->next = res;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive *new = res;
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t slotmem_attach(ap_slotmem_t **new, const char *name, apr_size_t *item_size, unsigned int *item_num, apr_pool_t *pool)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive/* void *slotmem = NULL; */
083eacf86f9327835b824f9e6be987d56cd96e82slive void *ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_slotmem_t *res;
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_slotmem_t *next = globallistmem;
083eacf86f9327835b824f9e6be987d56cd96e82slive struct sharedslotdesc desc;
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *fname;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_shm_t *shm;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive if (gpool == NULL) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOSHMAVAIL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive if (name) {
083eacf86f9327835b824f9e6be987d56cd96e82slive if (name[0] == ':') {
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = name;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive fname = ap_server_root_relative(pool, name);
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOSHMAVAIL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive /* first try to attach to existing slotmem */
083eacf86f9327835b824f9e6be987d56cd96e82slive if (next) {
083eacf86f9327835b824f9e6be987d56cd96e82slive for (;;) {
083eacf86f9327835b824f9e6be987d56cd96e82slive if (strcmp(next->name, fname) == 0) {
083eacf86f9327835b824f9e6be987d56cd96e82slive /* we already have it */
083eacf86f9327835b824f9e6be987d56cd96e82slive *new = next;
083eacf86f9327835b824f9e6be987d56cd96e82slive *item_size = next->size;
083eacf86f9327835b824f9e6be987d56cd96e82slive *item_num = next->num;
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive if (!next->next)
083eacf86f9327835b824f9e6be987d56cd96e82slive break;
083eacf86f9327835b824f9e6be987d56cd96e82slive next = next->next;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive /* first try to attach to existing shared memory */
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_shm_attach(&shm, fname, gpool);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive /* Read the description of the slotmem */
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = apr_shm_baseaddr_get(shm);
083eacf86f9327835b824f9e6be987d56cd96e82slive memcpy(&desc, ptr, sizeof(desc));
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = ptr + sizeof(desc);
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive /* For the chained slotmem stuff */
083eacf86f9327835b824f9e6be987d56cd96e82slive res = (ap_slotmem_t *) apr_pcalloc(gpool, sizeof(ap_slotmem_t));
083eacf86f9327835b824f9e6be987d56cd96e82slive res->name = apr_pstrdup(gpool, fname);
083eacf86f9327835b824f9e6be987d56cd96e82slive res->shm = shm;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->base = ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->size = desc.item_size;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->num = desc.item_num;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->gpool = gpool;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->smutex = smutex;
083eacf86f9327835b824f9e6be987d56cd96e82slive res->inuse = ptr + (desc.item_size * desc.item_num);
083eacf86f9327835b824f9e6be987d56cd96e82slive res->next = NULL;
083eacf86f9327835b824f9e6be987d56cd96e82slive if (globallistmem == NULL) {
083eacf86f9327835b824f9e6be987d56cd96e82slive globallistmem = res;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive else {
083eacf86f9327835b824f9e6be987d56cd96e82slive next->next = res;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive *new = res;
083eacf86f9327835b824f9e6be987d56cd96e82slive *item_size = desc.item_size;
083eacf86f9327835b824f9e6be987d56cd96e82slive *item_num = desc.item_num;
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t slotmem_mem(ap_slotmem_t *slot, unsigned int id, void **mem)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive void *ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive if (!slot) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOSHMAVAIL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive if (id < 0 || id > slot->num) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOSHMAVAIL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive ptr = slot->base + slot->size * id;
083eacf86f9327835b824f9e6be987d56cd96e82slive if (!ptr) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_ENOSHMAVAIL;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive *mem = ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t slotmem_get(ap_slotmem_t *slot, unsigned int id, unsigned char *dest, apr_size_t dest_len)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive void *ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t ret;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive ret = slotmem_mem(slot, id, &ptr);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (ret != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return ret;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive memcpy(dest, ptr, dest_len); /* bounds check? */
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_status_t slotmem_put(ap_slotmem_t *slot, unsigned int id, unsigned char *src, apr_size_t src_len)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive void *ptr;
083eacf86f9327835b824f9e6be987d56cd96e82slive char *inuse;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t ret;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive ret = slotmem_mem(slot, id, &ptr);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (ret != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive return ret;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive memcpy(ptr, src, src_len); /* bounds check? */
083eacf86f9327835b824f9e6be987d56cd96e82slive /* We know the id fit it */
083eacf86f9327835b824f9e6be987d56cd96e82slive inuse = (slot->base + (slot->size * slot->num));
083eacf86f9327835b824f9e6be987d56cd96e82slive inuse[id] = 1;
083eacf86f9327835b824f9e6be987d56cd96e82slive return APR_SUCCESS;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic unsigned int slotmem_num_slots(ap_slotmem_t *slot)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
530eba85dbd41b8a0fa5255d3648d1440199a661slive return slot->num;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic apr_size_t slotmem_slot_size(ap_slotmem_t *slot)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive return slot->size;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic const ap_slotmem_storage_method storage = {
083eacf86f9327835b824f9e6be987d56cd96e82slive "sharedmem",
083eacf86f9327835b824f9e6be987d56cd96e82slive &slotmem_do,
083eacf86f9327835b824f9e6be987d56cd96e82slive &slotmem_create,
083eacf86f9327835b824f9e6be987d56cd96e82slive &slotmem_attach,
083eacf86f9327835b824f9e6be987d56cd96e82slive &slotmem_get,
083eacf86f9327835b824f9e6be987d56cd96e82slive &slotmem_put,
083eacf86f9327835b824f9e6be987d56cd96e82slive &slotmem_num_slots,
083eacf86f9327835b824f9e6be987d56cd96e82slive &slotmem_slot_size
083eacf86f9327835b824f9e6be987d56cd96e82slive};
530eba85dbd41b8a0fa5255d3648d1440199a661slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/* make the storage usuable from outside */
530eba85dbd41b8a0fa5255d3648d1440199a661slivestatic const ap_slotmem_storage_method *sharedmem_getstorage(void)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
530eba85dbd41b8a0fa5255d3648d1440199a661slive return (&storage);
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/* initialise the global pool */
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic void sharedmem_initgpool(apr_pool_t *p)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
530eba85dbd41b8a0fa5255d3648d1440199a661slive gpool = p;
530eba85dbd41b8a0fa5255d3648d1440199a661slive}
530eba85dbd41b8a0fa5255d3648d1440199a661slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/* Add the pool_clean routine */
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic void sharedmem_initialize_cleanup(apr_pool_t *p)
530eba85dbd41b8a0fa5255d3648d1440199a661slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_cleanup_register(p, &globallistmem, cleanup_slotmem, apr_pool_cleanup_null);
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive/*
083eacf86f9327835b824f9e6be987d56cd96e82slive * Create the shared mem mutex and
083eacf86f9327835b824f9e6be987d56cd96e82slive * make sure the shared memory is cleaned
083eacf86f9327835b824f9e6be987d56cd96e82slive */
530eba85dbd41b8a0fa5255d3648d1440199a661slivestatic int post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive const char *temp_dir;
530eba85dbd41b8a0fa5255d3648d1440199a661slive char *template;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
530eba85dbd41b8a0fa5255d3648d1440199a661slive void *data;
530eba85dbd41b8a0fa5255d3648d1440199a661slive apr_file_t *fmutex;
530eba85dbd41b8a0fa5255d3648d1440199a661slive const char *userdata_key = "sharedmem_post_config";
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_userdata_get(&data, userdata_key, s->process->pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (!data) {
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_userdata_set((const void *)1, userdata_key,
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_cleanup_null, s->process->pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive return OK;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_temp_dir_get(&temp_dir, p);
530eba85dbd41b8a0fa5255d3648d1440199a661slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
083eacf86f9327835b824f9e6be987d56cd96e82slive "sharedmem: search for temporary directory failed");
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_filepath_merge(&template, temp_dir, "sharedmem.lck.XXXXXX",
083eacf86f9327835b824f9e6be987d56cd96e82slive APR_FILEPATH_NATIVE, p);
530eba85dbd41b8a0fa5255d3648d1440199a661slive rv = apr_file_mktemp(&fmutex, template, 0, p);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
083eacf86f9327835b824f9e6be987d56cd96e82slive "sharedmem: creation of mutex file in directory %s failed",
083eacf86f9327835b824f9e6be987d56cd96e82slive temp_dir);
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_file_name_get(&mutex_fname, fmutex);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
083eacf86f9327835b824f9e6be987d56cd96e82slive "sharedmem: unable to get mutex fname");
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
530eba85dbd41b8a0fa5255d3648d1440199a661slive
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_file_close(fmutex);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
083eacf86f9327835b824f9e6be987d56cd96e82slive "sharedmem: could not close mutex file");
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_global_mutex_create(&smutex,
083eacf86f9327835b824f9e6be987d56cd96e82slive mutex_fname, APR_LOCK_DEFAULT, p);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
083eacf86f9327835b824f9e6be987d56cd96e82slive "sharedmem: creation of mutex failed");
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
530eba85dbd41b8a0fa5255d3648d1440199a661slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive#ifdef AP_NEED_SET_MUTEX_PERMS
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = ap_unixd_set_global_mutex_perms(smutex);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
083eacf86f9327835b824f9e6be987d56cd96e82slive "sharedmem: failed to set mutex permissions");
083eacf86f9327835b824f9e6be987d56cd96e82slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
530eba85dbd41b8a0fa5255d3648d1440199a661slive#endif
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive sharedmem_initialize_cleanup(p);
083eacf86f9327835b824f9e6be987d56cd96e82slive return OK;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic int pre_config(apr_pool_t *p, apr_pool_t *plog,
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_t *ptemp)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_pool_t *global_pool;
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_pool_create(&global_pool, NULL);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
083eacf86f9327835b824f9e6be987d56cd96e82slive "Fatal error: unable to create global pool for shared slotmem");
530eba85dbd41b8a0fa5255d3648d1440199a661slive return rv;
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive sharedmem_initgpool(global_pool);
083eacf86f9327835b824f9e6be987d56cd96e82slive return OK;
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic void child_init(apr_pool_t *p, server_rec *s)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive apr_status_t rv;
530eba85dbd41b8a0fa5255d3648d1440199a661slive
083eacf86f9327835b824f9e6be987d56cd96e82slive rv = apr_global_mutex_child_init(&smutex,
083eacf86f9327835b824f9e6be987d56cd96e82slive mutex_fname, p);
083eacf86f9327835b824f9e6be987d56cd96e82slive if (rv != APR_SUCCESS) {
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
083eacf86f9327835b824f9e6be987d56cd96e82slive "Failed to initialise global mutex %s in child process %"
083eacf86f9327835b824f9e6be987d56cd96e82slive APR_PID_T_FMT ".",
083eacf86f9327835b824f9e6be987d56cd96e82slive mutex_fname, getpid());
083eacf86f9327835b824f9e6be987d56cd96e82slive }
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivestatic void ap_sharedmem_register_hook(apr_pool_t *p)
083eacf86f9327835b824f9e6be987d56cd96e82slive{
083eacf86f9327835b824f9e6be987d56cd96e82slive const ap_slotmem_storage_method *storage = sharedmem_getstorage();
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_register_provider(p, AP_SLOTMEM_STORAGE, "shared", "0", storage);
530eba85dbd41b8a0fa5255d3648d1440199a661slive ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_LAST);
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_hook_child_init(child_init, NULL, NULL, APR_HOOK_MIDDLE);
083eacf86f9327835b824f9e6be987d56cd96e82slive}
083eacf86f9327835b824f9e6be987d56cd96e82slive
083eacf86f9327835b824f9e6be987d56cd96e82slivemodule AP_MODULE_DECLARE_DATA sharedmem_module = {
083eacf86f9327835b824f9e6be987d56cd96e82slive STANDARD20_MODULE_STUFF,
083eacf86f9327835b824f9e6be987d56cd96e82slive NULL, /* create per-directory config structure */
083eacf86f9327835b824f9e6be987d56cd96e82slive NULL, /* merge per-directory config structures */
083eacf86f9327835b824f9e6be987d56cd96e82slive NULL, /* create per-server config structure */
083eacf86f9327835b824f9e6be987d56cd96e82slive NULL, /* merge per-server config structures */
083eacf86f9327835b824f9e6be987d56cd96e82slive NULL, /* command apr_table_t */
083eacf86f9327835b824f9e6be987d56cd96e82slive ap_sharedmem_register_hook /* register hooks */
083eacf86f9327835b824f9e6be987d56cd96e82slive};
083eacf86f9327835b824f9e6be987d56cd96e82slive
530eba85dbd41b8a0fa5255d3648d1440199a661slive