| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-01-21 07:06:25 +00:00
										 |  |  |  * Copyright (c) 2004 - 2005, Tilghman Lesher.  All rights reserved. | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  |  * Portions copyright (c) 2006, Philipp Dunkel. | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  |  * Tilghman Lesher <app_exec__v002@the-tilghman.com> | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This code is released by the author with no restrictions on usage. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Exec application | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  |  * \author Tilghman Lesher <app_exec__v002@the-tilghman.com> | 
					
						
							|  |  |  |  * \author Philipp Dunkel <philipp.dunkel@ebox.at> | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Maximum length of any variable */ | 
					
						
							|  |  |  | #define MAXRESULT	1024
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | /*! Note
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The key difference between these two apps is exit status.  In a | 
					
						
							|  |  |  |  * nutshell, Exec tries to be transparent as possible, behaving | 
					
						
							|  |  |  |  * in exactly the same way as if the application it calls was | 
					
						
							|  |  |  |  * directly invoked from the dialplan. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * TryExec, on the other hand, provides a way to execute applications | 
					
						
							|  |  |  |  * and catch any possible fatal error without actually fatally | 
					
						
							|  |  |  |  * affecting the dialplan. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | static char *app_exec = "Exec"; | 
					
						
							|  |  |  | static char *exec_synopsis = "Executes dialplan application"; | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | static char *exec_descrip = | 
					
						
							| 
									
										
										
										
											2007-11-06 19:04:45 +00:00
										 |  |  | "  Exec(appname(arguments)):\n" | 
					
						
							|  |  |  | "Allows an arbitrary application to be invoked even when not\n" | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | "hardcoded into the dialplan.  If the underlying application\n" | 
					
						
							|  |  |  | "terminates the dialplan, or if the application cannot be found,\n" | 
					
						
							|  |  |  | "Exec will terminate the dialplan.\n" | 
					
						
							|  |  |  | "  To invoke external applications, see the application System.\n" | 
					
						
							|  |  |  | "  If you would like to catch any error instead, see TryExec.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *app_tryexec = "TryExec"; | 
					
						
							|  |  |  | static char *tryexec_synopsis = "Executes dialplan application, always returning"; | 
					
						
							|  |  |  | static char *tryexec_descrip = | 
					
						
							| 
									
										
										
										
											2007-11-06 19:04:45 +00:00
										 |  |  | "  TryExec(appname(arguments)):\n" | 
					
						
							|  |  |  | "Allows an arbitrary application to be invoked even when not\n" | 
					
						
							| 
									
										
										
										
											2005-04-27 01:50:53 +00:00
										 |  |  | "hardcoded into the dialplan. To invoke external applications\n" | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | "see the application System.  Always returns to the dialplan.\n" | 
					
						
							| 
									
										
										
										
											2007-11-06 19:04:45 +00:00
										 |  |  | "The channel variable TRYSTATUS will be set to one of:\n" | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | "    SUCCESS   if the application returned zero\n" | 
					
						
							|  |  |  | "    FAILED    if the application returned non-zero\n" | 
					
						
							| 
									
										
										
										
											2006-05-10 13:22:15 +00:00
										 |  |  | "    NOAPP     if the application was not found or was not specified\n"; | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | static char *app_execif = "ExecIf"; | 
					
						
							|  |  |  | static char *execif_synopsis = "Executes dialplan application, conditionally"; | 
					
						
							|  |  |  | static char *execif_descrip =  | 
					
						
							| 
									
										
										
										
											2007-11-06 19:04:45 +00:00
										 |  |  | "  ExecIF (<expr>?<app>(<data>):<app2>(<data2>))\n" | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | "If <expr> is true, execute and return the result of <app>(<data>).\n" | 
					
						
							|  |  |  | "If <expr> is true, but <app> is not found, then the application\n" | 
					
						
							|  |  |  | "will return a non-zero value.\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | static int exec_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-07-16 14:39:29 +00:00
										 |  |  | 	int res = 0; | 
					
						
							| 
									
										
										
											
												This commits the performance mods that give the priority processing engine in the pbx, a 25-30% speed boost. The two updates used, are, first, to merge the ast_exists_extension() and the ast_spawn_extension() where they are called sequentially in a loop in the code, into a slightly upgraded version of ast_spawn_extension(), with a few extra args; and, second, I modified the substitute_variables_helper_full, so it zeroes out the byte after the evaluated string instead of demanding you pre-zero the buffer; I also went thru the code and removed the code that zeroed this buffer before every call to the substitute_variables_helper_full. The first fix provides about a 9% speedup, and the second the rest. These figures come from the 'PIPS' benchmark I describe in blogs, conf. reports, etc.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@88166 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2007-11-01 22:26:51 +00:00
										 |  |  | 	char *s, *appname, *endargs, args[MAXRESULT]; | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | 	struct ast_app *app; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-15 02:02:30 +00:00
										 |  |  | 	if (ast_strlen_zero(data)) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	s = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2007-11-12 18:44:36 +00:00
										 |  |  | 	args[0] = 0; | 
					
						
							| 
									
										
										
										
											2007-02-15 02:02:30 +00:00
										 |  |  | 	appname = strsep(&s, "("); | 
					
						
							|  |  |  | 	if (s) { | 
					
						
							|  |  |  | 		endargs = strrchr(s, ')'); | 
					
						
							|  |  |  | 		if (endargs) | 
					
						
							|  |  |  | 			*endargs = '\0'; | 
					
						
							|  |  |  | 		pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (appname) { | 
					
						
							|  |  |  | 		app = pbx_findapp(appname); | 
					
						
							|  |  |  | 		if (app) { | 
					
						
							|  |  |  | 			res = pbx_exec(chan, app, args); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Could not find application (%s)\n", appname); | 
					
						
							|  |  |  | 			res = -1; | 
					
						
							| 
									
										
										
										
											2006-05-10 13:22:15 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | static int tryexec_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-07-16 14:39:29 +00:00
										 |  |  | 	int res = 0; | 
					
						
							| 
									
										
										
											
												This commits the performance mods that give the priority processing engine in the pbx, a 25-30% speed boost. The two updates used, are, first, to merge the ast_exists_extension() and the ast_spawn_extension() where they are called sequentially in a loop in the code, into a slightly upgraded version of ast_spawn_extension(), with a few extra args; and, second, I modified the substitute_variables_helper_full, so it zeroes out the byte after the evaluated string instead of demanding you pre-zero the buffer; I also went thru the code and removed the code that zeroed this buffer before every call to the substitute_variables_helper_full. The first fix provides about a 9% speedup, and the second the rest. These figures come from the 'PIPS' benchmark I describe in blogs, conf. reports, etc.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@88166 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2007-11-01 22:26:51 +00:00
										 |  |  | 	char *s, *appname, *endargs, args[MAXRESULT]; | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | 	struct ast_app *app; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-15 02:02:30 +00:00
										 |  |  | 	if (ast_strlen_zero(data)) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	s = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2007-11-12 18:44:36 +00:00
										 |  |  | 	args[0] = 0; | 
					
						
							| 
									
										
										
										
											2007-02-15 02:02:30 +00:00
										 |  |  | 	appname = strsep(&s, "("); | 
					
						
							|  |  |  | 	if (s) { | 
					
						
							|  |  |  | 		endargs = strrchr(s, ')'); | 
					
						
							|  |  |  | 		if (endargs) | 
					
						
							|  |  |  | 			*endargs = '\0'; | 
					
						
							|  |  |  | 		pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (appname) { | 
					
						
							|  |  |  | 		app = pbx_findapp(appname); | 
					
						
							|  |  |  | 		if (app) { | 
					
						
							|  |  |  | 			res = pbx_exec(chan, app, args); | 
					
						
							|  |  |  | 			pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Could not find application (%s)\n", appname); | 
					
						
							|  |  |  | 			pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP"); | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int execif_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	char *truedata = NULL, *falsedata = NULL, *end; | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | 	struct ast_app *app = NULL; | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	AST_DECLARE_APP_ARGS(expr, | 
					
						
							|  |  |  | 		AST_APP_ARG(expr); | 
					
						
							|  |  |  | 		AST_APP_ARG(remainder); | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 	AST_DECLARE_APP_ARGS(apps, | 
					
						
							| 
									
										
										
										
											2007-08-05 04:01:28 +00:00
										 |  |  | 		AST_APP_ARG(t); | 
					
						
							|  |  |  | 		AST_APP_ARG(f); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	); | 
					
						
							|  |  |  | 	char *parse = ast_strdupa(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_NONSTANDARD_APP_ARGS(expr, parse, '?'); | 
					
						
							| 
									
										
										
										
											2007-10-30 22:15:28 +00:00
										 |  |  | 	if (ast_strlen_zero(expr.remainder)) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Usage: ExecIf(<cond>?<appiftrue>(<args>):<appiffalse>(<args))\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	AST_NONSTANDARD_APP_ARGS(apps, expr.remainder, ':'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-05 04:01:28 +00:00
										 |  |  | 	if (apps.t && (truedata = strchr(apps.t, '('))) { | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 		*truedata++ = '\0'; | 
					
						
							|  |  |  | 		if ((end = strrchr(truedata, ')'))) | 
					
						
							|  |  |  | 			*end = '\0'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-05 04:01:28 +00:00
										 |  |  | 	if (apps.f && (falsedata = strchr(apps.f, '('))) { | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 		*falsedata++ = '\0'; | 
					
						
							|  |  |  | 		if ((end = strrchr(falsedata, ')'))) | 
					
						
							|  |  |  | 			*end = '\0'; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	if (pbx_checkcondition(expr.expr)) {  | 
					
						
							| 
									
										
										
										
											2007-08-05 04:01:28 +00:00
										 |  |  | 		if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) { | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 			res = pbx_exec(chan, app, S_OR(truedata, "")); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2007-08-05 04:01:28 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 			res = -1; | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2007-08-05 04:01:28 +00:00
										 |  |  | 		if (!ast_strlen_zero(apps.f) && (app = pbx_findapp(apps.f))) { | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 			res = pbx_exec(chan, app, S_OR(falsedata, "")); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2007-08-05 04:01:28 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 			res = -1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = ast_unregister_application(app_exec); | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | 	res |= ast_unregister_application(app_tryexec); | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | 	res |= ast_unregister_application(app_execif); | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | 	int res = ast_register_application(app_exec, exec_exec, exec_synopsis, exec_descrip); | 
					
						
							|  |  |  | 	res |= ast_register_application(app_tryexec, tryexec_exec, tryexec_synopsis, tryexec_descrip); | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  | 	res |= ast_register_application(app_execif, execif_exec, execif_synopsis, execif_descrip); | 
					
						
							| 
									
										
										
										
											2006-04-05 03:53:41 +00:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2004-04-20 20:05:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications"); |