summaryrefslogtreecommitdiff
path: root/ext/sysvshm
diff options
context:
space:
mode:
Diffstat (limited to 'ext/sysvshm')
-rw-r--r--ext/sysvshm/Makefile.am6
-rw-r--r--ext/sysvshm/Makefile.in270
-rw-r--r--ext/sysvshm/config.h.stub3
-rw-r--r--ext/sysvshm/config.m417
-rw-r--r--ext/sysvshm/php3_sysvshm.h101
-rw-r--r--ext/sysvshm/setup.stub5
-rw-r--r--ext/sysvshm/sysvshm.c485
7 files changed, 887 insertions, 0 deletions
diff --git a/ext/sysvshm/Makefile.am b/ext/sysvshm/Makefile.am
new file mode 100644
index 0000000000..9c759a02bb
--- /dev/null
+++ b/ext/sysvshm/Makefile.am
@@ -0,0 +1,6 @@
+# $Id$
+
+INCLUDES=@INCLUDES@ -I@top_srcdir@ -I@top_srcdir@/libzend
+noinst_LIBRARIES=libphpext_sysvshm.a
+libphpext_sysvshm_a_SOURCES=sysvshm.c
+
diff --git a/ext/sysvshm/Makefile.in b/ext/sysvshm/Makefile.in
new file mode 100644
index 0000000000..27a168ca22
--- /dev/null
+++ b/ext/sysvshm/Makefile.in
@@ -0,0 +1,270 @@
+# Makefile.in generated automatically by automake 1.3 from Makefile.am
+
+# Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+# $Id$
+
+
+SHELL = /bin/sh
+
+srcdir = @srcdir@
+top_srcdir = @top_srcdir@
+VPATH = @srcdir@
+prefix = @prefix@
+exec_prefix = @exec_prefix@
+
+bindir = @bindir@
+sbindir = @sbindir@
+libexecdir = @libexecdir@
+datadir = @datadir@
+sysconfdir = @sysconfdir@
+sharedstatedir = @sharedstatedir@
+localstatedir = @localstatedir@
+libdir = @libdir@
+infodir = @infodir@
+mandir = @mandir@
+includedir = @includedir@
+oldincludedir = /usr/include
+
+DISTDIR =
+
+pkgdatadir = $(datadir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+
+top_builddir = ../..
+
+ACLOCAL = @ACLOCAL@
+AUTOCONF = @AUTOCONF@
+AUTOMAKE = @AUTOMAKE@
+AUTOHEADER = @AUTOHEADER@
+
+INSTALL = @INSTALL@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+transform = @program_transform_name@
+
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+APACHE_INCLUDE = @APACHE_INCLUDE@
+APACHE_TARGET = @APACHE_TARGET@
+APXS = @APXS@
+APXS_LDFLAGS = @APXS_LDFLAGS@
+BINNAME = @BINNAME@
+CC = @CC@
+CFLAGS = @CFLAGS@
+CFLAGS_SHLIB = @CFLAGS_SHLIB@
+DEBUG_CFLAGS = @DEBUG_CFLAGS@
+EXTINFO_DEPS = @EXTINFO_DEPS@
+EXTRA_LIBS = @EXTRA_LIBS@
+EXT_LIBS = @EXT_LIBS@
+EXT_SUBDIRS = @EXT_SUBDIRS@
+FHTTPD_LIB = @FHTTPD_LIB@
+FHTTPD_TARGET = @FHTTPD_TARGET@
+HSREGEX = @HSREGEX@
+INSTALL_IT = @INSTALL_IT@
+LDFLAGS_SHLIB = @LDFLAGS_SHLIB@
+LDFLAGS_SHLIB_EXPORT = @LDFLAGS_SHLIB_EXPORT@
+MAINT = @MAINT@
+MAKEINFO = @MAKEINFO@
+PACKAGE = @PACKAGE@
+PERL_PATH = @PERL_PATH@
+PHP_BUILD_DATE = @PHP_BUILD_DATE@
+PHP_DEBUG = @PHP_DEBUG@
+PHP_LIBS = @PHP_LIBS@
+PHP_VERSION = @PHP_VERSION@
+PROG_SENDMAIL = @PROG_SENDMAIL@
+RANLIB = @RANLIB@
+RDYNAMIC_LFLAGS = @RDYNAMIC_LFLAGS@
+REGEX_LIB = @REGEX_LIB@
+STRONGHOLD = @STRONGHOLD@
+VERSION = @VERSION@
+WARNING_LEVEL = @WARNING_LEVEL@
+YACC = @YACC@
+
+INCLUDES=@INCLUDES@ -I@top_srcdir@ -I@top_srcdir@/libzend
+noinst_LIBRARIES=libphpext_sysvshm.a
+libphpext_sysvshm_a_SOURCES=sysvshm.c
+mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
+CONFIG_HEADER = ../../config.h
+CONFIG_CLEAN_FILES =
+LIBRARIES = $(noinst_LIBRARIES)
+
+
+DEFS = @DEFS@ -I. -I$(srcdir) -I../..
+CPPFLAGS = @CPPFLAGS@
+LDFLAGS = @LDFLAGS@
+LIBS = @LIBS@
+libphpext_sysvshm_a_LIBADD =
+libphpext_sysvshm_a_OBJECTS = sysvshm.o
+AR = ar
+COMPILE = $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
+LINK = $(CC) $(CFLAGS) $(LDFLAGS) -o $@
+DIST_COMMON = Makefile.am Makefile.in
+
+
+DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
+
+TAR = tar
+GZIP = --best
+SOURCES = $(libphpext_sysvshm_a_SOURCES)
+OBJECTS = $(libphpext_sysvshm_a_OBJECTS)
+
+all: Makefile $(LIBRARIES)
+
+.SUFFIXES:
+.SUFFIXES: .S .c .o .s
+$(srcdir)/Makefile.in: @MAINT@ Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4)
+ cd $(top_srcdir) && $(AUTOMAKE) --gnu --include-deps ext/sysvshm/Makefile
+
+Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
+ cd $(top_builddir) \
+ && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
+
+
+mostlyclean-noinstLIBRARIES:
+
+clean-noinstLIBRARIES:
+ -test -z "$(noinst_LIBRARIES)" || rm -f $(noinst_LIBRARIES)
+
+distclean-noinstLIBRARIES:
+
+maintainer-clean-noinstLIBRARIES:
+
+.c.o:
+ $(COMPILE) -c $<
+
+.s.o:
+ $(COMPILE) -c $<
+
+.S.o:
+ $(COMPILE) -c $<
+
+mostlyclean-compile:
+ -rm -f *.o core *.core
+
+clean-compile:
+
+distclean-compile:
+ -rm -f *.tab.c
+
+maintainer-clean-compile:
+
+libphpext_sysvshm.a: $(libphpext_sysvshm_a_OBJECTS) $(libphpext_sysvshm_a_DEPENDENCIES)
+ -rm -f libphpext_sysvshm.a
+ $(AR) cru libphpext_sysvshm.a $(libphpext_sysvshm_a_OBJECTS) $(libphpext_sysvshm_a_LIBADD)
+ $(RANLIB) libphpext_sysvshm.a
+
+tags: TAGS
+
+ID: $(HEADERS) $(SOURCES) $(LISP)
+ here=`pwd` && cd $(srcdir) \
+ && mkid -f$$here/ID $(SOURCES) $(HEADERS) $(LISP)
+
+TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) $(LISP)
+ tags=; \
+ here=`pwd`; \
+ list='$(SOURCES) $(HEADERS)'; \
+ unique=`for i in $$list; do echo $$i; done | \
+ awk ' { files[$$0] = 1; } \
+ END { for (i in files) print i; }'`; \
+ test -z "$(ETAGS_ARGS)$$unique$(LISP)$$tags" \
+ || (cd $(srcdir) && etags $(ETAGS_ARGS) $$tags $$unique $(LISP) -o $$here/TAGS)
+
+mostlyclean-tags:
+
+clean-tags:
+
+distclean-tags:
+ -rm -f TAGS ID
+
+maintainer-clean-tags:
+
+distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
+
+subdir = ext/sysvshm
+
+distdir: $(DISTFILES)
+ @for file in $(DISTFILES); do \
+ d=$(srcdir); \
+ test -f $(distdir)/$$file \
+ || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
+ || cp -p $$d/$$file $(distdir)/$$file; \
+ done
+info:
+dvi:
+check: all
+ $(MAKE)
+installcheck:
+install-exec:
+ @$(NORMAL_INSTALL)
+
+install-data:
+ @$(NORMAL_INSTALL)
+
+install: install-exec install-data all
+ @:
+
+uninstall:
+
+install-strip:
+ $(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' INSTALL_SCRIPT='$(INSTALL_PROGRAM)' install
+installdirs:
+
+
+mostlyclean-generic:
+ -test -z "$(MOSTLYCLEANFILES)" || rm -f $(MOSTLYCLEANFILES)
+
+clean-generic:
+ -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES)
+
+distclean-generic:
+ -rm -f Makefile $(DISTCLEANFILES)
+ -rm -f config.cache config.log stamp-h stamp-h[0-9]*
+ -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
+
+maintainer-clean-generic:
+ -test -z "$(MAINTAINERCLEANFILES)" || rm -f $(MAINTAINERCLEANFILES)
+ -test -z "$(BUILT_SOURCES)" || rm -f $(BUILT_SOURCES)
+mostlyclean: mostlyclean-noinstLIBRARIES mostlyclean-compile \
+ mostlyclean-tags mostlyclean-generic
+
+clean: clean-noinstLIBRARIES clean-compile clean-tags clean-generic \
+ mostlyclean
+
+distclean: distclean-noinstLIBRARIES distclean-compile distclean-tags \
+ distclean-generic clean
+ -rm -f config.status
+
+maintainer-clean: maintainer-clean-noinstLIBRARIES \
+ maintainer-clean-compile maintainer-clean-tags \
+ maintainer-clean-generic distclean
+ @echo "This command is intended for maintainers to use;"
+ @echo "it deletes files that may require special tools to rebuild."
+
+.PHONY: mostlyclean-noinstLIBRARIES distclean-noinstLIBRARIES \
+clean-noinstLIBRARIES maintainer-clean-noinstLIBRARIES \
+mostlyclean-compile distclean-compile clean-compile \
+maintainer-clean-compile tags mostlyclean-tags distclean-tags \
+clean-tags maintainer-clean-tags distdir info dvi installcheck \
+install-exec install-data install uninstall all installdirs \
+mostlyclean-generic distclean-generic clean-generic \
+maintainer-clean-generic clean mostlyclean distclean maintainer-clean
+
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/ext/sysvshm/config.h.stub b/ext/sysvshm/config.h.stub
new file mode 100644
index 0000000000..9b5f5e35cb
--- /dev/null
+++ b/ext/sysvshm/config.h.stub
@@ -0,0 +1,3 @@
+/* Define if you want System V shared memory support.
+ */
+#define HAVE_SYSVSHM 0
diff --git a/ext/sysvshm/config.m4 b/ext/sysvshm/config.m4
new file mode 100644
index 0000000000..287841f26d
--- /dev/null
+++ b/ext/sysvshm/config.m4
@@ -0,0 +1,17 @@
+dnl $Id$
+
+AC_MSG_CHECKING(whether to enable System V shared memory support)
+AC_ARG_ENABLE(sysvshm,
+[ --enable-sysvshm Enable the System V shared memory support],[
+ if test "$enableval" = "yes"; then
+ AC_DEFINE(HAVE_SYSVSHM, 1)
+ AC_MSG_RESULT(yes)
+ PHP_EXTENSION(sysvshm)
+ else
+ AC_DEFINE(HAVE_SYSVSHM, 0)
+ AC_MSG_RESULT(no)
+ fi
+],[
+ AC_DEFINE(HAVE_SYSVSHM, 0)
+ AC_MSG_RESULT(no)
+])
diff --git a/ext/sysvshm/php3_sysvshm.h b/ext/sysvshm/php3_sysvshm.h
new file mode 100644
index 0000000000..c47e561d2c
--- /dev/null
+++ b/ext/sysvshm/php3_sysvshm.h
@@ -0,0 +1,101 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Authors: Christian Cartus <chc@idgruppe.de> |
+ +----------------------------------------------------------------------+
+ */
+
+
+/* $Id$ */
+
+#ifndef _PHP3_SYSVSHM_H
+#define _PHP3_SYSVSHM_H
+
+#if COMPILE_DL
+#undef HAVE_SYSVSHM
+#define HAVE_SYSVSHM 1
+#endif
+
+
+#if HAVE_SYSVSHM
+
+extern php3_module_entry sysvshm_module_entry;
+#define sysvshm_module_ptr &sysvshm_module_entry
+
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+
+typedef struct {
+ int le_shm;
+ long init_mem;
+} sysvshm_module;
+
+
+typedef struct {
+ long key;
+ long length;
+ long next;
+ char mem;
+} sysvshm_chunk;
+
+
+typedef struct {
+ char magic[8];
+ long start;
+ long end;
+ long free;
+ long total;
+} sysvshm_chunk_head;
+
+
+typedef struct {
+ key_t key; /* Key set by user */
+ long id; /* Returned by shmget. */
+ sysvshm_chunk_head *ptr; /* memoryaddress of shared memory */
+} sysvshm_shm;
+
+
+
+extern int php3_minit_sysvshm(INIT_FUNC_ARGS);
+extern void php3_sysvshm_attach(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sysvshm_detach(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sysvshm_remove(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sysvshm_put_var(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sysvshm_get_var(INTERNAL_FUNCTION_PARAMETERS);
+extern void php3_sysvshm_remove_var(INTERNAL_FUNCTION_PARAMETERS);
+extern int php3int_put_shmdata(sysvshm_chunk_head *ptr,long key,char *data, long len);
+extern long php3int_check_shmdata(sysvshm_chunk_head *ptr, long key);
+extern int php3int_remove_shmdata(sysvshm_chunk_head *ptr, long shm_varpos);
+
+extern sysvshm_module php3_sysvshm_module;
+
+#else
+
+#define sysvshm_module_ptr NULL
+
+#endif
+
+#endif /* _PHP3_SYSVSHM_H */
diff --git a/ext/sysvshm/setup.stub b/ext/sysvshm/setup.stub
new file mode 100644
index 0000000000..4a9e894b4e
--- /dev/null
+++ b/ext/sysvshm/setup.stub
@@ -0,0 +1,5 @@
+# $Source$
+# $Id$
+
+define_option enable-sysvshm 'System V shared memory support? ' yesno no \
+' Whether to use the System V shared memory (currently only Solaris and Linux).'
diff --git a/ext/sysvshm/sysvshm.c b/ext/sysvshm/sysvshm.c
new file mode 100644
index 0000000000..68547281c5
--- /dev/null
+++ b/ext/sysvshm/sysvshm.c
@@ -0,0 +1,485 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU General Public License for more details. |
+ | |
+ | You should have received a copy of both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Authors: Christian Cartus <chc@idgruppe.de> |
+ +----------------------------------------------------------------------+
+ */
+
+/* $Id$ */
+
+/* This has been built and tested on Solaris 2.6.
+ * It may not compile or execute correctly on other systems.
+ */
+
+#ifndef MSVC5
+#include "config.h"
+#endif
+
+
+#if HAVE_SYSVSHM
+
+#include <errno.h>
+
+#include "php.h"
+#include "php3_var.h"
+#include "php3_sysvshm.h"
+
+
+function_entry sysvshm_functions[] = {
+ {"shm_attach", php3_sysvshm_attach, NULL},
+ {"shm_detach", php3_sysvshm_detach, NULL},
+ {"shm_remove", php3_sysvshm_remove, NULL},
+ {"shm_put_var", php3_sysvshm_put_var, NULL},
+ {"shm_get_var", php3_sysvshm_get_var, NULL},
+ {"shm_remove_var", php3_sysvshm_remove_var, NULL},
+ {NULL, NULL, NULL}
+};
+
+php3_module_entry sysvshm_module_entry = {
+ "System V Shared-Memory", sysvshm_functions, php3_minit_sysvshm, NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
+};
+
+
+#if COMPILE_DL
+php3_module_entry *get_module() { return &sysvshm_module_entry; }
+#endif
+
+
+THREAD_LS sysvshm_module php3_sysvshm_module;
+
+
+
+static void php3i_release_sysvshm(sysvshm_shm *shm_ptr) {
+
+ shmdt((void*)shm_ptr->ptr);
+}
+
+
+
+int php3_minit_sysvshm(INIT_FUNC_ARGS)
+{
+ php3_sysvshm_module.le_shm = register_list_destructors(php3i_release_sysvshm, NULL);
+ if (cfg_get_long("sysvshm.init_mem",
+ &php3_sysvshm_module.init_mem)==FAILURE) {
+ php3_sysvshm_module.init_mem=10000;
+ }
+ return SUCCESS;
+}
+
+
+/* {{{ proto int shm_attach(int key, int size, int flag)
+ Return an id for the shared memory with the given key. */
+void php3_sysvshm_attach(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *arg_key,*arg_size,*arg_flag;
+ long shm_size,shm_flag;
+ sysvshm_shm *shm_list_ptr;
+ char *shm_ptr;
+ sysvshm_chunk_head *chunk_ptr;
+ key_t shm_key;
+ long shm_id,list_id;
+
+ shm_flag = 0666;
+ shm_size = php3_sysvshm_module.init_mem;
+
+ switch (ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &arg_key)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_key);
+ shm_key = arg_key->value.lval;
+ break;
+ case 2:
+ if (getParameters(ht, 2, &arg_key, &arg_size)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_key);
+ shm_key = arg_key->value.lval;
+ convert_to_long(arg_size);
+ shm_size=arg_size->value.lval;
+ break;
+ case 3:
+ if (getParameters(ht, 3, &arg_key, &arg_size, &arg_flag)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_key);
+ shm_key = arg_key->value.lval;
+ convert_to_long(arg_size);
+ shm_size=arg_size->value.lval;
+ convert_to_long(arg_flag);
+ shm_flag = arg_flag->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+
+ if((shm_list_ptr = (sysvshm_shm *) emalloc(sizeof(sysvshm_shm)))==NULL) {
+ php3_error(E_WARNING, "shm_attach() failed for key 0x%x: cannot allocate internal listelement", shm_key);
+ 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)) {
+ php3_error(E_WARNING, "shm_attach() failed for key 0x%x: memorysize too small", shm_key);
+ RETURN_FALSE;
+ }
+ if((shm_id=shmget(shm_key,shm_size,shm_flag|IPC_CREAT|IPC_EXCL))<0) {
+ php3_error(E_WARNING, "shmget() failed for key 0x%x: %s", shm_key, strerror(errno));
+ RETURN_FALSE;
+ }
+ }
+
+ if((shm_ptr = shmat(shm_id,NULL,0))==NULL) {
+ php3_error(E_WARNING, "shmget() failed for key 0x%x: %s", shm_key, strerror(errno));
+ RETURN_FALSE;
+ }
+
+ /* check if shm is already initialized */
+ chunk_ptr= (sysvshm_chunk_head*) shm_ptr;
+ if(strcmp((char*)&(chunk_ptr->magic),"PHP3SM")!=0) {
+ strcpy((char*)&(chunk_ptr->magic),"PHP3SM");
+ 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;
+ }
+
+
+ shm_list_ptr->key = shm_key;
+ shm_list_ptr->id = shm_id;
+ shm_list_ptr->ptr = chunk_ptr;
+ list_id = php3_list_insert(shm_list_ptr, php3_sysvshm_module.le_shm);
+ RETURN_LONG(list_id);
+}
+/* }}} */
+
+
+
+/* {{{ proto int shm_detach(int id)
+ releases the shared memory attachment with the given id. */
+void php3_sysvshm_detach(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *arg_id;
+ long id;
+ sysvshm_shm *shm_list_ptr;
+ int type;
+
+
+ switch (ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &arg_id)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_id);
+ id = arg_id->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ shm_list_ptr = (sysvshm_shm *) php3_list_find(id, &type);
+ if (type!=php3_sysvshm_module.le_shm) {
+ php3_error(E_WARNING, "%d is not a SysV shared memory index", id);
+ RETURN_FALSE;
+ }
+
+ if(shmdt((void*)shm_list_ptr->ptr)<0) {
+ php3_error(E_WARNING, "shm_detach() failed for id 0x%x: %s", id, strerror(errno));
+ RETURN_FALSE;
+ }
+}
+/* }}} */
+
+
+
+
+
+/* {{{ proto int shm_remove(int key)
+ removes the shared memory with the given key. */
+void php3_sysvshm_remove(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *arg_key;
+ long id;
+ key_t key;
+
+
+ switch (ARG_COUNT(ht)) {
+ case 1:
+ if (getParameters(ht, 1, &arg_key)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_key);
+ key = arg_key->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+
+ if((id=shmget(key,0,0))<0) {
+ php3_error(E_WARNING, "%d is not a existing SysV shared memory key", key);
+ RETURN_FALSE;
+ }
+ if(shmctl(id,IPC_RMID,NULL)<0) {
+ php3_error(E_WARNING, "shm_remove() failed for key 0x%x: %s", key, strerror(errno));
+ RETURN_FALSE;
+ }
+ RETURN_TRUE;
+}
+/* }}} */
+
+
+
+/* {{{ proto int shm_put(int id, int key, object *variable)
+ insert a variable into shared memory. */
+void php3_sysvshm_put_var(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *arg_id, *arg_key, *arg_var;
+ long key, id;
+ sysvshm_shm *shm_list_ptr;
+ int type;
+ pval shm_var;
+ int ret;
+
+ switch (ARG_COUNT(ht)) {
+ case 3:
+ if (getParameters(ht, 3, &arg_id, &arg_key,&arg_var)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_id);
+ id = arg_id->value.lval;
+ convert_to_long(arg_key);
+ key = arg_key->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ shm_list_ptr = (sysvshm_shm *) php3_list_find(id, &type);
+ if (type!=php3_sysvshm_module.le_shm) {
+ php3_error(E_WARNING, "%d is not a SysV shared memory index", id);
+ RETURN_FALSE;
+ }
+
+ /* setup string-variable and serialize */
+ shm_var.type=IS_STRING;
+ shm_var.value.str.len=0;
+ shm_var.value.str.val=emalloc(1);
+ shm_var.value.str.val[0]=0;
+ php3api_var_serialize(&shm_var,arg_var);
+ /* insert serialized variable into shared memory */
+ ret=php3int_put_shmdata(shm_list_ptr->ptr,key,shm_var.value.str.val,shm_var.value.str.len);
+
+ /* free string */
+ efree(shm_var.value.str.val);
+
+ if(ret==-1) {
+ php3_error(E_WARNING, "not enough shared memory left");
+ RETURN_FALSE;
+ }
+ RETURN_TRUE;
+}
+/* }}} */
+
+
+
+
+/* {{{ proto string/float/int/array shm_get_var(int id, int key)
+ returns a variable into shared memory. */
+void php3_sysvshm_get_var(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *arg_id, *arg_key;
+ long key, id;
+ sysvshm_shm *shm_list_ptr;
+ int type;
+ char *shm_data;
+ long shm_varpos;
+ sysvshm_chunk *shm_var;
+
+ switch (ARG_COUNT(ht)) {
+ case 2:
+ if (getParameters(ht, 2, &arg_id, &arg_key)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_id);
+ id = arg_id->value.lval;
+ convert_to_long(arg_key);
+ key = arg_key->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ shm_list_ptr = (sysvshm_shm *) php3_list_find(id, &type);
+ if (type!=php3_sysvshm_module.le_shm) {
+ php3_error(E_WARNING, "%d is not a SysV shared memory index", id);
+ RETURN_FALSE;
+ }
+
+ /* setup string-variable and serialize */
+ /* get serialized variable from shared memory */
+ shm_varpos=php3int_check_shmdata((shm_list_ptr->ptr),key);
+
+ if(shm_varpos<0) {
+ php3_error(E_WARNING, "variable key %d doesn't exist", key);
+ RETURN_FALSE;
+ }
+ shm_var=(sysvshm_chunk*)((char*)shm_list_ptr->ptr+shm_varpos);
+ shm_data=&shm_var->mem;
+
+ if(php3api_var_unserialize(return_value, &shm_data, shm_data+shm_var->length)!=1) {
+ php3_error(E_WARNING, "variable data in shared memory is corruped");
+ RETURN_FALSE;
+ }
+}
+/* }}} */
+
+/* {{{ proto int shm_remove_var(int id, int key)
+ removes variable from shared memory. */
+void php3_sysvshm_remove_var(INTERNAL_FUNCTION_PARAMETERS)
+{
+ pval *arg_id, *arg_key;
+ long key, id;
+ sysvshm_shm *shm_list_ptr;
+ int type;
+ long shm_varpos;
+
+ switch (ARG_COUNT(ht)) {
+ case 2:
+ if (getParameters(ht, 2, &arg_id, &arg_key)==FAILURE) {
+ RETURN_FALSE;
+ }
+ convert_to_long(arg_id);
+ id = arg_id->value.lval;
+ convert_to_long(arg_key);
+ key = arg_key->value.lval;
+ break;
+ default:
+ WRONG_PARAM_COUNT;
+ break;
+ }
+
+ shm_list_ptr = (sysvshm_shm *) php3_list_find(id, &type);
+ if (type!=php3_sysvshm_module.le_shm) {
+ php3_error(E_WARNING, "%d is not a SysV shared memory index", id);
+ RETURN_FALSE;
+ }
+
+ shm_varpos=php3int_check_shmdata((shm_list_ptr->ptr),key);
+
+ if(shm_varpos<0) {
+ php3_error(E_WARNING, "variable key %d doesn't exist", key);
+ RETURN_FALSE;
+ }
+ php3int_remove_shmdata((shm_list_ptr->ptr),shm_varpos);
+ RETURN_TRUE;
+}
+/* }}} */
+
+
+
+
+
+
+
+/* inserts an ascii-string into shared memory */
+int php3int_put_shmdata(sysvshm_chunk_head *ptr,long key,char *data, long len) {
+ sysvshm_chunk* shm_var;
+ long total_size;
+ long shm_varpos;
+
+ total_size=((long)(len+sizeof(sysvshm_chunk)-1)/4)*4+4; /* 4-byte alligment */
+
+ if(ptr->free<total_size) {
+ return -1; /* not enough memeory */
+ }
+
+ if((shm_varpos=php3int_check_shmdata(ptr,key))>0) {
+ php3int_remove_shmdata(ptr, shm_varpos);
+ }
+ 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;
+}
+
+
+long php3int_check_shmdata(sysvshm_chunk_head *ptr, long key) {
+ 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;
+}
+
+
+int php3int_remove_shmdata(sysvshm_chunk_head *ptr, long shm_varpos) {
+ 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;
+}
+
+
+
+#endif /* HAVE_SYSVSHM */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ */