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 */
38 <application name="System" language="en_US">
40 Execute a system command.
43 <parameter name="command" required="true">
44 <para>Command to execute</para>
48 <para>Executes a command by using system(). If the command
49 fails, the console should report a fallthrough.</para>
50 <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
52 <variable name="SYSTEMSTATUS">
53 <value name="FAILURE">
54 Could not execute the specified command.
56 <value name="SUCCESS">
57 Specified command successfully executed.
63 <application name="TrySystem" language="en_US">
65 Try executing a system command.
68 <parameter name="command" required="true">
69 <para>Command to execute</para>
73 <para>Executes a command by using system().</para>
74 <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
76 <variable name="SYSTEMSTATUS">
77 <value name="FAILURE">
78 Could not execute the specified command.
80 <value name="SUCCESS">
81 Specified command successfully executed.
83 <value name="APPERROR">
84 Specified command successfully executed, but returned error code.
93 static char *app = "System";
95 static char *app2 = "TrySystem";
97 static char *chanvar = "SYSTEMSTATUS";
99 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
103 if (ast_strlen_zero(data)) {
104 ast_log(LOG_WARNING, "System requires an argument(command)\n");
105 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
109 ast_autoservice_start(chan);
111 /* Do our thing here */
112 res = ast_safe_system((char *)data);
113 if ((res < 0) && (errno != ECHILD)) {
114 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
115 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
117 } else if (res == 127) {
118 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
119 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
125 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
127 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
131 ast_autoservice_stop(chan);
136 static int system_exec(struct ast_channel *chan, void *data)
138 return system_exec_helper(chan, data, -1);
141 static int trysystem_exec(struct ast_channel *chan, void *data)
143 return system_exec_helper(chan, data, 0);
146 static int unload_module(void)
150 res = ast_unregister_application(app);
151 res |= ast_unregister_application(app2);
156 static int load_module(void)
160 res = ast_register_application_xml(app2, trysystem_exec);
161 res |= ast_register_application_xml(app, system_exec);
166 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");