#include <stdio.h>
#include <sys/poll.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <sys/ioctl.h>
#include <asterisk/io.h>
#include <asterisk/logger.h>
Go to the source code of this file.
Data Structures | |
struct | io_context |
struct | io_rec |
Defines | |
#define | DEBUG(a) |
#define | GROW_SHRINK_SIZE 512 |
Functions | |
io_context * | io_context_create (void) |
Creates a context. | |
void | io_context_destroy (struct io_context *ioc) |
Destroys a context. | |
int * | ast_io_add (struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data) |
Adds an IO context. | |
int * | ast_io_change (struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data) |
Changes an IO handler. | |
int | ast_io_remove (struct io_context *ioc, int *_id) |
Removes an IO context. | |
int | ast_io_wait (struct io_context *ioc, int howlong) |
Waits for IO. | |
void | ast_io_dump (struct io_context *ioc) |
Dumps the IO array. | |
int | ast_hide_password (int fd) |
Set fd into non-echoing mode (if fd is a tty). | |
int | ast_restore_tty (int fd, int oldstate) |
Restores TTY mode. | |
int | ast_get_termcols (int fd) |
|
Definition at line 27 of file io.c. Referenced by ast_io_add(), ast_io_wait(), ast_sched_add(), ast_sched_del(), ast_sched_runq(), and ast_sched_wait(). |
|
Definition at line 46 of file io.c. Referenced by io_context_create(). |
|
Definition at line 342 of file io.c.
00343 { 00344 struct winsize win; 00345 int cols = 0; 00346 00347 if (!isatty(fd)) 00348 return -1; 00349 00350 if ( ioctl(fd, TIOCGWINSZ, &win) != -1 ) { 00351 if ( !cols && win.ws_col > 0 ) 00352 cols = (int) win.ws_col; 00353 } else { 00354 /* assume 80 characters if the ioctl fails for some reason */ 00355 cols = 80; 00356 } 00357 00358 return cols; 00359 } |
|
Set fd into non-echoing mode (if fd is a tty).
Definition at line 306 of file io.c.
00307 { 00308 struct termios tios; 00309 int res; 00310 int old; 00311 if (!isatty(fd)) 00312 return -1; 00313 res = tcgetattr(fd, &tios); 00314 if (res < 0) 00315 return -1; 00316 old = tios.c_lflag & (ECHO | ECHONL); 00317 tios.c_lflag &= ~ECHO; 00318 tios.c_lflag |= ECHONL; 00319 res = tcsetattr(fd, TCSAFLUSH, &tios); 00320 if (res < 0) 00321 return -1; 00322 return old; 00323 } |
|
Adds an IO context.
Definition at line 140 of file io.c. References ast_io_cb, ast_log(), DEBUG, io_context::fdcnt, io_context::fds, io_context::ior, LOG_DEBUG, malloc, and io_context::maxfdcnt. Referenced by ast_rtp_new().
00141 { 00142 /* 00143 * Add a new I/O entry for this file descriptor 00144 * with the given event mask, to call callback with 00145 * data as an argument. Returns NULL on failure. 00146 */ 00147 int *ret; 00148 DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n")); 00149 if (ioc->fdcnt >= ioc->maxfdcnt) { 00150 /* 00151 * We don't have enough space for this entry. We need to 00152 * reallocate maxfdcnt poll fd's and io_rec's, or back out now. 00153 */ 00154 if (io_grow(ioc)) 00155 return NULL; 00156 } 00157 00158 /* 00159 * At this point, we've got sufficiently large arrays going 00160 * and we can make an entry for it in the pollfd and io_r 00161 * structures. 00162 */ 00163 ioc->fds[ioc->fdcnt].fd = fd; 00164 ioc->fds[ioc->fdcnt].events = events; 00165 ioc->ior[ioc->fdcnt].callback = callback; 00166 ioc->ior[ioc->fdcnt].data = data; 00167 ioc->ior[ioc->fdcnt].id = (int *)malloc(sizeof(int)); 00168 /* Bonk if we couldn't allocate an int */ 00169 if (!ioc->ior[ioc->fdcnt].id) 00170 return NULL; 00171 *(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt; 00172 ret = ioc->ior[ioc->fdcnt].id; 00173 ioc->fdcnt++; 00174 return ret; 00175 } |
|
Changes an IO handler.
Definition at line 177 of file io.c. References ast_io_cb, io_rec::callback, io_rec::data, io_context::fdcnt, io_context::fds, and io_context::ior.
00178 { 00179 if (*id < ioc->fdcnt) { 00180 if (fd > -1) 00181 ioc->fds[*id].fd = fd; 00182 if (callback) 00183 ioc->ior[*id].callback = callback; 00184 if (events) 00185 ioc->fds[*id].events = events; 00186 if (data) 00187 ioc->ior[*id].data = data; 00188 return id; 00189 } else return NULL; 00190 } |
|
Dumps the IO array.
Definition at line 282 of file io.c. References ast_log(), io_rec::callback, io_rec::data, io_context::fdcnt, io_context::fds, io_rec::id, io_context::ior, LOG_DEBUG, and io_context::maxfdcnt.
00283 { 00284 /* 00285 * Print some debugging information via 00286 * the logger interface 00287 */ 00288 int x; 00289 ast_log(LOG_DEBUG, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt); 00290 ast_log(LOG_DEBUG, "================================================\n"); 00291 ast_log(LOG_DEBUG, "| ID FD Callback Data Events |\n"); 00292 ast_log(LOG_DEBUG, "+------+------+-----------+-----------+--------+\n"); 00293 for (x=0;x<ioc->fdcnt;x++) { 00294 ast_log(LOG_DEBUG, "| %.4d | %.4d | %p | %p | %.6x |\n", 00295 *ioc->ior[x].id, 00296 ioc->fds[x].fd, 00297 ioc->ior[x].callback, 00298 ioc->ior[x].data, 00299 ioc->fds[x].events); 00300 } 00301 ast_log(LOG_DEBUG, "================================================\n"); 00302 } |
|
Removes an IO context.
Definition at line 219 of file io.c. References ast_log(), io_context::current_ioc, io_context::fdcnt, io_context::fds, free, io_rec::id, io_context::ior, LOG_NOTICE, LOG_WARNING, and io_context::needshrink. Referenced by ast_io_wait(), and ast_rtp_destroy().
00220 { 00221 int x; 00222 if (!_id) { 00223 ast_log(LOG_WARNING, "Asked to remove NULL?\n"); 00224 return -1; 00225 } 00226 for (x=0;x<ioc->fdcnt;x++) { 00227 if (ioc->ior[x].id == _id) { 00228 /* Free the int immediately and set to NULL so we know it's unused now */ 00229 free(ioc->ior[x].id); 00230 ioc->ior[x].id = NULL; 00231 ioc->fds[x].events = 0; 00232 ioc->fds[x].revents = 0; 00233 ioc->needshrink = 1; 00234 if (!ioc->current_ioc) 00235 io_shrink(ioc); 00236 return 0; 00237 } 00238 } 00239 00240 ast_log(LOG_NOTICE, "Unable to remove unknown id %p\n", _id); 00241 return -1; 00242 } |
|
Waits for IO.
Definition at line 244 of file io.c. References ast_io_remove(), ast_log(), io_rec::callback, io_context::current_ioc, io_rec::data, DEBUG, io_context::fdcnt, io_context::fds, io_rec::id, io_context::ior, LOG_DEBUG, and io_context::needshrink.
00245 { 00246 /* 00247 * Make the poll call, and call 00248 * the callbacks for anything that needs 00249 * to be handled 00250 */ 00251 int res; 00252 int x; 00253 int origcnt; 00254 DEBUG(ast_log(LOG_DEBUG, "ast_io_wait()\n")); 00255 res = poll(ioc->fds, ioc->fdcnt, howlong); 00256 if (res > 0) { 00257 /* 00258 * At least one event 00259 */ 00260 origcnt = ioc->fdcnt; 00261 for(x=0;x<origcnt;x++) { 00262 /* Yes, it is possible for an entry to be deleted and still have an 00263 event waiting if it occurs after the original calling id */ 00264 if (ioc->fds[x].revents && ioc->ior[x].id) { 00265 /* There's an event waiting */ 00266 ioc->current_ioc = *ioc->ior[x].id; 00267 if (ioc->ior[x].callback) { 00268 if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) { 00269 /* Time to delete them since they returned a 0 */ 00270 ast_io_remove(ioc, ioc->ior[x].id); 00271 } 00272 } 00273 ioc->current_ioc = -1; 00274 } 00275 } 00276 if (ioc->needshrink) 00277 io_shrink(ioc); 00278 } 00279 return res; 00280 } |
|
Restores TTY mode.
Definition at line 325 of file io.c.
00326 { 00327 int res; 00328 struct termios tios; 00329 if (oldstate < 0) 00330 return 0; 00331 res = tcgetattr(fd, &tios); 00332 if (res < 0) 00333 return -1; 00334 tios.c_lflag &= ~(ECHO | ECHONL); 00335 tios.c_lflag |= oldstate; 00336 res = tcsetattr(fd, TCSAFLUSH, &tios); 00337 if (res < 0) 00338 return -1; 00339 return 0; 00340 } |
|
Creates a context. Create a context for I/O operations Basically mallocs an IO structure and sets up some default values. Returns an allocated io_context structure Definition at line 66 of file io.c. References free, GROW_SHRINK_SIZE, and malloc.
00067 { 00068 /* Create an I/O context */ 00069 struct io_context *tmp; 00070 tmp = malloc(sizeof(struct io_context)); 00071 if (tmp) { 00072 tmp->needshrink = 0; 00073 tmp->fdcnt = 0; 00074 tmp->maxfdcnt = GROW_SHRINK_SIZE/2; 00075 tmp->current_ioc = -1; 00076 tmp->fds = malloc((GROW_SHRINK_SIZE/2) * sizeof(struct pollfd)); 00077 if (!tmp->fds) { 00078 free(tmp); 00079 tmp = NULL; 00080 } else { 00081 memset(tmp->fds, 0, (GROW_SHRINK_SIZE/2) * sizeof(struct pollfd)); 00082 tmp->ior = malloc((GROW_SHRINK_SIZE/2) * sizeof(struct io_rec)); 00083 if (!tmp->ior) { 00084 free(tmp->fds); 00085 free(tmp); 00086 tmp = NULL; 00087 } else 00088 memset(tmp->ior, 0, (GROW_SHRINK_SIZE/2) * sizeof(struct io_rec)); 00089 } 00090 } 00091 return tmp; 00092 } |
|
Destroys a context.
Definition at line 94 of file io.c. References io_context::fds, free, and io_context::ior.
|