add a project-specific script to be used during release preparation
[asterisk-bristuff.git] / apps / app_system.c
blob4bada66b23e5bf0bf54089238c78b79b192de298
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 /* Do our thing here */
99 res = ast_safe_system((char *)data);
100 if ((res < 0) && (errno != ECHILD)) {
101 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
102 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
103 res = failmode;
104 } else if (res == 127) {
105 ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
106 pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
107 res = failmode;
108 } else {
109 if (res < 0)
110 res = 0;
111 if (ast_opt_priority_jumping && res)
112 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
114 if (res != 0)
115 pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
116 else
117 pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
118 res = 0;
121 ast_module_user_remove(u);
123 return res;
126 static int system_exec(struct ast_channel *chan, void *data)
128 return system_exec_helper(chan, data, -1);
131 static int trysystem_exec(struct ast_channel *chan, void *data)
133 return system_exec_helper(chan, data, 0);
136 static int unload_module(void)
138 int res;
140 res = ast_unregister_application(app);
141 res |= ast_unregister_application(app2);
143 ast_module_user_hangup_all();
145 return res;
148 static int load_module(void)
150 int res;
152 res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
153 res |= ast_register_application(app, system_exec, synopsis, descrip);
155 return res;
158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");