xref: /PHP-8.0/ext/sysvmsg/sysvmsg.c (revision 9494b1cd)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.php.net/license/3_01.txt                                  |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Wez Furlong <wez@thebrainroom.com>                           |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_globals.h"
23 #include "ext/standard/info.h"
24 #include "php_sysvmsg.h"
25 #include "sysvmsg_arginfo.h"
26 #include "ext/standard/php_var.h"
27 #include "zend_smart_str.h"
28 #include "Zend/zend_interfaces.h"
29 
30 #include <sys/types.h>
31 #include <sys/ipc.h>
32 #include <sys/msg.h>
33 
34 PHP_MINIT_FUNCTION(sysvmsg);
35 PHP_MINFO_FUNCTION(sysvmsg);
36 
37 typedef struct {
38 	key_t key;
39 	zend_long id;
40 	zend_object std;
41 } sysvmsg_queue_t;
42 
43 struct php_msgbuf {
44 	zend_long mtype;
45 	char mtext[1];
46 };
47 
48 /* In order to detect MSG_EXCEPT use at run time; we have no way
49  * of knowing what the bit definitions are, so we can't just define
50  * out own MSG_EXCEPT value. */
51 #define PHP_MSG_IPC_NOWAIT	1
52 #define PHP_MSG_NOERROR		2
53 #define PHP_MSG_EXCEPT		4
54 
55 /* {{{ sysvmsg_module_entry */
56 zend_module_entry sysvmsg_module_entry = {
57 	STANDARD_MODULE_HEADER,
58 	"sysvmsg",
59 	ext_functions,
60 	PHP_MINIT(sysvmsg),
61 	NULL,
62 	NULL,
63 	NULL,
64 	PHP_MINFO(sysvmsg),
65 	PHP_SYSVMSG_VERSION,
66 	STANDARD_MODULE_PROPERTIES
67 };
68 /* }}} */
69 
70 #ifdef COMPILE_DL_SYSVMSG
71 ZEND_GET_MODULE(sysvmsg)
72 #endif
73 
74 /* SysvMessageQueue class */
75 
76 zend_class_entry *sysvmsg_queue_ce;
77 static zend_object_handlers sysvmsg_queue_object_handlers;
78 
sysvmsg_queue_from_obj(zend_object * obj)79 static inline sysvmsg_queue_t *sysvmsg_queue_from_obj(zend_object *obj) {
80 	return (sysvmsg_queue_t *)((char *)(obj) - XtOffsetOf(sysvmsg_queue_t, std));
81 }
82 
83 #define Z_SYSVMSG_QUEUE_P(zv) sysvmsg_queue_from_obj(Z_OBJ_P(zv))
84 
sysvmsg_queue_create_object(zend_class_entry * class_type)85 static zend_object *sysvmsg_queue_create_object(zend_class_entry *class_type) {
86 	sysvmsg_queue_t *intern = zend_object_alloc(sizeof(sysvmsg_queue_t), class_type);
87 
88 	zend_object_std_init(&intern->std, class_type);
89 	object_properties_init(&intern->std, class_type);
90 	intern->std.handlers = &sysvmsg_queue_object_handlers;
91 
92 	return &intern->std;
93 }
94 
sysvmsg_queue_get_constructor(zend_object * object)95 static zend_function *sysvmsg_queue_get_constructor(zend_object *object) {
96 	zend_throw_error(NULL, "Cannot directly construct SysvMessageQueue, use msg_get_queue() instead");
97 	return NULL;
98 }
99 
sysvmsg_queue_free_obj(zend_object * object)100 static void sysvmsg_queue_free_obj(zend_object *object)
101 {
102 	sysvmsg_queue_t *sysvmsg_queue = sysvmsg_queue_from_obj(object);
103 
104 	zend_object_std_dtor(&sysvmsg_queue->std);
105 }
106 /* }}} */
107 
108 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(sysvmsg)109 PHP_MINIT_FUNCTION(sysvmsg)
110 {
111 	zend_class_entry ce;
112 	INIT_CLASS_ENTRY(ce, "SysvMessageQueue", class_SysvMessageQueue_methods);
113 	sysvmsg_queue_ce = zend_register_internal_class(&ce);
114 	sysvmsg_queue_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES;
115 	sysvmsg_queue_ce->create_object = sysvmsg_queue_create_object;
116 	sysvmsg_queue_ce->serialize = zend_class_serialize_deny;
117 	sysvmsg_queue_ce->unserialize = zend_class_unserialize_deny;
118 
119 	memcpy(&sysvmsg_queue_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
120 	sysvmsg_queue_object_handlers.offset = XtOffsetOf(sysvmsg_queue_t, std);
121 	sysvmsg_queue_object_handlers.free_obj = sysvmsg_queue_free_obj;
122 	sysvmsg_queue_object_handlers.get_constructor = sysvmsg_queue_get_constructor;
123 	sysvmsg_queue_object_handlers.clone_obj = NULL;
124 	sysvmsg_queue_object_handlers.compare = zend_objects_not_comparable;
125 
126 	REGISTER_LONG_CONSTANT("MSG_IPC_NOWAIT", PHP_MSG_IPC_NOWAIT, CONST_PERSISTENT|CONST_CS);
127 	REGISTER_LONG_CONSTANT("MSG_EAGAIN",	 EAGAIN, 	     CONST_PERSISTENT|CONST_CS);
128 	REGISTER_LONG_CONSTANT("MSG_ENOMSG",	 ENOMSG, 	     CONST_PERSISTENT|CONST_CS);
129 	REGISTER_LONG_CONSTANT("MSG_NOERROR",    PHP_MSG_NOERROR,    CONST_PERSISTENT|CONST_CS);
130 	REGISTER_LONG_CONSTANT("MSG_EXCEPT",     PHP_MSG_EXCEPT,     CONST_PERSISTENT|CONST_CS);
131 	return SUCCESS;
132 }
133 /* }}} */
134 
135 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(sysvmsg)136 PHP_MINFO_FUNCTION(sysvmsg)
137 {
138 	php_info_print_table_start();
139 	php_info_print_table_row(2, "sysvmsg support", "enabled");
140 	php_info_print_table_end();
141 }
142 /* }}} */
143 
144 /* {{{ Set information for a message queue */
PHP_FUNCTION(msg_set_queue)145 PHP_FUNCTION(msg_set_queue)
146 {
147 	zval *queue, *data;
148 	sysvmsg_queue_t *mq = NULL;
149 	struct msqid_ds stat;
150 
151 	RETVAL_FALSE;
152 
153 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &queue, sysvmsg_queue_ce, &data) == FAILURE) {
154 		RETURN_THROWS();
155 	}
156 
157 	mq = Z_SYSVMSG_QUEUE_P(queue);
158 
159 	if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
160 		zval *item;
161 
162 		/* now pull out members of data and set them in the stat buffer */
163 		if ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_perm.uid", sizeof("msg_perm.uid") - 1)) != NULL) {
164 			stat.msg_perm.uid = zval_get_long(item);
165 		}
166 		if ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_perm.gid", sizeof("msg_perm.gid") - 1)) != NULL) {
167 			stat.msg_perm.gid = zval_get_long(item);
168 		}
169 		if ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_perm.mode", sizeof("msg_perm.mode") - 1)) != NULL) {
170 			stat.msg_perm.mode = zval_get_long(item);
171 		}
172 		if ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_qbytes", sizeof("msg_qbytes") - 1)) != NULL) {
173 			stat.msg_qbytes = zval_get_long(item);
174 		}
175 		if (msgctl(mq->id, IPC_SET, &stat) == 0) {
176 			RETVAL_TRUE;
177 		}
178 	}
179 }
180 /* }}} */
181 
182 /* {{{ Returns information about a message queue */
PHP_FUNCTION(msg_stat_queue)183 PHP_FUNCTION(msg_stat_queue)
184 {
185 	zval *queue;
186 	sysvmsg_queue_t *mq = NULL;
187 	struct msqid_ds stat;
188 
189 	RETVAL_FALSE;
190 
191 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &queue, sysvmsg_queue_ce) == FAILURE) {
192 		RETURN_THROWS();
193 	}
194 
195 	mq = Z_SYSVMSG_QUEUE_P(queue);
196 
197 	if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
198 		array_init(return_value);
199 
200 		add_assoc_long(return_value, "msg_perm.uid", stat.msg_perm.uid);
201 		add_assoc_long(return_value, "msg_perm.gid", stat.msg_perm.gid);
202 		add_assoc_long(return_value, "msg_perm.mode", stat.msg_perm.mode);
203 		add_assoc_long(return_value, "msg_stime",  stat.msg_stime);
204 		add_assoc_long(return_value, "msg_rtime",  stat.msg_rtime);
205 		add_assoc_long(return_value, "msg_ctime",  stat.msg_ctime);
206 		add_assoc_long(return_value, "msg_qnum",   stat.msg_qnum);
207 		add_assoc_long(return_value, "msg_qbytes", stat.msg_qbytes);
208 		add_assoc_long(return_value, "msg_lspid",  stat.msg_lspid);
209 		add_assoc_long(return_value, "msg_lrpid",  stat.msg_lrpid);
210 	}
211 }
212 /* }}} */
213 
214 /* {{{ Check whether a message queue exists */
PHP_FUNCTION(msg_queue_exists)215 PHP_FUNCTION(msg_queue_exists)
216 {
217 	zend_long key;
218 
219 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &key) == FAILURE)	{
220 		RETURN_THROWS();
221 	}
222 
223 	if (msgget(key, 0) < 0) {
224 		RETURN_FALSE;
225 	}
226 
227 	RETURN_TRUE;
228 }
229 /* }}} */
230 
231 /* {{{ Attach to a message queue */
PHP_FUNCTION(msg_get_queue)232 PHP_FUNCTION(msg_get_queue)
233 {
234 	zend_long key;
235 	zend_long perms = 0666;
236 	sysvmsg_queue_t *mq;
237 
238 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &key, &perms) == FAILURE)	{
239 		RETURN_THROWS();
240 	}
241 
242 	object_init_ex(return_value, sysvmsg_queue_ce);
243 	mq = Z_SYSVMSG_QUEUE_P(return_value);
244 
245 	mq->key = key;
246 	mq->id = msgget(key, 0);
247 	if (mq->id < 0)	{
248 		/* doesn't already exist; create it */
249 		mq->id = msgget(key, IPC_CREAT | IPC_EXCL | perms);
250 		if (mq->id < 0)	{
251 			php_error_docref(NULL, E_WARNING, "Failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
252 			zval_ptr_dtor(return_value);
253 			RETURN_FALSE;
254 		}
255 	}
256 }
257 /* }}} */
258 
259 /* {{{ Destroy the queue */
PHP_FUNCTION(msg_remove_queue)260 PHP_FUNCTION(msg_remove_queue)
261 {
262 	zval *queue;
263 	sysvmsg_queue_t *mq = NULL;
264 
265 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &queue, sysvmsg_queue_ce) == FAILURE) {
266 		RETURN_THROWS();
267 	}
268 
269 	mq = Z_SYSVMSG_QUEUE_P(queue);
270 
271 	if (msgctl(mq->id, IPC_RMID, NULL) == 0) {
272 		RETVAL_TRUE;
273 	} else {
274 		RETVAL_FALSE;
275 	}
276 }
277 /* }}} */
278 
279 /* {{{ Send a message of type msgtype (must be > 0) to a message queue */
PHP_FUNCTION(msg_receive)280 PHP_FUNCTION(msg_receive)
281 {
282 	zval *out_message, *queue, *out_msgtype, *zerrcode = NULL;
283 	zend_long desiredmsgtype, maxsize, flags = 0;
284 	zend_long realflags = 0;
285 	zend_bool do_unserialize = 1;
286 	sysvmsg_queue_t *mq = NULL;
287 	struct php_msgbuf *messagebuffer = NULL; /* buffer to transmit */
288 	int result;
289 
290 	RETVAL_FALSE;
291 
292 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olzlz|blz",
293 				&queue, sysvmsg_queue_ce, &desiredmsgtype, &out_msgtype, &maxsize,
294 				&out_message, &do_unserialize, &flags, &zerrcode) == FAILURE) {
295 		RETURN_THROWS();
296 	}
297 
298 	if (maxsize <= 0) {
299 		zend_argument_value_error(4, "must be greater than 0");
300 		RETURN_THROWS();
301 	}
302 
303 	if (flags != 0) {
304 		if (flags & PHP_MSG_EXCEPT) {
305 #ifndef MSG_EXCEPT
306 			php_error_docref(NULL, E_WARNING, "MSG_EXCEPT is not supported on your system");
307 			RETURN_FALSE;
308 #else
309 			realflags |= MSG_EXCEPT;
310 #endif
311 		}
312 		if (flags & PHP_MSG_NOERROR) {
313 			realflags |= MSG_NOERROR;
314 		}
315 		if (flags & PHP_MSG_IPC_NOWAIT) {
316 			realflags |= IPC_NOWAIT;
317 		}
318 	}
319 
320 	mq = Z_SYSVMSG_QUEUE_P(queue);
321 
322 	messagebuffer = (struct php_msgbuf *) safe_emalloc(maxsize, 1, sizeof(struct php_msgbuf));
323 
324 	result = msgrcv(mq->id, messagebuffer, maxsize, desiredmsgtype, realflags);
325 
326 	if (result >= 0) {
327 		/* got it! */
328 		ZEND_TRY_ASSIGN_REF_LONG(out_msgtype, messagebuffer->mtype);
329 		if (zerrcode) {
330 			ZEND_TRY_ASSIGN_REF_LONG(zerrcode, 0);
331 		}
332 
333 		RETVAL_TRUE;
334 		if (do_unserialize)	{
335 			php_unserialize_data_t var_hash;
336 			zval tmp;
337 			const unsigned char *p = (const unsigned char *) messagebuffer->mtext;
338 
339 			PHP_VAR_UNSERIALIZE_INIT(var_hash);
340 			if (!php_var_unserialize(&tmp, &p, p + result, &var_hash)) {
341 				php_error_docref(NULL, E_WARNING, "Message corrupted");
342 				ZEND_TRY_ASSIGN_REF_FALSE(out_message);
343 				RETVAL_FALSE;
344 			} else {
345 				ZEND_TRY_ASSIGN_REF_TMP(out_message, &tmp);
346 			}
347 			PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
348 		} else {
349 			ZEND_TRY_ASSIGN_REF_STRINGL(out_message, messagebuffer->mtext, result);
350 		}
351 	} else {
352 		ZEND_TRY_ASSIGN_REF_LONG(out_msgtype, 0);
353 		ZEND_TRY_ASSIGN_REF_FALSE(out_message);
354 		if (zerrcode) {
355 			ZEND_TRY_ASSIGN_REF_LONG(zerrcode, errno);
356 		}
357 	}
358 	efree(messagebuffer);
359 }
360 /* }}} */
361 
362 /* {{{ Send a message of type msgtype (must be > 0) to a message queue */
PHP_FUNCTION(msg_send)363 PHP_FUNCTION(msg_send)
364 {
365 	zval *message, *queue, *zerror=NULL;
366 	zend_long msgtype;
367 	zend_bool do_serialize = 1, blocking = 1;
368 	sysvmsg_queue_t * mq = NULL;
369 	struct php_msgbuf * messagebuffer = NULL; /* buffer to transmit */
370 	int result;
371 	size_t message_len = 0;
372 
373 	RETVAL_FALSE;
374 
375 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olz|bbz",
376 				&queue, sysvmsg_queue_ce, &msgtype, &message, &do_serialize, &blocking, &zerror) == FAILURE) {
377 		RETURN_THROWS();
378 	}
379 
380 	mq = Z_SYSVMSG_QUEUE_P(queue);
381 
382 	if (do_serialize) {
383 		smart_str msg_var = {0};
384 		php_serialize_data_t var_hash;
385 
386 		PHP_VAR_SERIALIZE_INIT(var_hash);
387 		php_var_serialize(&msg_var, message, &var_hash);
388 		PHP_VAR_SERIALIZE_DESTROY(var_hash);
389 
390 		/* NB: php_msgbuf is 1 char bigger than a long, so there is no need to
391 		 * allocate the extra byte. */
392 		messagebuffer = safe_emalloc(ZSTR_LEN(msg_var.s), 1, sizeof(struct php_msgbuf));
393 		memcpy(messagebuffer->mtext, ZSTR_VAL(msg_var.s), ZSTR_LEN(msg_var.s) + 1);
394 		message_len = ZSTR_LEN(msg_var.s);
395 		smart_str_free(&msg_var);
396 	} else {
397 		char *p;
398 		switch (Z_TYPE_P(message)) {
399 			case IS_STRING:
400 				p = Z_STRVAL_P(message);
401 				message_len = Z_STRLEN_P(message);
402 				break;
403 			case IS_LONG:
404 				message_len = spprintf(&p, 0, ZEND_LONG_FMT, Z_LVAL_P(message));
405 				break;
406 			case IS_FALSE:
407 				message_len = spprintf(&p, 0, "0");
408 				break;
409 			case IS_TRUE:
410 				message_len = spprintf(&p, 0, "1");
411 				break;
412 			case IS_DOUBLE:
413 				message_len = spprintf(&p, 0, "%F", Z_DVAL_P(message));
414 				break;
415 
416 			default:
417 				zend_argument_type_error(3, "must be of type string|int|float|bool, %s given", zend_zval_type_name(message));
418 				RETURN_THROWS();
419 		}
420 
421 		messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
422 		memcpy(messagebuffer->mtext, p, message_len + 1);
423 
424 		if (Z_TYPE_P(message) != IS_STRING) {
425 			efree(p);
426 		}
427 	}
428 
429 	/* set the message type */
430 	messagebuffer->mtype = msgtype;
431 
432 	result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT);
433 
434 	efree(messagebuffer);
435 
436 	if (result == -1) {
437 		php_error_docref(NULL, E_WARNING, "msgsnd failed: %s", strerror(errno));
438 		if (zerror) {
439 			ZEND_TRY_ASSIGN_REF_LONG(zerror, errno);
440 		}
441 	} else {
442 		RETVAL_TRUE;
443 	}
444 }
445 /* }}} */
446