io.c 8.9 KB

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