Back to index

php5  5.3.10
sysvmsg.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author: Wez Furlong <wez@thebrainroom.com>                           |
00016   +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: sysvmsg.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 #include "php_globals.h"
00027 #include "ext/standard/info.h"
00028 #include "php_sysvmsg.h"
00029 #include "ext/standard/php_var.h"
00030 #include "ext/standard/php_smart_str.h"
00031 
00032 /* In order to detect MSG_EXCEPT use at run time; we have no way
00033  * of knowing what the bit definitions are, so we can't just define
00034  * out own MSG_EXCEPT value. */
00035 #define PHP_MSG_IPC_NOWAIT  1
00036 #define PHP_MSG_NOERROR            2
00037 #define PHP_MSG_EXCEPT             4
00038 
00039 /* True global resources - no need for thread safety here */
00040 static int le_sysvmsg;
00041 
00042 /* {{{ arginfo */
00043 ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_get_queue, 0, 0, 1)
00044        ZEND_ARG_INFO(0, key)
00045        ZEND_ARG_INFO(0, perms)
00046 ZEND_END_ARG_INFO()
00047 
00048 ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_send, 0, 0, 3)
00049        ZEND_ARG_INFO(0, queue)
00050        ZEND_ARG_INFO(0, msgtype)
00051        ZEND_ARG_INFO(0, message)
00052        ZEND_ARG_INFO(0, serialize)
00053        ZEND_ARG_INFO(0, blocking)
00054        ZEND_ARG_INFO(1, errorcode)
00055 ZEND_END_ARG_INFO()
00056 
00057 ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_receive, 0, 0, 5)
00058        ZEND_ARG_INFO(0, queue)
00059        ZEND_ARG_INFO(0, desiredmsgtype)
00060        ZEND_ARG_INFO(1, msgtype)
00061        ZEND_ARG_INFO(0, maxsize)
00062        ZEND_ARG_INFO(1, message)
00063        ZEND_ARG_INFO(0, unserialize)
00064        ZEND_ARG_INFO(0, flags)
00065        ZEND_ARG_INFO(1, errorcode)
00066 ZEND_END_ARG_INFO()
00067 
00068 ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_remove_queue, 0, 0, 1)
00069        ZEND_ARG_INFO(0, queue)
00070 ZEND_END_ARG_INFO()
00071 
00072 ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_stat_queue, 0, 0, 1)
00073        ZEND_ARG_INFO(0, queue)
00074 ZEND_END_ARG_INFO()
00075 
00076 ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_set_queue, 0, 0, 2)
00077        ZEND_ARG_INFO(0, queue)
00078        ZEND_ARG_INFO(0, data)
00079 ZEND_END_ARG_INFO()
00080 
00081 ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_queue_exists, 0, 0, 1)
00082        ZEND_ARG_INFO(0, key)
00083 ZEND_END_ARG_INFO()
00084 /* }}} */
00085 
00086 /* {{{ sysvmsg_functions[]
00087  *
00088  * Every user visible function must have an entry in sysvmsg_functions[].
00089  */
00090 const zend_function_entry sysvmsg_functions[] = {
00091        PHP_FE(msg_get_queue,                            arginfo_msg_get_queue)
00092        PHP_FE(msg_send,                                 arginfo_msg_send)
00093        PHP_FE(msg_receive,                              arginfo_msg_receive)
00094        PHP_FE(msg_remove_queue,                  arginfo_msg_remove_queue)
00095        PHP_FE(msg_stat_queue,                           arginfo_msg_stat_queue)
00096        PHP_FE(msg_set_queue,                            arginfo_msg_set_queue)
00097        PHP_FE(msg_queue_exists,                  arginfo_msg_queue_exists)
00098        PHP_FE_END
00099 };
00100 /* }}} */
00101 
00102 /* {{{ sysvmsg_module_entry
00103  */
00104 zend_module_entry sysvmsg_module_entry = {
00105        STANDARD_MODULE_HEADER,
00106        "sysvmsg",
00107        sysvmsg_functions,
00108        PHP_MINIT(sysvmsg),
00109        NULL,
00110        NULL,
00111        NULL,
00112        PHP_MINFO(sysvmsg),
00113        NO_VERSION_YET,
00114        STANDARD_MODULE_PROPERTIES
00115 };
00116 /* }}} */
00117 
00118 #ifdef COMPILE_DL_SYSVMSG
00119 ZEND_GET_MODULE(sysvmsg)
00120 #endif
00121 
00122 static void sysvmsg_release(zend_rsrc_list_entry *rsrc TSRMLS_DC)
00123 {
00124        sysvmsg_queue_t * mq = (sysvmsg_queue_t *) rsrc->ptr;
00125        efree(mq);
00126 }
00127 
00128 /* {{{ PHP_MINIT_FUNCTION
00129  */
00130 PHP_MINIT_FUNCTION(sysvmsg)
00131 {
00132        le_sysvmsg = zend_register_list_destructors_ex(sysvmsg_release, NULL, "sysvmsg queue", module_number);
00133        REGISTER_LONG_CONSTANT("MSG_IPC_NOWAIT", PHP_MSG_IPC_NOWAIT, CONST_PERSISTENT|CONST_CS);
00134        REGISTER_LONG_CONSTANT("MSG_EAGAIN",       EAGAIN,           CONST_PERSISTENT|CONST_CS);
00135        REGISTER_LONG_CONSTANT("MSG_ENOMSG",       ENOMSG,           CONST_PERSISTENT|CONST_CS);
00136        REGISTER_LONG_CONSTANT("MSG_NOERROR",    PHP_MSG_NOERROR,    CONST_PERSISTENT|CONST_CS);
00137        REGISTER_LONG_CONSTANT("MSG_EXCEPT",     PHP_MSG_EXCEPT,     CONST_PERSISTENT|CONST_CS);
00138        return SUCCESS;
00139 }
00140 /* }}} */
00141 
00142 /* {{{ PHP_MINFO_FUNCTION
00143  */
00144 PHP_MINFO_FUNCTION(sysvmsg)
00145 {
00146        php_info_print_table_start();
00147        php_info_print_table_row(2, "sysvmsg support", "enabled");
00148        php_info_print_table_row(2, "Revision", "$Revision: 321634 $");
00149        php_info_print_table_end();
00150 }
00151 /* }}} */
00152 
00153 /* {{{ proto bool msg_set_queue(resource queue, array data)
00154    Set information for a message queue */
00155 PHP_FUNCTION(msg_set_queue)
00156 {
00157        zval *queue, *data;
00158        sysvmsg_queue_t *mq = NULL;
00159        struct msqid_ds stat;
00160 
00161        RETVAL_FALSE;
00162 
00163        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &queue, &data) == FAILURE) {
00164               return;
00165        }
00166 
00167        ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
00168 
00169        if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
00170               zval **item;
00171 
00172               /* now pull out members of data and set them in the stat buffer */
00173               if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.uid", sizeof("msg_perm.uid"), (void **) &item) == SUCCESS) {
00174                      convert_to_long_ex(item);
00175                      stat.msg_perm.uid = Z_LVAL_PP(item);
00176               }
00177               if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.gid", sizeof("msg_perm.gid"), (void **) &item) == SUCCESS) {
00178                      convert_to_long_ex(item);
00179                      stat.msg_perm.gid = Z_LVAL_PP(item);
00180               }
00181               if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.mode", sizeof("msg_perm.mode"), (void **) &item) == SUCCESS) {
00182                      convert_to_long_ex(item);
00183                      stat.msg_perm.mode = Z_LVAL_PP(item);
00184               }
00185               if (zend_hash_find(Z_ARRVAL_P(data), "msg_qbytes", sizeof("msg_qbytes"), (void **) &item) == SUCCESS) {
00186                      convert_to_long_ex(item);
00187                      stat.msg_qbytes = Z_LVAL_PP(item);
00188               }
00189               if (msgctl(mq->id, IPC_SET, &stat) == 0) {
00190                      RETVAL_TRUE;
00191               }
00192        }
00193 }
00194 /* }}} */
00195 
00196 /* {{{ proto array msg_stat_queue(resource queue)
00197    Returns information about a message queue */
00198 PHP_FUNCTION(msg_stat_queue)
00199 {
00200        zval *queue;
00201        sysvmsg_queue_t *mq = NULL;
00202        struct msqid_ds stat;
00203 
00204        RETVAL_FALSE;
00205 
00206        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) {
00207               return;
00208        }
00209 
00210        ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
00211 
00212        if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
00213               array_init(return_value);
00214 
00215               add_assoc_long(return_value, "msg_perm.uid", stat.msg_perm.uid);
00216               add_assoc_long(return_value, "msg_perm.gid", stat.msg_perm.gid);
00217               add_assoc_long(return_value, "msg_perm.mode", stat.msg_perm.mode);
00218               add_assoc_long(return_value, "msg_stime",  stat.msg_stime);
00219               add_assoc_long(return_value, "msg_rtime",  stat.msg_rtime);
00220               add_assoc_long(return_value, "msg_ctime",  stat.msg_ctime);
00221               add_assoc_long(return_value, "msg_qnum",   stat.msg_qnum);
00222               add_assoc_long(return_value, "msg_qbytes", stat.msg_qbytes);
00223               add_assoc_long(return_value, "msg_lspid",  stat.msg_lspid);
00224               add_assoc_long(return_value, "msg_lrpid",  stat.msg_lrpid);
00225        }
00226 }
00227 /* }}} */
00228 
00229 
00230 /* {{{ proto bool msg_queue_exists(int key)
00231    Check wether a message queue exists */
00232 PHP_FUNCTION(msg_queue_exists)
00233 {
00234        long key;
00235 
00236        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &key) == FAILURE)  {
00237               return;
00238        }
00239 
00240        if (msgget(key, 0) < 0) {
00241               RETURN_FALSE;
00242        }
00243 
00244        RETURN_TRUE;
00245 }
00246 /* }}} */
00247 
00248 
00249 /* {{{ proto resource msg_get_queue(int key [, int perms])
00250    Attach to a message queue */
00251 PHP_FUNCTION(msg_get_queue)
00252 {
00253        long key;
00254        long perms = 0666;
00255        sysvmsg_queue_t *mq;
00256 
00257        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &key, &perms) == FAILURE)      {
00258               return;
00259        }
00260 
00261        mq = (sysvmsg_queue_t *) emalloc(sizeof(sysvmsg_queue_t));
00262 
00263        mq->key = key;
00264        mq->id = msgget(key, 0);
00265        if (mq->id < 0)      {
00266               /* doesn't already exist; create it */
00267               mq->id = msgget(key, IPC_CREAT | IPC_EXCL | perms);
00268               if (mq->id < 0)      {
00269                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
00270                      efree(mq);
00271                      RETURN_FALSE;
00272               }
00273        }
00274        RETVAL_RESOURCE(zend_list_insert(mq, le_sysvmsg));
00275 }
00276 /* }}} */
00277 
00278 /* {{{ proto bool msg_remove_queue(resource queue)
00279    Destroy the queue */
00280 PHP_FUNCTION(msg_remove_queue)
00281 {
00282        zval *queue;
00283        sysvmsg_queue_t *mq = NULL;
00284 
00285        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) {
00286               return;
00287        }
00288 
00289        ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
00290 
00291        if (msgctl(mq->id, IPC_RMID, NULL) == 0) {
00292               RETVAL_TRUE;
00293        } else {
00294               RETVAL_FALSE;
00295        }
00296 }
00297 /* }}} */
00298 
00299 /* {{{ proto mixed msg_receive(resource queue, int desiredmsgtype, int &msgtype, int maxsize, mixed message [, bool unserialize=true [, int flags=0 [, int errorcode]]])
00300    Send a message of type msgtype (must be > 0) to a message queue */
00301 PHP_FUNCTION(msg_receive)
00302 {
00303        zval *out_message, *queue, *out_msgtype, *zerrcode = NULL;
00304        long desiredmsgtype, maxsize, flags = 0;
00305        long realflags = 0;
00306        zend_bool do_unserialize = 1;
00307        sysvmsg_queue_t *mq = NULL;
00308        struct php_msgbuf *messagebuffer = NULL; /* buffer to transmit */
00309        int result;
00310 
00311        RETVAL_FALSE;
00312 
00313        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlzlz|blz",
00314                             &queue, &desiredmsgtype, &out_msgtype, &maxsize,
00315                             &out_message, &do_unserialize, &flags, &zerrcode) == FAILURE) {
00316               return;
00317        }
00318 
00319        if (maxsize <= 0) {
00320               php_error_docref(NULL TSRMLS_CC, E_WARNING, "maximum size of the message has to be greater than zero");
00321               return;
00322        }
00323 
00324        if (flags != 0) {
00325               if (flags & PHP_MSG_EXCEPT) {
00326 #ifndef MSG_EXCEPT
00327                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "MSG_EXCEPT is not supported on your system");
00328                      RETURN_FALSE;
00329 #else
00330                      realflags |= MSG_EXCEPT;
00331 #endif
00332               }
00333               if (flags & PHP_MSG_NOERROR) {
00334                      realflags |= MSG_NOERROR;
00335               }
00336               if (flags & PHP_MSG_IPC_NOWAIT) {
00337                      realflags |= IPC_NOWAIT;
00338               }
00339        }
00340 
00341        ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
00342 
00343        messagebuffer = (struct php_msgbuf *) safe_emalloc(maxsize, 1, sizeof(struct php_msgbuf));
00344 
00345        result = msgrcv(mq->id, messagebuffer, maxsize, desiredmsgtype, realflags);
00346 
00347        zval_dtor(out_msgtype);
00348        zval_dtor(out_message);
00349        ZVAL_LONG(out_msgtype, 0);
00350        ZVAL_FALSE(out_message);
00351 
00352        if (zerrcode) {
00353               zval_dtor(zerrcode);
00354               ZVAL_LONG(zerrcode, 0);
00355        }
00356 
00357        if (result >= 0) {
00358               /* got it! */
00359               ZVAL_LONG(out_msgtype, messagebuffer->mtype);
00360 
00361               RETVAL_TRUE;
00362               if (do_unserialize)  {
00363                      php_unserialize_data_t var_hash;
00364                      zval *tmp = NULL;
00365                      const unsigned char *p = (const unsigned char *) messagebuffer->mtext;
00366 
00367                      MAKE_STD_ZVAL(tmp);
00368                      PHP_VAR_UNSERIALIZE_INIT(var_hash);
00369                      if (!php_var_unserialize(&tmp, &p, p + result, &var_hash TSRMLS_CC)) {
00370                             php_error_docref(NULL TSRMLS_CC, E_WARNING, "message corrupted");
00371                             RETVAL_FALSE;
00372                      } else {
00373                             REPLACE_ZVAL_VALUE(&out_message, tmp, 0);
00374                      }
00375                      FREE_ZVAL(tmp);
00376                      PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
00377               } else {
00378                      ZVAL_STRINGL(out_message, messagebuffer->mtext, result, 1);
00379               }
00380        } else if (zerrcode) {
00381               ZVAL_LONG(zerrcode, errno);
00382        }
00383        efree(messagebuffer);
00384 }
00385 /* }}} */
00386 
00387 /* {{{ proto bool msg_send(resource queue, int msgtype, mixed message [, bool serialize=true [, bool blocking=true [, int errorcode]]])
00388    Send a message of type msgtype (must be > 0) to a message queue */
00389 PHP_FUNCTION(msg_send)
00390 {
00391        zval *message, *queue, *zerror=NULL;
00392        long msgtype;
00393        zend_bool do_serialize = 1, blocking = 1;
00394        sysvmsg_queue_t * mq = NULL;
00395        struct php_msgbuf * messagebuffer = NULL; /* buffer to transmit */
00396        int result;
00397        int message_len = 0;
00398 
00399        RETVAL_FALSE;
00400 
00401        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz|bbz",
00402                             &queue, &msgtype, &message, &do_serialize, &blocking, &zerror) == FAILURE) {
00403               return;
00404        }
00405 
00406        ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t*, &queue, -1, "sysvmsg queue", le_sysvmsg);
00407 
00408        if (do_serialize) {
00409               smart_str msg_var = {0};
00410               php_serialize_data_t var_hash;
00411 
00412               PHP_VAR_SERIALIZE_INIT(var_hash);
00413               php_var_serialize(&msg_var, &message, &var_hash TSRMLS_CC);
00414               PHP_VAR_SERIALIZE_DESTROY(var_hash);
00415 
00416               /* NB: php_msgbuf is 1 char bigger than a long, so there is no need to
00417                * allocate the extra byte. */
00418               messagebuffer = safe_emalloc(msg_var.len, 1, sizeof(struct php_msgbuf));
00419               memcpy(messagebuffer->mtext, msg_var.c, msg_var.len + 1);
00420               message_len = msg_var.len;
00421               smart_str_free(&msg_var);
00422        } else {
00423               char *p;
00424               switch (Z_TYPE_P(message)) {
00425                      case IS_STRING:
00426                             p = Z_STRVAL_P(message);
00427                             message_len = Z_STRLEN_P(message);
00428                             break;
00429 
00430                      case IS_LONG:
00431                      case IS_BOOL:
00432                             message_len = spprintf(&p, 0, "%ld", Z_LVAL_P(message));
00433                             break;
00434 
00435                      case IS_DOUBLE:
00436                             message_len = spprintf(&p, 0, "%F", Z_DVAL_P(message));
00437                             break;
00438 
00439                      default:
00440                             php_error_docref(NULL TSRMLS_CC, E_WARNING, "Message parameter must be either a string or a number.");
00441                             RETURN_FALSE;
00442               }
00443 
00444               messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
00445               memcpy(messagebuffer->mtext, p, message_len + 1);
00446 
00447               if (Z_TYPE_P(message) != IS_STRING) {
00448                      efree(p);
00449               }
00450        }
00451 
00452        /* set the message type */
00453        messagebuffer->mtype = msgtype;
00454 
00455        result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT);
00456 
00457        efree(messagebuffer);
00458 
00459        if (result == -1) {
00460               php_error_docref(NULL TSRMLS_CC, E_WARNING, "msgsnd failed: %s", strerror(errno));
00461               if (zerror) {
00462                      ZVAL_LONG(zerror, errno);
00463               }
00464        } else {
00465               RETVAL_TRUE;
00466        }
00467 }
00468 /* }}} */
00469 
00470 /*
00471  * Local variables:
00472  * tab-width: 4
00473  * c-basic-offset: 4
00474  * End:
00475  * vim600: noet sw=4 ts=4 tw=78 fdm=marker
00476  * vim<600: noet sw=4 ts=4 tw=78
00477  */