(closes issue #12846)
[asterisk-bristuff.git] / apps / app_system.c
blobb60e11c5a2b320861b06995ba9cd4bba6c936fdb
1 /*
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.
19 /*! \file
21 * \brief Execute arbitrary system commands
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
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"
47 static char *app = "System";
49 static char *app2 = "TrySystem";
51 static char *synopsis = "Execute a system command";
53 static char *synopsis2 = "Try executing a system command";
55 static char *chanvar = "SYSTEMSTATUS";
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 "\n"
64 "Old behaviour:\n"
65 "If the command itself executes but is in error, and if there exists\n"
66 "a priority n + 101, where 'n' is the priority of the current instance,\n"
67 "then the channel will be setup to continue at that priority level.\n"
68 "Note that this jump functionality has been deprecated and will only occur\n"
69 "if the global priority jumping option is enabled in extensions.conf.\n";
71 static char *descrip2 =
72 " TrySystem(command): Executes a command by using system().\n"
73 "on any situation.\n"
74 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
75 " FAILURE Could not execute the specified command\n"
76 " SUCCESS Specified command successfully executed\n"
77 " APPERROR Specified command successfully executed, but returned error code\n"
78 "\n"
79 "Old behaviour:\nIf the command itself executes but is in error, and if\n"
80 "there exists a priority n + 101, where 'n' is the priority of the current\n"
81 "instance, then the channel will be setup to continue at that\n"
82 "priority level. Otherwise, System will terminate.\n";
85 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
87 int res=0;
88 struct ast_module_user *u;
90 if (ast_strlen_zero(data)) {
91 ast_log(LOG_WARNING, "System requires an argument(command)\n");
92 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
93 return failmode;
96 u = ast_module_user_add(chan);
98 ast_autoservice_start(chan);
100 /* Do our thing here */
101 res = ast_safe_system((char *)data);
102 if ((res < 0) && (errno != ECHILD)) {
103 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
104 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
105 res = failmode;
106 } else if (res == 127) {
107 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
108 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
109 res = failmode;
110 } else {
111 if (res < 0)
112 res = 0;
113 if (ast_opt_priority_jumping && res)
114 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
116 if (res != 0)
117 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
118 else
119 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
120 res = 0;
123 ast_autoservice_stop(chan);
125 ast_module_user_remove(u);
127 return res;
130 static int system_exec(struct ast_channel *chan, void *data)
132 return system_exec_helper(chan, data, -1);
135 static int trysystem_exec(struct ast_channel *chan, void *data)
137 return system_exec_helper(chan, data, 0);
140 static int unload_module(void)
142 int res;
144 res = ast_unregister_application(app);
145 res |= ast_unregister_application(app2);
147 ast_module_user_hangup_all();
149 return res;
152 static int load_module(void)
154 int res;
156 res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
157 res |= ast_register_application(app, system_exec, synopsis, descrip);
159 return res;
162 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");