1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
27 #ifdef HAVE_SYS_SELECT_H
28 #include <sys/select.h>
29 #elif defined(HAVE_UNISTD_H)
33 #if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE)
34 #error "We can't compile without select() or poll() support."
38 #include <dos.h> /* delay() */
41 #include <curl/curl.h>
50 * Internal function used for waiting a specific amount of ms
51 * in Curl_socket_check() and Curl_poll() when no file descriptor
52 * is provided to wait on, just being used to delay execution.
53 * WinSock select() and poll() timeout mechanisms need a valid
54 * socket descriptor in a not null file descriptor set to work.
55 * Waiting indefinitely with this function is not allowed, a
56 * zero or negative timeout value will return immediately.
57 * Timeout resolution, accuracy, as well as maximum supported
58 * value is system dependent, neither factor is a critical issue
59 * for the intended use of this function in the library.
62 * -1 = system call error, invalid timeout value, or interrupted
63 * 0 = specified timeout has elapsed
65 int Curl_wait_ms(timediff_t timeout_ms)
72 SET_SOCKERRNO(EINVAL);
78 /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */
79 #if TIMEDIFF_T_MAX >= ULONG_MAX
80 if(timeout_ms >= ULONG_MAX)
81 timeout_ms = ULONG_MAX-1;
82 /* don't use ULONG_MAX, because that is equal to INFINITE */
84 Sleep((ULONG)timeout_ms);
86 #if defined(HAVE_POLL_FINE)
87 /* prevent overflow, timeout_ms is typecast to int. */
88 #if TIMEDIFF_T_MAX > INT_MAX
89 if(timeout_ms > INT_MAX)
92 r = poll(NULL, 0, (int)timeout_ms);
95 struct timeval pending_tv;
96 r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms));
98 #endif /* HAVE_POLL_FINE */
99 #endif /* USE_WINSOCK */
105 #ifndef HAVE_POLL_FINE
107 * This is a wrapper around select() to aid in Windows compatibility.
108 * A negative timeout value makes this function wait indefinitely,
109 * unless no valid file descriptor is given, when this happens the
110 * negative timeout is ignored and the function times out immediately.
113 * -1 = system call error or fd >= FD_SETSIZE
115 * N = number of signalled file descriptors
117 static int our_select(curl_socket_t maxfd, /* highest socket number */
118 fd_set *fds_read, /* sockets ready for reading */
119 fd_set *fds_write, /* sockets ready for writing */
120 fd_set *fds_err, /* sockets with errors */
121 timediff_t timeout_ms) /* milliseconds to wait */
123 struct timeval pending_tv;
124 struct timeval *ptimeout;
127 /* WinSock select() can't handle zero events. See the comment below. */
128 if((!fds_read || fds_read->fd_count == 0) &&
129 (!fds_write || fds_write->fd_count == 0) &&
130 (!fds_err || fds_err->fd_count == 0)) {
131 /* no sockets, just wait */
132 return Curl_wait_ms(timeout_ms);
136 ptimeout = curlx_mstotv(&pending_tv, timeout_ms);
139 /* WinSock select() must not be called with an fd_set that contains zero
140 fd flags, or it will return WSAEINVAL. But, it also can't be called
141 with no fd_sets at all! From the documentation:
143 Any two of the parameters, readfds, writefds, or exceptfds, can be
144 given as null. At least one must be non-null, and any non-null
145 descriptor set must contain at least one handle to a socket.
147 It is unclear why WinSock doesn't just handle this for us instead of
148 calling this an error. Luckily, with WinSock, we can _also_ ask how
149 many bits are set on an fd_set. So, let's just check it beforehand.
151 return select((int)maxfd + 1,
152 fds_read && fds_read->fd_count ? fds_read : NULL,
153 fds_write && fds_write->fd_count ? fds_write : NULL,
154 fds_err && fds_err->fd_count ? fds_err : NULL, ptimeout);
156 return select((int)maxfd + 1, fds_read, fds_write, fds_err, ptimeout);
163 * Wait for read or write events on a set of file descriptors. It uses poll()
164 * when a fine poll() is available, in order to avoid limits with FD_SETSIZE,
165 * otherwise select() is used. An error is returned if select() is being used
166 * and a file descriptor is too large for FD_SETSIZE.
168 * A negative timeout value makes this function wait indefinitely,
169 * unless no valid file descriptor is given, when this happens the
170 * negative timeout is ignored and the function times out immediately.
173 * -1 = system call error or fd >= FD_SETSIZE
175 * [bitmask] = action as described below
177 * CURL_CSELECT_IN - first socket is readable
178 * CURL_CSELECT_IN2 - second socket is readable
179 * CURL_CSELECT_OUT - write socket is writable
180 * CURL_CSELECT_ERR - an error condition occurred
182 int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
183 curl_socket_t readfd1,
184 curl_socket_t writefd, /* socket to write to */
185 timediff_t timeout_ms) /* milliseconds to wait */
187 struct pollfd pfd[3];
191 if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
192 (writefd == CURL_SOCKET_BAD)) {
193 /* no sockets, just wait */
194 return Curl_wait_ms(timeout_ms);
197 /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
198 time in this function does not need to be measured. This happens
199 when function is called with a zero timeout or a negative timeout
200 value indicating a blocking call should be performed. */
203 if(readfd0 != CURL_SOCKET_BAD) {
204 pfd[num].fd = readfd0;
205 pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
206 pfd[num].revents = 0;
209 if(readfd1 != CURL_SOCKET_BAD) {
210 pfd[num].fd = readfd1;
211 pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
212 pfd[num].revents = 0;
215 if(writefd != CURL_SOCKET_BAD) {
216 pfd[num].fd = writefd;
217 pfd[num].events = POLLWRNORM|POLLOUT|POLLPRI;
218 pfd[num].revents = 0;
222 r = Curl_poll(pfd, num, timeout_ms);
228 if(readfd0 != CURL_SOCKET_BAD) {
229 if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
230 r |= CURL_CSELECT_IN;
231 if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
232 r |= CURL_CSELECT_ERR;
235 if(readfd1 != CURL_SOCKET_BAD) {
236 if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
237 r |= CURL_CSELECT_IN2;
238 if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
239 r |= CURL_CSELECT_ERR;
242 if(writefd != CURL_SOCKET_BAD) {
243 if(pfd[num].revents & (POLLWRNORM|POLLOUT))
244 r |= CURL_CSELECT_OUT;
245 if(pfd[num].revents & (POLLERR|POLLHUP|POLLPRI|POLLNVAL))
246 r |= CURL_CSELECT_ERR;
253 * This is a wrapper around poll(). If poll() does not exist, then
254 * select() is used instead. An error is returned if select() is
255 * being used and a file descriptor is too large for FD_SETSIZE.
256 * A negative timeout value makes this function wait indefinitely,
257 * unless no valid file descriptor is given, when this happens the
258 * negative timeout is ignored and the function times out immediately.
261 * -1 = system call error or fd >= FD_SETSIZE
263 * N = number of structures with non zero revent fields
265 int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms)
267 #ifdef HAVE_POLL_FINE
275 bool fds_none = TRUE;
280 for(i = 0; i < nfds; i++) {
281 if(ufds[i].fd != CURL_SOCKET_BAD) {
288 /* no sockets, just wait */
289 return Curl_wait_ms(timeout_ms);
292 /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
293 time in this function does not need to be measured. This happens
294 when function is called with a zero timeout or a negative timeout
295 value indicating a blocking call should be performed. */
297 #ifdef HAVE_POLL_FINE
299 /* prevent overflow, timeout_ms is typecast to int. */
300 #if TIMEDIFF_T_MAX > INT_MAX
301 if(timeout_ms > INT_MAX)
302 timeout_ms = INT_MAX;
305 pending_ms = (int)timeout_ms;
306 else if(timeout_ms < 0)
310 r = poll(ufds, nfds, pending_ms);
314 for(i = 0; i < nfds; i++) {
315 if(ufds[i].fd == CURL_SOCKET_BAD)
317 if(ufds[i].revents & POLLHUP)
318 ufds[i].revents |= POLLIN;
319 if(ufds[i].revents & POLLERR)
320 ufds[i].revents |= POLLIN|POLLOUT;
323 #else /* HAVE_POLL_FINE */
328 maxfd = (curl_socket_t)-1;
330 for(i = 0; i < nfds; i++) {
332 if(ufds[i].fd == CURL_SOCKET_BAD)
334 VERIFY_SOCK(ufds[i].fd);
335 if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
336 POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
337 if(ufds[i].fd > maxfd)
339 if(ufds[i].events & (POLLRDNORM|POLLIN))
340 FD_SET(ufds[i].fd, &fds_read);
341 if(ufds[i].events & (POLLWRNORM|POLLOUT))
342 FD_SET(ufds[i].fd, &fds_write);
343 if(ufds[i].events & (POLLRDBAND|POLLPRI))
344 FD_SET(ufds[i].fd, &fds_err);
349 Note also that WinSock ignores the first argument, so we don't worry
350 about the fact that maxfd is computed incorrectly with WinSock (since
351 curl_socket_t is unsigned in such cases and thus -1 is the largest
354 r = our_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
359 for(i = 0; i < nfds; i++) {
361 if(ufds[i].fd == CURL_SOCKET_BAD)
363 if(FD_ISSET(ufds[i].fd, &fds_read)) {
364 if(ufds[i].events & POLLRDNORM)
365 ufds[i].revents |= POLLRDNORM;
366 if(ufds[i].events & POLLIN)
367 ufds[i].revents |= POLLIN;
369 if(FD_ISSET(ufds[i].fd, &fds_write)) {
370 if(ufds[i].events & POLLWRNORM)
371 ufds[i].revents |= POLLWRNORM;
372 if(ufds[i].events & POLLOUT)
373 ufds[i].revents |= POLLOUT;
375 if(FD_ISSET(ufds[i].fd, &fds_err)) {
376 if(ufds[i].events & POLLRDBAND)
377 ufds[i].revents |= POLLRDBAND;
378 if(ufds[i].events & POLLPRI)
379 ufds[i].revents |= POLLPRI;
385 #endif /* HAVE_POLL_FINE */