00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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"
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
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");