Go to the documentation of this file.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: 251879 $")
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
00040
00041
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 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, void *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
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, void *data)
00153 {
00154 return system_exec_helper(chan, data, -1);
00155 }
00156
00157 static int trysystem_exec(struct ast_channel *chan, void *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");