Files
asterisk/main/autoservice.c
T

298 lines
7.0 KiB
C
Raw Normal View History

2002-12-30 13:09:27 +00:00
/*
* Asterisk -- An open source telephony toolkit.
2002-12-30 13:09:27 +00:00
*
2008-03-03 16:02:19 +00:00
* Copyright (C) 1999 - 2008, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
2008-03-03 16:02:19 +00:00
* Russell Bryant <russell@digium.com>
2002-12-30 13:09:27 +00:00
*
* 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.
2002-12-30 13:09:27 +00:00
*
* 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
2005-12-30 21:18:06 +00:00
*
* \author Mark Spencer <markster@digium.com>
2008-03-03 16:02:19 +00:00
* \author Russell Bryant <russell@digium.com>
2002-12-30 13:09:27 +00:00
*/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
2002-12-30 13:09:27 +00:00
#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"
2002-12-30 13:09:27 +00:00
2008-01-08 20:56:38 +00:00
#define MAX_AUTOMONS 1500
2002-12-30 13:09:27 +00:00
struct asent {
struct ast_channel *chan;
2007-11-27 22:05:36 +00:00
/*! 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;
2007-12-26 19:09:17 +00:00
unsigned int orig_end_dtmf_flag:1;
2008-12-12 13:55:30 +00:00
/*! 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. */
2008-05-29 22:28:50 +00:00
AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
AST_LIST_ENTRY(asent) list;
2002-12-30 13:09:27 +00:00
};
static AST_LIST_HEAD_STATIC(aslist, asent);
static ast_cond_t as_cond;
static pthread_t asthread = AST_PTHREADT_NULL;
2002-12-30 13:09:27 +00:00
2008-02-29 23:36:46 +00:00
static int as_chan_list_state;
2007-12-07 16:11:00 +00:00
static void *autoservice_run(void *ign)
{
struct ast_frame hangup_frame = {
.frametype = AST_FRAME_CONTROL,
.subclass.integer = AST_CONTROL_HANGUP,
};
for (;;) {
2008-05-29 22:28:50 +00:00
struct ast_channel *mons[MAX_AUTOMONS];
struct asent *ents[MAX_AUTOMONS];
struct ast_channel *chan;
2006-02-15 01:38:20 +00:00
struct asent *as;
2008-05-29 22:28:50 +00:00
int i, x = 0, ms = 50;
struct ast_frame *f = NULL;
struct ast_frame *defer_frame = NULL;
2006-02-15 01:38:20 +00:00
AST_LIST_LOCK(&aslist);
2008-02-29 23:36:46 +00:00
/* At this point, we know that no channels that have been removed are going
* to get used again. */
as_chan_list_state++;
2008-05-29 22:28:50 +00:00
if (AST_LIST_EMPTY(&aslist)) {
ast_cond_wait(&as_cond, &aslist.lock);
2008-05-29 22:28:50 +00:00
}
AST_LIST_TRAVERSE(&aslist, as, list) {
if (!ast_check_hangup(as->chan)) {
2008-05-29 22:28:50 +00:00
if (x < MAX_AUTOMONS) {
ents[x] = as;
2002-12-30 13:09:27 +00:00
mons[x++] = as->chan;
2008-05-29 22:28:50 +00:00
} else {
2002-12-30 13:09:27 +00:00
ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
2008-05-29 22:28:50 +00:00
}
2002-12-30 13:09:27 +00:00
}
}
AST_LIST_UNLOCK(&aslist);
2002-12-30 13:09:27 +00:00
2008-06-13 21:45:21 +00:00
if (!x) {
continue;
}
2008-05-29 22:28:50 +00:00
chan = ast_waitfor_n(mons, x, &ms);
if (!chan) {
continue;
}
2007-12-07 16:11:00 +00:00
2008-05-29 22:28:50 +00:00
f = ast_read(chan);
2008-05-29 22:28:50 +00:00
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;
2010-05-21 16:44:27 +00:00
} else if (ast_is_deferrable_frame(f)) {
defer_frame = f;
2008-05-29 22:28:50 +00:00
}
2007-11-27 22:05:36 +00:00
2008-08-15 15:09:46 +00:00
if (defer_frame) {
2008-05-30 19:47:30 +00:00
for (i = 0; i < x; i++) {
struct ast_frame *dup_f;
if (mons[i] != chan) {
continue;
}
2009-06-16 18:54:30 +00:00
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);
}
2008-05-30 19:47:30 +00:00
}
break;
}
2009-06-16 18:54:30 +00:00
} else if (f) {
2008-05-29 22:28:50 +00:00
ast_frfree(f);
2002-12-30 13:09:27 +00:00
}
}
2007-06-12 14:16:37 +00:00
asthread = AST_PTHREADT_NULL;
2007-06-12 14:16:37 +00:00
2002-12-30 13:09:27 +00:00
return NULL;
}
int ast_autoservice_start(struct ast_channel *chan)
{
2007-12-02 09:42:48 +00:00
int res = 0;
2002-12-30 13:09:27 +00:00
struct asent *as;
AST_LIST_LOCK(&aslist);
AST_LIST_TRAVERSE(&aslist, as, list) {
2007-11-27 22:05:36 +00:00
if (as->chan == chan) {
as->use_count++;
2002-12-30 13:09:27 +00:00
break;
2007-11-27 22:05:36 +00:00
}
2002-12-30 13:09:27 +00:00
}
AST_LIST_UNLOCK(&aslist);
2007-12-02 09:42:48 +00:00
if (as) {
2007-12-26 19:09:17 +00:00
/* Entry exists, autoservice is already handling this channel */
return 0;
2002-12-30 13:09:27 +00:00
}
2007-11-27 22:05:36 +00:00
2007-12-26 19:09:17 +00:00
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);
2008-05-13 21:07:59 +00:00
if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
ast_cond_signal(&as_cond);
2008-05-13 21:07:59 +00:00
}
AST_LIST_INSERT_HEAD(&aslist, as, list);
2007-11-27 22:05:36 +00:00
2007-12-26 19:09:17 +00:00
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);
2007-12-26 19:09:17 +00:00
free(as);
2008-05-13 21:07:59 +00:00
asthread = AST_PTHREADT_NULL;
2007-12-26 19:09:17 +00:00
res = -1;
2008-05-13 21:07:59 +00:00
} else {
2007-12-26 19:09:17 +00:00
pthread_kill(asthread, SIGURG);
2008-05-13 21:07:59 +00:00
}
2007-12-26 19:09:17 +00:00
}
2008-05-13 21:07:59 +00:00
AST_LIST_UNLOCK(&aslist);
2002-12-30 13:09:27 +00:00
return res;
}
int ast_autoservice_stop(struct ast_channel *chan)
{
int res = -1;
2008-05-29 22:28:50 +00:00
struct asent *as, *removed = NULL;
2007-11-27 22:05:36 +00:00
struct ast_frame *f;
2008-02-29 23:36:46 +00:00
int chan_list_state;
2007-11-27 22:05:36 +00:00
AST_LIST_LOCK(&aslist);
2008-02-29 23:36:46 +00:00
/* 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;
2008-05-29 22:28:50 +00:00
/* 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) {
2007-11-27 22:05:36 +00:00
as->use_count--;
2008-05-29 22:28:50 +00:00
if (as->use_count < 1) {
AST_LIST_REMOVE_CURRENT(list);
removed = as;
}
2002-12-30 13:09:27 +00:00
break;
}
2002-12-30 13:09:27 +00:00
}
AST_LIST_TRAVERSE_SAFE_END;
2008-05-29 22:28:50 +00:00
if (removed && asthread != AST_PTHREADT_NULL) {
2002-12-30 13:09:27 +00:00
pthread_kill(asthread, SIGURG);
2008-05-29 22:28:50 +00:00
}
AST_LIST_UNLOCK(&aslist);
2008-05-29 22:28:50 +00:00
if (!removed) {
2007-11-27 22:05:36 +00:00
return 0;
2008-05-29 22:28:50 +00:00
}
/* 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;
}
2007-11-27 22:05:36 +00:00
2008-05-29 22:28:50 +00:00
if (!as->orig_end_dtmf_flag) {
2007-12-26 19:09:17 +00:00
ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
2008-05-29 22:28:50 +00:00
}
2007-12-26 19:09:17 +00:00
2008-12-12 13:55:30 +00:00
ast_channel_lock(chan);
2008-05-29 22:28:50 +00:00
while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
2008-12-12 13:55:30 +00:00
ast_queue_frame_head(chan, f);
2007-11-27 22:05:36 +00:00
ast_frfree(f);
}
2008-12-12 13:55:30 +00:00
ast_channel_unlock(chan);
2007-11-27 22:05:36 +00:00
2008-05-29 22:28:50 +00:00
free(as);
2008-02-29 23:36:46 +00:00
2002-12-30 13:09:27 +00:00
return res;
}
void ast_autoservice_init(void)
{
ast_cond_init(&as_cond, NULL);
}