From: Wez Furlong Date: Thu, 25 Apr 2002 23:14:43 +0000 (+0000) Subject: Add sysv IPC support (wrappers for msgsnd and friends). X-Git-Tag: php-4.3.0dev-ZendEngine2-Preview1~422 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f03115aae1c28e405493b89dc57741d3d28ba6eb;p=php Add sysv IPC support (wrappers for msgsnd and friends). --- diff --git a/ext/sysvmsg/CREDITS b/ext/sysvmsg/CREDITS new file mode 100644 index 0000000000..6b150d5301 --- /dev/null +++ b/ext/sysvmsg/CREDITS @@ -0,0 +1,2 @@ +System V Message based IPC +Wez Furlong diff --git a/ext/sysvmsg/EXPERIMENTAL b/ext/sysvmsg/EXPERIMENTAL new file mode 100644 index 0000000000..e69de29bb2 diff --git a/ext/sysvmsg/config.m4 b/ext/sysvmsg/config.m4 new file mode 100644 index 0000000000..7f0e4d31ad --- /dev/null +++ b/ext/sysvmsg/config.m4 @@ -0,0 +1,9 @@ +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_DEFINE(HAVE_SYSVMSG, 1, [ ]) + PHP_NEW_EXTENSION(sysvmsg, sysvmsg.c, $ext_shared) +fi diff --git a/ext/sysvmsg/php_sysvmsg.h b/ext/sysvmsg/php_sysvmsg.h new file mode 100644 index 0000000000..3d78bc051a --- /dev/null +++ b/ext/sysvmsg/php_sysvmsg.h @@ -0,0 +1,69 @@ +/* + +----------------------------------------------------------------------+ + | PHP version 4.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997, 1998, 1999, 2000, 2001 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: Wez Furlong | + +----------------------------------------------------------------------+ + */ + +#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 +#include +#include + +#ifdef ZTS +#include "TSRM.h" +#endif + +PHP_MINIT_FUNCTION(sysvmsg); +PHP_MSHUTDOWN_FUNCTION(sysvmsg); +PHP_RINIT_FUNCTION(sysvmsg); +PHP_RSHUTDOWN_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); + +typedef struct { + key_t key; + long id; +} sysvmsg_queue_t; + +#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 0000000000..6db777c3aa --- /dev/null +++ b/ext/sysvmsg/sysvmsg.c @@ -0,0 +1,354 @@ +/* + +----------------------------------------------------------------------+ + | PHP version 4.0 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2002 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: Wez Furlong ptr; + efree(mq); +} + +/* {{{ PHP_MINIT_FUNCTION + */ +PHP_MINIT_FUNCTION(sysvmsg) +{ + le_sysvmsg = zend_register_list_destructors_ex(sysvmsg_release, NULL, "sysmsgq", module_number); + REGISTER_LONG_CONSTANT("MSG_IPC_NOWAIT", IPC_NOWAIT, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("MSG_NOERROR", MSG_NOERROR, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("MSG_EXCEPT", MSG_EXCEPT, CONST_PERSISTENT|CONST_CS); + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MSHUTDOWN_FUNCTION + */ +PHP_MSHUTDOWN_FUNCTION(sysvmsg) +{ + return SUCCESS; +} +/* }}} */ + +/* {{{ PHP_MINFO_FUNCTION + */ +PHP_MINFO_FUNCTION(sysvmsg) +{ + php_info_print_table_start(); + php_info_print_table_header(2, "sysvmsg support", "enabled"); + php_info_print_table_row(2, "Revision", "$Revision$"); + php_info_print_table_end(); +} +/* }}} */ + +/* {{{ proto array 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, "sysmsg 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, "sysmsg 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 resource msg_get_queue(long key [, long 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) { + zend_error(E_WARNING, "%s: msgget() failed for key 0x%x: %s", + get_active_function_name(TSRMLS_C), key, strerror(errno)); + efree(mq); + RETURN_FALSE; + } + } + RETVAL_RESOURCE(zend_list_insert(mq, le_sysvmsg)); +} +/* }}} */ + +/* {{{ 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, "sysmsg queue", le_sysvmsg); + + if (msgctl(mq->id, IPC_RMID, NULL) == 0) { + RETVAL_TRUE; + } else { + RETVAL_FALSE; + } +} +/* }}} */ + +/* {{{ proto mixed msg_receive(resource queue, long desiredmsgtype, long &msgtype, long maxsize [[, bool unserialize=true][, long flags=0]] + Send a message of type msgtype (must be > 0) to a message queue */ +PHP_FUNCTION(msg_receive) +{ + zval *out_message, *queue, *out_msgtype; + long desiredmsgtype, maxsize, flags = 0; + zend_bool do_unserialize = 1; + sysvmsg_queue_t *mq = NULL; + struct msgbuf *messagebuffer = NULL; /* buffer to transmit */ + int result; + + RETVAL_FALSE; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlzl|bl", + &queue, &desiredmsgtype, &out_msgtype, &out_message, + &maxsize, &do_unserialize, &flags) == FAILURE) + return; + + ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysmsg queue", le_sysvmsg); + + messagebuffer = (struct msgbuf*)emalloc(sizeof(struct msgbuf) + maxsize); + + result = msgrcv(mq->id, messagebuffer, maxsize, desiredmsgtype, flags); + + if (result >= 0) { + /* got it! */ + ZVAL_LONG(out_msgtype, messagebuffer->mtype); + + if (do_unserialize) { + php_unserialize_data_t var_hash; + const char *p = (const char*)messagebuffer->mtext; + + PHP_VAR_UNSERIALIZE_INIT(var_hash); + if (!php_var_unserialize(&return_value, &p, p + result, &var_hash TSRMLS_CC)) { + zend_error(E_WARNING, "%s(): message corrupted", get_active_function_name(TSRMLS_C)); + RETVAL_FALSE; + } + PHP_VAR_UNSERIALIZE_DESTROY(var_hash); + } else { + RETVAL_STRINGL(messagebuffer->mtext, result, 1); + } + } + efree(messagebuffer); +} +/* }}} */ + +/* {{{ proto bool msg_send(resource queue, long msgtype, mixed message [[, bool serialize=true][, bool blocking=true]] + Send a message of type msgtype (must be > 0) to a message queue */ +PHP_FUNCTION(msg_send) +{ + zval *message, *queue; + long msgtype; + zend_bool do_serialize = 1, blocking = 1; + sysvmsg_queue_t * mq = NULL; + struct msgbuf * messagebuffer = NULL; /* buffer to transmit */ + int result; + int message_len = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz|bb", + &queue, &msgtype, &message, &do_serialize, &blocking) == FAILURE) + return; + + ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t*, &queue, -1, "sysmsg 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: msgbuf is 1 char bigger than a long, so there is no need to + * allocate the extra byte. */ + messagebuffer = emalloc(sizeof(struct msgbuf) + msg_var.len); + memcpy(messagebuffer->mtext, msg_var.c, msg_var.len + 1); + message_len = msg_var.len; + smart_str_free(&msg_var); + } else { + convert_to_string_ex(&message); + messagebuffer = emalloc(sizeof(struct msgbuf) + Z_STRLEN_P(message)); + memcpy(messagebuffer->mtext, Z_STRVAL_P(message), Z_STRLEN_P(message) + 1); + message_len = Z_STRLEN_P(message); + } + + /* set the message type */ + messagebuffer->mtype = msgtype; + + result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT); + + efree(messagebuffer); + + if (result == -1) { + zend_error(E_WARNING, "%s(): msgsnd failed: %s", + get_active_function_name(TSRMLS_C), strerror(errno)); + RETURN_LONG(errno); + } + + RETURN_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 0000000000..aa62e6f64b --- /dev/null +++ b/ext/sysvmsg/tests/001.phpt @@ -0,0 +1,24 @@ +--TEST-- +Check for sysvmsg presence +--SKIPIF-- + +--POST-- +--GET-- +--FILE-- + +--EXPECT-- +TYPE: 1 +DATA: hello