PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/php/ext/sysvmsg/sysvmsg.c

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