Fri Nov 12 11:45:36 2010

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: 177664 $")
00031 
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/module.h"
00034 #include "asterisk/app.h"
00035 #include "asterisk/channel.h" /* autoservice */
00036 #include "asterisk/strings.h"
00037 #include "asterisk/threadstorage.h"
00038 
00039 /*** DOCUMENTATION
00040    <application name="System" language="en_US">
00041       <synopsis>
00042          Execute a system command.
00043       </synopsis>
00044       <syntax>
00045          <parameter name="command" required="true">
00046             <para>Command to execute</para>
00047          </parameter>
00048       </syntax>
00049       <description>
00050          <para>Executes a command  by  using  system(). If the command
00051          fails, the console should report a fallthrough.</para>
00052          <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
00053          <variablelist>
00054             <variable name="SYSTEMSTATUS">
00055                <value name="FAILURE">
00056                   Could not execute the specified command.
00057                </value>
00058                <value name="SUCCESS">
00059                   Specified command successfully executed.
00060                </value>
00061             </variable>
00062          </variablelist>
00063       </description>
00064    </application>
00065    <application name="TrySystem" language="en_US">
00066       <synopsis>
00067          Try executing a system command.
00068       </synopsis>
00069       <syntax>
00070          <parameter name="command" required="true">
00071             <para>Command to execute</para>
00072          </parameter>
00073       </syntax>
00074       <description>
00075          <para>Executes a command  by  using  system().</para>
00076          <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
00077          <variablelist>
00078             <variable name="SYSTEMSTATUS">
00079                <value name="FAILURE">
00080                   Could not execute the specified command.
00081                </value>
00082                <value name="SUCCESS">
00083                   Specified command successfully executed.
00084                </value>
00085                <value name="APPERROR">
00086                   Specified command successfully executed, but returned error code.
00087                </value>
00088             </variable>
00089          </variablelist>
00090       </description>
00091    </application>
00092 
00093  ***/
00094 
00095 AST_THREADSTORAGE(buf_buf);
00096 
00097 static char *app = "System";
00098 
00099 static char *app2 = "TrySystem";
00100 
00101 static char *chanvar = "SYSTEMSTATUS";
00102 
00103 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
00104 {
00105    int res = 0;
00106    struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
00107    
00108    if (ast_strlen_zero(data)) {
00109       ast_log(LOG_WARNING, "System requires an argument(command)\n");
00110       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00111       return failmode;
00112    }
00113 
00114    ast_autoservice_start(chan);
00115 
00116    /* Do our thing here */
00117    ast_str_get_encoded_str(&buf, 0, (char *) data);
00118    res = ast_safe_system(ast_str_buffer(buf));
00119 
00120    if ((res < 0) && (errno != ECHILD)) {
00121       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00122       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00123       res = failmode;
00124    } else if (res == 127) {
00125       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00126       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00127       res = failmode;
00128    } else {
00129       if (res < 0) 
00130          res = 0;
00131       if (res != 0)
00132          pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
00133       else
00134          pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
00135       res = 0;
00136    } 
00137 
00138    ast_autoservice_stop(chan);
00139 
00140    return res;
00141 }
00142 
00143 static int system_exec(struct ast_channel *chan, void *data)
00144 {
00145    return system_exec_helper(chan, data, -1);
00146 }
00147 
00148 static int trysystem_exec(struct ast_channel *chan, void *data)
00149 {
00150    return system_exec_helper(chan, data, 0);
00151 }
00152 
00153 static int unload_module(void)
00154 {
00155    int res;
00156 
00157    res = ast_unregister_application(app);
00158    res |= ast_unregister_application(app2);
00159 
00160    return res;
00161 }
00162 
00163 static int load_module(void)
00164 {
00165    int res;
00166 
00167    res = ast_register_application_xml(app2, trysystem_exec);
00168    res |= ast_register_application_xml(app, system_exec);
00169 
00170    return res;
00171 }
00172 
00173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");

Generated by  doxygen 1.6.2