Mon May 14 04:42:53 2007

Asterisk developer's documentation


app_system.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Execute arbitrary system commands
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00031 
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <unistd.h>
00035 #include <string.h>
00036 #include <errno.h>
00037 
00038 #include "asterisk/lock.h"
00039 #include "asterisk/file.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/app.h"
00045 #include "asterisk/options.h"
00046 
00047 static char *app = "System";
00048 
00049 static char *app2 = "TrySystem";
00050 
00051 static char *synopsis = "Execute a system command";
00052 
00053 static char *synopsis2 = "Try executing a system command";
00054 
00055 static char *chanvar = "SYSTEMSTATUS";
00056 
00057 static char *descrip =
00058 "  System(command): Executes a command  by  using  system(). If the command\n"
00059 "fails, the console should report a fallthrough. \n"
00060 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
00061 "   FAILURE Could not execute the specified command\n"
00062 "   SUCCESS Specified command successfully executed\n"
00063 "\n"
00064 "Old behaviour:\n"
00065 "If the command itself executes but is in error, and if there exists\n"
00066 "a priority n + 101, where 'n' is the priority of the current instance,\n"
00067 "then  the  channel  will  be  setup to continue at that priority level.\n"
00068 "Note that this jump functionality has been deprecated and will only occur\n"
00069 "if the global priority jumping option is enabled in extensions.conf.\n";
00070 
00071 static char *descrip2 =
00072 "  TrySystem(command): Executes a command  by  using  system().\n"
00073 "on any situation.\n"
00074 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
00075 "   FAILURE Could not execute the specified command\n"
00076 "   SUCCESS Specified command successfully executed\n"
00077 "   APPERROR   Specified command successfully executed, but returned error code\n"
00078 "\n"
00079 "Old behaviour:\nIf  the command itself executes but is in error, and if\n"
00080 "there exists a priority n + 101, where 'n' is the priority of the current\n"
00081 "instance, then  the  channel  will  be  setup  to continue at that\n"
00082 "priority level.  Otherwise, System will terminate.\n";
00083 
00084 
00085 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
00086 {
00087    int res=0;
00088    struct ast_module_user *u;
00089    
00090    if (ast_strlen_zero(data)) {
00091       ast_log(LOG_WARNING, "System requires an argument(command)\n");
00092       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00093       return failmode;
00094    }
00095 
00096    u = ast_module_user_add(chan);
00097 
00098    /* Do our thing here */
00099    res = ast_safe_system((char *)data);
00100    if ((res < 0) && (errno != ECHILD)) {
00101       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00102       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00103       res = failmode;
00104    } else if (res == 127) {
00105       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00106       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00107       res = failmode;
00108    } else {
00109       if (res < 0) 
00110          res = 0;
00111       if (ast_opt_priority_jumping && res)
00112          ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
00113 
00114       if (res != 0)
00115          pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
00116       else
00117          pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
00118       res = 0;
00119    } 
00120 
00121    ast_module_user_remove(u);
00122 
00123    return res;
00124 }
00125 
00126 static int system_exec(struct ast_channel *chan, void *data)
00127 {
00128    return system_exec_helper(chan, data, -1);
00129 }
00130 
00131 static int trysystem_exec(struct ast_channel *chan, void *data)
00132 {
00133    return system_exec_helper(chan, data, 0);
00134 }
00135 
00136 static int unload_module(void)
00137 {
00138    int res;
00139 
00140    res = ast_unregister_application(app);
00141    res |= ast_unregister_application(app2);
00142    
00143    ast_module_user_hangup_all();
00144 
00145    return res;
00146 }
00147 
00148 static int load_module(void)
00149 {
00150    int res;
00151 
00152    res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
00153    res |= ast_register_application(app, system_exec, synopsis, descrip);
00154 
00155    return res;
00156 }
00157 
00158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");

Generated on Mon May 14 04:42:53 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1