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