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