io.c 8.8 KB

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