Wed Oct 28 11:52:45 2009

Asterisk developer's documentation


io.h File Reference

I/O Management (derived from Cheops-NG). More...

#include "asterisk/poll-compat.h"

Include dependency graph for io.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

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

Typedefs

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

Functions

int ast_get_termcols (int fd)
int ast_hide_password (int fd)
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.
void ast_io_dump (struct io_context *ioc)
 Dumps the IO array. Debugging: Dump everything in the I/O array.
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.
int ast_restore_tty (int fd, int oldstatus)
 Restores TTY mode. Call with result from previous ast_hide_password.
struct io_contextio_context_create (void)
 Creates a context Create a context for I/O operations Basically mallocs an IO structure and sets up some default values.
void io_context_destroy (struct io_context *ioc)
 Destroys a context.


Detailed Description

I/O Management (derived from Cheops-NG).

Definition in file io.h.


Define Documentation

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

Definition at line 72 of file io.h.

#define AST_IO_ERR   POLLERR

Error condition (errno or getsockopt)

Definition at line 41 of file io.h.

#define AST_IO_HUP   POLLHUP

Hangup

Definition at line 43 of file io.h.

#define AST_IO_IN   POLLIN

#define AST_IO_NVAL   POLLNVAL

Invalid fd

Definition at line 45 of file io.h.

#define AST_IO_OUT   POLLOUT

Output ready

Definition at line 35 of file io.h.

#define AST_IO_PRI   POLLPRI

Priority input ready

Definition at line 37 of file io.h.

Referenced by network_thread().


Typedef Documentation

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

Definition at line 71 of file io.h.


Function Documentation

int ast_get_termcols ( int  fd  ) 

Definition at line 364 of file io.c.

Referenced by ast_cli_display_match_list().

00365 {
00366    struct winsize win;
00367    int cols = 0;
00368 
00369    if (!isatty(fd))
00370       return -1;
00371 
00372    if ( ioctl(fd, TIOCGWINSZ, &win) != -1 ) {
00373       if ( !cols && win.ws_col > 0 )
00374          cols = (int) win.ws_col;
00375    } else {
00376       /* assume 80 characters if the ioctl fails for some reason */
00377       cols = 80;
00378    }
00379 
00380    return cols;
00381 }

int ast_hide_password ( int  fd  ) 

Set fd into non-echoing mode (if fd is a tty)

Definition at line 328 of file io.c.

References ECHO.

Referenced by pw_cb().

00329 {
00330    struct termios tios;
00331    int res;
00332    int old;
00333    if (!isatty(fd))
00334       return -1;
00335    res = tcgetattr(fd, &tios);
00336    if (res < 0)
00337       return -1;
00338    old = tios.c_lflag & (ECHO | ECHONL);
00339    tios.c_lflag &= ~ECHO;
00340    tios.c_lflag |= ECHONL;
00341    res = tcsetattr(fd, TCSAFLUSH, &tios);
00342    if (res < 0)
00343       return -1;
00344    return old;
00345 }

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.
Return values:
a pointer to ID of the IO event
NULL on failure
Adds an IO context.

Returns:
Returns NULL on failure.

Definition at line 153 of file io.c.

References ast_debug, ast_malloc, io_rec::callback, io_rec::data, DEBUG, io_context::fdcnt, io_context::fds, io_rec::id, io_grow(), io_context::ior, and io_context::maxfdcnt.

Referenced by ast_netsock_bindaddr(), ast_rtp_new_with_bindaddr(), ast_udptl_new_with_bindaddr(), do_monitor(), network_thread(), and p2p_callback_disable().

00154 {
00155    int *ret;
00156 
00157    DEBUG(ast_debug(1, "ast_io_add()\n"));
00158 
00159    if (ioc->fdcnt >= ioc->maxfdcnt) {
00160       /* 
00161        * We don't have enough space for this entry.  We need to
00162        * reallocate maxfdcnt poll fd's and io_rec's, or back out now.
00163        */
00164       if (io_grow(ioc))
00165          return NULL;
00166    }
00167 
00168    /*
00169     * At this point, we've got sufficiently large arrays going
00170     * and we can make an entry for it in the pollfd and io_r
00171     * structures.
00172     */
00173    ioc->fds[ioc->fdcnt].fd = fd;
00174    ioc->fds[ioc->fdcnt].events = events;
00175    ioc->fds[ioc->fdcnt].revents = 0;
00176    ioc->ior[ioc->fdcnt].callback = callback;
00177    ioc->ior[ioc->fdcnt].data = data;
00178 
00179    if (!(ioc->ior[ioc->fdcnt].id = ast_malloc(sizeof(*ioc->ior[ioc->fdcnt].id)))) {
00180       /* Bonk if we couldn't allocate an int */
00181       return NULL;
00182    }
00183 
00184    *(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt;
00185    ret = ioc->ior[ioc->fdcnt].id;
00186    ioc->fdcnt++;
00187 
00188    return ret;
00189 }

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.
Return values:
a pointer to the ID of the IO event
NULL on failure

Definition at line 191 of file io.c.

References io_rec::callback, io_rec::data, io_context::fdcnt, io_context::fds, and io_context::ior.

Referenced by do_monitor().

00192 {
00193    /* If this id exceeds our file descriptor count it doesn't exist here */
00194    if (*id > ioc->fdcnt)
00195       return NULL;
00196 
00197    if (fd > -1)
00198       ioc->fds[*id].fd = fd;
00199    if (callback)
00200       ioc->ior[*id].callback = callback;
00201    if (events)
00202       ioc->fds[*id].events = events;
00203    if (data)
00204       ioc->ior[*id].data = data;
00205 
00206    return id;
00207 }

void ast_io_dump ( struct io_context ioc  ) 

Dumps the IO array. Debugging: Dump everything in the I/O array.

Definition at line 303 of file io.c.

References ast_debug, io_rec::callback, io_rec::data, io_context::fdcnt, io_context::fds, io_rec::id, io_context::ior, and io_context::maxfdcnt.

00304 {
00305    /*
00306     * Print some debugging information via
00307     * the logger interface
00308     */
00309    int x;
00310 
00311    ast_debug(1, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt);
00312    ast_debug(1, "================================================\n");
00313    ast_debug(1, "| ID    FD     Callback    Data        Events  |\n");
00314    ast_debug(1, "+------+------+-----------+-----------+--------+\n");
00315    for (x = 0; x < ioc->fdcnt; x++) {
00316       ast_debug(1, "| %.4d | %.4d | %p | %p | %.6x |\n", 
00317             *ioc->ior[x].id,
00318             ioc->fds[x].fd,
00319             ioc->ior[x].callback,
00320             ioc->ior[x].data,
00321             ioc->fds[x].events);
00322    }
00323    ast_debug(1, "================================================\n");
00324 }

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
Return values:
0 on success
-1 on failure

Definition at line 236 of file io.c.

References ast_free, ast_log(), io_context::current_ioc, io_context::fdcnt, io_context::fds, io_rec::id, io_shrink(), io_context::ior, LOG_NOTICE, LOG_WARNING, and io_context::needshrink.

Referenced by ast_io_wait(), ast_netsock_destroy(), ast_rtp_destroy(), ast_udptl_destroy(), do_monitor(), p2p_callback_disable(), and reload_config().

00237 {
00238    int x;
00239 
00240    if (!_id) {
00241       ast_log(LOG_WARNING, "Asked to remove NULL?\n");
00242       return -1;
00243    }
00244 
00245    for (x = 0; x < ioc->fdcnt; x++) {
00246       if (ioc->ior[x].id == _id) {
00247          /* Free the int immediately and set to NULL so we know it's unused now */
00248          ast_free(ioc->ior[x].id);
00249          ioc->ior[x].id = NULL;
00250          ioc->fds[x].events = 0;
00251          ioc->fds[x].revents = 0;
00252          ioc->needshrink = 1;
00253          if (ioc->current_ioc == -1)
00254             io_shrink(ioc);
00255          return 0;
00256       }
00257    }
00258    
00259    ast_log(LOG_NOTICE, "Unable to remove unknown id %p\n", _id);
00260 
00261    return -1;
00262 }

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:
he number of I/O events which took place.

Definition at line 269 of file io.c.

References ast_debug, ast_io_remove(), ast_poll, io_rec::callback, io_context::current_ioc, io_rec::data, DEBUG, io_context::fdcnt, io_context::fds, io_rec::id, io_shrink(), io_context::ior, and io_context::needshrink.

Referenced by do_monitor(), network_thread(), and reload_config().

00270 {
00271    int res, x, origcnt;
00272 
00273    DEBUG(ast_debug(1, "ast_io_wait()\n"));
00274 
00275    if ((res = ast_poll(ioc->fds, ioc->fdcnt, howlong)) <= 0) {
00276       return res;
00277    }
00278 
00279    /* At least one event tripped */
00280    origcnt = ioc->fdcnt;
00281    for (x = 0; x < origcnt; x++) {
00282       /* Yes, it is possible for an entry to be deleted and still have an
00283          event waiting if it occurs after the original calling id */
00284       if (ioc->fds[x].revents && ioc->ior[x].id) {
00285          /* There's an event waiting */
00286          ioc->current_ioc = *ioc->ior[x].id;
00287          if (ioc->ior[x].callback) {
00288             if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) {
00289                /* Time to delete them since they returned a 0 */
00290                ast_io_remove(ioc, ioc->ior[x].id);
00291             }
00292          }
00293          ioc->current_ioc = -1;
00294       }
00295    }
00296 
00297    if (ioc->needshrink)
00298       io_shrink(ioc);
00299 
00300    return res;
00301 }

int ast_restore_tty ( int  fd,
int  oldstatus 
)

Restores TTY mode. Call with result from previous ast_hide_password.

Definition at line 347 of file io.c.

References ECHO.

Referenced by pw_cb().

00348 {
00349    int res;
00350    struct termios tios;
00351    if (oldstate < 0)
00352       return 0;
00353    res = tcgetattr(fd, &tios);
00354    if (res < 0)
00355       return -1;
00356    tios.c_lflag &= ~(ECHO | ECHONL);
00357    tios.c_lflag |= oldstate;
00358    res = tcsetattr(fd, TCSAFLUSH, &tios);
00359    if (res < 0)
00360       return -1;
00361    return 0;
00362 }

struct io_context* io_context_create ( void   )  [read]

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 72 of file io.c.

References ast_calloc, ast_free, ast_malloc, io_context::current_ioc, io_context::fdcnt, io_context::fds, GROW_SHRINK_SIZE, io_context::ior, io_context::maxfdcnt, and io_context::needshrink.

Referenced by load_module().

00073 {
00074    struct io_context *tmp = NULL;
00075 
00076    if (!(tmp = ast_malloc(sizeof(*tmp))))
00077       return NULL;
00078    
00079    tmp->needshrink = 0;
00080    tmp->fdcnt = 0;
00081    tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
00082    tmp->current_ioc = -1;
00083    
00084    if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) {
00085       ast_free(tmp);
00086       tmp = NULL;
00087    } else {
00088       if (!(tmp->ior = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->ior)))) {
00089          ast_free(tmp->fds);
00090          ast_free(tmp);
00091          tmp = NULL;
00092       }
00093    }
00094 
00095    return tmp;
00096 }

void io_context_destroy ( struct io_context ioc  ) 

Destroys a context.

Parameters:
ioc structure to destroy Destroy a context for I/O operations Frees all memory associated with the given io_context structure along with the structure itself

Definition at line 98 of file io.c.

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

Referenced by load_module(), and unload_module().

00099 {
00100    /* Free associated memory with an I/O context */
00101    if (ioc->fds)
00102       ast_free(ioc->fds);
00103    if (ioc->ior)
00104       ast_free(ioc->ior);
00105 
00106    ast_free(ioc);
00107 }


Generated on Wed Oct 28 11:52:45 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6