silly people that don't want to install/run autoconf :-)
[asterisk/asterisk.git] / 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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include <signal.h>
31 #include <errno.h>
32 #include <unistd.h>
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/pbx.h"
39 #include "asterisk/frame.h"
40 #include "asterisk/sched.h"
41 #include "asterisk/options.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/file.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/manager.h"
47 #include "asterisk/chanvars.h"
48 #include "asterisk/linkedlists.h"
49 #include "asterisk/indications.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/utils.h"
52
53 #define MAX_AUTOMONS 256
54
55 struct asent {
56         struct ast_channel *chan;
57         AST_LIST_ENTRY(asent) list;
58 };
59
60 static AST_LIST_HEAD_STATIC(aslist, asent);
61
62 static pthread_t asthread = AST_PTHREADT_NULL;
63
64 static void *autoservice_run(void *ign)
65 {
66
67         for(;;) {
68                 struct ast_channel *mons[MAX_AUTOMONS];
69                 struct ast_channel *chan;
70                 struct asent *as;
71                 int x = 0, ms = 500;
72
73                 AST_LIST_LOCK(&aslist);
74                 AST_LIST_TRAVERSE(&aslist, as, list) {
75                         if (!as->chan->_softhangup) {
76                                 if (x < MAX_AUTOMONS)
77                                         mons[x++] = as->chan;
78                                 else
79                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
80                         }
81                 }
82                 AST_LIST_UNLOCK(&aslist);
83
84                 chan = ast_waitfor_n(mons, x, &ms);
85                 if (chan) {
86                         /* Read and ignore anything that occurs */
87                         struct ast_frame *f = ast_read(chan);
88                         if (f)
89                                 ast_frfree(f);
90                 }
91         }
92         asthread = AST_PTHREADT_NULL;
93         return NULL;
94 }
95
96 int ast_autoservice_start(struct ast_channel *chan)
97 {
98         int res = -1;
99         struct asent *as;
100         AST_LIST_LOCK(&aslist);
101
102         /* Check if the channel already has autoservice */
103         AST_LIST_TRAVERSE(&aslist, as, list) {
104                 if (as->chan == chan)
105                         break;
106         }
107         /* XXX if found, we return -1, why ??? */
108
109         /* If not, start autoservice on channel */
110         if (!as && (as = ast_calloc(1, sizeof(*as)))) {
111                 as->chan = chan;
112                 AST_LIST_INSERT_HEAD(&aslist, as, list);
113                 res = 0;
114                 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
115                         if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
116                                 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
117                                 /* There will only be a single member in the list at this point,
118                                    the one we just added. */
119                                 AST_LIST_REMOVE(&aslist, as, list);
120                                 free(as);
121                                 res = -1;
122                         } else
123                                 pthread_kill(asthread, SIGURG);
124                 }
125         }
126         AST_LIST_UNLOCK(&aslist);
127         return res;
128 }
129
130 int ast_autoservice_stop(struct ast_channel *chan)
131 {
132         int res = -1;
133         struct asent *as;
134
135         AST_LIST_LOCK(&aslist);
136         AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {       
137                 if (as->chan == chan) {
138                         AST_LIST_REMOVE_CURRENT(&aslist, list);
139                         free(as);
140                         if (!chan->_softhangup)
141                                 res = 0;
142                         break;
143                 }
144         }
145         AST_LIST_TRAVERSE_SAFE_END
146
147         if (asthread != AST_PTHREADT_NULL) 
148                 pthread_kill(asthread, SIGURG);
149         AST_LIST_UNLOCK(&aslist);
150
151         /* Wait for it to un-block */
152         while(ast_test_flag(chan, AST_FLAG_BLOCKING))
153                 usleep(1000);
154         return res;
155 }