php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
sysvmsg.c
Go to the documentation of this file.
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"
26#include "zend_smart_str.h"
27
28#include <sys/types.h>
29#include <sys/ipc.h>
30#include <sys/msg.h>
31
32PHP_MINIT_FUNCTION(sysvmsg);
33PHP_MINFO_FUNCTION(sysvmsg);
34
40
41struct php_msgbuf {
43 char mtext[1];
44};
45
46/* {{{ sysvmsg_module_entry */
49 "sysvmsg",
50 ext_functions,
51 PHP_MINIT(sysvmsg),
52 NULL,
53 NULL,
54 NULL,
55 PHP_MINFO(sysvmsg),
56 PHP_SYSVMSG_VERSION,
58};
59/* }}} */
60
61#ifdef COMPILE_DL_SYSVMSG
62ZEND_GET_MODULE(sysvmsg)
63#endif
64
65/* SysvMessageQueue class */
66
68static zend_object_handlers sysvmsg_queue_object_handlers;
69
70static 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
76static 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
85static 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
90static 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 */
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 */
120{
122 php_info_print_table_row(2, "sysvmsg support", "enabled");
124}
125/* }}} */
126
127/* {{{ Set information for a message queue */
129{
130 zval *queue, *data;
131 sysvmsg_queue_t *mq = NULL;
132 struct msqid_ds stat;
133
135
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) {
160 }
161 }
162}
163/* }}} */
164
165/* {{{ Returns information about a message queue */
167{
168 zval *queue;
169 sysvmsg_queue_t *mq = NULL;
170 struct msqid_ds stat;
171
173
176 }
177
178 mq = Z_SYSVMSG_QUEUE_P(queue);
179
180 if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
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 */
199{
201
204 }
205
206 if (msgget(key, 0) < 0) {
208 }
209
211}
212/* }}} */
213
214/* {{{ Attach to a message queue */
216{
218 zend_long perms = 0666;
219 sysvmsg_queue_t *mq;
220
221 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &key, &perms) == FAILURE) {
223 }
224
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));
237 }
238 }
239}
240/* }}} */
241
242/* {{{ Destroy the queue */
244{
245 zval *queue;
246 sysvmsg_queue_t *mq = NULL;
247
250 }
251
252 mq = Z_SYSVMSG_QUEUE_P(queue);
253
254 if (msgctl(mq->id, IPC_RMID, NULL) == 0) {
256 } else {
258 }
259}
260/* }}} */
261
262/* {{{ Send a message of type msgtype (must be > 0) to a message queue */
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
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) {
279 }
280
281 if (maxsize <= 0) {
282 zend_argument_value_error(4, "must be greater than 0");
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");
291#else
292 realflags |= MSG_EXCEPT;
293#endif
294 }
295 if (flags & PHP_MSG_NOERROR) {
296 realflags |= MSG_NOERROR;
297 }
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
317 if (do_unserialize) {
319 zval tmp;
320 const unsigned char *p = (const unsigned char *) messagebuffer->mtext;
321
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);
327 } else {
328 ZEND_TRY_ASSIGN_REF_TMP(out_message, &tmp);
329 }
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) {
339 }
340 }
341 efree(messagebuffer);
342}
343/* }}} */
344
345/* {{{ Send a message of type msgtype (must be > 0) to a message queue */
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
357
358 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olz|bbz",
359 &queue, sysvmsg_queue_ce, &msgtype, &message, &do_serialize, &blocking, &zerror) == FAILURE) {
361 }
362
363 mq = Z_SYSVMSG_QUEUE_P(queue);
364
365 if (do_serialize) {
366 smart_str msg_var = {0};
368
370 php_var_serialize(&msg_var, message, &var_hash);
372
373 if (UNEXPECTED(EG(exception))) {
374 smart_str_free(&msg_var);
376 }
377
378
379 zend_string *str = smart_str_extract(&msg_var);
380 message_len = ZSTR_LEN(str);
381 /* NB: php_msgbuf is 1 char bigger than a long, so there is no need to
382 * allocate the extra byte. */
383 messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
384 memcpy(messagebuffer->mtext, ZSTR_VAL(str), message_len + 1);
385 zend_string_release_ex(str, false);
386 smart_str_free(&msg_var);
387 } else {
388 char *p;
389 switch (Z_TYPE_P(message)) {
390 case IS_STRING:
391 p = Z_STRVAL_P(message);
392 message_len = Z_STRLEN_P(message);
393 break;
394 case IS_LONG:
395 message_len = spprintf(&p, 0, ZEND_LONG_FMT, Z_LVAL_P(message));
396 break;
397 case IS_FALSE:
398 message_len = spprintf(&p, 0, "0");
399 break;
400 case IS_TRUE:
401 message_len = spprintf(&p, 0, "1");
402 break;
403 case IS_DOUBLE:
404 message_len = spprintf(&p, 0, "%F", Z_DVAL_P(message));
405 break;
406
407 default:
408 zend_argument_type_error(3, "must be of type string|int|float|bool, %s given", zend_zval_value_name(message));
410 }
411
412 messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
413 memcpy(messagebuffer->mtext, p, message_len + 1);
414
415 if (Z_TYPE_P(message) != IS_STRING) {
416 efree(p);
417 }
418 }
419
420 /* set the message type */
421 messagebuffer->mtype = msgtype;
422
423 result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT);
424
425 efree(messagebuffer);
426
427 if (result == -1) {
428 php_error_docref(NULL, E_WARNING, "msgsnd failed: %s", strerror(errno));
429 if (zerror) {
431 }
432 } else {
434 }
435}
436/* }}} */
bool exception
Definition assert.c:30
stat(string $filename)
memcpy(ptr1, ptr2, size)
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
int key_t
Definition ipc.h:26
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_MINIT
Definition php.h:392
unsigned char key[REFLECTION_KEY_LEN]
#define PHP_MSG_EXCEPT
Definition php_sysvmsg.h:35
#define PHP_MSG_NOERROR
Definition php_sysvmsg.h:34
#define PHP_MSG_IPC_NOWAIT
Definition php_sysvmsg.h:33
#define PHP_VAR_UNSERIALIZE_DESTROY(d)
Definition php_var.h:59
struct php_unserialize_data * php_unserialize_data_t
Definition php_var.h:32
struct php_serialize_data * php_serialize_data_t
Definition php_var.h:31
#define PHP_VAR_UNSERIALIZE_INIT(d)
Definition php_var.h:56
PHPAPI int php_var_unserialize(zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash)
#define PHP_VAR_SERIALIZE_INIT(d)
Definition php_var.h:50
PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data)
Definition var.c:1319
#define PHP_VAR_SERIALIZE_DESTROY(d)
Definition php_var.h:53
zend_constant * data
p
Definition session.c:1105
php_unserialize_data_t var_hash
Definition session.c:964
#define spprintf
Definition spprintf.h:29
char mtext[1]
Definition sysvmsg.c:43
zend_long mtype
Definition sysvmsg.c:42
zend_long id
Definition sysvmsg.c:37
zend_object std
Definition sysvmsg.c:38
#define Z_SYSVMSG_QUEUE_P(zv)
Definition sysvmsg.c:74
zend_module_entry sysvmsg_module_entry
Definition sysvmsg.c:47
zend_class_entry * sysvmsg_queue_ce
Definition sysvmsg.c:67
msg_get_queue(int $key, int $permissions=0666)
msg_queue_exists(int $key)
const MSG_NOERROR
msg_set_queue(SysvMessageQueue $queue, array $data)
msg_stat_queue(SysvMessageQueue $queue)
msg_remove_queue(SysvMessageQueue $queue)
const MSG_EXCEPT
msg_send(SysvMessageQueue $queue, int $message_type, $message, bool $serialize=true, bool $blocking=true, &$error_code=null)
msg_receive(SysvMessageQueue $queue, int $desired_message_type, &$received_message_type, int $max_message_size, mixed &$message, bool $unserialize=true, int $flags=0, &$error_code=null)
#define errno
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API const char * zend_zval_value_name(const zval *arg)
Definition zend_API.c:148
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type)
Definition zend_API.c:1849
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:423
#define ZEND_TRY_ASSIGN_REF_FALSE(zv)
Definition zend_API.h:1139
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define ZEND_TRY_ASSIGN_REF_STRINGL(zv, string, len)
Definition zend_API.h:1337
#define RETURN_FALSE
Definition zend_API.h:1058
#define ZEND_TRY_ASSIGN_REF_TMP(zv, other_zv)
Definition zend_API.h:1403
#define ZEND_GET_MODULE(name)
Definition zend_API.h:241
#define ZEND_TRY_ASSIGN_REF_LONG(zv, lval)
Definition zend_API.h:1205
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETVAL_TRUE
Definition zend_API.h:1033
#define RETVAL_FALSE
Definition zend_API.h:1032
#define RETURN_TRUE
Definition zend_API.h:1059
#define array_init(arg)
Definition zend_API.h:537
#define efree(ptr)
Definition zend_alloc.h:155
#define safe_emalloc(nmemb, size, offset)
Definition zend_alloc.h:154
struct _zval_struct zval
zend_string_release_ex(func->internal_function.function_name, 0)
#define E_WARNING
Definition zend_errors.h:24
union _zend_function zend_function
#define EG(v)
ZEND_API zval *ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:2689
int32_t zend_long
Definition zend_long.h:42
#define ZEND_LONG_FMT
Definition zend_long.h:87
#define ZEND_XLONG_FMT
Definition zend_long.h:89
struct _zend_string zend_string
#define STANDARD_MODULE_HEADER
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES
ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
#define XtOffsetOf(s_type, field)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_TRUE
Definition zend_types.h:603
#define IS_FALSE
Definition zend_types.h:602
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define IS_STRING
Definition zend_types.h:606
#define IS_DOUBLE
Definition zend_types.h:605
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
@ FAILURE
Definition zend_types.h:61
#define IS_LONG
Definition zend_types.h:604
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
#define Z_DVAL_P(zval_p)
Definition zend_types.h:969
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval * return_value
bool result