autoservice.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Russell Bryant <russell@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Automatic channel service routines
  22. *
  23. * \author Mark Spencer <markster@digium.com>
  24. * \author Russell Bryant <russell@digium.com>
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <sys/time.h>
  32. #include <signal.h>
  33. #include "asterisk/_private.h" /* prototype for ast_autoservice_init() */
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/frame.h"
  36. #include "asterisk/sched.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/file.h"
  39. #include "asterisk/translate.h"
  40. #include "asterisk/manager.h"
  41. #include "asterisk/chanvars.h"
  42. #include "asterisk/linkedlists.h"
  43. #include "asterisk/indications.h"
  44. #include "asterisk/lock.h"
  45. #include "asterisk/utils.h"
  46. #define MAX_AUTOMONS 1500
  47. struct asent {
  48. struct ast_channel *chan;
  49. /*! This gets incremented each time autoservice gets started on the same
  50. * channel. It will ensure that it doesn't actually get stopped until
  51. * it gets stopped for the last time. */
  52. unsigned int use_count;
  53. unsigned int orig_end_dtmf_flag:1;
  54. unsigned int ignore_frame_types;
  55. /*! Frames go on at the head of deferred_frames, so we have the frames
  56. * from newest to oldest. As we put them at the head of the readq, we'll
  57. * end up with them in the right order for the channel's readq. */
  58. AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
  59. AST_LIST_ENTRY(asent) list;
  60. };
  61. static AST_LIST_HEAD_STATIC(aslist, asent);
  62. static ast_cond_t as_cond;
  63. static pthread_t asthread = AST_PTHREADT_NULL;
  64. static int as_chan_list_state;
  65. static void *autoservice_run(void *ign)
  66. {
  67. struct ast_frame hangup_frame = {
  68. .frametype = AST_FRAME_CONTROL,
  69. .subclass.integer = AST_CONTROL_HANGUP,
  70. };
  71. for (;;) {
  72. struct ast_channel *mons[MAX_AUTOMONS];
  73. struct asent *ents[MAX_AUTOMONS];
  74. struct ast_channel *chan;
  75. struct asent *as;
  76. int i, x = 0, ms = 50;
  77. struct ast_frame *f = NULL;
  78. struct ast_frame *defer_frame = NULL;
  79. AST_LIST_LOCK(&aslist);
  80. /* At this point, we know that no channels that have been removed are going
  81. * to get used again. */
  82. as_chan_list_state++;
  83. if (AST_LIST_EMPTY(&aslist)) {
  84. ast_cond_wait(&as_cond, &aslist.lock);
  85. }
  86. AST_LIST_TRAVERSE(&aslist, as, list) {
  87. if (!ast_check_hangup(as->chan)) {
  88. if (x < MAX_AUTOMONS) {
  89. ents[x] = as;
  90. mons[x++] = as->chan;
  91. } else {
  92. ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
  93. }
  94. }
  95. }
  96. AST_LIST_UNLOCK(&aslist);
  97. if (!x) {
  98. /* If we don't sleep, this becomes a busy loop, which causes
  99. * problems when Asterisk runs at a different priority than other
  100. * user processes. As long as we check for new channels at least
  101. * once every 10ms, we should be fine. */
  102. usleep(10000);
  103. continue;
  104. }
  105. chan = ast_waitfor_n(mons, x, &ms);
  106. if (!chan) {
  107. continue;
  108. }
  109. f = ast_read(chan);
  110. if (!f) {
  111. /* No frame means the channel has been hung up.
  112. * A hangup frame needs to be queued here as ast_waitfor() may
  113. * never return again for the condition to be detected outside
  114. * of autoservice. So, we'll leave a HANGUP queued up so the
  115. * thread in charge of this channel will know. */
  116. defer_frame = &hangup_frame;
  117. } else if (ast_is_deferrable_frame(f)) {
  118. defer_frame = f;
  119. }
  120. if (defer_frame) {
  121. for (i = 0; i < x; i++) {
  122. struct ast_frame *dup_f;
  123. if (mons[i] != chan) {
  124. continue;
  125. }
  126. if (defer_frame != f) {
  127. if ((dup_f = ast_frdup(defer_frame))) {
  128. AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
  129. }
  130. } else {
  131. if ((dup_f = ast_frisolate(defer_frame))) {
  132. if (dup_f != defer_frame) {
  133. ast_frfree(defer_frame);
  134. }
  135. AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
  136. }
  137. }
  138. break;
  139. }
  140. } else if (f) {
  141. ast_frfree(f);
  142. }
  143. }
  144. asthread = AST_PTHREADT_NULL;
  145. return NULL;
  146. }
  147. int ast_autoservice_start(struct ast_channel *chan)
  148. {
  149. int res = 0;
  150. struct asent *as;
  151. AST_LIST_LOCK(&aslist);
  152. AST_LIST_TRAVERSE(&aslist, as, list) {
  153. if (as->chan == chan) {
  154. as->use_count++;
  155. break;
  156. }
  157. }
  158. AST_LIST_UNLOCK(&aslist);
  159. if (as) {
  160. /* Entry exists, autoservice is already handling this channel */
  161. return 0;
  162. }
  163. if (!(as = ast_calloc(1, sizeof(*as))))
  164. return -1;
  165. /* New entry created */
  166. as->chan = chan;
  167. as->use_count = 1;
  168. ast_channel_lock(chan);
  169. as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
  170. if (!as->orig_end_dtmf_flag)
  171. ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
  172. ast_channel_unlock(chan);
  173. AST_LIST_LOCK(&aslist);
  174. if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
  175. ast_cond_signal(&as_cond);
  176. }
  177. AST_LIST_INSERT_HEAD(&aslist, as, list);
  178. if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
  179. if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
  180. ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
  181. /* There will only be a single member in the list at this point,
  182. the one we just added. */
  183. AST_LIST_REMOVE(&aslist, as, list);
  184. free(as);
  185. asthread = AST_PTHREADT_NULL;
  186. res = -1;
  187. } else {
  188. pthread_kill(asthread, SIGURG);
  189. }
  190. }
  191. AST_LIST_UNLOCK(&aslist);
  192. return res;
  193. }
  194. int ast_autoservice_stop(struct ast_channel *chan)
  195. {
  196. int res = -1;
  197. struct asent *as, *removed = NULL;
  198. struct ast_frame *f;
  199. int chan_list_state;
  200. AST_LIST_LOCK(&aslist);
  201. /* Save the autoservice channel list state. We _must_ verify that the channel
  202. * list has been rebuilt before we return. Because, after we return, the channel
  203. * could get destroyed and we don't want our poor autoservice thread to step on
  204. * it after its gone! */
  205. chan_list_state = as_chan_list_state;
  206. /* Find the entry, but do not free it because it still can be in the
  207. autoservice thread array */
  208. AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
  209. if (as->chan == chan) {
  210. as->use_count--;
  211. if (as->use_count < 1) {
  212. AST_LIST_REMOVE_CURRENT(list);
  213. removed = as;
  214. }
  215. break;
  216. }
  217. }
  218. AST_LIST_TRAVERSE_SAFE_END;
  219. if (removed && asthread != AST_PTHREADT_NULL) {
  220. pthread_kill(asthread, SIGURG);
  221. }
  222. AST_LIST_UNLOCK(&aslist);
  223. if (!removed) {
  224. return 0;
  225. }
  226. /* Wait while autoservice thread rebuilds its list. */
  227. while (chan_list_state == as_chan_list_state) {
  228. usleep(1000);
  229. }
  230. /* Now autoservice thread should have no references to our entry
  231. and we can safely destroy it */
  232. if (!chan->_softhangup) {
  233. res = 0;
  234. }
  235. if (!as->orig_end_dtmf_flag) {
  236. ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
  237. }
  238. ast_channel_lock(chan);
  239. while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
  240. if (!((1 << f->frametype) & as->ignore_frame_types)) {
  241. ast_queue_frame_head(chan, f);
  242. }
  243. ast_frfree(f);
  244. }
  245. ast_channel_unlock(chan);
  246. free(as);
  247. return res;
  248. }
  249. int ast_autoservice_ignore(struct ast_channel *chan, enum ast_frame_type ftype)
  250. {
  251. struct asent *as;
  252. int res = -1;
  253. AST_LIST_LOCK(&aslist);
  254. AST_LIST_TRAVERSE(&aslist, as, list) {
  255. if (as->chan == chan) {
  256. res = 0;
  257. as->ignore_frame_types |= (1 << ftype);
  258. break;
  259. }
  260. }
  261. AST_LIST_UNLOCK(&aslist);
  262. return res;
  263. }
  264. void ast_autoservice_init(void)
  265. {
  266. ast_cond_init(&as_cond, NULL);
  267. }