io.c 8.7 KB

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