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