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