io.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * \brief I/O Management (derived from Cheops-NG)
  20. */
  21. #ifndef _ASTERISK_IO_H
  22. #define _ASTERISK_IO_H
  23. #include "asterisk/poll-compat.h"
  24. #if defined(__cplusplus) || defined(c_plusplus)
  25. extern "C" {
  26. #endif
  27. /*! Input ready */
  28. #define AST_IO_IN POLLIN
  29. /*! Output ready */
  30. #define AST_IO_OUT POLLOUT
  31. /*! Priority input ready */
  32. #define AST_IO_PRI POLLPRI
  33. /* Implicitly polled for */
  34. /*! Error condition (errno or getsockopt) */
  35. #define AST_IO_ERR POLLERR
  36. /*! Hangup */
  37. #define AST_IO_HUP POLLHUP
  38. /*! Invalid fd */
  39. #define AST_IO_NVAL POLLNVAL
  40. /*! \brief
  41. * An Asterisk IO callback takes its id, a file descriptor, list of events, and
  42. * callback data as arguments and returns 0 if it should not be
  43. * run again, or non-zero if it should be run again.
  44. */
  45. struct io_context;
  46. /*!
  47. * \brief Creates a context
  48. * Create a context for I/O operations
  49. * Basically mallocs an IO structure and sets up some default values.
  50. * \return an allocated io_context structure
  51. */
  52. struct io_context *io_context_create(void);
  53. /*!
  54. * \brief Destroys a context
  55. * \param ioc structure to destroy
  56. * Destroy a context for I/O operations
  57. * Frees all memory associated with the given io_context structure along with the structure itself
  58. */
  59. void io_context_destroy(struct io_context *ioc);
  60. typedef int (*ast_io_cb)(int *id, int fd, short events, void *cbdata);
  61. #define AST_IO_CB(a) ((ast_io_cb)(a))
  62. /*!
  63. * \brief Adds an IO context
  64. * \param ioc which context to use
  65. * \param fd which fd to monitor
  66. * \param callback callback function to run
  67. * \param events event mask of events to wait for
  68. * \param data data to pass to the callback
  69. * Watch for any of revents activites on fd, calling callback with data as
  70. * callback data.
  71. * \retval a pointer to ID of the IO event
  72. * \retval NULL on failure
  73. */
  74. int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data);
  75. /*!
  76. * \brief Changes an IO handler
  77. * \param ioc which context to use
  78. * \param id
  79. * \param fd the fd you wish it to contain now
  80. * \param callback new callback function
  81. * \param events event mask to wait for
  82. * \param data data to pass to the callback function
  83. * Change an I/O handler, updating fd if > -1, callback if non-null,
  84. * and revents if >-1, and data if non-null.
  85. * \retval a pointer to the ID of the IO event
  86. * \retval NULL on failure
  87. */
  88. int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data);
  89. /*!
  90. * \brief Removes an IO context
  91. * \param ioc which io_context to remove it from
  92. * \param id which ID to remove
  93. * Remove an I/O id from consideration
  94. * \retval 0 on success
  95. * \retval -1 on failure
  96. */
  97. int ast_io_remove(struct io_context *ioc, int *id);
  98. /*!
  99. * \brief Waits for IO
  100. * \param ioc which context to act upon
  101. * \param howlong how many milliseconds to wait
  102. * Wait for I/O to happen, returning after
  103. * howlong milliseconds, and after processing
  104. * any necessary I/O.
  105. * \return he number of I/O events which took place.
  106. */
  107. int ast_io_wait(struct io_context *ioc, int howlong);
  108. /*!
  109. * \brief Dumps the IO array.
  110. * Debugging: Dump everything in the I/O array
  111. */
  112. void ast_io_dump(struct io_context *ioc);
  113. /*! Set fd into non-echoing mode (if fd is a tty) */
  114. int ast_hide_password(int fd);
  115. /*!
  116. * \brief Restores TTY mode.
  117. * Call with result from previous ast_hide_password
  118. */
  119. int ast_restore_tty(int fd, int oldstatus);
  120. int ast_get_termcols(int fd);
  121. /*!
  122. * \brief a wrapper for sd_notify(): notify systemd of any state changes.
  123. * \param state a string that states the changes. See sd_notify(3).
  124. * The wrapper does nothing if systemd ('s development headers) was not
  125. * detected on the system.
  126. * \returns >=0 on success, negative value on error.
  127. */
  128. int ast_sd_notify(const char *state);
  129. #if defined(__cplusplus) || defined(c_plusplus)
  130. }
  131. #endif
  132. #endif /* _ASTERISK_IO_H */