cvs: php4 /ext/shmop .cvsignore Makefile.in README config.m4 php_shmop.h setup.stub shmop.c
| From: | Derick Rethans | Date: | Sun, 01 Oct 2000 15:06:05 +0000 |
| Subject: | cvs: php4 /ext/shmop .cvsignore Makefile.in README config.m4 php_shmop.h setup.stub shmop.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-1900@lists.php.net to get a copy of this message | ||
derick Sun Oct 1 08:06:05 2000 EDT
Added files:
/php4/ext/shmop .cvsignore Makefile.in README config.m4 php_shmop.h
setup.stub shmop.c
Log:
@- Added the shmop extension. It allows more general ways of shared memory
@ access. (thanks to Ilia Alshanestky <iliaa@home.com> and Slava Poliakov
@ <slavapl@mailandnews.com> (Derick)
Index: php4/ext/shmop/.cvsignore
+++ php4/ext/shmop/.cvsignore
deps
Makefile
*.o
*.lo
*.la
libs
libs.mk
Index: php4/ext/shmop/Makefile.in
+++ php4/ext/shmop/Makefile.in
# $Id: Makefile.in,v 1.1 2000/10/01 15:06:04 derick Exp $
LTLIBRARY_NAME = libshmop.la
LTLIBRARY_SOURCES = shmop.c
LTLIBRARY_SHARED_NAME = shmop.la
LTLIBRARY_SHARED_LIBADD = $(SHMOP_SHARED_LIBADD)
include $(top_srcdir)/build/dynlib.mk
Index: php4/ext/shmop/config.m4
+++ php4/ext/shmop/config.m4
dnl $Id: config.m4,v 1.1 2000/10/01 15:06:04 derick Exp $
PHP_ARG_ENABLE(shmop, whether to enable shmop support,
[ --enable-shmop Enable shmop support])
if test "$PHP_SHMOP" != "no"; then
AC_DEFINE(HAVE_SHMOP, 1, [ ])
PHP_EXTENSION(shmop, $ext_shared)
fi
Index: php4/ext/shmop/php_shmop.h
+++ php4/ext/shmop/php_shmop.h
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.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 (slavapl@mailandnews.com) |
| Ilia Alshanetsky (iliaa@home.com) |
+----------------------------------------------------------------------+
*/
#ifndef PHP_SHMOP_H
#define PHP_SHMOP_H
#if HAVE_SHMOP
extern zend_module_entry shmop_module_entry;
#define phpext_shmop_ptr &shmop_module_entry
#ifdef PHP_WIN32
#define PHP_SHMOP_API __declspec(dllexport)
#else
#define PHP_SHMOP_API
#endif
PHP_MINIT_FUNCTION(shmop);
PHP_MSHUTDOWN_FUNCTION(shmop);
PHP_RINIT_FUNCTION(shmop);
PHP_RSHUTDOWN_FUNCTION(shmop);
PHP_MINFO_FUNCTION(shmop);
PHP_FUNCTION(shm_open);
PHP_FUNCTION(shm_read);
PHP_FUNCTION(shm_close);
PHP_FUNCTION(shm_size);
PHP_FUNCTION(shm_write);
PHP_FUNCTION(shm_delete);
struct php_shmop
{
int shmid;
key_t key;
int shmflg;
char *addr;
int size;
};
static void rsclean(struct php_shmop *shmop);
typedef struct {
int le_shmop;
} php_shmop_globals;
#ifdef ZTS
#define SHMOPG(v) (shmop_globals->v)
#define SHMOPLS_FETCH() php_shmop_globals *shmop_globals = ts_resource(gd_shmop_id)
#else
#define SHMOPG(v) (shmop_globals.v)
#define SHMOPLS_FETCH()
#endif
#else
#define phpext_shmop_ptr NULL
#endif
#endif /* PHP_SHMOP_H */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
Index: php4/ext/shmop/setup.stub
+++ php4/ext/shmop/setup.stub
# $Source: /local/repository/php4/ext/shmop/setup.stub,v $
# $Id: setup.stub,v 1.1 2000/10/01 15:06:04 derick Exp $
define_option enable-shmop 'System V shmop suppport? ' yesno no \
' Whether to use the System V shmop (currently only Solaris and Linux).'
Index: php4/ext/shmop/shmop.c
+++ php4/ext/shmop/shmop.c
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.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 (slavapl@mailandnews.com) |
| Ilia Alshanetsky (iliaa@home.com) |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_ini.h"
#include "php_shmop.h"
#include <sys/ipc.h>
#include <sys/shm.h>
#if HAVE_SHMOP
#ifdef ZTS
int shmop_globals_id;
#else
php_shmop_globals shmop_globals;
#endif
int shm_type;
/* Every user visible function must have an entry in shmop_functions[].
*/
function_entry shmop_functions[] = {
PHP_FE(shm_open, NULL)
PHP_FE(shm_read, NULL)
PHP_FE(shm_close, NULL)
PHP_FE(shm_size, NULL)
PHP_FE(shm_write, NULL)
PHP_FE(shm_delete, NULL)
{NULL, NULL, NULL} /* Must be the last line in shmop_functions[] */
};
zend_module_entry shmop_module_entry = {
"shmop",
shmop_functions,
PHP_MINIT(shmop),
PHP_MSHUTDOWN(shmop),
NULL,
NULL,
PHP_MINFO(shmop),
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_SHMOP
ZEND_GET_MODULE(shmop)
#endif
PHP_MINIT_FUNCTION(shmop)
{
shm_type = register_list_destructors(rsclean, NULL);
return SUCCESS;
}
static void rsclean(struct php_shmop *shmop)
{
shmdt(shmop->addr);
efree(shmop);
}
PHP_MSHUTDOWN_FUNCTION(shmop)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(shmop)
{
php_info_print_table_start();
php_info_print_table_header(2, "shmop support", "enabled");
php_info_print_table_end();
}
/* {{{ void shm_open (int key, int flags, int mode, int size)
shm_open - gets and attaches an shared memory segment */
PHP_FUNCTION(shm_open)
{
zval **key, **flags, **mode, **size;
struct php_shmop *shmop;
struct shmid_ds shm;
int rsid;
int shmflg=0;
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &key, &flags, &mode, &size)
== FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(key);
convert_to_string_ex(flags);
convert_to_long_ex(mode);
convert_to_long_ex(size);
shmop = emalloc(sizeof(struct php_shmop));
memset(shmop, 0, sizeof(struct php_shmop));
shmop->key = (*key)->value.lval;
shmop->shmflg |= (*mode)->value.lval;
if (memchr((*flags)->value.str.val, 'a', (*flags)->value.str.len)) {
shmflg = SHM_RDONLY;
shmop->shmflg |= IPC_EXCL;
}
else if (memchr((*flags)->value.str.val, 'c', (*flags)->value.str.len)) {
shmop->shmflg |= IPC_CREAT;
shmop->size = (*size)->value.lval;
}
else {
php_error(E_WARNING, "shmopen: access mode invalid");
efree(shmop);
RETURN_FALSE;
}
shmop->shmid = shmget(shmop->key, shmop->size, shmop->shmflg);
if (shmop->shmid == -1) {
php_error(E_WARNING, "shmopen: can't get the block");
efree(shmop);
RETURN_FALSE;
}
if (shmctl(shmop->shmid, IPC_STAT, &shm)) {
efree(shmop);
php_error(E_WARNING, "shmopen: can't get information on the block");
RETURN_FALSE;
}
shmop->addr = shmat(shmop->shmid, 0, shmflg);
if (shmop->addr == (char*) -1) {
efree(shmop);
php_error(E_WARNING, "shmopen: can't attach the memory block");
RETURN_FALSE;
}
shmop->size = shm.shm_segsz;
rsid = zend_list_insert(shmop, shm_type);
RETURN_LONG(rsid);
}
/* }}} */
/* {{{ string shm_read (int shmid, int start, int count)
shm_read - reads from an shm segment */
PHP_FUNCTION(shm_read)
{
zval **shmid, **start, **count;
struct php_shmop *shmop;
int type;
char *startaddr;
int bytes;
char *return_string;
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &shmid, &start, &count) ==
FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(shmid);
convert_to_long_ex(start);
convert_to_long_ex(count);
shmop = zend_list_find((*shmid)->value.lval, &type);
if (!shmop) {
php_error(E_WARNING, "shmread: can't find this segment");
RETURN_FALSE;
}
if ((*start)->value.lval < 0 || (*start)->value.lval > shmop->size) {
php_error(E_WARNING, "shmread: start is out of range");
RETURN_FALSE;
}
if (((*start)->value.lval+(*count)->value.lval) > shmop->size) {
php_error(E_WARNING, "shmread: count is out of range");
RETURN_FALSE;
}
if ((*count)->value.lval < 0 ){
php_error(E_WARNING, "shmread: count is out of range");
RETURN_FALSE;
}
startaddr = shmop->addr + (*start)->value.lval;
bytes = (*count)->value.lval ? (*count)->value.lval : shmop->size-(*start)->value.lval;
return_string = emalloc(bytes);
memcpy(return_string, startaddr, bytes);
RETURN_STRINGL(return_string, bytes, 0);
}
/* }}} */
/* {{{ void shm_close (int shmid)
shm_close - closes a shared memory segment */
PHP_FUNCTION(shm_close)
{
zval **shmid;
struct php_shmop *shmop;
int type;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &shmid) == FAILURE) {
WRONG_PARAM_COUNT;
}
shmop = zend_list_find((*shmid)->value.lval, &type);
if (!shmop) {
php_error(E_WARNING, "shmclose: no such shmid");
RETURN_FALSE;
}
zend_list_delete((*shmid)->value.lval);
RETURN_LONG(0);
}
/* }}} */
/* {{{ int shm_size (int shmid)
shm_size - returns the shm size */
PHP_FUNCTION(shm_size)
{
zval **shmid;
struct php_shmop *shmop;
int type;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &shmid) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(shmid);
shmop = zend_list_find((*shmid)->value.lval, &type);
if (!shmop) {
php_error(E_WARNING, "shmsize: no such segment");
RETURN_FALSE;
}
RETURN_LONG(shmop->size);
}
/* }}} */
/* {{{ int shm_write (int shmid, string data, int offset)
shm_write - writes to a shared memory segment */
PHP_FUNCTION(shm_write)
{
zval **shmid, **data, **offset;
struct php_shmop *shmop;
int type;
int writesize;
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &shmid, &data, &offset) ==
FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(shmid);
convert_to_string_ex(data);
convert_to_long_ex(offset);
shmop = zend_list_find((*shmid)->value.lval, &type);
if (!shmop) {
php_error(E_WARNING, "shmwrite: error no such segment");
RETURN_FALSE;
}
if ( (*offset)->value.lval > shmop->size ) {
php_error(E_WARNING, "shmwrite: offset out of range");
RETURN_FALSE;
}
writesize = ((*data)->value.str.len<shmop->size-(*offset)->value.lval) ?
(*data)->value.str.len : shmop->size-(*offset)->value.lval;
memcpy(shmop->addr+(*offset)->value.lval, (*data)->value.str.val, writesize);
RETURN_LONG(writesize);
}
/* }}} */
/* {{{ bool shm_delete (int shmid)
shm_delete - mark segment for deletion */
PHP_FUNCTION(shm_delete)
{
zval **shmid;
struct php_shmop *shmop;
int type;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &shmid) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(shmid);
shmop = zend_list_find((*shmid)->value.lval, &type);
if (!shmop) {
php_error(E_WARNING, "shmdelete: error no such segment");
RETURN_FALSE;
}
if (shmctl(shmop->shmid, IPC_RMID, NULL)) {
php_error(E_WARNING, "shmdelete: 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:
*/