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