app_system.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/pbx.h"
  29. #include "asterisk/module.h"
  30. #include "asterisk/app.h"
  31. #include "asterisk/channel.h" /* autoservice */
  32. #include "asterisk/strings.h"
  33. #include "asterisk/threadstorage.h"
  34. AST_THREADSTORAGE(buf_buf);
  35. static char *app = "System";
  36. static char *app2 = "TrySystem";
  37. static char *synopsis = "Execute a system command";
  38. static char *synopsis2 = "Try executing a system command";
  39. static char *chanvar = "SYSTEMSTATUS";
  40. static char *descrip =
  41. " System(command): Executes a command by using system(). If the command\n"
  42. "fails, the console should report a fallthrough. \n"
  43. "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
  44. " FAILURE Could not execute the specified command\n"
  45. " SUCCESS Specified command successfully executed\n";
  46. static char *descrip2 =
  47. " TrySystem(command): Executes a command by using system().\n"
  48. "on any situation.\n"
  49. "Result of execution is returned in the SYSTEMSTATUS channel variable:\n"
  50. " FAILURE Could not execute the specified command\n"
  51. " SUCCESS Specified command successfully executed\n"
  52. " APPERROR Specified command successfully executed, but returned error code\n";
  53. static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
  54. {
  55. int res = 0;
  56. struct ast_str *buf = ast_str_thread_get(&buf_buf, 16);
  57. char *cbuf;
  58. if (ast_strlen_zero(data)) {
  59. ast_log(LOG_WARNING, "System requires an argument(command)\n");
  60. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  61. return failmode;
  62. }
  63. ast_autoservice_start(chan);
  64. /* Do our thing here */
  65. ast_str_get_encoded_str(&buf, 0, (char *) data);
  66. cbuf = ast_str_buffer(buf);
  67. if (strchr("\"'", cbuf[0]) && cbuf[ast_str_strlen(buf) - 1] == cbuf[0]) {
  68. cbuf[ast_str_strlen(buf) - 1] = '\0';
  69. cbuf++;
  70. ast_log(LOG_NOTICE, "It is not necessary to quote the argument to the System application.\n");
  71. }
  72. res = ast_safe_system(cbuf);
  73. if ((res < 0) && (errno != ECHILD)) {
  74. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  75. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  76. res = failmode;
  77. } else if (res == 127) {
  78. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  79. pbx_builtin_setvar_helper(chan, chanvar, "FAILURE");
  80. res = failmode;
  81. } else {
  82. if (res < 0)
  83. res = 0;
  84. if (res != 0)
  85. pbx_builtin_setvar_helper(chan, chanvar, "APPERROR");
  86. else
  87. pbx_builtin_setvar_helper(chan, chanvar, "SUCCESS");
  88. res = 0;
  89. }
  90. ast_autoservice_stop(chan);
  91. return res;
  92. }
  93. static int system_exec(struct ast_channel *chan, void *data)
  94. {
  95. return system_exec_helper(chan, data, -1);
  96. }
  97. static int trysystem_exec(struct ast_channel *chan, void *data)
  98. {
  99. return system_exec_helper(chan, data, 0);
  100. }
  101. static int unload_module(void)
  102. {
  103. int res;
  104. res = ast_unregister_application(app);
  105. res |= ast_unregister_application(app2);
  106. return res;
  107. }
  108. static int load_module(void)
  109. {
  110. int res;
  111. res = ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
  112. res |= ast_register_application(app, system_exec, synopsis, descrip);
  113. return res;
  114. }
  115. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Generic System() application");