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
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 340863 $")
00035
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/module.h"
00038 #include "asterisk/app.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/strings.h"
00041 #include "asterisk/threadstorage.h"
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
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
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");