Debian Codegraph @

main/autoservice.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2008, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
7 * Russell Bryant <[email protected]>
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
20 /*! \file
21 *
22 * \brief Automatic channel service routines
23 *
24 * \author Mark Spencer <[email protected]>
25 * \author Russell Bryant <[email protected]>
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include <sys/time.h>
35 #include <signal.h>
36
37 #include "asterisk/_private.h" /* prototype for ast_autoservice_init() */
38
39 #include "asterisk/pbx.h"
40 #include "asterisk/frame.h"
41 #include "asterisk/sched.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/file.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/chanvars.h"
47 #include "asterisk/linkedlists.h"
48 #include "asterisk/indications.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/utils.h"
51
52 #define MAX_AUTOMONS 1500
53
54 struct asent {
55 struct ast_channel *chan;
56 /*! This gets incremented each time autoservice gets started on the same
57 * channel. It will ensure that it doesn't actually get stopped until
58 * it gets stopped for the last time. */
59 unsigned int use_count;
60 unsigned int orig_end_dtmf_flag:1;
61 unsigned int video_update:1;
62 unsigned int ignore_frame_types;
63 /*! Frames go on at the head of deferred_frames, so we have the frames
64 * from newest to oldest. As we put them at the head of the readq, we'll
65 * end up with them in the right order for the channel's readq. */
66 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
67 AST_LIST_ENTRY(asent) list;
68 };
69
70 static AST_LIST_HEAD_STATIC(aslist, asent);
71 static ast_cond_t as_cond;
72
73 static pthread_t asthread = AST_PTHREADT_NULL;
74 static volatile int asexit = 0;
75
76 static int as_chan_list_state;
77
78 static void *autoservice_run(void *ign)
79 {
80 ast_callid callid = 0;
81 struct ast_frame hangup_frame = {
82 .frametype = AST_FRAME_CONTROL,
83 .subclass.integer = AST_CONTROL_HANGUP,
84 };
85
86 while (!asexit) {
87 struct ast_channel *mons[MAX_AUTOMONS];
88 struct asent *ents[MAX_AUTOMONS];
89 struct ast_channel *chan;
90 struct asent *as;
91 int i, x = 0, ms = 50;
92 struct ast_frame *f = NULL;
93 struct ast_frame *defer_frame = NULL;
94
95 AST_LIST_LOCK(&aslist);
96
97 /* At this point, we know that no channels that have been removed are going
98 * to get used again. */
99 as_chan_list_state++;
100
101 if (AST_LIST_EMPTY(&aslist)) {
102 ast_cond_wait(&as_cond, &aslist.lock);
103 }
104
105 AST_LIST_TRAVERSE(&aslist, as, list) {
106 if (!ast_check_hangup(as->chan)) {
107 if (x < MAX_AUTOMONS) {
108 ents[x] = as;
109 mons[x++] = as->chan;
110 } else {
111 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
112 }
113 }
114 }
115
116 AST_LIST_UNLOCK(&aslist);
117
118 if (!x) {
119 /* If we don't sleep, this becomes a busy loop, which causes
120 * problems when Asterisk runs at a different priority than other
121 * user processes. As long as we check for new channels at least
122 * once every 10ms, we should be fine. */
123 usleep(10000);
124 continue;
125 }
126
127 chan = ast_waitfor_n(mons, x, &ms);
128 if (!chan) {
129 continue;
130 }
131
132 callid = ast_channel_callid(chan);
133 ast_callid_threadassoc_change(callid);
134
135 f = ast_read(chan);
136
137 if (!f) {
138 /* No frame means the channel has been hung up.
139 * A hangup frame needs to be queued here as ast_waitfor() may
140 * never return again for the condition to be detected outside
141 * of autoservice. So, we'll leave a HANGUP queued up so the
142 * thread in charge of this channel will know. */
143
144 defer_frame = &hangup_frame;
145 } else if (ast_is_deferrable_frame(f)) {
146 defer_frame = f;
147 } else {
148 /* Can't defer. Discard and continue with next. */
149 ast_frfree(f);
150 continue;
151 }
152
153 for (i = 0; i < x; i++) {
154 struct ast_frame *dup_f;
155
156 if (mons[i] != chan) {
157 continue;
158 }
159
160 if (!f) { /* defer_frame == &hangup_frame */
161 if ((dup_f = ast_frdup(defer_frame))) {
162 AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
163 }
164 } else {
165 if (defer_frame->frametype == AST_FRAME_CONTROL &&
166 defer_frame->subclass.integer == AST_CONTROL_VIDUPDATE) {
167
168 /* If a video update is already queued don't needlessly queue another */
169 if (ents[i]->video_update) {
170 ast_frfree(defer_frame);
171 break;
172 }
173
174 ents[i]->video_update = 1;
175 }
176 if ((dup_f = ast_frisolate(defer_frame))) {
177 AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
178 }
179 if (dup_f != defer_frame) {
180 ast_frfree(defer_frame);
181 }
182 }
183
184 break;
185 }
186 /* The ast_waitfor_n() call will only read frames from
187 * the channels' file descriptors. If ast_waitfor_n()
188 * returns non-NULL, then one of the channels in the
189 * mons array must have triggered the return. It's
190 * therefore impossible that we got here while (i >= x).
191 * If we did, we'd need to ast_frfree(f) if (f). */
192 }
193
194 ast_callid_threadassoc_change(0);
195 asthread = AST_PTHREADT_NULL;
196
197 return NULL;
198 }
199
200 int ast_autoservice_start(struct ast_channel *chan)
201 {
202 int res = 0;
203 struct asent *as;
204
205 if (ast_thread_is_user_interface()) {
206 /* User interface threads do not handle channel media. */
207 ast_debug(1, "Thread is a user interface, not putting channel %s into autoservice\n",
208 ast_channel_name(chan));
209 return 0;
210 }
211
212 AST_LIST_LOCK(&aslist);
213 AST_LIST_TRAVERSE(&aslist, as, list) {
214 if (as->chan == chan) {
215 as->use_count++;
216 break;
217 }
218 }
219 AST_LIST_UNLOCK(&aslist);
220
221 if (as) {
222 /* Entry exists, autoservice is already handling this channel */
223 return 0;
224 }
225
226 if (!(as = ast_calloc(1, sizeof(*as))))
227 return -1;
228
229 /* New entry created */
230 as->chan = chan;
231 as->use_count = 1;
232
233 ast_channel_lock(chan);
234 as->orig_end_dtmf_flag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
235 if (!as->orig_end_dtmf_flag)
236 ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
237 ast_channel_unlock(chan);
238
239 AST_LIST_LOCK(&aslist);
240
241 if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
242 ast_cond_signal(&as_cond);
243 }
244
245 AST_LIST_INSERT_HEAD(&aslist, as, list);
246
247 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
248 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
249 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
250 /* There will only be a single member in the list at this point,
251 the one we just added. */
252 AST_LIST_REMOVE(&aslist, as, list);
253 ast_free(as);
254 asthread = AST_PTHREADT_NULL;
255 res = -1;
256 } else {
257 pthread_kill(asthread, SIGURG);
258 }
259 }
260
261 AST_LIST_UNLOCK(&aslist);
262
263 return res;
264 }
265
266 int ast_autoservice_stop(struct ast_channel *chan)
267 {
268 int res = -1;
269 struct asent *as, *removed = NULL;
270 struct ast_frame *f;
271 int chan_list_state;
272
273 if (ast_thread_is_user_interface()) {
274 /* User interface threads do not handle channel media. */
275 ast_debug(1, "Thread is a user interface, not removing channel %s from autoservice\n",
276 ast_channel_name(chan));
277 return 0;
278 }
279
280 AST_LIST_LOCK(&aslist);
281
282 /* Save the autoservice channel list state. We _must_ verify that the channel
283 * list has been rebuilt before we return. Because, after we return, the channel
284 * could get destroyed and we don't want our poor autoservice thread to step on
285 * it after its gone! */
286 chan_list_state = as_chan_list_state;
287
288 /* Find the entry, but do not free it because it still can be in the
289 autoservice thread array */
290 AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
291 if (as->chan == chan) {
292 as->use_count--;
293 if (as->use_count < 1) {
294 AST_LIST_REMOVE_CURRENT(list);
295 removed = as;
296 }
297 break;
298 }
299 }
300 AST_LIST_TRAVERSE_SAFE_END;
301
302 if (removed && asthread != AST_PTHREADT_NULL) {
303 pthread_kill(asthread, SIGURG);
304 }
305
306 AST_LIST_UNLOCK(&aslist);
307
308 if (!removed) {
309 return 0;
310 }
311
312 if (asthread != AST_PTHREADT_NULL && pthread_equal(pthread_self(), asthread)) {
313 /* Do not sleep if ast_autoservice_stop is called within the autoservice thread,
314 otherwise the thread will be stuck in an endless sleep. */
315 ast_debug(1, "ast_autoservice_stop is called within the autoservice thread, channel %s\n",
316 ast_channel_name(chan));
317 } else {
318 /* Wait while autoservice thread rebuilds its list. */
319 while (chan_list_state == as_chan_list_state) {
320 usleep(1000);
321 }
322 }
323
324 /* Now autoservice thread should have no references to our entry
325 and we can safely destroy it */
326
327 if (!ast_channel_softhangup_internal_flag(chan)) {
328 res = 0;
329 }
330
331 ast_channel_lock(chan);
332 if (!as->orig_end_dtmf_flag) {
333 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
334 }
335
336 while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
337 if (!((1 << f->frametype) & as->ignore_frame_types)) {
338 ast_queue_frame_head(chan, f);
339 }
340 ast_frfree(f);
341 }
342 ast_channel_unlock(chan);
343
344 ast_free(as);
345
346 return res;
347 }
348
349 void ast_autoservice_chan_hangup_peer(struct ast_channel *chan, struct ast_channel *peer)
350 {
351 if (chan && !ast_autoservice_start(chan)) {
352 ast_hangup(peer);
353 ast_autoservice_stop(chan);
354 } else {
355 ast_hangup(peer);
356 }
357 }
358
359 int ast_autoservice_ignore(struct ast_channel *chan, enum ast_frame_type ftype)
360 {
361 struct asent *as;
362 int res = -1;
363
364 AST_LIST_LOCK(&aslist);
365 AST_LIST_TRAVERSE(&aslist, as, list) {
366 if (as->chan == chan) {
367 res = 0;
368 as->ignore_frame_types |= (1 << ftype);
369 break;
370 }
371 }
372 AST_LIST_UNLOCK(&aslist);
373 return res;
374 }
375
376 static void autoservice_shutdown(void)
377 {
378 pthread_t th = asthread;
379 asexit = 1;
380 if (th != AST_PTHREADT_NULL) {
381 ast_cond_signal(&as_cond);
382 pthread_kill(th, SIGURG);
383 pthread_join(th, NULL);
384 }
385 }
386
387 void ast_autoservice_init(void)
388 {
389 ast_register_cleanup(autoservice_shutdown);
390 ast_cond_init(&as_cond, NULL);
391 }