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