diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-03-14 05:42:27 +0000 |
---|---|---|
committer | <> | 2013-04-03 16:25:08 +0000 |
commit | c4dd7a1a684490673e25aaf4fabec5df138854c4 (patch) | |
tree | 4d57c44caae4480efff02b90b9be86f44bf25409 /ext/sysvmsg | |
download | php2-master.tar.gz |
Imported from /home/lorry/working-area/delta_php2/php-5.4.13.tar.bz2.HEADphp-5.4.13master
Diffstat (limited to 'ext/sysvmsg')
-rw-r--r-- | ext/sysvmsg/CREDITS | 2 | ||||
-rw-r--r-- | ext/sysvmsg/config.m4 | 14 | ||||
-rw-r--r-- | ext/sysvmsg/package.xml | 40 | ||||
-rw-r--r-- | ext/sysvmsg/php_sysvmsg.h | 74 | ||||
-rw-r--r-- | ext/sysvmsg/sysvmsg.c | 477 | ||||
-rw-r--r-- | ext/sysvmsg/tests/001.phpt | 22 | ||||
-rw-r--r-- | ext/sysvmsg/tests/002.phpt | 23 | ||||
-rw-r--r-- | ext/sysvmsg/tests/003.phpt | 25 | ||||
-rw-r--r-- | ext/sysvmsg/tests/004.phpt | 59 | ||||
-rw-r--r-- | ext/sysvmsg/tests/005.phpt | 71 | ||||
-rw-r--r-- | ext/sysvmsg/tests/006.phpt | 69 |
11 files changed, 876 insertions, 0 deletions
diff --git a/ext/sysvmsg/CREDITS b/ext/sysvmsg/CREDITS new file mode 100644 index 0000000..6b150d5 --- /dev/null +++ b/ext/sysvmsg/CREDITS @@ -0,0 +1,2 @@ +System V Message based IPC +Wez Furlong diff --git a/ext/sysvmsg/config.m4 b/ext/sysvmsg/config.m4 new file mode 100644 index 0000000..4ae8a2e --- /dev/null +++ b/ext/sysvmsg/config.m4 @@ -0,0 +1,14 @@ +dnl $Id$ + +PHP_ARG_ENABLE(sysvmsg,whether to enable System V IPC support, +[ --enable-sysvmsg Enable sysvmsg support]) + +if test "$PHP_SYSVMSG" != "no"; then + AC_CHECK_HEADER([sys/msg.h], + [], + [AC_MSG_ERROR([Cannot enable System V IPC support, sys/msg.h is missing]) + ]) + + AC_DEFINE(HAVE_SYSVMSG, 1, [ ]) + PHP_NEW_EXTENSION(sysvmsg, sysvmsg.c, $ext_shared) +fi diff --git a/ext/sysvmsg/package.xml b/ext/sysvmsg/package.xml new file mode 100644 index 0000000..403e949 --- /dev/null +++ b/ext/sysvmsg/package.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="ISO-8859-1" ?> +<!DOCTYPE package SYSTEM "../pear/package.dtd"> +<package> + <name>sysvmsg</name> + <summary>Unix System V IPC Message Queues</summary> + <maintainers> + <maintainer> + <user>wez</user> + <name>Wez Furlong</name> + <email>wez@php.net</email> + <role>lead</role> + </maintainer> + </maintainers> + <description> +Unix System V IPC Message Queues + </description> + <license>PHP</license> + <release> + <state>stable</state> + <version>5.0.0rc1</version> + <date>2004-03-19</date> + <notes> +package.xml added to support installation using pear installer + </notes> + <filelist> + <file role="doc" name="CREDITS"/> + <file role="src" name="config.m4"/> + <file role="src" name="sysvmsg.c"/> + <file role="src" name="php_sysvmsg.h"/> + <file role="test" name="tests/001.phpt"/> + </filelist> + <deps> + <dep type="php" rel="ge" version="5" /> + <!-- doesn't work yet <dep type="os" rel="has" name="unix"/> --> + </deps> + </release> +</package> +<!-- +vim:et:ts=1:sw=1 +--> diff --git a/ext/sysvmsg/php_sysvmsg.h b/ext/sysvmsg/php_sysvmsg.h new file mode 100644 index 0000000..86c8c1e --- /dev/null +++ b/ext/sysvmsg/php_sysvmsg.h @@ -0,0 +1,74 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.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. | + +----------------------------------------------------------------------+ + | Author: Wez Furlong <wez@thebrainroom.com> | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_SYSVMSG_H +#define PHP_SYSVMSG_H + +#if HAVE_SYSVMSG + +extern zend_module_entry sysvmsg_module_entry; +#define phpext_sysvmsg_ptr &sysvmsg_module_entry + +#ifndef __USE_GNU +/* we want to use mtype instead of __mtype */ +#define __USE_GNU +#endif + +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/msg.h> + +#ifdef ZTS +#include "TSRM.h" +#endif + +PHP_MINIT_FUNCTION(sysvmsg); +PHP_MINFO_FUNCTION(sysvmsg); + +PHP_FUNCTION(msg_get_queue); +PHP_FUNCTION(msg_remove_queue); +PHP_FUNCTION(msg_stat_queue); +PHP_FUNCTION(msg_set_queue); +PHP_FUNCTION(msg_send); +PHP_FUNCTION(msg_receive); +PHP_FUNCTION(msg_queue_exists); + +typedef struct { + key_t key; + long id; +} sysvmsg_queue_t; + +struct php_msgbuf { + long mtype; + char mtext[1]; +}; + +#endif /* HAVE_SYSVMSG */ + +#endif /* PHP_SYSVMSG_H */ + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: t + * End: + */ diff --git a/ext/sysvmsg/sysvmsg.c b/ext/sysvmsg/sysvmsg.c new file mode 100644 index 0000000..ebc4427 --- /dev/null +++ b/ext/sysvmsg/sysvmsg.c @@ -0,0 +1,477 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.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. | + +----------------------------------------------------------------------+ + | Author: Wez Furlong <wez@thebrainroom.com> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: e268047b695fe9a43cdf415c429729252347becd $ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "php_globals.h" +#include "ext/standard/info.h" +#include "php_sysvmsg.h" +#include "ext/standard/php_var.h" +#include "ext/standard/php_smart_str.h" + +/* In order to detect MSG_EXCEPT use at run time; we have no way + * of knowing what the bit definitions are, so we can't just define + * out own MSG_EXCEPT value. */ +#define PHP_MSG_IPC_NOWAIT 1 +#define PHP_MSG_NOERROR 2 +#define PHP_MSG_EXCEPT 4 + +/* True global resources - no need for thread safety here */ +static int le_sysvmsg; + +/* {{{ arginfo */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_get_queue, 0, 0, 1) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, perms) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_send, 0, 0, 3) + ZEND_ARG_INFO(0, queue) + ZEND_ARG_INFO(0, msgtype) + ZEND_ARG_INFO(0, message) + ZEND_ARG_INFO(0, serialize) + ZEND_ARG_INFO(0, blocking) + ZEND_ARG_INFO(1, errorcode) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_receive, 0, 0, 5) + ZEND_ARG_INFO(0, queue) + ZEND_ARG_INFO(0, desiredmsgtype) + ZEND_ARG_INFO(1, msgtype) + ZEND_ARG_INFO(0, maxsize) + ZEND_ARG_INFO(1, message) + ZEND_ARG_INFO(0, unserialize) + ZEND_ARG_INFO(0, flags) + ZEND_ARG_INFO(1, errorcode) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_remove_queue, 0, 0, 1) + ZEND_ARG_INFO(0, queue) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_stat_queue, 0, 0, 1) + ZEND_ARG_INFO(0, queue) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_set_queue, 0, 0, 2) + ZEND_ARG_INFO(0, queue) + ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_queue_exists, 0, 0, 1) + ZEND_ARG_INFO(0, key) +ZEND_END_ARG_INFO() +/* }}} */ + +/* {{{ sysvmsg_functions[] + * + * Every user visible function must have an entry in sysvmsg_functions[]. + */ +const zend_function_entry sysvmsg_functions[] = { + PHP_FE(msg_get_queue, arginfo_msg_get_queue) + PHP_FE(msg_send, arginfo_msg_send) + PHP_FE(msg_receive, arginfo_msg_receive) + PHP_FE(msg_remove_queue, arginfo_msg_remove_queue) + PHP_FE(msg_stat_queue, arginfo_msg_stat_queue) + PHP_FE(msg_set_queue, arginfo_msg_set_queue) + PHP_FE(msg_queue_exists, arginfo_msg_queue_exists) + PHP_FE_END +}; +/* }}} */ + +/* {{{ sysvmsg_module_entry + */ +zend_module_entry sysvmsg_module_entry = { + STANDARD_MODULE_HEADER, + "sysvmsg", + sysvmsg_functions, + PHP_MINIT(sysvmsg), + NULL, + NULL, + NULL, + PHP_MINFO(sysvmsg), + NO_VERSION_YET, + STANDARD_MODULE_PROPERTIES +}; +/* }}} */ + +#ifdef COMPILE_DL_SYSVMSG +ZEND_GET_MODULE(sysvmsg) +#endif + +static void sysvmsg_release(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + sysvmsg_queue_t * mq = (sysvmsg_queue_t *) rsrc->ptr; + efree(mq); +} + +/* {{{ PHP_MINIT_FUNCTION + */ +PHP_MINIT_FUNCTION(sysvmsg) +{ + le_sysvmsg = zend_register_list_destructors_ex(sysvmsg_release, NULL, "sysvmsg queue", module_number); + REGISTER_LONG_CONSTANT("MSG_IPC_NOWAIT", PHP_MSG_IPC_NOWAIT, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("MSG_EAGAIN", EAGAIN, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("MSG_ENOMSG", ENOMSG, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("MSG_NOERROR", PHP_MSG_NOERROR, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("MSG_EXCEPT", PHP_MSG_EXCEPT, CONST_PERSISTENT|CONST_CS); + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MINFO_FUNCTION + */ +PHP_MINFO_FUNCTION(sysvmsg) +{ + php_info_print_table_start(); + php_info_print_table_row(2, "sysvmsg support", "enabled"); + php_info_print_table_row(2, "Revision", "$Id: e268047b695fe9a43cdf415c429729252347becd $"); + php_info_print_table_end(); +} +/* }}} */ + +/* {{{ proto bool msg_set_queue(resource queue, array data) + Set information for a message queue */ +PHP_FUNCTION(msg_set_queue) +{ + zval *queue, *data; + sysvmsg_queue_t *mq = NULL; + struct msqid_ds stat; + + RETVAL_FALSE; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &queue, &data) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg); + + if (msgctl(mq->id, IPC_STAT, &stat) == 0) { + zval **item; + + /* now pull out members of data and set them in the stat buffer */ + if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.uid", sizeof("msg_perm.uid"), (void **) &item) == SUCCESS) { + convert_to_long_ex(item); + stat.msg_perm.uid = Z_LVAL_PP(item); + } + if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.gid", sizeof("msg_perm.gid"), (void **) &item) == SUCCESS) { + convert_to_long_ex(item); + stat.msg_perm.gid = Z_LVAL_PP(item); + } + if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.mode", sizeof("msg_perm.mode"), (void **) &item) == SUCCESS) { + convert_to_long_ex(item); + stat.msg_perm.mode = Z_LVAL_PP(item); + } + if (zend_hash_find(Z_ARRVAL_P(data), "msg_qbytes", sizeof("msg_qbytes"), (void **) &item) == SUCCESS) { + convert_to_long_ex(item); + stat.msg_qbytes = Z_LVAL_PP(item); + } + if (msgctl(mq->id, IPC_SET, &stat) == 0) { + RETVAL_TRUE; + } + } +} +/* }}} */ + +/* {{{ proto array msg_stat_queue(resource queue) + Returns information about a message queue */ +PHP_FUNCTION(msg_stat_queue) +{ + zval *queue; + sysvmsg_queue_t *mq = NULL; + struct msqid_ds stat; + + RETVAL_FALSE; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg); + + if (msgctl(mq->id, IPC_STAT, &stat) == 0) { + array_init(return_value); + + add_assoc_long(return_value, "msg_perm.uid", stat.msg_perm.uid); + add_assoc_long(return_value, "msg_perm.gid", stat.msg_perm.gid); + add_assoc_long(return_value, "msg_perm.mode", stat.msg_perm.mode); + add_assoc_long(return_value, "msg_stime", stat.msg_stime); + add_assoc_long(return_value, "msg_rtime", stat.msg_rtime); + add_assoc_long(return_value, "msg_ctime", stat.msg_ctime); + add_assoc_long(return_value, "msg_qnum", stat.msg_qnum); + add_assoc_long(return_value, "msg_qbytes", stat.msg_qbytes); + add_assoc_long(return_value, "msg_lspid", stat.msg_lspid); + add_assoc_long(return_value, "msg_lrpid", stat.msg_lrpid); + } +} +/* }}} */ + + +/* {{{ proto bool msg_queue_exists(int key) + Check wether a message queue exists */ +PHP_FUNCTION(msg_queue_exists) +{ + long key; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &key) == FAILURE) { + return; + } + + if (msgget(key, 0) < 0) { + RETURN_FALSE; + } + + RETURN_TRUE; +} +/* }}} */ + + +/* {{{ proto resource msg_get_queue(int key [, int perms]) + Attach to a message queue */ +PHP_FUNCTION(msg_get_queue) +{ + long key; + long perms = 0666; + sysvmsg_queue_t *mq; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &key, &perms) == FAILURE) { + return; + } + + mq = (sysvmsg_queue_t *) emalloc(sizeof(sysvmsg_queue_t)); + + mq->key = key; + mq->id = msgget(key, 0); + if (mq->id < 0) { + /* doesn't already exist; create it */ + mq->id = msgget(key, IPC_CREAT | IPC_EXCL | perms); + if (mq->id < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno)); + efree(mq); + RETURN_FALSE; + } + } + RETVAL_RESOURCE(zend_list_insert(mq, le_sysvmsg TSRMLS_CC)); +} +/* }}} */ + +/* {{{ proto bool msg_remove_queue(resource queue) + Destroy the queue */ +PHP_FUNCTION(msg_remove_queue) +{ + zval *queue; + sysvmsg_queue_t *mq = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg); + + if (msgctl(mq->id, IPC_RMID, NULL) == 0) { + RETVAL_TRUE; + } else { + RETVAL_FALSE; + } +} +/* }}} */ + +/* {{{ proto mixed msg_receive(resource queue, int desiredmsgtype, int &msgtype, int maxsize, mixed message [, bool unserialize=true [, int flags=0 [, int errorcode]]]) + Send a message of type msgtype (must be > 0) to a message queue */ +PHP_FUNCTION(msg_receive) +{ + zval *out_message, *queue, *out_msgtype, *zerrcode = NULL; + long desiredmsgtype, maxsize, flags = 0; + long realflags = 0; + zend_bool do_unserialize = 1; + sysvmsg_queue_t *mq = NULL; + struct php_msgbuf *messagebuffer = NULL; /* buffer to transmit */ + int result; + + RETVAL_FALSE; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlzlz|blz", + &queue, &desiredmsgtype, &out_msgtype, &maxsize, + &out_message, &do_unserialize, &flags, &zerrcode) == FAILURE) { + return; + } + + if (maxsize <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "maximum size of the message has to be greater than zero"); + return; + } + + if (flags != 0) { + if (flags & PHP_MSG_EXCEPT) { +#ifndef MSG_EXCEPT + php_error_docref(NULL TSRMLS_CC, E_WARNING, "MSG_EXCEPT is not supported on your system"); + RETURN_FALSE; +#else + realflags |= MSG_EXCEPT; +#endif + } + if (flags & PHP_MSG_NOERROR) { + realflags |= MSG_NOERROR; + } + if (flags & PHP_MSG_IPC_NOWAIT) { + realflags |= IPC_NOWAIT; + } + } + + ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg); + + messagebuffer = (struct php_msgbuf *) safe_emalloc(maxsize, 1, sizeof(struct php_msgbuf)); + + result = msgrcv(mq->id, messagebuffer, maxsize, desiredmsgtype, realflags); + + zval_dtor(out_msgtype); + zval_dtor(out_message); + ZVAL_LONG(out_msgtype, 0); + ZVAL_FALSE(out_message); + + if (zerrcode) { + zval_dtor(zerrcode); + ZVAL_LONG(zerrcode, 0); + } + + if (result >= 0) { + /* got it! */ + ZVAL_LONG(out_msgtype, messagebuffer->mtype); + + RETVAL_TRUE; + if (do_unserialize) { + php_unserialize_data_t var_hash; + zval *tmp = NULL; + const unsigned char *p = (const unsigned char *) messagebuffer->mtext; + + MAKE_STD_ZVAL(tmp); + PHP_VAR_UNSERIALIZE_INIT(var_hash); + if (!php_var_unserialize(&tmp, &p, p + result, &var_hash TSRMLS_CC)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "message corrupted"); + RETVAL_FALSE; + } else { + REPLACE_ZVAL_VALUE(&out_message, tmp, 0); + } + FREE_ZVAL(tmp); + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + } else { + ZVAL_STRINGL(out_message, messagebuffer->mtext, result, 1); + } + } else if (zerrcode) { + ZVAL_LONG(zerrcode, errno); + } + efree(messagebuffer); +} +/* }}} */ + +/* {{{ proto bool msg_send(resource queue, int msgtype, mixed message [, bool serialize=true [, bool blocking=true [, int errorcode]]]) + Send a message of type msgtype (must be > 0) to a message queue */ +PHP_FUNCTION(msg_send) +{ + zval *message, *queue, *zerror=NULL; + long msgtype; + zend_bool do_serialize = 1, blocking = 1; + sysvmsg_queue_t * mq = NULL; + struct php_msgbuf * messagebuffer = NULL; /* buffer to transmit */ + int result; + int message_len = 0; + + RETVAL_FALSE; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz|bbz", + &queue, &msgtype, &message, &do_serialize, &blocking, &zerror) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t*, &queue, -1, "sysvmsg queue", le_sysvmsg); + + if (do_serialize) { + smart_str msg_var = {0}; + php_serialize_data_t var_hash; + + PHP_VAR_SERIALIZE_INIT(var_hash); + php_var_serialize(&msg_var, &message, &var_hash TSRMLS_CC); + PHP_VAR_SERIALIZE_DESTROY(var_hash); + + /* NB: php_msgbuf is 1 char bigger than a long, so there is no need to + * allocate the extra byte. */ + messagebuffer = safe_emalloc(msg_var.len, 1, sizeof(struct php_msgbuf)); + memcpy(messagebuffer->mtext, msg_var.c, msg_var.len + 1); + message_len = msg_var.len; + smart_str_free(&msg_var); + } else { + char *p; + switch (Z_TYPE_P(message)) { + case IS_STRING: + p = Z_STRVAL_P(message); + message_len = Z_STRLEN_P(message); + break; + + case IS_LONG: + case IS_BOOL: + message_len = spprintf(&p, 0, "%ld", Z_LVAL_P(message)); + break; + + case IS_DOUBLE: + message_len = spprintf(&p, 0, "%F", Z_DVAL_P(message)); + break; + + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Message parameter must be either a string or a number."); + RETURN_FALSE; + } + + messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf)); + memcpy(messagebuffer->mtext, p, message_len + 1); + + if (Z_TYPE_P(message) != IS_STRING) { + efree(p); + } + } + + /* set the message type */ + messagebuffer->mtype = msgtype; + + result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT); + + efree(messagebuffer); + + if (result == -1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "msgsnd failed: %s", strerror(errno)); + if (zerror) { + ZVAL_LONG(zerror, errno); + } + } else { + RETVAL_TRUE; + } +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 tw=78 fdm=marker + * vim<600: noet sw=4 ts=4 tw=78 + */ diff --git a/ext/sysvmsg/tests/001.phpt b/ext/sysvmsg/tests/001.phpt new file mode 100644 index 0000000..d3a5b1e --- /dev/null +++ b/ext/sysvmsg/tests/001.phpt @@ -0,0 +1,22 @@ +--TEST-- +send/receive serialized message. +--SKIPIF-- +<?php // vim600:syn=php +if (!extension_loaded("sysvmsg")) print "skip"; ?> +--FILE-- +<?php +$key = ftok(dirname(__FILE__) . "/001.phpt", "p"); +$q = msg_get_queue($key); +msg_send($q, 1, "hello") or print "FAIL\n"; +$type = null; +if (msg_receive($q, 0, $type, 1024, $message)) { + echo "TYPE: $type\n"; + echo "DATA: $message\n"; +} +if (!msg_remove_queue($q)) { + echo "BAD: queue removal failed\n"; +} +?> +--EXPECT-- +TYPE: 1 +DATA: hello diff --git a/ext/sysvmsg/tests/002.phpt b/ext/sysvmsg/tests/002.phpt new file mode 100644 index 0000000..e603ed0 --- /dev/null +++ b/ext/sysvmsg/tests/002.phpt @@ -0,0 +1,23 @@ +--TEST-- +msg_receive() should return false when unserialize() failed +--SKIPIF-- +<?php if (!extension_loaded("sysvmsg")) die("skip sysvmsg extenions is not available")?> +--FILE-- +<?php + +$queue = msg_get_queue (ftok(__FILE__, 'r'), 0600); +if (!msg_send ($queue, 1, 'Hi', false /* ! no_ser*/, true/*block*/, $msg_err)) { + die("error\n"); +} +var_dump($res = msg_receive ($queue, 1, $msg_type, 16384, $msg, true, 0, $msg_error)); + +if (!msg_remove_queue($queue)) { + echo "BAD: queue removal failed\n"; +} + +echo "Done\n"; +?> +--EXPECTF-- +Warning: msg_receive(): message corrupted in %s002.php on line %d +bool(false) +Done diff --git a/ext/sysvmsg/tests/003.phpt b/ext/sysvmsg/tests/003.phpt new file mode 100644 index 0000000..66ff046 --- /dev/null +++ b/ext/sysvmsg/tests/003.phpt @@ -0,0 +1,25 @@ +--TEST-- +msg_queue_exists() +--SKIPIF-- +<?php if (!extension_loaded("sysvmsg")) die("skip sysvmsg extension is not available")?> +--FILE-- +<?php +$id = ftok(__FILE__, 'r'); + +msg_remove_queue(msg_get_queue($id, 0600)); + +var_dump(msg_queue_exists($id)); +$res = msg_get_queue($id, 0600); +var_dump($res); +var_dump(msg_queue_exists($id)); +var_dump(msg_remove_queue($res)); +var_dump(msg_queue_exists($id)); +echo "Done\n"; +?> +--EXPECTF-- +bool(false) +resource(%d) of type (sysvmsg queue) +bool(true) +bool(true) +bool(false) +Done diff --git a/ext/sysvmsg/tests/004.phpt b/ext/sysvmsg/tests/004.phpt new file mode 100644 index 0000000..39467d7 --- /dev/null +++ b/ext/sysvmsg/tests/004.phpt @@ -0,0 +1,59 @@ +--TEST-- +msg_set_queue() and msg_stat_queue() +--SKIPIF-- +<?php if (!extension_loaded("sysvmsg")) die("skip sysvmsg extension is not available")?> +--FILE-- +<?php +$id = ftok(__FILE__, 'r'); + +$q = msg_get_queue($id); + +echo "Set mode:\n"; +$arr = array('msg_perm.mode' => 0600); +var_dump(msg_set_queue($q, $arr)); +echo "Did really work:\n"; +var_dump(count(array_diff_assoc($arr, msg_stat_queue($q))) == 0); + +echo "Set uid:\n"; // same as the running user to make it succeed +$arr = array('msg_perm.uid' => getmyuid()); +var_dump(msg_set_queue($q, $arr)); +echo "Did really work:\n"; +var_dump(count(array_diff_assoc($arr, msg_stat_queue($q))) == 0); + +echo "Set gid:\n"; // same as the running user to make it succeed +$arr = array('msg_perm.gid' => getmygid()); +var_dump(msg_set_queue($q, $arr)); +echo "Did really work:\n"; +var_dump(count(array_diff_assoc($arr, msg_stat_queue($q))) == 0); + +echo "Set smaller qbytes:\n"; +$res = msg_stat_queue($q); +$arr = array('msg_qbytes' => ($res['msg_qbytes'] -1)); +var_dump(msg_set_queue($q, $arr)); +echo "Did really work:\n"; +var_dump(count(array_diff_assoc($arr, msg_stat_queue($q))) == 0); + +if (!msg_remove_queue($q)) { + echo "BAD: queue removal failed\n"; +} + +echo "Done\n"; +?> +--EXPECTF-- +Set mode: +bool(true) +Did really work: +bool(true) +Set uid: +bool(true) +Did really work: +bool(true) +Set gid: +bool(true) +Did really work: +bool(true) +Set smaller qbytes: +bool(true) +Did really work: +bool(true) +Done diff --git a/ext/sysvmsg/tests/005.phpt b/ext/sysvmsg/tests/005.phpt new file mode 100644 index 0000000..d9437c6 --- /dev/null +++ b/ext/sysvmsg/tests/005.phpt @@ -0,0 +1,71 @@ +--TEST-- +sysvmsg functions on non-existing queue +--SKIPIF-- +<?php if (!extension_loaded("sysvmsg")) die("skip sysvmsg extension is not available")?> +--FILE-- +<?php + +$tests = array(null, 'foo'); + +foreach ($tests as $q) { + + if ($q === null) { + do { + $id = ftok(__FILE__, chr(mt_rand(0, 255))); + } while (msg_queue_exists($id)); + + $q = msg_get_queue($id) or die("Failed to create queue"); + msg_remove_queue($q) or die("Failed to close queue"); + } + + echo "Using '$q' as queue resource:\n"; + + $errno = 0; + + var_dump(msg_set_queue($q, array('msg_qbytes' => 1))); + + var_dump(msg_stat_queue($q)); + + var_dump(msg_receive($q, 0, $null, 1, $msg, true, 0, $errno)); + var_dump($errno != 0); + // again, but triggering an E_WARNING + var_dump(msg_receive($q, 0, $null, 0, $msg)); + + var_dump(msg_send($q, 1, 'foo', true, true, $errno)); + var_dump($errno != 0); +} + +echo "Done\n"; +?> +--EXPECTF-- +Using 'Resource id #4' as queue resource: +bool(false) +bool(false) +bool(false) +bool(true) + +Warning: msg_receive(): maximum size of the message has to be greater than zero in %s on line %d +bool(false) + +Warning: msg_send(): msgsnd failed: Invalid argument in %s on line %d +bool(false) +bool(true) +Using 'foo' as queue resource: + +Warning: msg_set_queue() expects parameter 1 to be resource, string given in %s on line %d +bool(false) + +Warning: msg_stat_queue() expects parameter 1 to be resource, string given in %s on line %d +bool(false) + +Warning: msg_receive() expects parameter 1 to be resource, string given in %s on line %d +bool(false) +bool(false) + +Warning: msg_receive() expects parameter 1 to be resource, string given in %s on line %d +bool(false) + +Warning: msg_send() expects parameter 1 to be resource, string given in %s on line %d +bool(false) +bool(false) +Done diff --git a/ext/sysvmsg/tests/006.phpt b/ext/sysvmsg/tests/006.phpt new file mode 100644 index 0000000..cb4fd24 --- /dev/null +++ b/ext/sysvmsg/tests/006.phpt @@ -0,0 +1,69 @@ +--TEST-- +msg_send() data types when not serializing +--SKIPIF-- +<?php if (!extension_loaded("sysvmsg")) die("skip sysvmsg extenions is not available")?> +--FILE-- +<?php + +$queue = msg_get_queue (ftok(__FILE__, 'r'), 0600); + +$tests = array('foo', 123, PHP_INT_MAX +1, true, 1.01, null, array('bar')); + +foreach ($tests as $elem) { + echo @"Sending/receiving '$elem':\n"; + var_dump(msg_send($queue, 1, $elem, false)); + + unset($msg); + var_dump(msg_receive($queue, 1, $msg_type, 1024, $msg, false, MSG_IPC_NOWAIT)); + + var_dump($elem == $msg); + var_dump($elem === $msg); +} + +if (!msg_remove_queue($queue)) { + echo "BAD: queue removal failed\n"; +} + +echo "Done\n"; +?> +--EXPECTF-- +Sending/receiving 'foo': +bool(true) +bool(true) +bool(true) +bool(true) +Sending/receiving '123': +bool(true) +bool(true) +bool(true) +bool(false) +Sending/receiving '%s': +bool(true) +bool(true) +bool(true) +bool(false) +Sending/receiving '1': +bool(true) +bool(true) +bool(true) +bool(false) +Sending/receiving '1.01': +bool(true) +bool(true) +bool(true) +bool(false) +Sending/receiving '': + +Warning: msg_send(): Message parameter must be either a string or a number. in %s on line %d +bool(false) +bool(false) +bool(true) +bool(false) +Sending/receiving 'Array': + +Warning: msg_send(): Message parameter must be either a string or a number. in %s on line %d +bool(false) +bool(false) +bool(false) +bool(false) +Done |