Back to index

php5  5.3.10
Defines | Functions | Variables
sysvmsg.c File Reference
#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"

Go to the source code of this file.

Defines

#define PHP_MSG_IPC_NOWAIT   1
#define PHP_MSG_NOERROR   2
#define PHP_MSG_EXCEPT   4

Functions

static void sysvmsg_release (zend_rsrc_list_entry *rsrc TSRMLS_DC)
 PHP_MINIT_FUNCTION (sysvmsg)
 PHP_MINFO_FUNCTION (sysvmsg)
 PHP_FUNCTION (msg_set_queue)
 PHP_FUNCTION (msg_stat_queue)
 PHP_FUNCTION (msg_queue_exists)
 PHP_FUNCTION (msg_get_queue)
 PHP_FUNCTION (msg_remove_queue)
 PHP_FUNCTION (msg_receive)
 PHP_FUNCTION (msg_send)

Variables

static int le_sysvmsg
const zend_function_entry sysvmsg_functions []
zend_module_entry sysvmsg_module_entry

Define Documentation

#define PHP_MSG_EXCEPT   4

Definition at line 37 of file sysvmsg.c.

#define PHP_MSG_IPC_NOWAIT   1

Definition at line 35 of file sysvmsg.c.

#define PHP_MSG_NOERROR   2

Definition at line 36 of file sysvmsg.c.


Function Documentation

PHP_FUNCTION ( msg_set_queue  )

Definition at line 155 of file sysvmsg.c.

{
       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;
              }
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( msg_stat_queue  )

Definition at line 198 of file sysvmsg.c.

{
       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);
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( msg_queue_exists  )

Definition at line 232 of file sysvmsg.c.

{
       long key;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &key) == FAILURE)  {
              return;
       }

       if (msgget(key, 0) < 0) {
              RETURN_FALSE;
       }

       RETURN_TRUE;
}

Here is the call graph for this function:

PHP_FUNCTION ( msg_get_queue  )

Definition at line 251 of file sysvmsg.c.

{
       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));
}

Here is the call graph for this function:

PHP_FUNCTION ( msg_remove_queue  )

Definition at line 280 of file sysvmsg.c.

{
       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;
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( msg_receive  )

Definition at line 301 of file sysvmsg.c.

{
       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);
}

Here is the call graph for this function:

PHP_FUNCTION ( msg_send  )

Definition at line 389 of file sysvmsg.c.

{
       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;
       }
}

Here is the call graph for this function:

PHP_MINFO_FUNCTION ( sysvmsg  )

Definition at line 144 of file sysvmsg.c.

{
       php_info_print_table_start();
       php_info_print_table_row(2, "sysvmsg support", "enabled");
       php_info_print_table_row(2, "Revision", "$Revision: 321634 $");
       php_info_print_table_end();
}

Here is the call graph for this function:

PHP_MINIT_FUNCTION ( sysvmsg  )

Definition at line 130 of file sysvmsg.c.

Here is the call graph for this function:

static void sysvmsg_release ( zend_rsrc_list_entry *rsrc  TSRMLS_DC) [static]

Definition at line 122 of file sysvmsg.c.

{
       sysvmsg_queue_t * mq = (sysvmsg_queue_t *) rsrc->ptr;
       efree(mq);
}

Here is the caller graph for this function:


Variable Documentation

int le_sysvmsg [static]

Definition at line 40 of file sysvmsg.c.

Initial value:

Definition at line 90 of file sysvmsg.c.

zend_module_entry sysvmsg_module_entry
Initial value:

Definition at line 104 of file sysvmsg.c.