Sat Mar 24 22:55:17 2007

Asterisk developer's documentation


io.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief I/O Managment (Derived from Cheops-NG)
00022  *
00023  */
00024 
00025 #include <stdio.h>
00026 #include <unistd.h>
00027 #include <stdlib.h>
00028 #include <termios.h>
00029 #include <string.h> /* for memset */
00030 #include <sys/ioctl.h>
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00035 
00036 #include "asterisk/io.h"
00037 #include "asterisk/logger.h"
00038 
00039 #ifdef DEBUG_IO
00040 #define DEBUG DEBUG_M
00041 #else
00042 #define DEBUG(a) 
00043 #endif
00044 
00045 /* 
00046  * Kept for each file descriptor
00047  */
00048 struct io_rec {
00049    ast_io_cb callback;     /* What is to be called */
00050    void *data;             /* Data to be passed */
00051    int *id;                /* ID number */
00052 };
00053 
00054 /* These two arrays are keyed with
00055    the same index.  it's too bad that
00056    pollfd doesn't have a callback field
00057    or something like that.  They grow as
00058    needed, by GROW_SHRINK_AMOUNT structures
00059    at once */
00060 
00061 #define GROW_SHRINK_SIZE 512
00062 
00063 /* Global variables are now in a struct in order to be
00064    made threadsafe */
00065 struct io_context {
00066    /* Poll structure */
00067    struct pollfd *fds;
00068    /* Associated I/O records */
00069    struct io_rec *ior;
00070    /* First available fd */
00071    unsigned int fdcnt;
00072    /* Maximum available fd */
00073    unsigned int maxfdcnt;
00074    /* Currently used io callback */
00075    int current_ioc;
00076    /* Whether something has been deleted */
00077    int needshrink;
00078 };
00079 
00080 struct io_context *io_context_create(void)
00081 {
00082    /* Create an I/O context */
00083    struct io_context *tmp;
00084    tmp = malloc(sizeof(struct io_context));
00085    if (tmp) {
00086       tmp->needshrink = 0;
00087       tmp->fdcnt = 0;
00088       tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
00089       tmp->current_ioc = -1;
00090       tmp->fds = malloc((GROW_SHRINK_SIZE/2) * sizeof(struct pollfd));
00091       if (!tmp->fds) {
00092          free(tmp);
00093          tmp = NULL;
00094       } else {
00095          memset(tmp->fds, 0, (GROW_SHRINK_SIZE / 2) * sizeof(struct pollfd));
00096          tmp->ior =  malloc((GROW_SHRINK_SIZE / 2) * sizeof(struct io_rec));
00097          if (!tmp->ior) {
00098             free(tmp->fds);
00099             free(tmp);
00100             tmp = NULL;
00101          } else {
00102             memset(tmp->ior, 0, (GROW_SHRINK_SIZE / 2) * sizeof(struct io_rec));
00103          }
00104       }
00105    }
00106    return tmp;
00107 }
00108 
00109 void io_context_destroy(struct io_context *ioc)
00110 {
00111    /* Free associated memory with an I/O context */
00112    if (ioc->fds)
00113       free(ioc->fds);
00114    if (ioc->ior)
00115       free(ioc->ior);
00116    free(ioc);
00117 }
00118 
00119 static int io_grow(struct io_context *ioc)
00120 {
00121    /* 
00122     * Grow the size of our arrays.  Return 0 on success or
00123     * -1 on failure
00124     */
00125    void *tmp;
00126    DEBUG(ast_log(LOG_DEBUG, "io_grow()\n"));
00127    ioc->maxfdcnt += GROW_SHRINK_SIZE;
00128    tmp = realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(struct io_rec));
00129    if (tmp) {
00130       ioc->ior = (struct io_rec *)tmp;
00131       tmp = realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(struct pollfd));
00132       if (tmp) {
00133          ioc->fds = tmp;
00134       } else {
00135          /*
00136           * Not enough memory for the pollfd.  Not really any need
00137           * to shrink back the iorec's as we'll probably want to
00138           * grow them again soon when more memory is available, and
00139           * then they'll already be the right size
00140           */
00141          ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00142          return -1;
00143       }
00144    } else {
00145       /*
00146        * Out of memory.  We return to the old size, and return a failure
00147        */
00148       ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00149       return -1;
00150    }
00151    return 0;
00152 }
00153 
00154 int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
00155 {
00156    /*
00157     * Add a new I/O entry for this file descriptor
00158     * with the given event mask, to call callback with
00159     * data as an argument.  Returns NULL on failure.
00160     */
00161    int *ret;
00162    DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n"));
00163    if (ioc->fdcnt >= ioc->maxfdcnt) {
00164       /* 
00165        * We don't have enough space for this entry.  We need to
00166        * reallocate maxfdcnt poll fd's and io_rec's, or back out now.
00167        */
00168       if (io_grow(ioc))
00169          return NULL;
00170    }
00171 
00172    /*
00173     * At this point, we've got sufficiently large arrays going
00174     * and we can make an entry for it in the pollfd and io_r
00175     * structures.
00176     */
00177    ioc->fds[ioc->fdcnt].fd = fd;
00178    ioc->fds[ioc->fdcnt].events = events;
00179    ioc->fds[ioc->fdcnt].revents = 0;
00180    ioc->ior[ioc->fdcnt].callback = callback;
00181    ioc->ior[ioc->fdcnt].data = data;
00182    ioc->ior[ioc->fdcnt].id = (int *)malloc(sizeof(int));
00183    /* Bonk if we couldn't allocate an int */
00184    if (!ioc->ior[ioc->fdcnt].id)
00185       return NULL;
00186    *(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt;
00187    ret = ioc->ior[ioc->fdcnt].id;
00188    ioc->fdcnt++;
00189    return ret;
00190 }
00191 
00192 int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data)
00193 {
00194    if (*id < ioc->fdcnt) {
00195       if (fd > -1)
00196          ioc->fds[*id].fd = fd;
00197       if (callback)
00198          ioc->ior[*id].callback = callback;
00199       if (events)
00200          ioc->fds[*id].events = events;
00201       if (data)
00202          ioc->ior[*id].data = data;
00203       return id;
00204    }
00205    return NULL;
00206 }
00207 
00208 static int io_shrink(struct io_context *ioc)
00209 {
00210    int getfrom;
00211    int putto = 0;
00212    /* 
00213     * Bring the fields from the very last entry to cover over
00214     * the entry we are removing, then decrease the size of the 
00215     * arrays by one.
00216     */
00217    for (getfrom = 0; getfrom < ioc->fdcnt; getfrom++) {
00218       if (ioc->ior[getfrom].id) {
00219          /* In use, save it */
00220          if (getfrom != putto) {
00221             ioc->fds[putto] = ioc->fds[getfrom];
00222             ioc->ior[putto] = ioc->ior[getfrom];
00223             *(ioc->ior[putto].id) = putto;
00224          }
00225          putto++;
00226       }
00227    }
00228    ioc->fdcnt = putto;
00229    ioc->needshrink = 0;
00230    /* FIXME: We should free some memory if we have lots of unused
00231       io structs */
00232    return 0;
00233 }
00234 
00235 int ast_io_remove(struct io_context *ioc, int *_id)
00236 {
00237    int x;
00238    if (!_id) {
00239       ast_log(LOG_WARNING, "Asked to remove NULL?\n");
00240       return -1;
00241    }
00242    for (x = 0; x < ioc->fdcnt; x++) {
00243       if (ioc->ior[x].id == _id) {
00244          /* Free the int immediately and set to NULL so we know it's unused now */
00245          free(ioc->ior[x].id);
00246          ioc->ior[x].id = NULL;
00247          ioc->fds[x].events = 0;
00248          ioc->fds[x].revents = 0;
00249          ioc->needshrink = 1;
00250          if (!ioc->current_ioc)
00251             io_shrink(ioc);
00252          return 0;
00253       }
00254    }
00255    
00256    ast_log(LOG_NOTICE, "Unable to remove unknown id %p\n", _id);
00257    return -1;
00258 }
00259 
00260 int ast_io_wait(struct io_context *ioc, int howlong)
00261 {
00262    /*
00263     * Make the poll call, and call
00264     * the callbacks for anything that needs
00265     * to be handled
00266     */
00267    int res;
00268    int x;
00269    int origcnt;
00270    DEBUG(ast_log(LOG_DEBUG, "ast_io_wait()\n"));
00271    res = poll(ioc->fds, ioc->fdcnt, howlong);
00272    if (res > 0) {
00273       /*
00274        * At least one event
00275        */
00276       origcnt = ioc->fdcnt;
00277       for(x = 0; x < origcnt; x++) {
00278          /* Yes, it is possible for an entry to be deleted and still have an
00279             event waiting if it occurs after the original calling id */
00280          if (ioc->fds[x].revents && ioc->ior[x].id) {
00281             /* There's an event waiting */
00282             ioc->current_ioc = *ioc->ior[x].id;
00283             if (ioc->ior[x].callback) {
00284                if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) {
00285                   /* Time to delete them since they returned a 0 */
00286                   ast_io_remove(ioc, ioc->ior[x].id);
00287                }
00288             }
00289             ioc->current_ioc = -1;
00290          }
00291       }
00292       if (ioc->needshrink)
00293          io_shrink(ioc);
00294    }
00295    return res;
00296 }
00297 
00298 void ast_io_dump(struct io_context *ioc)
00299 {
00300    /*
00301     * Print some debugging information via
00302     * the logger interface
00303     */
00304    int x;
00305    ast_log(LOG_DEBUG, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt);
00306    ast_log(LOG_DEBUG, "================================================\n");
00307    ast_log(LOG_DEBUG, "| ID    FD     Callback    Data        Events  |\n");
00308    ast_log(LOG_DEBUG, "+------+------+-----------+-----------+--------+\n");
00309    for (x = 0; x < ioc->fdcnt; x++) {
00310       ast_log(LOG_DEBUG, "| %.4d | %.4d | %p | %p | %.6x |\n", 
00311             *ioc->ior[x].id,
00312             ioc->fds[x].fd,
00313             ioc->ior[x].callback,
00314             ioc->ior[x].data,
00315             ioc->fds[x].events);
00316    }
00317    ast_log(LOG_DEBUG, "================================================\n");
00318 }
00319 
00320 /* Unrelated I/O functions */
00321 
00322 int ast_hide_password(int fd)
00323 {
00324    struct termios tios;
00325    int res;
00326    int old;
00327    if (!isatty(fd))
00328       return -1;
00329    res = tcgetattr(fd, &tios);
00330    if (res < 0)
00331       return -1;
00332    old = tios.c_lflag & (ECHO | ECHONL);
00333    tios.c_lflag &= ~ECHO;
00334    tios.c_lflag |= ECHONL;
00335    res = tcsetattr(fd, TCSAFLUSH, &tios);
00336    if (res < 0)
00337       return -1;
00338    return old;
00339 }
00340 
00341 int ast_restore_tty(int fd, int oldstate)
00342 {
00343    int res;
00344    struct termios tios;
00345    if (oldstate < 0)
00346       return 0;
00347    res = tcgetattr(fd, &tios);
00348    if (res < 0)
00349       return -1;
00350    tios.c_lflag &= ~(ECHO | ECHONL);
00351    tios.c_lflag |= oldstate;
00352    res = tcsetattr(fd, TCSAFLUSH, &tios);
00353    if (res < 0)
00354       return -1;
00355    return 0;
00356 }
00357 
00358 int ast_get_termcols(int fd)
00359 {
00360    struct winsize win;
00361    int cols = 0;
00362 
00363    if (!isatty(fd))
00364       return -1;
00365 
00366    if ( ioctl(fd, TIOCGWINSZ, &win) != -1 ) {
00367       if ( !cols && win.ws_col > 0 )
00368          cols = (int) win.ws_col;
00369    } else {
00370       /* assume 80 characters if the ioctl fails for some reason */
00371       cols = 80;
00372    }
00373 
00374    return cols;
00375 }
00376 

Generated on Sat Mar 24 22:55:17 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.7