ibase_events.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. | Authors: Ard Biesheuvel <a.k.biesheuvel@its.tudelft.nl> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #if HAVE_IBASE
  23. #include "php_interbase.h"
  24. #include "php_ibase_includes.h"
  25. static int le_event;
  26. static void _php_ibase_event_free(char *event_buf, char *result_buf) /* {{{ */
  27. {
  28. isc_free(event_buf);
  29. isc_free(result_buf);
  30. }
  31. /* }}} */
  32. void _php_ibase_free_event(ibase_event *event) /* {{{ */
  33. {
  34. unsigned short i;
  35. event->state = DEAD;
  36. if (event->link != NULL) {
  37. ibase_event **node;
  38. zend_list_delete(event->link_res);
  39. if (event->link->handle != 0 &&
  40. isc_cancel_events(IB_STATUS, &event->link->handle, &event->event_id)) {
  41. _php_ibase_error();
  42. }
  43. /* delete this event from the link struct */
  44. for (node = &event->link->event_head; *node != event; node = &(*node)->event_next);
  45. *node = event->event_next;
  46. }
  47. if (Z_TYPE(event->callback) != IS_UNDEF) {
  48. zval_ptr_dtor(&event->callback);
  49. ZVAL_UNDEF(&event->callback);
  50. _php_ibase_event_free(event->event_buffer,event->result_buffer);
  51. for (i = 0; i < event->event_count; ++i) {
  52. if (event->events[i]) {
  53. efree(event->events[i]);
  54. }
  55. }
  56. efree(event->events);
  57. }
  58. }
  59. /* }}} */
  60. static void _php_ibase_free_event_rsrc(zend_resource *rsrc) /* {{{ */
  61. {
  62. ibase_event *e = (ibase_event *) rsrc->ptr;
  63. _php_ibase_free_event(e);
  64. efree(e);
  65. }
  66. /* }}} */
  67. void php_ibase_events_minit(INIT_FUNC_ARGS) /* {{{ */
  68. {
  69. le_event = zend_register_list_destructors_ex(_php_ibase_free_event_rsrc, NULL,
  70. "interbase event", module_number);
  71. }
  72. /* }}} */
  73. static void _php_ibase_event_block(ibase_db_link *ib_link, unsigned short count, /* {{{ */
  74. char **events, unsigned short *l, char **event_buf, char **result_buf)
  75. {
  76. ISC_STATUS dummy_result[20];
  77. ISC_ULONG dummy_count[15];
  78. /**
  79. * Unfortunately, there's no clean and portable way in C to pass arguments to
  80. * a variadic function if you don't know the number of arguments at compile time.
  81. * (And even if there were a way, the Interbase API doesn't provide a version of
  82. * this function that takes a va_list as an argument)
  83. *
  84. * In this case, the number of arguments is limited to 18 by the underlying API,
  85. * so we can work around it.
  86. */
  87. *l = (unsigned short) isc_event_block(event_buf, result_buf, count, events[0],
  88. events[1], events[2], events[3], events[4], events[5], events[6], events[7],
  89. events[8], events[9], events[10], events[11], events[12], events[13], events[14]);
  90. /**
  91. * Currently, this is the only way to correctly initialize an event buffer.
  92. * This is clearly something that should be fixed, cause the semantics of
  93. * isc_wait_for_event() indicate that it blocks until an event occurs.
  94. * If the Firebird people ever fix this, these lines should be removed,
  95. * otherwise, events will have to fire twice before ibase_wait_event() returns.
  96. */
  97. isc_wait_for_event(dummy_result, &ib_link->handle, *l, *event_buf, *result_buf);
  98. isc_event_counts(dummy_count, *l, *event_buf, *result_buf);
  99. }
  100. /* }}} */
  101. /* {{{ proto string ibase_wait_event([resource link_identifier,] string event [, string event [, ...]])
  102. Waits for any one of the passed Interbase events to be posted by the database, and returns its name */
  103. PHP_FUNCTION(ibase_wait_event)
  104. {
  105. zval *args;
  106. ibase_db_link *ib_link;
  107. int num_args;
  108. char *event_buffer, *result_buffer, *events[15];
  109. unsigned short i = 0, event_count = 0, buffer_size;
  110. ISC_ULONG occurred_event[15];
  111. RESET_ERRMSG;
  112. /* no more than 15 events */
  113. if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 16) {
  114. WRONG_PARAM_COUNT;
  115. }
  116. if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &num_args) == FAILURE) {
  117. return;
  118. }
  119. if (Z_TYPE(args[0]) == IS_RESOURCE) {
  120. if ((ib_link = (ibase_db_link *)zend_fetch_resource2_ex(&args[0], "InterBase link", le_link, le_plink)) == NULL) {
  121. RETURN_FALSE;
  122. }
  123. i = 1;
  124. } else {
  125. if (ZEND_NUM_ARGS() > 15) {
  126. WRONG_PARAM_COUNT;
  127. }
  128. if ((ib_link = (ibase_db_link *)zend_fetch_resource2(IBG(default_link), "InterBase link", le_link, le_plink)) == NULL) {
  129. RETURN_FALSE;
  130. }
  131. }
  132. for (; i < ZEND_NUM_ARGS(); ++i) {
  133. convert_to_string_ex(&args[i]);
  134. events[event_count++] = Z_STRVAL(args[i]);
  135. }
  136. /* fills the required data structure with information about the events */
  137. _php_ibase_event_block(ib_link, event_count, events, &buffer_size, &event_buffer, &result_buffer);
  138. /* now block until an event occurs */
  139. if (isc_wait_for_event(IB_STATUS, &ib_link->handle, buffer_size, event_buffer, result_buffer)) {
  140. _php_ibase_error();
  141. _php_ibase_event_free(event_buffer,result_buffer);
  142. RETURN_FALSE;
  143. }
  144. /* find out which event occurred */
  145. isc_event_counts(occurred_event, buffer_size, event_buffer, result_buffer);
  146. for (i = 0; i < event_count; ++i) {
  147. if (occurred_event[i]) {
  148. zend_string *result = zend_string_init(events[i], strlen(events[i]), 0);
  149. _php_ibase_event_free(event_buffer,result_buffer);
  150. RETURN_STR(result);
  151. }
  152. }
  153. /* If we reach this line, isc_wait_for_event() did return, but we don't know
  154. which event fired. */
  155. _php_ibase_event_free(event_buffer,result_buffer);
  156. RETURN_FALSE;
  157. }
  158. /* }}} */
  159. #if FB_API_VER >= 20
  160. #define PHP_ISC_CALLBACK ISC_EVENT_CALLBACK
  161. static ISC_EVENT_CALLBACK _php_ibase_callback(ibase_event *event, /* {{{ */
  162. ISC_USHORT buffer_size, ISC_UCHAR *result_buf)
  163. #else
  164. #define PHP_ISC_CALLBACK isc_callback
  165. static isc_callback _php_ibase_callback(ibase_event *event, /* {{{ */
  166. unsigned short buffer_size, char *result_buf)
  167. #endif
  168. {
  169. /* this function is called asynchronously by the Interbase client library. */
  170. TSRMLS_FETCH_FROM_CTX(event->thread_ctx);
  171. /**
  172. * The callback function is called when the event is first registered and when the event
  173. * is cancelled. I consider this is a bug. By clearing event->callback first and setting
  174. * it to -1 later, we make sure nothing happens if no event was actually posted.
  175. */
  176. switch (event->state) {
  177. unsigned short i;
  178. ISC_ULONG occurred_event[15];
  179. zval return_value, args[2];
  180. default: /* == DEAD */
  181. break;
  182. case ACTIVE:
  183. /* copy the updated results into the result buffer */
  184. memcpy(event->result_buffer, result_buf, buffer_size);
  185. ZVAL_RES(&args[1], event->link_res);
  186. /* find out which event occurred */
  187. isc_event_counts(occurred_event, buffer_size, event->event_buffer, event->result_buffer);
  188. for (i = 0; i < event->event_count; ++i) {
  189. if (occurred_event[i]) {
  190. ZVAL_STRING(&args[0], event->events[i]);
  191. //efree(event->events[i]);
  192. break;
  193. }
  194. }
  195. /* call the callback provided by the user */
  196. if (SUCCESS != call_user_function(EG(function_table), NULL,
  197. &event->callback, &return_value, 2, args)) {
  198. _php_ibase_module_error("Error calling callback %s", Z_STRVAL(event->callback));
  199. break;
  200. }
  201. if (Z_TYPE(return_value) == IS_FALSE) {
  202. event->state = DEAD;
  203. break;
  204. }
  205. case NEW:
  206. /* re-register the event */
  207. if (isc_que_events(IB_STATUS, &event->link->handle, &event->event_id, buffer_size,
  208. event->event_buffer,(PHP_ISC_CALLBACK)_php_ibase_callback, (void *)event)) {
  209. _php_ibase_error();
  210. }
  211. event->state = ACTIVE;
  212. }
  213. return 0;
  214. }
  215. /* }}} */
  216. /* {{{ proto resource ibase_set_event_handler([resource link_identifier,] callback handler, string event [, string event [, ...]])
  217. Register the callback for handling each of the named events */
  218. PHP_FUNCTION(ibase_set_event_handler)
  219. {
  220. /**
  221. * The callback passed to this function should take an event name (string) and a
  222. * link resource id (int) as arguments. The value returned from the function is
  223. * used to determine if the event handler should remain set.
  224. */
  225. zval *args, *cb_arg;
  226. ibase_db_link *ib_link;
  227. ibase_event *event;
  228. unsigned short i = 1, buffer_size;
  229. int num_args;
  230. zend_resource *link_res;
  231. RESET_ERRMSG;
  232. /* Minimum and maximum number of arguments allowed */
  233. if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 17) {
  234. WRONG_PARAM_COUNT;
  235. }
  236. if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &num_args) == FAILURE) {
  237. return;
  238. }
  239. /* get a working link */
  240. if (Z_TYPE(args[0]) != IS_STRING) {
  241. /* resource, callback, event_1 [, ... event_15]
  242. * No more than 15 events
  243. */
  244. if (ZEND_NUM_ARGS() < 3 || ZEND_NUM_ARGS() > 17) {
  245. WRONG_PARAM_COUNT;
  246. }
  247. cb_arg = &args[1];
  248. i = 2;
  249. if ((ib_link = (ibase_db_link *)zend_fetch_resource2_ex(&args[0], "InterBase link", le_link, le_plink)) == NULL) {
  250. RETURN_FALSE;
  251. }
  252. link_res = Z_RES(args[0]);
  253. } else {
  254. /* callback, event_1 [, ... event_15]
  255. * No more than 15 events
  256. */
  257. if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 16) {
  258. WRONG_PARAM_COUNT;
  259. }
  260. cb_arg = &args[0];
  261. if ((ib_link = (ibase_db_link *)zend_fetch_resource2(IBG(default_link), "InterBase link", le_link, le_plink)) == NULL) {
  262. RETURN_FALSE;
  263. }
  264. link_res = IBG(default_link);
  265. }
  266. /* get the callback */
  267. if (!zend_is_callable(cb_arg, 0, NULL)) {
  268. zend_string *cb_name = zend_get_callable_name(cb_arg);
  269. _php_ibase_module_error("Callback argument %s is not a callable function", ZSTR_VAL(cb_name));
  270. zend_string_release_ex(cb_name, 0);
  271. RETURN_FALSE;
  272. }
  273. /* allocate the event resource */
  274. event = (ibase_event *) safe_emalloc(sizeof(ibase_event), 1, 0);
  275. TSRMLS_SET_CTX(event->thread_ctx);
  276. event->link_res = link_res;
  277. GC_ADDREF(link_res);
  278. event->link = ib_link;
  279. event->event_count = 0;
  280. event->state = NEW;
  281. event->events = (char **) safe_emalloc(sizeof(char *), 15, 0);
  282. ZVAL_DUP(&event->callback, cb_arg);
  283. for (; i < 15; ++i) {
  284. if (i < ZEND_NUM_ARGS()) {
  285. convert_to_string_ex(&args[i]);
  286. event->events[event->event_count++] = estrdup(Z_STRVAL(args[i]));
  287. } else {
  288. event->events[i] = NULL;
  289. }
  290. }
  291. /* fills the required data structure with information about the events */
  292. _php_ibase_event_block(ib_link, event->event_count, event->events,
  293. &buffer_size, &event->event_buffer, &event->result_buffer);
  294. /* now register the events with the Interbase API */
  295. if (isc_que_events(IB_STATUS, &ib_link->handle, &event->event_id, buffer_size,
  296. event->event_buffer,(PHP_ISC_CALLBACK)_php_ibase_callback, (void *)event)) {
  297. _php_ibase_error();
  298. efree(event);
  299. RETURN_FALSE;
  300. }
  301. event->event_next = ib_link->event_head;
  302. ib_link->event_head = event;
  303. RETVAL_RES(zend_register_resource(event, le_event));
  304. Z_TRY_ADDREF_P(return_value);
  305. }
  306. /* }}} */
  307. /* {{{ proto bool ibase_free_event_handler(resource event)
  308. Frees the event handler set by ibase_set_event_handler() */
  309. PHP_FUNCTION(ibase_free_event_handler)
  310. {
  311. zval *event_arg;
  312. RESET_ERRMSG;
  313. if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &event_arg)) {
  314. ibase_event *event;
  315. event = (ibase_event *)zend_fetch_resource_ex(event_arg, "Interbase event", le_event);
  316. event->state = DEAD;
  317. zend_list_delete(Z_RES_P(event_arg));
  318. RETURN_TRUE;
  319. } else {
  320. RETURN_FALSE;
  321. }
  322. }
  323. /* }}} */
  324. #endif /* HAVE_IBASE */
  325. /*
  326. * Local variables:
  327. * tab-width: 4
  328. * c-basic-offset: 4
  329. * End:
  330. * vim600: sw=4 ts=4 fdm=marker
  331. * vim<600: sw=4 ts=4
  332. */