Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

io.h File Reference

#include <sys/poll.h>

Go to the source code of this file.

Defines

#define AST_IO_IN   POLLIN
#define AST_IO_OUT   POLLOUT
#define AST_IO_PRI   POLLPRI
#define AST_IO_ERR   POLLERR
#define AST_IO_HUP   POLLHUP
#define AST_IO_NVAL   POLLNVAL
#define AST_IO_CB(a)   ((ast_io_cb)(a))

Typedefs

typedef int(* ast_io_cb )(int *id, int fd, short events, void *cbdata)

Functions

io_contextio_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 oldstatus)
 Restores TTY mode.

int ast_get_termcols (int fd)


Define Documentation

#define AST_IO_CB  )     ((ast_io_cb)(a))
 

Definition at line 63 of file io.h.

#define AST_IO_ERR   POLLERR
 

Error condition (errno or getsockopt)

Definition at line 32 of file io.h.

#define AST_IO_HUP   POLLHUP
 

Hangup

Definition at line 34 of file io.h.

#define AST_IO_IN   POLLIN
 

Input ready

Definition at line 24 of file io.h.

Referenced by ast_rtp_new().

#define AST_IO_NVAL   POLLNVAL
 

Invalid fd

Definition at line 36 of file io.h.

#define AST_IO_OUT   POLLOUT
 

Output ready

Definition at line 26 of file io.h.

#define AST_IO_PRI   POLLPRI
 

Priority input ready

Definition at line 28 of file io.h.


Typedef Documentation

typedef int(* ast_io_cb)(int *id, int fd, short events, void *cbdata)
 

Definition at line 62 of file io.h.

Referenced by ast_io_add(), and ast_io_change().


Function Documentation

int ast_get_termcols int  fd  ) 
 

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 }

int ast_hide_password int  fd  ) 
 

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 }

int* ast_io_add struct io_context ioc,
int  fd,
ast_io_cb  callback,
short  events,
void *  data
 

Adds an IO context.

Parameters:
ioc which context to use
fd which fd to monitor
callback callback function to run
events event mask of events to wait for
data data to pass to the callback Watch for any of revents activites on fd, calling callback with data as callback data. Returns a pointer to ID of the IO event, or NULL on failure.

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 }

int* ast_io_change struct io_context ioc,
int *  id,
int  fd,
ast_io_cb  callback,
short  events,
void *  data
 

Changes an IO handler.

Parameters:
ioc which context to use
id 
fd the fd you wish it to contain now
callback new callback function
events event mask to wait for
data data to pass to the callback function Change an i/o handler, updating fd if > -1, callback if non-null, and revents if >-1, and data if non-null. Returns a pointero to the ID of the IO event, or NULL on failure.

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 }

void ast_io_dump struct io_context ioc  ) 
 

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 }

int ast_io_remove struct io_context ioc,
int *  id
 

Removes an IO context.

Parameters:
ioc which io_context to remove it from
id which ID to remove Remove an I/O id from consideration Returns 0 on success or -1 on failure.

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 }

int ast_io_wait struct io_context ioc,
int  howlong
 

Waits for IO.

Parameters:
ioc which context to act upon
howlong how many milliseconds to wait Wait for I/O to happen, returning after howlong milliseconds, and after processing any necessary I/O. Returns the number of I/O events which took place.

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 }

int ast_restore_tty int  fd,
int  oldstatus
 

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 }

struct io_context* io_context_create void   ) 
 

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 }

void io_context_destroy struct io_context ioc  ) 
 

Destroys a context.

Definition at line 94 of file io.c.

References io_context::fds, free, and io_context::ior.

00095 {
00096    /* Free associated memory with an I/O context */
00097    if (ioc->fds)
00098       free(ioc->fds);
00099    if (ioc->ior)
00100       free(ioc->ior);
00101    free(ioc);
00102 }


Generated on Fri Oct 31 07:05:14 2003 for Asterisk by doxygen 1.3.4