00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <unistd.h>
00027 #include <stdlib.h>
00028 #include <termios.h>
00029 #include <string.h>
00030 #include <sys/ioctl.h>
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 44168 $")
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
00047
00048 struct io_rec {
00049 ast_io_cb callback;
00050 void *data;
00051 int *id;
00052 };
00053
00054
00055
00056
00057
00058
00059
00060
00061 #define GROW_SHRINK_SIZE 512
00062
00063
00064
00065 struct io_context {
00066
00067 struct pollfd *fds;
00068
00069 struct io_rec *ior;
00070
00071 unsigned int fdcnt;
00072
00073 unsigned int maxfdcnt;
00074
00075 int current_ioc;
00076
00077 int needshrink;
00078 };
00079
00080 struct io_context *io_context_create(void)
00081 {
00082
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
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
00123
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
00137
00138
00139
00140
00141 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00142 return -1;
00143 }
00144 } else {
00145
00146
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
00158
00159
00160
00161 int *ret;
00162 DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n"));
00163 if (ioc->fdcnt >= ioc->maxfdcnt) {
00164
00165
00166
00167
00168 if (io_grow(ioc))
00169 return NULL;
00170 }
00171
00172
00173
00174
00175
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
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
00214
00215
00216
00217 for (getfrom = 0; getfrom < ioc->fdcnt; getfrom++) {
00218 if (ioc->ior[getfrom].id) {
00219
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
00231
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
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 == -1)
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
00264
00265
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
00275
00276 origcnt = ioc->fdcnt;
00277 for(x = 0; x < origcnt; x++) {
00278
00279
00280 if (ioc->fds[x].revents && ioc->ior[x].id) {
00281
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
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
00302
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
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
00371 cols = 80;
00372 }
00373
00374 return cols;
00375 }
00376