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

Generated on Thu Apr 16 06:27:12 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6