Setup environment variables for the benefit of child processes and disallow changing...
[asterisk/asterisk.git] / funcs / func_shell.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * SHELL function to return the value of a system call.
20  * 
21  * \note Inspiration and Guidance from Russell! Thank You! 
22  *
23  * \author Brandon Kruse <bkruse@digium.com>
24  *
25  * \ingroup functions
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/app.h"
37
38 static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
39                                          char *buf, size_t len)
40 {
41         if (ast_strlen_zero(data)) {
42                 ast_log(LOG_WARNING, "Missing Argument!  Example:  Set(foo=${SHELL(echo \"bar\")})\n");
43                 return -1;
44         }
45
46         if (chan)
47                 ast_autoservice_start(chan);
48
49         if (len >= 1) {
50                 FILE *ptr;
51                 char plbuff[4096];
52
53                 ptr = popen(data, "r");
54                 while (fgets(plbuff, sizeof(plbuff), ptr)) {
55                         strncat(buf, plbuff, len - strlen(buf) - 1);
56                 }
57                 pclose(ptr);
58         }
59
60         if (chan)
61                 ast_autoservice_stop(chan);
62
63         return 0;
64 }
65
66 /*** DOCUMENTATION
67         <function name="SHELL" language="en_US">
68                 <synopsis>
69                         Executes a command as if you were at a shell.
70                 </synopsis>
71                 <syntax>
72                         <parameter name="command" required="true">
73                                 <para>This is the argument to the function, the command you want to pass to the shell.</para>
74                         </parameter>
75                 </syntax>
76                 <description>
77                         <para>Returns the value from a system command</para>
78                         <para>Example:  <literal>Set(foo=${SHELL(echo \bar\)})</literal></para>
79                         <note><para>When using the SHELL() dialplan function, your \SHELL\ is /bin/sh,
80                         which may differ as to the underlying shell, depending upon your production
81                         platform.  Also keep in mind that if you are using a common path, you should
82                         be mindful of race conditions that could result from two calls running
83                         SHELL() simultaneously.</para></note>
84                 </description>
85  
86         </function>
87  ***/
88 static struct ast_custom_function shell_function = {
89         .name = "SHELL",
90         .read = shell_helper,
91 };
92
93 static int unload_module(void)
94 {
95         return ast_custom_function_unregister(&shell_function);
96 }
97
98 static int load_module(void)
99 {
100         return ast_custom_function_register(&shell_function);
101 }
102
103 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Returns the output of a shell command");
104