#include "asterisk.h"
#include <sys/time.h>
#include <signal.h>
#include "asterisk/_private.h"
#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"
Go to the source code of this file.
Data Structures | |
struct | asent |
struct | aslist |
Defines | |
#define | MAX_AUTOMONS 1500 |
Functions | |
void | ast_autoservice_init (void) |
int | ast_autoservice_start (struct ast_channel *chan) |
Automatically service a channel for us... | |
int | ast_autoservice_stop (struct ast_channel *chan) |
Stop servicing a channel for us... | |
static void * | autoservice_run (void *ign) |
Variables | |
static int | as_chan_list_state |
static ast_cond_t | as_cond |
static pthread_t | asthread = AST_PTHREADT_NULL |
Russell Bryant <russell@digium.com>
Definition in file autoservice.c.
#define MAX_AUTOMONS 1500 |
void ast_autoservice_init | ( | void | ) |
Provided by autoservice.c
Definition at line 310 of file autoservice.c.
References as_cond, and ast_cond_init().
Referenced by main().
00311 { 00312 ast_cond_init(&as_cond, NULL); 00313 }
int ast_autoservice_start | ( | struct ast_channel * | chan | ) |
Automatically service a channel for us...
0 | success | |
-1 | failure, or the channel is already being autoserviced |
Definition at line 184 of file autoservice.c.
References as_cond, ast_calloc, ast_channel_lock, ast_channel_unlock, ast_cond_signal(), AST_FLAG_END_DTMF_ONLY, AST_LIST_EMPTY, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_pthread_create_background, AST_PTHREADT_NULL, ast_set_flag, ast_test_flag, asthread, autoservice_run(), asent::chan, free, asent::list, LOG_WARNING, and asent::use_count.
Referenced by _macro_exec(), acf_curl_exec(), acf_odbc_read(), acf_odbc_write(), ast_dtmf_stream(), ast_get_enum(), ast_get_srv(), ast_get_txt(), bridge_playfile(), builtin_atxfer(), builtin_automixmonitor(), builtin_blindtransfer(), conf_play(), dial_exec_full(), feature_exec_app(), function_realtime_read(), function_realtime_readdestroy(), function_realtime_store(), function_realtime_write(), lock_read(), lua_autoservice_start(), lua_get_variable_value(), lua_pbx_exec(), lua_set_variable(), lua_set_variable_value(), osplookup_exec(), pbx_find_extension(), play_message_in_bridged_call(), shell_helper(), sla_station_exec(), smdi_msg_retrieve_read(), system_exec_helper(), try_calling(), and trylock_read().
00185 { 00186 int res = 0; 00187 struct asent *as; 00188 00189 AST_LIST_LOCK(&aslist); 00190 AST_LIST_TRAVERSE(&aslist, as, list) { 00191 if (as->chan == chan) { 00192 as->use_count++; 00193 break; 00194 } 00195 } 00196 AST_LIST_UNLOCK(&aslist); 00197 00198 if (as) { 00199 /* Entry exists, autoservice is already handling this channel */ 00200 return 0; 00201 } 00202 00203 if (!(as = ast_calloc(1, sizeof(*as)))) 00204 return -1; 00205 00206 /* New entry created */ 00207 as->chan = chan; 00208 as->use_count = 1; 00209 00210 ast_channel_lock(chan); 00211 as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0; 00212 if (!as->orig_end_dtmf_flag) 00213 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY); 00214 ast_channel_unlock(chan); 00215 00216 AST_LIST_LOCK(&aslist); 00217 00218 if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) { 00219 ast_cond_signal(&as_cond); 00220 } 00221 00222 AST_LIST_INSERT_HEAD(&aslist, as, list); 00223 00224 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */ 00225 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) { 00226 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n"); 00227 /* There will only be a single member in the list at this point, 00228 the one we just added. */ 00229 AST_LIST_REMOVE(&aslist, as, list); 00230 free(as); 00231 asthread = AST_PTHREADT_NULL; 00232 res = -1; 00233 } else { 00234 pthread_kill(asthread, SIGURG); 00235 } 00236 } 00237 00238 AST_LIST_UNLOCK(&aslist); 00239 00240 return res; 00241 }
int ast_autoservice_stop | ( | struct ast_channel * | chan | ) |
Stop servicing a channel for us...
0 | success | |
-1 | error, or the channel has been hungup |
Definition at line 243 of file autoservice.c.
References ast_channel::_softhangup, as_chan_list_state, ast_channel_lock, ast_channel_unlock, ast_clear_flag, AST_FLAG_END_DTMF_ONLY, ast_frfree, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, AST_PTHREADT_NULL, ast_queue_frame_head(), asthread, asent::chan, asent::deferred_frames, f, ast_frame::frame_list, free, asent::list, asent::orig_end_dtmf_flag, and asent::use_count.
Referenced by _macro_exec(), acf_curl_exec(), acf_odbc_write(), array(), ast_dtmf_stream(), ast_get_srv(), ast_get_txt(), ast_hangup(), bridge_playfile(), builtin_atxfer(), builtin_automixmonitor(), conf_play(), dial_exec_full(), feature_exec_app(), finishup(), function_realtime_read(), function_realtime_readdestroy(), function_realtime_store(), function_realtime_write(), lock_read(), lua_autoservice_stop(), lua_get_variable_value(), lua_pbx_exec(), lua_set_variable(), lua_set_variable_value(), osplookup_exec(), pbx_find_extension(), play_message_in_bridged_call(), shell_helper(), sla_station_exec(), smdi_msg_retrieve_read(), system_exec_helper(), try_calling(), and trylock_read().
00244 { 00245 int res = -1; 00246 struct asent *as, *removed = NULL; 00247 struct ast_frame *f; 00248 int chan_list_state; 00249 00250 AST_LIST_LOCK(&aslist); 00251 00252 /* Save the autoservice channel list state. We _must_ verify that the channel 00253 * list has been rebuilt before we return. Because, after we return, the channel 00254 * could get destroyed and we don't want our poor autoservice thread to step on 00255 * it after its gone! */ 00256 chan_list_state = as_chan_list_state; 00257 00258 /* Find the entry, but do not free it because it still can be in the 00259 autoservice thread array */ 00260 AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) { 00261 if (as->chan == chan) { 00262 as->use_count--; 00263 if (as->use_count < 1) { 00264 AST_LIST_REMOVE_CURRENT(list); 00265 removed = as; 00266 } 00267 break; 00268 } 00269 } 00270 AST_LIST_TRAVERSE_SAFE_END; 00271 00272 if (removed && asthread != AST_PTHREADT_NULL) { 00273 pthread_kill(asthread, SIGURG); 00274 } 00275 00276 AST_LIST_UNLOCK(&aslist); 00277 00278 if (!removed) { 00279 return 0; 00280 } 00281 00282 /* Wait while autoservice thread rebuilds its list. */ 00283 while (chan_list_state == as_chan_list_state) { 00284 usleep(1000); 00285 } 00286 00287 /* Now autoservice thread should have no references to our entry 00288 and we can safely destroy it */ 00289 00290 if (!chan->_softhangup) { 00291 res = 0; 00292 } 00293 00294 if (!as->orig_end_dtmf_flag) { 00295 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY); 00296 } 00297 00298 ast_channel_lock(chan); 00299 while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) { 00300 ast_queue_frame_head(chan, f); 00301 ast_frfree(f); 00302 } 00303 ast_channel_unlock(chan); 00304 00305 free(as); 00306 00307 return res; 00308 }
static void* autoservice_run | ( | void * | ign | ) | [static] |
Definition at line 73 of file autoservice.c.
References ast_check_hangup(), ast_cond_wait(), AST_CONTROL_HANGUP, AST_FRAME_CNG, AST_FRAME_CONTROL, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_FRAME_HTML, AST_FRAME_IAX, AST_FRAME_IMAGE, AST_FRAME_MODEM, AST_FRAME_NULL, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frdup(), ast_frfree, AST_LIST_EMPTY, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_read(), ast_waitfor_n(), asthread, asent::chan, asent::deferred_frames, f, ast_frame::frame_list, ast_frame::frametype, LOG_WARNING, MAX_AUTOMONS, and ast_frame::subclass.
Referenced by ast_autoservice_start().
00074 { 00075 for (;;) { 00076 struct ast_channel *mons[MAX_AUTOMONS]; 00077 struct asent *ents[MAX_AUTOMONS]; 00078 struct ast_channel *chan; 00079 struct asent *as; 00080 int i, x = 0, ms = 50; 00081 struct ast_frame *f = NULL; 00082 struct ast_frame *defer_frame = NULL; 00083 00084 AST_LIST_LOCK(&aslist); 00085 00086 /* At this point, we know that no channels that have been removed are going 00087 * to get used again. */ 00088 as_chan_list_state++; 00089 00090 if (AST_LIST_EMPTY(&aslist)) { 00091 ast_cond_wait(&as_cond, &aslist.lock); 00092 } 00093 00094 AST_LIST_TRAVERSE(&aslist, as, list) { 00095 if (!ast_check_hangup(as->chan)) { 00096 if (x < MAX_AUTOMONS) { 00097 ents[x] = as; 00098 mons[x++] = as->chan; 00099 } else { 00100 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n"); 00101 } 00102 } 00103 } 00104 00105 AST_LIST_UNLOCK(&aslist); 00106 00107 if (!x) { 00108 continue; 00109 } 00110 00111 chan = ast_waitfor_n(mons, x, &ms); 00112 if (!chan) { 00113 continue; 00114 } 00115 00116 f = ast_read(chan); 00117 00118 if (!f) { 00119 struct ast_frame hangup_frame = { 0, }; 00120 /* No frame means the channel has been hung up. 00121 * A hangup frame needs to be queued here as ast_waitfor() may 00122 * never return again for the condition to be detected outside 00123 * of autoservice. So, we'll leave a HANGUP queued up so the 00124 * thread in charge of this channel will know. */ 00125 00126 hangup_frame.frametype = AST_FRAME_CONTROL; 00127 hangup_frame.subclass = AST_CONTROL_HANGUP; 00128 00129 defer_frame = &hangup_frame; 00130 } else { 00131 00132 /* Do not add a default entry in this switch statement. Each new 00133 * frame type should be addressed directly as to whether it should 00134 * be queued up or not. */ 00135 00136 switch (f->frametype) { 00137 /* Save these frames */ 00138 case AST_FRAME_DTMF_END: 00139 case AST_FRAME_CONTROL: 00140 case AST_FRAME_TEXT: 00141 case AST_FRAME_IMAGE: 00142 case AST_FRAME_HTML: 00143 defer_frame = f; 00144 break; 00145 00146 /* Throw these frames away */ 00147 case AST_FRAME_DTMF_BEGIN: 00148 case AST_FRAME_VOICE: 00149 case AST_FRAME_VIDEO: 00150 case AST_FRAME_NULL: 00151 case AST_FRAME_IAX: 00152 case AST_FRAME_CNG: 00153 case AST_FRAME_MODEM: 00154 break; 00155 } 00156 } 00157 00158 if (defer_frame) { 00159 for (i = 0; i < x; i++) { 00160 struct ast_frame *dup_f; 00161 00162 if (mons[i] != chan) { 00163 continue; 00164 } 00165 00166 if ((dup_f = ast_frdup(defer_frame))) { 00167 AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list); 00168 } 00169 00170 break; 00171 } 00172 } 00173 00174 if (f) { 00175 ast_frfree(f); 00176 } 00177 } 00178 00179 asthread = AST_PTHREADT_NULL; 00180 00181 return NULL; 00182 }
int as_chan_list_state [static] |
ast_cond_t as_cond [static] |
Definition at line 67 of file autoservice.c.
Referenced by ast_autoservice_init(), and ast_autoservice_start().
pthread_t asthread = AST_PTHREADT_NULL [static] |
Definition at line 69 of file autoservice.c.
Referenced by ast_autoservice_start(), ast_autoservice_stop(), and autoservice_run().