4 * Mark Spencer <markster@marko.net>
6 * Copyright(C) Mark Spencer
8 * Distributed under the terms of the GNU General Public License (GPL) Version 2
10 * I/O Managment (Derived from Cheops-NG)
19 #include <asterisk/io.h>
20 #include <asterisk/logger.h>
29 * Kept for each file descriptor
32 ast_io_cb callback; /* What is to be called */
33 void *data; /* Data to be passed */
34 int *id; /* ID number */
37 /* These two arrays are keyed with
38 the same index. it's too bad that
39 pollfd doesn't have a callback field
40 or something like that. They grow as
41 needed, by GROW_SHRINK_AMOUNT structures
44 #define GROW_SHRINK_SIZE 512
46 /* Global variables are now in a struct in order to be
51 /* Associated I/O records */
53 /* First available fd */
55 /* Maximum available fd */
56 unsigned int maxfdcnt;
57 /* Currently used io callback */
62 struct io_context *io_context_create(void)
64 /* Create an I/O context */
65 struct io_context *tmp;
66 tmp = malloc(sizeof(struct io_context));
72 tmp->current_ioc = -1;
77 void io_context_destroy(struct io_context *ioc)
79 /* Free associated memory with an I/O context */
87 static int io_grow(struct io_context *ioc)
90 * Grow the size of our arrays. Return 0 on success or
94 DEBUG(ast_log(LOG_DEBUG, "io_grow()\n"));
95 ioc->maxfdcnt += GROW_SHRINK_SIZE;
96 tmp = realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(struct io_rec));
98 ioc->ior = (struct io_rec *)tmp;
99 tmp = realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(struct pollfd));
104 * Not enough memory for the pollfd. Not really any need
105 * to shrink back the iorec's as we'll probably want to
106 * grow them again soon when more memory is available, and
107 * then they'll already be the right size
109 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
115 * Out of memory. We return to the old size, and return a failure
117 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
123 int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
126 * Add a new I/O entry for this file descriptor
127 * with the given event mask, to call callback with
128 * data as an argument. Returns NULL on failure.
130 DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n"));
131 if (ioc->fdcnt < ioc->maxfdcnt) {
133 * We don't have enough space for this entry. We need to
134 * reallocate maxfdcnt poll fd's and io_rec's, or back out now.
141 * At this point, we've got sufficiently large arrays going
142 * and we can make an entry for it in the pollfd and io_r
145 ioc->fds[ioc->fdcnt].fd = fd;
146 ioc->fds[ioc->fdcnt].events = events;
147 ioc->ior[ioc->fdcnt].callback = callback;
148 ioc->ior[ioc->fdcnt].data = data;
149 ioc->ior[ioc->fdcnt].id = (int *)malloc(sizeof(int));
150 /* Bonk if we couldn't allocate an int */
151 if (!ioc->ior[ioc->fdcnt].id)
153 *ioc->ior[ioc->fdcnt].id = ioc->fdcnt;
154 return ioc->ior[ioc->fdcnt++].id;
157 int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data)
159 if (*id < ioc->fdcnt) {
161 ioc->fds[*id].fd = fd;
163 ioc->ior[*id].callback = callback;
165 ioc->fds[*id].events = events;
167 ioc->ior[*id].data = data;
172 static int io_shrink(struct io_context *ioc, int which)
175 * Bring the fields from the very last entry to cover over
176 * the entry we are removing, then decrease the size of the
182 free(ioc->ior[which].id);
184 /* If we're not deleting the last one, move the last one to
185 the current position */
186 if (which != ioc->fdcnt) {
187 ioc->fds[which] = ioc->fds[ioc->fdcnt];
188 ioc->ior[which] = ioc->ior[ioc->fdcnt];
189 *ioc->ior[which].id = which;
191 /* FIXME: We should free some memory if we have lots of unused
196 int ast_io_remove(struct io_context *ioc, int *id)
198 if (ioc->current_ioc == *id) {
199 ast_log(LOG_NOTICE, "Callback for %d tried to remove itself\n", *id);
202 if (*id < ioc->fdcnt) {
203 return io_shrink(ioc, *id);
205 ast_log(LOG_NOTICE, "Unable to remove unknown id %d\n", *id);
210 int ast_io_wait(struct io_context *ioc, int howlong)
213 * Make the poll call, and call
214 * the callbacks for anything that needs
219 DEBUG(ast_log(LOG_DEBUG, "ast_io_wait()\n"));
220 res = poll(ioc->fds, ioc->fdcnt, howlong);
225 for(x=0;x<ioc->fdcnt;x++) {
226 if (ioc->fds[x].revents) {
227 /* There's an event waiting */
228 ioc->current_ioc = *ioc->ior[x].id;
229 if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) {
230 /* Time to delete them since they returned a 0 */
233 ioc->current_ioc = -1;
240 void ast_io_dump(struct io_context *ioc)
243 * Print some debugging information via
244 * the logger interface
247 ast_log(LOG_DEBUG, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt);
248 ast_log(LOG_DEBUG, "================================================\n");
249 ast_log(LOG_DEBUG, "| ID FD Callback Data Events |\n");
250 ast_log(LOG_DEBUG, "+------+------+-----------+-----------+--------+\n");
251 for (x=0;x<ioc->fdcnt;x++) {
252 ast_log(LOG_DEBUG, "| %.4d | %.4d | %p | %p | %.6x |\n",
255 ioc->ior[x].callback,
259 ast_log(LOG_DEBUG, "================================================\n");
262 /* Unrelated I/O functions */
264 int ast_hide_password(int fd)
271 res = tcgetattr(fd, &tios);
274 old = tios.c_lflag & (ECHO | ECHONL);
275 tios.c_lflag &= ~ECHO;
276 tios.c_lflag |= ECHONL;
277 res = tcsetattr(fd, TCSAFLUSH, &tios);
283 int ast_restore_tty(int fd, int oldstate)
289 res = tcgetattr(fd, &tios);
292 tios.c_lflag &= ~(ECHO | ECHONL);
293 tios.c_lflag |= oldstate;
294 res = tcsetattr(fd, TCSAFLUSH, &tios);