Asterisk - The Open Source Telephony Project  18.5.0
autoservice.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  * Russell Bryant <[email protected]>
8  *
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.
14  *
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.
18  */
19 
20 /*! \file
21  *
22  * \brief Automatic channel service routines
23  *
24  * \author Mark Spencer <[email protected]>
25  * \author Russell Bryant <[email protected]>
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include <sys/time.h>
35 #include <signal.h>
36 
37 #include "asterisk/_private.h" /* prototype for ast_autoservice_init() */
38 
39 #include "asterisk/pbx.h"
40 #include "asterisk/frame.h"
41 #include "asterisk/sched.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/file.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/chanvars.h"
47 #include "asterisk/linkedlists.h"
48 #include "asterisk/indications.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/utils.h"
51 
52 #define MAX_AUTOMONS 1500
53 
54 struct asent {
55  struct ast_channel *chan;
56  /*! This gets incremented each time autoservice gets started on the same
57  * channel. It will ensure that it doesn't actually get stopped until
58  * it gets stopped for the last time. */
59  unsigned int use_count;
60  unsigned int orig_end_dtmf_flag:1;
61  unsigned int video_update:1;
62  unsigned int ignore_frame_types;
63  /*! Frames go on at the head of deferred_frames, so we have the frames
64  * from newest to oldest. As we put them at the head of the readq, we'll
65  * end up with them in the right order for the channel's readq. */
68 };
69 
72 
73 static pthread_t asthread = AST_PTHREADT_NULL;
74 static volatile int asexit = 0;
75 
76 static int as_chan_list_state;
77 
78 static void *autoservice_run(void *ign)
79 {
80  ast_callid callid = 0;
81  struct ast_frame hangup_frame = {
83  .subclass.integer = AST_CONTROL_HANGUP,
84  };
85 
86  while (!asexit) {
87  struct ast_channel *mons[MAX_AUTOMONS];
88  struct asent *ents[MAX_AUTOMONS];
89  struct ast_channel *chan;
90  struct asent *as;
91  int i, x = 0, ms = 50;
92  struct ast_frame *f = NULL;
93  struct ast_frame *defer_frame = NULL;
94 
96 
97  /* At this point, we know that no channels that have been removed are going
98  * to get used again. */
100 
101  if (AST_LIST_EMPTY(&aslist)) {
103  }
104 
105  AST_LIST_TRAVERSE(&aslist, as, list) {
106  if (!ast_check_hangup(as->chan)) {
107  if (x < MAX_AUTOMONS) {
108  ents[x] = as;
109  mons[x++] = as->chan;
110  } else {
111  ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
112  }
113  }
114  }
115 
117 
118  if (!x) {
119  /* If we don't sleep, this becomes a busy loop, which causes
120  * problems when Asterisk runs at a different priority than other
121  * user processes. As long as we check for new channels at least
122  * once every 10ms, we should be fine. */
123  usleep(10000);
124  continue;
125  }
126 
127  chan = ast_waitfor_n(mons, x, &ms);
128  if (!chan) {
129  continue;
130  }
131 
132  callid = ast_channel_callid(chan);
134 
135  f = ast_read(chan);
136 
137  if (!f) {
138  /* No frame means the channel has been hung up.
139  * A hangup frame needs to be queued here as ast_waitfor() may
140  * never return again for the condition to be detected outside
141  * of autoservice. So, we'll leave a HANGUP queued up so the
142  * thread in charge of this channel will know. */
143 
144  defer_frame = &hangup_frame;
145  } else if (ast_is_deferrable_frame(f)) {
146  defer_frame = f;
147  } else {
148  /* Can't defer. Discard and continue with next. */
149  ast_frfree(f);
150  continue;
151  }
152 
153  for (i = 0; i < x; i++) {
154  struct ast_frame *dup_f;
155 
156  if (mons[i] != chan) {
157  continue;
158  }
159 
160  if (!f) { /* defer_frame == &hangup_frame */
161  if ((dup_f = ast_frdup(defer_frame))) {
163  }
164  } else {
165  if (defer_frame->frametype == AST_FRAME_CONTROL &&
166  defer_frame->subclass.integer == AST_CONTROL_VIDUPDATE) {
167 
168  /* If a video update is already queued don't needlessly queue another */
169  if (ents[i]->video_update) {
170  ast_frfree(defer_frame);
171  break;
172  }
173 
174  ents[i]->video_update = 1;
175  }
176  if ((dup_f = ast_frisolate(defer_frame))) {
178  }
179  if (dup_f != defer_frame) {
180  ast_frfree(defer_frame);
181  }
182  }
183 
184  break;
185  }
186  /* The ast_waitfor_n() call will only read frames from
187  * the channels' file descriptors. If ast_waitfor_n()
188  * returns non-NULL, then one of the channels in the
189  * mons array must have triggered the return. It's
190  * therefore impossible that we got here while (i >= x).
191  * If we did, we'd need to ast_frfree(f) if (f). */
192  }
193 
196 
197  return NULL;
198 }
199 
201 {
202  int res = 0;
203  struct asent *as;
204 
206  /* User interface threads do not handle channel media. */
207  ast_debug(1, "Thread is a user interface, not putting channel %s into autoservice\n",
208  ast_channel_name(chan));
209  return 0;
210  }
211 
213  AST_LIST_TRAVERSE(&aslist, as, list) {
214  if (as->chan == chan) {
215  as->use_count++;
216  break;
217  }
218  }
220 
221  if (as) {
222  /* Entry exists, autoservice is already handling this channel */
223  return 0;
224  }
225 
226  if (!(as = ast_calloc(1, sizeof(*as))))
227  return -1;
228 
229  /* New entry created */
230  as->chan = chan;
231  as->use_count = 1;
232 
233  ast_channel_lock(chan);
235  if (!as->orig_end_dtmf_flag)
237  ast_channel_unlock(chan);
238 
240 
243  }
244 
246 
247  if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
249  ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
250  /* There will only be a single member in the list at this point,
251  the one we just added. */
252  AST_LIST_REMOVE(&aslist, as, list);
253  ast_free(as);
255  res = -1;
256  } else {
257  pthread_kill(asthread, SIGURG);
258  }
259  }
260 
262 
263  return res;
264 }
265 
267 {
268  int res = -1;
269  struct asent *as, *removed = NULL;
270  struct ast_frame *f;
271  int chan_list_state;
272 
274  /* User interface threads do not handle channel media. */
275  ast_debug(1, "Thread is a user interface, not removing channel %s from autoservice\n",
276  ast_channel_name(chan));
277  return 0;
278  }
279 
281 
282  /* Save the autoservice channel list state. We _must_ verify that the channel
283  * list has been rebuilt before we return. Because, after we return, the channel
284  * could get destroyed and we don't want our poor autoservice thread to step on
285  * it after its gone! */
286  chan_list_state = as_chan_list_state;
287 
288  /* Find the entry, but do not free it because it still can be in the
289  autoservice thread array */
291  if (as->chan == chan) {
292  as->use_count--;
293  if (as->use_count < 1) {
295  removed = as;
296  }
297  break;
298  }
299  }
301 
302  if (removed && asthread != AST_PTHREADT_NULL) {
303  pthread_kill(asthread, SIGURG);
304  }
305 
307 
308  if (!removed) {
309  return 0;
310  }
311 
312  /* Wait while autoservice thread rebuilds its list. */
313  while (chan_list_state == as_chan_list_state) {
314  usleep(1000);
315  }
316 
317  /* Now autoservice thread should have no references to our entry
318  and we can safely destroy it */
319 
321  res = 0;
322  }
323 
324  ast_channel_lock(chan);
325  if (!as->orig_end_dtmf_flag) {
327  }
328 
329  while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
330  if (!((1 << f->frametype) & as->ignore_frame_types)) {
331  ast_queue_frame_head(chan, f);
332  }
333  ast_frfree(f);
334  }
335  ast_channel_unlock(chan);
336 
337  ast_free(as);
338 
339  return res;
340 }
341 
343 {
344  if (chan && !ast_autoservice_start(chan)) {
345  ast_hangup(peer);
346  ast_autoservice_stop(chan);
347  } else {
348  ast_hangup(peer);
349  }
350 }
351 
353 {
354  struct asent *as;
355  int res = -1;
356 
358  AST_LIST_TRAVERSE(&aslist, as, list) {
359  if (as->chan == chan) {
360  res = 0;
361  as->ignore_frame_types |= (1 << ftype);
362  break;
363  }
364  }
366  return res;
367 }
368 
369 static void autoservice_shutdown(void)
370 {
371  pthread_t th = asthread;
372  asexit = 1;
373  if (th != AST_PTHREADT_NULL) {
375  pthread_kill(th, SIGURG);
376  pthread_join(th, NULL);
377  }
378 }
379 
381 {
384 }
static volatile int asexit
Definition: autoservice.c:74
struct ast_channel * ast_waitfor_n(struct ast_channel **chan, int n, int *ms)
Waits for input on a group of channels Wait for input on an array of channels for a given # of millis...
Definition: channel.c:3166
Tone Indication Support.
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
#define ast_frdup(fr)
Copies a frame.
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:39
void ast_autoservice_init(void)
Definition: autoservice.c:380
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
unsigned int video_update
Definition: autoservice.c:61
#define ast_test_flag(p, flag)
Definition: utils.h:63
Support for translation of data formats. translate.c.
Channel Variables.
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define LOG_WARNING
Definition: logger.h:274
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:139
#define MAX_AUTOMONS
Definition: autoservice.c:52
int ast_callid_threadassoc_change(ast_callid callid)
Sets what is stored in the thread storage to the given callid if it does not match what is already th...
Definition: logger.c:1971
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4302
void ast_autoservice_chan_hangup_peer(struct ast_channel *chan, struct ast_channel *peer)
Put chan into autoservice while hanging up peer.
Definition: autoservice.c:342
#define ast_cond_wait(cond, mutex)
Definition: lock.h:203
#define ast_cond_init(cond, attr)
Definition: lock.h:199
static void autoservice_shutdown(void)
Definition: autoservice.c:369
unsigned int ast_callid
Definition: logger.h:87
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:449
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
static void * autoservice_run(void *ign)
Definition: autoservice.c:78
#define NULL
Definition: resample.c:96
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:855
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:614
#define ast_cond_signal(cond)
Definition: lock.h:201
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
struct ast_frame_subclass subclass
ast_mutex_t lock
Definition: autoservice.c:70
Utility functions.
static ast_cond_t as_cond
Definition: autoservice.c:71
pthread_cond_t ast_cond_t
Definition: lock.h:176
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:567
int ast_thread_is_user_interface(void)
Indicates whether the current thread is a user interface.
Definition: main/utils.c:2996
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static int as_chan_list_state
Definition: autoservice.c:76
int ast_autoservice_ignore(struct ast_channel *chan, enum ast_frame_type ftype)
Ignore certain frame types.
Definition: autoservice.c:352
General Asterisk PBX channel definitions.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
struct ast_channel * chan
Definition: autoservice.c:55
#define AST_PTHREADT_NULL
Definition: lock.h:66
Scheduler Routines (derived from cheops)
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:556
Asterisk internal frame definitions.
A set of macros to manage forward-linked lists.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:832
AST_LIST_HEAD_NOLOCK(contactliststruct, contact)
Core PBX routines and definitions.
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:445
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:290
ast_frame_type
Frame types.
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
unsigned int orig_end_dtmf_flag
Definition: autoservice.c:60
unsigned int use_count
Definition: autoservice.c:59
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:710
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static pthread_t asthread
Definition: autoservice.c:73
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2548
Prototypes for public functions only of internal interest,.
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
#define ast_clear_flag(p, flag)
Definition: utils.h:77
int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
const char * ast_channel_name(const struct ast_channel *chan)
struct asent::@345 deferred_frames
#define ast_frfree(fr)
int ast_is_deferrable_frame(const struct ast_frame *frame)
Should we keep this frame for later?
Definition: channel.c:1467
Data structure associated with a single frame of data.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
enum ast_frame_type frametype
unsigned int ignore_frame_types
Definition: autoservice.c:62
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to the head of a channel&#39;s frame queue.
Definition: channel.c:1144
struct asent::@346 list
ast_callid ast_channel_callid(const struct ast_channel *chan)