func_shell.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /*! \file
  17. *
  18. * SHELL function to return the value of a system call.
  19. *
  20. * \note Inspiration and Guidance from Russell! Thank You!
  21. *
  22. * \author Brandon Kruse <bkruse@digium.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/app.h"
  36. static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
  37. char *buf, size_t len)
  38. {
  39. int res = 0;
  40. if (ast_strlen_zero(data)) {
  41. ast_log(LOG_WARNING, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n");
  42. return -1;
  43. }
  44. if (chan) {
  45. ast_autoservice_start(chan);
  46. }
  47. if (len >= 1) {
  48. FILE *ptr;
  49. char plbuff[4096];
  50. ptr = popen(data, "r");
  51. if (ptr) {
  52. while (fgets(plbuff, sizeof(plbuff), ptr)) {
  53. strncat(buf, plbuff, len - strlen(buf) - 1);
  54. }
  55. pclose(ptr);
  56. } else {
  57. ast_log(LOG_WARNING, "Failed to execute shell command '%s'\n", data);
  58. res = -1;
  59. }
  60. }
  61. if (chan) {
  62. ast_autoservice_stop(chan);
  63. }
  64. return res;
  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. </function>
  86. ***/
  87. static struct ast_custom_function shell_function = {
  88. .name = "SHELL",
  89. .read = shell_helper,
  90. };
  91. static int unload_module(void)
  92. {
  93. return ast_custom_function_unregister(&shell_function);
  94. }
  95. static int load_module(void)
  96. {
  97. return ast_custom_function_register(&shell_function);
  98. }
  99. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Returns the output of a shell command");