io.c 8.2 KB

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