Mon Jun 27 16:50:47 2011

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: 251877 $")
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, const char *data, int failmode)
00104 {
00105    int res = 0;
00106    struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
00107    char *cbuf;
00108 
00109    if (ast_strlen_zero(data)) {
00110       ast_log(LOG_WARNING, "System requires an argument(command)\n");
00111       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00112       return failmode;
00113    }
00114 
00115    ast_autoservice_start(chan);
00116 
00117    /* Do our thing here */
00118    ast_str_get_encoded_str(&buf, 0, (char *) data);
00119    cbuf = ast_str_buffer(buf);
00120 
00121    if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
00122       cbuf[ast_str_strlen(buf) - 1] = '\0';
00123       cbuf++;
00124       ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
00125    }
00126 
00127    res = ast_safe_system(cbuf);
00128 
00129    if ((res < 0) && (errno != ECHILD)) {
00130       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00131       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00132       res = failmode;
00133    } else if (res == 127) {
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 {
00138       if (res < 0) 
00139          res = 0;
00140       if (res != 0)
00141          pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
00142       else
00143          pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
00144       res = 0;
00145    } 
00146 
00147    ast_autoservice_stop(chan);
00148 
00149    return res;
00150 }
00151 
00152 static int system_exec(struct ast_channel *chan, const char *data)
00153 {
00154    return system_exec_helper(chan, data, -1);
00155 }
00156 
00157 static int trysystem_exec(struct ast_channel *chan, const char *data)
00158 {
00159    return system_exec_helper(chan, data, 0);
00160 }
00161 
00162 static int unload_module(void)
00163 {
00164    int res;
00165 
00166    res = ast_unregister_application(app);
00167    res |= ast_unregister_application(app2);
00168 
00169    return res;
00170 }
00171 
00172 static int load_module(void)
00173 {
00174    int res;
00175 
00176    res = ast_register_application_xml(app2, trysystem_exec);
00177    res |= ast_register_application_xml(app, system_exec);
00178 
00179    return res;
00180 }
00181 
00182 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");

Generated on Mon Jun 27 16:50:47 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7