Thu Jul 9 13:40:20 2009

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: 177665 $")
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    
00071    if (ast_strlen_zero(data)) {
00072       ast_log(LOG_WARNING, "System requires an argument(command)\n");
00073       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00074       return failmode;
00075    }
00076 
00077    ast_autoservice_start(chan);
00078 
00079    /* Do our thing here */
00080    ast_str_get_encoded_str(&buf, 0, (char *) data);
00081    res = ast_safe_system(ast_str_buffer(buf));
00082 
00083    if ((res < 0) && (errno != ECHILD)) {
00084       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00085       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00086       res = failmode;
00087    } else if (res == 127) {
00088       ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00089       pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00090       res = failmode;
00091    } else {
00092       if (res < 0) 
00093          res = 0;
00094       if (res != 0)
00095          pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
00096       else
00097          pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
00098       res = 0;
00099    } 
00100 
00101    ast_autoservice_stop(chan);
00102 
00103    return res;
00104 }
00105 
00106 static int system_exec(struct ast_channel *chan, void *data)
00107 {
00108    return system_exec_helper(chan, data, -1);
00109 }
00110 
00111 static int trysystem_exec(struct ast_channel *chan, void *data)
00112 {
00113    return system_exec_helper(chan, data, 0);
00114 }
00115 
00116 static int unload_module(void)
00117 {
00118    int res;
00119 
00120    res = ast_unregister_application(app);
00121    res |= ast_unregister_application(app2);
00122 
00123    return res;
00124 }
00125 
00126 static int load_module(void)
00127 {
00128    int res;
00129 
00130    res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
00131    res |= ast_register_application(app, system_exec, synopsis, descrip);
00132 
00133    return res;
00134 }
00135 
00136 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");

Generated on Thu Jul 9 13:40:20 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7