22 #define _XOPEN_SOURCE 700 26 #ifdef HAVE_SYS_SELECT_H 27 #include <sys/select.h> 30 #if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE) 31 #error "We can't compile without select() or poll() support." 38 #include <sys/types.h> 41 #include "curl_poll.h" 70 int curl_poll(
struct pollfd ufds[],
unsigned int nfds,
int timeout_ms)
72 return poll(ufds, nfds, timeout_ms);
77 static struct timeval curlx_tvnow(void)
86 (void)gettimeofday(&now, NULL);
97 long curlx_tvdiff(
struct timeval newer,
struct timeval older)
99 return (newer.tv_sec - older.tv_sec) * 1000 +
100 (long)(newer.tv_usec - older.tv_usec) / 1000;
104 static int verify_sock(
int s)
106 if (s < 0 || s >= FD_SETSIZE) {
108 log_notice(
"curl_poll: Invalid socket %d", s);
115 int curl_poll(
struct pollfd ufds[],
unsigned int nfds,
int timeout_ms)
117 struct timeval pending_tv;
118 struct timeval* ptimeout;
124 struct timeval initial_tv = { 0, 0 };
134 if (timeout_ms > 0) {
135 pending_ms = timeout_ms;
136 gettimeofday(&initial_tv, NULL);
144 for (i = 0; i < nfds; i++) {
146 if (ufds[i].fd == -1)
148 ufds[i].fd = verify_sock(ufds[i].fd);
149 if (ufds[i].events & (POLLIN | POLLOUT | POLLPRI |
150 POLLRDNORM | POLLWRNORM | POLLRDBAND)) {
151 if (ufds[i].fd > maxfd)
153 if (ufds[i].events & (POLLRDNORM | POLLIN))
154 FD_SET(ufds[i].fd, &fds_read);
155 if (ufds[i].events & (POLLWRNORM | POLLOUT))
156 FD_SET(ufds[i].fd, &fds_write);
157 if (ufds[i].events & (POLLRDBAND | POLLPRI))
158 FD_SET(ufds[i].fd, &fds_err);
162 ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
164 if (timeout_ms > 0) {
165 pending_tv.tv_sec = pending_ms / 1000;
166 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
167 }
else if (!timeout_ms) {
168 pending_tv.tv_sec = 0;
169 pending_tv.tv_usec = 0;
171 r = select((
int)maxfd + 1, &fds_read, &fds_write, &fds_err,
178 for (i = 0; i < nfds; i++) {
180 if (ufds[i].fd == -1)
182 if (FD_ISSET(ufds[i].fd, &fds_read))
183 ufds[i].revents |= POLLIN;
184 if (FD_ISSET(ufds[i].fd, &fds_write))
185 ufds[i].revents |= POLLOUT;
186 if (FD_ISSET(ufds[i].fd, &fds_err))
187 ufds[i].revents |= POLLPRI;
188 if (ufds[i].revents != 0)
#define log_notice(fmt,...)