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
|
/*
+----------------------------------------------------------------------+
| PHP version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Slava Poliakov <hackie@prohost.org> |
| Ilia Alshanetsky <ilia@prohost.org> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_shmop.h"
# ifndef PHP_WIN32
# include <sys/ipc.h>
# include <sys/shm.h>
#else
#include "tsrm_win32.h"
#endif
#if HAVE_SHMOP
#include "ext/standard/info.h"
#ifdef ZTS
int shmop_globals_id;
#else
php_shmop_globals shmop_globals;
#endif
int shm_type;
/* {{{ shmop_functions[]
*/
const zend_function_entry shmop_functions[] = {
PHP_FE(shmop_open, NULL)
PHP_FE(shmop_read, NULL)
PHP_FE(shmop_close, NULL)
PHP_FE(shmop_size, NULL)
PHP_FE(shmop_write, NULL)
PHP_FE(shmop_delete, NULL)
{NULL, NULL, NULL} /* Must be the last line in shmop_functions[] */
};
/* }}} */
/* {{{ shmop_module_entry
*/
zend_module_entry shmop_module_entry = {
STANDARD_MODULE_HEADER,
"shmop",
shmop_functions,
PHP_MINIT(shmop),
NULL,
NULL,
NULL,
PHP_MINFO(shmop),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_SHMOP
ZEND_GET_MODULE(shmop)
#endif
#define PHP_SHMOP_GET_RES \
shmop = zend_list_find(shmid, &type); \
if (!shmop) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no shared memory segment with an id of [%lu]", shmid); \
RETURN_FALSE; \
} else if (type != shm_type) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not a shmop resource"); \
RETURN_FALSE; \
} \
/* {{{ rsclean
*/
static void rsclean(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
struct php_shmop *shmop = (struct php_shmop *)rsrc->ptr;
shmdt(shmop->addr);
efree(shmop);
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(shmop)
{
shm_type = zend_register_list_destructors_ex(rsclean, NULL, "shmop", module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(shmop)
{
php_info_print_table_start();
php_info_print_table_row(2, "shmop support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ proto int shmop_open (int key, string flags, int mode, int size)
gets and attaches a shared memory segment */
PHP_FUNCTION(shmop_open)
{
long key, mode, size;
struct php_shmop *shmop;
struct shmid_ds shm;
int rsid;
char *flags;
int flags_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lsll", &key, &flags, &flags_len, &mode, &size) == FAILURE) {
return;
}
if (flags_len != 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not a valid flag", flags);
RETURN_FALSE;
}
shmop = emalloc(sizeof(struct php_shmop));
memset(shmop, 0, sizeof(struct php_shmop));
shmop->key = key;
shmop->shmflg |= mode;
switch (flags[0])
{
case 'a':
shmop->shmatflg |= SHM_RDONLY;
break;
case 'c':
shmop->shmflg |= IPC_CREAT;
shmop->size = size;
break;
case 'n':
shmop->shmflg |= (IPC_CREAT | IPC_EXCL);
shmop->size = size;
break;
case 'w':
/* noop
shm segment is being opened for read & write
will fail if segment does not exist
*/
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid access mode");
goto err;
}
if (shmop->shmflg & IPC_CREAT && shmop->size < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Shared memory segment size must be greater then zero.");
goto err;
}
shmop->shmid = shmget(shmop->key, shmop->size, shmop->shmflg);
if (shmop->shmid == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to attach or create shared memory segment");
goto err;
}
if (shmctl(shmop->shmid, IPC_STAT, &shm)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to get shared memory segment information");
goto err;
}
shmop->addr = shmat(shmop->shmid, 0, shmop->shmatflg);
if (shmop->addr == (char*) -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to attach to shared memory segment");
goto err;
}
shmop->size = shm.shm_segsz;
rsid = zend_list_insert(shmop, shm_type);
RETURN_LONG(rsid);
err:
efree(shmop);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string shmop_read (int shmid, int start, int count)
reads from a shm segment */
PHP_FUNCTION(shmop_read)
{
long shmid, start, count;
struct php_shmop *shmop;
int type;
char *startaddr;
int bytes;
char *return_string;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &shmid, &start, &count) == FAILURE) {
return;
}
PHP_SHMOP_GET_RES
if (start < 0 || start > shmop->size) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "start is out of range");
RETURN_FALSE;
}
if (start + count > shmop->size || count < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "count is out of range");
RETURN_FALSE;
}
startaddr = shmop->addr + start;
bytes = count ? count : shmop->size - start;
return_string = emalloc(bytes+1);
memcpy(return_string, startaddr, bytes);
return_string[bytes] = 0;
RETURN_STRINGL(return_string, bytes, 0);
}
/* }}} */
/* {{{ proto void shmop_close (int shmid)
closes a shared memory segment */
PHP_FUNCTION(shmop_close)
{
long shmid;
struct php_shmop *shmop;
int type;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &shmid) == FAILURE) {
return;
}
PHP_SHMOP_GET_RES
zend_list_delete(shmid);
}
/* }}} */
/* {{{ proto int shmop_size (int shmid)
returns the shm size */
PHP_FUNCTION(shmop_size)
{
long shmid;
struct php_shmop *shmop;
int type;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &shmid) == FAILURE) {
return;
}
PHP_SHMOP_GET_RES
RETURN_LONG(shmop->size);
}
/* }}} */
/* {{{ proto int shmop_write (int shmid, string data, int offset)
writes to a shared memory segment */
PHP_FUNCTION(shmop_write)
{
struct php_shmop *shmop;
int type;
int writesize;
long shmid, offset;
char *data;
int data_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lsl", &shmid, &data, &data_len, &offset) == FAILURE) {
return;
}
PHP_SHMOP_GET_RES
if ((shmop->shmatflg & SHM_RDONLY) == SHM_RDONLY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "trying to write to a read only segment");
RETURN_FALSE;
}
if (offset < 0 || offset > shmop->size) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "offset out of range");
RETURN_FALSE;
}
writesize = (data_len < shmop->size - offset) ? data_len : shmop->size - offset;
memcpy(shmop->addr + offset, data, writesize);
RETURN_LONG(writesize);
}
/* }}} */
/* {{{ proto bool shmop_delete (int shmid)
mark segment for deletion */
PHP_FUNCTION(shmop_delete)
{
long shmid;
struct php_shmop *shmop;
int type;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &shmid) == FAILURE) {
return;
}
PHP_SHMOP_GET_RES
if (shmctl(shmop->shmid, IPC_RMID, NULL)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't mark segment for deletion (are you the owner?)");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#endif /* HAVE_SHMOP */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|