xref: /PHP-5.4/ext/sysvmsg/sysvmsg.c (revision c0d060f5)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 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: adf1d2d6be849c46eed3c3ee6f1cbebd1448d6e5 $ */
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 	PHP_FE_END
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
ZEND_GET_MODULE(sysvmsg)119 ZEND_GET_MODULE(sysvmsg)
120 #endif
121 
122 static void sysvmsg_release(zend_rsrc_list_entry *rsrc TSRMLS_DC)
123 {
124 	sysvmsg_queue_t * mq = (sysvmsg_queue_t *) rsrc->ptr;
125 	efree(mq);
126 }
127 
128 /* {{{ PHP_MINIT_FUNCTION
129  */
PHP_MINIT_FUNCTION(sysvmsg)130 PHP_MINIT_FUNCTION(sysvmsg)
131 {
132 	le_sysvmsg = zend_register_list_destructors_ex(sysvmsg_release, NULL, "sysvmsg queue", module_number);
133 	REGISTER_LONG_CONSTANT("MSG_IPC_NOWAIT", PHP_MSG_IPC_NOWAIT, CONST_PERSISTENT|CONST_CS);
134 	REGISTER_LONG_CONSTANT("MSG_EAGAIN",	 EAGAIN, 	     CONST_PERSISTENT|CONST_CS);
135 	REGISTER_LONG_CONSTANT("MSG_ENOMSG",	 ENOMSG, 	     CONST_PERSISTENT|CONST_CS);
136 	REGISTER_LONG_CONSTANT("MSG_NOERROR",    PHP_MSG_NOERROR,    CONST_PERSISTENT|CONST_CS);
137 	REGISTER_LONG_CONSTANT("MSG_EXCEPT",     PHP_MSG_EXCEPT,     CONST_PERSISTENT|CONST_CS);
138 	return SUCCESS;
139 }
140 /* }}} */
141 
142 /* {{{ PHP_MINFO_FUNCTION
143  */
PHP_MINFO_FUNCTION(sysvmsg)144 PHP_MINFO_FUNCTION(sysvmsg)
145 {
146 	php_info_print_table_start();
147 	php_info_print_table_row(2, "sysvmsg support", "enabled");
148 	php_info_print_table_row(2, "Revision", "$Id: adf1d2d6be849c46eed3c3ee6f1cbebd1448d6e5 $");
149 	php_info_print_table_end();
150 }
151 /* }}} */
152 
153 /* {{{ proto bool msg_set_queue(resource queue, array data)
154    Set information for a message queue */
PHP_FUNCTION(msg_set_queue)155 PHP_FUNCTION(msg_set_queue)
156 {
157 	zval *queue, *data;
158 	sysvmsg_queue_t *mq = NULL;
159 	struct msqid_ds stat;
160 
161 	RETVAL_FALSE;
162 
163 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &queue, &data) == FAILURE) {
164 		return;
165 	}
166 
167 	ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
168 
169 	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 		if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.uid", sizeof("msg_perm.uid"), (void **) &item) == SUCCESS) {
174 			convert_to_long_ex(item);
175 			stat.msg_perm.uid = Z_LVAL_PP(item);
176 		}
177 		if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.gid", sizeof("msg_perm.gid"), (void **) &item) == SUCCESS) {
178 			convert_to_long_ex(item);
179 			stat.msg_perm.gid = Z_LVAL_PP(item);
180 		}
181 		if (zend_hash_find(Z_ARRVAL_P(data), "msg_perm.mode", sizeof("msg_perm.mode"), (void **) &item) == SUCCESS) {
182 			convert_to_long_ex(item);
183 			stat.msg_perm.mode = Z_LVAL_PP(item);
184 		}
185 		if (zend_hash_find(Z_ARRVAL_P(data), "msg_qbytes", sizeof("msg_qbytes"), (void **) &item) == SUCCESS) {
186 			convert_to_long_ex(item);
187 			stat.msg_qbytes = Z_LVAL_PP(item);
188 		}
189 		if (msgctl(mq->id, IPC_SET, &stat) == 0) {
190 			RETVAL_TRUE;
191 		}
192 	}
193 }
194 /* }}} */
195 
196 /* {{{ proto array msg_stat_queue(resource queue)
197    Returns information about a message queue */
PHP_FUNCTION(msg_stat_queue)198 PHP_FUNCTION(msg_stat_queue)
199 {
200 	zval *queue;
201 	sysvmsg_queue_t *mq = NULL;
202 	struct msqid_ds stat;
203 
204 	RETVAL_FALSE;
205 
206 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) {
207 		return;
208 	}
209 
210 	ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
211 
212 	if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
213 		array_init(return_value);
214 
215 		add_assoc_long(return_value, "msg_perm.uid", stat.msg_perm.uid);
216 		add_assoc_long(return_value, "msg_perm.gid", stat.msg_perm.gid);
217 		add_assoc_long(return_value, "msg_perm.mode", stat.msg_perm.mode);
218 		add_assoc_long(return_value, "msg_stime",  stat.msg_stime);
219 		add_assoc_long(return_value, "msg_rtime",  stat.msg_rtime);
220 		add_assoc_long(return_value, "msg_ctime",  stat.msg_ctime);
221 		add_assoc_long(return_value, "msg_qnum",   stat.msg_qnum);
222 		add_assoc_long(return_value, "msg_qbytes", stat.msg_qbytes);
223 		add_assoc_long(return_value, "msg_lspid",  stat.msg_lspid);
224 		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 whether a message queue exists */
PHP_FUNCTION(msg_queue_exists)232 PHP_FUNCTION(msg_queue_exists)
233 {
234 	long key;
235 
236 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &key) == FAILURE)	{
237 		return;
238 	}
239 
240 	if (msgget(key, 0) < 0) {
241 		RETURN_FALSE;
242 	}
243 
244 	RETURN_TRUE;
245 }
246 /* }}} */
247 
248 
249 /* {{{ proto resource msg_get_queue(int key [, int perms])
250    Attach to a message queue */
PHP_FUNCTION(msg_get_queue)251 PHP_FUNCTION(msg_get_queue)
252 {
253 	long key;
254 	long perms = 0666;
255 	sysvmsg_queue_t *mq;
256 
257 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &key, &perms) == FAILURE)	{
258 		return;
259 	}
260 
261 	mq = (sysvmsg_queue_t *) emalloc(sizeof(sysvmsg_queue_t));
262 
263 	mq->key = key;
264 	mq->id = msgget(key, 0);
265 	if (mq->id < 0)	{
266 		/* doesn't already exist; create it */
267 		mq->id = msgget(key, IPC_CREAT | IPC_EXCL | perms);
268 		if (mq->id < 0)	{
269 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
270 			efree(mq);
271 			RETURN_FALSE;
272 		}
273 	}
274 	RETVAL_RESOURCE(zend_list_insert(mq, le_sysvmsg TSRMLS_CC));
275 }
276 /* }}} */
277 
278 /* {{{ proto bool msg_remove_queue(resource queue)
279    Destroy the queue */
PHP_FUNCTION(msg_remove_queue)280 PHP_FUNCTION(msg_remove_queue)
281 {
282 	zval *queue;
283 	sysvmsg_queue_t *mq = NULL;
284 
285 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &queue) == FAILURE) {
286 		return;
287 	}
288 
289 	ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
290 
291 	if (msgctl(mq->id, IPC_RMID, NULL) == 0) {
292 		RETVAL_TRUE;
293 	} else {
294 		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 */
PHP_FUNCTION(msg_receive)301 PHP_FUNCTION(msg_receive)
302 {
303 	zval *out_message, *queue, *out_msgtype, *zerrcode = NULL;
304 	long desiredmsgtype, maxsize, flags = 0;
305 	long realflags = 0;
306 	zend_bool do_unserialize = 1;
307 	sysvmsg_queue_t *mq = NULL;
308 	struct php_msgbuf *messagebuffer = NULL; /* buffer to transmit */
309 	int result;
310 
311 	RETVAL_FALSE;
312 
313 	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 		return;
317 	}
318 
319 	if (maxsize <= 0) {
320 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "maximum size of the message has to be greater than zero");
321 		return;
322 	}
323 
324 	if (flags != 0) {
325 		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 			realflags |= MSG_EXCEPT;
331 #endif
332 		}
333 		if (flags & PHP_MSG_NOERROR) {
334 			realflags |= MSG_NOERROR;
335 		}
336 		if (flags & PHP_MSG_IPC_NOWAIT) {
337 			realflags |= IPC_NOWAIT;
338 		}
339 	}
340 
341 	ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t *, &queue, -1, "sysvmsg queue", le_sysvmsg);
342 
343 	messagebuffer = (struct php_msgbuf *) safe_emalloc(maxsize, 1, sizeof(struct php_msgbuf));
344 
345 	result = msgrcv(mq->id, messagebuffer, maxsize, desiredmsgtype, realflags);
346 
347 	zval_dtor(out_msgtype);
348 	zval_dtor(out_message);
349 	ZVAL_LONG(out_msgtype, 0);
350 	ZVAL_FALSE(out_message);
351 
352 	if (zerrcode) {
353 		zval_dtor(zerrcode);
354 		ZVAL_LONG(zerrcode, 0);
355 	}
356 
357 	if (result >= 0) {
358 		/* got it! */
359 		ZVAL_LONG(out_msgtype, messagebuffer->mtype);
360 
361 		RETVAL_TRUE;
362 		if (do_unserialize)	{
363 			php_unserialize_data_t var_hash;
364 			zval *tmp = NULL;
365 			const unsigned char *p = (const unsigned char *) messagebuffer->mtext;
366 
367 			MAKE_STD_ZVAL(tmp);
368 			PHP_VAR_UNSERIALIZE_INIT(var_hash);
369 			if (!php_var_unserialize(&tmp, &p, p + result, &var_hash TSRMLS_CC)) {
370 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "message corrupted");
371 				RETVAL_FALSE;
372 			} else {
373 				REPLACE_ZVAL_VALUE(&out_message, tmp, 0);
374 			}
375 			FREE_ZVAL(tmp);
376 			PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
377 		} else {
378 			ZVAL_STRINGL(out_message, messagebuffer->mtext, result, 1);
379 		}
380 	} else if (zerrcode) {
381 		ZVAL_LONG(zerrcode, errno);
382 	}
383 	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 */
PHP_FUNCTION(msg_send)389 PHP_FUNCTION(msg_send)
390 {
391 	zval *message, *queue, *zerror=NULL;
392 	long msgtype;
393 	zend_bool do_serialize = 1, blocking = 1;
394 	sysvmsg_queue_t * mq = NULL;
395 	struct php_msgbuf * messagebuffer = NULL; /* buffer to transmit */
396 	int result;
397 	int message_len = 0;
398 
399 	RETVAL_FALSE;
400 
401 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz|bbz",
402 				&queue, &msgtype, &message, &do_serialize, &blocking, &zerror) == FAILURE) {
403 		return;
404 	}
405 
406 	ZEND_FETCH_RESOURCE(mq, sysvmsg_queue_t*, &queue, -1, "sysvmsg queue", le_sysvmsg);
407 
408 	if (do_serialize) {
409 		smart_str msg_var = {0};
410 		php_serialize_data_t var_hash;
411 
412 		PHP_VAR_SERIALIZE_INIT(var_hash);
413 		php_var_serialize(&msg_var, &message, &var_hash TSRMLS_CC);
414 		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 		messagebuffer = safe_emalloc(msg_var.len, 1, sizeof(struct php_msgbuf));
419 		memcpy(messagebuffer->mtext, msg_var.c, msg_var.len + 1);
420 		message_len = msg_var.len;
421 		smart_str_free(&msg_var);
422 	} else {
423 		char *p;
424 		switch (Z_TYPE_P(message)) {
425 			case IS_STRING:
426 				p = Z_STRVAL_P(message);
427 				message_len = Z_STRLEN_P(message);
428 				break;
429 
430 			case IS_LONG:
431 			case IS_BOOL:
432 				message_len = spprintf(&p, 0, "%ld", Z_LVAL_P(message));
433 				break;
434 
435 			case IS_DOUBLE:
436 				message_len = spprintf(&p, 0, "%F", Z_DVAL_P(message));
437 				break;
438 
439 			default:
440 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Message parameter must be either a string or a number.");
441 				RETURN_FALSE;
442 		}
443 
444 		messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
445 		memcpy(messagebuffer->mtext, p, message_len + 1);
446 
447 		if (Z_TYPE_P(message) != IS_STRING) {
448 			efree(p);
449 		}
450 	}
451 
452 	/* set the message type */
453 	messagebuffer->mtype = msgtype;
454 
455 	result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT);
456 
457 	efree(messagebuffer);
458 
459 	if (result == -1) {
460 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "msgsnd failed: %s", strerror(errno));
461 		if (zerror) {
462 			ZVAL_LONG(zerror, errno);
463 		}
464 	} else {
465 		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  */
478