688a649da664750896757c28b95c67f92e7105f2
[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
37 /*** DOCUMENTATION
38         <application name="System" language="en_US">
39                 <synopsis>
40                         Execute a system command.
41                 </synopsis>
42                 <syntax>
43                         <parameter name="command" required="true">
44                                 <para>Command to execute</para>
45                         </parameter>
46                 </syntax>
47                 <description>
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>
51                         <variablelist>
52                                 <variable name="SYSTEMSTATUS">
53                                         <value name="FAILURE">
54                                                 Could not execute the specified command.
55                                         </value>
56                                         <value name="SUCCESS">
57                                                 Specified command successfully executed.
58                                         </value>
59                                 </variable>
60                         </variablelist>
61                 </description>
62         </application>
63         <application name="TrySystem" language="en_US">
64                 <synopsis>
65                         Try executing a system command.
66                 </synopsis>
67                 <syntax>
68                         <parameter name="command" required="true">
69                                 <para>Command to execute</para>
70                         </parameter>
71                 </syntax>
72                 <description>
73                         <para>Executes a command  by  using  system().</para>
74                         <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
75                         <variablelist>
76                                 <variable name="SYSTEMSTATUS">
77                                         <value name="FAILURE">
78                                                 Could not execute the specified command.
79                                         </value>
80                                         <value name="SUCCESS">
81                                                 Specified command successfully executed.
82                                         </value>
83                                         <value name="APPERROR">
84                                                 Specified command successfully executed, but returned error code.
85                                         </value>
86                                 </variable>
87                         </variablelist>
88                 </description>
89         </application>
90
91  ***/
92
93 static char *app = "System";
94
95 static char *app2 = "TrySystem";
96
97 static char *chanvar = "SYSTEMSTATUS";
98
99 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
100 {
101         int res = 0;
102         
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");
106                 return failmode;
107         }
108
109         ast_autoservice_start(chan);
110
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");
116                 res = failmode;
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");
120                 res = failmode;
121         } else {
122                 if (res < 0) 
123                         res = 0;
124                 if (res != 0)
125                         pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
126                 else
127                         pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
128                 res = 0;
129         } 
130
131         ast_autoservice_stop(chan);
132
133         return res;
134 }
135
136 static int system_exec(struct ast_channel *chan, void *data)
137 {
138         return system_exec_helper(chan, data, -1);
139 }
140
141 static int trysystem_exec(struct ast_channel *chan, void *data)
142 {
143         return system_exec_helper(chan, data, 0);
144 }
145
146 static int unload_module(void)
147 {
148         int res;
149
150         res = ast_unregister_application(app);
151         res |= ast_unregister_application(app2);
152
153         return res;
154 }
155
156 static int load_module(void)
157 {
158         int res;
159
160         res = ast_register_application_xml(app2, trysystem_exec);
161         res |= ast_register_application_xml(app, system_exec);
162
163         return res;
164 }
165
166 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");