remove the dlfcn compatibility stuff, because no platforms that Asterisk currently...
[asterisk-bristuff.git] / main / autoservice.c
bloba681b0f1d55fad0e1f6d803111a9694abc9ab237
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Russell Bryant <russell@digium.com>
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.
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.
20 /*! \file
22 * \brief Automatic channel service routines
24 * \author Mark Spencer <markster@digium.com>
25 * \author Russell Bryant <russell@digium.com>
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/time.h>
36 #include <signal.h>
37 #include <errno.h>
38 #include <unistd.h>
40 #include "asterisk/pbx.h"
41 #include "asterisk/frame.h"
42 #include "asterisk/sched.h"
43 #include "asterisk/options.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/file.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/manager.h"
49 #include "asterisk/chanvars.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/indications.h"
52 #include "asterisk/lock.h"
53 #include "asterisk/utils.h"
55 #define MAX_AUTOMONS 1500
57 struct asent {
58 struct ast_channel *chan;
59 /*! This gets incremented each time autoservice gets started on the same
60 * channel. It will ensure that it doesn't actually get stopped until
61 * it gets stopped for the last time. */
62 unsigned int use_count;
63 unsigned int orig_end_dtmf_flag:1;
64 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
65 AST_LIST_ENTRY(asent) list;
68 static AST_LIST_HEAD_STATIC(aslist, asent);
69 static ast_cond_t as_cond;
71 static pthread_t asthread = AST_PTHREADT_NULL;
73 static int as_chan_list_state;
75 static void *autoservice_run(void *ign)
77 for (;;) {
78 struct ast_channel *mons[MAX_AUTOMONS];
79 struct asent *ents[MAX_AUTOMONS];
80 struct ast_channel *chan;
81 struct asent *as;
82 int i, x = 0, ms = 50;
83 struct ast_frame *f = NULL;
84 struct ast_frame *defer_frame = NULL;
86 AST_LIST_LOCK(&aslist);
88 /* At this point, we know that no channels that have been removed are going
89 * to get used again. */
90 as_chan_list_state++;
92 if (AST_LIST_EMPTY(&aslist)) {
93 ast_cond_wait(&as_cond, &aslist.lock);
96 AST_LIST_TRAVERSE(&aslist, as, list) {
97 if (!as->chan->_softhangup) {
98 if (x < MAX_AUTOMONS) {
99 ents[x] = as;
100 mons[x++] = as->chan;
101 } else {
102 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
107 AST_LIST_UNLOCK(&aslist);
109 if (!x) {
110 continue;
113 chan = ast_waitfor_n(mons, x, &ms);
114 if (!chan) {
115 continue;
118 f = ast_read(chan);
120 if (!f) {
121 struct ast_frame hangup_frame = { 0, };
122 /* No frame means the channel has been hung up.
123 * A hangup frame needs to be queued here as ast_waitfor() may
124 * never return again for the condition to be detected outside
125 * of autoservice. So, we'll leave a HANGUP queued up so the
126 * thread in charge of this channel will know. */
128 hangup_frame.frametype = AST_FRAME_CONTROL;
129 hangup_frame.subclass = AST_CONTROL_HANGUP;
131 defer_frame = &hangup_frame;
132 } else {
134 /* Do not add a default entry in this switch statement. Each new
135 * frame type should be addressed directly as to whether it should
136 * be queued up or not. */
138 switch (f->frametype) {
139 /* Save these frames */
140 case AST_FRAME_DTMF_END:
141 case AST_FRAME_CONTROL:
142 case AST_FRAME_TEXT:
143 case AST_FRAME_IMAGE:
144 case AST_FRAME_HTML:
145 defer_frame = f;
146 break;
148 /* Throw these frames away */
149 case AST_FRAME_DTMF_BEGIN:
150 case AST_FRAME_VOICE:
151 case AST_FRAME_VIDEO:
152 case AST_FRAME_NULL:
153 case AST_FRAME_IAX:
154 case AST_FRAME_CNG:
155 case AST_FRAME_MODEM:
156 break;
160 if (!defer_frame) {
161 if (f) {
162 ast_frfree(f);
164 continue;
167 if (f) {
168 for (i = 0; i < x; i++) {
169 struct ast_frame *dup_f;
171 if (mons[i] != chan) {
172 continue;
175 if ((dup_f = ast_frdup(f))) {
176 AST_LIST_INSERT_TAIL(&ents[i]->deferred_frames, dup_f, frame_list);
179 break;
181 ast_frfree(f);
185 asthread = AST_PTHREADT_NULL;
187 return NULL;
190 int ast_autoservice_start(struct ast_channel *chan)
192 int res = 0;
193 struct asent *as;
195 /* Check if the channel already has autoservice */
196 AST_LIST_LOCK(&aslist);
197 AST_LIST_TRAVERSE(&aslist, as, list) {
198 if (as->chan == chan) {
199 as->use_count++;
200 break;
203 AST_LIST_UNLOCK(&aslist);
205 if (as) {
206 /* Entry exists, autoservice is already handling this channel */
207 return 0;
210 if (!(as = ast_calloc(1, sizeof(*as))))
211 return -1;
213 /* New entry created */
214 as->chan = chan;
215 as->use_count = 1;
217 ast_channel_lock(chan);
218 as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
219 if (!as->orig_end_dtmf_flag)
220 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
221 ast_channel_unlock(chan);
223 AST_LIST_LOCK(&aslist);
225 if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
226 ast_cond_signal(&as_cond);
229 AST_LIST_INSERT_HEAD(&aslist, as, list);
231 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
232 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
233 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
234 /* There will only be a single member in the list at this point,
235 the one we just added. */
236 AST_LIST_REMOVE(&aslist, as, list);
237 free(as);
238 asthread = AST_PTHREADT_NULL;
239 res = -1;
240 } else {
241 pthread_kill(asthread, SIGURG);
245 AST_LIST_UNLOCK(&aslist);
247 return res;
250 int ast_autoservice_stop(struct ast_channel *chan)
252 int res = -1;
253 struct asent *as, *removed = NULL;
254 struct ast_frame *f;
255 int chan_list_state;
257 AST_LIST_LOCK(&aslist);
259 /* Save the autoservice channel list state. We _must_ verify that the channel
260 * list has been rebuilt before we return. Because, after we return, the channel
261 * could get destroyed and we don't want our poor autoservice thread to step on
262 * it after its gone! */
263 chan_list_state = as_chan_list_state;
265 /* Find the entry, but do not free it because it still can be in the
266 autoservice thread array */
267 AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
268 if (as->chan == chan) {
269 as->use_count--;
270 if (as->use_count < 1) {
271 AST_LIST_REMOVE_CURRENT(&aslist, list);
272 removed = as;
274 break;
277 AST_LIST_TRAVERSE_SAFE_END
279 if (removed && asthread != AST_PTHREADT_NULL) {
280 pthread_kill(asthread, SIGURG);
283 AST_LIST_UNLOCK(&aslist);
285 if (!removed) {
286 return 0;
289 /* Wait while autoservice thread rebuilds its list. */
290 while (chan_list_state == as_chan_list_state) {
291 usleep(1000);
294 /* Now autoservice thread should have no references to our entry
295 and we can safely destroy it */
297 if (!chan->_softhangup) {
298 res = 0;
301 if (!as->orig_end_dtmf_flag) {
302 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
305 while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
306 ast_queue_frame(chan, f);
307 ast_frfree(f);
310 free(as);
312 return res;
315 void ast_autoservice_init(void)
317 ast_cond_init(&as_cond, NULL);