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