1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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
#include "apr_arch_shm.h"
19
#include "apr_general.h"
20
#include "apr_errno.h"
22
#include "apr_strings.h"
24
static apr_status_t shm_cleanup_owner(void *m_)
26
apr_shm_t *m = (apr_shm_t *)m_;
28
/* anonymous shared memory */
29
if (m->filename == NULL) {
30
#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
31
if (munmap(m->base, m->realsize) == -1) {
36
#if APR_USE_SHMEM_SHMGET_ANON
37
if (shmdt(m->base) == -1) {
40
/* This segment will automatically remove itself after all
41
* references have detached. */
46
/* name-based shared memory */
48
#if APR_USE_SHMEM_MMAP_TMP
49
if (munmap(m->base, m->realsize) == -1) {
52
return apr_file_remove(m->filename, m->pool);
54
#if APR_USE_SHMEM_MMAP_SHM
55
if (munmap(m->base, m->realsize) == -1) {
58
if (shm_unlink(m->filename) == -1) {
63
#if APR_USE_SHMEM_SHMGET
64
/* Indicate that the segment is to be destroyed as soon
65
* as all processes have detached. This also disallows any
66
* new attachments to the segment. */
67
if (shmctl(m->shmid, IPC_RMID, NULL) == -1) {
70
if (shmdt(m->base) == -1) {
73
return apr_file_remove(m->filename, m->pool);
80
APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
87
#if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON
88
struct shmid_ds shmbuf;
92
#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || \
93
APR_USE_SHMEM_MMAP_ZERO
96
#if APR_USE_SHMEM_SHMGET
100
#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_SHMGET || \
101
APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
102
apr_file_t *file; /* file where metadata is stored */
105
/* Check if they want anonymous or name-based shared memory */
106
if (filename == NULL) {
107
#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
108
new_m = apr_palloc(pool, sizeof(apr_shm_t));
110
new_m->reqsize = reqsize;
111
new_m->realsize = reqsize +
112
APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */
113
new_m->filename = NULL;
115
#if APR_USE_SHMEM_MMAP_ZERO
116
status = apr_file_open(&file, "/dev/zero", APR_READ | APR_WRITE,
117
APR_OS_DEFAULT, pool);
118
if (status != APR_SUCCESS) {
121
status = apr_os_file_get(&tmpfd, file);
122
if (status != APR_SUCCESS) {
126
new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
127
MAP_SHARED, tmpfd, 0);
128
if (new_m->base == (void *)MAP_FAILED) {
132
status = apr_file_close(file);
133
if (status != APR_SUCCESS) {
137
/* store the real size in the metadata */
138
*(apr_size_t*)(new_m->base) = new_m->realsize;
139
/* metadata isn't usable */
140
new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
142
apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
143
apr_pool_cleanup_null);
147
#elif APR_USE_SHMEM_MMAP_ANON
148
new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
149
MAP_ANON|MAP_SHARED, -1, 0);
150
if (new_m->base == (void *)MAP_FAILED) {
154
/* store the real size in the metadata */
155
*(apr_size_t*)(new_m->base) = new_m->realsize;
156
/* metadata isn't usable */
157
new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
159
apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
160
apr_pool_cleanup_null);
164
#endif /* APR_USE_SHMEM_MMAP_ZERO */
165
#endif /* APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON */
166
#if APR_USE_SHMEM_SHMGET_ANON
168
new_m = apr_palloc(pool, sizeof(apr_shm_t));
170
new_m->reqsize = reqsize;
171
new_m->realsize = reqsize;
172
new_m->filename = NULL;
174
if ((new_m->shmid = shmget(IPC_PRIVATE, new_m->realsize,
175
SHM_R | SHM_W | IPC_CREAT)) < 0) {
179
if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
182
new_m->usable = new_m->base;
184
if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
187
apr_uid_current(&uid, &gid, pool);
188
shmbuf.shm_perm.uid = uid;
189
shmbuf.shm_perm.gid = gid;
190
if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
194
/* Remove the segment once use count hits zero.
195
* We will not attach to this segment again, since it is
196
* anonymous memory, so it is ok to mark it for deletion.
198
if (shmctl(new_m->shmid, IPC_RMID, NULL) == -1) {
202
apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
203
apr_pool_cleanup_null);
206
#endif /* APR_USE_SHMEM_SHMGET_ANON */
207
/* It is an error if they want anonymous memory but we don't have it. */
208
return APR_ENOTIMPL; /* requested anonymous but we don't have it */
211
/* Name-based shared memory */
213
new_m = apr_palloc(pool, sizeof(apr_shm_t));
215
new_m->reqsize = reqsize;
216
new_m->filename = apr_pstrdup(pool, filename);
218
#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
219
new_m->realsize = reqsize +
220
APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */
221
/* FIXME: Ignore error for now. *
222
* status = apr_file_remove(file, pool);*/
223
status = APR_SUCCESS;
225
#if APR_USE_SHMEM_MMAP_TMP
226
/* FIXME: Is APR_OS_DEFAULT sufficient? */
227
status = apr_file_open(&file, filename,
228
APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
229
APR_OS_DEFAULT, pool);
230
if (status != APR_SUCCESS) {
234
status = apr_os_file_get(&tmpfd, file);
235
if (status != APR_SUCCESS) {
236
apr_file_close(file); /* ignore errors, we're failing */
237
apr_file_remove(new_m->filename, new_m->pool);
241
status = apr_file_trunc(file, new_m->realsize);
242
if (status != APR_SUCCESS) {
243
apr_file_close(file); /* ignore errors, we're failing */
244
apr_file_remove(new_m->filename, new_m->pool);
248
new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
249
MAP_SHARED, tmpfd, 0);
250
/* FIXME: check for errors */
252
status = apr_file_close(file);
253
if (status != APR_SUCCESS) {
256
#endif /* APR_USE_SHMEM_MMAP_TMP */
257
#if APR_USE_SHMEM_MMAP_SHM
258
tmpfd = shm_open(filename, O_RDWR | O_CREAT | O_EXCL, 0644);
263
status = apr_os_file_put(&file, &tmpfd,
264
APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
266
if (status != APR_SUCCESS) {
270
status = apr_file_trunc(file, new_m->realsize);
271
if (status != APR_SUCCESS) {
272
shm_unlink(filename); /* we're failing, remove the object */
275
new_m->base = mmap(NULL, reqsize, PROT_READ | PROT_WRITE,
276
MAP_SHARED, tmpfd, 0);
278
/* FIXME: check for errors */
280
status = apr_file_close(file);
281
if (status != APR_SUCCESS) {
284
#endif /* APR_USE_SHMEM_MMAP_SHM */
286
/* store the real size in the metadata */
287
*(apr_size_t*)(new_m->base) = new_m->realsize;
288
/* metadata isn't usable */
289
new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
291
apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
292
apr_pool_cleanup_null);
296
#endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
298
#if APR_USE_SHMEM_SHMGET
299
new_m->realsize = reqsize;
301
/* FIXME: APR_OS_DEFAULT is too permissive, switch to 600 I think. */
302
status = apr_file_open(&file, filename,
303
APR_WRITE | APR_CREATE | APR_EXCL,
304
APR_OS_DEFAULT, pool);
305
if (status != APR_SUCCESS) {
309
/* ftok() (on solaris at least) requires that the file actually
310
* exist before calling ftok(). */
311
shmkey = ftok(filename, 1);
312
if (shmkey == (key_t)-1) {
316
if ((new_m->shmid = shmget(shmkey, new_m->realsize,
317
SHM_R | SHM_W | IPC_CREAT | IPC_EXCL)) < 0) {
321
if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
324
new_m->usable = new_m->base;
326
if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
329
apr_uid_current(&uid, &gid, pool);
330
shmbuf.shm_perm.uid = uid;
331
shmbuf.shm_perm.gid = gid;
332
if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
336
nbytes = sizeof(reqsize);
337
status = apr_file_write(file, (const void *)&reqsize,
339
if (status != APR_SUCCESS) {
342
status = apr_file_close(file);
343
if (status != APR_SUCCESS) {
347
apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
348
apr_pool_cleanup_null);
352
#endif /* APR_USE_SHMEM_SHMGET */
358
APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
361
#if APR_USE_SHMEM_SHMGET
368
#if APR_USE_SHMEM_MMAP_TMP
369
return apr_file_remove(filename, pool);
371
#if APR_USE_SHMEM_MMAP_SHM
372
if (shm_unlink(filename) == -1) {
377
#if APR_USE_SHMEM_SHMGET
378
/* Presume that the file already exists; just open for writing */
379
status = apr_file_open(&file, filename, APR_WRITE,
380
APR_OS_DEFAULT, pool);
385
/* ftok() (on solaris at least) requires that the file actually
386
* exist before calling ftok(). */
387
shmkey = ftok(filename, 1);
388
if (shmkey == (key_t)-1) {
389
goto shm_remove_failed;
392
apr_file_close(file);
394
if ((shmid = shmget(shmkey, 0, SHM_R | SHM_W)) < 0) {
395
goto shm_remove_failed;
398
/* Indicate that the segment is to be destroyed as soon
399
* as all processes have detached. This also disallows any
400
* new attachments to the segment. */
401
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
402
goto shm_remove_failed;
404
return apr_file_remove(filename, pool);
408
/* ensure the file has been removed anyway. */
409
apr_file_remove(filename, pool);
413
/* No support for anonymous shm */
417
APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
419
return apr_pool_cleanup_run(m->pool, m, shm_cleanup_owner);
422
static apr_status_t shm_cleanup_attach(void *m_)
424
apr_shm_t *m = (apr_shm_t *)m_;
426
if (m->filename == NULL) {
427
/* It doesn't make sense to detach from an anonymous memory segment. */
431
#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
432
if (munmap(m->base, m->realsize) == -1) {
436
#endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
437
#if APR_USE_SHMEM_SHMGET
438
if (shmdt(m->base) == -1) {
448
APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
449
const char *filename,
452
if (filename == NULL) {
453
/* It doesn't make sense to attach to a segment if you don't know
458
#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
462
apr_file_t *file; /* file where metadata is stored */
465
new_m = apr_palloc(pool, sizeof(apr_shm_t));
467
new_m->filename = apr_pstrdup(pool, filename);
469
status = apr_file_open(&file, filename,
470
APR_READ | APR_WRITE,
471
APR_OS_DEFAULT, pool);
472
if (status != APR_SUCCESS) {
475
status = apr_os_file_get(&tmpfd, file);
476
if (status != APR_SUCCESS) {
480
nbytes = sizeof(new_m->realsize);
481
status = apr_file_read(file, (void *)&(new_m->realsize),
483
if (status != APR_SUCCESS) {
487
status = apr_os_file_get(&tmpfd, file);
488
if (status != APR_SUCCESS) {
489
apr_file_close(file); /* ignore errors, we're failing */
490
apr_file_remove(new_m->filename, new_m->pool);
494
new_m->reqsize = new_m->realsize - sizeof(apr_size_t);
496
new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
497
MAP_SHARED, tmpfd, 0);
498
/* FIXME: check for errors */
500
status = apr_file_close(file);
501
if (status != APR_SUCCESS) {
505
/* metadata isn't part of the usable segment */
506
new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
508
apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
509
apr_pool_cleanup_null);
513
#endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
514
#if APR_USE_SHMEM_SHMGET
517
apr_file_t *file; /* file where metadata is stored */
521
new_m = apr_palloc(pool, sizeof(apr_shm_t));
523
status = apr_file_open(&file, filename,
524
APR_READ, APR_OS_DEFAULT, pool);
525
if (status != APR_SUCCESS) {
529
nbytes = sizeof(new_m->reqsize);
530
status = apr_file_read(file, (void *)&(new_m->reqsize),
532
if (status != APR_SUCCESS) {
535
status = apr_file_close(file);
536
if (status != APR_SUCCESS) {
540
new_m->filename = apr_pstrdup(pool, filename);
542
shmkey = ftok(filename, 1);
543
if (shmkey == (key_t)-1) {
546
if ((new_m->shmid = shmget(shmkey, 0, SHM_R | SHM_W)) == -1) {
549
if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
552
new_m->usable = new_m->base;
553
new_m->realsize = new_m->reqsize;
555
apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
556
apr_pool_cleanup_null);
560
#endif /* APR_USE_SHMEM_SHMGET */
566
APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
568
apr_status_t rv = shm_cleanup_attach(m);
569
apr_pool_cleanup_kill(m->pool, m, shm_cleanup_attach);
573
APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
578
APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
583
APR_POOL_IMPLEMENT_ACCESSOR(shm)
585
APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
591
APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,