sysvmsg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_globals.h"
  23. #include "ext/standard/info.h"
  24. #include "php_sysvmsg.h"
  25. #include "ext/standard/php_var.h"
  26. #include "zend_smart_str.h"
  27. /* In order to detect MSG_EXCEPT use at run time; we have no way
  28. * of knowing what the bit definitions are, so we can't just define
  29. * out own MSG_EXCEPT value. */
  30. #define PHP_MSG_IPC_NOWAIT 1
  31. #define PHP_MSG_NOERROR 2
  32. #define PHP_MSG_EXCEPT 4
  33. /* True global resources - no need for thread safety here */
  34. static int le_sysvmsg;
  35. /* {{{ arginfo */
  36. ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_get_queue, 0, 0, 1)
  37. ZEND_ARG_INFO(0, key)
  38. ZEND_ARG_INFO(0, perms)
  39. ZEND_END_ARG_INFO()
  40. ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_send, 0, 0, 3)
  41. ZEND_ARG_INFO(0, queue)
  42. ZEND_ARG_INFO(0, msgtype)
  43. ZEND_ARG_INFO(0, message)
  44. ZEND_ARG_INFO(0, serialize)
  45. ZEND_ARG_INFO(0, blocking)
  46. ZEND_ARG_INFO(1, errorcode)
  47. ZEND_END_ARG_INFO()
  48. ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_receive, 0, 0, 5)
  49. ZEND_ARG_INFO(0, queue)
  50. ZEND_ARG_INFO(0, desiredmsgtype)
  51. ZEND_ARG_INFO(1, msgtype)
  52. ZEND_ARG_INFO(0, maxsize)
  53. ZEND_ARG_INFO(1, message)
  54. ZEND_ARG_INFO(0, unserialize)
  55. ZEND_ARG_INFO(0, flags)
  56. ZEND_ARG_INFO(1, errorcode)
  57. ZEND_END_ARG_INFO()
  58. ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_remove_queue, 0, 0, 1)
  59. ZEND_ARG_INFO(0, queue)
  60. ZEND_END_ARG_INFO()
  61. ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_stat_queue, 0, 0, 1)
  62. ZEND_ARG_INFO(0, queue)
  63. ZEND_END_ARG_INFO()
  64. ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_set_queue, 0, 0, 2)
  65. ZEND_ARG_INFO(0, queue)
  66. ZEND_ARG_INFO(0, data)
  67. ZEND_END_ARG_INFO()
  68. ZEND_BEGIN_ARG_INFO_EX(arginfo_msg_queue_exists, 0, 0, 1)
  69. ZEND_ARG_INFO(0, key)
  70. ZEND_END_ARG_INFO()
  71. /* }}} */
  72. /* {{{ sysvmsg_functions[]
  73. *
  74. * Every user visible function must have an entry in sysvmsg_functions[].
  75. */
  76. static const zend_function_entry sysvmsg_functions[] = {
  77. PHP_FE(msg_get_queue, arginfo_msg_get_queue)
  78. PHP_FE(msg_send, arginfo_msg_send)
  79. PHP_FE(msg_receive, arginfo_msg_receive)
  80. PHP_FE(msg_remove_queue, arginfo_msg_remove_queue)
  81. PHP_FE(msg_stat_queue, arginfo_msg_stat_queue)
  82. PHP_FE(msg_set_queue, arginfo_msg_set_queue)
  83. PHP_FE(msg_queue_exists, arginfo_msg_queue_exists)
  84. PHP_FE_END
  85. };
  86. /* }}} */
  87. /* {{{ sysvmsg_module_entry
  88. */
  89. zend_module_entry sysvmsg_module_entry = {
  90. STANDARD_MODULE_HEADER,
  91. "sysvmsg",
  92. sysvmsg_functions,
  93. PHP_MINIT(sysvmsg),
  94. NULL,
  95. NULL,
  96. NULL,
  97. PHP_MINFO(sysvmsg),
  98. PHP_SYSVMSG_VERSION,
  99. STANDARD_MODULE_PROPERTIES
  100. };
  101. /* }}} */
  102. #ifdef COMPILE_DL_SYSVMSG
  103. ZEND_GET_MODULE(sysvmsg)
  104. #endif
  105. static void sysvmsg_release(zend_resource *rsrc)
  106. {
  107. sysvmsg_queue_t *mq = (sysvmsg_queue_t *) rsrc->ptr;
  108. efree(mq);
  109. }
  110. /* {{{ PHP_MINIT_FUNCTION
  111. */
  112. PHP_MINIT_FUNCTION(sysvmsg)
  113. {
  114. le_sysvmsg = zend_register_list_destructors_ex(sysvmsg_release, NULL, "sysvmsg queue", module_number);
  115. REGISTER_LONG_CONSTANT("MSG_IPC_NOWAIT", PHP_MSG_IPC_NOWAIT, CONST_PERSISTENT|CONST_CS);
  116. REGISTER_LONG_CONSTANT("MSG_EAGAIN", EAGAIN, CONST_PERSISTENT|CONST_CS);
  117. REGISTER_LONG_CONSTANT("MSG_ENOMSG", ENOMSG, CONST_PERSISTENT|CONST_CS);
  118. REGISTER_LONG_CONSTANT("MSG_NOERROR", PHP_MSG_NOERROR, CONST_PERSISTENT|CONST_CS);
  119. REGISTER_LONG_CONSTANT("MSG_EXCEPT", PHP_MSG_EXCEPT, CONST_PERSISTENT|CONST_CS);
  120. return SUCCESS;
  121. }
  122. /* }}} */
  123. /* {{{ PHP_MINFO_FUNCTION
  124. */
  125. PHP_MINFO_FUNCTION(sysvmsg)
  126. {
  127. php_info_print_table_start();
  128. php_info_print_table_row(2, "sysvmsg support", "enabled");
  129. php_info_print_table_end();
  130. }
  131. /* }}} */
  132. /* {{{ proto bool msg_set_queue(resource queue, array data)
  133. Set information for a message queue */
  134. PHP_FUNCTION(msg_set_queue)
  135. {
  136. zval *queue, *data;
  137. sysvmsg_queue_t *mq = NULL;
  138. struct msqid_ds stat;
  139. RETVAL_FALSE;
  140. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra", &queue, &data) == FAILURE) {
  141. return;
  142. }
  143. if ((mq = (sysvmsg_queue_t *)zend_fetch_resource(Z_RES_P(queue), "sysvmsg queue", le_sysvmsg)) == NULL) {
  144. RETURN_FALSE;
  145. }
  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 ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_perm.uid", sizeof("msg_perm.uid") - 1)) != NULL) {
  150. stat.msg_perm.uid = zval_get_long(item);
  151. }
  152. if ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_perm.gid", sizeof("msg_perm.gid") - 1)) != NULL) {
  153. stat.msg_perm.gid = zval_get_long(item);
  154. }
  155. if ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_perm.mode", sizeof("msg_perm.mode") - 1)) != NULL) {
  156. stat.msg_perm.mode = zval_get_long(item);
  157. }
  158. if ((item = zend_hash_str_find(Z_ARRVAL_P(data), "msg_qbytes", sizeof("msg_qbytes") - 1)) != NULL) {
  159. stat.msg_qbytes = zval_get_long(item);
  160. }
  161. if (msgctl(mq->id, IPC_SET, &stat) == 0) {
  162. RETVAL_TRUE;
  163. }
  164. }
  165. }
  166. /* }}} */
  167. /* {{{ proto array msg_stat_queue(resource queue)
  168. Returns information about a message queue */
  169. PHP_FUNCTION(msg_stat_queue)
  170. {
  171. zval *queue;
  172. sysvmsg_queue_t *mq = NULL;
  173. struct msqid_ds stat;
  174. RETVAL_FALSE;
  175. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &queue) == FAILURE) {
  176. return;
  177. }
  178. if ((mq = (sysvmsg_queue_t *)zend_fetch_resource(Z_RES_P(queue), "sysvmsg queue", le_sysvmsg)) == NULL) {
  179. RETURN_FALSE;
  180. }
  181. if (msgctl(mq->id, IPC_STAT, &stat) == 0) {
  182. array_init(return_value);
  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. /* {{{ proto bool msg_queue_exists(int key)
  197. Check whether a message queue exists */
  198. PHP_FUNCTION(msg_queue_exists)
  199. {
  200. zend_long key;
  201. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &key) == FAILURE) {
  202. return;
  203. }
  204. if (msgget(key, 0) < 0) {
  205. RETURN_FALSE;
  206. }
  207. RETURN_TRUE;
  208. }
  209. /* }}} */
  210. /* {{{ proto resource msg_get_queue(int key [, int perms])
  211. Attach to a message queue */
  212. PHP_FUNCTION(msg_get_queue)
  213. {
  214. zend_long key;
  215. zend_long perms = 0666;
  216. sysvmsg_queue_t *mq;
  217. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &key, &perms) == FAILURE) {
  218. return;
  219. }
  220. mq = (sysvmsg_queue_t *) emalloc(sizeof(sysvmsg_queue_t));
  221. mq->key = key;
  222. mq->id = msgget(key, 0);
  223. if (mq->id < 0) {
  224. /* doesn't already exist; create it */
  225. mq->id = msgget(key, IPC_CREAT | IPC_EXCL | perms);
  226. if (mq->id < 0) {
  227. php_error_docref(NULL, E_WARNING, "failed for key 0x" ZEND_XLONG_FMT ": %s", key, strerror(errno));
  228. efree(mq);
  229. RETURN_FALSE;
  230. }
  231. }
  232. ZVAL_COPY_VALUE(return_value, zend_list_insert(mq, le_sysvmsg));
  233. }
  234. /* }}} */
  235. /* {{{ proto bool msg_remove_queue(resource queue)
  236. Destroy the queue */
  237. PHP_FUNCTION(msg_remove_queue)
  238. {
  239. zval *queue;
  240. sysvmsg_queue_t *mq = NULL;
  241. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &queue) == FAILURE) {
  242. return;
  243. }
  244. if ((mq = (sysvmsg_queue_t *)zend_fetch_resource(Z_RES_P(queue), "sysvmsg queue", le_sysvmsg)) == NULL) {
  245. RETURN_FALSE;
  246. }
  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. zend_long desiredmsgtype, maxsize, flags = 0;
  260. zend_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(), "rlz/lz/|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, 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, 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. if ((mq = (sysvmsg_queue_t *)zend_fetch_resource(Z_RES_P(queue), "sysvmsg queue", le_sysvmsg)) == NULL) {
  292. RETURN_FALSE;
  293. }
  294. messagebuffer = (struct php_msgbuf *) safe_emalloc(maxsize, 1, sizeof(struct php_msgbuf));
  295. result = msgrcv(mq->id, messagebuffer, maxsize, desiredmsgtype, realflags);
  296. zval_ptr_dtor(out_msgtype);
  297. zval_ptr_dtor(out_message);
  298. ZVAL_LONG(out_msgtype, 0);
  299. ZVAL_FALSE(out_message);
  300. if (zerrcode) {
  301. zval_ptr_dtor(zerrcode);
  302. ZVAL_LONG(zerrcode, 0);
  303. }
  304. if (result >= 0) {
  305. /* got it! */
  306. ZVAL_LONG(out_msgtype, messagebuffer->mtype);
  307. RETVAL_TRUE;
  308. if (do_unserialize) {
  309. php_unserialize_data_t var_hash;
  310. zval tmp;
  311. const unsigned char *p = (const unsigned char *) messagebuffer->mtext;
  312. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  313. if (!php_var_unserialize(&tmp, &p, p + result, &var_hash)) {
  314. php_error_docref(NULL, E_WARNING, "message corrupted");
  315. RETVAL_FALSE;
  316. } else {
  317. ZVAL_COPY_VALUE(out_message, &tmp);
  318. }
  319. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  320. } else {
  321. ZVAL_STRINGL(out_message, messagebuffer->mtext, result);
  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. zend_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(), "rlz|bbz/",
  342. &queue, &msgtype, &message, &do_serialize, &blocking, &zerror) == FAILURE) {
  343. return;
  344. }
  345. if ((mq = (sysvmsg_queue_t *)zend_fetch_resource(Z_RES_P(queue), "sysvmsg queue", le_sysvmsg)) == NULL) {
  346. RETURN_FALSE;
  347. }
  348. if (do_serialize) {
  349. smart_str msg_var = {0};
  350. php_serialize_data_t var_hash;
  351. PHP_VAR_SERIALIZE_INIT(var_hash);
  352. php_var_serialize(&msg_var, message, &var_hash);
  353. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  354. /* NB: php_msgbuf is 1 char bigger than a long, so there is no need to
  355. * allocate the extra byte. */
  356. messagebuffer = safe_emalloc(ZSTR_LEN(msg_var.s), 1, sizeof(struct php_msgbuf));
  357. memcpy(messagebuffer->mtext, ZSTR_VAL(msg_var.s), ZSTR_LEN(msg_var.s) + 1);
  358. message_len = ZSTR_LEN(msg_var.s);
  359. smart_str_free(&msg_var);
  360. } else {
  361. char *p;
  362. switch (Z_TYPE_P(message)) {
  363. case IS_STRING:
  364. p = Z_STRVAL_P(message);
  365. message_len = Z_STRLEN_P(message);
  366. break;
  367. case IS_LONG:
  368. message_len = spprintf(&p, 0, ZEND_LONG_FMT, Z_LVAL_P(message));
  369. break;
  370. case IS_FALSE:
  371. message_len = spprintf(&p, 0, "0");
  372. break;
  373. case IS_TRUE:
  374. message_len = spprintf(&p, 0, "1");
  375. break;
  376. case IS_DOUBLE:
  377. message_len = spprintf(&p, 0, "%F", Z_DVAL_P(message));
  378. break;
  379. default:
  380. php_error_docref(NULL, E_WARNING, "Message parameter must be either a string or a number.");
  381. RETURN_FALSE;
  382. }
  383. messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
  384. memcpy(messagebuffer->mtext, p, message_len + 1);
  385. if (Z_TYPE_P(message) != IS_STRING) {
  386. efree(p);
  387. }
  388. }
  389. /* set the message type */
  390. messagebuffer->mtype = msgtype;
  391. result = msgsnd(mq->id, messagebuffer, message_len, blocking ? 0 : IPC_NOWAIT);
  392. efree(messagebuffer);
  393. if (result == -1) {
  394. php_error_docref(NULL, E_WARNING, "msgsnd failed: %s", strerror(errno));
  395. if (zerror) {
  396. zval_ptr_dtor(zerror);
  397. ZVAL_LONG(zerror, errno);
  398. }
  399. } else {
  400. RETVAL_TRUE;
  401. }
  402. }
  403. /* }}} */
  404. /*
  405. * Local variables:
  406. * tab-width: 4
  407. * c-basic-offset: 4
  408. * End:
  409. * vim600: noet sw=4 ts=4 tw=78 fdm=marker
  410. * vim<600: noet sw=4 ts=4 tw=78
  411. */