autoservice.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. } else {
  127. /* Can't defer. Discard and continue with next. */
  128. ast_frfree(f);
  129. continue;
  130. }
  131. for (i = 0; i < x; i++) {
  132. struct ast_frame *dup_f;
  133. if (mons[i] != chan) {
  134. continue;
  135. }
  136. if (!f) { /* defer_frame == &hangup_frame */
  137. if ((dup_f = ast_frdup(defer_frame))) {
  138. AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
  139. }
  140. } else {
  141. if ((dup_f = ast_frisolate(defer_frame))) {
  142. AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
  143. }
  144. if (dup_f != defer_frame) {
  145. ast_frfree(defer_frame);
  146. }
  147. }
  148. break;
  149. }
  150. /* The ast_waitfor_n() call will only read frames from
  151. * the channels' file descriptors. If ast_waitfor_n()
  152. * returns non-NULL, then one of the channels in the
  153. * mons array must have triggered the return. It's
  154. * therefore impossible that we got here while (i >= x).
  155. * If we did, we'd need to ast_frfree(f) if (f). */
  156. }
  157. ast_callid_threadassoc_change(NULL);
  158. asthread = AST_PTHREADT_NULL;
  159. return NULL;
  160. }
  161. int ast_autoservice_start(struct ast_channel *chan)
  162. {
  163. int res = 0;
  164. struct asent *as;
  165. if (ast_thread_is_user_interface()) {
  166. /* User interface threads do not handle channel media. */
  167. ast_debug(1, "Thread is a user interface, not putting channel %s into autoservice\n",
  168. ast_channel_name(chan));
  169. return 0;
  170. }
  171. AST_LIST_LOCK(&aslist);
  172. AST_LIST_TRAVERSE(&aslist, as, list) {
  173. if (as->chan == chan) {
  174. as->use_count++;
  175. break;
  176. }
  177. }
  178. AST_LIST_UNLOCK(&aslist);
  179. if (as) {
  180. /* Entry exists, autoservice is already handling this channel */
  181. return 0;
  182. }
  183. if (!(as = ast_calloc(1, sizeof(*as))))
  184. return -1;
  185. /* New entry created */
  186. as->chan = chan;
  187. as->use_count = 1;
  188. ast_channel_lock(chan);
  189. as->orig_end_dtmf_flag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
  190. if (!as->orig_end_dtmf_flag)
  191. ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  192. ast_channel_unlock(chan);
  193. AST_LIST_LOCK(&aslist);
  194. if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
  195. ast_cond_signal(&as_cond);
  196. }
  197. AST_LIST_INSERT_HEAD(&aslist, as, list);
  198. if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
  199. if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
  200. ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
  201. /* There will only be a single member in the list at this point,
  202. the one we just added. */
  203. AST_LIST_REMOVE(&aslist, as, list);
  204. free(as);
  205. asthread = AST_PTHREADT_NULL;
  206. res = -1;
  207. } else {
  208. pthread_kill(asthread, SIGURG);
  209. }
  210. }
  211. AST_LIST_UNLOCK(&aslist);
  212. return res;
  213. }
  214. int ast_autoservice_stop(struct ast_channel *chan)
  215. {
  216. int res = -1;
  217. struct asent *as, *removed = NULL;
  218. struct ast_frame *f;
  219. int chan_list_state;
  220. if (ast_thread_is_user_interface()) {
  221. /* User interface threads do not handle channel media. */
  222. ast_debug(1, "Thread is a user interface, not removing channel %s from autoservice\n",
  223. ast_channel_name(chan));
  224. return 0;
  225. }
  226. AST_LIST_LOCK(&aslist);
  227. /* Save the autoservice channel list state. We _must_ verify that the channel
  228. * list has been rebuilt before we return. Because, after we return, the channel
  229. * could get destroyed and we don't want our poor autoservice thread to step on
  230. * it after its gone! */
  231. chan_list_state = as_chan_list_state;
  232. /* Find the entry, but do not free it because it still can be in the
  233. autoservice thread array */
  234. AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
  235. if (as->chan == chan) {
  236. as->use_count--;
  237. if (as->use_count < 1) {
  238. AST_LIST_REMOVE_CURRENT(list);
  239. removed = as;
  240. }
  241. break;
  242. }
  243. }
  244. AST_LIST_TRAVERSE_SAFE_END;
  245. if (removed && asthread != AST_PTHREADT_NULL) {
  246. pthread_kill(asthread, SIGURG);
  247. }
  248. AST_LIST_UNLOCK(&aslist);
  249. if (!removed) {
  250. return 0;
  251. }
  252. /* Wait while autoservice thread rebuilds its list. */
  253. while (chan_list_state == as_chan_list_state) {
  254. usleep(1000);
  255. }
  256. /* Now autoservice thread should have no references to our entry
  257. and we can safely destroy it */
  258. if (!ast_channel_softhangup_internal_flag(chan)) {
  259. res = 0;
  260. }
  261. ast_channel_lock(chan);
  262. if (!as->orig_end_dtmf_flag) {
  263. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  264. }
  265. while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
  266. if (!((1 << f->frametype) & as->ignore_frame_types)) {
  267. ast_queue_frame_head(chan, f);
  268. }
  269. ast_frfree(f);
  270. }
  271. ast_channel_unlock(chan);
  272. free(as);
  273. return res;
  274. }
  275. void ast_autoservice_chan_hangup_peer(struct ast_channel *chan, struct ast_channel *peer)
  276. {
  277. if (chan && !ast_autoservice_start(chan)) {
  278. ast_hangup(peer);
  279. ast_autoservice_stop(chan);
  280. } else {
  281. ast_hangup(peer);
  282. }
  283. }
  284. int ast_autoservice_ignore(struct ast_channel *chan, enum ast_frame_type ftype)
  285. {
  286. struct asent *as;
  287. int res = -1;
  288. AST_LIST_LOCK(&aslist);
  289. AST_LIST_TRAVERSE(&aslist, as, list) {
  290. if (as->chan == chan) {
  291. res = 0;
  292. as->ignore_frame_types |= (1 << ftype);
  293. break;
  294. }
  295. }
  296. AST_LIST_UNLOCK(&aslist);
  297. return res;
  298. }
  299. static void autoservice_shutdown(void)
  300. {
  301. pthread_t th = asthread;
  302. asexit = 1;
  303. if (th != AST_PTHREADT_NULL) {
  304. ast_cond_signal(&as_cond);
  305. pthread_kill(th, SIGURG);
  306. pthread_join(th, NULL);
  307. }
  308. }
  309. void ast_autoservice_init(void)
  310. {
  311. ast_register_cleanup(autoservice_shutdown);
  312. ast_cond_init(&as_cond, NULL);
  313. }