app_system.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Execute arbitrary system commands
  5. *
  6. * Copyright (C) 1999-2004, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/app.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. static char *tdesc = "Generic System() application";
  26. static char *app = "System";
  27. static char *app2 = "TrySystem";
  28. static char *synopsis = "Execute a system command";
  29. static char *synopsis2 = "Try executing a system command";
  30. static char *descrip =
  31. " System(command): Executes a command by using system(). Returns -1 on\n"
  32. "failure to execute the specified command. If the command itself executes\n"
  33. "but is in error, and if there exists a priority n + 101, where 'n' is the\n"
  34. "priority of the current instance, then the channel will be setup to\n"
  35. "continue at that priority level. Otherwise, System returns 0.\n";
  36. static char *descrip2 =
  37. " TrySystem(command): Executes a command by using system(). Returns 0\n"
  38. "on any situation. If the command itself executes but is in error, and if\n"
  39. "there exists a priority n + 101, where 'n' is the priority of the current\n"
  40. "instance, then the channel will be setup to continue at that\n"
  41. "priority level. Otherwise, System returns 0.\n";
  42. STANDARD_LOCAL_USER;
  43. LOCAL_USER_DECL;
  44. static int system_exec_helper(struct ast_channel *chan, void *data, int failmode)
  45. {
  46. int res=0;
  47. struct localuser *u;
  48. if (!data) {
  49. ast_log(LOG_WARNING, "System requires an argument(command)\n");
  50. return failmode;
  51. }
  52. LOCAL_USER_ADD(u);
  53. /* Do our thing here */
  54. res = ast_safe_system((char *)data);
  55. if ((res < 0) && (errno != ECHILD)) {
  56. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  57. res = failmode;
  58. } else if (res == 127) {
  59. ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data);
  60. res = failmode;
  61. } else {
  62. if (res < 0)
  63. res = 0;
  64. if (res && ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
  65. chan->priority+=100;
  66. res = 0;
  67. }
  68. LOCAL_USER_REMOVE(u);
  69. return res;
  70. }
  71. static int system_exec(struct ast_channel *chan, void *data)
  72. {
  73. return system_exec_helper(chan, data, -1);
  74. }
  75. static int trysystem_exec(struct ast_channel *chan, void *data)
  76. {
  77. return system_exec_helper(chan, data, 0);
  78. }
  79. int unload_module(void)
  80. {
  81. STANDARD_HANGUP_LOCALUSERS;
  82. ast_unregister_application(app2);
  83. return ast_unregister_application(app);
  84. }
  85. int load_module(void)
  86. {
  87. ast_register_application(app2, trysystem_exec, synopsis2, descrip2);
  88. return ast_register_application(app, system_exec, synopsis, descrip);
  89. }
  90. char *description(void)
  91. {
  92. return tdesc;
  93. }
  94. int usecount(void)
  95. {
  96. int res;
  97. STANDARD_USECOUNT(res);
  98. return res;
  99. }
  100. char *key()
  101. {
  102. return ASTERISK_GPL_KEY;
  103. }