1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/* 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.
*/
/* Memory handler for a shared memory divided in slot.
* This one uses shared memory.
*/
#include "ap_slotmem.h"
struct ap_slotmem_t {
char *name; /* per segment name */
void *shm; /* ptr to memory segment (apr_shm_t *) */
void *base; /* data set start */
apr_size_t size; /* size of each memory slot */
int num; /* number of mem slots */
apr_pool_t *gpool; /* per segment global pool */
apr_global_mutex_t *smutex; /* mutex */
struct ap_slotmem_t *next; /* location of next allocated segment */
};
/* The description of the slots to reuse the slotmem */
struct sharedslotdesc {
apr_size_t item_size;
int item_num;
};
/* global pool and list of slotmem we are handling */
static struct ap_slotmem_t *globallistmem = NULL;
static apr_pool_t *gpool = NULL;
static apr_global_mutex_t *smutex;
static const char *mutex_fname;
/*
* Persiste the slotmem in a file
* slotmem name and file name.
* anonymous : $server_root/logs/anonymous.slotmem
* :module.c : $server_root/logs/module.c.slotmem
* abs_name : $abs_name.slotmem
*
*/
static const char *store_filename(apr_pool_t *pool, const char *slotmemname)
{
const char *storename;
const char *fname;
if (strcmp(slotmemname, "anonymous") == 0)
fname = ap_server_root_relative(pool, "logs/anonymous");
else if (slotmemname[0] == ':') {
const char *tmpname;
tmpname = apr_pstrcat(pool, "logs/", &slotmemname[1], NULL);
fname = ap_server_root_relative(pool, tmpname);
}
else {
fname = slotmemname;
}
storename = apr_pstrcat(pool, fname, ".slotmem", NULL);
return storename;
}
static void store_slotmem(ap_slotmem_t *slotmem)
{
apr_file_t *fp;
apr_status_t rv;
apr_size_t nbytes;
const char *storename;
storename = store_filename(slotmem->gpool, slotmem->name);
rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, slotmem->gpool);
if (APR_STATUS_IS_EEXIST(rv)) {
apr_file_remove(storename, slotmem->gpool);
rv = apr_file_open(&fp, storename, APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, slotmem->gpool);
}
if (rv != APR_SUCCESS) {
return;
}
nbytes = slotmem->size * slotmem->num;
apr_file_write(fp, slotmem->base, &nbytes);
apr_file_close(fp);
}
static void restore_slotmem(void *ptr, const char *name, apr_size_t item_size, int item_num, apr_pool_t *pool)
{
const char *storename;
apr_file_t *fp;
apr_size_t nbytes = item_size * item_num;
apr_status_t rv;
storename = store_filename(pool, name);
rv = apr_file_open(&fp, storename, APR_READ | APR_WRITE, APR_OS_DEFAULT, pool);
if (rv == APR_SUCCESS) {
apr_finfo_t fi;
if (apr_file_info_get(&fi, APR_FINFO_SIZE, fp) == APR_SUCCESS) {
if (fi.size == nbytes) {
apr_file_read(fp, ptr, &nbytes);
}
else {
apr_file_close(fp);
apr_file_remove(storename, pool);
return;
}
}
apr_file_close(fp);
}
}
static apr_status_t cleanup_slotmem(void *param)
{
ap_slotmem_t **mem = param;
apr_status_t rv;
apr_pool_t *pool = NULL;
if (*mem) {
ap_slotmem_t *next = *mem;
pool = next->gpool;
while (next) {
store_slotmem(next);
rv = apr_shm_destroy((apr_shm_t *)next->shm);
next = next->next;
}
apr_pool_destroy(pool);
}
return APR_SUCCESS;
}
static apr_status_t slotmem_do(ap_slotmem_t *mem, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool)
{
int i;
void *ptr;
if (!mem) {
return APR_ENOSHMAVAIL;
}
ptr = mem->base;
for (i = 0; i < mem->num; i++) {
ptr = ptr + mem->size;
func((void *) ptr, data, pool);
}
return APR_SUCCESS;
}
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)
{
/* void *slotmem = NULL; */
void *ptr;
struct sharedslotdesc desc;
ap_slotmem_t *res;
ap_slotmem_t *next = globallistmem;
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);
}
/* 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 */
if (name && name[0] != ':') {
rv = apr_shm_attach(&shm, fname, gpool);
}
else {
rv = APR_EINVAL;
}
if (rv == APR_SUCCESS) {
/* check size */
if (apr_shm_size_get(shm) != item_size * item_num + sizeof(struct sharedslotdesc)) {
apr_shm_detach(shm);
return APR_EINVAL;
}
ptr = apr_shm_baseaddr_get(shm);
memcpy(&desc, ptr, sizeof(desc));
if (desc.item_size != item_size || desc.item_num != item_num) {
apr_shm_detach(shm);
return APR_EINVAL;
}
ptr = ptr + sizeof(desc);
}
else {
if (name && name[0] != ':') {
apr_shm_remove(fname, gpool);
rv = apr_shm_create(&shm, item_size * item_num + sizeof(struct sharedslotdesc), fname, gpool);
}
else {
rv = apr_shm_create(&shm, item_size * item_num + sizeof(struct sharedslotdesc), NULL, gpool);
}
if (rv != APR_SUCCESS) {
return rv;
}
ptr = apr_shm_baseaddr_get(shm);
desc.item_size = item_size;
desc.item_num = item_num;
memcpy(ptr, &desc, sizeof(desc));
ptr = ptr + sizeof(desc);
memset(ptr, 0, item_size * item_num);
restore_slotmem(ptr, fname, item_size, item_num, pool);
}
/* For the chained slotmem stuff */
res = (ap_slotmem_t *) apr_pcalloc(gpool, sizeof(ap_slotmem_t));
res->name = apr_pstrdup(gpool, fname);
res->shm = shm;
res->base = ptr;
res->size = item_size;
res->num = item_num;
res->gpool = gpool;
res->smutex = smutex;
res->next = NULL;
if (globallistmem == NULL) {
globallistmem = res;
}
else {
next->next = res;
}
*new = res;
return APR_SUCCESS;
}
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)
{
/* void *slotmem = NULL; */
void *ptr;
ap_slotmem_t *res;
ap_slotmem_t *next = globallistmem;
struct sharedslotdesc 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->size;
*item_num = next->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 = apr_shm_baseaddr_get(shm);
memcpy(&desc, ptr, sizeof(desc));
ptr = ptr + sizeof(desc);
/* For the chained slotmem stuff */
res = (ap_slotmem_t *) apr_pcalloc(gpool, sizeof(ap_slotmem_t));
res->name = apr_pstrdup(gpool, fname);
res->shm = shm;
res->base = ptr;
res->size = desc.item_size;
res->num = desc.item_num;
res->gpool = gpool;
res->smutex = smutex;
res->next = NULL;
if (globallistmem == NULL) {
globallistmem = res;
}
else {
next->next = res;
}
*new = res;
*item_size = desc.item_size;
*item_num = desc.item_num;
return APR_SUCCESS;
}
static apr_status_t slotmem_mem(ap_slotmem_t *slot, int id, void **mem)
{
void *ptr;
if (!slot) {
return APR_ENOSHMAVAIL;
}
if (id < 0 || id > slot->num) {
return APR_ENOSHMAVAIL;
}
ptr = slot->base + slot->size * id;
if (!ptr) {
return APR_ENOSHMAVAIL;
}
*mem = ptr;
return APR_SUCCESS;
}
static apr_status_t slotmem_lock(ap_slotmem_t *slot)
{
return (apr_global_mutex_lock(slot->smutex));
}
static apr_status_t slotmem_unlock(ap_slotmem_t *slot)
{
return (apr_global_mutex_unlock(slot->smutex));
}
static const ap_slotmem_storage_method storage = {
&slotmem_do,
&slotmem_create,
&slotmem_attach,
&slotmem_mem,
&slotmem_lock,
&slotmem_unlock
};
/* make the storage usuable from outside */
static const ap_slotmem_storage_method *sharedmem_getstorage(void)
{
return (&storage);
}
/* initialise the global pool */
static void sharedmem_initgpool(apr_pool_t *p)
{
gpool = p;
}
/* Add the pool_clean routine */
static void sharedmem_initialize_cleanup(apr_pool_t *p)
{
apr_pool_cleanup_register(p, &globallistmem, cleanup_slotmem, apr_pool_cleanup_null);
}
/*
* Create the shared mem mutex and
* 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)
{
const char *temp_dir;
char *template;
apr_status_t rv;
void *data;
apr_file_t *fmutex;
const char *userdata_key = "sharedmem_post_config";
apr_pool_userdata_get(&data, userdata_key, s->process->pool);
if (!data) {
apr_pool_userdata_set((const void *)1, userdata_key,
apr_pool_cleanup_null, s->process->pool);
return OK;
}
rv = apr_temp_dir_get(&temp_dir, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"sharedmem: search for temporary directory failed");
return rv;
}
apr_filepath_merge(&template, temp_dir, "sharedmem.lck.XXXXXX",
APR_FILEPATH_NATIVE, p);
rv = apr_file_mktemp(&fmutex, template, 0, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"sharedmem: creation of mutex file in directory %s failed",
temp_dir);
return rv;
}
rv = apr_file_name_get(&mutex_fname, fmutex);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"sharedmem: unable to get mutex fname");
return rv;
}
rv = apr_global_mutex_create(&smutex,
mutex_fname, APR_LOCK_DEFAULT, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"sharedmem: creation of mutex failed");
return rv;
}
#ifdef AP_NEED_SET_MUTEX_PERMS
rv = ap_unixd_set_global_mutex_perms(smutex);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"sharedmem: failed to set mutex permissions");
return rv;
}
#endif
sharedmem_initialize_cleanup(p);
return OK;
}
static int pre_config(apr_pool_t *p, apr_pool_t *plog,
apr_pool_t *ptemp)
{
apr_pool_t *global_pool;
apr_status_t rv;
rv = apr_pool_create(&global_pool, NULL);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
"Fatal error: unable to create global pool for shared slotmem");
return rv;
}
sharedmem_initgpool(global_pool);
return OK;
}
static void child_init(apr_pool_t *p, server_rec *s)
{
apr_status_t rv;
rv = apr_global_mutex_child_init(&smutex,
mutex_fname, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
"Failed to initialise global mutex %s in child process %"
APR_PID_T_FMT ".",
mutex_fname, getpid());
}
}
static void ap_sharedmem_register_hook(apr_pool_t *p)
{
const ap_slotmem_storage_method *storage = sharedmem_getstorage();
ap_register_provider(p, AP_SLOTMEM_STORAGE, "shared", "0", storage);
ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_LAST);
ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init(child_init, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA sharedmem_module = {
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_sharedmem_register_hook /* register hooks */
};
|