Use the same delimited character as the FILTER function in FIELDQTY and CUT.
[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 <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37
38 #include "asterisk/lock.h"
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/app.h"
45 #include "asterisk/options.h"
46
47 static char *app = "System";
48
49 static char *app2 = "TrySystem";
50
51 static char *synopsis = "Execute a system command";
52
53 static char *synopsis2 = "Try executing a system command";
54
55 static char *chanvar = "SYSTEMSTATUS";
56
57 static char *descrip =
58 "  System(command): Executes a command  by  using  system(). If the command\n"
59 "fails, the console should report a fallthrough. \n"
60 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
61 "   FAILURE     Could not execute the specified command\n"
62 "   SUCCESS     Specified command successfully executed\n";
63
64 static char *descrip2 =
65 "  TrySystem(command): Executes a command  by  using  system().\n"
66 "on any situation.\n"
67 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
68 "   FAILURE     Could not execute the specified command\n"
69 "   SUCCESS     Specified command successfully executed\n"
70 "   APPERROR    Specified command successfully executed, but returned error code\n";
71
72 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
73 {
74         int res = 0;
75         
76         if (ast_strlen_zero(data)) {
77                 ast_log(LOG_WARNING, "System requires an argument(command)\n");
78                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
79                 return failmode;
80         }
81
82         ast_autoservice_start(chan);
83
84         /* Do our thing here */
85         res = ast_safe_system((char *)data);
86         if ((res < 0) && (errno != ECHILD)) {
87                 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
88                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
89                 res = failmode;
90         } else if (res == 127) {
91                 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
92                 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
93                 res = failmode;
94         } else {
95                 if (res < 0) 
96                         res = 0;
97                 if (res != 0)
98                         pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
99                 else
100                         pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
101                 res = 0;
102         } 
103
104         ast_autoservice_stop(chan);
105
106         return res;
107 }
108
109 static int system_exec(struct ast_channel *chan, void *data)
110 {
111         return system_exec_helper(chan, data, -1);
112 }
113
114 static int trysystem_exec(struct ast_channel *chan, void *data)
115 {
116         return system_exec_helper(chan, data, 0);
117 }
118
119 static int unload_module(void)
120 {
121         int res;
122
123         res = ast_unregister_application(app);
124         res |= ast_unregister_application(app2);
125
126         return res;
127 }
128
129 static int load_module(void)
130 {
131         int res;
132
133         res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
134         res |= ast_register_application(app, system_exec, synopsis, descrip);
135
136         return res;
137 }
138
139 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");