1--- 2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al. 3SPDX-License-Identifier: curl 4Title: curl_multi_fdset 5Section: 3 6Source: libcurl 7See-also: 8 - curl_multi_cleanup (3) 9 - curl_multi_init (3) 10 - curl_multi_perform (3) 11 - curl_multi_timeout (3) 12 - curl_multi_wait (3) 13 - select (2) 14--- 15 16# NAME 17 18curl_multi_fdset - extracts file descriptor information from a multi handle 19 20# SYNOPSIS 21 22~~~c 23#include <curl/curl.h> 24 25CURLMcode curl_multi_fdset(CURLM *multi_handle, 26 fd_set *read_fd_set, 27 fd_set *write_fd_set, 28 fd_set *exc_fd_set, 29 int *max_fd); 30~~~ 31 32# DESCRIPTION 33 34This function extracts file descriptor information from a given multi_handle. 35libcurl returns its *fd_set* sets. The application can use these to 36select() on, but be sure to *FD_ZERO* them before calling this function as 37curl_multi_fdset(3) only adds its own descriptors, it does not zero or 38otherwise remove any others. The curl_multi_perform(3) function should 39be called as soon as one of them is ready to be read from or written to. 40 41The *read_fd_set* argument should point to an object of type **fd_set** 42that on returns specifies the file descriptors to be checked for being ready 43to read. 44 45The *write_fd_set* argument should point to an object of type **fd_set** 46that on return specifies the file descriptors to be checked for being ready to 47write. 48 49The *exc_fd_set* argument should point to an object of type **fd_set** 50that on return specifies the file descriptors to be checked for error 51conditions. 52 53If no file descriptors are set by libcurl, *max_fd* contain -1 when this 54function returns. Otherwise it contains the highest descriptor number libcurl 55set. When libcurl returns -1 in *max_fd*, it is because libcurl currently 56does something that is not possible for your application to monitor with a 57socket and unfortunately you can then not know exactly when the current action 58is completed using select(). You then need to wait a while before you proceed 59and call curl_multi_perform(3) anyway. How long to wait? Unless 60curl_multi_timeout(3) gives you a lower number, we suggest 100 61milliseconds or so, but you may want to test it out in your own particular 62conditions to find a suitable value. 63 64When doing select(), you should use curl_multi_timeout(3) to figure out 65how long to wait for action. Call curl_multi_perform(3) even if no 66activity has been seen on the **fd_sets** after the timeout expires as 67otherwise internal retries and timeouts may not work as you would think and 68want. 69 70If one of the sockets used by libcurl happens to be larger than what can be 71set in an **fd_set**, which on POSIX systems means that the file descriptor 72is larger than **FD_SETSIZE**, then libcurl tries to not set it. Setting a 73too large file descriptor in an **fd_set** implies an out of bounds write 74which can cause crashes, or worse. The effect of NOT storing it might possibly 75save you from the crash, but makes your program NOT wait for sockets it should 76wait for... 77 78# EXAMPLE 79 80~~~c 81int main(void) 82{ 83 fd_set fdread; 84 fd_set fdwrite; 85 fd_set fdexcep; 86 int maxfd; 87 int rc; 88 CURLMcode mc; 89 struct timeval timeout = {1, 0}; 90 91 CURLM *multi = curl_multi_init(); 92 93 do { 94 95 /* call curl_multi_perform() */ 96 97 /* get file descriptors from the transfers */ 98 mc = curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd); 99 100 if(mc != CURLM_OK) { 101 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); 102 break; 103 } 104 105 /* wait for activity on one of the sockets */ 106 rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); 107 108 } while(!mc); 109} 110~~~ 111 112# AVAILABILITY 113 114Added in 7.9.6 115 116# RETURN VALUE 117 118**CURLMcode** type, general libcurl multi interface error code. See 119libcurl-errors(3) 120