autoservice.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <sys/time.h>
  31. #include <signal.h>
  32. #include "asterisk/_private.h" /* prototype for ast_autoservice_init() */
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/frame.h"
  35. #include "asterisk/sched.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/file.h"
  38. #include "asterisk/translate.h"
  39. #include "asterisk/manager.h"
  40. #include "asterisk/chanvars.h"
  41. #include "asterisk/linkedlists.h"
  42. #include "asterisk/indications.h"
  43. #include "asterisk/lock.h"
  44. #include "asterisk/utils.h"
  45. #define MAX_AUTOMONS 1500
  46. struct asent {
  47. struct ast_channel *chan;
  48. /*! This gets incremented each time autoservice gets started on the same
  49. * channel. It will ensure that it doesn't actually get stopped until
  50. * it gets stopped for the last time. */
  51. unsigned int use_count;
  52. unsigned int orig_end_dtmf_flag:1;
  53. unsigned int video_update: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 (defer_frame->frametype == AST_FRAME_CONTROL &&
  139. defer_frame->subclass.integer == AST_CONTROL_VIDUPDATE) {
  140. /* If a video update is already queued don't needlessly queue another */
  141. if (ents[i]->video_update) {
  142. ast_frfree(defer_frame);
  143. break;
  144. }
  145. ents[i]->video_update = 1;
  146. }
  147. if ((dup_f = ast_frisolate(defer_frame))) {
  148. AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
  149. }
  150. if (dup_f != defer_frame) {
  151. ast_frfree(defer_frame);
  152. }
  153. }
  154. break;
  155. }
  156. /* The ast_waitfor_n() call will only read frames from
  157. * the channels' file descriptors. If ast_waitfor_n()
  158. * returns non-NULL, then one of the channels in the
  159. * mons array must have triggered the return. It's
  160. * therefore impossible that we got here while (i >= x).
  161. * If we did, we'd need to ast_frfree(f) if (f). */
  162. }
  163. ast_callid_threadassoc_change(0);
  164. asthread = AST_PTHREADT_NULL;
  165. return NULL;
  166. }
  167. int ast_autoservice_start(struct ast_channel *chan)
  168. {
  169. int res = 0;
  170. struct asent *as;
  171. if (ast_thread_is_user_interface()) {
  172. /* User interface threads do not handle channel media. */
  173. ast_debug(1, "Thread is a user interface, not putting channel %s into autoservice\n",
  174. ast_channel_name(chan));
  175. return 0;
  176. }
  177. AST_LIST_LOCK(&aslist);
  178. AST_LIST_TRAVERSE(&aslist, as, list) {
  179. if (as->chan == chan) {
  180. as->use_count++;
  181. break;
  182. }
  183. }
  184. AST_LIST_UNLOCK(&aslist);
  185. if (as) {
  186. /* Entry exists, autoservice is already handling this channel */
  187. return 0;
  188. }
  189. if (!(as = ast_calloc(1, sizeof(*as))))
  190. return -1;
  191. /* New entry created */
  192. as->chan = chan;
  193. as->use_count = 1;
  194. ast_channel_lock(chan);
  195. as->orig_end_dtmf_flag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
  196. if (!as->orig_end_dtmf_flag)
  197. ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  198. ast_channel_unlock(chan);
  199. AST_LIST_LOCK(&aslist);
  200. if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
  201. ast_cond_signal(&as_cond);
  202. }
  203. AST_LIST_INSERT_HEAD(&aslist, as, list);
  204. if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
  205. if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
  206. ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
  207. /* There will only be a single member in the list at this point,
  208. the one we just added. */
  209. AST_LIST_REMOVE(&aslist, as, list);
  210. ast_free(as);
  211. asthread = AST_PTHREADT_NULL;
  212. res = -1;
  213. } else {
  214. pthread_kill(asthread, SIGURG);
  215. }
  216. }
  217. AST_LIST_UNLOCK(&aslist);
  218. return res;
  219. }
  220. int ast_autoservice_stop(struct ast_channel *chan)
  221. {
  222. int res = -1;
  223. struct asent *as, *removed = NULL;
  224. struct ast_frame *f;
  225. int chan_list_state;
  226. if (ast_thread_is_user_interface()) {
  227. /* User interface threads do not handle channel media. */
  228. ast_debug(1, "Thread is a user interface, not removing channel %s from autoservice\n",
  229. ast_channel_name(chan));
  230. return 0;
  231. }
  232. AST_LIST_LOCK(&aslist);
  233. /* Save the autoservice channel list state. We _must_ verify that the channel
  234. * list has been rebuilt before we return. Because, after we return, the channel
  235. * could get destroyed and we don't want our poor autoservice thread to step on
  236. * it after its gone! */
  237. chan_list_state = as_chan_list_state;
  238. /* Find the entry, but do not free it because it still can be in the
  239. autoservice thread array */
  240. AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
  241. if (as->chan == chan) {
  242. as->use_count--;
  243. if (as->use_count < 1) {
  244. AST_LIST_REMOVE_CURRENT(list);
  245. removed = as;
  246. }
  247. break;
  248. }
  249. }
  250. AST_LIST_TRAVERSE_SAFE_END;
  251. if (removed && asthread != AST_PTHREADT_NULL) {
  252. pthread_kill(asthread, SIGURG);
  253. }
  254. AST_LIST_UNLOCK(&aslist);
  255. if (!removed) {
  256. return 0;
  257. }
  258. if (asthread != AST_PTHREADT_NULL && pthread_equal(pthread_self(), asthread)) {
  259. /* Do not sleep if ast_autoservice_stop is called within the autoservice thread,
  260. otherwise the thread will be stuck in an endless sleep. */
  261. ast_debug(1, "ast_autoservice_stop is called within the autoservice thread, channel %s\n",
  262. ast_channel_name(chan));
  263. } else {
  264. /* Wait while autoservice thread rebuilds its list. */
  265. while (chan_list_state == as_chan_list_state) {
  266. usleep(1000);
  267. }
  268. }
  269. /* Now autoservice thread should have no references to our entry
  270. and we can safely destroy it */
  271. if (!ast_channel_softhangup_internal_flag(chan)) {
  272. res = 0;
  273. }
  274. ast_channel_lock(chan);
  275. if (!as->orig_end_dtmf_flag) {
  276. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  277. }
  278. while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
  279. if (!((1 << f->frametype) & as->ignore_frame_types)) {
  280. ast_queue_frame_head(chan, f);
  281. }
  282. ast_frfree(f);
  283. }
  284. ast_channel_unlock(chan);
  285. ast_free(as);
  286. return res;
  287. }
  288. void ast_autoservice_chan_hangup_peer(struct ast_channel *chan, struct ast_channel *peer)
  289. {
  290. if (chan && !ast_autoservice_start(chan)) {
  291. ast_hangup(peer);
  292. ast_autoservice_stop(chan);
  293. } else {
  294. ast_hangup(peer);
  295. }
  296. }
  297. int ast_autoservice_ignore(struct ast_channel *chan, enum ast_frame_type ftype)
  298. {
  299. struct asent *as;
  300. int res = -1;
  301. AST_LIST_LOCK(&aslist);
  302. AST_LIST_TRAVERSE(&aslist, as, list) {
  303. if (as->chan == chan) {
  304. res = 0;
  305. as->ignore_frame_types |= (1 << ftype);
  306. break;
  307. }
  308. }
  309. AST_LIST_UNLOCK(&aslist);
  310. return res;
  311. }
  312. static void autoservice_shutdown(void)
  313. {
  314. pthread_t th = asthread;
  315. asexit = 1;
  316. if (th != AST_PTHREADT_NULL) {
  317. ast_cond_signal(&as_cond);
  318. pthread_kill(th, SIGURG);
  319. pthread_join(th, NULL);
  320. }
  321. }
  322. void ast_autoservice_init(void)
  323. {
  324. ast_register_cleanup(autoservice_shutdown);
  325. ast_cond_init(&as_cond, NULL);
  326. }