]> granicus.if.org Git - apache/blob - modules/mem/mod_sharedmem.c
874682a844ea193da7f5bbad0eca4d5566c3c4c5
[apache] / modules / mem / mod_sharedmem.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /* Memory handler for a shared memory divided in slot.
18  * This one uses shared memory.
19  */
20
21 #include  "ap_slotmem.h"
22
23 struct ap_slotmem_t {
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 */
32 };
33
34
35 /* The description of the slots to reuse the slotmem */
36 struct sharedslotdesc {
37     apr_size_t item_size;
38     int item_num;
39 };
40
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;
46
47 /*
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
53  *
54  */
55 static const char *store_filename(apr_pool_t *pool, const char *slotmemname)
56 {
57     const char *storename;
58     const char *fname;
59     if (strcmp(slotmemname, "anonymous") == 0)
60         fname = ap_server_root_relative(pool, "logs/anonymous");
61     else if (slotmemname[0] == ':') {
62         const char *tmpname;
63         tmpname = apr_pstrcat(pool, "logs/", &slotmemname[1], NULL);
64         fname = ap_server_root_relative(pool, tmpname);
65     }
66     else {
67         fname = slotmemname;
68     }
69     storename = apr_pstrcat(pool, fname, ".slotmem", NULL);
70     return storename;
71 }
72
73 static void store_slotmem(ap_slotmem_t *slotmem)
74 {
75     apr_file_t *fp;
76     apr_status_t rv;
77     apr_size_t nbytes;
78     const char *storename;
79
80     storename = store_filename(slotmem->gpool, slotmem->name);
81
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);
86     }
87     if (rv != APR_SUCCESS) {
88         return;
89     }
90     nbytes = slotmem->size * slotmem->num;
91     apr_file_write(fp, slotmem->base, &nbytes);
92     apr_file_close(fp);
93 }
94
95 static void restore_slotmem(void *ptr, const char *name, apr_size_t item_size, int item_num, apr_pool_t *pool)
96 {
97     const char *storename;
98     apr_file_t *fp;
99     apr_size_t nbytes = item_size * item_num;
100     apr_status_t rv;
101
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) {
105         apr_finfo_t fi;
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);
109             }
110             else {
111                 apr_file_close(fp);
112                 apr_file_remove(storename, pool);
113                 return;
114             }
115         }
116         apr_file_close(fp);
117     }
118 }
119
120 static apr_status_t cleanup_slotmem(void *param)
121 {
122     ap_slotmem_t **mem = param;
123     apr_status_t rv;
124     apr_pool_t *pool = NULL;
125
126     if (*mem) {
127         ap_slotmem_t *next = *mem;
128         pool = next->gpool;
129         while (next) {
130             store_slotmem(next);
131             rv = apr_shm_destroy((apr_shm_t *)next->shm);
132             next = next->next;
133         }
134         apr_pool_destroy(pool);
135     }
136     return APR_SUCCESS;
137 }
138
139 static apr_status_t slotmem_do(ap_slotmem_t *mem, ap_slotmem_callback_fn_t *func, void *data, apr_pool_t *pool)
140 {
141     int i;
142     void *ptr;
143
144     if (!mem) {
145         return APR_ENOSHMAVAIL;
146     }
147
148     ptr = mem->base;
149     for (i = 0; i < mem->num; i++) {
150         ptr = ptr + mem->size;
151         func((void *) ptr, data, pool);
152     }
153     return APR_SUCCESS;
154 }
155
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)
157 {
158 /*    void *slotmem = NULL; */
159     void *ptr;
160     struct sharedslotdesc desc;
161     ap_slotmem_t *res;
162     ap_slotmem_t *next = globallistmem;
163     const char *fname;
164     apr_shm_t *shm;
165     apr_status_t rv;
166
167     if (gpool == NULL)
168         return APR_ENOSHMAVAIL;
169     if (name) {
170         if (name[0] == ':') {
171             fname = name;
172         }
173         else {
174             fname = ap_server_root_relative(pool, name);
175         }
176
177         /* first try to attach to existing slotmem */
178         if (next) {
179             for (;;) {
180                 if (strcmp(next->name, fname) == 0) {
181                     /* we already have it */
182                     *new = next;
183                     return APR_SUCCESS;
184                 }
185                 if (!next->next) {
186                     break;
187                 }
188                 next = next->next;
189             }
190         }
191     }
192     else {
193         fname = "anonymous";
194     }
195
196     /* first try to attach to existing shared memory */
197     if (name && name[0] != ':') {
198         rv = apr_shm_attach(&shm, fname, gpool);
199     }
200     else {
201         rv = APR_EINVAL;
202     }
203     if (rv == APR_SUCCESS) {
204         /* check size */
205         if (apr_shm_size_get(shm) != item_size * item_num + sizeof(struct sharedslotdesc)) {
206             apr_shm_detach(shm);
207             return APR_EINVAL;
208         }
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) {
212             apr_shm_detach(shm);
213             return APR_EINVAL;
214         }
215         ptr = ptr + sizeof(desc);
216     }
217     else {
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);
221         }
222         else {
223             rv = apr_shm_create(&shm, item_size * item_num + sizeof(struct sharedslotdesc), NULL, gpool);
224         }
225         if (rv != APR_SUCCESS) {
226             return rv;
227         }
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);
235     }
236
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);
240     res->shm = shm;
241     res->base = ptr;
242     res->size = item_size;
243     res->num = item_num;
244     res->gpool = gpool;
245     res->smutex = smutex;
246     res->next = NULL;
247     if (globallistmem == NULL) {
248         globallistmem = res;
249     }
250     else {
251         next->next = res;
252     }
253
254     *new = res;
255     return APR_SUCCESS;
256 }
257
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)
259 {
260 /*    void *slotmem = NULL; */
261     void *ptr;
262     ap_slotmem_t *res;
263     ap_slotmem_t *next = globallistmem;
264     struct sharedslotdesc desc;
265     const char *fname;
266     apr_shm_t *shm;
267     apr_status_t rv;
268
269     if (gpool == NULL) {
270         return APR_ENOSHMAVAIL;
271     }
272     if (name) {
273         if (name[0] == ':') {
274             fname = name;
275         }
276         else {
277             fname = ap_server_root_relative(pool, name);
278         }
279     }
280     else {
281         return APR_ENOSHMAVAIL;
282     }
283
284     /* first try to attach to existing slotmem */
285     if (next) {
286         for (;;) {
287             if (strcmp(next->name, fname) == 0) {
288                 /* we already have it */
289                 *new = next;
290                 *item_size = next->size;
291                 *item_num = next->num;
292                 return APR_SUCCESS;
293             }
294             if (!next->next)
295                 break;
296             next = next->next;
297         }
298     }
299
300     /* first try to attach to existing shared memory */
301     rv = apr_shm_attach(&shm, fname, gpool);
302     if (rv != APR_SUCCESS) {
303         return rv;
304     }
305
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);
310
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);
314     res->shm = shm;
315     res->base = ptr;
316     res->size = desc.item_size;
317     res->num = desc.item_num;
318     res->gpool = gpool;
319     res->smutex = smutex;
320     res->next = NULL;
321     if (globallistmem == NULL) {
322         globallistmem = res;
323     }
324     else {
325         next->next = res;
326     }
327
328     *new = res;
329     *item_size = desc.item_size;
330     *item_num = desc.item_num;
331     return APR_SUCCESS;
332 }
333
334 static apr_status_t slotmem_mem(ap_slotmem_t *slot, int id, void **mem)
335 {
336
337     void *ptr;
338
339     if (!slot) {
340         return APR_ENOSHMAVAIL;
341     }
342     if (id < 0 || id > slot->num) {
343         return APR_ENOSHMAVAIL;
344     }
345
346     ptr = slot->base + slot->size * id;
347     if (!ptr) {
348         return APR_ENOSHMAVAIL;
349     }
350     *mem = ptr;
351     return APR_SUCCESS;
352 }
353
354 static apr_status_t slotmem_lock(ap_slotmem_t *slot)
355 {
356     return (apr_global_mutex_lock(slot->smutex));
357 }
358
359 static apr_status_t slotmem_unlock(ap_slotmem_t *slot)
360 {
361     return (apr_global_mutex_unlock(slot->smutex));
362 }
363
364 static const ap_slotmem_storage_method storage = {
365     "sharedmem",
366     &slotmem_do,
367     &slotmem_create,
368     &slotmem_attach,
369     &slotmem_mem,
370     &slotmem_lock,
371     &slotmem_unlock
372 };
373
374 /* make the storage usuable from outside */
375 static const ap_slotmem_storage_method *sharedmem_getstorage(void)
376 {
377     return (&storage);
378 }
379
380 /* initialise the global pool */
381 static void sharedmem_initgpool(apr_pool_t *p)
382 {
383     gpool = p;
384 }
385
386 /* Add the pool_clean routine */
387 static void sharedmem_initialize_cleanup(apr_pool_t *p)
388 {
389     apr_pool_cleanup_register(p, &globallistmem, cleanup_slotmem, apr_pool_cleanup_null);
390 }
391
392 /*
393  * Create the shared mem mutex and
394  * make sure the shared memory is cleaned
395  */
396 static int post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
397 {
398     const char *temp_dir;
399     char *template;
400     apr_status_t rv;
401     void *data;
402     apr_file_t *fmutex;
403     const char *userdata_key = "sharedmem_post_config";
404
405     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
406     if (!data) {
407         apr_pool_userdata_set((const void *)1, userdata_key,
408                                apr_pool_cleanup_null, s->process->pool);
409         return OK;
410     }
411
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");
416         return rv;
417     }
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",
424                      temp_dir);
425         return rv;
426     }
427
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");
432         return rv;
433     }
434
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");
440         return rv;
441     }
442
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");
448         return rv;
449     }
450 #endif
451
452     sharedmem_initialize_cleanup(p);
453     return OK;
454 }
455
456 static int pre_config(apr_pool_t *p, apr_pool_t *plog,
457                       apr_pool_t *ptemp)
458 {
459     apr_pool_t *global_pool;
460     apr_status_t rv;
461
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");
466         return rv;
467     }
468     sharedmem_initgpool(global_pool);
469     return OK;
470 }
471
472 static void child_init(apr_pool_t *p, server_rec *s)
473 {
474     apr_status_t rv;
475
476     rv = apr_global_mutex_child_init(&smutex,
477                                      mutex_fname, p);
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 %"
481                      APR_PID_T_FMT ".",
482                      mutex_fname, getpid());
483     }
484 }
485
486 static void ap_sharedmem_register_hook(apr_pool_t *p)
487 {
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);
493 }
494
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 */
503 };
504