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