1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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 | Author: Christian Cartus <cartus@atrior.de> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* This has been built and tested on Linux 2.2.14
20 *
21 * This has been built and tested on Solaris 2.6.
22 * It may not compile or execute correctly on other systems.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "php.h"
30
31 #if HAVE_SYSVSHM
32
33 #include <errno.h>
34
35 #include "php_sysvshm.h"
36 #include "ext/standard/info.h"
37 #include "ext/standard/php_var.h"
38 #include "zend_smart_str.h"
39 #include "php_ini.h"
40
41 /* {{{ arginfo */
42 ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_attach, 0, 0, 1)
43 ZEND_ARG_INFO(0, key)
44 ZEND_ARG_INFO(0, memsize)
45 ZEND_ARG_INFO(0, perm)
46 ZEND_END_ARG_INFO()
47
48 ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_detach, 0, 0, 1)
49 ZEND_ARG_INFO(0, shm_identifier)
50 ZEND_END_ARG_INFO()
51
52 ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_has_var, 0, 0, 2)
53 ZEND_ARG_INFO(0, id)
54 ZEND_ARG_INFO(0, variable_key)
55 ZEND_END_ARG_INFO()
56
57 ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_remove, 0, 0, 1)
58 ZEND_ARG_INFO(0, shm_identifier)
59 ZEND_END_ARG_INFO()
60
61 ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_put_var, 0, 0, 3)
62 ZEND_ARG_INFO(0, shm_identifier)
63 ZEND_ARG_INFO(0, variable_key)
64 ZEND_ARG_INFO(0, variable)
65 ZEND_END_ARG_INFO()
66
67 ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_get_var, 0, 0, 2)
68 ZEND_ARG_INFO(0, id)
69 ZEND_ARG_INFO(0, variable_key)
70 ZEND_END_ARG_INFO()
71
72 ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_remove_var, 0, 0, 2)
73 ZEND_ARG_INFO(0, id)
74 ZEND_ARG_INFO(0, variable_key)
75 ZEND_END_ARG_INFO()
76 /* }}} */
77
78 /* {{{ sysvshm_functions[]
79 */
80 static const zend_function_entry sysvshm_functions[] = {
81 PHP_FE(shm_attach, arginfo_shm_attach)
82 PHP_FE(shm_remove, arginfo_shm_detach)
83 PHP_FE(shm_detach, arginfo_shm_remove)
84 PHP_FE(shm_put_var, arginfo_shm_put_var)
85 PHP_FE(shm_has_var, arginfo_shm_has_var)
86 PHP_FE(shm_get_var, arginfo_shm_get_var)
87 PHP_FE(shm_remove_var, arginfo_shm_remove_var)
88 PHP_FE_END
89 };
90 /* }}} */
91
92 /* {{{ sysvshm_module_entry
93 */
94 zend_module_entry sysvshm_module_entry = {
95 STANDARD_MODULE_HEADER,
96 "sysvshm",
97 sysvshm_functions,
98 PHP_MINIT(sysvshm),
99 NULL,
100 NULL,
101 NULL,
102 PHP_MINFO(sysvshm),
103 PHP_SYSVSHM_VERSION,
104 STANDARD_MODULE_PROPERTIES
105 };
106 /* }}} */
107
108 #ifdef COMPILE_DL_SYSVSHM
109 ZEND_GET_MODULE(sysvshm)
110 #endif
111
112 #undef shm_ptr /* undefine AIX-specific macro */
113
114 #define SHM_FETCH_RESOURCE(shm_ptr, z_ptr) do { \
115 if ((shm_ptr = (sysvshm_shm *)zend_fetch_resource(Z_RES_P(z_ptr), PHP_SHM_RSRC_NAME, php_sysvshm.le_shm)) == NULL) { \
116 RETURN_FALSE; \
117 } \
118 } while (0)
119
120 THREAD_LS sysvshm_module php_sysvshm;
121
122 static int php_put_shm_data(sysvshm_chunk_head *ptr, zend_long key, const char *data, zend_long len);
123 static zend_long php_check_shm_data(sysvshm_chunk_head *ptr, zend_long key);
124 static int php_remove_shm_data(sysvshm_chunk_head *ptr, zend_long shm_varpos);
125
126 /* {{{ php_release_sysvshm
127 */
php_release_sysvshm(zend_resource * rsrc)128 static void php_release_sysvshm(zend_resource *rsrc)
129 {
130 sysvshm_shm *shm_ptr = (sysvshm_shm *) rsrc->ptr;
131 shmdt((void *) shm_ptr->ptr);
132 efree(shm_ptr);
133 }
134 /* }}} */
135
136 /* {{{ PHP_MINIT_FUNCTION
137 */
PHP_MINIT_FUNCTION(sysvshm)138 PHP_MINIT_FUNCTION(sysvshm)
139 {
140 php_sysvshm.le_shm = zend_register_list_destructors_ex(php_release_sysvshm, NULL, PHP_SHM_RSRC_NAME, module_number);
141
142 if (cfg_get_long("sysvshm.init_mem", &php_sysvshm.init_mem) == FAILURE) {
143 php_sysvshm.init_mem=10000;
144 }
145 return SUCCESS;
146 }
147 /* }}} */
148
149 /* {{{ PHP_MINFO_FUNCTION
150 */
PHP_MINFO_FUNCTION(sysvshm)151 PHP_MINFO_FUNCTION(sysvshm)
152 {
153 php_info_print_table_start();
154 php_info_print_table_row(2, "sysvshm support", "enabled");
155 php_info_print_table_end();
156 }
157 /* }}} */
158
159 /* {{{ proto int shm_attach(int key [, int memsize [, int perm]])
160 Creates or open a shared memory segment */
PHP_FUNCTION(shm_attach)161 PHP_FUNCTION(shm_attach)
162 {
163 sysvshm_shm *shm_list_ptr;
164 char *shm_ptr;
165 sysvshm_chunk_head *chunk_ptr;
166 zend_long shm_key, shm_id, shm_size = php_sysvshm.init_mem, shm_flag = 0666;
167
168 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|ll", &shm_key, &shm_size, &shm_flag)) {
169 return;
170 }
171
172 if (shm_size < 1) {
173 php_error_docref(NULL, E_WARNING, "Segment size must be greater than zero");
174 RETURN_FALSE;
175 }
176
177 shm_list_ptr = (sysvshm_shm *) emalloc(sizeof(sysvshm_shm));
178
179 /* get the id from a specified key or create new shared memory */
180 if ((shm_id = shmget(shm_key, 0, 0)) < 0) {
181 if (shm_size < (zend_long)sizeof(sysvshm_chunk_head)) {
182 php_error_docref(NULL, E_WARNING, "failed for key 0x" ZEND_XLONG_FMT ": memorysize too small", shm_key);
183 efree(shm_list_ptr);
184 RETURN_FALSE;
185 }
186 if ((shm_id = shmget(shm_key, shm_size, shm_flag | IPC_CREAT | IPC_EXCL)) < 0) {
187 php_error_docref(NULL, E_WARNING, "failed for key 0x" ZEND_XLONG_FMT ": %s", shm_key, strerror(errno));
188 efree(shm_list_ptr);
189 RETURN_FALSE;
190 }
191 }
192
193 if ((shm_ptr = shmat(shm_id, NULL, 0)) == (void *) -1) {
194 php_error_docref(NULL, E_WARNING, "failed for key 0x" ZEND_XLONG_FMT ": %s", shm_key, strerror(errno));
195 efree(shm_list_ptr);
196 RETURN_FALSE;
197 }
198
199 /* check if shm is already initialized */
200 chunk_ptr = (sysvshm_chunk_head *) shm_ptr;
201 if (strcmp((char*) &(chunk_ptr->magic), "PHP_SM") != 0) {
202 strcpy((char*) &(chunk_ptr->magic), "PHP_SM");
203 chunk_ptr->start = sizeof(sysvshm_chunk_head);
204 chunk_ptr->end = chunk_ptr->start;
205 chunk_ptr->total = shm_size;
206 chunk_ptr->free = shm_size-chunk_ptr->end;
207 }
208
209 shm_list_ptr->key = shm_key;
210 shm_list_ptr->id = shm_id;
211 shm_list_ptr->ptr = chunk_ptr;
212
213 RETURN_RES(zend_register_resource(shm_list_ptr, php_sysvshm.le_shm));
214 }
215 /* }}} */
216
217 /* {{{ proto bool shm_detach(resource shm_identifier)
218 Disconnects from shared memory segment */
PHP_FUNCTION(shm_detach)219 PHP_FUNCTION(shm_detach)
220 {
221 zval *shm_id;
222 sysvshm_shm *shm_list_ptr;
223
224 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "r", &shm_id)) {
225 return;
226 }
227 SHM_FETCH_RESOURCE(shm_list_ptr, shm_id);
228 RETURN_BOOL(SUCCESS == zend_list_close(Z_RES_P(shm_id)));
229 }
230 /* }}} */
231
232 /* {{{ proto bool shm_remove(resource shm_identifier)
233 Removes shared memory from Unix systems */
PHP_FUNCTION(shm_remove)234 PHP_FUNCTION(shm_remove)
235 {
236 zval *shm_id;
237 sysvshm_shm *shm_list_ptr;
238
239 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "r", &shm_id)) {
240 return;
241 }
242 SHM_FETCH_RESOURCE(shm_list_ptr, shm_id);
243
244 if (shmctl(shm_list_ptr->id, IPC_RMID, NULL) < 0) {
245 php_error_docref(NULL, E_WARNING, "failed for key 0x%x, id " ZEND_LONG_FMT ": %s", shm_list_ptr->key, Z_LVAL_P(shm_id), strerror(errno));
246 RETURN_FALSE;
247 }
248
249 RETURN_TRUE;
250 }
251 /* }}} */
252
253 /* {{{ proto bool shm_put_var(resource shm_identifier, int variable_key, mixed variable)
254 Inserts or updates a variable in shared memory */
PHP_FUNCTION(shm_put_var)255 PHP_FUNCTION(shm_put_var)
256 {
257 zval *shm_id, *arg_var;
258 int ret;
259 zend_long shm_key;
260 sysvshm_shm *shm_list_ptr;
261 smart_str shm_var = {0};
262 php_serialize_data_t var_hash;
263
264 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &shm_id, &shm_key, &arg_var)) {
265 return;
266 }
267
268 /* setup string-variable and serialize */
269 PHP_VAR_SERIALIZE_INIT(var_hash);
270 php_var_serialize(&shm_var, arg_var, &var_hash);
271 PHP_VAR_SERIALIZE_DESTROY(var_hash);
272
273 shm_list_ptr = zend_fetch_resource(Z_RES_P(shm_id), PHP_SHM_RSRC_NAME, php_sysvshm.le_shm);
274 if (!shm_list_ptr) {
275 smart_str_free(&shm_var);
276 RETURN_FALSE;
277 }
278
279 /* insert serialized variable into shared memory */
280 ret = php_put_shm_data(shm_list_ptr->ptr, shm_key, shm_var.s? ZSTR_VAL(shm_var.s) : NULL, shm_var.s? ZSTR_LEN(shm_var.s) : 0);
281
282 /* free string */
283 smart_str_free(&shm_var);
284
285 if (ret == -1) {
286 php_error_docref(NULL, E_WARNING, "not enough shared memory left");
287 RETURN_FALSE;
288 }
289 RETURN_TRUE;
290 }
291 /* }}} */
292
293 /* {{{ proto mixed shm_get_var(resource id, int variable_key)
294 Returns a variable from shared memory */
PHP_FUNCTION(shm_get_var)295 PHP_FUNCTION(shm_get_var)
296 {
297 zval *shm_id;
298 zend_long shm_key;
299 sysvshm_shm *shm_list_ptr;
300 char *shm_data;
301 zend_long shm_varpos;
302 sysvshm_chunk *shm_var;
303 php_unserialize_data_t var_hash;
304
305 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &shm_id, &shm_key)) {
306 return;
307 }
308 SHM_FETCH_RESOURCE(shm_list_ptr, shm_id);
309
310 /* setup string-variable and serialize */
311 /* get serialized variable from shared memory */
312 shm_varpos = php_check_shm_data((shm_list_ptr->ptr), shm_key);
313
314 if (shm_varpos < 0) {
315 php_error_docref(NULL, E_WARNING, "variable key " ZEND_LONG_FMT " doesn't exist", shm_key);
316 RETURN_FALSE;
317 }
318 shm_var = (sysvshm_chunk*) ((char *)shm_list_ptr->ptr + shm_varpos);
319 shm_data = &shm_var->mem;
320
321 PHP_VAR_UNSERIALIZE_INIT(var_hash);
322 if (php_var_unserialize(return_value, (const unsigned char **) &shm_data, (unsigned char *) shm_data + shm_var->length, &var_hash) != 1) {
323 php_error_docref(NULL, E_WARNING, "variable data in shared memory is corrupted");
324 RETVAL_FALSE;
325 }
326 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
327 }
328 /* }}} */
329
330 /* {{{ proto bool shm_has_var(resource id, int variable_key)
331 Checks whether a specific entry exists */
PHP_FUNCTION(shm_has_var)332 PHP_FUNCTION(shm_has_var)
333 {
334 zval *shm_id;
335 zend_long shm_key;
336 sysvshm_shm *shm_list_ptr;
337
338 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &shm_id, &shm_key)) {
339 return;
340 }
341 SHM_FETCH_RESOURCE(shm_list_ptr, shm_id);
342 RETURN_BOOL(php_check_shm_data(shm_list_ptr->ptr, shm_key) >= 0);
343 }
344 /* }}} */
345
346 /* {{{ proto bool shm_remove_var(resource id, int variable_key)
347 Removes variable from shared memory */
PHP_FUNCTION(shm_remove_var)348 PHP_FUNCTION(shm_remove_var)
349 {
350 zval *shm_id;
351 zend_long shm_key, shm_varpos;
352 sysvshm_shm *shm_list_ptr;
353
354 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &shm_id, &shm_key)) {
355 return;
356 }
357 SHM_FETCH_RESOURCE(shm_list_ptr, shm_id);
358
359 shm_varpos = php_check_shm_data((shm_list_ptr->ptr), shm_key);
360
361 if (shm_varpos < 0) {
362 php_error_docref(NULL, E_WARNING, "variable key " ZEND_LONG_FMT " doesn't exist", shm_key);
363 RETURN_FALSE;
364 }
365 php_remove_shm_data((shm_list_ptr->ptr), shm_varpos);
366 RETURN_TRUE;
367 }
368 /* }}} */
369
370 /* {{{ php_put_shm_data
371 * inserts an ascii-string into shared memory */
php_put_shm_data(sysvshm_chunk_head * ptr,zend_long key,const char * data,zend_long len)372 static int php_put_shm_data(sysvshm_chunk_head *ptr, zend_long key, const char *data, zend_long len)
373 {
374 sysvshm_chunk *shm_var;
375 zend_long total_size;
376 zend_long shm_varpos;
377
378 total_size = ((zend_long) (len + sizeof(sysvshm_chunk) - 1) / sizeof(zend_long)) * sizeof(zend_long) + sizeof(zend_long); /* zend_long alligment */
379
380 if ((shm_varpos = php_check_shm_data(ptr, key)) > 0) {
381 php_remove_shm_data(ptr, shm_varpos);
382 }
383
384 if (ptr->free < total_size) {
385 return -1; /* not enough memory */
386 }
387
388 shm_var = (sysvshm_chunk *) ((char *) ptr + ptr->end);
389 shm_var->key = key;
390 shm_var->length = len;
391 shm_var->next = total_size;
392 memcpy(&(shm_var->mem), data, len);
393 ptr->end += total_size;
394 ptr->free -= total_size;
395 return 0;
396 }
397 /* }}} */
398
399 /* {{{ php_check_shm_data
400 */
php_check_shm_data(sysvshm_chunk_head * ptr,zend_long key)401 static zend_long php_check_shm_data(sysvshm_chunk_head *ptr, zend_long key)
402 {
403 zend_long pos;
404 sysvshm_chunk *shm_var;
405
406 pos = ptr->start;
407
408 for (;;) {
409 if (pos >= ptr->end) {
410 return -1;
411 }
412 shm_var = (sysvshm_chunk*) ((char *) ptr + pos);
413 if (shm_var->key == key) {
414 return pos;
415 }
416 pos += shm_var->next;
417
418 if (shm_var->next <= 0 || pos < ptr->start) {
419 return -1;
420 }
421 }
422 return -1;
423 }
424 /* }}} */
425
426 /* {{{ php_remove_shm_data
427 */
php_remove_shm_data(sysvshm_chunk_head * ptr,zend_long shm_varpos)428 static int php_remove_shm_data(sysvshm_chunk_head *ptr, zend_long shm_varpos)
429 {
430 sysvshm_chunk *chunk_ptr, *next_chunk_ptr;
431 zend_long memcpy_len;
432
433 chunk_ptr = (sysvshm_chunk *) ((char *) ptr + shm_varpos);
434 next_chunk_ptr = (sysvshm_chunk *) ((char *) ptr + shm_varpos + chunk_ptr->next);
435
436 memcpy_len = ptr->end-shm_varpos - chunk_ptr->next;
437 ptr->free += chunk_ptr->next;
438 ptr->end -= chunk_ptr->next;
439 if (memcpy_len > 0) {
440 memmove(chunk_ptr, next_chunk_ptr, memcpy_len);
441 }
442 return 0;
443 }
444 /* }}} */
445
446 #endif /* HAVE_SYSVSHM */
447