1999-04-22 10:48:28 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 23:34:45 +08:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-05-18 23:34:45 +08:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-07-16 21:13:16 +08:00
|
|
|
| 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. |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-07-11 03:46:29 +08:00
|
|
|
| Authors: Christian Cartus <cartus@atrior.de> |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2001-05-15 15:32:34 +08:00
|
|
|
/* This has been built and tested on Linux 2.2.14
|
|
|
|
*
|
|
|
|
* This has been built and tested on Solaris 2.6.
|
1999-04-22 10:48:28 +08:00
|
|
|
* It may not compile or execute correctly on other systems.
|
|
|
|
*/
|
|
|
|
|
2001-05-24 18:07:29 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
1999-05-21 16:20:13 +08:00
|
|
|
#include "php.h"
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
#if HAVE_SYSVSHM
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
1999-12-05 03:19:57 +08:00
|
|
|
#include "php_sysvshm.h"
|
2001-08-04 03:11:31 +08:00
|
|
|
#include "ext/standard/php_var.h"
|
|
|
|
#include "ext/standard/php_smart_str.h"
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ sysvshm_functions[]
|
|
|
|
*/
|
1999-04-22 10:48:28 +08:00
|
|
|
function_entry sysvshm_functions[] = {
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_FE(shm_attach, NULL)
|
|
|
|
PHP_FE(shm_remove, NULL)
|
|
|
|
PHP_FE(shm_detach, NULL)
|
|
|
|
PHP_FE(shm_put_var, NULL)
|
|
|
|
PHP_FE(shm_get_var, NULL)
|
|
|
|
PHP_FE(shm_remove_var, NULL)
|
|
|
|
{0}
|
1999-04-22 10:48:28 +08:00
|
|
|
};
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ sysvshm_module_entry
|
|
|
|
*/
|
1999-09-25 03:15:17 +08:00
|
|
|
zend_module_entry sysvshm_module_entry = {
|
2001-11-22 23:14:49 +08:00
|
|
|
STANDARD_MODULE_HEADER,
|
2000-03-07 02:44:01 +08:00
|
|
|
"sysvshm", sysvshm_functions,
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_MINIT(sysvshm), NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL,
|
2001-11-22 23:14:49 +08:00
|
|
|
NO_VERSION_YET,
|
1999-09-25 03:15:17 +08:00
|
|
|
STANDARD_MODULE_PROPERTIES
|
1999-04-22 10:48:28 +08:00
|
|
|
};
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-05-23 17:33:51 +08:00
|
|
|
#ifdef COMPILE_DL_SYSVSHM
|
2000-05-02 08:30:36 +08:00
|
|
|
ZEND_GET_MODULE(sysvshm)
|
1999-04-22 10:48:28 +08:00
|
|
|
#endif
|
|
|
|
|
2000-01-13 02:21:39 +08:00
|
|
|
#undef shm_ptr /* undefine AIX-specific macro */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
1999-12-18 12:01:20 +08:00
|
|
|
THREAD_LS sysvshm_module php_sysvshm;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_release_sysvshm
|
|
|
|
*/
|
2001-07-31 13:44:11 +08:00
|
|
|
static void php_release_sysvshm(zend_rsrc_list_entry *rsrc)
|
1999-08-09 23:03:09 +08:00
|
|
|
{
|
2000-10-21 02:25:16 +08:00
|
|
|
sysvshm_shm *shm_ptr = (sysvshm_shm *)rsrc->ptr;
|
1999-04-22 10:48:28 +08:00
|
|
|
shmdt((void*)shm_ptr->ptr);
|
1999-08-09 23:03:09 +08:00
|
|
|
efree(shm_ptr);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION
|
|
|
|
*/
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_MINIT_FUNCTION(sysvshm)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2000-10-26 01:44:02 +08:00
|
|
|
php_sysvshm.le_shm = zend_register_list_destructors_ex(php_release_sysvshm, NULL, "sysvshm", module_number);
|
1999-04-22 10:48:28 +08:00
|
|
|
if (cfg_get_long("sysvshm.init_mem",
|
1999-12-18 12:01:20 +08:00
|
|
|
&php_sysvshm.init_mem)==FAILURE) {
|
|
|
|
php_sysvshm.init_mem=10000;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-07-11 03:46:29 +08:00
|
|
|
/* {{{ proto int shm_attach(int key [, int memsize [, int perm]])
|
2000-07-11 04:00:02 +08:00
|
|
|
Creates or open a shared memory segment */
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_FUNCTION(shm_attach)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-09-25 03:15:17 +08:00
|
|
|
pval **arg_key,**arg_size,**arg_flag;
|
1999-04-22 10:48:28 +08:00
|
|
|
long shm_size,shm_flag;
|
|
|
|
sysvshm_shm *shm_list_ptr;
|
|
|
|
char *shm_ptr;
|
|
|
|
sysvshm_chunk_head *chunk_ptr;
|
1999-12-12 18:55:27 +08:00
|
|
|
key_t shm_key = (key_t) 0;
|
1999-04-22 10:48:28 +08:00
|
|
|
long shm_id,list_id;
|
2000-06-06 03:47:54 +08:00
|
|
|
int ac = ZEND_NUM_ARGS();
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
shm_flag = 0666;
|
1999-12-18 12:01:20 +08:00
|
|
|
shm_size = php_sysvshm.init_mem;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
1999-12-19 06:40:35 +08:00
|
|
|
if(ac < 1 || ac > 3 || zend_get_parameters_ex(ac, &arg_key, &arg_size, &arg_flag) == FAILURE) {
|
1999-09-25 03:15:17 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
1999-09-25 03:15:17 +08:00
|
|
|
switch (ac) {
|
|
|
|
case 3:
|
|
|
|
convert_to_long_ex(arg_flag);
|
2001-09-26 05:58:48 +08:00
|
|
|
shm_flag = Z_LVAL_PP(arg_flag);
|
1999-09-25 03:15:17 +08:00
|
|
|
case 2:
|
|
|
|
convert_to_long_ex(arg_size);
|
2001-09-26 05:58:48 +08:00
|
|
|
shm_size= Z_LVAL_PP(arg_size);
|
1999-09-25 03:15:17 +08:00
|
|
|
case 1:
|
|
|
|
convert_to_long_ex(arg_key);
|
2001-09-26 05:58:48 +08:00
|
|
|
shm_key = Z_LVAL_PP(arg_key);
|
1999-09-25 03:15:17 +08:00
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
if((shm_list_ptr = (sysvshm_shm *) emalloc(sizeof(sysvshm_shm)))==NULL) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "shm_attach() failed for key 0x%x: cannot allocate internal listelement", shm_key);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the id from a specified key or create new shared memory */
|
|
|
|
if((shm_id=shmget(shm_key,0,0))<0) {
|
|
|
|
if(shm_size<sizeof(sysvshm_chunk_head)) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "shm_attach() failed for key 0x%x: memorysize too small", shm_key);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
if((shm_id=shmget(shm_key,shm_size,shm_flag|IPC_CREAT|IPC_EXCL))<0) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "shmget() failed for key 0x%x: %s", shm_key, strerror(errno));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-18 20:22:28 +08:00
|
|
|
if((shm_ptr = shmat(shm_id,NULL,0))==(void *)-1) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "shmget() failed for key 0x%x: %s", shm_key, strerror(errno));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if shm is already initialized */
|
|
|
|
chunk_ptr= (sysvshm_chunk_head*) shm_ptr;
|
2000-02-26 11:20:55 +08:00
|
|
|
if(strcmp((char*)&(chunk_ptr->magic),"PHP_SM")!=0) {
|
|
|
|
strcpy((char*)&(chunk_ptr->magic),"PHP_SM");
|
1999-04-22 10:48:28 +08:00
|
|
|
chunk_ptr->start=sizeof(sysvshm_chunk_head);
|
|
|
|
chunk_ptr->end=chunk_ptr->start;
|
|
|
|
chunk_ptr->total=shm_size;
|
|
|
|
chunk_ptr->free=shm_size-chunk_ptr->end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-25 03:15:17 +08:00
|
|
|
shm_list_ptr->key = shm_key;
|
1999-04-22 10:48:28 +08:00
|
|
|
shm_list_ptr->id = shm_id;
|
|
|
|
shm_list_ptr->ptr = chunk_ptr;
|
1999-12-18 12:01:20 +08:00
|
|
|
list_id = zend_list_insert(shm_list_ptr, php_sysvshm.le_shm);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_LONG(list_id);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2000-07-11 03:46:29 +08:00
|
|
|
/* {{{ proto int shm_detach(int shm_identifier)
|
2000-07-11 04:00:02 +08:00
|
|
|
Disconnects from shared memory segment */
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_FUNCTION(shm_detach)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-09-25 03:15:17 +08:00
|
|
|
pval **arg_id;
|
1999-04-22 10:48:28 +08:00
|
|
|
long id;
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_id) == FAILURE) {
|
1999-09-25 03:15:17 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
1999-09-25 03:15:17 +08:00
|
|
|
convert_to_long_ex(arg_id);
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
id = Z_LVAL_PP(arg_id);
|
1999-09-25 03:15:17 +08:00
|
|
|
|
1999-12-18 03:51:39 +08:00
|
|
|
zend_list_delete(id);
|
1999-09-25 03:15:17 +08:00
|
|
|
|
|
|
|
RETURN_TRUE;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2001-06-05 21:12:10 +08:00
|
|
|
|
2000-07-11 04:00:02 +08:00
|
|
|
/* {{{ proto int shm_remove(int shm_identifier)
|
|
|
|
Removes shared memory from Unix systems */
|
2001-05-15 15:32:34 +08:00
|
|
|
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_FUNCTION(shm_remove)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2001-05-15 15:32:34 +08:00
|
|
|
pval **arg_id;
|
1999-04-22 10:48:28 +08:00
|
|
|
long id;
|
2001-05-15 15:32:34 +08:00
|
|
|
int type;
|
|
|
|
sysvshm_shm *shm_list_ptr;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-05-15 15:32:34 +08:00
|
|
|
if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_id) == FAILURE) {
|
1999-09-25 03:15:17 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
2001-05-15 15:32:34 +08:00
|
|
|
convert_to_long_ex(arg_id);
|
1999-09-25 03:15:17 +08:00
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
id = Z_LVAL_PP(arg_id);
|
1999-09-25 03:15:17 +08:00
|
|
|
|
2001-05-15 15:32:34 +08:00
|
|
|
shm_list_ptr = (sysvshm_shm *) zend_list_find(id, &type);
|
2001-11-22 22:33:35 +08:00
|
|
|
|
2001-11-22 23:14:49 +08:00
|
|
|
if (!shm_list_ptr) {
|
|
|
|
php_error(E_WARNING, "The parameter is not a valid shm_indentifier");
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-11-22 22:33:35 +08:00
|
|
|
|
2001-05-15 15:32:34 +08:00
|
|
|
if(shmctl(shm_list_ptr->id,IPC_RMID,NULL)<0) {
|
|
|
|
php_error(E_WARNING, "shm_remove() failed for key 0x%x, id %i: %s", shm_list_ptr->key, id,strerror(errno));
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-08-09 23:03:09 +08:00
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2000-08-13 04:45:48 +08:00
|
|
|
/* {{{ proto int shm_put_var(int shm_identifier, int variable_key, mixed variable)
|
2000-07-11 04:00:02 +08:00
|
|
|
Inserts or updates a variable in shared memory */
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_FUNCTION(shm_put_var)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-09-25 03:15:17 +08:00
|
|
|
pval **arg_id, **arg_key, **arg_var;
|
1999-04-22 10:48:28 +08:00
|
|
|
long key, id;
|
|
|
|
sysvshm_shm *shm_list_ptr;
|
|
|
|
int type;
|
2001-08-03 15:25:27 +08:00
|
|
|
smart_str shm_var = {0};
|
1999-04-22 10:48:28 +08:00
|
|
|
int ret;
|
2001-11-11 19:08:31 +08:00
|
|
|
php_serialize_data_t var_hash;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
if(ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg_id, &arg_key,&arg_var) == FAILURE) {
|
1999-09-25 03:15:17 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
1999-09-25 03:15:17 +08:00
|
|
|
|
|
|
|
convert_to_long_ex(arg_id);
|
2001-09-26 05:58:48 +08:00
|
|
|
id = Z_LVAL_PP(arg_id);
|
1999-09-25 03:15:17 +08:00
|
|
|
convert_to_long_ex(arg_key);
|
2001-09-26 05:58:48 +08:00
|
|
|
key = Z_LVAL_PP(arg_key);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
1999-12-18 03:51:39 +08:00
|
|
|
shm_list_ptr = (sysvshm_shm *) zend_list_find(id, &type);
|
1999-12-18 12:01:20 +08:00
|
|
|
if (type!=php_sysvshm.le_shm) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "%d is not a SysV shared memory index", id);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup string-variable and serialize */
|
1999-08-09 23:03:09 +08:00
|
|
|
|
2000-10-31 01:18:39 +08:00
|
|
|
PHP_VAR_SERIALIZE_INIT(var_hash);
|
2001-08-06 00:52:29 +08:00
|
|
|
php_var_serialize(&shm_var,arg_var,&var_hash TSRMLS_CC);
|
2000-10-31 01:18:39 +08:00
|
|
|
PHP_VAR_SERIALIZE_DESTROY(var_hash);
|
1999-04-22 10:48:28 +08:00
|
|
|
/* insert serialized variable into shared memory */
|
2001-08-03 15:25:27 +08:00
|
|
|
ret=php_put_shm_data(shm_list_ptr->ptr,key,shm_var.c,shm_var.len);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
/* free string */
|
2001-08-03 15:25:27 +08:00
|
|
|
smart_str_free(&shm_var);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
if(ret==-1) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "not enough shared memory left");
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2000-07-11 03:46:29 +08:00
|
|
|
/* {{{ proto mixed shm_get_var(int id, int variable_key)
|
2000-07-11 04:00:02 +08:00
|
|
|
Returns a variable from shared memory */
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_FUNCTION(shm_get_var)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-09-25 03:15:17 +08:00
|
|
|
pval **arg_id, **arg_key;
|
1999-04-22 10:48:28 +08:00
|
|
|
long key, id;
|
|
|
|
sysvshm_shm *shm_list_ptr;
|
|
|
|
int type;
|
|
|
|
char *shm_data;
|
|
|
|
long shm_varpos;
|
|
|
|
sysvshm_chunk *shm_var;
|
2001-11-11 19:08:31 +08:00
|
|
|
php_unserialize_data_t var_hash;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_id, &arg_key) == FAILURE) {
|
1999-09-25 03:15:17 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
1999-09-25 03:15:17 +08:00
|
|
|
convert_to_long_ex(arg_id);
|
2001-09-26 05:58:48 +08:00
|
|
|
id = Z_LVAL_PP(arg_id);
|
1999-09-25 03:15:17 +08:00
|
|
|
convert_to_long_ex(arg_key);
|
2001-09-26 05:58:48 +08:00
|
|
|
key = Z_LVAL_PP(arg_key);
|
1999-09-25 03:15:17 +08:00
|
|
|
|
1999-12-18 03:51:39 +08:00
|
|
|
shm_list_ptr = (sysvshm_shm *) zend_list_find(id, &type);
|
1999-12-18 12:01:20 +08:00
|
|
|
if (type!=php_sysvshm.le_shm) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "%d is not a SysV shared memory index", id);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup string-variable and serialize */
|
|
|
|
/* get serialized variable from shared memory */
|
1999-12-18 12:01:20 +08:00
|
|
|
shm_varpos=php_check_shm_data((shm_list_ptr->ptr),key);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
if(shm_varpos<0) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "variable key %d doesn't exist", key);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
shm_var=(sysvshm_chunk*)((char*)shm_list_ptr->ptr+shm_varpos);
|
|
|
|
shm_data=&shm_var->mem;
|
|
|
|
|
2000-10-31 01:18:39 +08:00
|
|
|
PHP_VAR_UNSERIALIZE_INIT(var_hash);
|
2001-08-06 00:52:29 +08:00
|
|
|
if(php_var_unserialize(&return_value, (const char **) &shm_data, shm_data+shm_var->length,&var_hash TSRMLS_CC)!=1) {
|
2000-10-31 01:18:39 +08:00
|
|
|
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "variable data in shared memory is corruped");
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-10-31 01:18:39 +08:00
|
|
|
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2000-08-13 04:45:48 +08:00
|
|
|
/* {{{ proto int shm_remove_var(int id, int variable_key)
|
2000-02-24 22:43:53 +08:00
|
|
|
Removes variable from shared memory */
|
1999-09-25 03:15:17 +08:00
|
|
|
PHP_FUNCTION(shm_remove_var)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
1999-09-25 03:15:17 +08:00
|
|
|
pval **arg_id, **arg_key;
|
1999-04-22 10:48:28 +08:00
|
|
|
long key, id;
|
|
|
|
sysvshm_shm *shm_list_ptr;
|
|
|
|
int type;
|
|
|
|
long shm_varpos;
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg_id, &arg_key) == FAILURE) {
|
1999-09-25 03:15:17 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
1999-09-25 03:15:17 +08:00
|
|
|
convert_to_long_ex(arg_id);
|
2001-09-26 05:58:48 +08:00
|
|
|
id = Z_LVAL_PP(arg_id);
|
1999-09-25 03:15:17 +08:00
|
|
|
convert_to_long_ex(arg_key);
|
2001-09-26 05:58:48 +08:00
|
|
|
key = Z_LVAL_PP(arg_key);
|
1999-09-25 03:15:17 +08:00
|
|
|
|
1999-12-18 03:51:39 +08:00
|
|
|
shm_list_ptr = (sysvshm_shm *) zend_list_find(id, &type);
|
1999-12-18 12:01:20 +08:00
|
|
|
if (type!=php_sysvshm.le_shm) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "%d is not a SysV shared memory index", id);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-12-18 12:01:20 +08:00
|
|
|
shm_varpos=php_check_shm_data((shm_list_ptr->ptr),key);
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
if(shm_varpos<0) {
|
1999-08-03 03:17:14 +08:00
|
|
|
php_error(E_WARNING, "variable key %d doesn't exist", key);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-12-18 12:01:20 +08:00
|
|
|
php_remove_shm_data((shm_list_ptr->ptr),shm_varpos);
|
1999-04-22 10:48:28 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_put_shm_data
|
|
|
|
* inserts an ascii-string into shared memory */
|
1999-12-18 12:01:20 +08:00
|
|
|
int php_put_shm_data(sysvshm_chunk_head *ptr,long key,char *data, long len) {
|
1999-04-22 10:48:28 +08:00
|
|
|
sysvshm_chunk* shm_var;
|
|
|
|
long total_size;
|
|
|
|
long shm_varpos;
|
|
|
|
|
|
|
|
total_size=((long)(len+sizeof(sysvshm_chunk)-1)/4)*4+4; /* 4-byte alligment */
|
2001-09-07 20:12:19 +08:00
|
|
|
|
|
|
|
if((shm_varpos=php_check_shm_data(ptr,key))>0) {
|
|
|
|
php_remove_shm_data(ptr, shm_varpos);
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
if(ptr->free<total_size) {
|
|
|
|
return -1; /* not enough memeory */
|
|
|
|
}
|
|
|
|
|
|
|
|
shm_var=(sysvshm_chunk*)((char *)ptr+ptr->end);
|
|
|
|
shm_var->key=key;
|
|
|
|
shm_var->length=len;
|
|
|
|
shm_var->next=total_size;
|
|
|
|
memcpy(&(shm_var->mem),data,len);
|
|
|
|
ptr->end+=total_size;
|
|
|
|
ptr->free-=total_size;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_check_shm_data
|
|
|
|
*/
|
1999-12-18 12:01:20 +08:00
|
|
|
long php_check_shm_data(sysvshm_chunk_head *ptr, long key) {
|
1999-04-22 10:48:28 +08:00
|
|
|
long pos;
|
|
|
|
sysvshm_chunk *shm_var;
|
|
|
|
|
|
|
|
pos=ptr->start;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if(pos>=ptr->end) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
shm_var=(sysvshm_chunk*)((char*)ptr+pos);
|
|
|
|
if(shm_var->key==key) {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
pos+=shm_var->next;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_remove_shm_data
|
|
|
|
*/
|
1999-12-18 12:01:20 +08:00
|
|
|
int php_remove_shm_data(sysvshm_chunk_head *ptr, long shm_varpos) {
|
1999-04-22 10:48:28 +08:00
|
|
|
sysvshm_chunk *chunk_ptr, *next_chunk_ptr;
|
|
|
|
long memcpy_len;
|
|
|
|
|
|
|
|
chunk_ptr=(sysvshm_chunk*)((char*)ptr+shm_varpos);
|
|
|
|
next_chunk_ptr=(sysvshm_chunk*)((char*)ptr+shm_varpos+chunk_ptr->next);
|
|
|
|
|
|
|
|
memcpy_len=ptr->end-shm_varpos-chunk_ptr->next;
|
|
|
|
ptr->free+=chunk_ptr->next;
|
|
|
|
ptr->end-=chunk_ptr->next;
|
|
|
|
if(memcpy_len>0)
|
|
|
|
memcpy(chunk_ptr,next_chunk_ptr,memcpy_len);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
#endif /* HAVE_SYSVSHM */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
1999-04-22 10:48:28 +08:00
|
|
|
*/
|