1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Memory handler for a shared memory divided in slot.
18 * This one uses shared memory.
21 #include "ap_slotmem.h"
24 char *name; /* per segment name */
25 void *shm; /* ptr to memory segment (apr_shm_t *) */
26 void *base; /* data set start */
27 apr_size_t size; /* size of each memory slot */
28 int num; /* number of mem slots */
29 apr_pool_t *gpool; /* per segment global pool */
30 apr_global_mutex_t *smutex; /* mutex */
31 struct ap_slotmem_t *next; /* location of next allocated segment */
35 /* The description of the slots to reuse the slotmem */
36 struct sharedslotdesc {
41 /* global pool and list of slotmem we are handling */
42 static struct ap_slotmem_t *globallistmem = NULL;
43 static apr_pool_t *gpool = NULL;
44 static apr_global_mutex_t *smutex;
45 static const char *mutex_fname;
48 * Persiste the slotmem in a file
49 * slotmem name and file name.
50 * anonymous : $server_root/logs/anonymous.slotmem
51 * :module.c : $server_root/logs/module.c.slotmem
52 * abs_name : $abs_name.slotmem
55 static const char *store_filename(apr_pool_t *pool, const char *slotmemname)
57 const char *storename;
59 if (strcmp(slotmemname, "anonymous") == 0)
60 fname = ap_server_root_relative(pool, "logs/anonymous");
61 else if (slotmemname[0] == ':') {
63 tmpname = apr_pstrcat(pool, "logs/", &slotmemname[1], NULL);
64 fname = ap_server_root_relative(pool, tmpname);
69 storename = apr_pstrcat(pool, fname, ".slotmem", NULL);
73 static void store_slotmem(ap_slotmem_t *slotmem)
78 const char *storename;
80 storename = store_filename(slotmem->gpool, slotmem->name);
82 rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, slotmem->gpool);
83 if (APR_STATUS_IS_EEXIST(rv)) {
84 apr_file_remove(storename, slotmem->gpool);
85 rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, slotmem->gpool);
87 if (rv != APR_SUCCESS) {
90 nbytes = slotmem->size * slotmem->num;
91 apr_file_write(fp, slotmem->base, &nbytes);
95 static void restore_slotmem(void *ptr, const char *name, apr_size_t item_size, int item_num, apr_pool_t *pool)
97 const char *storename;
99 apr_size_t nbytes = item_size * item_num;
102 storename = store_filename(pool, name);
103 rv = apr_file_open(&fp, storename, APR_READ | APR_WRITE, APR_OS_DEFAULT, pool);
104 if (rv == APR_SUCCESS) {
106 if (apr_file_info_get(&fi, APR_FINFO_SIZE, fp) == APR_SUCCESS) {
107 if (fi.size == nbytes) {
108 apr_file_read(fp, ptr, &nbytes);
112 apr_file_remove(storename, pool);
120 static apr_status_t cleanup_slotmem(void *param)
122 ap_slotmem_t **mem = param;
124 apr_pool_t *pool = NULL;
127 ap_slotmem_t *next = *mem;
131 rv = apr_shm_destroy((apr_shm_t *)next->shm);
134 apr_pool_destroy(pool);
139 static apr_status_t slotmem_do(ap_slotmem_t *mem, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool)
145 return APR_ENOSHMAVAIL;
149 for (i = 0; i < mem->num; i++) {
150 ptr = ptr + mem->size;
151 func((void *) ptr, data, pool);
156 static apr_status_t slotmem_create(ap_slotmem_t **new, const char *name, apr_size_t item_size, int item_num, apr_pool_t *pool)
158 /* void *slotmem = NULL; */
160 struct sharedslotdesc desc;
162 ap_slotmem_t *next = globallistmem;
168 return APR_ENOSHMAVAIL;
170 if (name[0] == ':') {
174 fname = ap_server_root_relative(pool, name);
177 /* first try to attach to existing slotmem */
180 if (strcmp(next->name, fname) == 0) {
181 /* we already have it */
196 /* first try to attach to existing shared memory */
197 if (name && name[0] != ':') {
198 rv = apr_shm_attach(&shm, fname, gpool);
203 if (rv == APR_SUCCESS) {
205 if (apr_shm_size_get(shm) != item_size * item_num + sizeof(struct sharedslotdesc)) {
209 ptr = apr_shm_baseaddr_get(shm);
210 memcpy(&desc, ptr, sizeof(desc));
211 if (desc.item_size != item_size || desc.item_num != item_num) {
215 ptr = ptr + sizeof(desc);
218 if (name && name[0] != ':') {
219 apr_shm_remove(fname, gpool);
220 rv = apr_shm_create(&shm, item_size * item_num + sizeof(struct sharedslotdesc), fname, gpool);
223 rv = apr_shm_create(&shm, item_size * item_num + sizeof(struct sharedslotdesc), NULL, gpool);
225 if (rv != APR_SUCCESS) {
228 ptr = apr_shm_baseaddr_get(shm);
229 desc.item_size = item_size;
230 desc.item_num = item_num;
231 memcpy(ptr, &desc, sizeof(desc));
232 ptr = ptr + sizeof(desc);
233 memset(ptr, 0, item_size * item_num);
234 restore_slotmem(ptr, fname, item_size, item_num, pool);
237 /* For the chained slotmem stuff */
238 res = (ap_slotmem_t *) apr_pcalloc(gpool, sizeof(ap_slotmem_t));
239 res->name = apr_pstrdup(gpool, fname);
242 res->size = item_size;
245 res->smutex = smutex;
247 if (globallistmem == NULL) {
258 static apr_status_t slotmem_attach(ap_slotmem_t **new, const char *name, apr_size_t *item_size, int *item_num, apr_pool_t *pool)
260 /* void *slotmem = NULL; */
263 ap_slotmem_t *next = globallistmem;
264 struct sharedslotdesc desc;
270 return APR_ENOSHMAVAIL;
273 if (name[0] == ':') {
277 fname = ap_server_root_relative(pool, name);
281 return APR_ENOSHMAVAIL;
284 /* first try to attach to existing slotmem */
287 if (strcmp(next->name, fname) == 0) {
288 /* we already have it */
290 *item_size = next->size;
291 *item_num = next->num;
300 /* first try to attach to existing shared memory */
301 rv = apr_shm_attach(&shm, fname, gpool);
302 if (rv != APR_SUCCESS) {
306 /* Read the description of the slotmem */
307 ptr = apr_shm_baseaddr_get(shm);
308 memcpy(&desc, ptr, sizeof(desc));
309 ptr = ptr + sizeof(desc);
311 /* For the chained slotmem stuff */
312 res = (ap_slotmem_t *) apr_pcalloc(gpool, sizeof(ap_slotmem_t));
313 res->name = apr_pstrdup(gpool, fname);
316 res->size = desc.item_size;
317 res->num = desc.item_num;
319 res->smutex = smutex;
321 if (globallistmem == NULL) {
329 *item_size = desc.item_size;
330 *item_num = desc.item_num;
334 static apr_status_t slotmem_mem(ap_slotmem_t *slot, int id, void **mem)
340 return APR_ENOSHMAVAIL;
342 if (id < 0 || id > slot->num) {
343 return APR_ENOSHMAVAIL;
346 ptr = slot->base + slot->size * id;
348 return APR_ENOSHMAVAIL;
354 static apr_status_t slotmem_lock(ap_slotmem_t *slot)
356 return (apr_global_mutex_lock(slot->smutex));
359 static apr_status_t slotmem_unlock(ap_slotmem_t *slot)
361 return (apr_global_mutex_unlock(slot->smutex));
364 static const ap_slotmem_storage_method storage = {
374 /* make the storage usuable from outside */
375 static const ap_slotmem_storage_method *sharedmem_getstorage(void)
380 /* initialise the global pool */
381 static void sharedmem_initgpool(apr_pool_t *p)
386 /* Add the pool_clean routine */
387 static void sharedmem_initialize_cleanup(apr_pool_t *p)
389 apr_pool_cleanup_register(p, &globallistmem, cleanup_slotmem, apr_pool_cleanup_null);
393 * Create the shared mem mutex and
394 * make sure the shared memory is cleaned
396 static int post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
398 const char *temp_dir;
403 const char *userdata_key = "sharedmem_post_config";
405 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
407 apr_pool_userdata_set((const void *)1, userdata_key,
408 apr_pool_cleanup_null, s->process->pool);
412 rv = apr_temp_dir_get(&temp_dir, p);
413 if (rv != APR_SUCCESS) {
414 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
415 "sharedmem: search for temporary directory failed");
418 apr_filepath_merge(&template, temp_dir, "sharedmem.lck.XXXXXX",
419 APR_FILEPATH_NATIVE, p);
420 rv = apr_file_mktemp(&fmutex, template, 0, p);
421 if (rv != APR_SUCCESS) {
422 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
423 "sharedmem: creation of mutex file in directory %s failed",
428 rv = apr_file_name_get(&mutex_fname, fmutex);
429 if (rv != APR_SUCCESS) {
430 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
431 "sharedmem: unable to get mutex fname");
435 rv = apr_global_mutex_create(&smutex,
436 mutex_fname, APR_LOCK_DEFAULT, p);
437 if (rv != APR_SUCCESS) {
438 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
439 "sharedmem: creation of mutex failed");
443 #ifdef AP_NEED_SET_MUTEX_PERMS
444 rv = ap_unixd_set_global_mutex_perms(smutex);
445 if (rv != APR_SUCCESS) {
446 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
447 "sharedmem: failed to set mutex permissions");
452 sharedmem_initialize_cleanup(p);
456 static int pre_config(apr_pool_t *p, apr_pool_t *plog,
459 apr_pool_t *global_pool;
462 rv = apr_pool_create(&global_pool, NULL);
463 if (rv != APR_SUCCESS) {
464 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
465 "Fatal error: unable to create global pool for shared slotmem");
468 sharedmem_initgpool(global_pool);
472 static void child_init(apr_pool_t *p, server_rec *s)
476 rv = apr_global_mutex_child_init(&smutex,
478 if (rv != APR_SUCCESS) {
479 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
480 "Failed to initialise global mutex %s in child process %"
482 mutex_fname, getpid());
486 static void ap_sharedmem_register_hook(apr_pool_t *p)
488 const ap_slotmem_storage_method *storage = sharedmem_getstorage();
489 ap_register_provider(p, AP_SLOTMEM_STORAGE, "shared", "0", storage);
490 ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_LAST);
491 ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
492 ap_hook_child_init(child_init, NULL, NULL, APR_HOOK_MIDDLE);
495 module AP_MODULE_DECLARE_DATA sharedmem_module = {
496 STANDARD20_MODULE_STUFF,
497 NULL, /* create per-directory config structure */
498 NULL, /* merge per-directory config structures */
499 NULL, /* create per-server config structure */
500 NULL, /* merge per-server config structures */
501 NULL, /* command apr_table_t */
502 ap_sharedmem_register_hook /* register hooks */