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: 177761 $")
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
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
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");