Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_system.c
blob1f39c5a4ed7c38da02627c8de5ba74e2b46d49e9
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 "asterisk/pbx.h"
33 #include "asterisk/module.h"
34 #include "asterisk/app.h"
35 #include "asterisk/channel.h" /* autoservice */
37 static char *app = "System";
39 static char *app2 = "TrySystem";
41 static char *synopsis = "Execute a system command";
43 static char *synopsis2 = "Try executing a system command";
45 static char *chanvar = "SYSTEMSTATUS";
47 static char *descrip =
48 " System(command): Executes a command by using system(). If the command\n"
49 "fails, the console should report a fallthrough. \n"
50 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
51 " FAILURE Could not execute the specified command\n"
52 " SUCCESS Specified command successfully executed\n";
54 static char *descrip2 =
55 " TrySystem(command): Executes a command by using system().\n"
56 "on any situation.\n"
57 "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
58 " FAILURE Could not execute the specified command\n"
59 " SUCCESS Specified command successfully executed\n"
60 " APPERROR Specified command successfully executed, but returned error code\n";
62 static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
64 int res = 0;
66 if (ast_strlen_zero(data)) {
67 ast_log(LOG_WARNING, "System requires an argument(command)\n");
68 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
69 return failmode;
72 ast_autoservice_start(chan);
74 /* Do our thing here */
75 res = ast_safe_system((char *)data);
76 if ((res < 0) && (errno != ECHILD)) {
77 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
78 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
79 res = failmode;
80 } else if (res == 127) {
81 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
82 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
83 res = failmode;
84 } else {
85 if (res < 0)
86 res = 0;
87 if (res != 0)
88 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
89 else
90 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
91 res = 0;
94 ast_autoservice_stop(chan);
96 return res;
99 static int system_exec(struct ast_channel *chan, void *data)
101 return system_exec_helper(chan, data, -1);
104 static int trysystem_exec(struct ast_channel *chan, void *data)
106 return system_exec_helper(chan, data, 0);
109 static int unload_module(void)
111 int res;
113 res = ast_unregister_application(app);
114 res |= ast_unregister_application(app2);
116 return res;
119 static int load_module(void)
121 int res;
123 res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
124 res |= ast_register_application(app, system_exec, synopsis, descrip);
126 return res;
129 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");