Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/cshutdn.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Linus Nielsen Feltzing, <linus@haxx.se>
9
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
10
 *
11
 * This software is licensed as described in the file COPYING, which
12
 * you should have received as part of this distribution. The terms
13
 * are also available at https://curl.se/docs/copyright.html.
14
 *
15
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16
 * copies of the Software, and permit persons to whom the Software is
17
 * furnished to do so, under the terms of the COPYING file.
18
 *
19
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20
 * KIND, either express or implied.
21
 *
22
 * SPDX-License-Identifier: curl
23
 *
24
 ***************************************************************************/
25
#include "curl_setup.h"
26
27
#include "urldata.h"
28
#include "url.h"
29
#include "cfilters.h"
30
#include "progress.h"
31
#include "multiif.h"
32
#include "multi_ev.h"
33
#include "curl_trc.h"
34
#include "cshutdn.h"
35
#include "sigpipe.h"
36
#include "connect.h"
37
#include "select.h"
38
#include "curlx/strparse.h"
39
40
41
9.57k
#define DEFAULT_SHUTDOWN_TIMEOUT_MS   (2 * 1000)
42
43
void Curl_cshutdn_start_timer(struct Curl_easy *data,
44
                              struct Curl_cfchain *chain,
45
                              int timeout_ms)
46
9.57k
{
47
9.57k
  const struct curltime *pnow = Curl_pgrs_now(data);
48
49
9.57k
  chain->shutdown_start_ms = curlx_ptimediff_ms(pnow, &chain->conn->created);
50
9.57k
  chain->shutdown_timeout_ms = (timeout_ms > 0) ?
51
0
    (timediff_t)timeout_ms :
52
9.57k
    ((data->set.shutdowntimeout > 0) ?
53
9.57k
     data->set.shutdowntimeout : DEFAULT_SHUTDOWN_TIMEOUT_MS);
54
9.57k
  CURL_TRC_M(data, "shutdown start on%s connection",
55
9.57k
             !chain->primary ? " secondary" : "");
56
9.57k
}
57
58
timediff_t Curl_cshutdn_timeleft_ms(struct Curl_easy *data,
59
                                    struct Curl_cfchain *chain)
60
9.57k
{
61
9.57k
  timediff_t left_ms;
62
63
9.57k
  if(chain->shutdown_start_ms < 0)
64
0
    return 0; /* not started or no limits */
65
66
9.57k
  left_ms = chain->shutdown_timeout_ms -
67
9.57k
            (curlx_ptimediff_ms(Curl_pgrs_now(data), &chain->conn->created) -
68
9.57k
             chain->shutdown_start_ms);
69
9.57k
  return left_ms ? left_ms : -1;
70
9.57k
}
71
72
static timediff_t cshutdn_conn_timeleft(struct Curl_easy *data,
73
                                        struct connectdata *conn)
74
0
{
75
0
  if(conn->chain.shutdown_start_ms >= 0 &&
76
0
     conn->chain.shutdown_timeout_ms)
77
0
    return Curl_cshutdn_timeleft_ms(data, &conn->chain);
78
0
  return 0;
79
0
}
80
81
void Curl_cshutdn_clear_timer(struct Curl_cfchain *chain)
82
0
{
83
0
  chain->shutdown_start_ms = -1;
84
0
}
85
86
static void cshutdn_run_conn_handler(struct Curl_easy *data,
87
                                     struct connectdata *conn)
88
30.0k
{
89
30.0k
  if(!conn->bits.shutdown_handler) {
90
91
20.4k
    if(conn->scheme && conn->scheme->run->disconnect) {
92
      /* Some disconnect handlers do a blocking wait on server responses.
93
       * FTP/IMAP/SMTP and SFTP are among them. When using the internal
94
       * handle, set an overall short timeout so we do not hang for the
95
       * default 120 seconds. */
96
0
      if(data->state.internal) {
97
0
        data->set.timeout = DEFAULT_SHUTDOWN_TIMEOUT_MS;
98
0
        Curl_pgrsTime(data, TIMER_STARTOP);
99
0
      }
100
101
      /* This is set if protocol-specific cleanups should be made */
102
0
      DEBUGF(infof(data, "connection #%" FMT_OFF_T
103
0
                   ", shutdown protocol handler (aborted=%d)",
104
0
                   conn->connection_id, conn->bits.aborted));
105
      /* There are protocol handlers that block on retrieving
106
       * server responses here (FTP). Set a short timeout. */
107
0
      conn->scheme->run->disconnect(data, conn, (bool)conn->bits.aborted);
108
0
    }
109
110
20.4k
    conn->bits.shutdown_handler = TRUE;
111
20.4k
  }
112
30.0k
}
113
114
static CURLcode cshutdn_cfchain_try_once(struct Curl_easy *data,
115
                                         struct Curl_cfchain *chain,
116
                                         bool *done)
117
9.57k
{
118
9.57k
  struct Curl_cfilter *cf = chain->cf;
119
9.57k
  CURLcode result = CURLE_OK;
120
9.57k
  timediff_t timeout_ms;
121
122
  /* Get the first connected filter that is not shut down already. */
123
9.57k
  while(cf && (!cf->connected || cf->shutdown))
124
0
    cf = cf->next;
125
126
9.57k
  if(!cf) {
127
0
    *done = TRUE;
128
0
    return CURLE_OK;
129
0
  }
130
131
9.57k
  *done = FALSE;
132
9.57k
  if(!CURL_CFCHAIN_IN_SHUTDOWN(chain)) {
133
0
    Curl_cshutdn_start_timer(data, chain, 0);
134
0
  }
135
9.57k
  else {
136
9.57k
    timeout_ms = Curl_cshutdn_timeleft_ms(data, chain);
137
9.57k
    if(timeout_ms < 0) {
138
      /* info message, since this might be regarded as acceptable */
139
0
      infof(data, "shutdown timeout");
140
0
      return CURLE_OPERATION_TIMEDOUT;
141
0
    }
142
9.57k
  }
143
144
19.5k
  while(cf) {
145
9.97k
    if(!cf->shutdown) {
146
9.97k
      bool cfdone = FALSE;
147
9.97k
      result = cf->cft->do_shutdown(cf, data, &cfdone);
148
9.97k
      if(result) {
149
5
        CURL_TRC_CF(data, cf, "shut down failed with %d", (int)result);
150
5
        return result;
151
5
      }
152
9.96k
      else if(!cfdone) {
153
0
        CURL_TRC_CF(data, cf, "shut down not done yet");
154
0
        return CURLE_OK;
155
0
      }
156
9.96k
      CURL_TRC_CF(data, cf, "shut down successfully");
157
9.96k
      cf->shutdown = TRUE;
158
9.96k
    }
159
9.96k
    cf = cf->next;
160
9.96k
  }
161
9.56k
  *done = (!result);
162
9.56k
  return result;
163
9.57k
}
164
165
CURLcode Curl_cshutdn_cfchain_try_once(struct Curl_easy *data,
166
                                       struct Curl_cfchain *chain,
167
                                       bool *done)
168
0
{
169
0
  CURLcode result = cshutdn_cfchain_try_once(data, chain, done);
170
  /* Set a timer if shutdown is ongoing */
171
0
  if(!result && !*done) {
172
0
    timediff_t ms = Curl_cshutdn_timeleft_ms(data, chain);
173
0
    if(ms)
174
0
      Curl_expire_latest(data, EXPIRE_SHUTDOWN, ms, Curl_pgrs_now(data));
175
0
  }
176
0
  return result;
177
0
}
178
179
static void cshutdn_run_once(struct Curl_easy *data,
180
                             struct connectdata *conn,
181
                             bool *done)
182
9.57k
{
183
  /* We expect to be attached when called */
184
9.57k
  DEBUGASSERT(data->conn == conn);
185
186
9.57k
  *done = FALSE;
187
9.57k
  if(!CURL_CFCHAIN_IN_SHUTDOWN(&conn->chain))
188
9.57k
    Curl_cshutdn_start_timer(data, &conn->chain, 0);
189
190
9.57k
  cshutdn_run_conn_handler(data, conn);
191
192
9.57k
  if(conn->bits.shutdown_filters) {
193
0
    *done = TRUE;
194
0
    return;
195
0
  }
196
197
9.57k
  if(!conn->bits.connect_only && Curl_conn_is_connected(conn)) {
198
9.57k
    if(cshutdn_cfchain_try_once(data, &conn->chain, done))
199
5
      *done = TRUE;  /* we are done on errors encountered */
200
9.57k
  }
201
2
  else
202
2
    *done = TRUE; /* connection is not clean, just close it */
203
204
9.57k
  if(*done)
205
9.57k
    conn->bits.shutdown_filters = TRUE;
206
9.57k
}
207
208
void Curl_cshutdn_try_once(struct Curl_easy *admin,
209
                           struct connectdata *conn,
210
                           bool *done)
211
9.57k
{
212
9.57k
  DEBUGASSERT(!admin->conn);
213
9.57k
  DEBUGASSERT(!admin->mid);
214
9.57k
  Curl_attach_connection(admin, conn, FALSE);
215
9.57k
  cshutdn_run_once(admin, conn, done);
216
9.57k
  CURL_TRC_M(admin, "[SHUTDOWN] shutdown, done=%d", *done);
217
9.57k
  Curl_detach_connection(admin);
218
9.57k
}
219
220
void Curl_cshutdn_terminate(struct Curl_easy *admin,
221
                            struct connectdata *conn,
222
                            bool do_shutdown)
223
20.4k
{
224
20.4k
  bool done;
225
226
  /* there must be a connection to close */
227
20.4k
  DEBUGASSERT(conn);
228
  /* it must be removed from the connection pool */
229
20.4k
  DEBUGASSERT(conn->cpid == UINT32_MAX);
230
  /* the transfer must be detached from the connection */
231
20.4k
  DEBUGASSERT(admin && !admin->conn);
232
20.4k
  DEBUGASSERT(!admin->mid);
233
234
20.4k
  Curl_attach_connection(admin, conn, FALSE);
235
236
20.4k
  cshutdn_run_conn_handler(admin, conn);
237
20.4k
  if(do_shutdown) {
238
    /* Make a last attempt to shutdown handlers and filters, if
239
     * not done so already. */
240
0
    cshutdn_run_once(admin, conn, &done);
241
0
  }
242
20.4k
  CURL_TRC_M(admin, "[SHUTDOWN] %sclosing connection #%" FMT_OFF_T,
243
20.4k
             conn->bits.shutdown_filters ? "" : "force ",
244
20.4k
             conn->connection_id);
245
20.4k
  Curl_cfchain_reset(&conn->chain, admin);
246
20.4k
  Curl_detach_connection(admin);
247
248
20.4k
  if(admin->multi)
249
20.4k
    Curl_multi_ev_conn_done(admin->multi, admin, conn);
250
20.4k
  Curl_conn_free(admin, conn);
251
252
20.4k
  if(admin->multi) {
253
20.4k
    CURL_TRC_M(admin, "[SHUTDOWN] trigger multi connchanged");
254
20.4k
    Curl_multi_connchanged(admin->multi);
255
20.4k
  }
256
20.4k
}
257
258
static size_t cshutdn_destroy_oldest(struct cshutdn *cshutdn,
259
                                     struct Curl_easy *admin,
260
                                     const char *destination,
261
                                     size_t count)
262
0
{
263
0
  struct Curl_sigpipe_ctx sigpipe_ctx;
264
0
  size_t removed = 0;
265
0
  uint32_t i = 0;
266
267
0
  sigpipe_init(&sigpipe_ctx);
268
0
  while((removed < count) && (i < CURL_PTRARRAY_COUNT(&cshutdn->conns))) {
269
0
    struct connectdata *conn = CURL_PTRARRAY_GET(&cshutdn->conns, i);
270
0
    if(!destination || !strcmp(destination, conn->destination)) {
271
0
      Curl_ptrarray_remove(&cshutdn->conns, i);
272
0
      sigpipe_apply(admin, &sigpipe_ctx);
273
0
      Curl_cshutdn_terminate(admin, conn, FALSE);
274
0
      ++removed;
275
0
    }
276
0
    else
277
0
      ++i;
278
0
  }
279
0
  sigpipe_restore(&sigpipe_ctx);
280
0
  return removed;
281
0
}
282
283
size_t Curl_cshutdn_close_oldest(struct cshutdn *cshutdn,
284
                               struct Curl_easy *admin,
285
                               const char *destination,
286
                               size_t count)
287
0
{
288
0
  if(cshutdn) {
289
0
    return cshutdn_destroy_oldest(cshutdn, admin, destination, count);
290
0
  }
291
0
  return 0;
292
0
}
293
294
0
#define NUM_POLLS_ON_STACK 10
295
296
static CURLcode cshutdn_wait(struct cshutdn *cshutdn,
297
                             struct Curl_easy *admin,
298
                             int timeout_ms)
299
0
{
300
0
  struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
301
0
  struct curl_pollfds cpfds;
302
0
  CURLcode result;
303
304
0
  Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
305
306
0
  result = Curl_cshutdn_add_pollfds(cshutdn, admin, &cpfds);
307
0
  if(result)
308
0
    goto out;
309
310
0
  Curl_poll(cpfds.pfds, cpfds.n, CURLMIN(timeout_ms, 1000));
311
312
0
out:
313
0
  Curl_pollfds_cleanup(&cpfds);
314
0
  return result;
315
0
}
316
317
static void cshutdn_perform(struct cshutdn *cshutdn,
318
                            struct Curl_easy *admin,
319
                            struct Curl_sigpipe_ctx *sigpipe_ctx)
320
88.3k
{
321
88.3k
  struct connectdata *conn;
322
88.3k
  timediff_t next_expire_ms = 0, ms;
323
88.3k
  uint32_t i = 0;
324
88.3k
  bool done;
325
326
88.3k
  if(!CURL_PTRARRAY_COUNT(&cshutdn->conns))
327
88.3k
    return;
328
329
0
  CURL_TRC_M(admin, "[SHUTDOWN] perform on %u connections",
330
0
             CURL_PTRARRAY_COUNT(&cshutdn->conns));
331
0
  sigpipe_apply(admin, sigpipe_ctx);
332
0
  while(i < CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
333
0
    conn = CURL_PTRARRAY_GET(&cshutdn->conns, i);
334
0
    Curl_cshutdn_try_once(admin, conn, &done);
335
0
    if(done) {
336
0
      Curl_ptrarray_remove(&cshutdn->conns, i);
337
0
      Curl_cshutdn_terminate(admin, conn, FALSE);
338
0
    }
339
0
    else {
340
0
      ++i;
341
      /* idata has one timer list, but maybe more than one connection.
342
       * Set EXPIRE_SHUTDOWN to the smallest time left for all. */
343
0
      ms = cshutdn_conn_timeleft(admin, conn);
344
0
      if(ms && (!next_expire_ms || (ms < next_expire_ms)))
345
0
        next_expire_ms = ms;
346
0
    }
347
0
  }
348
349
0
  if(next_expire_ms)
350
0
    Curl_expire(admin, next_expire_ms, EXPIRE_SHUTDOWN);
351
0
}
352
353
static void cshutdn_terminate_all(struct cshutdn *cshutdn,
354
                                  struct Curl_easy *admin,
355
                                  int timeout_ms)
356
0
{
357
0
  struct curltime started = *Curl_pgrs_now(admin);
358
0
  struct Curl_sigpipe_ctx sigpipe_ctx;
359
360
0
  CURL_TRC_M(admin, "[SHUTDOWN] shutdown all");
361
0
  sigpipe_init(&sigpipe_ctx);
362
363
0
  while(CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
364
0
    timediff_t spent_ms;
365
0
    int remain_ms;
366
367
0
    cshutdn_perform(cshutdn, admin, &sigpipe_ctx);
368
369
0
    if(!CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
370
0
      CURL_TRC_M(admin, "[SHUTDOWN] shutdown finished cleanly");
371
0
      break;
372
0
    }
373
374
    /* wait for activity, timeout or "nothing" */
375
0
    spent_ms = curlx_ptimediff_ms(Curl_pgrs_now(admin), &started);
376
0
    if(spent_ms >= (timediff_t)timeout_ms) {
377
0
      CURL_TRC_M(admin, "[SHUTDOWN] shutdown finished, %s",
378
0
                 (timeout_ms > 0) ? "timeout" : "best effort done");
379
0
      break;
380
0
    }
381
382
0
    remain_ms = timeout_ms - (int)spent_ms;
383
0
    if(cshutdn_wait(cshutdn, admin, remain_ms)) {
384
0
      CURL_TRC_M(admin, "[SHUTDOWN] shutdown finished, aborted");
385
0
      break;
386
0
    }
387
0
  }
388
389
  /* Terminate any remaining. */
390
0
  while(CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
391
0
    struct connectdata *conn = Curl_ptrarray_remove(&cshutdn->conns, 0);
392
0
    Curl_cshutdn_terminate(admin, conn, FALSE);
393
0
  }
394
0
  DEBUGASSERT(!CURL_PTRARRAY_COUNT(&cshutdn->conns));
395
396
0
  sigpipe_restore(&sigpipe_ctx);
397
0
}
398
399
void Curl_cshutdn_init(struct cshutdn *cshutdn)
400
21.0k
{
401
21.0k
  Curl_ptrarray_init(&cshutdn->conns);
402
21.0k
}
403
404
void Curl_cshutdn_destroy(struct cshutdn *cshutdn,
405
                          struct Curl_easy *admin)
406
21.0k
{
407
21.0k
  if(admin) {
408
21.0k
    int timeout_ms = 0;
409
    /* for testing, run graceful shutdown */
410
21.0k
#ifdef DEBUGBUILD
411
21.0k
    {
412
21.0k
      const char *p = getenv("CURL_GRACEFUL_SHUTDOWN");
413
21.0k
      if(p) {
414
0
        curl_off_t l;
415
0
        if(!curlx_str_number(&p, &l, INT_MAX))
416
0
          timeout_ms = (int)l;
417
0
      }
418
21.0k
    }
419
21.0k
#endif
420
21.0k
    if(CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
421
0
      CURL_TRC_M(admin, "[SHUTDOWN] destroy, %u connections, timeout=%dms",
422
0
                 CURL_PTRARRAY_COUNT(&cshutdn->conns), timeout_ms);
423
0
      cshutdn_terminate_all(cshutdn, admin, timeout_ms);
424
0
    }
425
21.0k
  }
426
21.0k
}
427
428
size_t Curl_cshutdn_count(struct cshutdn *cshutdn)
429
0
{
430
0
  if(cshutdn) {
431
0
    return CURL_PTRARRAY_COUNT(&cshutdn->conns);
432
0
  }
433
0
  return 0;
434
0
}
435
436
size_t Curl_cshutdn_dest_count(struct cshutdn *cshutdn,
437
                               const char *destination)
438
0
{
439
0
  if(cshutdn) {
440
0
    size_t n = 0;
441
0
    uint32_t i = 0;
442
0
    for(i = 0; i < CURL_PTRARRAY_COUNT(&cshutdn->conns); ++i) {
443
0
      struct connectdata *conn = CURL_PTRARRAY_GET(&cshutdn->conns, i);
444
0
      if(!strcmp(destination, conn->destination))
445
0
        ++n;
446
0
    }
447
0
    return n;
448
0
  }
449
0
  return 0;
450
0
}
451
452
static CURLMcode cshutdn_update_ev(struct Curl_multi *multi,
453
                                   struct connectdata *conn)
454
0
{
455
0
  CURLMcode mresult;
456
0
  Curl_attach_connection(multi->admin, conn, FALSE);
457
0
  mresult = Curl_multi_ev_assess_conn(multi, multi->admin, conn);
458
0
  Curl_detach_connection(multi->admin);
459
0
  return mresult;
460
0
}
461
462
void Curl_cshutdn_add(struct cshutdn *cshutdn,
463
                      struct Curl_multi *multi,
464
                      struct connectdata *conn,
465
                      size_t max_shutdowns)
466
0
{
467
0
  struct Curl_easy *admin = multi->admin;
468
469
  /* Add the connection to our shutdown list for non-blocking shutdown
470
   * during multi processing. */
471
0
  if(max_shutdowns <= CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
472
0
    CURL_TRC_M(admin, "[SHUTDOWN] discarding oldest shutdown connection "
473
0
               "due to shutdown limit of %zu", max_shutdowns);
474
0
    cshutdn_destroy_oldest(cshutdn, admin, NULL, 1);
475
0
  }
476
477
0
  if(multi->socket_cb && cshutdn_update_ev(multi, conn)) {
478
0
    CURL_TRC_M(admin, "[SHUTDOWN] update events failed, discarding #%"
479
0
               FMT_OFF_T, conn->connection_id);
480
0
    Curl_cshutdn_terminate(admin, conn, FALSE);
481
0
    return;
482
0
  }
483
484
0
  if(Curl_ptrarray_add(&cshutdn->conns, conn)) {
485
0
    CURL_TRC_M(admin, "[SHUTDOWN] adding failed, discarding #%"
486
0
               FMT_OFF_T, conn->connection_id);
487
0
    Curl_cshutdn_terminate(admin, conn, FALSE);
488
0
    return;
489
0
  }
490
0
  CURL_TRC_M(admin, "[SHUTDOWN] added #%" FMT_OFF_T
491
0
             " to shutdowns, now %u conns in shutdown",
492
0
             conn->connection_id, CURL_PTRARRAY_COUNT(&cshutdn->conns));
493
0
  Curl_multi_mark_dirty(admin);
494
0
}
495
496
void Curl_cshutdn_perform(struct cshutdn *cshutdn,
497
                          struct Curl_easy *admin,
498
                          struct Curl_sigpipe_ctx *sigpipe_ctx)
499
88.3k
{
500
88.3k
  cshutdn_perform(cshutdn, admin, sigpipe_ctx);
501
88.3k
}
502
503
/* return fd_set info about the shutdown connections */
504
void Curl_cshutdn_setfds(struct cshutdn *cshutdn,
505
                         struct Curl_easy *admin,
506
                         fd_set *read_fd_set, fd_set *write_fd_set,
507
                         int *maxfd)
508
0
{
509
0
  if(CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
510
0
    struct easy_pollset ps;
511
0
    uint32_t i;
512
513
0
    Curl_pollset_init(&ps);
514
0
    for(i = 0; i < CURL_PTRARRAY_COUNT(&cshutdn->conns); ++i) {
515
0
      unsigned int j;
516
0
      struct connectdata *conn = CURL_PTRARRAY_GET(&cshutdn->conns, i);
517
0
      CURLcode result;
518
519
0
      Curl_pollset_reset(&ps);
520
0
      Curl_attach_connection(admin, conn, FALSE);
521
0
      result = Curl_conn_adjust_pollset(admin, conn, &ps);
522
0
      Curl_detach_connection(admin);
523
524
0
      if(result)
525
0
        continue;
526
527
0
      for(j = 0; j < ps.n; j++) {
528
0
        curl_socket_t sock = ps.sockets[j];
529
0
        if(!FDSET_SOCK(sock))
530
0
          continue;
531
0
        if(ps.actions[j] & CURL_POLL_IN)
532
0
          FD_SET(sock, read_fd_set);
533
0
        if(ps.actions[j] & CURL_POLL_OUT)
534
0
          FD_SET(sock, write_fd_set);
535
0
        if((ps.actions[j] & (CURL_POLL_OUT | CURL_POLL_IN)) &&
536
0
           ((int)sock > *maxfd))
537
0
          *maxfd = (int)sock;
538
0
      }
539
0
    }
540
0
    Curl_pollset_cleanup(&ps);
541
0
  }
542
0
}
543
544
/* return information about the shutdown connections */
545
unsigned int Curl_cshutdn_add_waitfds(struct cshutdn *cshutdn,
546
                                      struct Curl_easy *admin,
547
                                      struct Curl_waitfds *cwfds)
548
0
{
549
0
  unsigned int need = 0;
550
551
0
  if(CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
552
0
    struct easy_pollset ps;
553
0
    struct connectdata *conn;
554
0
    uint32_t i;
555
0
    CURLcode result;
556
557
0
    Curl_pollset_init(&ps);
558
0
    for(i = 0; i < CURL_PTRARRAY_COUNT(&cshutdn->conns); ++i) {
559
0
      conn = CURL_PTRARRAY_GET(&cshutdn->conns, i);
560
0
      Curl_pollset_reset(&ps);
561
0
      Curl_attach_connection(admin, conn, FALSE);
562
0
      result = Curl_conn_adjust_pollset(admin, conn, &ps);
563
0
      Curl_detach_connection(admin);
564
565
0
      if(!result)
566
0
        need += Curl_waitfds_add_ps(cwfds, &ps);
567
0
    }
568
0
    Curl_pollset_cleanup(&ps);
569
0
  }
570
0
  return need;
571
0
}
572
573
CURLcode Curl_cshutdn_add_pollfds(struct cshutdn *cshutdn,
574
                                  struct Curl_easy *admin,
575
                                  struct curl_pollfds *cpfds)
576
0
{
577
0
  CURLcode result = CURLE_OK;
578
579
0
  if(CURL_PTRARRAY_COUNT(&cshutdn->conns)) {
580
0
    struct easy_pollset ps;
581
0
    struct connectdata *conn;
582
0
    uint32_t i;
583
584
0
    Curl_pollset_init(&ps);
585
0
    for(i = 0; i < CURL_PTRARRAY_COUNT(&cshutdn->conns); ++i) {
586
0
      conn = CURL_PTRARRAY_GET(&cshutdn->conns, i);
587
0
      Curl_pollset_reset(&ps);
588
0
      Curl_attach_connection(admin, conn, FALSE);
589
0
      result = Curl_conn_adjust_pollset(admin, conn, &ps);
590
0
      Curl_detach_connection(admin);
591
592
0
      if(!result)
593
0
        result = Curl_pollfds_add_ps(cpfds, &ps);
594
0
      if(result) {
595
0
        Curl_pollset_cleanup(&ps);
596
0
        Curl_pollfds_cleanup(cpfds);
597
0
        goto out;
598
0
      }
599
0
    }
600
0
    Curl_pollset_cleanup(&ps);
601
0
  }
602
0
out:
603
0
  return result;
604
0
}