File: autoservice.c

package info (click to toggle)
asterisk 1%3A1.6.2.9-2%2Bsqueeze12
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 67,296 kB
  • ctags: 65,026
  • sloc: ansic: 327,660; sh: 11,153; cpp: 5,940; perl: 3,078; makefile: 2,594; yacc: 2,140; asm: 642; xml: 309; sql: 290; tcl: 113; php: 62
file content (297 lines) | stat: -rw-r--r-- 7,140 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2008, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 * Russell Bryant <russell@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Automatic channel service routines
 *
 * \author Mark Spencer <markster@digium.com> 
 * \author Russell Bryant <russell@digium.com>
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision: 264998 $")

#include <sys/time.h>
#include <signal.h>

#include "asterisk/_private.h" /* prototype for ast_autoservice_init() */

#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/sched.h"
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/translate.h"
#include "asterisk/manager.h"
#include "asterisk/chanvars.h"
#include "asterisk/linkedlists.h"
#include "asterisk/indications.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"

#define MAX_AUTOMONS 1500

struct asent {
	struct ast_channel *chan;
	/*! This gets incremented each time autoservice gets started on the same
	 *  channel.  It will ensure that it doesn't actually get stopped until 
	 *  it gets stopped for the last time. */
	unsigned int use_count;
	unsigned int orig_end_dtmf_flag:1;
	/*! Frames go on at the head of deferred_frames, so we have the frames
	 *  from newest to oldest.  As we put them at the head of the readq, we'll
	 *  end up with them in the right order for the channel's readq. */
	AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
	AST_LIST_ENTRY(asent) list;
};

static AST_LIST_HEAD_STATIC(aslist, asent);
static ast_cond_t as_cond;

static pthread_t asthread = AST_PTHREADT_NULL;

static int as_chan_list_state;

static void *autoservice_run(void *ign)
{
	struct ast_frame hangup_frame = {
		.frametype = AST_FRAME_CONTROL,
		.subclass = AST_CONTROL_HANGUP,
	};

	for (;;) {
		struct ast_channel *mons[MAX_AUTOMONS];
		struct asent *ents[MAX_AUTOMONS];
		struct ast_channel *chan;
		struct asent *as;
		int i, x = 0, ms = 50;
		struct ast_frame *f = NULL;
		struct ast_frame *defer_frame = NULL;

		AST_LIST_LOCK(&aslist);

		/* At this point, we know that no channels that have been removed are going
		 * to get used again. */
		as_chan_list_state++;

		if (AST_LIST_EMPTY(&aslist)) {
			ast_cond_wait(&as_cond, &aslist.lock);
		}

		AST_LIST_TRAVERSE(&aslist, as, list) {
			if (!ast_check_hangup(as->chan)) {
				if (x < MAX_AUTOMONS) {
					ents[x] = as;
					mons[x++] = as->chan;
				} else {
					ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
				}
			}
		}

		AST_LIST_UNLOCK(&aslist);

		if (!x) {
			continue;
		}

		chan = ast_waitfor_n(mons, x, &ms);
		if (!chan) {
			continue;
		}

		f = ast_read(chan);

		if (!f) {
			/* No frame means the channel has been hung up.
			 * A hangup frame needs to be queued here as ast_waitfor() may
			 * never return again for the condition to be detected outside
			 * of autoservice.  So, we'll leave a HANGUP queued up so the
			 * thread in charge of this channel will know. */

			defer_frame = &hangup_frame;
		} else if (ast_is_deferrable_frame(f)) {
			defer_frame = f;
		}

		if (defer_frame) {
			for (i = 0; i < x; i++) {
				struct ast_frame *dup_f;
				
				if (mons[i] != chan) {
					continue;
				}
				
				if (defer_frame != f) {
					if ((dup_f = ast_frdup(defer_frame))) {
						AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
					}
				} else {
					if ((dup_f = ast_frisolate(defer_frame))) {
						if (dup_f != defer_frame) {
							ast_frfree(defer_frame);
						}
						AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
					}
				}
				
				break;
			}
		} else if (f) {
			ast_frfree(f);
		}
	}

	asthread = AST_PTHREADT_NULL;

	return NULL;
}

int ast_autoservice_start(struct ast_channel *chan)
{
	int res = 0;
	struct asent *as;

	AST_LIST_LOCK(&aslist);
	AST_LIST_TRAVERSE(&aslist, as, list) {
		if (as->chan == chan) {
			as->use_count++;
			break;
		}
	}
	AST_LIST_UNLOCK(&aslist);

	if (as) {
		/* Entry exists, autoservice is already handling this channel */
		return 0;
	}

	if (!(as = ast_calloc(1, sizeof(*as))))
		return -1;
	
	/* New entry created */
	as->chan = chan;
	as->use_count = 1;

	ast_channel_lock(chan);
	as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
	if (!as->orig_end_dtmf_flag)
		ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
	ast_channel_unlock(chan);

	AST_LIST_LOCK(&aslist);

	if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
		ast_cond_signal(&as_cond);
	}

	AST_LIST_INSERT_HEAD(&aslist, as, list);

	if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
		if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
			ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
			/* There will only be a single member in the list at this point,
			   the one we just added. */
			AST_LIST_REMOVE(&aslist, as, list);
			free(as);
			asthread = AST_PTHREADT_NULL;
			res = -1;
		} else {
			pthread_kill(asthread, SIGURG);
		}
	}

	AST_LIST_UNLOCK(&aslist);

	return res;
}

int ast_autoservice_stop(struct ast_channel *chan)
{
	int res = -1;
	struct asent *as, *removed = NULL;
	struct ast_frame *f;
	int chan_list_state;

	AST_LIST_LOCK(&aslist);

	/* Save the autoservice channel list state.  We _must_ verify that the channel
	 * list has been rebuilt before we return.  Because, after we return, the channel
	 * could get destroyed and we don't want our poor autoservice thread to step on
	 * it after its gone! */
	chan_list_state = as_chan_list_state;

	/* Find the entry, but do not free it because it still can be in the
	   autoservice thread array */
	AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {	
		if (as->chan == chan) {
			as->use_count--;
			if (as->use_count < 1) {
				AST_LIST_REMOVE_CURRENT(list);
				removed = as;
			}
			break;
		}
	}
	AST_LIST_TRAVERSE_SAFE_END;

	if (removed && asthread != AST_PTHREADT_NULL) {
		pthread_kill(asthread, SIGURG);
	}

	AST_LIST_UNLOCK(&aslist);

	if (!removed) {
		return 0;
	}

	/* Wait while autoservice thread rebuilds its list. */
	while (chan_list_state == as_chan_list_state) {
		usleep(1000);
	}

	/* Now autoservice thread should have no references to our entry
	   and we can safely destroy it */

	if (!chan->_softhangup) {
		res = 0;
	}

	if (!as->orig_end_dtmf_flag) {
		ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
	}

	ast_channel_lock(chan);
	while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
		ast_queue_frame_head(chan, f);
		ast_frfree(f);
	}
	ast_channel_unlock(chan);

	free(as);

	return res;
}

void ast_autoservice_init(void)
{
	ast_cond_init(&as_cond, NULL);
}