app_system.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  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.
  13. *
  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.
  17. */
  18. /*! \file
  19. *
  20. * \brief Execute arbitrary system commands
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/channel.h" /* autoservice */
  35. #include "asterisk/strings.h"
  36. #include "asterisk/threadstorage.h"
  37. /*** DOCUMENTATION
  38. <application name="System" language="en_US">
  39. <synopsis>
  40. Execute a system command.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="command" required="true">
  44. <para>Command to execute</para>
  45. </parameter>
  46. </syntax>
  47. <description>
  48. <para>Executes a command by using system(). If the command
  49. fails, the console should report a fallthrough.</para>
  50. <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
  51. <variablelist>
  52. <variable name="SYSTEMSTATUS">
  53. <value name="FAILURE">
  54. Could not execute the specified command.
  55. </value>
  56. <value name="SUCCESS">
  57. Specified command successfully executed.
  58. </value>
  59. </variable>
  60. </variablelist>
  61. </description>
  62. </application>
  63. <application name="TrySystem" language="en_US">
  64. <synopsis>
  65. Try executing a system command.
  66. </synopsis>
  67. <syntax>
  68. <parameter name="command" required="true">
  69. <para>Command to execute</para>
  70. </parameter>
  71. </syntax>
  72. <description>
  73. <para>Executes a command by using system().</para>
  74. <para>Result of execution is returned in the <variable>SYSTEMSTATUS</variable> channel variable:</para>
  75. <variablelist>
  76. <variable name="SYSTEMSTATUS">
  77. <value name="FAILURE">
  78. Could not execute the specified command.
  79. </value>
  80. <value name="SUCCESS">
  81. Specified command successfully executed.
  82. </value>
  83. <value name="APPERROR">
  84. Specified command successfully executed, but returned error code.
  85. </value>
  86. </variable>
  87. </variablelist>
  88. </description>
  89. </application>
  90. ***/
  91. AST_THREADSTORAGE(buf_buf);
  92. static char *app = "System";
  93. static char *app2 = "TrySystem";
  94. static char *chanvar = "SYSTEMSTATUS";
  95. static int system_exec_helper(struct ast_channel *chan, const char *data, int failmode)
  96. {
  97. int res = 0;
  98. struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
  99. char *cbuf;
  100. if (ast_strlen_zero(data)) {
  101. ast_log(LOG_WARNING, "System requires an argument(command)\n");
  102. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  103. return failmode;
  104. }
  105. ast_autoservice_start(chan);
  106. /* Do our thing here */
  107. ast_str_get_encoded_str(&buf, 0, (char *) data);
  108. cbuf = ast_str_buffer(buf);
  109. if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
  110. cbuf[ast_str_strlen(buf) - 1] = '\0';
  111. cbuf++;
  112. ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
  113. }
  114. res = ast_safe_system(cbuf);
  115. if ((res < 0) && (errno != ECHILD)) {
  116. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  117. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  118. res = failmode;
  119. } else if (res == 127) {
  120. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  121. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  122. res = failmode;
  123. } else {
  124. if (res < 0)
  125. res = 0;
  126. if (res != 0)
  127. pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
  128. else
  129. pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
  130. res = 0;
  131. }
  132. ast_autoservice_stop(chan);
  133. return res;
  134. }
  135. static int system_exec(struct ast_channel *chan, const char *data)
  136. {
  137. return system_exec_helper(chan, data, -1);
  138. }
  139. static int trysystem_exec(struct ast_channel *chan, const char *data)
  140. {
  141. return system_exec_helper(chan, data, 0);
  142. }
  143. static int unload_module(void)
  144. {
  145. int res;
  146. res = ast_unregister_application(app);
  147. res |= ast_unregister_application(app2);
  148. return res;
  149. }
  150. static int load_module(void)
  151. {
  152. int res;
  153. res = ast_register_application_xml(app2, trysystem_exec);
  154. res |= ast_register_application_xml(app, system_exec);
  155. return res;
  156. }
  157. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");