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
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00033
00034 #include <termios.h>
00035 #include <sys/ioctl.h>
00036
00037 #include "asterisk/io.h"
00038 #include "asterisk/utils.h"
00039
00040 #ifdef DEBUG_IO
00041 #define DEBUG DEBUG_M
00042 #else
00043 #define DEBUG(a)
00044 #endif
00045
00046
00047
00048
00049 struct io_rec {
00050 ast_io_cb callback;
00051 void *data;
00052 int *id;
00053 };
00054
00055
00056
00057
00058
00059
00060
00061
00062 #define GROW_SHRINK_SIZE 512
00063
00064
00065
00066 struct io_context {
00067 struct pollfd *fds;
00068 struct io_rec *ior;
00069 unsigned int fdcnt;
00070 unsigned int maxfdcnt;
00071 int current_ioc;
00072 int needshrink;
00073 };
00074
00075
00076 struct io_context *io_context_create(void)
00077 {
00078 struct io_context *tmp = NULL;
00079
00080 if (!(tmp = ast_malloc(sizeof(*tmp))))
00081 return NULL;
00082
00083 tmp->needshrink = 0;
00084 tmp->fdcnt = 0;
00085 tmp->maxfdcnt = GROW_SHRINK_SIZE/2;
00086 tmp->current_ioc = -1;
00087
00088 if (!(tmp->fds = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->fds)))) {
00089 ast_free(tmp);
00090 tmp = NULL;
00091 } else {
00092 if (!(tmp->ior = ast_calloc(1, (GROW_SHRINK_SIZE / 2) * sizeof(*tmp->ior)))) {
00093 ast_free(tmp->fds);
00094 ast_free(tmp);
00095 tmp = NULL;
00096 }
00097 }
00098
00099 return tmp;
00100 }
00101
00102 void io_context_destroy(struct io_context *ioc)
00103 {
00104
00105 if (ioc->fds)
00106 ast_free(ioc->fds);
00107 if (ioc->ior)
00108 ast_free(ioc->ior);
00109
00110 ast_free(ioc);
00111 }
00112
00113
00114
00115
00116
00117 static int io_grow(struct io_context *ioc)
00118 {
00119 void *tmp;
00120
00121 DEBUG(ast_debug(1, "io_grow()\n"));
00122
00123 ioc->maxfdcnt += GROW_SHRINK_SIZE;
00124
00125 if ((tmp = ast_realloc(ioc->ior, (ioc->maxfdcnt + 1) * sizeof(*ioc->ior)))) {
00126 ioc->ior = tmp;
00127 if ((tmp = ast_realloc(ioc->fds, (ioc->maxfdcnt + 1) * sizeof(*ioc->fds)))) {
00128 ioc->fds = tmp;
00129 } else {
00130
00131
00132
00133
00134
00135
00136 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00137 return -1;
00138 }
00139 } else {
00140
00141
00142
00143
00144 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00145 return -1;
00146 }
00147
00148 return 0;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157 int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
00158 {
00159 int *ret;
00160
00161 DEBUG(ast_debug(1, "ast_io_add()\n"));
00162
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
00183 if (!(ioc->ior[ioc->fdcnt].id = ast_malloc(sizeof(*ioc->ior[ioc->fdcnt].id)))) {
00184
00185 return NULL;
00186 }
00187
00188 *(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt;
00189 ret = ioc->ior[ioc->fdcnt].id;
00190 ioc->fdcnt++;
00191
00192 return ret;
00193 }
00194
00195 int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data)
00196 {
00197
00198 if (*id > ioc->fdcnt)
00199 return NULL;
00200
00201 if (fd > -1)
00202 ioc->fds[*id].fd = fd;
00203 if (callback)
00204 ioc->ior[*id].callback = callback;
00205 if (events)
00206 ioc->fds[*id].events = events;
00207 if (data)
00208 ioc->ior[*id].data = data;
00209
00210 return id;
00211 }
00212
00213 static int io_shrink(struct io_context *ioc)
00214 {
00215 int getfrom, putto = 0;
00216
00217
00218
00219
00220
00221
00222 for (getfrom = 0; getfrom < ioc->fdcnt; getfrom++) {
00223 if (ioc->ior[getfrom].id) {
00224
00225 if (getfrom != putto) {
00226 ioc->fds[putto] = ioc->fds[getfrom];
00227 ioc->ior[putto] = ioc->ior[getfrom];
00228 *(ioc->ior[putto].id) = putto;
00229 }
00230 putto++;
00231 }
00232 }
00233 ioc->fdcnt = putto;
00234 ioc->needshrink = 0;
00235
00236
00237 return 0;
00238 }
00239
00240 int ast_io_remove(struct io_context *ioc, int *_id)
00241 {
00242 int x;
00243
00244 if (!_id) {
00245 ast_log(LOG_WARNING, "Asked to remove NULL?\n");
00246 return -1;
00247 }
00248
00249 for (x = 0; x < ioc->fdcnt; x++) {
00250 if (ioc->ior[x].id == _id) {
00251
00252 ast_free(ioc->ior[x].id);
00253 ioc->ior[x].id = NULL;
00254 ioc->fds[x].events = 0;
00255 ioc->fds[x].revents = 0;
00256 ioc->needshrink = 1;
00257 if (ioc->current_ioc == -1)
00258 io_shrink(ioc);
00259 return 0;
00260 }
00261 }
00262
00263 ast_log(LOG_NOTICE, "Unable to remove unknown id %p\n", _id);
00264
00265 return -1;
00266 }
00267
00268
00269
00270
00271
00272
00273 int ast_io_wait(struct io_context *ioc, int howlong)
00274 {
00275 int res, x, origcnt;
00276
00277 DEBUG(ast_debug(1, "ast_io_wait()\n"));
00278
00279 if ((res = ast_poll(ioc->fds, ioc->fdcnt, howlong)) <= 0) {
00280 return res;
00281 }
00282
00283
00284 origcnt = ioc->fdcnt;
00285 for (x = 0; x < origcnt; x++) {
00286
00287
00288 if (ioc->fds[x].revents && ioc->ior[x].id) {
00289
00290 ioc->current_ioc = *ioc->ior[x].id;
00291 if (ioc->ior[x].callback) {
00292 if (!ioc->ior[x].callback(ioc->ior[x].id, ioc->fds[x].fd, ioc->fds[x].revents, ioc->ior[x].data)) {
00293
00294 ast_io_remove(ioc, ioc->ior[x].id);
00295 }
00296 }
00297 ioc->current_ioc = -1;
00298 }
00299 }
00300
00301 if (ioc->needshrink)
00302 io_shrink(ioc);
00303
00304 return res;
00305 }
00306
00307 void ast_io_dump(struct io_context *ioc)
00308 {
00309
00310
00311
00312
00313 int x;
00314
00315 ast_debug(1, "Asterisk IO Dump: %d entries, %d max entries\n", ioc->fdcnt, ioc->maxfdcnt);
00316 ast_debug(1, "================================================\n");
00317 ast_debug(1, "| ID FD Callback Data Events |\n");
00318 ast_debug(1, "+------+------+-----------+-----------+--------+\n");
00319 for (x = 0; x < ioc->fdcnt; x++) {
00320 ast_debug(1, "| %.4d | %.4d | %p | %p | %.6x |\n",
00321 *ioc->ior[x].id,
00322 ioc->fds[x].fd,
00323 ioc->ior[x].callback,
00324 ioc->ior[x].data,
00325 ioc->fds[x].events);
00326 }
00327 ast_debug(1, "================================================\n");
00328 }
00329
00330
00331
00332 int ast_hide_password(int fd)
00333 {
00334 struct termios tios;
00335 int res;
00336 int old;
00337 if (!isatty(fd))
00338 return -1;
00339 res = tcgetattr(fd, &tios);
00340 if (res < 0)
00341 return -1;
00342 old = tios.c_lflag & (ECHO | ECHONL);
00343 tios.c_lflag &= ~ECHO;
00344 tios.c_lflag |= ECHONL;
00345 res = tcsetattr(fd, TCSAFLUSH, &tios);
00346 if (res < 0)
00347 return -1;
00348 return old;
00349 }
00350
00351 int ast_restore_tty(int fd, int oldstate)
00352 {
00353 int res;
00354 struct termios tios;
00355 if (oldstate < 0)
00356 return 0;
00357 res = tcgetattr(fd, &tios);
00358 if (res < 0)
00359 return -1;
00360 tios.c_lflag &= ~(ECHO | ECHONL);
00361 tios.c_lflag |= oldstate;
00362 res = tcsetattr(fd, TCSAFLUSH, &tios);
00363 if (res < 0)
00364 return -1;
00365 return 0;
00366 }
00367
00368 int ast_get_termcols(int fd)
00369 {
00370 struct winsize win;
00371 int cols = 0;
00372
00373 if (!isatty(fd))
00374 return -1;
00375
00376 if ( ioctl(fd, TIOCGWINSZ, &win) != -1 ) {
00377 if ( !cols && win.ws_col > 0 )
00378 cols = (int) win.ws_col;
00379 } else {
00380
00381 cols = 80;
00382 }
00383
00384 return cols;
00385 }
00386