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

io.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk
00003  * 
00004  * Mark Spencer <markster@marko.net>
00005  *
00006  * Copyright(C) Mark Spencer
00007  * 
00008  * Distributed under the terms of the GNU General Public License (GPL) Version 2
00009  *
00010  * I/O Managment (Derived from Cheops-NG)
00011  *
00012  */
00013 
00014 #include <stdio.h>
00015 #include <sys/poll.h>
00016 #include <unistd.h>
00017 #include <stdlib.h>
00018 #include <termios.h>
00019 #include <string.h> /* for memset */
00020 #include <sys/ioctl.h>
00021 #include <asterisk/io.h>
00022 #include <asterisk/logger.h>
00023 
00024 #ifdef DEBUG_IO
00025 #define DEBUG DEBUG_M
00026 #else
00027 #define DEBUG(a) 
00028 #endif
00029 
00030 /* 
00031  * Kept for each file descriptor
00032  */
00033 struct io_rec {
00034    ast_io_cb callback;     /* What is to be called */
00035    void *data;             /* Data to be passed */
00036    int *id;                /* ID number */
00037 };
00038 
00039 /* These two arrays are keyed with
00040    the same index.  it's too bad that
00041    pollfd doesn't have a callback field
00042    or something like that.  They grow as
00043    needed, by GROW_SHRINK_AMOUNT structures
00044    at once */
00045 
00046 #define GROW_SHRINK_SIZE 512
00047 
00048 /* Global variables are now in a struct in order to be
00049    made threadsafe */
00050 struct io_context {
00051    /* Poll structure */
00052    struct pollfd *fds;
00053    /* Associated I/O records */
00054    struct io_rec *ior;
00055    /* First available fd */
00056    unsigned int fdcnt;
00057    /* Maximum available fd */
00058    unsigned int maxfdcnt;
00059    /* Currently used io callback */
00060    int current_ioc;
00061    /* Whether something has been deleted */
00062    int needshrink;
00063 };
00064 
00065 
00066 struct io_context *io_context_create(void)
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 }
00093 
00094 void io_context_destroy(struct io_context *ioc)
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 }
00103 
00104 static int io_grow(struct io_context *ioc)
00105 {
00106    /* 
00107     * Grow the size of our arrays.  Return 0 on success or
00108     * -1 on failure
00109     */
00110    void *tmp;
00111    DEBUG(ast_log(LOG_DEBUG, "io_grow()\n"));
00112    ioc->maxfdcnt += GROW_SHRINK_SIZE;
00113    tmp = realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(struct io_rec));
00114    if (tmp) {
00115       ioc->ior = (struct io_rec *)tmp;
00116       tmp = realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(struct pollfd));
00117       if (tmp) {
00118          ioc->fds = tmp;
00119       } else {
00120          /*
00121           * Not enough memory for the pollfd.  Not really any need
00122           * to shrink back the iorec's as we'll probably want to
00123           * grow them again soon when more memory is available, and
00124           * then they'll already be the right size
00125           */
00126          ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00127          return -1;
00128       }
00129       
00130    } else {
00131       /*
00132        * Out of memory.  We return to the old size, and return a failure
00133        */
00134       ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00135       return -1;
00136    }
00137    return 0;
00138 }
00139 
00140 int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
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 }
00176 
00177 int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data)
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 }
00191 
00192 static int io_shrink(struct io_context *ioc)
00193 {
00194    int getfrom;
00195    int putto = 0;
00196    /* 
00197     * Bring the fields from the very last entry to cover over
00198     * the entry we are removing, then decrease the size of the 
00199     * arrays by one.
00200     */
00201    for (getfrom=0;getfrom<ioc->fdcnt;getfrom++) {
00202       if (ioc->ior[getfrom].id) {
00203          /* In use, save it */
00204          if (getfrom != putto) {
00205             ioc->fds[putto] = ioc->fds[getfrom];
00206             ioc->ior[putto] = ioc->ior[getfrom];
00207             *(ioc->ior[putto].id) = putto;
00208          }
00209          putto++;
00210       }
00211    }
00212    ioc->fdcnt = putto;
00213    ioc->needshrink = 0;
00214    /* FIXME: We should free some memory if we have lots of unused
00215       io structs */
00216    return 0;
00217 }
00218 
00219 int ast_io_remove(struct io_context *ioc, int *_id)
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 }
00243 
00244 int ast_io_wait(struct io_context *ioc, int howlong)
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 }
00281 
00282 void ast_io_dump(struct io_context *ioc)
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 }
00303 
00304 /* Unrelated I/O functions */
00305 
00306 int ast_hide_password(int fd)
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 }
00324 
00325 int ast_restore_tty(int fd, int oldstate)
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 }
00341 
00342 int ast_get_termcols(int fd)
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 }
00360 

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