io.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. *
  20. * \brief I/O Managment (Derived from Cheops-NG)
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <termios.h>
  30. #include <sys/ioctl.h>
  31. #include "asterisk/io.h"
  32. #include "asterisk/utils.h"
  33. #ifdef DEBUG_IO
  34. #define DEBUG DEBUG_M
  35. #else
  36. #define DEBUG(a)
  37. #endif
  38. /*! \brief
  39. * Kept for each file descriptor
  40. */
  41. struct io_rec {
  42. ast_io_cb callback; /*!< What is to be called */
  43. void *data; /*!< Data to be passed */
  44. int *id; /*!< ID number */
  45. };
  46. /* These two arrays are keyed with
  47. the same index. it's too bad that
  48. pollfd doesn't have a callback field
  49. or something like that. They grow as
  50. needed, by GROW_SHRINK_SIZE structures
  51. at once */
  52. #define GROW_SHRINK_SIZE 512
  53. /*! \brief Global IO variables are now in a struct in order to be
  54. made threadsafe */
  55. struct io_context {
  56. struct pollfd *fds; /*!< Poll structure */
  57. struct io_rec *ior; /*!< Associated I/O records */
  58. unsigned int fdcnt; /*!< First available fd */
  59. unsigned int maxfdcnt; /*!< Maximum available fd */
  60. int current_ioc; /*!< Currently used io callback */
  61. int needshrink; /*!< Whether something has been deleted */
  62. };
  63. /*! \brief Create an I/O context */
  64. struct io_context *io_context_create(void)
  65. {
  66. struct io_context *tmp = NULL;
  67. if (!(tmp = ast_malloc(sizeof(*tmp))))
  68. return NULL;
  69. tmp->needshrink = 0;
  70. tmp->fdcnt = 0;
  71. tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
  72. tmp->current_ioc = -1;
  73. if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) {
  74. ast_free(tmp);
  75. tmp = NULL;
  76. } else {
  77. if (!(tmp->ior = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->ior)))) {
  78. ast_free(tmp->fds);
  79. ast_free(tmp);
  80. tmp = NULL;
  81. }
  82. }
  83. return tmp;
  84. }
  85. void io_context_destroy(struct io_context *ioc)
  86. {
  87. /* Free associated memory with an I/O context */
  88. if (ioc->fds)
  89. ast_free(ioc->fds);
  90. if (ioc->ior)
  91. ast_free(ioc->ior);
  92. ast_free(ioc);
  93. }
  94. /*! \brief
  95. * Grow the size of our arrays.
  96. * \return 0 on success or -1 on failure
  97. */
  98. static int io_grow(struct io_context *ioc)
  99. {
  100. void *tmp;
  101. DEBUG(ast_debug(1, "io_grow()\n"));
  102. ioc->maxfdcnt += GROW_SHRINK_SIZE;
  103. if ((tmp = ast_realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(*ioc->ior)))) {
  104. ioc->ior = tmp;
  105. if ((tmp = ast_realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(*ioc->fds)))) {
  106. ioc->fds = tmp;
  107. } else {
  108. /*
  109. * Failed to allocate enough memory for the pollfd. Not
  110. * really any need to shrink back the iorec's as we'll
  111. * probably want to grow them again soon when more memory
  112. * is available, and then they'll already be the right size
  113. */
  114. ioc->maxfdcnt -= GROW_SHRINK_SIZE;
  115. return -1;
  116. }
  117. } else {
  118. /*
  119. * Memory allocation failure. We return to the old size, and
  120. * return a failure
  121. */
  122. ioc->maxfdcnt -= GROW_SHRINK_SIZE;
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. /*! \brief
  128. * Add a new I/O entry for this file descriptor
  129. * with the given event mask, to call callback with
  130. * data as an argument.
  131. * \return Returns NULL on failure.
  132. */
  133. int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
  134. {
  135. int *ret;
  136. DEBUG(ast_debug(1, "ast_io_add()\n"));
  137. if (ioc->fdcnt >= ioc->maxfdcnt) {
  138. /*
  139. * We don't have enough space for this entry. We need to
  140. * reallocate maxfdcnt poll fd's and io_rec's, or back out now.
  141. */
  142. if (io_grow(ioc))
  143. return NULL;
  144. }
  145. /*
  146. * At this point, we've got sufficiently large arrays going
  147. * and we can make an entry for it in the pollfd and io_r
  148. * structures.
  149. */
  150. ioc->fds[ioc->fdcnt].fd = fd;
  151. ioc->fds[ioc->fdcnt].events = events;
  152. ioc->fds[ioc->fdcnt].revents = 0;
  153. ioc->ior[ioc->fdcnt].callback = callback;
  154. ioc->ior[ioc->fdcnt].data = data;
  155. if (!(ioc->ior[ioc->fdcnt].id = ast_malloc(sizeof(*ioc->ior[ioc->fdcnt].id)))) {
  156. /* Bonk if we couldn't allocate an int */
  157. return NULL;
  158. }
  159. *(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt;
  160. ret = ioc->ior[ioc->fdcnt].id;
  161. ioc->fdcnt++;
  162. return ret;
  163. }
  164. int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data)
  165. {
  166. /* If this id exceeds our file descriptor count it doesn't exist here */
  167. if (*id > ioc->fdcnt)
  168. return NULL;
  169. if (fd > -1)
  170. ioc->fds[*id].fd = fd;
  171. if (callback)
  172. ioc->ior[*id].callback = callback;
  173. if (events)
  174. ioc->fds[*id].events = events;
  175. if (data)
  176. ioc->ior[*id].data = data;
  177. return id;
  178. }
  179. static int io_shrink(struct io_context *ioc)
  180. {
  181. int getfrom, putto = 0;
  182. /*
  183. * Bring the fields from the very last entry to cover over
  184. * the entry we are removing, then decrease the size of the
  185. * arrays by one.
  186. */
  187. for (getfrom = 0; getfrom < ioc->fdcnt; getfrom++) {
  188. if (ioc->ior[getfrom].id) {
  189. /* In use, save it */
  190. if (getfrom != putto) {
  191. ioc->fds[putto] = ioc->fds[getfrom];
  192. ioc->ior[putto] = ioc->ior[getfrom];
  193. *(ioc->ior[putto].id) = putto;
  194. }
  195. putto++;
  196. }
  197. }
  198. ioc->fdcnt = putto;
  199. ioc->needshrink = 0;
  200. /* FIXME: We should free some memory if we have lots of unused
  201. io structs */
  202. return 0;
  203. }
  204. int ast_io_remove(struct io_context *ioc, int *_id)
  205. {
  206. int x;
  207. if (!_id) {
  208. ast_log(LOG_WARNING, "Asked to remove NULL?\n");
  209. return -1;
  210. }
  211. for (x = 0; x < ioc->fdcnt; x++) {
  212. if (ioc->ior[x].id == _id) {
  213. /* Free the int immediately and set to NULL so we know it's unused now */
  214. ast_free(ioc->ior[x].id);
  215. ioc->ior[x].id = NULL;
  216. ioc->fds[x].events = 0;
  217. ioc->fds[x].revents = 0;
  218. ioc->needshrink = 1;
  219. if (ioc->current_ioc == -1)
  220. io_shrink(ioc);
  221. return 0;
  222. }
  223. }
  224. ast_log(LOG_NOTICE, "Unable to remove unknown id %p\n", _id);
  225. return -1;
  226. }
  227. /*! \brief
  228. * Make the poll call, and call
  229. * the callbacks for anything that needs
  230. * to be handled
  231. */
  232. int ast_io_wait(struct io_context *ioc, int howlong)
  233. {
  234. int res, x, origcnt;
  235. DEBUG(ast_debug(1, "ast_io_wait()\n"));
  236. if ((res = ast_poll(ioc->fds, ioc->fdcnt, howlong)) <= 0) {
  237. return res;
  238. }
  239. /* At least one event tripped */
  240. origcnt = ioc->fdcnt;
  241. for (x = 0; x < origcnt; x++) {
  242. /* Yes, it is possible for an entry to be deleted and still have an
  243. event waiting if it occurs after the original calling id */
  244. if (ioc->fds[x].revents && ioc->ior[x].id) {
  245. /* There's an event waiting */
  246. ioc->current_ioc = *ioc->ior[x].id;
  247. if (ioc->ior[x].callback) {
  248. if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) {
  249. /* Time to delete them since they returned a 0 */
  250. ast_io_remove(ioc, ioc->ior[x].id);
  251. }
  252. }
  253. ioc->current_ioc = -1;
  254. }
  255. }
  256. if (ioc->needshrink)
  257. io_shrink(ioc);
  258. return res;
  259. }
  260. void ast_io_dump(struct io_context *ioc)
  261. {
  262. /*
  263. * Print some debugging information via
  264. * the logger interface
  265. */
  266. int x;
  267. ast_debug(1, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt);
  268. ast_debug(1, "================================================\n");
  269. ast_debug(1, "| ID FD Callback Data Events |\n");
  270. ast_debug(1, "+------+------+-----------+-----------+--------+\n");
  271. for (x = 0; x < ioc->fdcnt; x++) {
  272. ast_debug(1, "| %.4d | %.4d | %p | %p | %.6x |\n",
  273. *ioc->ior[x].id,
  274. ioc->fds[x].fd,
  275. ioc->ior[x].callback,
  276. ioc->ior[x].data,
  277. ioc->fds[x].events);
  278. }
  279. ast_debug(1, "================================================\n");
  280. }
  281. /* Unrelated I/O functions */
  282. int ast_hide_password(int fd)
  283. {
  284. struct termios tios;
  285. int res;
  286. int old;
  287. if (!isatty(fd))
  288. return -1;
  289. res = tcgetattr(fd, &tios);
  290. if (res < 0)
  291. return -1;
  292. old = tios.c_lflag & (ECHO | ECHONL);
  293. tios.c_lflag &= ~ECHO;
  294. tios.c_lflag |= ECHONL;
  295. res = tcsetattr(fd, TCSAFLUSH, &tios);
  296. if (res < 0)
  297. return -1;
  298. return old;
  299. }
  300. int ast_restore_tty(int fd, int oldstate)
  301. {
  302. int res;
  303. struct termios tios;
  304. if (oldstate < 0)
  305. return 0;
  306. res = tcgetattr(fd, &tios);
  307. if (res < 0)
  308. return -1;
  309. tios.c_lflag &= ~(ECHO | ECHONL);
  310. tios.c_lflag |= oldstate;
  311. res = tcsetattr(fd, TCSAFLUSH, &tios);
  312. if (res < 0)
  313. return -1;
  314. return 0;
  315. }
  316. int ast_get_termcols(int fd)
  317. {
  318. struct winsize win;
  319. int cols = 0;
  320. if (!isatty(fd))
  321. return -1;
  322. if ( ioctl(fd, TIOCGWINSZ, &win) != -1 ) {
  323. if ( !cols && win.ws_col > 0 )
  324. cols = (int) win.ws_col;
  325. } else {
  326. /* assume 80 characters if the ioctl fails for some reason */
  327. cols = 80;
  328. }
  329. return cols;
  330. }