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
29 <support_level>core</support_level>
34 ASTERISK_REGISTER_FILE()
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/app.h"
39 #include "asterisk/channel.h" /* autoservice */
40 #include "asterisk/strings.h"
41 #include "asterisk/threadstorage.h"
44 <application name="System" language="en_US">
46 Execute a system command.
49 <parameter name="command" required="true">
50 <para>Command to execute</para>
54 <para>Executes a command by using system(). If the command
55 fails, the console should report a fallthrough.</para>
56 <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
58 <variable name="SYSTEMSTATUS">
59 <value name="FAILURE">
60 Could not execute the specified command.
62 <value name="SUCCESS">
63 Specified command successfully executed.
69 <application name="TrySystem" language="en_US">
71 Try executing a system command.
74 <parameter name="command" required="true">
75 <para>Command to execute</para>
79 <para>Executes a command by using system().</para>
80 <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
82 <variable name="SYSTEMSTATUS">
83 <value name="FAILURE">
84 Could not execute the specified command.
86 <value name="SUCCESS">
87 Specified command successfully executed.
89 <value name="APPERROR">
90 Specified command successfully executed, but returned error code.
99 AST_THREADSTORAGE(buf_buf);
101 static char *app = "System";
103 static char *app2 = "TrySystem";
105 static char *chanvar = "SYSTEMSTATUS";
107 static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
110 struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
113 if (ast_strlen_zero(data)) {
114 ast_log(LOG_WARNING, "System requires an argument(command)\n");
115 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
119 ast_autoservice_start(chan);
121 /* Do our thing here */
122 ast_str_get_encoded_str(&buf, 0, (char *) data);
123 cbuf = ast_str_buffer(buf);
125 if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
126 cbuf[ast_str_strlen(buf) - 1] = '\0';
128 ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
131 res = ast_safe_system(cbuf);
133 if ((res < 0) && (errno != ECHILD)) {
134 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
135 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
137 } else if (res == 127) {
138 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
139 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
145 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
147 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
151 ast_autoservice_stop(chan);
156 static int system_exec(struct ast_channel *chan, const char *data)
158 return system_exec_helper(chan, data, -1);
161 static int trysystem_exec(struct ast_channel *chan, const char *data)
163 return system_exec_helper(chan, data, 0);
166 static int unload_module(void)
170 res = ast_unregister_application(app);
171 res |= ast_unregister_application(app2);
176 static int load_module(void)
180 res = ast_register_application_xml(app2, trysystem_exec);
181 res |= ast_register_application_xml(app, system_exec);
186 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");