Asterisk data retrieval API.
[asterisk/asterisk.git] / apps / app_system.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Execute arbitrary system commands
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
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"
38
39 /*** DOCUMENTATION
40         <application name="System" language="en_US">
41                 <synopsis>
42                         Execute a system command.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="command" required="true">
46                                 <para>Command to execute</para>
47                         </parameter>
48                 </syntax>
49                 <description>
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>
53                         <variablelist>
54                                 <variable name="SYSTEMSTATUS">
55                                         <value name="FAILURE">
56                                                 Could not execute the specified command.
57                                         </value>
58                                         <value name="SUCCESS">
59                                                 Specified command successfully executed.
60                                         </value>
61                                 </variable>
62                         </variablelist>
63                 </description>
64         </application>
65         <application name="TrySystem" language="en_US">
66                 <synopsis>
67                         Try executing a system command.
68                 </synopsis>
69                 <syntax>
70                         <parameter name="command" required="true">
71                                 <para>Command to execute</para>
72                         </parameter>
73                 </syntax>
74                 <description>
75                         <para>Executes a command  by  using  system().</para>
76                         <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
77                         <variablelist>
78                                 <variable name="SYSTEMSTATUS">
79                                         <value name="FAILURE">
80                                                 Could not execute the specified command.
81                                         </value>
82                                         <value name="SUCCESS">
83                                                 Specified command successfully executed.
84                                         </value>
85                                         <value name="APPERROR">
86                                                 Specified command successfully executed, but returned error code.
87                                         </value>
88                                 </variable>
89                         </variablelist>
90                 </description>
91         </application>
92
93  ***/
94
95 AST_THREADSTORAGE(buf_buf);
96
97 static char *app = "System";
98
99 static char *app2 = "TrySystem";
100
101 static char *chanvar = "SYSTEMSTATUS";
102
103 static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
104 {
105         int res = 0;
106         struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
107         char *cbuf;
108
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");
112                 return failmode;
113         }
114
115         ast_autoservice_start(chan);
116
117         /* Do our thing here */
118         ast_str_get_encoded_str(&buf, 0, (char *) data);
119         cbuf = ast_str_buffer(buf);
120
121         if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
122                 cbuf[ast_str_strlen(buf) - 1] = '\0';
123                 cbuf++;
124                 ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
125         }
126
127         res = ast_safe_system(cbuf);
128
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");
132                 res = failmode;
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");
136                 res = failmode;
137         } else {
138                 if (res < 0) 
139                         res = 0;
140                 if (res != 0)
141                         pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
142                 else
143                         pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
144                 res = 0;
145         } 
146
147         ast_autoservice_stop(chan);
148
149         return res;
150 }
151
152 static int system_exec(struct ast_channel *chan, const char *data)
153 {
154         return system_exec_helper(chan, data, -1);
155 }
156
157 static int trysystem_exec(struct ast_channel *chan, const char *data)
158 {
159         return system_exec_helper(chan, data, 0);
160 }
161
162 static int unload_module(void)
163 {
164         int res;
165
166         res = ast_unregister_application(app);
167         res |= ast_unregister_application(app2);
168
169         return res;
170 }
171
172 static int load_module(void)
173 {
174         int res;
175
176         res = ast_register_application_xml(app2, trysystem_exec);
177         res |= ast_register_application_xml(app, system_exec);
178
179         return res;
180 }
181
182 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");