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