Fix a few things I missed to ensure zt_chan_conf structure is not modified in mkintf
[asterisk-bristuff.git] / apps / app_zapras.c
blobc3ccf30d917da16f24975acde006722b65b3e002
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Execute an ISDN RAS
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 /*** MODULEINFO
29 <depend>zaptel</depend>
30 ***/
32 #include "asterisk.h"
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include <sys/ioctl.h>
37 #include <sys/wait.h>
38 #ifdef __linux__
39 #include <sys/signal.h>
40 #else
41 #include <signal.h>
42 #endif /* __linux__ */
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <fcntl.h>
51 #include <zaptel/zaptel.h>
53 #include "asterisk/lock.h"
54 #include "asterisk/file.h"
55 #include "asterisk/logger.h"
56 #include "asterisk/channel.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/module.h"
59 #include "asterisk/options.h"
61 static char *app = "ZapRAS";
63 static char *synopsis = "Executes Zaptel ISDN RAS application";
65 static char *descrip =
66 " ZapRAS(args): Executes a RAS server using pppd on the given channel.\n"
67 "The channel must be a clear channel (i.e. PRI source) and a Zaptel\n"
68 "channel to be able to use this function (No modem emulation is included).\n"
69 "Your pppd must be patched to be zaptel aware. Arguments should be\n"
70 "separated by | characters.\n";
73 #define PPP_MAX_ARGS 32
74 #define PPP_EXEC "/usr/sbin/pppd"
76 static pid_t spawn_ras(struct ast_channel *chan, char *args)
78 pid_t pid;
79 int x;
80 char *c;
82 char *argv[PPP_MAX_ARGS];
83 int argc = 0;
84 char *stringp=NULL;
85 sigset_t fullset, oldset;
87 sigfillset(&fullset);
88 pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
90 /* Start by forking */
91 pid = fork();
92 if (pid) {
93 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
94 return pid;
97 /* Restore original signal handlers */
98 for (x=0;x<NSIG;x++)
99 signal(x, SIG_DFL);
101 pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
103 /* Execute RAS on File handles */
104 dup2(chan->fds[0], STDIN_FILENO);
106 /* Drop high priority */
107 if (ast_opt_high_priority)
108 ast_set_priority(0);
110 /* Close other file descriptors */
111 for (x=STDERR_FILENO + 1;x<1024;x++)
112 close(x);
114 /* Reset all arguments */
115 memset(argv, 0, sizeof(argv));
117 /* First argument is executable, followed by standard
118 arguments for zaptel PPP */
119 argv[argc++] = PPP_EXEC;
120 argv[argc++] = "nodetach";
122 /* And all the other arguments */
123 stringp=args;
124 c = strsep(&stringp, "|");
125 while(c && strlen(c) && (argc < (PPP_MAX_ARGS - 4))) {
126 argv[argc++] = c;
127 c = strsep(&stringp, "|");
130 argv[argc++] = "plugin";
131 argv[argc++] = "zaptel.so";
132 argv[argc++] = "stdin";
134 /* Finally launch PPP */
135 execv(PPP_EXEC, argv);
136 fprintf(stderr, "Failed to exec PPPD!\n");
137 exit(1);
140 static void run_ras(struct ast_channel *chan, char *args)
142 pid_t pid;
143 int status;
144 int res;
145 int signalled = 0;
146 struct zt_bufferinfo savebi;
147 int x;
149 res = ioctl(chan->fds[0], ZT_GET_BUFINFO, &savebi);
150 if(res) {
151 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %s\n", chan->name);
152 return;
155 pid = spawn_ras(chan, args);
156 if (pid < 0) {
157 ast_log(LOG_WARNING, "Failed to spawn RAS\n");
158 } else {
159 for (;;) {
160 res = wait4(pid, &status, WNOHANG, NULL);
161 if (!res) {
162 /* Check for hangup */
163 if (chan->_softhangup && !signalled) {
164 ast_log(LOG_DEBUG, "Channel '%s' hungup. Signalling RAS at %d to die...\n", chan->name, pid);
165 kill(pid, SIGTERM);
166 signalled=1;
168 /* Try again */
169 sleep(1);
170 continue;
172 if (res < 0) {
173 ast_log(LOG_WARNING, "wait4 returned %d: %s\n", res, strerror(errno));
175 if (option_verbose > 2) {
176 if (WIFEXITED(status)) {
177 ast_verbose(VERBOSE_PREFIX_3 "RAS on %s terminated with status %d\n", chan->name, WEXITSTATUS(status));
178 } else if (WIFSIGNALED(status)) {
179 ast_verbose(VERBOSE_PREFIX_3 "RAS on %s terminated with signal %d\n",
180 chan->name, WTERMSIG(status));
181 } else {
182 ast_verbose(VERBOSE_PREFIX_3 "RAS on %s terminated weirdly.\n", chan->name);
185 /* Throw back into audio mode */
186 x = 1;
187 ioctl(chan->fds[0], ZT_AUDIOMODE, &x);
189 /* Restore saved values */
190 res = ioctl(chan->fds[0], ZT_SET_BUFINFO, &savebi);
191 if (res < 0) {
192 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %s\n", chan->name);
194 break;
199 static int zapras_exec(struct ast_channel *chan, void *data)
201 int res=-1;
202 char *args;
203 struct ast_module_user *u;
204 ZT_PARAMS ztp;
206 if (!data)
207 data = "";
209 u = ast_module_user_add(chan);
211 args = ast_strdupa(data);
213 /* Answer the channel if it's not up */
214 if (chan->_state != AST_STATE_UP)
215 ast_answer(chan);
216 if (strcasecmp(chan->tech->type, "Zap")) {
217 /* If it's not a zap channel, we're done. Wait a couple of
218 seconds and then hangup... */
219 if (option_verbose > 1)
220 ast_verbose(VERBOSE_PREFIX_2 "Channel %s is not a Zap channel\n", chan->name);
221 sleep(2);
222 } else {
223 memset(&ztp, 0, sizeof(ztp));
224 if (ioctl(chan->fds[0], ZT_GET_PARAMS, &ztp)) {
225 ast_log(LOG_WARNING, "Unable to get zaptel parameters\n");
226 } else if (ztp.sigtype != ZT_SIG_CLEAR) {
227 if (option_verbose > 1)
228 ast_verbose(VERBOSE_PREFIX_2 "Channel %s is not a clear channel\n", chan->name);
229 } else {
230 /* Everything should be okay. Run PPP. */
231 if (option_verbose > 2)
232 ast_verbose(VERBOSE_PREFIX_3 "Starting RAS on %s\n", chan->name);
233 /* Execute RAS */
234 run_ras(chan, args);
237 ast_module_user_remove(u);
238 return res;
241 static int unload_module(void)
243 int res;
245 res = ast_unregister_application(app);
247 ast_module_user_hangup_all();
249 return res;
252 static int load_module(void)
254 return ast_register_application(app, zapras_exec, synopsis, descrip);
257 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Zap RAS Application");