autoservice.c 8.8 KB

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