Wed Aug 18 22:33:43 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: 251878 $")
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 AST_THREADSTORAGE(buf_buf);
00040 
00041 static char *app = "System";
00042 
00043 static char *app2 = "TrySystem";
00044 
00045 static char *synopsis = "Execute a system command";
00046 
00047 static char *synopsis2 = "Try executing a system command";
00048 
00049 static char *chanvar = "SYSTEMSTATUS";
00050 
00051 static char *descrip =
00052 "  System(command): Executes a command  by  using  system(). If the command\n"
00053 "fails, the console should report a fallthrough. \n"
00054 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
00055 "   FAILURE Could not execute the specified command\n"
00056 "   SUCCESS Specified command successfully executed\n";
00057 
00058 static char *descrip2 =
00059 "  TrySystem(command): Executes a command  by  using  system().\n"
00060 "on any situation.\n"
00061 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
00062 "   FAILURE Could not execute the specified command\n"
00063 "   SUCCESS Specified command successfully executed\n"
00064 "   APPERROR   Specified command successfully executed, but returned error code\n";
00065 
00066 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
00067 {
00068    int res = 0;
00069    struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
00070    char *cbuf;
00071 
00072    if (ast_strlen_zero(data)) {
00073       ast_log(LOG_WARNING, "System requires an argument(command)\n");
00074       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00075       return failmode;
00076    }
00077 
00078    ast_autoservice_start(chan);
00079 
00080    /* Do our thing here */
00081    ast_str_get_encoded_str(&buf, 0, (char *) data);
00082    cbuf = ast_str_buffer(buf);
00083 
00084    if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
00085       cbuf[ast_str_strlen(buf) - 1] = '\0';
00086       cbuf++;
00087       ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
00088    }
00089 
00090    res = ast_safe_system(cbuf);
00091 
00092    if ((res < 0) && (errno != ECHILD)) {
00093       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00094       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00095       res = failmode;
00096    } else if (res == 127) {
00097       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00098       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00099       res = failmode;
00100    } else {
00101       if (res < 0) 
00102          res = 0;
00103       if (res != 0)
00104          pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
00105       else
00106          pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
00107       res = 0;
00108    } 
00109 
00110    ast_autoservice_stop(chan);
00111 
00112    return res;
00113 }
00114 
00115 static int system_exec(struct ast_channel *chan, void *data)
00116 {
00117    return system_exec_helper(chan, data, -1);
00118 }
00119 
00120 static int trysystem_exec(struct ast_channel *chan, void *data)
00121 {
00122    return system_exec_helper(chan, data, 0);
00123 }
00124 
00125 static int unload_module(void)
00126 {
00127    int res;
00128 
00129    res = ast_unregister_application(app);
00130    res |= ast_unregister_application(app2);
00131 
00132    return res;
00133 }
00134 
00135 static int load_module(void)
00136 {
00137    int res;
00138 
00139    res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
00140    res |= ast_register_application(app, system_exec, synopsis, descrip);
00141 
00142    return res;
00143 }
00144 
00145 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");

Generated on Wed Aug 18 22:33:43 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7