alertpipe.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2017, Sean Bright
  5. *
  6. * Sean Bright <sean.bright@gmail.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 Alert Pipe API
  21. *
  22. * \author Sean Bright
  23. */
  24. #include "asterisk.h"
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #ifdef HAVE_EVENTFD
  28. # include <sys/eventfd.h>
  29. #endif
  30. #include "asterisk/alertpipe.h"
  31. #include "asterisk/logger.h"
  32. int ast_alertpipe_init(int alert_pipe[2])
  33. {
  34. #ifdef HAVE_EVENTFD
  35. int fd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE);
  36. if (fd > -1) {
  37. alert_pipe[0] = alert_pipe[1] = fd;
  38. return 0;
  39. }
  40. ast_log(LOG_WARNING, "Failed to create alert pipe with eventfd(), falling back to pipe(): %s\n",
  41. strerror(errno));
  42. ast_alertpipe_clear(alert_pipe);
  43. #endif
  44. #ifdef HAVE_PIPE2
  45. if (pipe2(alert_pipe, O_NONBLOCK)) {
  46. ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno));
  47. return -1;
  48. }
  49. #else
  50. if (pipe(alert_pipe)) {
  51. ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno));
  52. return -1;
  53. } else {
  54. if (ast_fd_set_flags(alert_pipe[0], O_NONBLOCK)
  55. || ast_fd_set_flags(alert_pipe[1], O_NONBLOCK)) {
  56. ast_alertpipe_close(alert_pipe);
  57. return -1;
  58. }
  59. }
  60. #endif
  61. return 0;
  62. }
  63. void ast_alertpipe_close(int alert_pipe[2])
  64. {
  65. #ifdef HAVE_EVENTFD
  66. if (alert_pipe[0] == alert_pipe[1]) {
  67. if (alert_pipe[0] > -1) {
  68. close(alert_pipe[0]);
  69. ast_alertpipe_clear(alert_pipe);
  70. }
  71. return;
  72. }
  73. #endif
  74. if (alert_pipe[0] > -1) {
  75. close(alert_pipe[0]);
  76. }
  77. if (alert_pipe[1] > -1) {
  78. close(alert_pipe[1]);
  79. }
  80. ast_alertpipe_clear(alert_pipe);
  81. }
  82. ast_alert_status_t ast_alertpipe_read(int alert_pipe[2])
  83. {
  84. uint64_t tmp;
  85. if (!ast_alertpipe_readable(alert_pipe)) {
  86. return AST_ALERT_NOT_READABLE;
  87. }
  88. if (read(alert_pipe[0], &tmp, sizeof(tmp)) < 0) {
  89. if (errno != EINTR && errno != EAGAIN) {
  90. ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
  91. return AST_ALERT_READ_FAIL;
  92. }
  93. }
  94. return AST_ALERT_READ_SUCCESS;
  95. }
  96. ssize_t ast_alertpipe_write(int alert_pipe[2])
  97. {
  98. uint64_t tmp = 1;
  99. if (!ast_alertpipe_writable(alert_pipe)) {
  100. errno = EBADF;
  101. return 0;
  102. }
  103. /* preset errno in case returned size does not match */
  104. errno = EPIPE;
  105. return write(alert_pipe[1], &tmp, sizeof(tmp)) != sizeof(tmp);
  106. }
  107. ast_alert_status_t ast_alertpipe_flush(int alert_pipe[2])
  108. {
  109. int bytes_read;
  110. uint64_t tmp[16];
  111. if (!ast_alertpipe_readable(alert_pipe)) {
  112. return AST_ALERT_NOT_READABLE;
  113. }
  114. /* Read the alertpipe until it is exhausted. */
  115. for (;;) {
  116. bytes_read = read(alert_pipe[0], tmp, sizeof(tmp));
  117. if (bytes_read < 0) {
  118. if (errno == EINTR) {
  119. continue;
  120. }
  121. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  122. /*
  123. * Would block so nothing left to read.
  124. * This is the normal loop exit.
  125. */
  126. break;
  127. }
  128. ast_log(LOG_WARNING, "read() failed flushing alertpipe: %s\n",
  129. strerror(errno));
  130. return AST_ALERT_READ_FAIL;
  131. }
  132. if (!bytes_read) {
  133. /* Read nothing so we are done */
  134. break;
  135. }
  136. }
  137. return AST_ALERT_READ_SUCCESS;
  138. }