00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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>
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
00032
00033 struct io_rec {
00034 ast_io_cb callback;
00035 void *data;
00036 int *id;
00037 };
00038
00039
00040
00041
00042
00043
00044
00045
00046 #define GROW_SHRINK_SIZE 512
00047
00048
00049
00050 struct io_context {
00051
00052 struct pollfd *fds;
00053
00054 struct io_rec *ior;
00055
00056 unsigned int fdcnt;
00057
00058 unsigned int maxfdcnt;
00059
00060 int current_ioc;
00061
00062 int needshrink;
00063 };
00064
00065
00066 struct io_context *io_context_create(void)
00067 {
00068
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
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
00108
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
00122
00123
00124
00125
00126 ioc->maxfdcnt -= GROW_SHRINK_SIZE;
00127 return -1;
00128 }
00129
00130 } else {
00131
00132
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
00144
00145
00146
00147 int *ret;
00148 DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n"));
00149 if (ioc->fdcnt >= ioc->maxfdcnt) {
00150
00151
00152
00153
00154 if (io_grow(ioc))
00155 return NULL;
00156 }
00157
00158
00159
00160
00161
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
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
00198
00199
00200
00201 for (getfrom=0;getfrom<ioc->fdcnt;getfrom++) {
00202 if (ioc->ior[getfrom].id) {
00203
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
00215
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
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
00248
00249
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
00259
00260 origcnt = ioc->fdcnt;
00261 for(x=0;x<origcnt;x++) {
00262
00263
00264 if (ioc->fds[x].revents && ioc->ior[x].id) {
00265
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
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
00286
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
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
00355 cols = 80;
00356 }
00357
00358 return cols;
00359 }
00360