2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Execute arbitrary system commands
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/pbx.h"
33 #include "asterisk/module.h"
34 #include "asterisk/app.h"
35 #include "asterisk/channel.h" /* autoservice */
36 #include "asterisk/strings.h"
37 #include "asterisk/threadstorage.h"
40 <application name="System" language="en_US">
42 Execute a system command.
45 <parameter name="command" required="true">
46 <para>Command to execute</para>
50 <para>Executes a command by using system(). If the command
51 fails, the console should report a fallthrough.</para>
52 <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
54 <variable name="SYSTEMSTATUS">
55 <value name="FAILURE">
56 Could not execute the specified command.
58 <value name="SUCCESS">
59 Specified command successfully executed.
65 <application name="TrySystem" language="en_US">
67 Try executing a system command.
70 <parameter name="command" required="true">
71 <para>Command to execute</para>
75 <para>Executes a command by using system().</para>
76 <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
78 <variable name="SYSTEMSTATUS">
79 <value name="FAILURE">
80 Could not execute the specified command.
82 <value name="SUCCESS">
83 Specified command successfully executed.
85 <value name="APPERROR">
86 Specified command successfully executed, but returned error code.
95 AST_THREADSTORAGE(buf_buf);
97 static char *app = "System";
99 static char *app2 = "TrySystem";
101 static char *chanvar = "SYSTEMSTATUS";
103 static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
106 struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
109 if (ast_strlen_zero(data)) {
110 ast_log(LOG_WARNING, "System requires an argument(command)\n");
111 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
115 ast_autoservice_start(chan);
117 /* Do our thing here */
118 ast_str_get_encoded_str(&buf, 0, (char *) data);
119 cbuf = ast_str_buffer(buf);
121 if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
122 cbuf[ast_str_strlen(buf) - 1] = '\0';
124 ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
127 res = ast_safe_system(cbuf);
129 if ((res < 0) && (errno != ECHILD)) {
130 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
131 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
133 } else if (res == 127) {
134 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
135 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
141 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
143 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
147 ast_autoservice_stop(chan);
152 static int system_exec(struct ast_channel *chan, const char *data)
154 return system_exec_helper(chan, data, -1);
157 static int trysystem_exec(struct ast_channel *chan, const char *data)
159 return system_exec_helper(chan, data, 0);
162 static int unload_module(void)
166 res = ast_unregister_application(app);
167 res |= ast_unregister_application(app2);
172 static int load_module(void)
176 res = ast_register_application_xml(app2, trysystem_exec);
177 res |= ast_register_application_xml(app, system_exec);
182 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");