alertpipe.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef ASTERISK_ALERTPIPE_H
  19. #define ASTERISK_ALERTPIPE_H
  20. #include "asterisk/utils.h"
  21. typedef enum {
  22. AST_ALERT_READ_SUCCESS = 0,
  23. AST_ALERT_NOT_READABLE,
  24. AST_ALERT_READ_FAIL,
  25. AST_ALERT_READ_FATAL,
  26. } ast_alert_status_t;
  27. /*!
  28. * \brief Initialize an alert pipe
  29. * \since 13.16.0
  30. *
  31. * \param p a two-element array to hold the alert pipe's file descriptors
  32. *
  33. * \return non-zero if a failure occurred, zero otherwise.
  34. */
  35. int ast_alertpipe_init(int alert_pipe[2]);
  36. /*!
  37. * \brief Close an alert pipe
  38. * \since 13.16.0
  39. *
  40. * \param p a two-element containing the alert pipe's file descriptors
  41. */
  42. void ast_alertpipe_close(int alert_pipe[2]);
  43. /*!
  44. * \brief Read an event from an alert pipe
  45. * \since 13.16.0
  46. *
  47. * \param p a two-element array containing the alert pipe's file descriptors
  48. *
  49. * \retval AST_ALERT_READ_SUCCESS on success
  50. * \retval AST_ALERT_NOT_READABLE if the alert pipe is not readable
  51. * \retval AST_ALERT_READ_FATAL if the alert pipe's file descriptors are in
  52. * blocking mode, or a read error occurs.
  53. */
  54. ast_alert_status_t ast_alertpipe_read(int alert_pipe[2]);
  55. /*!
  56. * \brief Write an event to an alert pipe
  57. * \since 13.16.0
  58. *
  59. * \param p a two-element array containing the alert pipe's file descriptors
  60. *
  61. * \retval 0 Success
  62. * \retval 1 Failure
  63. */
  64. ssize_t ast_alertpipe_write(int alert_pipe[2]);
  65. /*!
  66. * \brief Consume all alerts written to the alert pipe
  67. * \since 13.16.0
  68. *
  69. * \param p a two-element array containing the alert pipe's file descriptors
  70. *
  71. * \retval AST_ALERT_READ_SUCCESS on success
  72. * \retval AST_ALERT_NOT_READABLE if the alert pipe is not readable
  73. * \retval AST_ALERT_READ_FATAL if the alert pipe's file descriptors are in
  74. * blocking mode, or a read error occurs.
  75. */
  76. ast_alert_status_t ast_alertpipe_flush(int alert_pipe[2]);
  77. /*!
  78. * \brief Sets the alert pipe file descriptors to default values
  79. * \since 13.16.0
  80. *
  81. * \param p a two-element array containing the alert pipe's file descriptors
  82. */
  83. AST_INLINE_API(
  84. void ast_alertpipe_clear(int alert_pipe[2]),
  85. {
  86. alert_pipe[0] = alert_pipe[1] = -1;
  87. }
  88. )
  89. /*!
  90. * \brief Determine if the alert pipe is readable
  91. * \since 13.16.0
  92. *
  93. * \param p a two-element array containing the alert pipe's file descriptors
  94. *
  95. * \return non-zero if the alert pipe is readable, zero otherwise.
  96. */
  97. AST_INLINE_API(
  98. int attribute_pure ast_alertpipe_readable(int alert_pipe[2]),
  99. {
  100. return alert_pipe[0] > -1;
  101. }
  102. )
  103. /*!
  104. * \brief Determine if the alert pipe is writable
  105. * \since 13.16.0
  106. *
  107. * \param p a two-element array containing the alert pipe's file descriptors
  108. *
  109. * \return non-zero if the alert pipe is writable, zero otherwise.
  110. */
  111. AST_INLINE_API(
  112. int attribute_pure ast_alertpipe_writable(int alert_pipe[2]),
  113. {
  114. return alert_pipe[1] > -1;
  115. }
  116. )
  117. /*!
  118. * \brief Get the alert pipe's read file descriptor
  119. * \since 13.16.0
  120. *
  121. * \param p a two-element array containing the alert pipe's file descriptors
  122. *
  123. * \return -1 if the file descriptor is not initialized, a non-negative value
  124. * otherwise.
  125. */
  126. AST_INLINE_API(
  127. int attribute_pure ast_alertpipe_readfd(int alert_pipe[2]),
  128. {
  129. return alert_pipe[0];
  130. }
  131. )
  132. /*!
  133. * \brief Swap the file descriptors from two alert pipes
  134. * \since 13.16.0
  135. *
  136. * \param p1 a two-element array containing an alert pipe's file descriptors
  137. * \param p2 a two-element array containing an alert pipe's file descriptors
  138. */
  139. AST_INLINE_API(
  140. void ast_alertpipe_swap(int alert_pipe_1[2], int alert_pipe_2[2]),
  141. {
  142. SWAP(alert_pipe_1[0], alert_pipe_2[0]);
  143. SWAP(alert_pipe_1[1], alert_pipe_2[1]);
  144. }
  145. )
  146. #endif /* ASTERISK_ALERTPIPE_H */