mod_slotmem_shm.c revision 714a36287cb6cc92748d7e87bbe549d660acae1c
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina/* Licensed to the Apache Software Foundation (ASF) under one or more
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * contributor license agreements. See the NOTICE file distributed with
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * this work for additional information regarding copyright ownership.
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * The ASF licenses this file to You under the Apache License, Version 2.0
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * (the "License"); you may not use this file except in compliance with
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * the License. You may obtain a copy of the License at
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina *
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * http://www.apache.org/licenses/LICENSE-2.0
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina *
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * Unless required by applicable law or agreed to in writing, software
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * distributed under the License is distributed on an "AS IS" BASIS,
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * See the License for the specific language governing permissions and
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * limitations under the License.
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina/* Memory handler for a shared memory divided in slot.
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * This one uses shared memory.
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina *
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * Shared memory is cleaned-up for each restart, graceful or
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * otherwise.
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#include "ap_slotmem.h"
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#include "httpd.h"
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#ifdef AP_NEED_SET_MUTEX_PERMS
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#include "unixd.h"
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#endif
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#if APR_HAVE_UNISTD_H
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina#include <unistd.h> /* for getpid() */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#endif
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#if HAVE_SYS_SEM_H
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#include <sys/shm.h>
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#if !defined(SHM_R)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#define SHM_R 0400
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#endif
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#if !defined(SHM_W)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#define SHM_W 0200
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#endif
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#endif
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#define AP_SLOTMEM_IS_PREGRAB(t) (t->desc.type & AP_SLOTMEM_TYPE_PREGRAB)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#define AP_SLOTMEM_IS_PERSIST(t) (t->desc.type & AP_SLOTMEM_TYPE_PERSIST)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina/* The description of the slots to reuse the slotmem */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březinatypedef struct {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_size_t size; /* size of each memory slot */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina unsigned int num; /* number of mem slots */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina ap_slotmem_type_t type; /* type-specific flags */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina} sharedslotdesc_t;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#define AP_SLOTMEM_OFFSET (APR_ALIGN_DEFAULT(sizeof(sharedslotdesc_t)))
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#define AP_UNSIGNEDINT_OFFSET (APR_ALIGN_DEFAULT(sizeof(unsigned int)))
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březinastruct ap_slotmem_instance_t {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina char *name; /* per segment name */
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina int fbased; /* filebased? */
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina void *shm; /* ptr to memory segment (apr_shm_t *) */
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina void *base; /* data set start */
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina apr_pool_t *gpool; /* per segment global pool */
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina char *inuse; /* in-use flag table*/
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina unsigned int *num_free; /* slot free count for this instance */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina void *persist; /* persist dataset start */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina sharedslotdesc_t desc; /* per slot desc */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina struct ap_slotmem_instance_t *next; /* location of next allocated segment */
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina};
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina/*
b0d3164ca2bd842e176268c26935c5ce54f7f76ePavel Březina * Memory layout:
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * sharedslotdesc_t | num_free | slots | isuse array |
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * ^ ^
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * | . base
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina * . persist (also num_free)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina/* global pool and list of slotmem we are handling */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březinastatic struct ap_slotmem_instance_t *globallistmem = NULL;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březinastatic apr_pool_t *gpool = NULL;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina/* apr:shmem/unix/shm.c */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březinastatic apr_status_t unixd_set_shm_perms(const char *fname)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina{
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#ifdef AP_NEED_SET_MUTEX_PERMS
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina#if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina struct shmid_ds shmbuf;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina key_t shmkey;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina int shmid;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina shmkey = ftok(fname, 1);
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina if (shmkey == (key_t)-1) {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina return errno;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina }
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina if ((shmid = shmget(shmkey, 0, SHM_R | SHM_W)) == -1) {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina return errno;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#if MODULE_MAGIC_NUMBER_MAJOR <= 20081212
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#define ap_unixd_config unixd_config
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#endif
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina shmbuf.shm_perm.uid = ap_unixd_config.user_id;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina shmbuf.shm_perm.gid = ap_unixd_config.group_id;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina shmbuf.shm_perm.mode = 0600;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (shmctl(shmid, IPC_SET, &shmbuf) == -1) {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina return errno;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina return APR_SUCCESS;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#else
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina return APR_ENOTIMPL;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#endif
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#else
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina return APR_ENOTIMPL;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina#endif
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina}
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina/*
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * Persist the slotmem in a file
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * slotmem name and file name.
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina * none : no persistent data
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * anonymous : $server_root/logs/anonymous.slotmem
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina * :rel_name : $server_root/logs/rel_name.slotmem
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina * abs_name : $abs_name.slotmem
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina *
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březinastatic const char *store_filename(apr_pool_t *pool, const char *slotmemname)
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina{
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina const char *storename;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina const char *fname;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (strcasecmp(slotmemname, "none") == 0)
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina return NULL;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina else if (strcasecmp(slotmemname, "anonymous") == 0)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina fname = ap_server_root_relative(pool, "logs/anonymous");
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina else if (slotmemname[0] == ':') {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina const char *tmpname;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina tmpname = apr_pstrcat(pool, "logs/", &slotmemname[1], NULL);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina fname = ap_server_root_relative(pool, tmpname);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina else {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina fname = slotmemname;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina storename = apr_pstrcat(pool, fname, ".slotmem", NULL);
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina return storename;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina}
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březinastatic void store_slotmem(ap_slotmem_instance_t *slotmem)
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina{
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_file_t *fp;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_status_t rv;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina apr_size_t nbytes;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina const char *storename;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina storename = store_filename(slotmem->gpool, slotmem->name);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (storename) {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE,
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina APR_OS_DEFAULT, slotmem->gpool);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (APR_STATUS_IS_EEXIST(rv)) {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina apr_file_remove(storename, slotmem->gpool);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE,
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina APR_OS_DEFAULT, slotmem->gpool);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina if (rv != APR_SUCCESS) {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina return;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina nbytes = (slotmem->desc.size * slotmem->desc.num) +
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina (slotmem->desc.num * sizeof(char)) + AP_UNSIGNEDINT_OFFSET;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_file_write(fp, slotmem->persist, &nbytes);
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina apr_file_close(fp);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina}
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina/* should be apr_status_t really */
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březinastatic void restore_slotmem(void *ptr, const char *name, apr_size_t size,
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina apr_pool_t *pool)
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina{
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina const char *storename;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_file_t *fp;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_size_t nbytes = size;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina apr_status_t rv;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina storename = store_filename(pool, name);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (storename) {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina rv = apr_file_open(&fp, storename, APR_READ | APR_WRITE, APR_OS_DEFAULT,
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina pool);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (rv == APR_SUCCESS) {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_finfo_t fi;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (apr_file_info_get(&fi, APR_FINFO_SIZE, fp) == APR_SUCCESS) {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina if (fi.size == nbytes) {
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_file_read(fp, ptr, &nbytes);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina else {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina apr_file_close(fp);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_file_remove(storename, pool);
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina return;
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina }
7b5e7e539ae9312ab55d75aa94feaad549b2a708Pavel Březina apr_file_close(fp);
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina }
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina }
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina}
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březinastatic apr_status_t cleanup_slotmem(void *param)
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina{
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina ap_slotmem_instance_t **mem = param;
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina if (*mem) {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina ap_slotmem_instance_t *next = *mem;
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina while (next) {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina if (AP_SLOTMEM_IS_PERSIST(next)) {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina store_slotmem(next);
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina }
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina if (next->fbased) {
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina apr_shm_remove(next->name, next->gpool);
ab0eda3622b828df2bfb7850c96d1395f614eb13Pavel Březina }
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina apr_shm_destroy((apr_shm_t *)next->shm);
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina next = next->next;
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina }
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina }
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina /* apr_pool_destroy(gpool); */
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina globallistmem = NULL;
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina return APR_SUCCESS;
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina}
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březinastatic apr_status_t slotmem_doall(ap_slotmem_instance_t *mem,
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina ap_slotmem_callback_fn_t *func,
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina void *data, apr_pool_t *pool)
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina{
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina unsigned int i;
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina char *ptr;
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina char *inuse;
17531a398cc9084036cb08d69fe876a8f12707bbPavel Březina apr_status_t retval = APR_SUCCESS;
if (!mem) {
return APR_ENOSHMAVAIL;
}
ptr = (char *)mem->base;
inuse = mem->inuse;
for (i = 0; i < mem->desc.num; i++, inuse++) {
if (!AP_SLOTMEM_IS_PREGRAB(mem) ||
(AP_SLOTMEM_IS_PREGRAB(mem) && *inuse)) {
retval = func((void *) ptr, data, pool);
if (retval != APR_SUCCESS)
break;
}
ptr += mem->desc.size;
}
return retval;
}
static apr_status_t slotmem_create(ap_slotmem_instance_t **new,
const char *name, apr_size_t item_size,
unsigned int item_num,
ap_slotmem_type_t type, apr_pool_t *pool)
{
/* void *slotmem = NULL; */
int fbased;
char *ptr;
sharedslotdesc_t desc;
ap_slotmem_instance_t *res;
ap_slotmem_instance_t *next = globallistmem;
const char *fname;
apr_shm_t *shm;
apr_size_t basesize = (item_size * item_num);
apr_size_t size = AP_SLOTMEM_OFFSET + AP_UNSIGNEDINT_OFFSET +
(item_num * sizeof(char)) + basesize;
apr_status_t rv;
if (gpool == NULL)
return APR_ENOSHMAVAIL;
if (name) {
if (name[0] == ':') {
fname = name;
}
else {
fname = ap_server_root_relative(pool, name);
}
/* first try to attach to existing slotmem */
if (next) {
for (;;) {
if (strcmp(next->name, fname) == 0) {
/* we already have it */
*new = next;
return APR_SUCCESS;
}
if (!next->next) {
break;
}
next = next->next;
}
}
}
else {
fname = "anonymous";
}
/* first try to attach to existing shared memory */
fbased = (name && name[0] != ':');
if (fbased) {
rv = apr_shm_attach(&shm, fname, gpool);
}
else {
rv = APR_EINVAL;
}
if (rv == APR_SUCCESS) {
/* check size */
if (apr_shm_size_get(shm) != size) {
apr_shm_detach(shm);
return APR_EINVAL;
}
ptr = (char *)apr_shm_baseaddr_get(shm);
memcpy(&desc, ptr, sizeof(desc));
if (desc.size != item_size || desc.num != item_num) {
apr_shm_detach(shm);
return APR_EINVAL;
}
ptr += AP_SLOTMEM_OFFSET;
}
else {
apr_size_t dsize = size - AP_SLOTMEM_OFFSET;
if (fbased) {
apr_shm_remove(fname, gpool);
rv = apr_shm_create(&shm, size, fname, gpool);
}
else {
rv = apr_shm_create(&shm, size, NULL, gpool);
}
if (rv != APR_SUCCESS) {
return rv;
}
if (fbased) {
/* Set permissions to shared memory
* so it can be attached by child process
* having different user credentials
*
* See apr:shmem/unix/shm.c
*/
unixd_set_shm_perms(fname);
}
ptr = (char *)apr_shm_baseaddr_get(shm);
desc.size = item_size;
desc.num = item_num;
desc.type = type;
memcpy(ptr, &desc, sizeof(desc));
ptr += AP_SLOTMEM_OFFSET;
memset(ptr, 0, dsize);
/*
* TODO: Error check the below... What error makes
* sense if the restore fails? Any?
*/
if (type & AP_SLOTMEM_TYPE_PERSIST) {
restore_slotmem(ptr, fname, dsize, pool);
}
}
/* For the chained slotmem stuff */
res = (ap_slotmem_instance_t *) apr_pcalloc(gpool,
sizeof(ap_slotmem_instance_t));
res->name = apr_pstrdup(gpool, fname);
res->fbased = fbased;
res->shm = shm;
res->num_free = (unsigned int *)ptr;
*res->num_free = item_num;
res->persist = (void *)ptr;
ptr += AP_UNSIGNEDINT_OFFSET;
res->base = (void *)ptr;
res->desc = desc;
res->gpool = gpool;
res->next = NULL;
res->inuse = ptr + basesize;
if (globallistmem == NULL) {
globallistmem = res;
}
else {
next->next = res;
}
*new = res;
return APR_SUCCESS;
}
static apr_status_t slotmem_attach(ap_slotmem_instance_t **new,
const char *name, apr_size_t *item_size,
unsigned int *item_num, apr_pool_t *pool)
{
/* void *slotmem = NULL; */
char *ptr;
ap_slotmem_instance_t *res;
ap_slotmem_instance_t *next = globallistmem;
sharedslotdesc_t desc;
const char *fname;
apr_shm_t *shm;
apr_status_t rv;
if (gpool == NULL) {
return APR_ENOSHMAVAIL;
}
if (name) {
if (name[0] == ':') {
fname = name;
}
else {
fname = ap_server_root_relative(pool, name);
}
}
else {
return APR_ENOSHMAVAIL;
}
/* first try to attach to existing slotmem */
if (next) {
for (;;) {
if (strcmp(next->name, fname) == 0) {
/* we already have it */
*new = next;
*item_size = next->desc.size;
*item_num = next->desc.num;
return APR_SUCCESS;
}
if (!next->next) {
break;
}
next = next->next;
}
}
/* first try to attach to existing shared memory */
rv = apr_shm_attach(&shm, fname, gpool);
if (rv != APR_SUCCESS) {
return rv;
}
/* Read the description of the slotmem */
ptr = (char *)apr_shm_baseaddr_get(shm);
memcpy(&desc, ptr, sizeof(desc));
ptr += AP_SLOTMEM_OFFSET;
/* For the chained slotmem stuff */
res = (ap_slotmem_instance_t *) apr_pcalloc(gpool,
sizeof(ap_slotmem_instance_t));
res->name = apr_pstrdup(gpool, fname);
res->fbased = 1;
res->shm = shm;
res->num_free = (unsigned int *)ptr;
res->persist = (void *)ptr;
ptr += AP_UNSIGNEDINT_OFFSET;
res->base = (void *)ptr;
res->desc = desc;
res->gpool = gpool;
res->inuse = ptr + (desc.size * desc.num);
res->next = NULL;
if (globallistmem == NULL) {
globallistmem = res;
}
else {
next->next = res;
}
*new = res;
*item_size = desc.size;
*item_num = desc.num;
return APR_SUCCESS;
}
static apr_status_t slotmem_dptr(ap_slotmem_instance_t *slot,
unsigned int id, void **mem)
{
char *ptr;
if (!slot) {
return APR_ENOSHMAVAIL;
}
if (id >= slot->desc.num) {
return APR_ENOSHMAVAIL;
}
ptr = (char *)slot->base + slot->desc.size * id;
if (!ptr) {
return APR_ENOSHMAVAIL;
}
*mem = (void *)ptr;
return APR_SUCCESS;
}
static apr_status_t slotmem_get(ap_slotmem_instance_t *slot, unsigned int id,
unsigned char *dest, apr_size_t dest_len)
{
void *ptr;
char *inuse;
apr_status_t ret;
if (!slot) {
return APR_ENOSHMAVAIL;
}
inuse = slot->inuse + id;
if (id >= slot->desc.num || (AP_SLOTMEM_IS_PREGRAB(slot) && !*inuse)) {
return APR_NOTFOUND;
}
ret = slotmem_dptr(slot, id, &ptr);
if (ret != APR_SUCCESS) {
return ret;
}
*inuse = 1;
memcpy(dest, ptr, dest_len); /* bounds check? */
return APR_SUCCESS;
}
static apr_status_t slotmem_put(ap_slotmem_instance_t *slot, unsigned int id,
unsigned char *src, apr_size_t src_len)
{
void *ptr;
char *inuse;
apr_status_t ret;
if (!slot) {
return APR_ENOSHMAVAIL;
}
inuse = slot->inuse + id;
if (id >= slot->desc.num || (AP_SLOTMEM_IS_PREGRAB(slot) && !*inuse)) {
return APR_NOTFOUND;
}
ret = slotmem_dptr(slot, id, &ptr);
if (ret != APR_SUCCESS) {
return ret;
}
*inuse=1;
memcpy(ptr, src, src_len); /* bounds check? */
return APR_SUCCESS;
}
static unsigned int slotmem_num_slots(ap_slotmem_instance_t *slot)
{
return slot->desc.num;
}
static unsigned int slotmem_num_free_slots(ap_slotmem_instance_t *slot)
{
if (AP_SLOTMEM_IS_PREGRAB(slot))
return *slot->num_free;
else {
unsigned int i, counter=0;
char *inuse = slot->inuse;
for (i=0; i<slot->desc.num; i++, inuse++) {
if (!*inuse)
counter++;
}
return counter;
}
}
static apr_size_t slotmem_slot_size(ap_slotmem_instance_t *slot)
{
return slot->desc.size;
}
static apr_status_t slotmem_grab(ap_slotmem_instance_t *slot, unsigned int *id)
{
unsigned int i;
char *inuse;
if (!slot) {
return APR_ENOSHMAVAIL;
}
inuse = slot->inuse;
for (i = 0; i < slot->desc.num; i++, inuse++) {
if (!*inuse) {
break;
}
}
if (i >= slot->desc.num) {
return APR_EINVAL;
}
*inuse = 1;
*id = i;
(*slot->num_free)--;
return APR_SUCCESS;
}
static apr_status_t slotmem_release(ap_slotmem_instance_t *slot,
unsigned int id)
{
char *inuse;
if (!slot) {
return APR_ENOSHMAVAIL;
}
inuse = slot->inuse;
if (id >= slot->desc.num || !inuse[id] ) {
return APR_NOTFOUND;
}
inuse[id] = 0;
(*slot->num_free)++;
return APR_SUCCESS;
}
static const ap_slotmem_provider_t storage = {
"sharedmem",
&slotmem_doall,
&slotmem_create,
&slotmem_attach,
&slotmem_dptr,
&slotmem_get,
&slotmem_put,
&slotmem_num_slots,
&slotmem_num_free_slots,
&slotmem_slot_size,
&slotmem_grab,
&slotmem_release
};
/* make the storage usuable from outside */
static const ap_slotmem_provider_t *slotmem_shm_getstorage(void)
{
return (&storage);
}
/* initialise the global pool */
static void slotmem_shm_initgpool(apr_pool_t *p)
{
gpool = p;
}
/* Add the pool_clean routine */
static void slotmem_shm_initialize_cleanup(apr_pool_t *p)
{
apr_pool_cleanup_register(p, &globallistmem, cleanup_slotmem,
apr_pool_cleanup_null);
}
/*
* Make sure the shared memory is cleaned
*/
static int post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
server_rec *s)
{
slotmem_shm_initialize_cleanup(p);
return OK;
}
static int pre_config(apr_pool_t *p, apr_pool_t *plog,
apr_pool_t *ptemp)
{
slotmem_shm_initgpool(p);
return OK;
}
static void ap_slotmem_shm_register_hook(apr_pool_t *p)
{
const ap_slotmem_provider_t *storage = slotmem_shm_getstorage();
ap_register_provider(p, AP_SLOTMEM_PROVIDER_GROUP, "shared",
AP_SLOTMEM_PROVIDER_VERSION, storage);
ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_LAST);
ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
}
AP_DECLARE_MODULE(slotmem_shm) = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
ap_slotmem_shm_register_hook /* register hooks */
};