app_system.c
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: 177664 $")
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
00108 if (ast_strlen_zero(data)) {
00109 ast_log(LOG_WARNING, "System requires an argument(command)\n");
00110 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00111 return failmode;
00112 }
00113
00114 ast_autoservice_start(chan);
00115
00116
00117 ast_str_get_encoded_str(&buf, 0, (char *) data);
00118 res = ast_safe_system(ast_str_buffer(buf));
00119
00120 if ((res < 0) && (errno != ECHILD)) {
00121 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00122 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00123 res = failmode;
00124 } else if (res == 127) {
00125 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
00126 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
00127 res = failmode;
00128 } else {
00129 if (res < 0)
00130 res = 0;
00131 if (res != 0)
00132 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
00133 else
00134 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
00135 res = 0;
00136 }
00137
00138 ast_autoservice_stop(chan);
00139
00140 return res;
00141 }
00142
00143 static int system_exec(struct ast_channel *chan, void *data)
00144 {
00145 return system_exec_helper(chan, data, -1);
00146 }
00147
00148 static int trysystem_exec(struct ast_channel *chan, void *data)
00149 {
00150 return system_exec_helper(chan, data, 0);
00151 }
00152
00153 static int unload_module(void)
00154 {
00155 int res;
00156
00157 res = ast_unregister_application(app);
00158 res |= ast_unregister_application(app2);
00159
00160 return res;
00161 }
00162
00163 static int load_module(void)
00164 {
00165 int res;
00166
00167 res = ast_register_application_xml(app2, trysystem_exec);
00168 res |= ast_register_application_xml(app, system_exec);
00169
00170 return res;
00171 }
00172
00173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");