New PHP5 APC - version 3.0.19, using PHP5 5.2.0-8+etch11,
[php5-apc.git] / apc_shm.c
1 /*
2   +----------------------------------------------------------------------+
3   | APC                                                                  |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2008 The PHP Group                                     |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Daniel Cowgill <dcowgill@communityconnect.com>              |
16   |          Rasmus Lerdorf <rasmus@php.net>                             |
17   +----------------------------------------------------------------------+
18
19    This software was contributed to PHP by Community Connect Inc. in 2002
20    and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
21    Future revisions and derivatives of this source code must acknowledge
22    Community Connect Inc. as the original contributor of this module by
23    leaving this note intact in the source code.
24
25    All other licensing and usage conditions are those of the PHP Group.
26
27  */
28
29 /* $Id: apc_shm.c,v 3.11.2.2 2008/05/11 18:57:00 rasmus Exp $ */
30
31 #include "apc_shm.h"
32 #include "apc.h"
33 #ifdef PHP_WIN32
34 /* shm functions are available in TSRM */
35 #include <tsrm/tsrm_win32.h>
36 #define key_t long
37 #else
38 #include <sys/ipc.h>
39 #include <sys/shm.h>
40 #include <sys/stat.h>
41 #endif
42
43 #ifndef SHM_R
44 # define SHM_R 0444 /* read permission */
45 #endif
46 #ifndef SHM_A
47 # define SHM_A 0222 /* write permission */
48 #endif
49
50 int apc_shm_create(const char* pathname, int proj, size_t size)
51 {
52     int shmid;  /* shared memory id */
53     int oflag;  /* permissions on shm */
54     key_t key;  /* shm key returned by ftok */
55
56     key = IPC_PRIVATE;
57 #ifndef PHP_WIN32
58         /* no ftok yet for win32 */
59     if (pathname != NULL) {
60         if ((key = ftok(pathname, proj)) < 0) {
61             apc_eprint("apc_shm_create: ftok failed:");
62         }
63     }
64 #endif
65
66     oflag = IPC_CREAT | SHM_R | SHM_A;
67     if ((shmid = shmget(key, size, oflag)) < 0) {
68         apc_eprint("apc_shm_create: shmget(%d, %d, %d) failed: %s. It is possible that the chosen SHM segment size is higher than the operation system allows. Linux has usually a default limit of 32MB per segment.", key, size, oflag, strerror(errno));
69     }
70
71     return shmid;
72 }
73
74 void apc_shm_destroy(int shmid)
75 {
76     /* we expect this call to fail often, so we do not check */
77     shmctl(shmid, IPC_RMID, 0);
78 }
79
80 void* apc_shm_attach(int shmid)
81 {
82     void* shmaddr;  /* the shared memory address */
83
84     if ((long)(shmaddr = shmat(shmid, 0, 0)) == -1) {
85         apc_eprint("apc_shm_attach: shmat failed:");
86     }
87
88     /*
89      * We set the shmid for removal immediately after attaching to it. The
90      * segment won't disappear until all processes have detached from it.
91      */
92     apc_shm_destroy(shmid);
93     return shmaddr;
94 }
95
96 void apc_shm_detach(void* shmaddr)
97 {
98     if (shmdt(shmaddr) < 0) {
99         apc_eprint("apc_shm_detach: shmdt failed:");
100     }
101 }
102
103 /*
104  * Local variables:
105  * tab-width: 4
106  * c-basic-offset: 4
107  * End:
108  * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
109  * vim<600: expandtab sw=4 ts=4 sts=4
110  */