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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 #include "asterisk.h"
00075
00076 #include <unistd.h>
00077 #include <sys/types.h>
00078 #include <sys/time.h>
00079 #include <assert.h>
00080 #include <string.h>
00081 #include <errno.h>
00082
00083 #include "asterisk/utils.h"
00084 #include "asterisk/poll-compat.h"
00085
00086 unsigned int ast_FD_SETSIZE = FD_SETSIZE;
00087
00088
00089
00090
00091
00092 #if defined(AST_POLL_COMPAT)
00093 static int map_poll_spec(struct pollfd *pArray, unsigned long n_fds,
00094 ast_fdset *pReadSet, ast_fdset *pWriteSet, ast_fdset *pExceptSet)
00095 {
00096 register unsigned long i;
00097 register struct pollfd *pCur;
00098 register int max_fd = -1;
00099
00100
00101
00102
00103
00104 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00105
00106
00107 if (pCur->fd < 0) {
00108 continue;
00109 }
00110
00111 if (pCur->events & POLLIN) {
00112
00113 FD_SET(pCur->fd, pReadSet);
00114 }
00115
00116 if (pCur->events & POLLOUT) {
00117
00118 FD_SET(pCur->fd, pWriteSet);
00119 }
00120
00121 if (pCur->events & POLLPRI) {
00122
00123
00124
00125
00126 FD_SET(pCur->fd, pExceptSet);
00127 }
00128
00129 max_fd = MAX(max_fd, pCur->fd);
00130 }
00131
00132 return max_fd;
00133 }
00134
00135 #ifdef AST_POLL_COMPAT
00136 static struct timeval *map_timeout(int poll_timeout, struct timeval *pSelTimeout)
00137 {
00138 struct timeval *pResult;
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 assert(pSelTimeout != NULL);
00156
00157 switch (poll_timeout) {
00158 case -1:
00159
00160
00161
00162 pResult = (struct timeval *) NULL;
00163 break;
00164
00165 case 0:
00166
00167
00168
00169
00170 pSelTimeout->tv_sec = 0;
00171 pSelTimeout->tv_usec = 0;
00172 pResult = pSelTimeout;
00173 break;
00174
00175 default:
00176
00177 pSelTimeout->tv_sec = poll_timeout / 1000;
00178 poll_timeout %= 1000;
00179 pSelTimeout->tv_usec = poll_timeout * 1000;
00180 pResult = pSelTimeout;
00181 break;
00182 }
00183
00184 return pResult;
00185 }
00186 #endif
00187
00188 static void map_select_results(struct pollfd *pArray, unsigned long n_fds,
00189 ast_fdset *pReadSet, ast_fdset *pWriteSet, ast_fdset *pExceptSet)
00190 {
00191 register unsigned long i;
00192 register struct pollfd *pCur;
00193
00194 for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
00195
00196
00197 if (pCur->fd < 0) {
00198 continue;
00199 }
00200
00201
00202 pCur->revents = 0;
00203 if (FD_ISSET(pCur->fd, (fd_set *) pExceptSet)) {
00204 pCur->revents |= POLLPRI;
00205 } else if (FD_ISSET(pCur->fd, (fd_set *) pReadSet)) {
00206 pCur->revents |= POLLIN;
00207 }
00208
00209 if (FD_ISSET(pCur->fd, (fd_set *) pWriteSet)) {
00210 pCur->revents |= POLLOUT;
00211 }
00212 }
00213
00214 return;
00215 }
00216 #endif
00217
00218
00219
00220
00221 #ifdef AST_POLL_COMPAT
00222 int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
00223 {
00224 ast_fdset read_descs;
00225 ast_fdset write_descs;
00226 ast_fdset except_descs;
00227 struct timeval stime;
00228 int ready_descriptors;
00229 int max_fd = 0;
00230 struct timeval *pTimeout;
00231 int save_errno;
00232
00233 FD_ZERO(&read_descs);
00234 FD_ZERO(&write_descs);
00235 FD_ZERO(&except_descs);
00236
00237
00238
00239 if (pArray) {
00240 max_fd = map_poll_spec (pArray, n_fds,
00241 &read_descs, &write_descs, &except_descs);
00242 }
00243
00244
00245
00246 pTimeout = map_timeout (timeout, &stime);
00247
00248
00249
00250 ready_descriptors = ast_select(max_fd + 1, &read_descs, &write_descs,
00251 &except_descs, pTimeout);
00252 save_errno = errno;
00253
00254 if (ready_descriptors >= 0) {
00255 map_select_results (pArray, n_fds,
00256 &read_descs, &write_descs, &except_descs);
00257 }
00258
00259 errno = save_errno;
00260 return ready_descriptors;
00261 }
00262 #endif
00263
00264 int ast_poll2(struct pollfd *pArray, unsigned long n_fds, struct timeval *tv)
00265 {
00266 #if !defined(AST_POLL_COMPAT)
00267 struct timeval start = ast_tvnow();
00268 #if defined(HAVE_PPOLL)
00269 struct timespec ts = { tv ? tv->tv_sec : 0, tv ? tv->tv_usec * 1000 : 0 };
00270 int res = ppoll(pArray, n_fds, tv ? &ts : NULL, NULL);
00271 #else
00272 int res = poll(pArray, n_fds, tv ? tv->tv_sec * 1000 + tv->tv_usec / 1000 : -1);
00273 #endif
00274 struct timeval after = ast_tvnow();
00275 if (res > 0 && tv && ast_tvdiff_ms(ast_tvadd(*tv, start), after) > 0) {
00276 *tv = ast_tvsub(*tv, ast_tvsub(after, start));
00277 } else if (res > 0 && tv) {
00278 *tv = ast_tv(0, 0);
00279 }
00280 return res;
00281 #else
00282 ast_fdset read_descs, write_descs, except_descs;
00283 int ready_descriptors, max_fd = 0;
00284
00285 FD_ZERO(&read_descs);
00286 FD_ZERO(&write_descs);
00287 FD_ZERO(&except_descs);
00288
00289 if (pArray) {
00290 max_fd = map_poll_spec(pArray, n_fds, &read_descs, &write_descs, &except_descs);
00291 }
00292
00293 ready_descriptors = ast_select(max_fd + 1, &read_descs, &write_descs, &except_descs, tv);
00294
00295 if (ready_descriptors >= 0) {
00296 map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
00297 }
00298
00299 return ready_descriptors;
00300 #endif
00301 }
00302
00303