ivrd.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2009-2012, Anthony Minessale II
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of the original author; nor the names of any contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  25. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <esl.h>
  36. #include <errno.h>
  37. #include <sys/wait.h>
  38. #include <signal.h>
  39. static void handle_SIGCHLD(int sig)
  40. {
  41. int status = 0;
  42. wait(&status);
  43. return;
  44. }
  45. static void my_forking_callback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
  46. {
  47. esl_handle_t handle = {{0}};
  48. char path_buffer[1024] = { 0 };
  49. const char *path;
  50. char arg[64] = { 0 };
  51. signal(SIGCHLD, handle_SIGCHLD);
  52. if (fork()) {
  53. close(client_sock);
  54. return;
  55. }
  56. if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS || !handle.info_event) {
  57. esl_log(ESL_LOG_ERROR, "Socket Error\n");
  58. exit(0);
  59. }
  60. if (!(path = esl_event_get_header(handle.info_event, "variable_ivr_path"))) {
  61. esl_disconnect(&handle);
  62. esl_log(ESL_LOG_ERROR, "Missing ivr_path param!\n");
  63. exit(0);
  64. }
  65. snprintf(arg, sizeof(arg), "%d", client_sock);
  66. strncpy(path_buffer, path, sizeof(path_buffer) - 1);
  67. /* hotwire the socket to STDIN/STDOUT */
  68. /* hotwire the socket to STDIN/STDOUT */
  69. if (!(dup2(client_sock, STDIN_FILENO)) && !(dup2(client_sock, STDOUT_FILENO))){
  70. esl_disconnect(&handle);
  71. esl_log(ESL_LOG_ERROR, "Socket Error hotwiring socket to STDIN/STDOUT!\n");
  72. return;
  73. }
  74. /* close the handle but leak the socket on purpose cos the child will need it open */
  75. handle.sock = -1;
  76. esl_disconnect(&handle);
  77. execl(path_buffer, path_buffer, arg, (char *)NULL);
  78. close(client_sock);
  79. exit(0);
  80. }
  81. static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
  82. {
  83. esl_handle_t handle = {{0}};
  84. const char *path;
  85. char path_buffer[1024] = { 0 };
  86. if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS || !handle.info_event) {
  87. close(client_sock);
  88. esl_log(ESL_LOG_ERROR, "Socket Error\n");
  89. return;
  90. }
  91. if (!(path = esl_event_get_header(handle.info_event, "variable_ivr_path"))) {
  92. esl_disconnect(&handle);
  93. esl_log(ESL_LOG_ERROR, "Missing ivr_path param!\n");
  94. return;
  95. }
  96. snprintf(path_buffer, sizeof(path_buffer), "%s %d", path, client_sock);
  97. if (system(path_buffer)) {
  98. esl_log(ESL_LOG_ERROR, "System Call Failed! [%s]\n", strerror(errno));
  99. }
  100. esl_disconnect(&handle);
  101. }
  102. int main(int argc, char *argv[])
  103. {
  104. int i;
  105. char *ip = NULL;
  106. int port = 0, thread = 0;
  107. for (i = 1; i < argc; ) {
  108. int cont = 0;
  109. if (i + 1 < argc) {
  110. if (!strcasecmp(argv[i], "-h")) {
  111. ip = argv[++i]; cont++;
  112. } else if (!strcasecmp(argv[i], "-p")) {
  113. port = atoi(argv[++i]); cont++;
  114. }
  115. }
  116. if (cont) {
  117. i++;
  118. continue;
  119. }
  120. if (!strcasecmp(argv[i], "-t")) {
  121. thread++;
  122. }
  123. i++;
  124. }
  125. if (!(ip && port)) {
  126. fprintf(stderr, "Usage %s [-t] -h <host> -p <port>\n", argv[0]);
  127. return -1;
  128. }
  129. /*
  130. * NOTE: fflush() stdout before entering esl_listen().
  131. * Not doing so causes the fork()ed child to repeat the message for every incoming
  132. * connection, if stdout has been redirected (into a logfile, for example).
  133. */
  134. if (thread) {
  135. printf("Starting threaded listener.\n");
  136. fflush(stdout);
  137. esl_listen_threaded(ip, port, mycallback, NULL, 100000);
  138. } else {
  139. printf("Starting forking listener.\n");
  140. fflush(stdout);
  141. esl_listen(ip, port, my_forking_callback, NULL, NULL);
  142. }
  143. return 0;
  144. }