autoservice.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Russell Bryant <russell@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Automatic channel service routines
00023  *
00024  * \author Mark Spencer <markster@digium.com>
00025  * \author Russell Bryant <russell@digium.com>
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 432834 $")
00035 
00036 #include <sys/time.h>
00037 #include <signal.h>
00038 
00039 #include "asterisk/_private.h" /* prototype for ast_autoservice_init() */
00040 
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/frame.h"
00043 #include "asterisk/sched.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/file.h"
00046 #include "asterisk/translate.h"
00047 #include "asterisk/manager.h"
00048 #include "asterisk/chanvars.h"
00049 #include "asterisk/linkedlists.h"
00050 #include "asterisk/indications.h"
00051 #include "asterisk/lock.h"
00052 #include "asterisk/utils.h"
00053 
00054 #define MAX_AUTOMONS 1500
00055 
00056 struct asent {
00057    struct ast_channel *chan;
00058    /*! This gets incremented each time autoservice gets started on the same
00059     *  channel.  It will ensure that it doesn't actually get stopped until
00060     *  it gets stopped for the last time. */
00061    unsigned int use_count;
00062    unsigned int orig_end_dtmf_flag:1;
00063    unsigned int ignore_frame_types;
00064    /*! Frames go on at the head of deferred_frames, so we have the frames
00065     *  from newest to oldest.  As we put them at the head of the readq, we'll
00066     *  end up with them in the right order for the channel's readq. */
00067    AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
00068    AST_LIST_ENTRY(asent) list;
00069 };
00070 
00071 static AST_LIST_HEAD_STATIC(aslist, asent);
00072 static ast_cond_t as_cond;
00073 
00074 static pthread_t asthread = AST_PTHREADT_NULL;
00075 static volatile int asexit = 0;
00076 
00077 static int as_chan_list_state;
00078 
00079 static void *autoservice_run(void *ign)
00080 {
00081    ast_callid callid = 0;
00082    struct ast_frame hangup_frame = {
00083       .frametype = AST_FRAME_CONTROL,
00084       .subclass.integer = AST_CONTROL_HANGUP,
00085    };
00086 
00087    while (!asexit) {
00088       struct ast_channel *mons[MAX_AUTOMONS];
00089       struct asent *ents[MAX_AUTOMONS];
00090       struct ast_channel *chan;
00091       struct asent *as;
00092       int i, x = 0, ms = 50;
00093       struct ast_frame *f = NULL;
00094       struct ast_frame *defer_frame = NULL;
00095 
00096       AST_LIST_LOCK(&aslist);
00097 
00098       /* At this point, we know that no channels that have been removed are going
00099        * to get used again. */
00100       as_chan_list_state++;
00101 
00102       if (AST_LIST_EMPTY(&aslist)) {
00103          ast_cond_wait(&as_cond, &aslist.lock);
00104       }
00105 
00106       AST_LIST_TRAVERSE(&aslist, as, list) {
00107          if (!ast_check_hangup(as->chan)) {
00108             if (x < MAX_AUTOMONS) {
00109                ents[x] = as;
00110                mons[x++] = as->chan;
00111             } else {
00112                ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
00113             }
00114          }
00115       }
00116 
00117       AST_LIST_UNLOCK(&aslist);
00118 
00119       if (!x) {
00120          /* If we don't sleep, this becomes a busy loop, which causes
00121           * problems when Asterisk runs at a different priority than other
00122           * user processes.  As long as we check for new channels at least
00123           * once every 10ms, we should be fine. */
00124          usleep(10000);
00125          continue;
00126       }
00127 
00128       chan = ast_waitfor_n(mons, x, &ms);
00129       if (!chan) {
00130          continue;
00131       }
00132 
00133       callid = ast_channel_callid(chan);
00134       ast_callid_threadassoc_change(callid);
00135 
00136       f = ast_read(chan);
00137 
00138       if (!f) {
00139          /* No frame means the channel has been hung up.
00140           * A hangup frame needs to be queued here as ast_waitfor() may
00141           * never return again for the condition to be detected outside
00142           * of autoservice.  So, we'll leave a HANGUP queued up so the
00143           * thread in charge of this channel will know. */
00144 
00145          defer_frame = &hangup_frame;
00146       } else if (ast_is_deferrable_frame(f)) {
00147          defer_frame = f;
00148       } else {
00149          /* Can't defer. Discard and continue with next. */
00150          ast_frfree(f);
00151          continue;
00152       }
00153 
00154       for (i = 0; i < x; i++) {
00155          struct ast_frame *dup_f;
00156 
00157          if (mons[i] != chan) {
00158             continue;
00159          }
00160 
00161          if (!f) { /* defer_frame == &hangup_frame */
00162             if ((dup_f = ast_frdup(defer_frame))) {
00163                AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
00164             }
00165          } else {
00166             if ((dup_f = ast_frisolate(defer_frame))) {
00167                AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
00168             }
00169             if (dup_f != defer_frame) {
00170                ast_frfree(defer_frame);
00171             }
00172          }
00173 
00174          break;
00175       }
00176       /* The ast_waitfor_n() call will only read frames from
00177        * the channels' file descriptors. If ast_waitfor_n()
00178        * returns non-NULL, then one of the channels in the
00179        * mons array must have triggered the return. It's
00180        * therefore impossible that we got here while (i >= x).
00181        * If we did, we'd need to ast_frfree(f) if (f). */
00182    }
00183 
00184    ast_callid_threadassoc_change(0);
00185    asthread = AST_PTHREADT_NULL;
00186 
00187    return NULL;
00188 }
00189 
00190 int ast_autoservice_start(struct ast_channel *chan)
00191 {
00192    int res = 0;
00193    struct asent *as;
00194 
00195    AST_LIST_LOCK(&aslist);
00196    AST_LIST_TRAVERSE(&aslist, as, list) {
00197       if (as->chan == chan) {
00198          as->use_count++;
00199          break;
00200       }
00201    }
00202    AST_LIST_UNLOCK(&aslist);
00203 
00204    if (as) {
00205       /* Entry exists, autoservice is already handling this channel */
00206       return 0;
00207    }
00208 
00209    if (!(as = ast_calloc(1, sizeof(*as))))
00210       return -1;
00211 
00212    /* New entry created */
00213    as->chan = chan;
00214    as->use_count = 1;
00215 
00216    ast_channel_lock(chan);
00217    as->orig_end_dtmf_flag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
00218    if (!as->orig_end_dtmf_flag)
00219       ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
00220    ast_channel_unlock(chan);
00221 
00222    AST_LIST_LOCK(&aslist);
00223 
00224    if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
00225       ast_cond_signal(&as_cond);
00226    }
00227 
00228    AST_LIST_INSERT_HEAD(&aslist, as, list);
00229 
00230    if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
00231       if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
00232          ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
00233          /* There will only be a single member in the list at this point,
00234             the one we just added. */
00235          AST_LIST_REMOVE(&aslist, as, list);
00236          ast_free(as);
00237          asthread = AST_PTHREADT_NULL;
00238          res = -1;
00239       } else {
00240          pthread_kill(asthread, SIGURG);
00241       }
00242    }
00243 
00244    AST_LIST_UNLOCK(&aslist);
00245 
00246    return res;
00247 }
00248 
00249 int ast_autoservice_stop(struct ast_channel *chan)
00250 {
00251    int res = -1;
00252    struct asent *as, *removed = NULL;
00253    struct ast_frame *f;
00254    int chan_list_state;
00255 
00256    AST_LIST_LOCK(&aslist);
00257 
00258    /* Save the autoservice channel list state.  We _must_ verify that the channel
00259     * list has been rebuilt before we return.  Because, after we return, the channel
00260     * could get destroyed and we don't want our poor autoservice thread to step on
00261     * it after its gone! */
00262    chan_list_state = as_chan_list_state;
00263 
00264    /* Find the entry, but do not free it because it still can be in the
00265       autoservice thread array */
00266    AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
00267       if (as->chan == chan) {
00268          as->use_count--;
00269          if (as->use_count < 1) {
00270             AST_LIST_REMOVE_CURRENT(list);
00271             removed = as;
00272          }
00273          break;
00274       }
00275    }
00276    AST_LIST_TRAVERSE_SAFE_END;
00277 
00278    if (removed && asthread != AST_PTHREADT_NULL) {
00279       pthread_kill(asthread, SIGURG);
00280    }
00281 
00282    AST_LIST_UNLOCK(&aslist);
00283 
00284    if (!removed) {
00285       return 0;
00286    }
00287 
00288    /* Wait while autoservice thread rebuilds its list. */
00289    while (chan_list_state == as_chan_list_state) {
00290       usleep(1000);
00291    }
00292 
00293    /* Now autoservice thread should have no references to our entry
00294       and we can safely destroy it */
00295 
00296    if (!ast_channel_softhangup_internal_flag(chan)) {
00297       res = 0;
00298    }
00299 
00300    if (!as->orig_end_dtmf_flag) {
00301       ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
00302    }
00303 
00304    ast_channel_lock(chan);
00305    while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
00306       if (!((1 << f->frametype) & as->ignore_frame_types)) {
00307          ast_queue_frame_head(chan, f);
00308       }
00309       ast_frfree(f);
00310    }
00311    ast_channel_unlock(chan);
00312 
00313    ast_free(as);
00314 
00315    return res;
00316 }
00317 
00318 void ast_autoservice_chan_hangup_peer(struct ast_channel *chan, struct ast_channel *peer)
00319 {
00320    if (chan && !ast_autoservice_start(chan)) {
00321       ast_hangup(peer);
00322       ast_autoservice_stop(chan);
00323    } else {
00324       ast_hangup(peer);
00325    }
00326 }
00327 
00328 int ast_autoservice_ignore(struct ast_channel *chan, enum ast_frame_type ftype)
00329 {
00330    struct asent *as;
00331    int res = -1;
00332 
00333    AST_LIST_LOCK(&aslist);
00334    AST_LIST_TRAVERSE(&aslist, as, list) {
00335       if (as->chan == chan) {
00336          res = 0;
00337          as->ignore_frame_types |= (1 << ftype);
00338          break;
00339       }
00340    }
00341    AST_LIST_UNLOCK(&aslist);
00342    return res;
00343 }
00344 
00345 static void autoservice_shutdown(void)
00346 {
00347    pthread_t th = asthread;
00348    asexit = 1;
00349    if (th != AST_PTHREADT_NULL) {
00350       ast_cond_signal(&as_cond);
00351       pthread_kill(th, SIGURG);
00352       pthread_join(th, NULL);
00353    }
00354 }
00355 
00356 void ast_autoservice_init(void)
00357 {
00358    ast_register_cleanup(autoservice_shutdown);
00359    ast_cond_init(&as_cond, NULL);
00360 }

Generated on Thu Apr 16 06:27:15 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6