/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2005, Digium, Inc. * * Mark Spencer * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. */ /*! \file * * \brief Execute an ISDN RAS * * \author Mark Spencer * * \ingroup applications */ /*** MODULEINFO dahdi working_fork ***/ #include "asterisk.h" ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include #include #ifdef __linux__ #include #else #include #endif /* __linux__ */ #include #include #ifdef HAVE_CAP #include #endif /* HAVE_CAP */ #include "asterisk/lock.h" #include "asterisk/file.h" #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/module.h" static char *app = "DAHDIRAS"; static char *synopsis = "Executes DAHDI ISDN RAS application"; static char *descrip = " DAHDIRAS(args): Executes a RAS server using pppd on the given channel.\n" "The channel must be a clear channel (i.e. PRI source) and a DAHDI\n" "channel to be able to use this function (No modem emulation is included).\n" "Your pppd must be patched to be dahdi aware. Arguments should be\n" "separated by , characters.\n"; #define PPP_MAX_ARGS 32 #define PPP_EXEC "/usr/sbin/pppd" static pid_t spawn_ras(struct ast_channel *chan, char *args) { pid_t pid; int x; char *c; char *argv[PPP_MAX_ARGS]; int argc = 0; char *stringp=NULL; sigset_t fullset, oldset; #ifdef HAVE_CAP cap_t cap; #endif sigfillset(&fullset); pthread_sigmask(SIG_BLOCK, &fullset, &oldset); /* Start by forking */ pid = fork(); if (pid) { pthread_sigmask(SIG_SETMASK, &oldset, NULL); return pid; } #ifdef HAVE_CAP cap = cap_from_text("cap_net_admin-eip"); if (cap_set_proc(cap)) { /* Careful with order! Logging cannot happen after we close FDs */ ast_log(LOG_WARNING, "Unable to remove capabilities.\n"); } cap_free(cap); #endif /* Restore original signal handlers */ for (x=0;xfds[0], STDIN_FILENO); /* Drop high priority */ if (ast_opt_high_priority) ast_set_priority(0); /* Close other file descriptors */ for (x=STDERR_FILENO + 1;x<1024;x++) close(x); /* Reset all arguments */ memset(argv, 0, sizeof(argv)); /* First argument is executable, followed by standard arguments for dahdi PPP */ argv[argc++] = PPP_EXEC; argv[argc++] = "nodetach"; /* And all the other arguments */ stringp=args; c = strsep(&stringp, ","); while(c && strlen(c) && (argc < (PPP_MAX_ARGS - 4))) { argv[argc++] = c; c = strsep(&stringp, ","); } argv[argc++] = "plugin"; argv[argc++] = "dahdi.so"; argv[argc++] = "stdin"; /* Finally launch PPP */ execv(PPP_EXEC, argv); fprintf(stderr, "Failed to exec PPPD!\n"); exit(1); } static void run_ras(struct ast_channel *chan, char *args) { pid_t pid; int status; int res; int signalled = 0; struct dahdi_bufferinfo savebi; int x; res = ioctl(chan->fds[0], DAHDI_GET_BUFINFO, &savebi); if(res) { ast_log(LOG_WARNING, "Unable to check buffer policy on channel %s\n", chan->name); return; } pid = spawn_ras(chan, args); if (pid < 0) { ast_log(LOG_WARNING, "Failed to spawn RAS\n"); } else { for (;;) { res = wait4(pid, &status, WNOHANG, NULL); if (!res) { /* Check for hangup */ if (ast_check_hangup(chan) && !signalled) { ast_debug(1, "Channel '%s' hungup. Signalling RAS at %d to die...\n", chan->name, pid); kill(pid, SIGTERM); signalled=1; } /* Try again */ sleep(1); continue; } if (res < 0) { ast_log(LOG_WARNING, "wait4 returned %d: %s\n", res, strerror(errno)); } if (WIFEXITED(status)) { ast_verb(3, "RAS on %s terminated with status %d\n", chan->name, WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { ast_verb(3, "RAS on %s terminated with signal %d\n", chan->name, WTERMSIG(status)); } else { ast_verb(3, "RAS on %s terminated weirdly.\n", chan->name); } /* Throw back into audio mode */ x = 1; ioctl(chan->fds[0], DAHDI_AUDIOMODE, &x); /* Restore saved values */ res = ioctl(chan->fds[0], DAHDI_SET_BUFINFO, &savebi); if (res < 0) { ast_log(LOG_WARNING, "Unable to set buffer policy on channel %s\n", chan->name); } break; } } } static int dahdiras_exec(struct ast_channel *chan, void *data) { int res=-1; char *args; struct dahdi_params dahdip; if (!data) data = ""; args = ast_strdupa(data); /* Answer the channel if it's not up */ if (chan->_state != AST_STATE_UP) ast_answer(chan); if (strcasecmp(chan->tech->type, "DAHDI")) { /* If it's not a DAHDI channel, we're done. Wait a couple of seconds and then hangup... */ ast_verb(2, "Channel %s is not a DAHDI channel\n", chan->name); sleep(2); } else { memset(&dahdip, 0, sizeof(dahdip)); if (ioctl(chan->fds[0], DAHDI_GET_PARAMS, &dahdip)) { ast_log(LOG_WARNING, "Unable to get DAHDI parameters\n"); } else if (dahdip.sigtype != DAHDI_SIG_CLEAR) { ast_verb(2, "Channel %s is not a clear channel\n", chan->name); } else { /* Everything should be okay. Run PPP. */ ast_verb(3, "Starting RAS on %s\n", chan->name); /* Execute RAS */ run_ras(chan, args); } } return res; } static int unload_module(void) { return ast_unregister_application(app); } static int load_module(void) { return ((ast_register_application(app, dahdiras_exec, synopsis, descrip)) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS); } AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DAHDI ISDN Remote Access Server");