Patch sysvshm.diff for Semaphore related Bug #53100
Patch version 2010-10-27 21:09 UTC
Return to Bug #53100 |
Download this patch
Patch Revisions:
Developer: marc-bennewitz@arcor.de
Index: tests/009.phpt
===================================================================
--- tests/009.phpt (revision 0)
+++ tests/009.phpt (revision 0)
@@ -0,0 +1,67 @@
+--TEST--
+shm_get_vars() tests
+--SKIPIF--
+<?php if (!extension_loaded("sysvshm")) print "skip"; ?>
+--FILE--
+<?php
+
+// failures
+var_dump(shm_get_vars());
+var_dump(shm_get_vars(1));
+var_dump(shm_get_vars(""));
+
+$key = ftok(dirname(__FILE__)."/009.phpt", 'q');
+
+// open shm segment
+$shmId = shm_attach($key, 1024);
+var_dump($shmId);
+
+// first get
+var_dump(shm_get_vars($shmId));
+
+// write data + get
+var_dump(shm_put_var($shmId, 30, '30'));
+var_dump(shm_put_var($shmId, 20, array('20', 20)));
+var_dump(shm_put_var($shmId, 10, '10'));
+var_dump(shm_get_vars($shmId));
+
+echo "Done\n";
+?>
+--CLEAN--
+<?php
+
+$key = ftok(dirname(__FILE__)."/009.phpt", 'q');
+$s = shm_attach($key);
+shm_remove($s);
+
+?>
+--EXPECTF--
+
+Warning: shm_get_vars() expects exactly 1 parameter, 0 given in %s on line %d
+NULL
+
+Warning: shm_get_vars() expects parameter 1 to be resource, integer given in %s on line %d
+NULL
+
+Warning: shm_get_vars() expects parameter 1 to be resource, string given in %s on line %d
+NULL
+resource(%d) of type (sysvshm)
+array(0) {
+}
+bool(true)
+bool(true)
+bool(true)
+array(3) {
+ [30]=>
+ string(2) "30"
+ [20]=>
+ array(2) {
+ [0]=>
+ string(2) "20"
+ [1]=>
+ int(20)
+ }
+ [10]=>
+ string(2) "10"
+}
+Done
Index: tests/010.phpt
===================================================================
--- tests/010.phpt (revision 0)
+++ tests/010.phpt (revision 0)
@@ -0,0 +1,62 @@
+--TEST--
+shm_list_vars() tests
+--SKIPIF--
+<?php if (!extension_loaded("sysvshm")) print "skip"; ?>
+--FILE--
+<?php
+
+// failures
+var_dump(shm_list_vars());
+var_dump(shm_list_vars(1));
+var_dump(shm_list_vars(""));
+
+$key = ftok(dirname(__FILE__)."/010.phpt", 'q');
+
+// open shm segment
+$shmId = shm_attach($key, 1024);
+var_dump($shmId);
+
+// first list calls
+var_dump(shm_list_vars($shmId));
+
+// write data + list
+var_dump(shm_put_var($shmId, 30, '30'));
+var_dump(shm_put_var($shmId, 20, '20'));
+var_dump(shm_put_var($shmId, 10, '10'));
+var_dump(shm_list_vars($shmId));
+
+echo "Done\n";
+
+?>
+--CLEAN--
+<?php
+
+$key = ftok(dirname(__FILE__)."/010.phpt", 'q');
+$s = shm_attach($key);
+shm_remove($s);
+
+?>
+--EXPECTF--
+Warning: shm_list_vars() expects exactly 1 parameter, 0 given in %s on line %d
+NULL
+
+Warning: shm_list_vars() expects parameter 1 to be resource, integer given in %s on line %d
+NULL
+
+Warning: shm_list_vars() expects parameter 1 to be resource, string given in %s on line %d
+NULL
+resource(%d) of type (sysvshm)
+array(0) {
+}
+bool(true)
+bool(true)
+bool(true)
+array(3) {
+ [0]=>
+ int(30)
+ [1]=>
+ int(20)
+ [2]=>
+ int(10)
+}
+Done
Index: php_sysvshm.h
===================================================================
--- php_sysvshm.h (revision 304930)
+++ php_sysvshm.h (working copy)
@@ -64,6 +64,8 @@
PHP_FUNCTION(shm_remove);
PHP_FUNCTION(shm_put_var);
PHP_FUNCTION(shm_get_var);
+PHP_FUNCTION(shm_get_vars);
+PHP_FUNCTION(shm_list_vars);
PHP_FUNCTION(shm_has_var);
PHP_FUNCTION(shm_remove_var);
Index: sysvshm.c
===================================================================
--- sysvshm.c (revision 304930)
+++ sysvshm.c (working copy)
@@ -70,6 +70,14 @@
ZEND_ARG_INFO(0, variable_key)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_get_vars, 0, 0, 1)
+ ZEND_ARG_INFO(0, id)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_list_vars, 0, 0, 1)
+ ZEND_ARG_INFO(0, shm_identifier)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_remove_var, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, variable_key)
@@ -85,6 +93,8 @@
PHP_FE(shm_put_var, arginfo_shm_put_var)
PHP_FE(shm_has_var, arginfo_shm_has_var)
PHP_FE(shm_get_var, arginfo_shm_get_var)
+ PHP_FE(shm_get_vars, arginfo_shm_get_vars)
+ PHP_FE(shm_list_vars, arginfo_shm_list_vars)
PHP_FE(shm_remove_var, arginfo_shm_remove_var)
{NULL, NULL, NULL}
};
@@ -314,6 +324,89 @@
}
/* }}} */
+/* {{{ proto array shm_get_vars(resource id)
+ * * Get all variables from shared memory */
+PHP_FUNCTION(shm_get_vars)
+{
+ zval *shm_id, *shm_data_value;
+ sysvshm_shm *shm_list_ptr;
+ sysvshm_chunk *shm_var;
+ sysvshm_chunk_head *ptr;
+ php_unserialize_data_t var_hash;
+ char *shm_data;
+ long pos;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &shm_id)) {
+ return;
+ }
+ SHM_FETCH_RESOURCE(shm_list_ptr, shm_id);
+
+ array_init(return_value);
+ ptr = shm_list_ptr->ptr;
+ pos = ptr->start;
+
+ for (;;) {
+ if (pos >= ptr->end) {
+ break;
+ }
+
+ shm_var = (sysvshm_chunk*) ((char *) ptr + pos);
+ shm_data = &shm_var->mem;
+
+ ALLOC_INIT_ZVAL(shm_data_value);
+ PHP_VAR_UNSERIALIZE_INIT(var_hash);
+ if (php_var_unserialize(&shm_data_value, (const unsigned char **) &shm_data, (unsigned char *) shm_data + shm_var->length, &var_hash TSRMLS_CC) != 1) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "variable data in shared memory is corrupted");
+ zval_ptr_dtor(&shm_data_value);
+ RETURN_FALSE;
+ }
+ PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
+
+ add_index_zval(return_value, shm_var->key, shm_data_value);
+
+ pos += shm_var->next;
+ if (shm_var->next <= 0 || pos < ptr->start) {
+ break;
+ }
+ }
+}
+/* }}} */
+
+/* {{{ proto array shm_list_vars(resource id)
+ * List all variables from shared memory */
+PHP_FUNCTION(shm_list_vars)
+{
+ zval *shm_id;
+ sysvshm_shm *shm_list_ptr;
+ sysvshm_chunk *shm_var;
+ sysvshm_chunk_head *ptr;
+ long pos;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &shm_id)) {
+ return;
+ }
+ SHM_FETCH_RESOURCE(shm_list_ptr, shm_id);
+
+ array_init(return_value);
+ ptr = shm_list_ptr->ptr;
+ pos = ptr->start;
+
+ for (;;) {
+ if (pos >= ptr->end) {
+ break;
+ }
+
+ shm_var = (sysvshm_chunk*) ((char *) ptr + pos);
+ add_next_index_long(return_value, shm_var->key);
+
+ pos += shm_var->next;
+ if (shm_var->next <= 0 || pos < ptr->start) {
+ break;
+ }
+ }
+}
+/* }}} */
+
/* {{{ proto bool shm_has_var(resource id, int variable_key)
Checks whether a specific entry exists */
PHP_FUNCTION(shm_has_var)
|