another bunch of include removals (errno.h and asterisk/logger.h)
[asterisk/asterisk.git] / main / autoservice.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Automatic channel service routines
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <sys/time.h>
31 #include <signal.h>
32
33 #include "asterisk/pbx.h"
34 #include "asterisk/frame.h"
35 #include "asterisk/sched.h"
36 #include "asterisk/options.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/file.h"
39 #include "asterisk/translate.h"
40 #include "asterisk/manager.h"
41 #include "asterisk/chanvars.h"
42 #include "asterisk/linkedlists.h"
43 #include "asterisk/indications.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/utils.h"
46
47 #define MAX_AUTOMONS 256
48
49 struct asent {
50         struct ast_channel *chan;
51         AST_LIST_ENTRY(asent) list;
52 };
53
54 static AST_RWLIST_HEAD_STATIC(aslist, asent);
55
56 static pthread_t asthread = AST_PTHREADT_NULL;
57
58 static void *autoservice_run(void *ign)
59 {
60
61         for (;;) {
62                 struct ast_channel *mons[MAX_AUTOMONS], *chan;
63                 struct asent *as;
64                 int x = 0, ms = 500;
65
66                 AST_RWLIST_RDLOCK(&aslist);
67                 AST_RWLIST_TRAVERSE(&aslist, as, list) {
68                         if (!ast_check_hangup(as->chan)) {
69                                 if (x < MAX_AUTOMONS)
70                                         mons[x++] = as->chan;
71                                 else
72                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
73                         }
74                 }
75                 AST_RWLIST_UNLOCK(&aslist);
76
77                 if ((chan = ast_waitfor_n(mons, x, &ms))) {
78                         /* Read and ignore anything that occurs */
79                         struct ast_frame *f = ast_read(chan);
80                         if (f)
81                                 ast_frfree(f);
82                 }
83         }
84
85         asthread = AST_PTHREADT_NULL;
86
87         return NULL;
88 }
89
90 int ast_autoservice_start(struct ast_channel *chan)
91 {
92         int res = -1;
93         struct asent *as;
94
95         AST_RWLIST_WRLOCK(&aslist);
96
97         /* Check if the channel already has autoservice */
98         AST_RWLIST_TRAVERSE(&aslist, as, list) {
99                 if (as->chan == chan)
100                         break;
101         }
102
103         /* If not, start autoservice on channel */
104         if (!as && (as = ast_calloc(1, sizeof(*as)))) {
105                 as->chan = chan;
106                 AST_RWLIST_INSERT_HEAD(&aslist, as, list);
107                 res = 0;
108                 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
109                         if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
110                                 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
111                                 /* There will only be a single member in the list at this point,
112                                    the one we just added. */
113                                 AST_RWLIST_REMOVE(&aslist, as, list);
114                                 ast_free(as);
115                                 res = -1;
116                         } else
117                                 pthread_kill(asthread, SIGURG);
118                 }
119         }
120         AST_RWLIST_UNLOCK(&aslist);
121         return res;
122 }
123
124 int ast_autoservice_stop(struct ast_channel *chan)
125 {
126         int res = -1;
127         struct asent *as;
128
129         AST_RWLIST_WRLOCK(&aslist);
130         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {     
131                 if (as->chan == chan) {
132                         AST_RWLIST_REMOVE_CURRENT(list);
133                         ast_free(as);
134                         if (!ast_check_hangup(chan))
135                                 res = 0;
136                         break;
137                 }
138         }
139         AST_RWLIST_TRAVERSE_SAFE_END;
140
141         if (asthread != AST_PTHREADT_NULL) 
142                 pthread_kill(asthread, SIGURG);
143         AST_RWLIST_UNLOCK(&aslist);
144
145         /* Wait for it to un-block */
146         while (ast_test_flag(chan, AST_FLAG_BLOCKING))
147                 usleep(1000);
148
149         return res;
150 }