1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Wez Furlong <wez@thebrainroom.com> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: sysvmsg.c 276986 2009-03-10 23:40:06Z helly $ */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include "php.h"
26 : #include "php_globals.h"
27 : #include "php_ini.h"
28 : #include "ext/standard/info.h"
29 : #include "php_sysvmsg.h"
30 : #include "ext/standard/php_var.h"
31 : #include "ext/standard/php_smart_str.h"
32 :
33 : /* In order to detect MSG_EXCEPT use at run time; we have no way
34 : * of knowing what the bit definitions are, so we can't just define
35 : * out own MSG_EXCEPT value. */
36 : #define PHP_MSG_IPC_NOWAIT 1
37 : #define PHP_MSG_NOERROR 2
38 : #define PHP_MSG_EXCEPT 4
39 :
40 : /* True global resources - no need for thread safety here */
41 : static int le_sysvmsg;
42 :
43 : /* {{{ arginfo */
44 : ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_get_queue, 0, 0, 1)
45 : ZEND_ARG_INFO(0, key)
46 : ZEND_ARG_INFO(0, perms)
47 : ZEND_END_ARG_INFO()
48 :
49 : ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_send, 0, 0, 3)
50 : ZEND_ARG_INFO(0, queue)
51 : ZEND_ARG_INFO(0, msgtype)
52 : ZEND_ARG_INFO(0, message)
53 : ZEND_ARG_INFO(0, serialize)
54 : ZEND_ARG_INFO(0, blocking)
55 : ZEND_ARG_INFO(1, errorcode)
56 : ZEND_END_ARG_INFO()
57 :
58 : ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_receive, 0, 0, 5)
59 : ZEND_ARG_INFO(0, queue)
60 : ZEND_ARG_INFO(0, desiredmsgtype)
61 : ZEND_ARG_INFO(1, msgtype)
62 : ZEND_ARG_INFO(0, maxsize)
63 : ZEND_ARG_INFO(1, message)
64 : ZEND_ARG_INFO(0, unserialize)
65 : ZEND_ARG_INFO(0, flags)
66 : ZEND_ARG_INFO(1, errorcode)
67 : ZEND_END_ARG_INFO()
68 :
69 : ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_remove_queue, 0, 0, 1)
70 : ZEND_ARG_INFO(0, queue)
71 : ZEND_END_ARG_INFO()
72 :
73 : ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_stat_queue, 0, 0, 1)
74 : ZEND_ARG_INFO(0, queue)
75 : ZEND_END_ARG_INFO()
76 :
77 : ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_set_queue, 0, 0, 2)
78 : ZEND_ARG_INFO(0, queue)
79 : ZEND_ARG_INFO(0, data)
80 : ZEND_END_ARG_INFO()
81 :
82 : ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_queue_exists, 0, 0, 1)
83 : ZEND_ARG_INFO(0, key)
84 : ZEND_END_ARG_INFO()
85 : /* }}} */
86 :
87 : /* {{{ sysvmsg_functions[]
88 : *
89 : * Every user visible function must have an entry in sysvmsg_functions[].
90 : */
91 : const zend_function_entry sysvmsg_functions[] = {
92 : PHP_FE(msg_get_queue, arginfo_msg_get_queue)
93 : PHP_FE(msg_send, arginfo_msg_send)
94 : PHP_FE(msg_receive, arginfo_msg_receive)
95 : PHP_FE(msg_remove_queue, arginfo_msg_remove_queue)
96 : PHP_FE(msg_stat_queue, arginfo_msg_stat_queue)
97 : PHP_FE(msg_set_queue, arginfo_msg_set_queue)
98 : PHP_FE(msg_queue_exists, arginfo_msg_queue_exists)
99 : {NULL, NULL, NULL} /* Must be the last line in sysvmsg_functions[] */
100 : };
101 : /* }}} */
102 :
103 : /* {{{ sysvmsg_module_entry
104 : */
105 : zend_module_entry sysvmsg_module_entry = {
106 : STANDARD_MODULE_HEADER,
107 : "sysvmsg",
108 : sysvmsg_functions,
109 : PHP_MINIT(sysvmsg),
110 : NULL,
111 : NULL,
112 : NULL,
113 : PHP_MINFO(sysvmsg),
114 : NO_VERSION_YET,
115 : STANDARD_MODULE_PROPERTIES
116 : };
117 : /* }}} */
118 :
119 : #ifdef COMPILE_DL_SYSVMSG
120 : ZEND_GET_MODULE(sysvmsg)
121 : #endif
122 :
123 : /* {{{ PHP_INI
124 : */
125 : /* Remove comments and fill if you need to have entries in php.ini
126 : PHP_INI_BEGIN()
127 : STD_PHP_INI_ENTRY("sysvmsg.value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_sysvmsg_globals, sysvmsg_globals)
128 : STD_PHP_INI_ENTRY("sysvmsg.string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_sysvmsg_globals, sysvmsg_globals)
129 : PHP_INI_END()
130 : */
131 : /* }}} */
132 :
133 : static void sysvmsg_release(zend_rsrc_list_entry *rsrc TSRMLS_DC)
134 4 : {
135 4 : sysvmsg_queue_t * mq = (sysvmsg_queue_t *) rsrc->ptr;
136 4 : efree(mq);
137 4 : }
138 :
139 : /* {{{ PHP_MINIT_FUNCTION
140 : */
141 : PHP_MINIT_FUNCTION(sysvmsg)
142 17007 : {
143 17007 : le_sysvmsg = zend_register_list_destructors_ex(sysvmsg_release, NULL, "sysvmsg queue", module_number);
144 17007 : REGISTER_LONG_CONSTANT("MSG_IPC_NOWAIT", PHP_MSG_IPC_NOWAIT, CONST_PERSISTENT|CONST_CS);
145 17007 : REGISTER_LONG_CONSTANT("MSG_EAGAIN", EAGAIN, CONST_PERSISTENT|CONST_CS);
146 17007 : REGISTER_LONG_CONSTANT("MSG_ENOMSG", ENOMSG, CONST_PERSISTENT|CONST_CS);
147 17007 : REGISTER_LONG_CONSTANT("MSG_NOERROR", PHP_MSG_NOERROR, CONST_PERSISTENT|CONST_CS);
148 17007 : REGISTER_LONG_CONSTANT("MSG_EXCEPT", PHP_MSG_EXCEPT, CONST_PERSISTENT|CONST_CS);
149 17007 : return SUCCESS;
150 : }
151 : /* }}} */
152 :
153 : /* {{{ PHP_MINFO_FUNCTION
154 : */
155 : PHP_MINFO_FUNCTION(sysvmsg)
156 43 : {
157 43 : php_info_print_table_start();
158 43 : php_info_print_table_row(2, "sysvmsg support", "enabled");
159 43 : php_info_print_table_row(2, "Revision", "$Revision: 276986 $");
160 43 : php_info_print_table_end();
161 43 : }
162 : /* }}} */
163 :
164 : /* {{{ proto bool msg_set_queue(resource queue, array data) U
165 : Set information for a message queue */
166 : PHP_FUNCTION(msg_set_queue)
167 0 : {
168 : zval *queue, *data;
169 0 : sysvmsg_queue_t *mq = NULL;
170 : struct msqid_ds stat;
171 :
172 0 : RETVAL_FALSE;
173 :
174 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &queue, &data) == FAILURE) {
175 0 : return;
176 : }
177 :
178 0 : ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
179 :
180 0 : if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
181 : zval **item;
182 :
183 : /* now pull out members of data and set them in the stat buffer */
184 0 : if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.uid", sizeof("msg_perm.uid"), (void **) &item) == SUCCESS) {
185 0 : convert_to_long_ex(item);
186 0 : stat.msg_perm.uid = Z_LVAL_PP(item);
187 : }
188 0 : if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.gid", sizeof("msg_perm.gid"), (void **) &item) == SUCCESS) {
189 0 : convert_to_long_ex(item);
190 0 : stat.msg_perm.gid = Z_LVAL_PP(item);
191 : }
192 0 : if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.mode", sizeof("msg_perm.mode"), (void **) &item) == SUCCESS) {
193 0 : convert_to_long_ex(item);
194 0 : stat.msg_perm.mode = Z_LVAL_PP(item);
195 : }
196 0 : if (zend_hash_find(Z_ARRVAL_P(data), "msg_qbytes", sizeof("msg_qbytes"), (void **) &item) == SUCCESS) {
197 0 : convert_to_long_ex(item);
198 0 : stat.msg_qbytes = Z_LVAL_PP(item);
199 : }
200 0 : if (msgctl(mq->id, IPC_SET, &stat) == 0) {
201 0 : RETVAL_TRUE;
202 : }
203 : }
204 : }
205 : /* }}} */
206 :
207 : /* {{{ proto array msg_stat_queue(resource queue) U
208 : Returns information about a message queue */
209 : PHP_FUNCTION(msg_stat_queue)
210 0 : {
211 : zval *queue;
212 0 : sysvmsg_queue_t *mq = NULL;
213 : struct msqid_ds stat;
214 :
215 0 : RETVAL_FALSE;
216 :
217 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) {
218 0 : return;
219 : }
220 :
221 0 : ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
222 :
223 0 : if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
224 0 : array_init(return_value);
225 :
226 0 : add_assoc_long(return_value, "msg_perm.uid", stat.msg_perm.uid);
227 0 : add_assoc_long(return_value, "msg_perm.gid", stat.msg_perm.gid);
228 0 : add_assoc_long(return_value, "msg_perm.mode", stat.msg_perm.mode);
229 0 : add_assoc_long(return_value, "msg_stime", stat.msg_stime);
230 0 : add_assoc_long(return_value, "msg_rtime", stat.msg_rtime);
231 0 : add_assoc_long(return_value, "msg_ctime", stat.msg_ctime);
232 0 : add_assoc_long(return_value, "msg_qnum", stat.msg_qnum);
233 0 : add_assoc_long(return_value, "msg_qbytes", stat.msg_qbytes);
234 0 : add_assoc_long(return_value, "msg_lspid", stat.msg_lspid);
235 0 : add_assoc_long(return_value, "msg_lrpid", stat.msg_lrpid);
236 : }
237 : }
238 : /* }}} */
239 :
240 : /* {{{ proto bool msg_queue_exists(int key)
241 : Check wether a message queue exists */
242 : PHP_FUNCTION(msg_queue_exists)
243 3 : {
244 : long key;
245 :
246 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &key) == FAILURE) {
247 0 : return;
248 : }
249 :
250 3 : if (msgget(key, 0) < 0) {
251 2 : RETURN_FALSE;
252 : }
253 :
254 1 : RETURN_TRUE;
255 : }
256 : /* }}} */
257 :
258 : /* {{{ proto resource msg_get_queue(int key [, int perms]) U
259 : Attach to a message queue */
260 : PHP_FUNCTION(msg_get_queue)
261 4 : {
262 : long key;
263 4 : long perms = 0666;
264 : sysvmsg_queue_t *mq;
265 :
266 4 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &key, &perms) == FAILURE) {
267 0 : return;
268 : }
269 :
270 4 : mq = (sysvmsg_queue_t *) emalloc(sizeof(sysvmsg_queue_t));
271 :
272 4 : mq->key = key;
273 4 : mq->id = msgget(key, 0);
274 4 : if (mq->id < 0) {
275 : /* doesn't already exist; create it */
276 4 : mq->id = msgget(key, IPC_CREAT | IPC_EXCL | perms);
277 4 : if (mq->id < 0) {
278 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
279 0 : efree(mq);
280 0 : RETURN_FALSE;
281 : }
282 : }
283 4 : RETVAL_RESOURCE(zend_list_insert(mq, le_sysvmsg));
284 : }
285 : /* }}} */
286 :
287 : /* {{{ proto bool msg_remove_queue(resource queue) U
288 : Destroy the queue */
289 : PHP_FUNCTION(msg_remove_queue)
290 4 : {
291 : zval *queue;
292 4 : sysvmsg_queue_t *mq = NULL;
293 :
294 4 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) {
295 0 : return;
296 : }
297 :
298 4 : ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
299 :
300 4 : if (msgctl(mq->id, IPC_RMID, NULL) == 0) {
301 4 : RETVAL_TRUE;
302 : } else {
303 0 : RETVAL_FALSE;
304 : }
305 : }
306 : /* }}} */
307 :
308 : /* {{{ proto mixed msg_receive(resource queue, int desiredmsgtype, int &msgtype, int maxsize, mixed message [, bool unserialize=true [, int flags=0 [, int errorcode]]]) U
309 : Send a message of type msgtype (must be > 0) to a message queue */
310 : PHP_FUNCTION(msg_receive)
311 2 : {
312 2 : zval *out_message, *queue, *out_msgtype, *zerrcode = NULL;
313 2 : long desiredmsgtype, maxsize, flags = 0;
314 2 : long realflags = 0;
315 2 : zend_bool do_unserialize = 1;
316 2 : sysvmsg_queue_t *mq = NULL;
317 2 : struct php_msgbuf *messagebuffer = NULL; /* buffer to transmit */
318 : int result;
319 :
320 2 : RETVAL_FALSE;
321 :
322 2 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlzlz|blz",
323 : &queue, &desiredmsgtype, &out_msgtype, &maxsize,
324 : &out_message, &do_unserialize, &flags, &zerrcode) == FAILURE) {
325 0 : return;
326 : }
327 :
328 2 : if (maxsize <= 0) {
329 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "maximum size of the message has to be greater than zero");
330 0 : return;
331 : }
332 :
333 2 : if (flags != 0) {
334 0 : if (flags & PHP_MSG_EXCEPT) {
335 : #ifndef MSG_EXCEPT
336 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "MSG_EXCEPT is not supported on your system");
337 : RETURN_FALSE;
338 : #else
339 0 : realflags |= MSG_EXCEPT;
340 : #endif
341 : }
342 0 : if (flags & PHP_MSG_NOERROR) {
343 0 : realflags |= MSG_NOERROR;
344 : }
345 0 : if (flags & PHP_MSG_IPC_NOWAIT) {
346 0 : realflags |= IPC_NOWAIT;
347 : }
348 : }
349 :
350 2 : ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
351 :
352 2 : messagebuffer = (struct php_msgbuf *) safe_emalloc(maxsize, 1, sizeof(struct php_msgbuf));
353 :
354 2 : result = msgrcv(mq->id, messagebuffer, maxsize, desiredmsgtype, realflags);
355 :
356 2 : zval_dtor(out_msgtype);
357 2 : zval_dtor(out_message);
358 2 : ZVAL_LONG(out_msgtype, 0);
359 2 : ZVAL_FALSE(out_message);
360 :
361 2 : if (zerrcode) {
362 1 : zval_dtor(zerrcode);
363 1 : ZVAL_LONG(zerrcode, 0);
364 : }
365 :
366 2 : if (result >= 0) {
367 : /* got it! */
368 2 : ZVAL_LONG(out_msgtype, messagebuffer->mtype);
369 :
370 2 : RETVAL_TRUE;
371 2 : if (do_unserialize) {
372 : php_unserialize_data_t var_hash;
373 2 : zval *tmp = NULL;
374 2 : const unsigned char *p = (const unsigned char *) messagebuffer->mtext;
375 :
376 2 : MAKE_STD_ZVAL(tmp);
377 2 : PHP_VAR_UNSERIALIZE_INIT(var_hash);
378 2 : if (!php_var_unserialize(&tmp, &p, p + result, &var_hash TSRMLS_CC)) {
379 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "message corrupted");
380 1 : RETVAL_FALSE;
381 : } else {
382 1 : REPLACE_ZVAL_VALUE(&out_message, tmp, 0);
383 : }
384 2 : FREE_ZVAL(tmp);
385 2 : PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
386 : } else {
387 0 : ZVAL_STRINGL(out_message, messagebuffer->mtext, result, 1);
388 : }
389 0 : } else if (zerrcode) {
390 0 : ZVAL_LONG(zerrcode, errno);
391 : }
392 2 : efree(messagebuffer);
393 : }
394 : /* }}} */
395 :
396 : /* {{{ proto bool msg_send(resource queue, int msgtype, mixed message [, bool serialize=true [, bool blocking=true [, int errorcode]]]) U
397 : Send a message of type msgtype (must be > 0) to a message queue */
398 : PHP_FUNCTION(msg_send)
399 2 : {
400 2 : zval *message, *queue, *zerror=NULL;
401 : long msgtype;
402 2 : zend_bool do_serialize = 1, blocking = 1;
403 2 : sysvmsg_queue_t * mq = NULL;
404 2 : struct php_msgbuf * messagebuffer = NULL; /* buffer to transmit */
405 : int result;
406 2 : int message_len = 0;
407 :
408 2 : RETVAL_FALSE;
409 :
410 2 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz|bbz",
411 : &queue, &msgtype, &message, &do_serialize, &blocking, &zerror) == FAILURE) {
412 0 : return;
413 : }
414 :
415 2 : ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t*, &queue, -1, "sysvmsg queue", le_sysvmsg);
416 :
417 2 : if (do_serialize) {
418 1 : smart_str msg_var = {0};
419 : php_serialize_data_t var_hash;
420 :
421 1 : PHP_VAR_SERIALIZE_INIT(var_hash);
422 1 : php_var_serialize(&msg_var, &message, &var_hash TSRMLS_CC);
423 1 : PHP_VAR_SERIALIZE_DESTROY(var_hash);
424 :
425 : /* NB: php_msgbuf is 1 char bigger than a long, so there is no need to
426 : * allocate the extra byte. */
427 1 : messagebuffer = safe_emalloc(msg_var.len, 1, sizeof(struct php_msgbuf));
428 1 : memcpy(messagebuffer->mtext, msg_var.c, msg_var.len + 1);
429 1 : message_len = msg_var.len;
430 1 : smart_str_free(&msg_var);
431 : } else {
432 1 : char *p = NULL;
433 1 : switch (Z_TYPE_P(message)) {
434 : case IS_UNICODE:
435 1 : if (SUCCESS != zend_unicode_to_string(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &p, &message_len, Z_USTRVAL_P(message), Z_USTRLEN_P(message) TSRMLS_CC)) {
436 0 : RETURN_FALSE;
437 : }
438 1 : break;
439 :
440 : case IS_STRING:
441 0 : p = Z_STRVAL_P(message);
442 0 : message_len = Z_STRLEN_P(message);
443 0 : break;
444 :
445 : case IS_LONG:
446 : case IS_BOOL:
447 0 : message_len = spprintf(&p, 0, "%ld", Z_LVAL_P(message));
448 0 : break;
449 :
450 : case IS_DOUBLE:
451 0 : message_len = spprintf(&p, 0, "%f", Z_DVAL_P(message));
452 0 : break;
453 :
454 : default:
455 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Message parameter must be either a string or a number");
456 0 : RETURN_FALSE;
457 : }
458 :
459 1 : messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
460 1 : memcpy(messagebuffer->mtext, p, message_len + 1);
461 :
462 1 : if (Z_TYPE_P(message) != IS_STRING) {
463 1 : efree(p);
464 : }
465 : }
466 :
467 : /* set the message type */
468 2 : messagebuffer->mtype = msgtype;
469 :
470 2 : result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT);
471 :
472 2 : efree(messagebuffer);
473 :
474 2 : if (result == -1) {
475 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "msgsnd failed: %s", strerror(errno));
476 0 : if (zerror) {
477 0 : ZVAL_LONG(zerror, errno);
478 : }
479 : } else {
480 2 : RETVAL_TRUE;
481 : }
482 : }
483 : /* }}} */
484 :
485 : /*
486 : * Local variables:
487 : * tab-width: 4
488 : * c-basic-offset: 4
489 : * End:
490 : * vim600: noet sw=4 ts=4 tw=78 fdm=marker
491 : * vim<600: noet sw=4 ts=4 tw=78
492 : */
|