autoservice.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 volatile int asexit = 0;
  65. static int as_chan_list_state;
  66. static void *autoservice_run(void *ign)
  67. {
  68. struct ast_callid *callid = NULL;
  69. struct ast_frame hangup_frame = {
  70. .frametype = AST_FRAME_CONTROL,
  71. .subclass.integer = AST_CONTROL_HANGUP,
  72. };
  73. while (!asexit) {
  74. struct ast_channel *mons[MAX_AUTOMONS];
  75. struct asent *ents[MAX_AUTOMONS];
  76. struct ast_channel *chan;
  77. struct asent *as;
  78. int i, x = 0, ms = 50;
  79. struct ast_frame *f = NULL;
  80. struct ast_frame *defer_frame = NULL;
  81. AST_LIST_LOCK(&aslist);
  82. /* At this point, we know that no channels that have been removed are going
  83. * to get used again. */
  84. as_chan_list_state++;
  85. if (AST_LIST_EMPTY(&aslist)) {
  86. ast_cond_wait(&as_cond, &aslist.lock);
  87. }
  88. AST_LIST_TRAVERSE(&aslist, as, list) {
  89. if (!ast_check_hangup(as->chan)) {
  90. if (x < MAX_AUTOMONS) {
  91. ents[x] = as;
  92. mons[x++] = as->chan;
  93. } else {
  94. ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
  95. }
  96. }
  97. }
  98. AST_LIST_UNLOCK(&aslist);
  99. if (!x) {
  100. /* If we don't sleep, this becomes a busy loop, which causes
  101. * problems when Asterisk runs at a different priority than other
  102. * user processes. As long as we check for new channels at least
  103. * once every 10ms, we should be fine. */
  104. usleep(10000);
  105. continue;
  106. }
  107. chan = ast_waitfor_n(mons, x, &ms);
  108. if (!chan) {
  109. continue;
  110. }
  111. callid = ast_channel_callid(chan);
  112. ast_callid_threadassoc_change(callid);
  113. if (callid) {
  114. callid = ast_callid_unref(callid);
  115. }
  116. f = ast_read(chan);
  117. if (!f) {
  118. /* No frame means the channel has been hung up.
  119. * A hangup frame needs to be queued here as ast_waitfor() may
  120. * never return again for the condition to be detected outside
  121. * of autoservice. So, we'll leave a HANGUP queued up so the
  122. * thread in charge of this channel will know. */
  123. defer_frame = &hangup_frame;
  124. } else if (ast_is_deferrable_frame(f)) {
  125. defer_frame = f;
  126. }
  127. if (defer_frame) {
  128. for (i = 0; i < x; i++) {
  129. struct ast_frame *dup_f;
  130. if (mons[i] != chan) {
  131. continue;
  132. }
  133. if (defer_frame != f) {
  134. if ((dup_f = ast_frdup(defer_frame))) {
  135. AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
  136. }
  137. } else {
  138. if ((dup_f = ast_frisolate(defer_frame))) {
  139. if (dup_f != defer_frame) {
  140. ast_frfree(defer_frame);
  141. }
  142. AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
  143. }
  144. }
  145. break;
  146. }
  147. } else if (f) {
  148. ast_frfree(f);
  149. }
  150. }
  151. ast_callid_threadassoc_change(NULL);
  152. asthread = AST_PTHREADT_NULL;
  153. return NULL;
  154. }
  155. int ast_autoservice_start(struct ast_channel *chan)
  156. {
  157. int res = 0;
  158. struct asent *as;
  159. AST_LIST_LOCK(&aslist);
  160. AST_LIST_TRAVERSE(&aslist, as, list) {
  161. if (as->chan == chan) {
  162. as->use_count++;
  163. break;
  164. }
  165. }
  166. AST_LIST_UNLOCK(&aslist);
  167. if (as) {
  168. /* Entry exists, autoservice is already handling this channel */
  169. return 0;
  170. }
  171. if (!(as = ast_calloc(1, sizeof(*as))))
  172. return -1;
  173. /* New entry created */
  174. as->chan = chan;
  175. as->use_count = 1;
  176. ast_channel_lock(chan);
  177. as->orig_end_dtmf_flag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
  178. if (!as->orig_end_dtmf_flag)
  179. ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  180. ast_channel_unlock(chan);
  181. AST_LIST_LOCK(&aslist);
  182. if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
  183. ast_cond_signal(&as_cond);
  184. }
  185. AST_LIST_INSERT_HEAD(&aslist, as, list);
  186. if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
  187. if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
  188. ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
  189. /* There will only be a single member in the list at this point,
  190. the one we just added. */
  191. AST_LIST_REMOVE(&aslist, as, list);
  192. free(as);
  193. asthread = AST_PTHREADT_NULL;
  194. res = -1;
  195. } else {
  196. pthread_kill(asthread, SIGURG);
  197. }
  198. }
  199. AST_LIST_UNLOCK(&aslist);
  200. return res;
  201. }
  202. int ast_autoservice_stop(struct ast_channel *chan)
  203. {
  204. int res = -1;
  205. struct asent *as, *removed = NULL;
  206. struct ast_frame *f;
  207. int chan_list_state;
  208. AST_LIST_LOCK(&aslist);
  209. /* Save the autoservice channel list state. We _must_ verify that the channel
  210. * list has been rebuilt before we return. Because, after we return, the channel
  211. * could get destroyed and we don't want our poor autoservice thread to step on
  212. * it after its gone! */
  213. chan_list_state = as_chan_list_state;
  214. /* Find the entry, but do not free it because it still can be in the
  215. autoservice thread array */
  216. AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
  217. if (as->chan == chan) {
  218. as->use_count--;
  219. if (as->use_count < 1) {
  220. AST_LIST_REMOVE_CURRENT(list);
  221. removed = as;
  222. }
  223. break;
  224. }
  225. }
  226. AST_LIST_TRAVERSE_SAFE_END;
  227. if (removed && asthread != AST_PTHREADT_NULL) {
  228. pthread_kill(asthread, SIGURG);
  229. }
  230. AST_LIST_UNLOCK(&aslist);
  231. if (!removed) {
  232. return 0;
  233. }
  234. /* Wait while autoservice thread rebuilds its list. */
  235. while (chan_list_state == as_chan_list_state) {
  236. usleep(1000);
  237. }
  238. /* Now autoservice thread should have no references to our entry
  239. and we can safely destroy it */
  240. if (!ast_channel_softhangup_internal_flag(chan)) {
  241. res = 0;
  242. }
  243. if (!as->orig_end_dtmf_flag) {
  244. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  245. }
  246. ast_channel_lock(chan);
  247. while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
  248. if (!((1 << f->frametype) & as->ignore_frame_types)) {
  249. ast_queue_frame_head(chan, f);
  250. }
  251. ast_frfree(f);
  252. }
  253. ast_channel_unlock(chan);
  254. free(as);
  255. return res;
  256. }
  257. void ast_autoservice_chan_hangup_peer(struct ast_channel *chan, struct ast_channel *peer)
  258. {
  259. if (chan && !ast_autoservice_start(chan)) {
  260. ast_hangup(peer);
  261. ast_autoservice_stop(chan);
  262. } else {
  263. ast_hangup(peer);
  264. }
  265. }
  266. int ast_autoservice_ignore(struct ast_channel *chan, enum ast_frame_type ftype)
  267. {
  268. struct asent *as;
  269. int res = -1;
  270. AST_LIST_LOCK(&aslist);
  271. AST_LIST_TRAVERSE(&aslist, as, list) {
  272. if (as->chan == chan) {
  273. res = 0;
  274. as->ignore_frame_types |= (1 << ftype);
  275. break;
  276. }
  277. }
  278. AST_LIST_UNLOCK(&aslist);
  279. return res;
  280. }
  281. static void autoservice_shutdown(void)
  282. {
  283. pthread_t th = asthread;
  284. asexit = 1;
  285. if (th != AST_PTHREADT_NULL) {
  286. ast_cond_signal(&as_cond);
  287. pthread_kill(th, SIGURG);
  288. pthread_join(th, NULL);
  289. }
  290. }
  291. void ast_autoservice_init(void)
  292. {
  293. ast_register_cleanup(autoservice_shutdown);
  294. ast_cond_init(&as_cond, NULL);
  295. }