Coverage Report

Created: 2026-12-01 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/select.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
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.
13
 *
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.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifdef HAVE_SYS_SELECT_H
27
#include <sys/select.h>
28
#elif defined(HAVE_UNISTD_H)
29
#include <unistd.h>
30
#endif
31
32
#include "urldata.h"
33
#include "connect.h"
34
#include "select.h"
35
#include "curl_trc.h"
36
#include "curlx/timediff.h"
37
#include "curlx/wait.h"
38
39
#ifndef HAVE_POLL  /* use select() */
40
/*
41
 * This is a wrapper around select() to aid in Windows compatibility. A
42
 * negative timeout value makes this function wait indefinitely, unless no
43
 * valid file descriptor is given, when this happens the negative timeout is
44
 * ignored and the function times out immediately.
45
 *
46
 * Return values:
47
 *   -1 = system call error or fd >= FD_SETSIZE
48
 *    0 = timeout
49
 *    N = number of signalled file descriptors
50
 */
51
static int our_select(curl_socket_t maxfd,   /* highest socket number */
52
                      fd_set *fds_read,      /* sockets ready for reading */
53
                      fd_set *fds_write,     /* sockets ready for writing */
54
                      fd_set *fds_err,       /* sockets with errors */
55
                      timediff_t timeout_ms) /* milliseconds to wait */
56
{
57
  struct timeval pending_tv;
58
  struct timeval *ptimeout;
59
60
#ifdef USE_WINSOCK
61
  /* Winsock select() cannot handle zero events. See the comment below. */
62
  if((!fds_read || fds_read->fd_count == 0) &&
63
     (!fds_write || fds_write->fd_count == 0) &&
64
     (!fds_err || fds_err->fd_count == 0)) {
65
    /* no sockets, wait */
66
    return curlx_wait_ms(timeout_ms);
67
  }
68
#endif
69
70
  ptimeout = curlx_mstotv(&pending_tv, timeout_ms);
71
72
#ifdef USE_WINSOCK
73
  /* Winsock select() must not be called with an fd_set that contains zero
74
     fd flags, or it will return WSAEINVAL. It also cannot be called with
75
     no fd_sets at all!  From the documentation:
76
77
     Any two of the parameters, readfds, writefds, or exceptfds, can be
78
     given as null. At least one must be non-null, and any non-null
79
     descriptor set must contain at least one handle to a socket.
80
81
     It is unclear why Winsock does not handle this for us instead of
82
     calling this an error. Luckily, with Winsock, we can _also_ ask how
83
     many bits are set on an fd_set. Therefore, let's check it beforehand.
84
   */
85
  return select((int)maxfd + 1,
86
                fds_read && fds_read->fd_count ? fds_read : NULL,
87
                fds_write && fds_write->fd_count ? fds_write : NULL,
88
                fds_err && fds_err->fd_count ? fds_err : NULL, ptimeout);
89
#else
90
  return select((int)maxfd + 1, fds_read, fds_write, fds_err, ptimeout);
91
#endif
92
}
93
94
#endif
95
96
/*
97
 * Wait for read or write events on a set of file descriptors. It uses poll()
98
 * when poll() is available, in order to avoid limits with FD_SETSIZE,
99
 * otherwise select() is used. An error is returned if select() is being used
100
 * and a file descriptor is too large for FD_SETSIZE.
101
 *
102
 * A negative timeout value makes this function wait indefinitely, unless no
103
 * valid file descriptor is given, when this happens the negative timeout is
104
 * ignored and the function times out immediately.
105
 *
106
 * Return values:
107
 *   -1 = system call error or fd >= FD_SETSIZE
108
 *    0 = timeout
109
 *    [bitmask] = action as described below
110
 *
111
 * CURL_CSELECT_IN - first socket is readable
112
 * CURL_CSELECT_OUT - write socket is writable
113
 * CURL_CSELECT_ERR - an error condition occurred
114
 */
115
int Curl_socket_check(curl_socket_t readfd, /* socket to read from */
116
                      curl_socket_t writefd, /* socket to write to */
117
                      timediff_t timeout_ms) /* milliseconds to wait */
118
0
{
119
0
  struct pollfd pfd[2];
120
0
  int num;
121
0
  int r;
122
123
0
  if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
124
    /* no sockets, wait */
125
0
    return curlx_wait_ms(timeout_ms);
126
0
  }
127
128
  /* Avoid initial timestamp, avoid curlx_now() call, when elapsed
129
     time in this function does not need to be measured. This happens
130
     when function is called with a zero timeout or a negative timeout
131
     value indicating a blocking call should be performed. */
132
133
0
  num = 0;
134
0
  if(readfd != CURL_SOCKET_BAD) {
135
0
    pfd[num].fd = readfd;
136
0
    pfd[num].events = POLLRDNORM | POLLIN | POLLRDBAND | POLLPRI;
137
0
    pfd[num].revents = 0;
138
0
    num++;
139
0
  }
140
0
  if(writefd != CURL_SOCKET_BAD) {
141
0
    pfd[num].fd = writefd;
142
0
    pfd[num].events = POLLWRNORM | POLLOUT | POLLPRI;
143
0
    pfd[num].revents = 0;
144
0
    num++;
145
0
  }
146
147
0
  r = Curl_poll(pfd, (unsigned int)num, timeout_ms);
148
0
  if(r <= 0)
149
0
    return r;
150
151
0
  r = 0;
152
0
  num = 0;
153
0
  if(readfd != CURL_SOCKET_BAD) {
154
0
    if(pfd[num].revents & (POLLRDNORM | POLLIN | POLLERR | POLLHUP))
155
0
      r |= CURL_CSELECT_IN;
156
0
    if(pfd[num].revents & (POLLPRI | POLLNVAL))
157
0
      r |= CURL_CSELECT_ERR;
158
0
    num++;
159
0
  }
160
0
  if(writefd != CURL_SOCKET_BAD) {
161
0
    if(pfd[num].revents & (POLLWRNORM | POLLOUT))
162
0
      r |= CURL_CSELECT_OUT;
163
0
    if(pfd[num].revents & (POLLERR | POLLHUP | POLLPRI | POLLNVAL))
164
0
      r |= CURL_CSELECT_ERR;
165
0
  }
166
167
0
  return r;
168
0
}
169
170
/*
171
 * This is a wrapper around poll(). If poll() does not exist, then
172
 * select() is used instead. An error is returned if select() is
173
 * being used and a file descriptor is too large for FD_SETSIZE.
174
 * A negative timeout value makes this function wait indefinitely,
175
 * unless no valid file descriptor is given, when this happens the
176
 * negative timeout is ignored and the function times out immediately.
177
 *
178
 * Return values:
179
 *   -1 = system call error or fd >= FD_SETSIZE
180
 *    0 = timeout
181
 *    N = number of structures with non zero revent fields
182
 */
183
int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms)
184
0
{
185
0
#ifdef HAVE_POLL
186
0
  int pending_ms;
187
#else
188
  fd_set fds_read;
189
  fd_set fds_write;
190
  fd_set fds_err;
191
  curl_socket_t maxfd;
192
#endif
193
0
  bool fds_none = TRUE;
194
0
  unsigned int i;
195
0
  int r;
196
197
0
  if(ufds) {
198
0
    for(i = 0; i < nfds; i++) {
199
0
      if(ufds[i].fd != CURL_SOCKET_BAD) {
200
0
        fds_none = FALSE;
201
0
        break;
202
0
      }
203
0
    }
204
0
  }
205
0
  if(fds_none) {
206
    /* no sockets, wait */
207
0
    return curlx_wait_ms(timeout_ms);
208
0
  }
209
210
  /* Avoid initial timestamp, avoid curlx_now() call, when elapsed
211
     time in this function does not need to be measured. This happens
212
     when function is called with a zero timeout or a negative timeout
213
     value indicating a blocking call should be performed. */
214
215
0
#ifdef HAVE_POLL
216
217
  /* prevent overflow, timeout_ms is typecast to int. */
218
0
#if TIMEDIFF_T_MAX > INT_MAX
219
0
  if(timeout_ms > INT_MAX)
220
0
    timeout_ms = INT_MAX;
221
0
#endif
222
0
  if(timeout_ms > 0)
223
0
    pending_ms = (int)timeout_ms;
224
0
  else if(timeout_ms < 0)
225
0
    pending_ms = -1;
226
0
  else
227
0
    pending_ms = 0;
228
0
  r = poll(ufds, nfds, pending_ms);
229
0
  if(r <= 0) {
230
0
    if((r == -1) && (SOCKERRNO == SOCKEINTR))
231
      /* make EINTR from select or poll not a "lethal" error */
232
0
      r = 0;
233
0
    return r;
234
0
  }
235
236
0
  for(i = 0; i < nfds; i++) {
237
0
    if(ufds[i].fd == CURL_SOCKET_BAD)
238
0
      continue;
239
0
    if(ufds[i].revents & POLLHUP)
240
0
      ufds[i].revents |= POLLIN;
241
0
    if(ufds[i].revents & POLLERR)
242
0
      ufds[i].revents |= POLLIN | POLLOUT;
243
0
  }
244
245
#else /* !HAVE_POLL */
246
247
  FD_ZERO(&fds_read);
248
  FD_ZERO(&fds_write);
249
  FD_ZERO(&fds_err);
250
  maxfd = (curl_socket_t)-1;
251
252
  for(i = 0; i < nfds; i++) {
253
    ufds[i].revents = 0;
254
    if(ufds[i].fd == CURL_SOCKET_BAD)
255
      continue;
256
    VERIFY_SOCK(ufds[i].fd);
257
    if(ufds[i].events & (POLLIN | POLLOUT | POLLPRI |
258
                         POLLRDNORM | POLLWRNORM | POLLRDBAND)) {
259
      if(ufds[i].fd > maxfd)
260
        maxfd = ufds[i].fd;
261
      if(ufds[i].events & (POLLRDNORM | POLLIN))
262
        FD_SET(ufds[i].fd, &fds_read);
263
      if(ufds[i].events & (POLLWRNORM | POLLOUT))
264
        FD_SET(ufds[i].fd, &fds_write);
265
      if(ufds[i].events & (POLLRDBAND | POLLPRI))
266
        FD_SET(ufds[i].fd, &fds_err);
267
    }
268
  }
269
270
  /* Note also that Winsock ignores the first argument, so we do not worry
271
     about the fact that maxfd is computed incorrectly with Winsock (since
272
     curl_socket_t is unsigned in such cases and thus -1 is the largest
273
     value). */
274
  r = our_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
275
  if(r <= 0) {
276
    if((r == -1) && (SOCKERRNO == SOCKEINTR))
277
      /* make EINTR from select or poll not a "lethal" error */
278
      r = 0;
279
    return r;
280
  }
281
282
  r = 0;
283
  for(i = 0; i < nfds; i++) {
284
    ufds[i].revents = 0;
285
    if(ufds[i].fd == CURL_SOCKET_BAD)
286
      continue;
287
    if(FD_ISSET(ufds[i].fd, &fds_read)) {
288
      if(ufds[i].events & POLLRDNORM)
289
        ufds[i].revents |= POLLRDNORM;
290
      if(ufds[i].events & POLLIN)
291
        ufds[i].revents |= POLLIN;
292
    }
293
    if(FD_ISSET(ufds[i].fd, &fds_write)) {
294
      if(ufds[i].events & POLLWRNORM)
295
        ufds[i].revents |= POLLWRNORM;
296
      if(ufds[i].events & POLLOUT)
297
        ufds[i].revents |= POLLOUT;
298
    }
299
    if(FD_ISSET(ufds[i].fd, &fds_err)) {
300
      if(ufds[i].events & POLLRDBAND)
301
        ufds[i].revents |= POLLRDBAND;
302
      if(ufds[i].events & POLLPRI)
303
        ufds[i].revents |= POLLPRI;
304
    }
305
    if(ufds[i].revents)
306
      r++;
307
  }
308
309
#endif /* HAVE_POLL */
310
311
0
  return r;
312
0
}
313
314
void Curl_pollfds_init(struct curl_pollfds *cpfds,
315
                       struct pollfd *static_pfds,
316
                       unsigned int static_count)
317
0
{
318
0
  DEBUGASSERT(cpfds);
319
0
  memset(cpfds, 0, sizeof(*cpfds));
320
0
  if(static_pfds && static_count) {
321
0
    cpfds->pfds = static_pfds;
322
0
    cpfds->count = static_count;
323
0
  }
324
0
}
325
326
void Curl_pollfds_reset(struct curl_pollfds *cpfds)
327
0
{
328
0
  cpfds->n = 0;
329
0
}
330
331
void Curl_pollfds_cleanup(struct curl_pollfds *cpfds)
332
0
{
333
0
  DEBUGASSERT(cpfds);
334
0
  if(cpfds->allocated_pfds) {
335
0
    curlx_safefree(cpfds->pfds);
336
0
    cpfds->count = 0;
337
0
    cpfds->allocated_pfds = FALSE;
338
0
  }
339
0
  cpfds->n = 0;
340
0
}
341
342
static CURLcode cpfds_increase(struct curl_pollfds *cpfds, unsigned int inc)
343
0
{
344
0
  struct pollfd *new_fds;
345
0
  unsigned int new_count = cpfds->count + inc;
346
347
0
  new_fds = curlx_calloc(new_count, sizeof(struct pollfd));
348
0
  if(!new_fds)
349
0
    return CURLE_OUT_OF_MEMORY;
350
351
0
  memcpy(new_fds, cpfds->pfds, cpfds->count * sizeof(struct pollfd));
352
0
  if(cpfds->allocated_pfds)
353
0
    curlx_free(cpfds->pfds);
354
0
  cpfds->pfds = new_fds;
355
0
  cpfds->count = new_count;
356
0
  cpfds->allocated_pfds = TRUE;
357
0
  return CURLE_OK;
358
0
}
359
360
static CURLcode cpfds_add_sock(struct curl_pollfds *cpfds,
361
                               curl_socket_t sock, short events, bool fold)
362
0
{
363
0
  int i;
364
365
0
  if(fold && cpfds->n <= INT_MAX) {
366
0
    for(i = (int)cpfds->n - 1; i >= 0; --i) {
367
0
      if(sock == cpfds->pfds[i].fd) {
368
0
        cpfds->pfds[i].events |= events;
369
0
        return CURLE_OK;
370
0
      }
371
0
    }
372
0
  }
373
  /* not folded, add new entry */
374
0
  if(cpfds->n >= cpfds->count && cpfds_increase(cpfds, 100))
375
0
    return CURLE_OUT_OF_MEMORY;
376
0
  cpfds->pfds[cpfds->n].fd = sock;
377
0
  cpfds->pfds[cpfds->n].events = events;
378
0
  ++cpfds->n;
379
0
  return CURLE_OK;
380
0
}
381
382
CURLcode Curl_pollfds_add_sock(struct curl_pollfds *cpfds,
383
                               curl_socket_t sock, short events)
384
0
{
385
0
  return cpfds_add_sock(cpfds, sock, events, FALSE);
386
0
}
387
388
CURLcode Curl_pollfds_add_ps(struct curl_pollfds *cpfds,
389
                             struct easy_pollset *ps)
390
0
{
391
0
  size_t i;
392
393
0
  DEBUGASSERT(cpfds);
394
0
  DEBUGASSERT(ps);
395
0
  for(i = 0; i < ps->n; i++) {
396
0
    short events = 0;
397
0
    if(ps->actions[i] & CURL_POLL_IN)
398
0
      events |= POLLIN;
399
0
    if(ps->actions[i] & CURL_POLL_OUT)
400
0
      events |= POLLOUT;
401
0
    if(events && cpfds_add_sock(cpfds, ps->sockets[i], events, TRUE))
402
0
      return CURLE_OUT_OF_MEMORY;
403
0
  }
404
0
  return CURLE_OK;
405
0
}
406
407
void Curl_waitfds_init(struct Curl_waitfds *cwfds,
408
                       struct curl_waitfd *static_wfds,
409
                       unsigned int static_count)
410
0
{
411
0
  DEBUGASSERT(cwfds);
412
0
  DEBUGASSERT(static_wfds || !static_count);
413
0
  memset(cwfds, 0, sizeof(*cwfds));
414
0
  cwfds->wfds = static_wfds;
415
0
  cwfds->count = static_count;
416
0
}
417
418
static unsigned int cwfds_add_sock(struct Curl_waitfds *cwfds,
419
                                   curl_socket_t sock, short events)
420
0
{
421
0
  int i;
422
0
  if(!cwfds->wfds) {
423
0
    DEBUGASSERT(!cwfds->count && !cwfds->n);
424
0
    return 1;
425
0
  }
426
0
  if(cwfds->n <= INT_MAX) {
427
0
    for(i = (int)cwfds->n - 1; i >= 0; --i) {
428
0
      if(sock == cwfds->wfds[i].fd) {
429
0
        cwfds->wfds[i].events |= events;
430
0
        return 0;
431
0
      }
432
0
    }
433
0
  }
434
  /* not folded, add new entry */
435
0
  if(cwfds->n < cwfds->count) {
436
0
    cwfds->wfds[cwfds->n].fd = sock;
437
0
    cwfds->wfds[cwfds->n].events = events;
438
0
    ++cwfds->n;
439
0
  }
440
0
  return 1;
441
0
}
442
443
unsigned int Curl_waitfds_add_ps(struct Curl_waitfds *cwfds,
444
                                 struct easy_pollset *ps)
445
0
{
446
0
  size_t i;
447
0
  unsigned int need = 0;
448
449
0
  DEBUGASSERT(cwfds);
450
0
  DEBUGASSERT(ps);
451
0
  for(i = 0; i < ps->n; i++) {
452
0
    short events = 0;
453
0
    if(ps->actions[i] & CURL_POLL_IN)
454
0
      events |= CURL_WAIT_POLLIN;
455
0
    if(ps->actions[i] & CURL_POLL_OUT)
456
0
      events |= CURL_WAIT_POLLOUT;
457
0
    if(events)
458
0
      need += cwfds_add_sock(cwfds, ps->sockets[i], events);
459
0
  }
460
0
  return need;
461
0
}
462
463
void Curl_pollset_reset(struct easy_pollset *ps)
464
0
{
465
0
#ifdef DEBUGBUILD
466
0
  DEBUGASSERT(ps->init == CURL_EASY_POLLSET_MAGIC);
467
0
#endif
468
0
  ps->n = 0;
469
0
}
470
471
void Curl_pollset_init(struct easy_pollset *ps)
472
0
{
473
0
#ifdef DEBUGBUILD
474
0
  ps->init = CURL_EASY_POLLSET_MAGIC;
475
0
#endif
476
0
  ps->sockets = ps->def_sockets;
477
0
  ps->actions = ps->def_actions;
478
0
  ps->count = CURL_ARRAYSIZE(ps->def_sockets);
479
0
  ps->n = 0;
480
0
  Curl_pollset_reset(ps);
481
0
}
482
483
struct easy_pollset *Curl_pollset_create(void)
484
0
{
485
0
  struct easy_pollset *ps = curlx_calloc(1, sizeof(*ps));
486
0
  if(ps)
487
0
    Curl_pollset_init(ps);
488
0
  return ps;
489
0
}
490
491
void Curl_pollset_cleanup(struct easy_pollset *ps)
492
0
{
493
0
#ifdef DEBUGBUILD
494
0
  DEBUGASSERT(ps->init == CURL_EASY_POLLSET_MAGIC);
495
0
#endif
496
0
  if(ps->sockets != ps->def_sockets) {
497
0
    curlx_free(ps->sockets);
498
0
    ps->sockets = ps->def_sockets;
499
0
  }
500
0
  if(ps->actions != ps->def_actions) {
501
0
    curlx_free(ps->actions);
502
0
    ps->actions = ps->def_actions;
503
0
  }
504
0
  ps->count = CURL_ARRAYSIZE(ps->def_sockets);
505
0
  Curl_pollset_reset(ps);
506
0
}
507
508
void Curl_pollset_move(struct easy_pollset *to, struct easy_pollset *from)
509
0
{
510
0
  Curl_pollset_cleanup(to); /* deallocate anything in to */
511
0
  if(from->sockets != from->def_sockets) {
512
0
    DEBUGASSERT(from->actions != from->def_actions);
513
0
    to->sockets = from->sockets;
514
0
    to->actions = from->actions;
515
0
    to->count = from->count;
516
0
    to->n = from->n;
517
0
    Curl_pollset_init(from);
518
0
  }
519
0
  else {
520
0
    DEBUGASSERT(to->sockets == to->def_sockets);
521
0
    DEBUGASSERT(to->actions == to->def_actions);
522
0
    memcpy(to->sockets, from->sockets, to->count * sizeof(to->sockets[0]));
523
0
    memcpy(to->actions, from->actions, to->count * sizeof(to->actions[0]));
524
0
    to->n = from->n;
525
0
    Curl_pollset_init(from);
526
0
  }
527
0
}
528
529
/**
530
 *
531
 */
532
CURLcode Curl_pollset_change(struct Curl_easy *data,
533
                             struct easy_pollset *ps, curl_socket_t sock,
534
                             int add_flags, int remove_flags)
535
0
{
536
0
  unsigned int i;
537
538
0
#ifdef DEBUGBUILD
539
0
  DEBUGASSERT(ps->init == CURL_EASY_POLLSET_MAGIC);
540
0
#endif
541
542
0
  DEBUGASSERT(VALID_SOCK(sock));
543
0
  if(!VALID_SOCK(sock))
544
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
545
546
0
  DEBUGASSERT(add_flags <= (CURL_POLL_IN | CURL_POLL_OUT));
547
0
  DEBUGASSERT(remove_flags <= (CURL_POLL_IN | CURL_POLL_OUT));
548
0
  DEBUGASSERT((add_flags & remove_flags) == 0); /* no overlap */
549
0
  for(i = 0; i < ps->n; ++i) {
550
0
    if(ps->sockets[i] == sock) {
551
0
      ps->actions[i] &= (unsigned char)(~remove_flags);
552
0
      ps->actions[i] |= (unsigned char)add_flags;
553
      /* all gone? remove socket */
554
0
      if(!ps->actions[i]) {
555
0
        if((i + 1) < ps->n) {
556
0
          memmove(&ps->sockets[i], &ps->sockets[i + 1],
557
0
                  (ps->n - (i + 1)) * sizeof(ps->sockets[0]));
558
0
          memmove(&ps->actions[i], &ps->actions[i + 1],
559
0
                  (ps->n - (i + 1)) * sizeof(ps->actions[0]));
560
0
        }
561
0
        --ps->n;
562
0
      }
563
0
      return CURLE_OK;
564
0
    }
565
0
  }
566
  /* not present */
567
0
  if(add_flags) {
568
0
    if(i >= ps->count) { /* need to grow */
569
0
      unsigned int new_count = CURLMAX(ps->count * 2, 8);
570
0
      curl_socket_t *nsockets;
571
0
      unsigned char *nactions;
572
573
0
      CURL_TRC_M(data, "growing pollset capacity from %u to %u",
574
0
                 ps->count, new_count);
575
0
      if(new_count <= ps->count)
576
0
        return CURLE_OUT_OF_MEMORY;
577
0
      nsockets = curlx_calloc(new_count, sizeof(nsockets[0]));
578
0
      if(!nsockets)
579
0
        return CURLE_OUT_OF_MEMORY;
580
0
      nactions = curlx_calloc(new_count, sizeof(nactions[0]));
581
0
      if(!nactions) {
582
0
        curlx_free(nsockets);
583
0
        return CURLE_OUT_OF_MEMORY;
584
0
      }
585
0
      memcpy(nsockets, ps->sockets, ps->count * sizeof(ps->sockets[0]));
586
0
      memcpy(nactions, ps->actions, ps->count * sizeof(ps->actions[0]));
587
0
      if(ps->sockets != ps->def_sockets)
588
0
        curlx_free(ps->sockets);
589
0
      ps->sockets = nsockets;
590
0
      if(ps->actions != ps->def_actions)
591
0
        curlx_free(ps->actions);
592
0
      ps->actions = nactions;
593
0
      ps->count = new_count;
594
0
    }
595
0
    DEBUGASSERT(i < ps->count);
596
0
    if(i < ps->count) {
597
0
      ps->sockets[i] = sock;
598
0
      ps->actions[i] = (unsigned char)add_flags;
599
0
      ps->n = i + 1;
600
0
    }
601
0
  }
602
0
  return CURLE_OK;
603
0
}
604
605
CURLcode Curl_pollset_set(struct Curl_easy *data,
606
                          struct easy_pollset *ps, curl_socket_t sock,
607
                          bool do_in, bool do_out)
608
0
{
609
0
  return Curl_pollset_change(data, ps, sock,
610
0
                             (do_in ? CURL_POLL_IN : 0) |
611
0
                             (do_out ? CURL_POLL_OUT : 0),
612
0
                             (!do_in ? CURL_POLL_IN : 0) |
613
0
                             (!do_out ? CURL_POLL_OUT : 0));
614
0
}
615
616
void Curl_pollset_remove(struct easy_pollset *ps, curl_socket_t sock)
617
0
{
618
0
  unsigned int i;
619
0
  for(i = 0; i < ps->n; ++i) {
620
0
    if(ps->sockets[i] == sock) {
621
0
      if((i + 1) < ps->n) {
622
0
        memmove(&ps->sockets[i], &ps->sockets[i + 1],
623
0
                (ps->n - (i + 1)) * sizeof(ps->sockets[0]));
624
0
        memmove(&ps->actions[i], &ps->actions[i + 1],
625
0
                (ps->n - (i + 1)) * sizeof(ps->actions[0]));
626
0
      }
627
0
      --ps->n;
628
0
      return;
629
0
    }
630
0
  }
631
0
}
632
633
/*
634
 * Return values:
635
 *   -1 = error
636
 *    0 = timeout
637
 *    N = number of structures with non zero revent fields
638
 */
639
int Curl_pollset_poll(struct Curl_easy *data,
640
                      struct easy_pollset *ps,
641
                      timediff_t timeout_ms)
642
0
{
643
0
  struct pollfd *pfds;
644
0
  unsigned int i, npfds;
645
0
  int rc;
646
647
0
  (void)data;
648
0
  DEBUGASSERT(data);
649
0
  DEBUGASSERT(data->conn);
650
651
0
  if(!ps->n)
652
0
    return curlx_wait_ms(timeout_ms);
653
654
0
  pfds = curlx_calloc(ps->n, sizeof(*pfds));
655
0
  if(!pfds)
656
0
    return -1;
657
658
0
  npfds = 0;
659
0
  for(i = 0; i < ps->n; ++i) {
660
0
    short events = 0;
661
0
    if(ps->actions[i] & CURL_POLL_IN) {
662
0
      events |= POLLIN;
663
0
    }
664
0
    if(ps->actions[i] & CURL_POLL_OUT) {
665
0
      events |= POLLOUT;
666
0
    }
667
0
    if(events) {
668
0
      pfds[npfds].fd = ps->sockets[i];
669
0
      pfds[npfds].events = events;
670
0
      ++npfds;
671
0
    }
672
0
  }
673
674
0
  rc = Curl_poll(pfds, npfds, timeout_ms);
675
0
  curlx_free(pfds);
676
0
  return rc;
677
0
}
678
679
void Curl_pollset_check(struct Curl_easy *data,
680
                        struct easy_pollset *ps, curl_socket_t sock,
681
                        bool *pwant_read, bool *pwant_write)
682
0
{
683
0
  unsigned int i;
684
685
0
  (void)data;
686
0
  DEBUGASSERT(VALID_SOCK(sock));
687
0
  for(i = 0; i < ps->n; ++i) {
688
0
    if(ps->sockets[i] == sock) {
689
0
      *pwant_read = !!(ps->actions[i] & CURL_POLL_IN);
690
0
      *pwant_write = !!(ps->actions[i] & CURL_POLL_OUT);
691
0
      return;
692
0
    }
693
0
  }
694
0
  *pwant_read = *pwant_write = FALSE;
695
0
}
696
697
bool Curl_pollset_want_recv(struct Curl_easy *data,
698
                            struct easy_pollset *ps,
699
                            curl_socket_t sock)
700
0
{
701
0
  unsigned int i;
702
0
  (void)data;
703
0
  for(i = 0; i < ps->n; ++i) {
704
0
    if((ps->sockets[i] == sock) && (ps->actions[i] & CURL_POLL_IN))
705
0
      return TRUE;
706
0
  }
707
0
  return FALSE;
708
0
}
709
710
bool Curl_pollset_want_send(struct Curl_easy *data,
711
                            struct easy_pollset *ps,
712
                            curl_socket_t sock)
713
0
{
714
0
  unsigned int i;
715
0
  (void)data;
716
0
  for(i = 0; i < ps->n; ++i) {
717
0
    if((ps->sockets[i] == sock) && (ps->actions[i] & CURL_POLL_OUT))
718
0
      return TRUE;
719
0
  }
720
0
  return FALSE;
721
0
}