Coverage Report

Created: 2025-08-26 07:08

/src/PROJ/curl/lib/multi.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "curl_setup.h"
26
27
#include <curl/curl.h>
28
29
#include "urldata.h"
30
#include "transfer.h"
31
#include "url.h"
32
#include "cfilters.h"
33
#include "connect.h"
34
#include "progress.h"
35
#include "easyif.h"
36
#include "share.h"
37
#include "psl.h"
38
#include "multiif.h"
39
#include "multi_ev.h"
40
#include "sendf.h"
41
#include "curlx/timeval.h"
42
#include "http.h"
43
#include "select.h"
44
#include "curlx/warnless.h"
45
#include "curlx/wait.h"
46
#include "speedcheck.h"
47
#include "conncache.h"
48
#include "multihandle.h"
49
#include "sigpipe.h"
50
#include "vtls/vtls.h"
51
#include "vtls/vtls_scache.h"
52
#include "http_proxy.h"
53
#include "http2.h"
54
#include "socketpair.h"
55
#include "socks.h"
56
#include "urlapi-int.h"
57
/* The last 3 #include files should be in this order */
58
#include "curl_printf.h"
59
#include "curl_memory.h"
60
#include "memdebug.h"
61
62
/* initial multi->xfers table size for a full multi */
63
0
#define CURL_XFER_TABLE_SIZE    512
64
65
/*
66
  CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97
67
  to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every
68
  curl handle takes 6K memory, therefore this 3K are not significant.
69
*/
70
#ifndef CURL_SOCKET_HASH_TABLE_SIZE
71
0
#define CURL_SOCKET_HASH_TABLE_SIZE 911
72
#endif
73
74
#ifndef CURL_CONNECTION_HASH_SIZE
75
0
#define CURL_CONNECTION_HASH_SIZE 97
76
#endif
77
78
#ifndef CURL_DNS_HASH_SIZE
79
0
#define CURL_DNS_HASH_SIZE 71
80
#endif
81
82
#ifndef CURL_TLS_SESSION_SIZE
83
0
#define CURL_TLS_SESSION_SIZE 25
84
#endif
85
86
0
#define CURL_MULTI_HANDLE 0x000bab1e
87
88
#ifdef DEBUGBUILD
89
/* On a debug build, we want to fail hard on multi handles that
90
 * are not NULL, but no longer have the MAGIC touch. This gives
91
 * us early warning on things only discovered by valgrind otherwise. */
92
#define GOOD_MULTI_HANDLE(x) \
93
  (((x) && (x)->magic == CURL_MULTI_HANDLE)? TRUE:      \
94
  (DEBUGASSERT(!(x)), FALSE))
95
#else
96
#define GOOD_MULTI_HANDLE(x) \
97
0
  ((x) && (x)->magic == CURL_MULTI_HANDLE)
98
#endif
99
100
static void move_pending_to_connect(struct Curl_multi *multi,
101
                                    struct Curl_easy *data);
102
static CURLMcode add_next_timeout(struct curltime now,
103
                                  struct Curl_multi *multi,
104
                                  struct Curl_easy *d);
105
static CURLMcode multi_timeout(struct Curl_multi *multi,
106
                               struct curltime *expire_time,
107
                               long *timeout_ms);
108
static void process_pending_handles(struct Curl_multi *multi);
109
static void multi_xfer_bufs_free(struct Curl_multi *multi);
110
#ifdef DEBUGBUILD
111
static void multi_xfer_tbl_dump(struct Curl_multi *multi);
112
#endif
113
114
/* function pointer called once when switching TO a state */
115
typedef void (*init_multistate_func)(struct Curl_easy *data);
116
117
/* called in DID state, before PERFORMING state */
118
static void before_perform(struct Curl_easy *data)
119
0
{
120
0
  data->req.chunk = FALSE;
121
0
  Curl_pgrsTime(data, TIMER_PRETRANSFER);
122
0
}
123
124
static void init_completed(struct Curl_easy *data)
125
0
{
126
  /* this is a completed transfer */
127
128
  /* Important: reset the conn pointer so that we do not point to memory
129
     that could be freed anytime */
130
0
  Curl_detach_connection(data);
131
0
  Curl_expire_clear(data); /* stop all timers */
132
0
}
133
134
/* always use this function to change state, to make debugging easier */
135
static void mstate(struct Curl_easy *data, CURLMstate state
136
#ifdef DEBUGBUILD
137
                   , int lineno
138
#endif
139
)
140
0
{
141
0
  CURLMstate oldstate = data->mstate;
142
0
  static const init_multistate_func finit[MSTATE_LAST] = {
143
0
    NULL,              /* INIT */
144
0
    NULL,              /* PENDING */
145
0
    NULL,              /* SETUP */
146
0
    Curl_init_CONNECT, /* CONNECT */
147
0
    NULL,              /* RESOLVING */
148
0
    NULL,              /* CONNECTING */
149
0
    NULL,              /* TUNNELING */
150
0
    NULL,              /* PROTOCONNECT */
151
0
    NULL,              /* PROTOCONNECTING */
152
0
    NULL,              /* DO */
153
0
    NULL,              /* DOING */
154
0
    NULL,              /* DOING_MORE */
155
0
    before_perform,    /* DID */
156
0
    NULL,              /* PERFORMING */
157
0
    NULL,              /* RATELIMITING */
158
0
    NULL,              /* DONE */
159
0
    init_completed,    /* COMPLETED */
160
    NULL               /* MSGSENT */
161
0
  };
162
163
0
  if(oldstate == state)
164
    /* do not bother when the new state is the same as the old state */
165
0
    return;
166
167
#ifdef DEBUGBUILD
168
  CURL_TRC_M(data, "-> [%s] (line %d)", CURL_MSTATE_NAME(state), lineno);
169
#else
170
0
  CURL_TRC_M(data, "-> [%s]", CURL_MSTATE_NAME(state));
171
0
#endif
172
173
0
  data->mstate = state;
174
175
0
  if(state == MSTATE_COMPLETED) {
176
    /* changing to COMPLETED means it is in process and needs to go */
177
0
    DEBUGASSERT(Curl_uint_bset_contains(&data->multi->process, data->mid));
178
0
    Curl_uint_bset_remove(&data->multi->process, data->mid);
179
0
    Curl_uint_bset_remove(&data->multi->pending, data->mid); /* to be sure */
180
181
0
    if(Curl_uint_bset_empty(&data->multi->process)) {
182
      /* free the transfer buffer when we have no more active transfers */
183
0
      multi_xfer_bufs_free(data->multi);
184
0
    }
185
0
  }
186
187
  /* if this state has an init-function, run it */
188
0
  if(finit[state])
189
0
    finit[state](data);
190
0
}
191
192
#ifndef DEBUGBUILD
193
0
#define multistate(x,y) mstate(x,y)
194
#else
195
#define multistate(x,y) mstate(x,y, __LINE__)
196
#endif
197
198
199
/* multi->proto_hash destructor. Should never be called as elements
200
 * MUST be added with their own destructor */
201
static void ph_freeentry(void *p)
202
0
{
203
0
  (void)p;
204
  /* Will always be FALSE. Cannot use a 0 assert here since compilers
205
   * are not in agreement if they then want a NORETURN attribute or
206
   * not. *sigh* */
207
0
  DEBUGASSERT(p == NULL);
208
0
}
209
210
/*
211
 * multi_addmsg()
212
 *
213
 * Called when a transfer is completed. Adds the given msg pointer to
214
 * the list kept in the multi handle.
215
 */
216
static void multi_addmsg(struct Curl_multi *multi, struct Curl_message *msg)
217
0
{
218
0
  Curl_llist_append(&multi->msglist, msg, &msg->list);
219
0
}
220
221
struct Curl_multi *Curl_multi_handle(unsigned int xfer_table_size,
222
                                     size_t ev_hashsize,  /* event hash */
223
                                     size_t chashsize, /* connection hash */
224
                                     size_t dnssize,   /* dns hash */
225
                                     size_t sesssize)  /* TLS session cache */
226
0
{
227
0
  struct Curl_multi *multi = calloc(1, sizeof(struct Curl_multi));
228
229
0
  if(!multi)
230
0
    return NULL;
231
232
0
  multi->magic = CURL_MULTI_HANDLE;
233
234
0
  Curl_dnscache_init(&multi->dnscache, dnssize);
235
0
  Curl_multi_ev_init(multi, ev_hashsize);
236
0
  Curl_uint_tbl_init(&multi->xfers, NULL);
237
0
  Curl_uint_bset_init(&multi->process);
238
0
  Curl_uint_bset_init(&multi->dirty);
239
0
  Curl_uint_bset_init(&multi->pending);
240
0
  Curl_uint_bset_init(&multi->msgsent);
241
0
  Curl_hash_init(&multi->proto_hash, 23,
242
0
                 Curl_hash_str, curlx_str_key_compare, ph_freeentry);
243
0
  Curl_llist_init(&multi->msglist, NULL);
244
245
0
  multi->multiplexing = TRUE;
246
0
  multi->max_concurrent_streams = 100;
247
0
  multi->last_timeout_ms = -1;
248
249
0
  if(Curl_uint_bset_resize(&multi->process, xfer_table_size) ||
250
0
     Curl_uint_bset_resize(&multi->pending, xfer_table_size) ||
251
0
     Curl_uint_bset_resize(&multi->dirty, xfer_table_size) ||
252
0
     Curl_uint_bset_resize(&multi->msgsent, xfer_table_size) ||
253
0
     Curl_uint_tbl_resize(&multi->xfers, xfer_table_size))
254
0
    goto error;
255
256
0
  multi->admin = curl_easy_init();
257
0
  if(!multi->admin)
258
0
    goto error;
259
  /* Initialize admin handle to operate inside this multi */
260
0
  multi->admin->multi = multi;
261
0
  multi->admin->state.internal = TRUE;
262
0
  Curl_llist_init(&multi->admin->state.timeoutlist, NULL);
263
#ifdef DEBUGBUILD
264
  if(getenv("CURL_DEBUG"))
265
    multi->admin->set.verbose = TRUE;
266
#endif
267
0
  Curl_uint_tbl_add(&multi->xfers, multi->admin, &multi->admin->mid);
268
269
0
  if(Curl_cshutdn_init(&multi->cshutdn, multi))
270
0
    goto error;
271
272
0
  Curl_cpool_init(&multi->cpool, multi->admin, NULL, chashsize);
273
274
0
#ifdef USE_SSL
275
0
  if(Curl_ssl_scache_create(sesssize, 2, &multi->ssl_scache))
276
0
    goto error;
277
#else
278
  (void)sesssize;
279
#endif
280
281
#ifdef USE_WINSOCK
282
  multi->wsa_event = WSACreateEvent();
283
  if(multi->wsa_event == WSA_INVALID_EVENT)
284
    goto error;
285
#elif defined(ENABLE_WAKEUP)
286
0
  if(wakeup_create(multi->wakeup_pair, TRUE) < 0) {
287
0
    multi->wakeup_pair[0] = CURL_SOCKET_BAD;
288
0
    multi->wakeup_pair[1] = CURL_SOCKET_BAD;
289
0
  }
290
0
#endif
291
292
0
  return multi;
293
294
0
error:
295
296
0
  Curl_multi_ev_cleanup(multi);
297
0
  Curl_hash_destroy(&multi->proto_hash);
298
0
  Curl_dnscache_destroy(&multi->dnscache);
299
0
  Curl_cpool_destroy(&multi->cpool);
300
0
  Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
301
0
#ifdef USE_SSL
302
0
  Curl_ssl_scache_destroy(multi->ssl_scache);
303
0
#endif
304
0
  if(multi->admin) {
305
0
    multi->admin->multi = NULL;
306
0
    Curl_close(&multi->admin);
307
0
  }
308
309
0
  Curl_uint_bset_destroy(&multi->process);
310
0
  Curl_uint_bset_destroy(&multi->dirty);
311
0
  Curl_uint_bset_destroy(&multi->pending);
312
0
  Curl_uint_bset_destroy(&multi->msgsent);
313
0
  Curl_uint_tbl_destroy(&multi->xfers);
314
315
0
  free(multi);
316
0
  return NULL;
317
0
}
318
319
CURLM *curl_multi_init(void)
320
0
{
321
0
  return Curl_multi_handle(CURL_XFER_TABLE_SIZE,
322
0
                           CURL_SOCKET_HASH_TABLE_SIZE,
323
0
                           CURL_CONNECTION_HASH_SIZE,
324
0
                           CURL_DNS_HASH_SIZE,
325
0
                           CURL_TLS_SESSION_SIZE);
326
0
}
327
328
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
329
static void multi_warn_debug(struct Curl_multi *multi, struct Curl_easy *data)
330
{
331
  if(!multi->warned) {
332
    infof(data, "!!! WARNING !!!");
333
    infof(data, "This is a debug build of libcurl, "
334
          "do not use in production.");
335
    multi->warned = TRUE;
336
  }
337
}
338
#else
339
0
#define multi_warn_debug(x,y) Curl_nop_stmt
340
#endif
341
342
343
static CURLMcode multi_xfers_add(struct Curl_multi *multi,
344
                                 struct Curl_easy *data)
345
0
{
346
0
  unsigned int capacity = Curl_uint_tbl_capacity(&multi->xfers);
347
0
  unsigned int new_size = 0;
348
  /* Prepare to make this into a CURLMOPT_MAX_TRANSFERS, because some
349
   * applications may want to prevent a run-away of their memory use. */
350
  /* UINT_MAX is our "invalid" id, do not let the table grow up to that. */
351
0
  const unsigned int max_capacity = UINT_MAX - 1;
352
353
0
  if(capacity < max_capacity) {
354
    /* We want `multi->xfers` to have "sufficient" free rows, so that we do
355
     * have to reuse the `mid` from a just removed easy right away.
356
     * Since uint_tbl and uint_bset are quite memory efficient,
357
     * regard less than 25% free as insufficient.
358
     * (for low capacities, e.g. multi_easy, 4 or less). */
359
0
    unsigned int used = Curl_uint_tbl_count(&multi->xfers);
360
0
    unsigned int unused = capacity - used;
361
0
    unsigned int min_unused = CURLMAX(capacity >> 2, 4);
362
0
    if(unused <= min_unused) {
363
      /* Make sure the uint arithmetic here works on the corner
364
       * cases where we are close to max_capacity or UINT_MAX */
365
0
      if((min_unused >= max_capacity) ||
366
0
         ((max_capacity - min_unused) <= capacity) ||
367
0
         ((UINT_MAX - min_unused - 63) <= capacity)) {
368
0
        new_size = max_capacity; /* can not be larger than this */
369
0
      }
370
0
      else {
371
         /* make it a 64 multiple, since our bitsets frow by that and
372
          * small (easy_multi) grows to at least 64 on first resize. */
373
0
        new_size = (((used + min_unused) + 63) / 64) * 64;
374
0
      }
375
0
    }
376
0
  }
377
378
0
  if(new_size > capacity) {
379
    /* Grow the bitsets first. Should one fail, we do not need
380
     * to downsize the already resized ones. The sets continue
381
     * to work properly when larger than the table, but not
382
     * the other way around. */
383
0
    CURL_TRC_M(data, "increasing xfer table size to %u", new_size);
384
0
    if(Curl_uint_bset_resize(&multi->process, new_size) ||
385
0
       Curl_uint_bset_resize(&multi->dirty, new_size) ||
386
0
       Curl_uint_bset_resize(&multi->pending, new_size) ||
387
0
       Curl_uint_bset_resize(&multi->msgsent, new_size) ||
388
0
       Curl_uint_tbl_resize(&multi->xfers, new_size))
389
0
      return CURLM_OUT_OF_MEMORY;
390
0
  }
391
392
  /* Insert the easy into the table now */
393
0
  if(!Curl_uint_tbl_add(&multi->xfers, data, &data->mid)) {
394
    /* MUST only happen when table is full */
395
0
    DEBUGASSERT(Curl_uint_tbl_capacity(&multi->xfers) <=
396
0
                Curl_uint_tbl_count(&multi->xfers));
397
0
    return CURLM_OUT_OF_MEMORY;
398
0
  }
399
0
  return CURLM_OK;
400
0
}
401
402
403
CURLMcode curl_multi_add_handle(CURLM *m, CURL *d)
404
0
{
405
0
  CURLMcode rc;
406
0
  struct Curl_multi *multi = m;
407
0
  struct Curl_easy *data = d;
408
  /* First, make some basic checks that the CURLM handle is a good handle */
409
0
  if(!GOOD_MULTI_HANDLE(multi))
410
0
    return CURLM_BAD_HANDLE;
411
412
  /* Verify that we got a somewhat good easy handle too */
413
0
  if(!GOOD_EASY_HANDLE(data))
414
0
    return CURLM_BAD_EASY_HANDLE;
415
416
  /* Prevent users from adding same easy handle more than once and prevent
417
     adding to more than one multi stack */
418
0
  if(data->multi)
419
0
    return CURLM_ADDED_ALREADY;
420
421
0
  if(multi->in_callback)
422
0
    return CURLM_RECURSIVE_API_CALL;
423
424
0
  if(multi->dead) {
425
    /* a "dead" handle cannot get added transfers while any existing easy
426
       handles are still alive - but if there are none alive anymore, it is
427
       fine to start over and unmark the "deadness" of this handle.
428
       This means only the admin handle MUST be present. */
429
0
    if((Curl_uint_tbl_count(&multi->xfers) != 1) ||
430
0
       !Curl_uint_tbl_contains(&multi->xfers, 0))
431
0
      return CURLM_ABORTED_BY_CALLBACK;
432
0
    multi->dead = FALSE;
433
0
    Curl_uint_bset_clear(&multi->process);
434
0
    Curl_uint_bset_clear(&multi->dirty);
435
0
    Curl_uint_bset_clear(&multi->pending);
436
0
    Curl_uint_bset_clear(&multi->msgsent);
437
0
  }
438
439
0
  if(data->multi_easy) {
440
    /* if this easy handle was previously used for curl_easy_perform(), there
441
       is a private multi handle here that we can kill */
442
0
    curl_multi_cleanup(data->multi_easy);
443
0
    data->multi_easy = NULL;
444
0
  }
445
446
  /* Insert the easy into the multi->xfers table, assigning it a `mid`. */
447
0
  if(multi_xfers_add(multi, data))
448
0
    return CURLM_OUT_OF_MEMORY;
449
450
  /* Initialize timeout list for this handle */
451
0
  Curl_llist_init(&data->state.timeoutlist, NULL);
452
453
  /*
454
   * No failure allowed in this function beyond this point. No modification of
455
   * easy nor multi handle allowed before this except for potential multi's
456
   * connection pool growing which will not be undone in this function no
457
   * matter what.
458
   */
459
0
  if(data->set.errorbuffer)
460
0
    data->set.errorbuffer[0] = 0;
461
462
0
  data->state.os_errno = 0;
463
464
  /* make the Curl_easy refer back to this multi handle - before Curl_expire()
465
     is called. */
466
0
  data->multi = multi;
467
468
  /* set the easy handle */
469
0
  multistate(data, MSTATE_INIT);
470
471
#ifdef USE_LIBPSL
472
  /* Do the same for PSL. */
473
  if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL)))
474
    data->psl = &data->share->psl;
475
  else
476
    data->psl = &multi->psl;
477
#endif
478
479
  /* add the easy handle to the process set */
480
0
  Curl_uint_bset_add(&multi->process, data->mid);
481
0
  ++multi->xfers_alive;
482
0
  ++multi->xfers_total_ever;
483
484
0
  Curl_cpool_xfer_init(data);
485
0
  multi_warn_debug(multi, data);
486
487
  /* Make sure the new handle will run */
488
0
  Curl_multi_mark_dirty(data);
489
  /* Necessary in event based processing, where dirty handles trigger
490
   * a timeout callback invocation. */
491
0
  rc = Curl_update_timer(multi);
492
0
  if(rc) {
493
0
    data->multi = NULL; /* not anymore */
494
0
    Curl_uint_tbl_remove(&multi->xfers, data->mid);
495
0
    data->mid = UINT_MAX;
496
0
    return rc;
497
0
  }
498
499
  /* The admin handle only ever has default timeouts set. To improve the
500
     state somewhat we clone the timeouts from each added handle so that the
501
     admin handle always has the same timeouts as the most recently added
502
     easy handle. */
503
0
  multi->admin->set.timeout = data->set.timeout;
504
0
  multi->admin->set.server_response_timeout =
505
0
    data->set.server_response_timeout;
506
0
  multi->admin->set.no_signal = data->set.no_signal;
507
508
0
  CURL_TRC_M(data, "added to multi, mid=%u, running=%u, total=%u",
509
0
             data->mid, Curl_multi_xfers_running(multi),
510
0
             Curl_uint_tbl_count(&multi->xfers));
511
0
  return CURLM_OK;
512
0
}
513
514
#if 0
515
/* Debug-function, used like this:
516
 *
517
 * Curl_hash_print(&multi->sockhash, debug_print_sock_hash);
518
 *
519
 * Enable the hash print function first by editing hash.c
520
 */
521
static void debug_print_sock_hash(void *p)
522
{
523
  struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p;
524
525
  fprintf(stderr, " [readers %u][writers %u]",
526
          sh->readers, sh->writers);
527
}
528
#endif
529
530
struct multi_done_ctx {
531
  BIT(premature);
532
};
533
534
static void multi_done_locked(struct connectdata *conn,
535
                              struct Curl_easy *data,
536
                              void *userdata)
537
0
{
538
0
  struct multi_done_ctx *mdctx = userdata;
539
0
#ifndef CURL_DISABLE_VERBOSE_STRINGS
540
0
  const char *host =
541
0
#ifndef CURL_DISABLE_PROXY
542
0
        conn->bits.socksproxy ?
543
0
        conn->socks_proxy.host.dispname :
544
0
        conn->bits.httpproxy ? conn->http_proxy.host.dispname :
545
0
#endif
546
0
        conn->bits.conn_to_host ? conn->conn_to_host.dispname :
547
0
        conn->host.dispname;
548
0
  int port =
549
0
#ifndef CURL_DISABLE_PROXY
550
0
        conn->bits.httpproxy ? conn->http_proxy.port :
551
0
#endif
552
0
        conn->bits.conn_to_port ? conn->conn_to_port :
553
0
        conn->remote_port;
554
0
#endif
555
556
0
  Curl_detach_connection(data);
557
558
0
  CURL_TRC_M(data, "multi_done_locked, in use=%u",
559
0
             Curl_uint_spbset_count(&conn->xfers_attached));
560
0
  if(CONN_INUSE(conn)) {
561
    /* Stop if still used. */
562
0
    CURL_TRC_M(data, "Connection still in use %u, no more multi_done now!",
563
0
               Curl_uint_spbset_count(&conn->xfers_attached));
564
0
    return;
565
0
  }
566
567
0
  data->state.done = TRUE; /* called just now! */
568
0
  data->state.recent_conn_id = conn->connection_id;
569
570
0
  Curl_resolv_unlink(data, &data->state.dns[0]); /* done with this */
571
0
  Curl_resolv_unlink(data, &data->state.dns[1]);
572
0
  Curl_dnscache_prune(data);
573
574
  /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
575
     forced us to close this connection. This is ignored for requests taking
576
     place in a NTLM/NEGOTIATE authentication handshake
577
578
     if conn->bits.close is TRUE, it means that the connection should be
579
     closed in spite of all our efforts to be nice, due to protocol
580
     restrictions in our or the server's end
581
582
     if premature is TRUE, it means this connection was said to be DONE before
583
     the entire request operation is complete and thus we cannot know in what
584
     state it is for reusing, so we are forced to close it. In a perfect world
585
     we can add code that keep track of if we really must close it here or not,
586
     but currently we have no such detail knowledge.
587
  */
588
589
0
  if((data->set.reuse_forbid
590
0
#ifdef USE_NTLM
591
0
      && !(conn->http_ntlm_state == NTLMSTATE_TYPE2 ||
592
0
           conn->proxy_ntlm_state == NTLMSTATE_TYPE2)
593
0
#endif
594
#ifdef USE_SPNEGO
595
      && !(conn->http_negotiate_state == GSS_AUTHRECV ||
596
           conn->proxy_negotiate_state == GSS_AUTHRECV)
597
#endif
598
0
     ) || conn->bits.close
599
0
       || (mdctx->premature && !Curl_conn_is_multiplex(conn, FIRSTSOCKET))) {
600
0
#ifndef CURL_DISABLE_VERBOSE_STRINGS
601
0
    CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s:%d, "
602
0
               "forbid=%d, close=%d, premature=%d, conn_multiplex=%d",
603
0
               conn->connection_id, host, port, data->set.reuse_forbid,
604
0
               conn->bits.close, mdctx->premature,
605
0
               Curl_conn_is_multiplex(conn, FIRSTSOCKET));
606
0
#endif
607
0
    connclose(conn, "disconnecting");
608
0
    Curl_conn_terminate(data, conn, mdctx->premature);
609
0
  }
610
0
  else if(!Curl_conn_get_max_concurrent(data, conn, FIRSTSOCKET)) {
611
0
#ifndef CURL_DISABLE_VERBOSE_STRINGS
612
0
    CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s:%d was shutdown"
613
0
               " by server, not reusing", conn->connection_id, host, port);
614
0
#endif
615
0
    connclose(conn, "server shutdown");
616
0
    Curl_conn_terminate(data, conn, mdctx->premature);
617
0
  }
618
0
  else {
619
    /* the connection is no longer in use by any transfer */
620
0
    if(Curl_cpool_conn_now_idle(data, conn)) {
621
      /* connection kept in the cpool */
622
0
      data->state.lastconnect_id = conn->connection_id;
623
0
#ifndef CURL_DISABLE_VERBOSE_STRINGS
624
0
      infof(data, "Connection #%" FMT_OFF_T " to host %s:%d left intact",
625
0
            conn->connection_id, host, port);
626
0
#endif
627
0
    }
628
0
    else {
629
      /* connection was removed from the cpool and destroyed. */
630
0
      data->state.lastconnect_id = -1;
631
0
    }
632
0
  }
633
0
}
634
635
static CURLcode multi_done(struct Curl_easy *data,
636
                           CURLcode status,  /* an error if this is called
637
                                                after an error was detected */
638
                           bool premature)
639
0
{
640
0
  CURLcode result;
641
0
  struct connectdata *conn = data->conn;
642
0
  struct multi_done_ctx mdctx;
643
644
0
  memset(&mdctx, 0, sizeof(mdctx));
645
646
0
  CURL_TRC_M(data, "multi_done: status: %d prem: %d done: %d",
647
0
             (int)status, (int)premature, data->state.done);
648
649
0
  if(data->state.done)
650
    /* Stop if multi_done() has already been called */
651
0
    return CURLE_OK;
652
653
  /* Shut down any ongoing async resolver operation. */
654
0
  Curl_async_shutdown(data);
655
656
  /* Cleanup possible redirect junk */
657
0
  Curl_safefree(data->req.newurl);
658
0
  Curl_safefree(data->req.location);
659
660
0
  switch(status) {
661
0
  case CURLE_ABORTED_BY_CALLBACK:
662
0
  case CURLE_READ_ERROR:
663
0
  case CURLE_WRITE_ERROR:
664
    /* When we are aborted due to a callback return code it basically have to
665
       be counted as premature as there is trouble ahead if we do not. We have
666
       many callbacks and protocols work differently, we could potentially do
667
       this more fine-grained in the future. */
668
0
    premature = TRUE;
669
0
    FALLTHROUGH();
670
0
  default:
671
0
    break;
672
0
  }
673
674
  /* this calls the protocol-specific function pointer previously set */
675
0
  if(conn->handler->done && (data->mstate >= MSTATE_PROTOCONNECT))
676
0
    result = conn->handler->done(data, status, premature);
677
0
  else
678
0
    result = status;
679
680
0
  if(CURLE_ABORTED_BY_CALLBACK != result) {
681
    /* avoid this if we already aborted by callback to avoid this calling
682
       another callback */
683
0
    int rc = Curl_pgrsDone(data);
684
0
    if(!result && rc)
685
0
      result = CURLE_ABORTED_BY_CALLBACK;
686
0
  }
687
688
  /* Make sure that transfer client writes are really done now. */
689
0
  result = Curl_1st_err(result, Curl_xfer_write_done(data, premature));
690
691
  /* Inform connection filters that this transfer is done */
692
0
  Curl_conn_ev_data_done(data, premature);
693
694
0
  process_pending_handles(data->multi); /* connection / multiplex */
695
696
0
  if(!result)
697
0
    result = Curl_req_done(&data->req, data, premature);
698
699
  /* Under the potential connection pool's share lock, decide what to
700
   * do with the transfer's connection. */
701
0
  mdctx.premature = premature;
702
0
  Curl_cpool_do_locked(data, data->conn, multi_done_locked, &mdctx);
703
704
  /* flush the netrc cache */
705
0
  Curl_netrc_cleanup(&data->state.netrc);
706
0
  return result;
707
0
}
708
709
static void close_connect_only(struct connectdata *conn,
710
                               struct Curl_easy *data,
711
                               void *userdata)
712
0
{
713
0
  (void)userdata;
714
0
  (void)data;
715
0
  if(conn->connect_only)
716
0
    connclose(conn, "Removing connect-only easy handle");
717
0
}
718
719
CURLMcode curl_multi_remove_handle(CURLM *m, CURL *d)
720
0
{
721
0
  struct Curl_multi *multi = m;
722
0
  struct Curl_easy *data = d;
723
0
  bool premature;
724
0
  struct Curl_llist_node *e;
725
0
  CURLMcode rc;
726
0
  bool removed_timer = FALSE;
727
0
  unsigned int mid;
728
729
  /* First, make some basic checks that the CURLM handle is a good handle */
730
0
  if(!GOOD_MULTI_HANDLE(multi))
731
0
    return CURLM_BAD_HANDLE;
732
733
  /* Verify that we got a somewhat good easy handle too */
734
0
  if(!GOOD_EASY_HANDLE(data))
735
0
    return CURLM_BAD_EASY_HANDLE;
736
737
  /* Prevent users from trying to remove same easy handle more than once */
738
0
  if(!data->multi)
739
0
    return CURLM_OK; /* it is already removed so let's say it is fine! */
740
741
  /* Prevent users from trying to remove an easy handle from the wrong multi */
742
0
  if(data->multi != multi)
743
0
    return CURLM_BAD_EASY_HANDLE;
744
745
0
  if(data->mid == UINT_MAX) {
746
0
    DEBUGASSERT(0);
747
0
    return CURLM_INTERNAL_ERROR;
748
0
  }
749
0
  if(Curl_uint_tbl_get(&multi->xfers, data->mid) != data) {
750
0
    DEBUGASSERT(0);
751
0
    return CURLM_INTERNAL_ERROR;
752
0
  }
753
754
0
  if(multi->in_callback)
755
0
    return CURLM_RECURSIVE_API_CALL;
756
757
0
  premature = (data->mstate < MSTATE_COMPLETED);
758
759
  /* If the 'state' is not INIT or COMPLETED, we might need to do something
760
     nice to put the easy_handle in a good known state when this returns. */
761
0
  if(data->conn &&
762
0
     data->mstate > MSTATE_DO &&
763
0
     data->mstate < MSTATE_COMPLETED) {
764
    /* Set connection owner so that the DONE function closes it. We can
765
       safely do this here since connection is killed. */
766
0
    streamclose(data->conn, "Removed with partial response");
767
0
  }
768
769
0
  if(data->conn) {
770
    /* multi_done() clears the association between the easy handle and the
771
       connection.
772
773
       Note that this ignores the return code simply because there is
774
       nothing really useful to do with it anyway! */
775
0
    (void)multi_done(data, data->result, premature);
776
0
  }
777
778
  /* The timer must be shut down before data->multi is set to NULL, else the
779
     timenode will remain in the splay tree after curl_easy_cleanup is
780
     called. Do it after multi_done() in case that sets another time! */
781
0
  removed_timer = Curl_expire_clear(data);
782
783
  /* If in `msgsent`, it was deducted from `multi->xfers_alive` already. */
784
0
  if(!Curl_uint_bset_contains(&multi->msgsent, data->mid))
785
0
    --multi->xfers_alive;
786
787
0
  Curl_wildcard_dtor(&data->wildcard);
788
789
0
  data->mstate = MSTATE_COMPLETED;
790
791
  /* Remove the association between the connection and the handle */
792
0
  Curl_detach_connection(data);
793
794
  /* Tell event handling that this transfer is definitely going away */
795
0
  Curl_multi_ev_xfer_done(multi, data);
796
797
0
  if(data->set.connect_only && !data->multi_easy) {
798
    /* This removes a handle that was part the multi interface that used
799
       CONNECT_ONLY, that connection is now left alive but since this handle
800
       has bits.close set nothing can use that transfer anymore and it is
801
       forbidden from reuse. This easy handle cannot find the connection
802
       anymore once removed from the multi handle
803
804
       Better close the connection here, at once.
805
    */
806
0
    struct connectdata *c;
807
0
    curl_socket_t s;
808
0
    s = Curl_getconnectinfo(data, &c);
809
0
    if((s != CURL_SOCKET_BAD) && c) {
810
0
      Curl_conn_terminate(data, c, TRUE);
811
0
    }
812
0
  }
813
814
0
  if(data->state.lastconnect_id != -1) {
815
    /* Mark any connect-only connection for closure */
816
0
    Curl_cpool_do_by_id(data, data->state.lastconnect_id,
817
0
                            close_connect_only, NULL);
818
0
  }
819
820
#ifdef USE_LIBPSL
821
  /* Remove the PSL association. */
822
  if(data->psl == &multi->psl)
823
    data->psl = NULL;
824
#endif
825
826
  /* make sure there is no pending message in the queue sent from this easy
827
     handle */
828
0
  for(e = Curl_llist_head(&multi->msglist); e; e = Curl_node_next(e)) {
829
0
    struct Curl_message *msg = Curl_node_elem(e);
830
831
0
    if(msg->extmsg.easy_handle == data) {
832
0
      Curl_node_remove(e);
833
      /* there can only be one from this specific handle */
834
0
      break;
835
0
    }
836
0
  }
837
838
  /* clear the association to this multi handle */
839
0
  mid = data->mid;
840
0
  DEBUGASSERT(Curl_uint_tbl_contains(&multi->xfers, mid));
841
0
  Curl_uint_tbl_remove(&multi->xfers, mid);
842
0
  Curl_uint_bset_remove(&multi->process, mid);
843
0
  Curl_uint_bset_remove(&multi->dirty, mid);
844
0
  Curl_uint_bset_remove(&multi->pending, mid);
845
0
  Curl_uint_bset_remove(&multi->msgsent, mid);
846
0
  data->multi = NULL;
847
0
  data->mid = UINT_MAX;
848
0
  data->master_mid = UINT_MAX;
849
850
  /* NOTE NOTE NOTE
851
     We do not touch the easy handle here! */
852
0
  process_pending_handles(multi);
853
854
0
  if(removed_timer) {
855
0
    rc = Curl_update_timer(multi);
856
0
    if(rc)
857
0
      return rc;
858
0
  }
859
860
0
  CURL_TRC_M(data, "removed from multi, mid=%u, running=%u, total=%u",
861
0
             mid, Curl_multi_xfers_running(multi),
862
0
             Curl_uint_tbl_count(&multi->xfers));
863
0
  return CURLM_OK;
864
0
}
865
866
/* Return TRUE if the application asked for multiplexing */
867
bool Curl_multiplex_wanted(const struct Curl_multi *multi)
868
0
{
869
0
  return multi && multi->multiplexing;
870
0
}
871
872
/*
873
 * Curl_detach_connection() removes the given transfer from the connection.
874
 *
875
 * This is the only function that should clear data->conn. This will
876
 * occasionally be called with the data->conn pointer already cleared.
877
 */
878
void Curl_detach_connection(struct Curl_easy *data)
879
0
{
880
0
  struct connectdata *conn = data->conn;
881
0
  if(conn) {
882
0
    Curl_uint_spbset_remove(&conn->xfers_attached, data->mid);
883
0
    if(Curl_uint_spbset_empty(&conn->xfers_attached))
884
0
      conn->attached_multi = NULL;
885
0
  }
886
0
  data->conn = NULL;
887
0
}
888
889
/*
890
 * Curl_attach_connection() attaches this transfer to this connection.
891
 *
892
 * This is the only function that should assign data->conn
893
 */
894
void Curl_attach_connection(struct Curl_easy *data,
895
                            struct connectdata *conn)
896
0
{
897
0
  DEBUGASSERT(data);
898
0
  DEBUGASSERT(!data->conn);
899
0
  DEBUGASSERT(conn);
900
0
  data->conn = conn;
901
0
  Curl_uint_spbset_add(&conn->xfers_attached, data->mid);
902
  /* all attached transfers must be from the same multi */
903
0
  if(!conn->attached_multi)
904
0
    conn->attached_multi = data->multi;
905
0
  DEBUGASSERT(conn->attached_multi == data->multi);
906
907
0
  if(conn->handler && conn->handler->attach)
908
0
    conn->handler->attach(data, conn);
909
0
}
910
911
static CURLcode mstate_connecting_pollset(struct Curl_easy *data,
912
                                          struct easy_pollset *ps)
913
0
{
914
0
  if(data->conn) {
915
0
    curl_socket_t sockfd = Curl_conn_get_first_socket(data);
916
0
    if(sockfd != CURL_SOCKET_BAD) {
917
      /* Default is to wait to something from the server */
918
0
      return Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
919
0
    }
920
0
  }
921
0
  return CURLE_OK;
922
0
}
923
924
static CURLcode mstate_protocol_pollset(struct Curl_easy *data,
925
                                        struct easy_pollset *ps)
926
0
{
927
0
  if(data->conn) {
928
0
    curl_socket_t sockfd;
929
0
    if(data->conn->handler->proto_pollset)
930
0
      return data->conn->handler->proto_pollset(data, ps);
931
0
    sockfd = data->conn->sock[FIRSTSOCKET];
932
0
    if(sockfd != CURL_SOCKET_BAD) {
933
      /* Default is to wait to something from the server */
934
0
      return Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
935
0
    }
936
0
  }
937
0
  return CURLE_OK;
938
0
}
939
940
static CURLcode mstate_do_pollset(struct Curl_easy *data,
941
                                  struct easy_pollset *ps)
942
0
{
943
0
  if(data->conn) {
944
0
    if(data->conn->handler->doing_pollset)
945
0
      return data->conn->handler->doing_pollset(data, ps);
946
0
    else if(CONN_SOCK_IDX_VALID(data->conn->send_idx)) {
947
      /* Default is that we want to send something to the server */
948
0
      return Curl_pollset_add_out(
949
0
        data, ps, data->conn->sock[data->conn->send_idx]);
950
0
    }
951
0
  }
952
0
  return CURLE_OK;
953
0
}
954
955
static CURLcode mstate_domore_pollset(struct Curl_easy *data,
956
                                      struct easy_pollset *ps)
957
0
{
958
0
  if(data->conn) {
959
0
    if(data->conn->handler->domore_pollset)
960
0
      return data->conn->handler->domore_pollset(data, ps);
961
0
    else if(CONN_SOCK_IDX_VALID(data->conn->send_idx)) {
962
      /* Default is that we want to send something to the server */
963
0
      return Curl_pollset_add_out(
964
0
        data, ps, data->conn->sock[data->conn->send_idx]);
965
0
    }
966
0
  }
967
0
  return CURLE_OK;
968
0
}
969
970
static CURLcode mstate_perform_pollset(struct Curl_easy *data,
971
                                       struct easy_pollset *ps)
972
0
{
973
0
  if(!data->conn)
974
0
    return CURLE_OK;
975
0
  else if(data->conn->handler->perform_pollset)
976
0
    return data->conn->handler->perform_pollset(data, ps);
977
0
  else {
978
    /* Default is to obey the data->req.keepon flags for send/recv */
979
0
    CURLcode result = CURLE_OK;
980
0
    if(CURL_WANT_RECV(data) && CONN_SOCK_IDX_VALID(data->conn->recv_idx)) {
981
0
      result = Curl_pollset_add_in(
982
0
        data, ps, data->conn->sock[data->conn->recv_idx]);
983
0
    }
984
985
0
    if(!result && Curl_req_want_send(data) &&
986
0
       CONN_SOCK_IDX_VALID(data->conn->send_idx)) {
987
0
      result = Curl_pollset_add_out(
988
0
        data, ps, data->conn->sock[data->conn->send_idx]);
989
0
    }
990
0
    return result;
991
0
  }
992
0
}
993
994
/* Initializes `poll_set` with the current socket poll actions needed
995
 * for transfer `data`. */
996
CURLMcode Curl_multi_pollset(struct Curl_easy *data,
997
                             struct easy_pollset *ps,
998
                             const char *caller)
999
0
{
1000
0
  CURLMcode mresult = CURLM_OK;
1001
0
  CURLcode result = CURLE_OK;
1002
0
  bool expect_sockets = TRUE;
1003
1004
  /* If the transfer has no connection, this is fine. Happens when
1005
     called via curl_multi_remove_handle() => Curl_multi_ev_assess() =>
1006
     Curl_multi_pollset(). */
1007
0
  Curl_pollset_reset(ps);
1008
0
  if(!data->conn)
1009
0
    return CURLM_OK;
1010
1011
0
  switch(data->mstate) {
1012
0
  case MSTATE_INIT:
1013
0
  case MSTATE_PENDING:
1014
0
  case MSTATE_SETUP:
1015
0
  case MSTATE_CONNECT:
1016
    /* nothing to poll for yet */
1017
0
    expect_sockets = FALSE;
1018
0
    break;
1019
1020
0
  case MSTATE_RESOLVING:
1021
0
    result = Curl_resolv_pollset(data, ps);
1022
    /* connection filters are not involved in this phase. It's ok if we get no
1023
     * sockets to wait for. Resolving can wake up from other sources. */
1024
0
    expect_sockets = FALSE;
1025
0
    break;
1026
1027
0
  case MSTATE_CONNECTING:
1028
0
  case MSTATE_TUNNELING:
1029
0
    if(!Curl_xfer_recv_is_paused(data)) {
1030
0
      result = mstate_connecting_pollset(data, ps);
1031
0
      if(!result)
1032
0
        result = Curl_conn_adjust_pollset(data, data->conn, ps);
1033
0
    }
1034
0
    else
1035
0
      expect_sockets = FALSE;
1036
0
    break;
1037
1038
0
  case MSTATE_PROTOCONNECT:
1039
0
  case MSTATE_PROTOCONNECTING:
1040
0
    result = mstate_protocol_pollset(data, ps);
1041
0
    if(!result)
1042
0
      result = Curl_conn_adjust_pollset(data, data->conn, ps);
1043
0
    break;
1044
1045
0
  case MSTATE_DO:
1046
0
  case MSTATE_DOING:
1047
0
    result = mstate_do_pollset(data, ps);
1048
0
    if(!result)
1049
0
      result = Curl_conn_adjust_pollset(data, data->conn, ps);
1050
0
    break;
1051
1052
0
  case MSTATE_DOING_MORE:
1053
0
    result = mstate_domore_pollset(data, ps);
1054
0
    if(!result)
1055
0
      result = Curl_conn_adjust_pollset(data, data->conn, ps);
1056
0
    break;
1057
1058
0
  case MSTATE_DID: /* same as PERFORMING in regard to polling */
1059
0
  case MSTATE_PERFORMING:
1060
0
    result = mstate_perform_pollset(data, ps);
1061
0
    if(!result)
1062
0
      result = Curl_conn_adjust_pollset(data, data->conn, ps);
1063
0
    break;
1064
1065
0
  case MSTATE_RATELIMITING:
1066
    /* we need to let time pass, ignore socket(s) */
1067
0
    expect_sockets = FALSE;
1068
0
    break;
1069
1070
0
  case MSTATE_DONE:
1071
0
  case MSTATE_COMPLETED:
1072
0
  case MSTATE_MSGSENT:
1073
    /* nothing more to poll for */
1074
0
    expect_sockets = FALSE;
1075
0
    break;
1076
1077
0
  default:
1078
0
    failf(data, "multi_getsock: unexpected multi state %d", data->mstate);
1079
0
    DEBUGASSERT(0);
1080
0
    expect_sockets = FALSE;
1081
0
    break;
1082
0
  }
1083
1084
0
  if(result) {
1085
0
    if(result == CURLE_OUT_OF_MEMORY)
1086
0
      mresult = CURLM_OUT_OF_MEMORY;
1087
0
    else {
1088
0
      failf(data, "error determining pollset: %d", result);
1089
0
      mresult = CURLM_INTERNAL_ERROR;
1090
0
    }
1091
0
    goto out;
1092
0
  }
1093
1094
  /* Unblocked and waiting to receive with buffered input.
1095
   * Make transfer run again at next opportunity. */
1096
0
  if(!Curl_xfer_is_blocked(data) &&
1097
0
     ((Curl_pollset_want_read(data, ps, data->conn->sock[FIRSTSOCKET]) &&
1098
0
       Curl_conn_data_pending(data, FIRSTSOCKET)) ||
1099
0
      (Curl_pollset_want_read(data, ps, data->conn->sock[SECONDARYSOCKET]) &&
1100
0
       Curl_conn_data_pending(data, SECONDARYSOCKET)))) {
1101
0
    CURL_TRC_M(data, "%s pollset[] has POLLIN, but there is still "
1102
0
               "buffered input to consume -> mark as dirty", caller);
1103
0
    Curl_multi_mark_dirty(data);
1104
0
  }
1105
1106
0
  switch(ps->n) {
1107
0
    case 0:
1108
0
      CURL_TRC_M(data, "%s pollset[], timeouts=%zu, paused %d/%d (r/w)",
1109
0
                 caller, Curl_llist_count(&data->state.timeoutlist),
1110
0
                 Curl_xfer_send_is_paused(data),
1111
0
                 Curl_xfer_recv_is_paused(data));
1112
0
      break;
1113
0
    case 1:
1114
0
      CURL_TRC_M(data, "%s pollset[fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
1115
0
                 caller, ps->sockets[0],
1116
0
                 (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
1117
0
                 (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
1118
0
                 Curl_llist_count(&data->state.timeoutlist));
1119
0
      break;
1120
0
    case 2:
1121
0
      CURL_TRC_M(data, "%s pollset[fd=%" FMT_SOCKET_T " %s%s, "
1122
0
                 "fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
1123
0
                 caller, ps->sockets[0],
1124
0
                 (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
1125
0
                 (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
1126
0
                 ps->sockets[1],
1127
0
                 (ps->actions[1] & CURL_POLL_IN) ? "IN" : "",
1128
0
                 (ps->actions[1] & CURL_POLL_OUT) ? "OUT" : "",
1129
0
                 Curl_llist_count(&data->state.timeoutlist));
1130
0
      break;
1131
0
    default:
1132
0
      CURL_TRC_M(data, "%s pollset[fds=%u], timeouts=%zu",
1133
0
                 caller, ps->n, Curl_llist_count(&data->state.timeoutlist));
1134
0
      break;
1135
0
  }
1136
0
  if(expect_sockets && !ps->n && data->multi &&
1137
0
     !Curl_uint_bset_contains(&data->multi->dirty, data->mid) &&
1138
0
     !Curl_llist_count(&data->state.timeoutlist) &&
1139
0
     !Curl_cwriter_is_paused(data) && !Curl_creader_is_paused(data) &&
1140
0
     Curl_conn_is_ip_connected(data, FIRSTSOCKET)) {
1141
    /* We expected sockets for POLL monitoring, but none are set.
1142
     * We are not dirty (and run anyway).
1143
     * We are not waiting on any timer.
1144
     * None of the READ/WRITE directions are paused.
1145
     * We are connected to the server on IP level, at least. */
1146
0
    infof(data, "WARNING: no socket in pollset or timer, transfer may stall!");
1147
0
    DEBUGASSERT(0);
1148
0
  }
1149
0
out:
1150
0
  return mresult;
1151
0
}
1152
1153
CURLMcode curl_multi_fdset(CURLM *m,
1154
                           fd_set *read_fd_set, fd_set *write_fd_set,
1155
                           fd_set *exc_fd_set, int *max_fd)
1156
0
{
1157
  /* Scan through all the easy handles to get the file descriptors set.
1158
     Some easy handles may not have connected to the remote host yet,
1159
     and then we must make sure that is done. */
1160
0
  int this_max_fd = -1;
1161
0
  struct Curl_multi *multi = m;
1162
0
  struct easy_pollset ps;
1163
0
  unsigned int i, mid;
1164
0
  (void)exc_fd_set; /* not used */
1165
1166
0
  if(!GOOD_MULTI_HANDLE(multi))
1167
0
    return CURLM_BAD_HANDLE;
1168
1169
0
  if(multi->in_callback)
1170
0
    return CURLM_RECURSIVE_API_CALL;
1171
1172
0
  Curl_pollset_init(&ps);
1173
0
  if(Curl_uint_bset_first(&multi->process, &mid)) {
1174
0
    do {
1175
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
1176
1177
0
      if(!data) {
1178
0
        DEBUGASSERT(0);
1179
0
        continue;
1180
0
      }
1181
1182
0
      Curl_multi_pollset(data, &ps, "curl_multi_fdset");
1183
0
      for(i = 0; i < ps.n; i++) {
1184
0
        if(!FDSET_SOCK(ps.sockets[i]))
1185
          /* pretend it does not exist */
1186
0
          continue;
1187
#ifdef __DJGPP__
1188
#pragma GCC diagnostic push
1189
#pragma GCC diagnostic ignored "-Warith-conversion"
1190
#endif
1191
0
        if(ps.actions[i] & CURL_POLL_IN)
1192
0
          FD_SET(ps.sockets[i], read_fd_set);
1193
0
        if(ps.actions[i] & CURL_POLL_OUT)
1194
0
          FD_SET(ps.sockets[i], write_fd_set);
1195
#ifdef __DJGPP__
1196
#pragma GCC diagnostic pop
1197
#endif
1198
0
        if((int)ps.sockets[i] > this_max_fd)
1199
0
          this_max_fd = (int)ps.sockets[i];
1200
0
      }
1201
0
    }
1202
0
    while(Curl_uint_bset_next(&multi->process, mid, &mid));
1203
0
  }
1204
1205
0
  Curl_cshutdn_setfds(&multi->cshutdn, multi->admin,
1206
0
                      read_fd_set, write_fd_set, &this_max_fd);
1207
1208
0
  *max_fd = this_max_fd;
1209
0
  Curl_pollset_cleanup(&ps);
1210
1211
0
  return CURLM_OK;
1212
0
}
1213
1214
CURLMcode curl_multi_waitfds(CURLM *m,
1215
                             struct curl_waitfd *ufds,
1216
                             unsigned int size,
1217
                             unsigned int *fd_count)
1218
0
{
1219
0
  struct Curl_waitfds cwfds;
1220
0
  CURLMcode result = CURLM_OK;
1221
0
  struct Curl_multi *multi = m;
1222
0
  struct easy_pollset ps;
1223
0
  unsigned int need = 0, mid;
1224
1225
0
  if(!ufds && (size || !fd_count))
1226
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
1227
1228
0
  if(!GOOD_MULTI_HANDLE(multi))
1229
0
    return CURLM_BAD_HANDLE;
1230
1231
0
  if(multi->in_callback)
1232
0
    return CURLM_RECURSIVE_API_CALL;
1233
1234
0
  Curl_pollset_init(&ps);
1235
0
  Curl_waitfds_init(&cwfds, ufds, size);
1236
0
  if(Curl_uint_bset_first(&multi->process, &mid)) {
1237
0
    do {
1238
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
1239
0
      if(!data) {
1240
0
        DEBUGASSERT(0);
1241
0
        Curl_uint_bset_remove(&multi->process, mid);
1242
0
        Curl_uint_bset_remove(&multi->dirty, mid);
1243
0
        continue;
1244
0
      }
1245
0
      Curl_multi_pollset(data, &ps, "curl_multi_waitfds");
1246
0
      need += Curl_waitfds_add_ps(&cwfds, &ps);
1247
0
    }
1248
0
    while(Curl_uint_bset_next(&multi->process, mid, &mid));
1249
0
  }
1250
1251
0
  need += Curl_cshutdn_add_waitfds(&multi->cshutdn, multi->admin, &cwfds);
1252
1253
0
  if(need != cwfds.n && ufds) {
1254
0
    result = CURLM_OUT_OF_MEMORY;
1255
0
  }
1256
1257
0
  if(fd_count)
1258
0
    *fd_count = need;
1259
0
  Curl_pollset_cleanup(&ps);
1260
0
  return result;
1261
0
}
1262
1263
#ifdef USE_WINSOCK
1264
/* Reset FD_WRITE for TCP sockets. Nothing is actually sent. UDP sockets cannot
1265
 * be reset this way because an empty datagram would be sent. #9203
1266
 *
1267
 * "On Windows the internal state of FD_WRITE as returned from
1268
 * WSAEnumNetworkEvents is only reset after successful send()."
1269
 */
1270
static void reset_socket_fdwrite(curl_socket_t s)
1271
{
1272
  int t;
1273
  int l = (int)sizeof(t);
1274
  if(!getsockopt(s, SOL_SOCKET, SO_TYPE, (char *)&t, &l) && t == SOCK_STREAM)
1275
    send(s, NULL, 0, 0);
1276
}
1277
#endif
1278
1279
0
#define NUM_POLLS_ON_STACK 10
1280
1281
static CURLMcode multi_wait(struct Curl_multi *multi,
1282
                            struct curl_waitfd extra_fds[],
1283
                            unsigned int extra_nfds,
1284
                            int timeout_ms,
1285
                            int *ret,
1286
                            bool extrawait, /* when no socket, wait */
1287
                            bool use_wakeup)
1288
0
{
1289
0
  size_t i;
1290
0
  struct curltime expire_time;
1291
0
  long timeout_internal;
1292
0
  int retcode = 0;
1293
0
  struct easy_pollset ps;
1294
0
  struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
1295
0
  struct curl_pollfds cpfds;
1296
0
  unsigned int curl_nfds = 0; /* how many pfds are for curl transfers */
1297
0
  struct Curl_easy *data = NULL;
1298
0
  CURLMcode result = CURLM_OK;
1299
0
  unsigned int mid;
1300
1301
#ifdef USE_WINSOCK
1302
  WSANETWORKEVENTS wsa_events;
1303
  DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT);
1304
#endif
1305
#ifndef ENABLE_WAKEUP
1306
  (void)use_wakeup;
1307
#endif
1308
1309
0
  if(!GOOD_MULTI_HANDLE(multi))
1310
0
    return CURLM_BAD_HANDLE;
1311
1312
0
  if(multi->in_callback)
1313
0
    return CURLM_RECURSIVE_API_CALL;
1314
1315
0
  if(timeout_ms < 0)
1316
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
1317
1318
0
  Curl_pollset_init(&ps);
1319
0
  Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
1320
1321
  /* Add the curl handles to our pollfds first */
1322
0
  if(Curl_uint_bset_first(&multi->process, &mid)) {
1323
0
    do {
1324
0
      data = Curl_multi_get_easy(multi, mid);
1325
0
      if(!data) {
1326
0
        DEBUGASSERT(0);
1327
0
        Curl_uint_bset_remove(&multi->process, mid);
1328
0
        Curl_uint_bset_remove(&multi->dirty, mid);
1329
0
        continue;
1330
0
      }
1331
0
      Curl_multi_pollset(data, &ps, "multi_wait");
1332
0
      if(Curl_pollfds_add_ps(&cpfds, &ps)) {
1333
0
        result = CURLM_OUT_OF_MEMORY;
1334
0
        goto out;
1335
0
      }
1336
0
    }
1337
0
    while(Curl_uint_bset_next(&multi->process, mid, &mid));
1338
0
  }
1339
1340
0
  if(Curl_cshutdn_add_pollfds(&multi->cshutdn, multi->admin, &cpfds)) {
1341
0
    result = CURLM_OUT_OF_MEMORY;
1342
0
    goto out;
1343
0
  }
1344
1345
0
  curl_nfds = cpfds.n; /* what curl internally uses in cpfds */
1346
  /* Add external file descriptions from poll-like struct curl_waitfd */
1347
0
  for(i = 0; i < extra_nfds; i++) {
1348
0
    unsigned short events = 0;
1349
0
    if(extra_fds[i].events & CURL_WAIT_POLLIN)
1350
0
      events |= POLLIN;
1351
0
    if(extra_fds[i].events & CURL_WAIT_POLLPRI)
1352
0
      events |= POLLPRI;
1353
0
    if(extra_fds[i].events & CURL_WAIT_POLLOUT)
1354
0
      events |= POLLOUT;
1355
0
    if(Curl_pollfds_add_sock(&cpfds, extra_fds[i].fd, events)) {
1356
0
      result = CURLM_OUT_OF_MEMORY;
1357
0
      goto out;
1358
0
    }
1359
0
  }
1360
1361
#ifdef USE_WINSOCK
1362
  /* Set the WSA events based on the collected pollds */
1363
  for(i = 0; i < cpfds.n; i++) {
1364
    long mask = 0;
1365
    if(cpfds.pfds[i].events & POLLIN)
1366
      mask |= FD_READ|FD_ACCEPT|FD_CLOSE;
1367
    if(cpfds.pfds[i].events & POLLPRI)
1368
      mask |= FD_OOB;
1369
    if(cpfds.pfds[i].events & POLLOUT) {
1370
      mask |= FD_WRITE|FD_CONNECT|FD_CLOSE;
1371
      reset_socket_fdwrite(cpfds.pfds[i].fd);
1372
    }
1373
    if(mask) {
1374
      if(WSAEventSelect(cpfds.pfds[i].fd, multi->wsa_event, mask) != 0) {
1375
        result = CURLM_OUT_OF_MEMORY;
1376
        goto out;
1377
      }
1378
    }
1379
  }
1380
#endif
1381
1382
0
#ifdef ENABLE_WAKEUP
1383
0
#ifndef USE_WINSOCK
1384
0
  if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
1385
0
    if(Curl_pollfds_add_sock(&cpfds, multi->wakeup_pair[0], POLLIN)) {
1386
0
      result = CURLM_OUT_OF_MEMORY;
1387
0
      goto out;
1388
0
    }
1389
0
  }
1390
0
#endif
1391
0
#endif
1392
1393
  /* We check the internal timeout *AFTER* we collected all sockets to
1394
   * poll. Collecting the sockets may install new timers by protocols
1395
   * and connection filters.
1396
   * Use the shorter one of the internal and the caller requested timeout. */
1397
0
  (void)multi_timeout(multi, &expire_time, &timeout_internal);
1398
0
  if((timeout_internal >= 0) && (timeout_internal < (long)timeout_ms))
1399
0
    timeout_ms = (int)timeout_internal;
1400
1401
0
  if(data)
1402
0
    CURL_TRC_M(data, "multi_wait(fds=%d, timeout=%d) tinternal=%ld",
1403
0
               cpfds.n, timeout_ms, timeout_internal);
1404
#if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK)
1405
  if(cpfds.n || use_wakeup) {
1406
#else
1407
0
  if(cpfds.n) {
1408
0
#endif
1409
0
    int pollrc;
1410
#ifdef USE_WINSOCK
1411
    if(cpfds.n)         /* just pre-check with Winsock */
1412
      pollrc = Curl_poll(cpfds.pfds, cpfds.n, 0);
1413
    else
1414
      pollrc = 0;
1415
#else
1416
0
    pollrc = Curl_poll(cpfds.pfds, cpfds.n, timeout_ms); /* wait... */
1417
0
#endif
1418
0
    if(pollrc < 0) {
1419
0
      result = CURLM_UNRECOVERABLE_POLL;
1420
0
      goto out;
1421
0
    }
1422
1423
0
    if(pollrc > 0) {
1424
0
      retcode = pollrc;
1425
#ifdef USE_WINSOCK
1426
    }
1427
    else { /* now wait... if not ready during the pre-check (pollrc == 0) */
1428
      WSAWaitForMultipleEvents(1, &multi->wsa_event, FALSE, (DWORD)timeout_ms,
1429
                               FALSE);
1430
    }
1431
    /* With Winsock, we have to run the following section unconditionally
1432
       to call WSAEventSelect(fd, event, 0) on all the sockets */
1433
    {
1434
#endif
1435
      /* copy revents results from the poll to the curl_multi_wait poll
1436
         struct, the bit values of the actual underlying poll() implementation
1437
         may not be the same as the ones in the public libcurl API! */
1438
0
      for(i = 0; i < extra_nfds; i++) {
1439
0
        unsigned r = (unsigned)cpfds.pfds[curl_nfds + i].revents;
1440
0
        unsigned short mask = 0;
1441
#ifdef USE_WINSOCK
1442
        curl_socket_t s = extra_fds[i].fd;
1443
        wsa_events.lNetworkEvents = 0;
1444
        if(WSAEnumNetworkEvents(s, NULL, &wsa_events) == 0) {
1445
          if(wsa_events.lNetworkEvents & (FD_READ|FD_ACCEPT|FD_CLOSE))
1446
            mask |= CURL_WAIT_POLLIN;
1447
          if(wsa_events.lNetworkEvents & (FD_WRITE|FD_CONNECT|FD_CLOSE))
1448
            mask |= CURL_WAIT_POLLOUT;
1449
          if(wsa_events.lNetworkEvents & FD_OOB)
1450
            mask |= CURL_WAIT_POLLPRI;
1451
          if(ret && !pollrc && wsa_events.lNetworkEvents)
1452
            retcode++;
1453
        }
1454
        WSAEventSelect(s, multi->wsa_event, 0);
1455
        if(!pollrc) {
1456
          extra_fds[i].revents = (short)mask;
1457
          continue;
1458
        }
1459
#endif
1460
0
        if(r & POLLIN)
1461
0
          mask |= CURL_WAIT_POLLIN;
1462
0
        if(r & POLLOUT)
1463
0
          mask |= CURL_WAIT_POLLOUT;
1464
0
        if(r & POLLPRI)
1465
0
          mask |= CURL_WAIT_POLLPRI;
1466
0
        extra_fds[i].revents = (short)mask;
1467
0
      }
1468
1469
#ifdef USE_WINSOCK
1470
      /* Count up all our own sockets that had activity,
1471
         and remove them from the event. */
1472
      for(i = 0; i < curl_nfds; ++i) {
1473
        wsa_events.lNetworkEvents = 0;
1474
        if(WSAEnumNetworkEvents(cpfds.pfds[i].fd, NULL, &wsa_events) == 0) {
1475
          if(ret && !pollrc && wsa_events.lNetworkEvents)
1476
            retcode++;
1477
        }
1478
        WSAEventSelect(cpfds.pfds[i].fd, multi->wsa_event, 0);
1479
      }
1480
      WSAResetEvent(multi->wsa_event);
1481
#else
1482
0
#ifdef ENABLE_WAKEUP
1483
0
      if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
1484
0
        if(cpfds.pfds[curl_nfds + extra_nfds].revents & POLLIN) {
1485
0
          char buf[64];
1486
0
          ssize_t nread;
1487
0
          while(1) {
1488
            /* the reading socket is non-blocking, try to read
1489
               data from it until it receives an error (except EINTR).
1490
               In normal cases it will get EAGAIN or EWOULDBLOCK
1491
               when there is no more data, breaking the loop. */
1492
0
            nread = wakeup_read(multi->wakeup_pair[0], buf, sizeof(buf));
1493
0
            if(nread <= 0) {
1494
0
              if(nread < 0 && SOCKEINTR == SOCKERRNO)
1495
0
                continue;
1496
0
              break;
1497
0
            }
1498
0
          }
1499
          /* do not count the wakeup socket into the returned value */
1500
0
          retcode--;
1501
0
        }
1502
0
      }
1503
0
#endif
1504
0
#endif
1505
0
    }
1506
0
  }
1507
1508
0
  if(ret)
1509
0
    *ret = retcode;
1510
#if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK)
1511
  if(extrawait && !cpfds.n && !use_wakeup) {
1512
#else
1513
0
  if(extrawait && !cpfds.n) {
1514
0
#endif
1515
0
    long sleep_ms = 0;
1516
1517
    /* Avoid busy-looping when there is nothing particular to wait for */
1518
0
    if(!curl_multi_timeout(multi, &sleep_ms) && sleep_ms) {
1519
0
      if(sleep_ms > timeout_ms)
1520
0
        sleep_ms = timeout_ms;
1521
      /* when there are no easy handles in the multi, this holds a -1
1522
         timeout */
1523
0
      else if(sleep_ms < 0)
1524
0
        sleep_ms = timeout_ms;
1525
0
      curlx_wait_ms(sleep_ms);
1526
0
    }
1527
0
  }
1528
1529
0
out:
1530
0
  Curl_pollset_cleanup(&ps);
1531
0
  Curl_pollfds_cleanup(&cpfds);
1532
0
  return result;
1533
0
}
1534
1535
CURLMcode curl_multi_wait(CURLM *multi,
1536
                          struct curl_waitfd extra_fds[],
1537
                          unsigned int extra_nfds,
1538
                          int timeout_ms,
1539
                          int *ret)
1540
0
{
1541
0
  return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, FALSE,
1542
0
                    FALSE);
1543
0
}
1544
1545
CURLMcode curl_multi_poll(CURLM *multi,
1546
                          struct curl_waitfd extra_fds[],
1547
                          unsigned int extra_nfds,
1548
                          int timeout_ms,
1549
                          int *ret)
1550
0
{
1551
0
  return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, TRUE,
1552
0
                    TRUE);
1553
0
}
1554
1555
CURLMcode curl_multi_wakeup(CURLM *m)
1556
0
{
1557
  /* this function is usually called from another thread,
1558
     it has to be careful only to access parts of the
1559
     Curl_multi struct that are constant */
1560
0
  struct Curl_multi *multi = m;
1561
1562
  /* GOOD_MULTI_HANDLE can be safely called */
1563
0
  if(!GOOD_MULTI_HANDLE(multi))
1564
0
    return CURLM_BAD_HANDLE;
1565
1566
0
#ifdef ENABLE_WAKEUP
1567
#ifdef USE_WINSOCK
1568
  if(WSASetEvent(multi->wsa_event))
1569
    return CURLM_OK;
1570
#else
1571
  /* the wakeup_pair variable is only written during init and cleanup,
1572
     making it safe to access from another thread after the init part
1573
     and before cleanup */
1574
0
  if(multi->wakeup_pair[1] != CURL_SOCKET_BAD) {
1575
0
    while(1) {
1576
0
#ifdef USE_EVENTFD
1577
      /* eventfd has a stringent rule of requiring the 8-byte buffer when
1578
         calling write(2) on it */
1579
0
      const uint64_t buf[1] = { 1 };
1580
#else
1581
      const char buf[1] = { 1 };
1582
#endif
1583
      /* swrite() is not thread-safe in general, because concurrent calls
1584
         can have their messages interleaved, but in this case the content
1585
         of the messages does not matter, which makes it ok to call.
1586
1587
         The write socket is set to non-blocking, this way this function
1588
         cannot block, making it safe to call even from the same thread
1589
         that will call curl_multi_wait(). If swrite() returns that it
1590
         would block, it is considered successful because it means that
1591
         previous calls to this function will wake up the poll(). */
1592
0
      if(wakeup_write(multi->wakeup_pair[1], buf, sizeof(buf)) < 0) {
1593
0
        int err = SOCKERRNO;
1594
0
        int return_success;
1595
#ifdef USE_WINSOCK
1596
        return_success = SOCKEWOULDBLOCK == err;
1597
#else
1598
0
        if(SOCKEINTR == err)
1599
0
          continue;
1600
0
        return_success = SOCKEWOULDBLOCK == err || EAGAIN == err;
1601
0
#endif
1602
0
        if(!return_success)
1603
0
          return CURLM_WAKEUP_FAILURE;
1604
0
      }
1605
0
      return CURLM_OK;
1606
0
    }
1607
0
  }
1608
0
#endif
1609
0
#endif
1610
0
  return CURLM_WAKEUP_FAILURE;
1611
0
}
1612
1613
/*
1614
 * multi_ischanged() is called
1615
 *
1616
 * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND
1617
 * => CONNECT action.
1618
 *
1619
 * Set 'clear' to TRUE to have it also clear the state variable.
1620
 */
1621
static bool multi_ischanged(struct Curl_multi *multi, bool clear)
1622
0
{
1623
0
  bool retval = multi->recheckstate;
1624
0
  if(clear)
1625
0
    multi->recheckstate = FALSE;
1626
0
  return retval;
1627
0
}
1628
1629
/*
1630
 * Curl_multi_connchanged() is called to tell that there is a connection in
1631
 * this multi handle that has changed state (multiplexing become possible, the
1632
 * number of allowed streams changed or similar), and a subsequent use of this
1633
 * multi handle should move CONNECT_PEND handles back to CONNECT to have them
1634
 * retry.
1635
 */
1636
void Curl_multi_connchanged(struct Curl_multi *multi)
1637
0
{
1638
0
  multi->recheckstate = TRUE;
1639
0
}
1640
1641
CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
1642
                                 struct Curl_easy *data,
1643
                                 struct connectdata *conn)
1644
0
{
1645
0
  CURLMcode rc;
1646
1647
0
  if(multi->in_callback)
1648
0
    return CURLM_RECURSIVE_API_CALL;
1649
1650
0
  rc = curl_multi_add_handle(multi, data);
1651
0
  if(!rc) {
1652
0
    struct SingleRequest *k = &data->req;
1653
0
    CURLcode result;
1654
1655
    /* pass in NULL for 'conn' here since we do not want to init the
1656
       connection, only this transfer */
1657
0
    result = Curl_init_do(data, NULL);
1658
0
    if(result) {
1659
0
      curl_multi_remove_handle(multi, data);
1660
0
      return CURLM_INTERNAL_ERROR;
1661
0
    }
1662
1663
    /* take this handle to the perform state right away */
1664
0
    multistate(data, MSTATE_PERFORMING);
1665
0
    Curl_attach_connection(data, conn);
1666
0
    k->keepon |= KEEP_RECV; /* setup to receive! */
1667
0
  }
1668
0
  return rc;
1669
0
}
1670
1671
static CURLcode multi_do(struct Curl_easy *data, bool *done)
1672
0
{
1673
0
  CURLcode result = CURLE_OK;
1674
0
  struct connectdata *conn = data->conn;
1675
1676
0
  DEBUGASSERT(conn);
1677
0
  DEBUGASSERT(conn->handler);
1678
1679
0
  if(conn->handler->do_it)
1680
0
    result = conn->handler->do_it(data, done);
1681
1682
0
  return result;
1683
0
}
1684
1685
/*
1686
 * multi_do_more() is called during the DO_MORE multi state. It is basically a
1687
 * second stage DO state which (wrongly) was introduced to support FTP's
1688
 * second connection.
1689
 *
1690
 * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
1691
 * DOING state there is more work to do!
1692
 */
1693
1694
static CURLcode multi_do_more(struct Curl_easy *data, int *complete)
1695
0
{
1696
0
  CURLcode result = CURLE_OK;
1697
0
  struct connectdata *conn = data->conn;
1698
1699
0
  *complete = 0;
1700
1701
0
  if(conn->handler->do_more)
1702
0
    result = conn->handler->do_more(data, complete);
1703
1704
0
  return result;
1705
0
}
1706
1707
/*
1708
 * Check whether a timeout occurred, and handle it if it did
1709
 */
1710
static bool multi_handle_timeout(struct Curl_easy *data,
1711
                                 struct curltime *now,
1712
                                 bool *stream_error,
1713
                                 CURLcode *result)
1714
0
{
1715
0
  bool connect_timeout = data->mstate < MSTATE_DO;
1716
0
  timediff_t timeout_ms = Curl_timeleft(data, now, connect_timeout);
1717
0
  if(timeout_ms < 0) {
1718
    /* Handle timed out */
1719
0
    struct curltime since;
1720
0
    if(connect_timeout)
1721
0
      since = data->progress.t_startsingle;
1722
0
    else
1723
0
      since = data->progress.t_startop;
1724
0
    if(data->mstate == MSTATE_RESOLVING)
1725
0
      failf(data, "Resolving timed out after %" FMT_TIMEDIFF_T
1726
0
            " milliseconds", curlx_timediff(*now, since));
1727
0
    else if(data->mstate == MSTATE_CONNECTING)
1728
0
      failf(data, "Connection timed out after %" FMT_TIMEDIFF_T
1729
0
            " milliseconds", curlx_timediff(*now, since));
1730
0
    else {
1731
0
      struct SingleRequest *k = &data->req;
1732
0
      if(k->size != -1) {
1733
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
1734
0
              " milliseconds with %" FMT_OFF_T " out of %"
1735
0
              FMT_OFF_T " bytes received",
1736
0
              curlx_timediff(*now, since), k->bytecount, k->size);
1737
0
      }
1738
0
      else {
1739
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
1740
0
              " milliseconds with %" FMT_OFF_T " bytes received",
1741
0
              curlx_timediff(*now, since), k->bytecount);
1742
0
      }
1743
0
    }
1744
0
    *result = CURLE_OPERATION_TIMEDOUT;
1745
0
    if(data->conn) {
1746
      /* Force connection closed if the connection has indeed been used */
1747
0
      if(data->mstate > MSTATE_DO) {
1748
0
        streamclose(data->conn, "Disconnect due to timeout");
1749
0
        *stream_error = TRUE;
1750
0
      }
1751
0
      (void)multi_done(data, *result, TRUE);
1752
0
    }
1753
0
    return TRUE;
1754
0
  }
1755
1756
0
  return FALSE;
1757
0
}
1758
1759
/*
1760
 * We are doing protocol-specific connecting and this is being called over and
1761
 * over from the multi interface until the connection phase is done on
1762
 * protocol layer.
1763
 */
1764
1765
static CURLcode protocol_connecting(struct Curl_easy *data, bool *done)
1766
0
{
1767
0
  CURLcode result = CURLE_OK;
1768
0
  struct connectdata *conn = data->conn;
1769
1770
0
  if(conn && conn->handler->connecting) {
1771
0
    *done = FALSE;
1772
0
    result = conn->handler->connecting(data, done);
1773
0
  }
1774
0
  else
1775
0
    *done = TRUE;
1776
1777
0
  return result;
1778
0
}
1779
1780
/*
1781
 * We are DOING this is being called over and over from the multi interface
1782
 * until the DOING phase is done on protocol layer.
1783
 */
1784
1785
static CURLcode protocol_doing(struct Curl_easy *data, bool *done)
1786
0
{
1787
0
  CURLcode result = CURLE_OK;
1788
0
  struct connectdata *conn = data->conn;
1789
1790
0
  if(conn && conn->handler->doing) {
1791
0
    *done = FALSE;
1792
0
    result = conn->handler->doing(data, done);
1793
0
  }
1794
0
  else
1795
0
    *done = TRUE;
1796
1797
0
  return result;
1798
0
}
1799
1800
/*
1801
 * We have discovered that the TCP connection has been successful, we can now
1802
 * proceed with some action.
1803
 *
1804
 */
1805
static CURLcode protocol_connect(struct Curl_easy *data,
1806
                                 bool *protocol_done)
1807
0
{
1808
0
  CURLcode result = CURLE_OK;
1809
0
  struct connectdata *conn = data->conn;
1810
0
  DEBUGASSERT(conn);
1811
0
  DEBUGASSERT(protocol_done);
1812
1813
0
  *protocol_done = FALSE;
1814
1815
0
  if(Curl_conn_is_connected(conn, FIRSTSOCKET)
1816
0
     && conn->bits.protoconnstart) {
1817
    /* We already are connected, get back. This may happen when the connect
1818
       worked fine in the first call, like when we connect to a local server
1819
       or proxy. Note that we do not know if the protocol is actually done.
1820
1821
       Unless this protocol does not have any protocol-connect callback, as
1822
       then we know we are done. */
1823
0
    if(!conn->handler->connecting)
1824
0
      *protocol_done = TRUE;
1825
1826
0
    return CURLE_OK;
1827
0
  }
1828
1829
0
  if(!conn->bits.protoconnstart) {
1830
0
    if(conn->handler->connect_it) {
1831
      /* is there a protocol-specific connect() procedure? */
1832
1833
      /* Call the protocol-specific connect function */
1834
0
      result = conn->handler->connect_it(data, protocol_done);
1835
0
    }
1836
0
    else
1837
0
      *protocol_done = TRUE;
1838
1839
    /* it has started, possibly even completed but that knowledge is not stored
1840
       in this bit! */
1841
0
    if(!result)
1842
0
      conn->bits.protoconnstart = TRUE;
1843
0
  }
1844
1845
0
  return result; /* pass back status */
1846
0
}
1847
1848
static void set_in_callback(struct Curl_multi *multi, bool value)
1849
0
{
1850
0
  multi->in_callback = value;
1851
0
}
1852
1853
/*
1854
 * posttransfer() is called immediately after a transfer ends
1855
 */
1856
static void multi_posttransfer(struct Curl_easy *data)
1857
0
{
1858
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
1859
  /* restore the signal handler for SIGPIPE before we get back */
1860
  if(!data->set.no_signal)
1861
    signal(SIGPIPE, data->state.prev_signal);
1862
#else
1863
0
  (void)data; /* unused parameter */
1864
0
#endif
1865
0
}
1866
1867
/*
1868
 * multi_follow() handles the URL redirect magic. Pass in the 'newurl' string
1869
 * as given by the remote server and set up the new URL to request.
1870
 *
1871
 * This function DOES NOT FREE the given url.
1872
 */
1873
static CURLcode multi_follow(struct Curl_easy *data,
1874
                             const struct Curl_handler *handler,
1875
                             const char *newurl, /* the Location: string */
1876
                             followtype type) /* see transfer.h */
1877
0
{
1878
0
  if(handler && handler->follow)
1879
0
    return handler->follow(data, newurl, type);
1880
0
  return CURLE_TOO_MANY_REDIRECTS;
1881
0
}
1882
1883
static CURLMcode state_performing(struct Curl_easy *data,
1884
                                  struct curltime *nowp,
1885
                                  bool *stream_errorp,
1886
                                  CURLcode *resultp)
1887
0
{
1888
0
  char *newurl = NULL;
1889
0
  bool retry = FALSE;
1890
0
  timediff_t recv_timeout_ms = 0;
1891
0
  timediff_t send_timeout_ms = 0;
1892
0
  CURLMcode rc = CURLM_OK;
1893
0
  CURLcode result = *resultp = CURLE_OK;
1894
0
  *stream_errorp = FALSE;
1895
1896
  /* check if over send speed */
1897
0
  if(data->set.max_send_speed)
1898
0
    send_timeout_ms = Curl_pgrsLimitWaitTime(&data->progress.ul,
1899
0
                                             data->set.max_send_speed,
1900
0
                                             *nowp);
1901
1902
  /* check if over recv speed */
1903
0
  if(data->set.max_recv_speed)
1904
0
    recv_timeout_ms = Curl_pgrsLimitWaitTime(&data->progress.dl,
1905
0
                                             data->set.max_recv_speed,
1906
0
                                             *nowp);
1907
1908
0
  if(send_timeout_ms || recv_timeout_ms) {
1909
0
    Curl_ratelimit(data, *nowp);
1910
0
    multistate(data, MSTATE_RATELIMITING);
1911
0
    if(send_timeout_ms >= recv_timeout_ms)
1912
0
      Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST);
1913
0
    else
1914
0
      Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST);
1915
0
    return CURLM_OK;
1916
0
  }
1917
1918
  /* read/write data if it is ready to do so */
1919
0
  result = Curl_sendrecv(data, nowp);
1920
1921
0
  if(data->req.done || (result == CURLE_RECV_ERROR)) {
1922
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
1923
     * condition and the server closed the reused connection exactly when we
1924
     * wanted to use it, so figure out if that is indeed the case.
1925
     */
1926
0
    CURLcode ret = Curl_retry_request(data, &newurl);
1927
0
    if(!ret)
1928
0
      retry = !!newurl;
1929
0
    else if(!result)
1930
0
      result = ret;
1931
1932
0
    if(retry) {
1933
      /* if we are to retry, set the result to OK and consider the
1934
         request as done */
1935
0
      result = CURLE_OK;
1936
0
      data->req.done = TRUE;
1937
0
    }
1938
0
  }
1939
0
#ifndef CURL_DISABLE_HTTP
1940
0
  else if((CURLE_HTTP2_STREAM == result) &&
1941
0
          Curl_h2_http_1_1_error(data)) {
1942
0
    CURLcode ret = Curl_retry_request(data, &newurl);
1943
1944
0
    if(!ret) {
1945
0
      infof(data, "Downgrades to HTTP/1.1");
1946
0
      streamclose(data->conn, "Disconnect HTTP/2 for HTTP/1");
1947
0
      data->state.http_neg.wanted = CURL_HTTP_V1x;
1948
0
      data->state.http_neg.allowed = CURL_HTTP_V1x;
1949
      /* clear the error message bit too as we ignore the one we got */
1950
0
      data->state.errorbuf = FALSE;
1951
0
      if(!newurl)
1952
        /* typically for HTTP_1_1_REQUIRED error on first flight */
1953
0
        newurl = strdup(data->state.url);
1954
      /* if we are to retry, set the result to OK and consider the request
1955
         as done */
1956
0
      retry = TRUE;
1957
0
      result = CURLE_OK;
1958
0
      data->req.done = TRUE;
1959
0
    }
1960
0
    else
1961
0
      result = ret;
1962
0
  }
1963
0
#endif
1964
1965
0
  if(result) {
1966
    /*
1967
     * The transfer phase returned error, we mark the connection to get closed
1968
     * to prevent being reused. This is because we cannot possibly know if the
1969
     * connection is in a good shape or not now. Unless it is a protocol which
1970
     * uses two "channels" like FTP, as then the error happened in the data
1971
     * connection.
1972
     */
1973
1974
0
    if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
1975
0
       result != CURLE_HTTP2_STREAM)
1976
0
      streamclose(data->conn, "Transfer returned error");
1977
1978
0
    multi_posttransfer(data);
1979
0
    multi_done(data, result, TRUE);
1980
0
  }
1981
0
  else if(data->req.done && !Curl_cwriter_is_paused(data)) {
1982
0
    const struct Curl_handler *handler = data->conn->handler;
1983
1984
    /* call this even if the readwrite function returned error */
1985
0
    multi_posttransfer(data);
1986
1987
    /* When we follow redirects or is set to retry the connection, we must to
1988
       go back to the CONNECT state */
1989
0
    if(data->req.newurl || retry) {
1990
0
      followtype follow = FOLLOW_NONE;
1991
0
      if(!retry) {
1992
        /* if the URL is a follow-location and not just a retried request then
1993
           figure out the URL here */
1994
0
        free(newurl);
1995
0
        newurl = data->req.newurl;
1996
0
        data->req.newurl = NULL;
1997
0
        follow = FOLLOW_REDIR;
1998
0
      }
1999
0
      else
2000
0
        follow = FOLLOW_RETRY;
2001
0
      (void)multi_done(data, CURLE_OK, FALSE);
2002
      /* multi_done() might return CURLE_GOT_NOTHING */
2003
0
      result = multi_follow(data, handler, newurl, follow);
2004
0
      if(!result) {
2005
0
        multistate(data, MSTATE_SETUP);
2006
0
        rc = CURLM_CALL_MULTI_PERFORM;
2007
0
      }
2008
0
    }
2009
0
    else {
2010
      /* after the transfer is done, go DONE */
2011
2012
      /* but first check to see if we got a location info even though we are
2013
         not following redirects */
2014
0
      if(data->req.location) {
2015
0
        free(newurl);
2016
0
        newurl = data->req.location;
2017
0
        data->req.location = NULL;
2018
0
        result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
2019
0
        if(result) {
2020
0
          *stream_errorp = TRUE;
2021
0
          result = multi_done(data, result, TRUE);
2022
0
        }
2023
0
      }
2024
2025
0
      if(!result) {
2026
0
        multistate(data, MSTATE_DONE);
2027
0
        rc = CURLM_CALL_MULTI_PERFORM;
2028
0
      }
2029
0
    }
2030
0
  }
2031
0
  free(newurl);
2032
0
  *resultp = result;
2033
0
  return rc;
2034
0
}
2035
2036
static CURLMcode state_do(struct Curl_easy *data,
2037
                          bool *stream_errorp,
2038
                          CURLcode *resultp)
2039
0
{
2040
0
  CURLMcode rc = CURLM_OK;
2041
0
  CURLcode result = CURLE_OK;
2042
0
  if(data->set.fprereq) {
2043
0
    int prereq_rc;
2044
2045
    /* call the prerequest callback function */
2046
0
    Curl_set_in_callback(data, TRUE);
2047
0
    prereq_rc = data->set.fprereq(data->set.prereq_userp,
2048
0
                                  data->info.primary.remote_ip,
2049
0
                                  data->info.primary.local_ip,
2050
0
                                  data->info.primary.remote_port,
2051
0
                                  data->info.primary.local_port);
2052
0
    Curl_set_in_callback(data, FALSE);
2053
0
    if(prereq_rc != CURL_PREREQFUNC_OK) {
2054
0
      failf(data, "operation aborted by pre-request callback");
2055
      /* failure in pre-request callback - do not do any other processing */
2056
0
      result = CURLE_ABORTED_BY_CALLBACK;
2057
0
      multi_posttransfer(data);
2058
0
      multi_done(data, result, FALSE);
2059
0
      *stream_errorp = TRUE;
2060
0
      goto end;
2061
0
    }
2062
0
  }
2063
2064
0
  if(data->set.connect_only && !data->set.connect_only_ws) {
2065
    /* keep connection open for application to use the socket */
2066
0
    connkeep(data->conn, "CONNECT_ONLY");
2067
0
    multistate(data, MSTATE_DONE);
2068
0
    rc = CURLM_CALL_MULTI_PERFORM;
2069
0
  }
2070
0
  else {
2071
0
    bool dophase_done = FALSE;
2072
    /* Perform the protocol's DO action */
2073
0
    result = multi_do(data, &dophase_done);
2074
2075
    /* When multi_do() returns failure, data->conn might be NULL! */
2076
2077
0
    if(!result) {
2078
0
      if(!dophase_done) {
2079
0
#ifndef CURL_DISABLE_FTP
2080
        /* some steps needed for wildcard matching */
2081
0
        if(data->state.wildcardmatch) {
2082
0
          struct WildcardData *wc = data->wildcard;
2083
0
          if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) {
2084
            /* skip some states if it is important */
2085
0
            multi_done(data, CURLE_OK, FALSE);
2086
2087
            /* if there is no connection left, skip the DONE state */
2088
0
            multistate(data, data->conn ?
2089
0
                       MSTATE_DONE : MSTATE_COMPLETED);
2090
0
            rc = CURLM_CALL_MULTI_PERFORM;
2091
0
            goto end;
2092
0
          }
2093
0
        }
2094
0
#endif
2095
        /* DO was not completed in one function call, we must continue
2096
           DOING... */
2097
0
        multistate(data, MSTATE_DOING);
2098
0
        rc = CURLM_CALL_MULTI_PERFORM;
2099
0
      }
2100
2101
      /* after DO, go DO_DONE... or DO_MORE */
2102
0
      else if(data->conn->bits.do_more) {
2103
        /* we are supposed to do more, but we need to sit down, relax and wait
2104
           a little while first */
2105
0
        multistate(data, MSTATE_DOING_MORE);
2106
0
        rc = CURLM_CALL_MULTI_PERFORM;
2107
0
      }
2108
0
      else {
2109
        /* we are done with the DO, now DID */
2110
0
        multistate(data, MSTATE_DID);
2111
0
        rc = CURLM_CALL_MULTI_PERFORM;
2112
0
      }
2113
0
    }
2114
0
    else if((CURLE_SEND_ERROR == result) &&
2115
0
            data->conn->bits.reuse) {
2116
      /*
2117
       * In this situation, a connection that we were trying to use may have
2118
       * unexpectedly died. If possible, send the connection back to the
2119
       * CONNECT phase so we can try again.
2120
       */
2121
0
      const struct Curl_handler *handler = data->conn->handler;
2122
0
      char *newurl = NULL;
2123
0
      followtype follow = FOLLOW_NONE;
2124
0
      CURLcode drc;
2125
2126
0
      drc = Curl_retry_request(data, &newurl);
2127
0
      if(drc) {
2128
        /* a failure here pretty much implies an out of memory */
2129
0
        result = drc;
2130
0
        *stream_errorp = TRUE;
2131
0
      }
2132
2133
0
      multi_posttransfer(data);
2134
0
      drc = multi_done(data, result, FALSE);
2135
2136
      /* When set to retry the connection, we must go back to the CONNECT
2137
       * state */
2138
0
      if(newurl) {
2139
0
        if(!drc || (drc == CURLE_SEND_ERROR)) {
2140
0
          follow = FOLLOW_RETRY;
2141
0
          drc = multi_follow(data, handler, newurl, follow);
2142
0
          if(!drc) {
2143
0
            multistate(data, MSTATE_SETUP);
2144
0
            rc = CURLM_CALL_MULTI_PERFORM;
2145
0
            result = CURLE_OK;
2146
0
          }
2147
0
          else {
2148
            /* Follow failed */
2149
0
            result = drc;
2150
0
          }
2151
0
        }
2152
0
        else {
2153
          /* done did not return OK or SEND_ERROR */
2154
0
          result = drc;
2155
0
        }
2156
0
      }
2157
0
      else {
2158
        /* Have error handler disconnect conn if we cannot retry */
2159
0
        *stream_errorp = TRUE;
2160
0
      }
2161
0
      free(newurl);
2162
0
    }
2163
0
    else {
2164
      /* failure detected */
2165
0
      multi_posttransfer(data);
2166
0
      if(data->conn)
2167
0
        multi_done(data, result, FALSE);
2168
0
      *stream_errorp = TRUE;
2169
0
    }
2170
0
  }
2171
0
end:
2172
0
  *resultp = result;
2173
0
  return rc;
2174
0
}
2175
2176
static CURLMcode state_ratelimiting(struct Curl_easy *data,
2177
                                    struct curltime *nowp,
2178
                                    CURLcode *resultp)
2179
0
{
2180
0
  CURLcode result = CURLE_OK;
2181
0
  CURLMcode rc = CURLM_OK;
2182
0
  DEBUGASSERT(data->conn);
2183
  /* if both rates are within spec, resume transfer */
2184
0
  if(Curl_pgrsUpdate(data))
2185
0
    result = CURLE_ABORTED_BY_CALLBACK;
2186
0
  else
2187
0
    result = Curl_speedcheck(data, *nowp);
2188
2189
0
  if(result) {
2190
0
    if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
2191
0
       result != CURLE_HTTP2_STREAM)
2192
0
      streamclose(data->conn, "Transfer returned error");
2193
2194
0
    multi_posttransfer(data);
2195
0
    multi_done(data, result, TRUE);
2196
0
  }
2197
0
  else {
2198
0
    timediff_t recv_timeout_ms = 0;
2199
0
    timediff_t send_timeout_ms = 0;
2200
0
    if(data->set.max_send_speed)
2201
0
      send_timeout_ms =
2202
0
        Curl_pgrsLimitWaitTime(&data->progress.ul,
2203
0
                               data->set.max_send_speed,
2204
0
                               *nowp);
2205
2206
0
    if(data->set.max_recv_speed)
2207
0
      recv_timeout_ms =
2208
0
        Curl_pgrsLimitWaitTime(&data->progress.dl,
2209
0
                               data->set.max_recv_speed,
2210
0
                               *nowp);
2211
2212
0
    if(!send_timeout_ms && !recv_timeout_ms) {
2213
0
      multistate(data, MSTATE_PERFORMING);
2214
0
      Curl_ratelimit(data, *nowp);
2215
      /* start performing again right away */
2216
0
      rc = CURLM_CALL_MULTI_PERFORM;
2217
0
    }
2218
0
    else if(send_timeout_ms >= recv_timeout_ms)
2219
0
      Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST);
2220
0
    else
2221
0
      Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST);
2222
0
  }
2223
0
  *resultp = result;
2224
0
  return rc;
2225
0
}
2226
2227
static CURLMcode state_resolving(struct Curl_multi *multi,
2228
                                 struct Curl_easy *data,
2229
                                 bool *stream_errorp,
2230
                                 CURLcode *resultp)
2231
0
{
2232
0
  struct Curl_dns_entry *dns = NULL;
2233
0
  CURLcode result;
2234
0
  CURLMcode rc = CURLM_OK;
2235
2236
0
  result = Curl_resolv_check(data, &dns);
2237
0
  CURL_TRC_DNS(data, "Curl_resolv_check() -> %d, %s",
2238
0
               result, dns ? "found" : "missing");
2239
  /* Update sockets here, because the socket(s) may have been closed and the
2240
     application thus needs to be told, even if it is likely that the same
2241
     socket(s) will again be used further down. If the name has not yet been
2242
     resolved, it is likely that new sockets have been opened in an attempt to
2243
     contact another resolver. */
2244
0
  rc = Curl_multi_ev_assess_xfer(multi, data);
2245
0
  if(rc)
2246
0
    return rc;
2247
2248
0
  if(dns) {
2249
0
    bool connected;
2250
    /* Perform the next step in the connection phase, and then move on to the
2251
       WAITCONNECT state */
2252
0
    result = Curl_once_resolved(data, dns, &connected);
2253
2254
0
    if(result)
2255
      /* if Curl_once_resolved() returns failure, the connection struct is
2256
         already freed and gone */
2257
0
      data->conn = NULL; /* no more connection */
2258
0
    else {
2259
      /* call again please so that we get the next socket setup */
2260
0
      rc = CURLM_CALL_MULTI_PERFORM;
2261
0
      if(connected)
2262
0
        multistate(data, MSTATE_PROTOCONNECT);
2263
0
      else {
2264
0
        multistate(data, MSTATE_CONNECTING);
2265
0
      }
2266
0
    }
2267
0
  }
2268
2269
0
  if(result)
2270
    /* failure detected */
2271
0
    *stream_errorp = TRUE;
2272
2273
0
  *resultp = result;
2274
0
  return rc;
2275
0
}
2276
2277
static CURLMcode state_connect(struct Curl_multi *multi,
2278
                               struct Curl_easy *data,
2279
                               struct curltime *nowp,
2280
                               CURLcode *resultp)
2281
0
{
2282
  /* Connect. We want to get a connection identifier filled in. This state can
2283
     be entered from SETUP and from PENDING. */
2284
0
  bool connected;
2285
0
  bool async;
2286
0
  CURLMcode rc = CURLM_OK;
2287
0
  CURLcode result = Curl_connect(data, &async, &connected);
2288
0
  if(CURLE_NO_CONNECTION_AVAILABLE == result) {
2289
    /* There was no connection available. We will go to the pending state and
2290
       wait for an available connection. */
2291
0
    multistate(data, MSTATE_PENDING);
2292
    /* move from process to pending set */
2293
0
    Curl_uint_bset_remove(&multi->process, data->mid);
2294
0
    Curl_uint_bset_remove(&multi->dirty, data->mid);
2295
0
    Curl_uint_bset_add(&multi->pending, data->mid);
2296
0
    *resultp = CURLE_OK;
2297
0
    return rc;
2298
0
  }
2299
0
  else
2300
0
    process_pending_handles(data->multi);
2301
2302
0
  if(!result) {
2303
0
    *nowp = Curl_pgrsTime(data, TIMER_POSTQUEUE);
2304
0
    if(async)
2305
      /* We are now waiting for an asynchronous name lookup */
2306
0
      multistate(data, MSTATE_RESOLVING);
2307
0
    else {
2308
      /* after the connect has been sent off, go WAITCONNECT unless the
2309
         protocol connect is already done and we can go directly to WAITDO or
2310
         DO! */
2311
0
      rc = CURLM_CALL_MULTI_PERFORM;
2312
2313
0
      if(connected) {
2314
0
        if(!data->conn->bits.reuse &&
2315
0
           Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
2316
          /* new connection, can multiplex, wake pending handles */
2317
0
          process_pending_handles(data->multi);
2318
0
        }
2319
0
        multistate(data, MSTATE_PROTOCONNECT);
2320
0
      }
2321
0
      else {
2322
0
        multistate(data, MSTATE_CONNECTING);
2323
0
      }
2324
0
    }
2325
0
  }
2326
0
  *resultp = result;
2327
0
  return rc;
2328
0
}
2329
2330
static CURLMcode multi_runsingle(struct Curl_multi *multi,
2331
                                 struct curltime *nowp,
2332
                                 struct Curl_easy *data)
2333
0
{
2334
0
  struct Curl_message *msg = NULL;
2335
0
  bool connected;
2336
0
  bool protocol_connected = FALSE;
2337
0
  bool dophase_done = FALSE;
2338
0
  CURLMcode rc;
2339
0
  CURLcode result = CURLE_OK;
2340
0
  int control;
2341
2342
0
  if(!GOOD_EASY_HANDLE(data))
2343
0
    return CURLM_BAD_EASY_HANDLE;
2344
2345
0
  if(multi->dead) {
2346
    /* a multi-level callback returned error before, meaning every individual
2347
     transfer now has failed */
2348
0
    result = CURLE_ABORTED_BY_CALLBACK;
2349
0
    multi_posttransfer(data);
2350
0
    multi_done(data, result, FALSE);
2351
0
    multistate(data, MSTATE_COMPLETED);
2352
0
  }
2353
2354
0
  multi_warn_debug(multi, data);
2355
2356
  /* transfer runs now, clear the dirty bit. This may be set
2357
   * again during processing, triggering a re-run later. */
2358
0
  Curl_uint_bset_remove(&multi->dirty, data->mid);
2359
2360
0
  do {
2361
    /* A "stream" here is a logical stream if the protocol can handle that
2362
       (HTTP/2), or the full connection for older protocols */
2363
0
    bool stream_error = FALSE;
2364
0
    rc = CURLM_OK;
2365
2366
0
    if(multi_ischanged(multi, TRUE)) {
2367
0
      CURL_TRC_M(data, "multi changed, check CONNECT_PEND queue");
2368
0
      process_pending_handles(multi); /* multiplexed */
2369
0
    }
2370
2371
0
    if(data->mstate > MSTATE_CONNECT &&
2372
0
       data->mstate < MSTATE_COMPLETED) {
2373
      /* Make sure we set the connection's current owner */
2374
0
      DEBUGASSERT(data->conn);
2375
0
      if(!data->conn)
2376
0
        return CURLM_INTERNAL_ERROR;
2377
0
    }
2378
2379
    /* Wait for the connect state as only then is the start time stored, but
2380
       we must not check already completed handles */
2381
0
    if((data->mstate >= MSTATE_CONNECT) && (data->mstate < MSTATE_COMPLETED) &&
2382
0
       multi_handle_timeout(data, nowp, &stream_error, &result))
2383
      /* Skip the statemachine and go directly to error handling section. */
2384
0
      goto statemachine_end;
2385
2386
0
    switch(data->mstate) {
2387
0
    case MSTATE_INIT:
2388
      /* Transitional state. init this transfer. A handle never comes back to
2389
         this state. */
2390
0
      result = Curl_pretransfer(data);
2391
0
      if(result)
2392
0
        break;
2393
2394
      /* after init, go SETUP */
2395
0
      multistate(data, MSTATE_SETUP);
2396
0
      (void)Curl_pgrsTime(data, TIMER_STARTOP);
2397
0
      FALLTHROUGH();
2398
2399
0
    case MSTATE_SETUP:
2400
      /* Transitional state. Setup things for a new transfer. The handle
2401
         can come back to this state on a redirect. */
2402
0
      *nowp = Curl_pgrsTime(data, TIMER_STARTSINGLE);
2403
0
      if(data->set.timeout)
2404
0
        Curl_expire(data, data->set.timeout, EXPIRE_TIMEOUT);
2405
0
      if(data->set.connecttimeout)
2406
        /* Since a connection might go to pending and back to CONNECT several
2407
           times before it actually takes off, we need to set the timeout once
2408
           in SETUP before we enter CONNECT the first time. */
2409
0
        Curl_expire(data, data->set.connecttimeout, EXPIRE_CONNECTTIMEOUT);
2410
2411
0
      multistate(data, MSTATE_CONNECT);
2412
0
      FALLTHROUGH();
2413
2414
0
    case MSTATE_CONNECT:
2415
0
      rc = state_connect(multi, data, nowp, &result);
2416
0
      break;
2417
2418
0
    case MSTATE_RESOLVING:
2419
      /* awaiting an asynch name resolve to complete */
2420
0
      rc = state_resolving(multi, data, &stream_error, &result);
2421
0
      break;
2422
2423
0
#ifndef CURL_DISABLE_HTTP
2424
0
    case MSTATE_TUNNELING:
2425
      /* this is HTTP-specific, but sending CONNECT to a proxy is HTTP... */
2426
0
      DEBUGASSERT(data->conn);
2427
0
      result = Curl_http_connect(data, &protocol_connected);
2428
0
      if(!result) {
2429
0
        rc = CURLM_CALL_MULTI_PERFORM;
2430
        /* initiate protocol connect phase */
2431
0
        multistate(data, MSTATE_PROTOCONNECT);
2432
0
      }
2433
0
      else
2434
0
        stream_error = TRUE;
2435
0
      break;
2436
0
#endif
2437
2438
0
    case MSTATE_CONNECTING:
2439
      /* awaiting a completion of an asynch TCP connect */
2440
0
      DEBUGASSERT(data->conn);
2441
0
      if(!Curl_xfer_recv_is_paused(data)) {
2442
0
        result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &connected);
2443
0
        if(connected && !result) {
2444
0
          if(!data->conn->bits.reuse &&
2445
0
             Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
2446
            /* new connection, can multiplex, wake pending handles */
2447
0
            process_pending_handles(data->multi);
2448
0
          }
2449
0
          rc = CURLM_CALL_MULTI_PERFORM;
2450
0
          multistate(data, MSTATE_PROTOCONNECT);
2451
0
        }
2452
0
        else if(result) {
2453
          /* failure detected */
2454
0
          multi_posttransfer(data);
2455
0
          multi_done(data, result, TRUE);
2456
0
          stream_error = TRUE;
2457
0
          break;
2458
0
        }
2459
0
      }
2460
0
      break;
2461
2462
0
    case MSTATE_PROTOCONNECT:
2463
0
      if(!result && data->conn->bits.reuse) {
2464
        /* ftp seems to hang when protoconnect on reused connection since we
2465
         * handle PROTOCONNECT in general inside the filers, it seems wrong to
2466
         * restart this on a reused connection.
2467
         */
2468
0
        multistate(data, MSTATE_DO);
2469
0
        rc = CURLM_CALL_MULTI_PERFORM;
2470
0
        break;
2471
0
      }
2472
0
      if(!result)
2473
0
        result = protocol_connect(data, &protocol_connected);
2474
0
      if(!result && !protocol_connected) {
2475
        /* switch to waiting state */
2476
0
        multistate(data, MSTATE_PROTOCONNECTING);
2477
0
        rc = CURLM_CALL_MULTI_PERFORM;
2478
0
      }
2479
0
      else if(!result) {
2480
        /* protocol connect has completed, go WAITDO or DO */
2481
0
        multistate(data, MSTATE_DO);
2482
0
        rc = CURLM_CALL_MULTI_PERFORM;
2483
0
      }
2484
0
      else {
2485
        /* failure detected */
2486
0
        multi_posttransfer(data);
2487
0
        multi_done(data, result, TRUE);
2488
0
        stream_error = TRUE;
2489
0
      }
2490
0
      break;
2491
2492
0
    case MSTATE_PROTOCONNECTING:
2493
      /* protocol-specific connect phase */
2494
0
      result = protocol_connecting(data, &protocol_connected);
2495
0
      if(!result && protocol_connected) {
2496
        /* after the connect has completed, go WAITDO or DO */
2497
0
        multistate(data, MSTATE_DO);
2498
0
        rc = CURLM_CALL_MULTI_PERFORM;
2499
0
      }
2500
0
      else if(result) {
2501
        /* failure detected */
2502
0
        multi_posttransfer(data);
2503
0
        multi_done(data, result, TRUE);
2504
0
        stream_error = TRUE;
2505
0
      }
2506
0
      break;
2507
2508
0
    case MSTATE_DO:
2509
0
      rc = state_do(data, &stream_error, &result);
2510
0
      break;
2511
2512
0
    case MSTATE_DOING:
2513
      /* we continue DOING until the DO phase is complete */
2514
0
      DEBUGASSERT(data->conn);
2515
0
      result = protocol_doing(data, &dophase_done);
2516
0
      if(!result) {
2517
0
        if(dophase_done) {
2518
          /* after DO, go DO_DONE or DO_MORE */
2519
0
          multistate(data, data->conn->bits.do_more ?
2520
0
                     MSTATE_DOING_MORE : MSTATE_DID);
2521
0
          rc = CURLM_CALL_MULTI_PERFORM;
2522
0
        } /* dophase_done */
2523
0
      }
2524
0
      else {
2525
        /* failure detected */
2526
0
        multi_posttransfer(data);
2527
0
        multi_done(data, result, FALSE);
2528
0
        stream_error = TRUE;
2529
0
      }
2530
0
      break;
2531
2532
0
    case MSTATE_DOING_MORE:
2533
      /*
2534
       * When we are connected, DOING MORE and then go DID
2535
       */
2536
0
      DEBUGASSERT(data->conn);
2537
0
      result = multi_do_more(data, &control);
2538
2539
0
      if(!result) {
2540
0
        if(control) {
2541
          /* if positive, advance to DO_DONE
2542
             if negative, go back to DOING */
2543
0
          multistate(data, control == 1 ?
2544
0
                     MSTATE_DID : MSTATE_DOING);
2545
0
          rc = CURLM_CALL_MULTI_PERFORM;
2546
0
        }
2547
        /* else
2548
           stay in DO_MORE */
2549
0
      }
2550
0
      else {
2551
        /* failure detected */
2552
0
        multi_posttransfer(data);
2553
0
        multi_done(data, result, FALSE);
2554
0
        stream_error = TRUE;
2555
0
      }
2556
0
      break;
2557
2558
0
    case MSTATE_DID:
2559
0
      DEBUGASSERT(data->conn);
2560
0
      if(data->conn->bits.multiplex)
2561
        /* Check if we can move pending requests to send pipe */
2562
0
        process_pending_handles(multi); /*  multiplexed */
2563
2564
      /* Only perform the transfer if there is a good socket to work with.
2565
         Having both BAD is a signal to skip immediately to DONE */
2566
0
      if(CONN_SOCK_IDX_VALID(data->conn->recv_idx) ||
2567
0
         CONN_SOCK_IDX_VALID(data->conn->send_idx))
2568
0
        multistate(data, MSTATE_PERFORMING);
2569
0
      else {
2570
0
#ifndef CURL_DISABLE_FTP
2571
0
        if(data->state.wildcardmatch &&
2572
0
           ((data->conn->handler->flags & PROTOPT_WILDCARD) == 0)) {
2573
0
          data->wildcard->state = CURLWC_DONE;
2574
0
        }
2575
0
#endif
2576
0
        multistate(data, MSTATE_DONE);
2577
0
      }
2578
0
      rc = CURLM_CALL_MULTI_PERFORM;
2579
0
      break;
2580
2581
0
    case MSTATE_RATELIMITING: /* limit-rate exceeded in either direction */
2582
0
      rc = state_ratelimiting(data, nowp, &result);
2583
0
      break;
2584
2585
0
    case MSTATE_PERFORMING:
2586
0
      rc = state_performing(data, nowp, &stream_error, &result);
2587
0
      break;
2588
2589
0
    case MSTATE_DONE:
2590
      /* this state is highly transient, so run another loop after this */
2591
0
      rc = CURLM_CALL_MULTI_PERFORM;
2592
2593
0
      if(data->conn) {
2594
0
        CURLcode res;
2595
2596
        /* post-transfer command */
2597
0
        res = multi_done(data, result, FALSE);
2598
2599
        /* allow a previously set error code take precedence */
2600
0
        if(!result)
2601
0
          result = res;
2602
0
      }
2603
2604
0
#ifndef CURL_DISABLE_FTP
2605
0
      if(data->state.wildcardmatch) {
2606
0
        if(data->wildcard->state != CURLWC_DONE) {
2607
          /* if a wildcard is set and we are not ending -> lets start again
2608
             with MSTATE_INIT */
2609
0
          multistate(data, MSTATE_INIT);
2610
0
          break;
2611
0
        }
2612
0
      }
2613
0
#endif
2614
      /* after we have DONE what we are supposed to do, go COMPLETED, and
2615
         it does not matter what the multi_done() returned! */
2616
0
      multistate(data, MSTATE_COMPLETED);
2617
0
      break;
2618
2619
0
    case MSTATE_COMPLETED:
2620
0
      break;
2621
2622
0
    case MSTATE_PENDING:
2623
0
    case MSTATE_MSGSENT:
2624
      /* handles in these states should NOT be in this list */
2625
0
      break;
2626
2627
0
    default:
2628
0
      return CURLM_INTERNAL_ERROR;
2629
0
    }
2630
2631
0
    if(data->mstate >= MSTATE_CONNECT &&
2632
0
       data->mstate < MSTATE_DO &&
2633
0
       rc != CURLM_CALL_MULTI_PERFORM &&
2634
0
       !multi_ischanged(multi, FALSE)) {
2635
      /* We now handle stream timeouts if and only if this will be the last
2636
       * loop iteration. We only check this on the last iteration to ensure
2637
       * that if we know we have additional work to do immediately
2638
       * (i.e. CURLM_CALL_MULTI_PERFORM == TRUE) then we should do that before
2639
       * declaring the connection timed out as we may almost have a completed
2640
       * connection. */
2641
0
      multi_handle_timeout(data, nowp, &stream_error, &result);
2642
0
    }
2643
2644
0
statemachine_end:
2645
2646
0
    if(data->mstate < MSTATE_COMPLETED) {
2647
0
      if(result) {
2648
        /*
2649
         * If an error was returned, and we are not in completed state now,
2650
         * then we go to completed and consider this transfer aborted.
2651
         */
2652
2653
        /* NOTE: no attempt to disconnect connections must be made
2654
           in the case blocks above - cleanup happens only here */
2655
2656
        /* Check if we can move pending requests to send pipe */
2657
0
        process_pending_handles(multi); /* connection */
2658
2659
0
        if(data->conn) {
2660
0
          if(stream_error) {
2661
            /* Do not attempt to send data over a connection that timed out */
2662
0
            bool dead_connection = result == CURLE_OPERATION_TIMEDOUT;
2663
0
            struct connectdata *conn = data->conn;
2664
2665
            /* This is where we make sure that the conn pointer is reset.
2666
               We do not have to do this in every case block above where a
2667
               failure is detected */
2668
0
            Curl_detach_connection(data);
2669
0
            Curl_conn_terminate(data, conn, dead_connection);
2670
0
          }
2671
0
        }
2672
0
        else if(data->mstate == MSTATE_CONNECT) {
2673
          /* Curl_connect() failed */
2674
0
          multi_posttransfer(data);
2675
0
          Curl_pgrsUpdate_nometer(data);
2676
0
        }
2677
2678
0
        multistate(data, MSTATE_COMPLETED);
2679
0
        rc = CURLM_CALL_MULTI_PERFORM;
2680
0
      }
2681
      /* if there is still a connection to use, call the progress function */
2682
0
      else if(data->conn && Curl_pgrsUpdate(data)) {
2683
        /* aborted due to progress callback return code must close the
2684
           connection */
2685
0
        result = CURLE_ABORTED_BY_CALLBACK;
2686
0
        streamclose(data->conn, "Aborted by callback");
2687
2688
        /* if not yet in DONE state, go there, otherwise COMPLETED */
2689
0
        multistate(data, (data->mstate < MSTATE_DONE) ?
2690
0
                   MSTATE_DONE : MSTATE_COMPLETED);
2691
0
        rc = CURLM_CALL_MULTI_PERFORM;
2692
0
      }
2693
0
    }
2694
2695
0
    if(MSTATE_COMPLETED == data->mstate) {
2696
0
      if(data->master_mid != UINT_MAX) {
2697
        /* A sub transfer, not for msgsent to application */
2698
0
        struct Curl_easy *mdata;
2699
2700
0
        CURL_TRC_M(data, "sub xfer done for master %u", data->master_mid);
2701
0
        mdata = Curl_multi_get_easy(multi, data->master_mid);
2702
0
        if(mdata) {
2703
0
          if(mdata->sub_xfer_done)
2704
0
            mdata->sub_xfer_done(mdata, data, result);
2705
0
          else
2706
0
            CURL_TRC_M(data, "master easy %u without sub_xfer_done callback.",
2707
0
                       data->master_mid);
2708
0
        }
2709
0
        else {
2710
0
          CURL_TRC_M(data, "master easy %u already gone.", data->master_mid);
2711
0
        }
2712
0
      }
2713
0
      else {
2714
        /* now fill in the Curl_message with this info */
2715
0
        msg = &data->msg;
2716
2717
0
        msg->extmsg.msg = CURLMSG_DONE;
2718
0
        msg->extmsg.easy_handle = data;
2719
0
        msg->extmsg.data.result = result;
2720
2721
0
        multi_addmsg(multi, msg);
2722
0
        DEBUGASSERT(!data->conn);
2723
0
      }
2724
0
      multistate(data, MSTATE_MSGSENT);
2725
2726
      /* remove from the other sets, add to msgsent */
2727
0
      Curl_uint_bset_remove(&multi->process, data->mid);
2728
0
      Curl_uint_bset_remove(&multi->dirty, data->mid);
2729
0
      Curl_uint_bset_remove(&multi->pending, data->mid);
2730
0
      Curl_uint_bset_add(&multi->msgsent, data->mid);
2731
0
      --multi->xfers_alive;
2732
0
      return CURLM_OK;
2733
0
    }
2734
0
  } while((rc == CURLM_CALL_MULTI_PERFORM) || multi_ischanged(multi, FALSE));
2735
2736
0
  data->result = result;
2737
0
  return rc;
2738
0
}
2739
2740
2741
CURLMcode curl_multi_perform(CURLM *m, int *running_handles)
2742
0
{
2743
0
  CURLMcode returncode = CURLM_OK;
2744
0
  struct Curl_tree *t = NULL;
2745
0
  struct curltime now = curlx_now();
2746
0
  struct Curl_multi *multi = m;
2747
0
  unsigned int mid;
2748
0
  SIGPIPE_VARIABLE(pipe_st);
2749
2750
0
  if(!GOOD_MULTI_HANDLE(multi))
2751
0
    return CURLM_BAD_HANDLE;
2752
2753
0
  if(multi->in_callback)
2754
0
    return CURLM_RECURSIVE_API_CALL;
2755
2756
0
  sigpipe_init(&pipe_st);
2757
0
  if(Curl_uint_bset_first(&multi->process, &mid)) {
2758
0
    CURL_TRC_M(multi->admin, "multi_perform(running=%u)",
2759
0
               Curl_multi_xfers_running(multi));
2760
0
    do {
2761
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
2762
0
      CURLMcode result;
2763
0
      if(!data) {
2764
0
        DEBUGASSERT(0);
2765
0
        Curl_uint_bset_remove(&multi->process, mid);
2766
0
        Curl_uint_bset_remove(&multi->dirty, mid);
2767
0
        continue;
2768
0
      }
2769
0
      if(data != multi->admin) {
2770
        /* admin handle is processed below */
2771
0
        sigpipe_apply(data, &pipe_st);
2772
0
        result = multi_runsingle(multi, &now, data);
2773
0
        if(result)
2774
0
          returncode = result;
2775
0
      }
2776
0
    }
2777
0
    while(Curl_uint_bset_next(&multi->process, mid, &mid));
2778
0
  }
2779
2780
0
  sigpipe_apply(multi->admin, &pipe_st);
2781
0
  Curl_cshutdn_perform(&multi->cshutdn, multi->admin, CURL_SOCKET_TIMEOUT);
2782
0
  sigpipe_restore(&pipe_st);
2783
2784
0
  if(multi_ischanged(m, TRUE))
2785
0
    process_pending_handles(m);
2786
2787
  /*
2788
   * Simply remove all expired timers from the splay since handles are dealt
2789
   * with unconditionally by this function and curl_multi_timeout() requires
2790
   * that already passed/handled expire times are removed from the splay.
2791
   *
2792
   * It is important that the 'now' value is set at the entry of this function
2793
   * and not for the current time as it may have ticked a little while since
2794
   * then and then we risk this loop to remove timers that actually have not
2795
   * been handled!
2796
   */
2797
0
  do {
2798
0
    multi->timetree = Curl_splaygetbest(now, multi->timetree, &t);
2799
0
    if(t) {
2800
      /* the removed may have another timeout in queue */
2801
0
      struct Curl_easy *data = Curl_splayget(t);
2802
0
      (void)add_next_timeout(now, multi, data);
2803
0
      if(data->mstate == MSTATE_PENDING) {
2804
0
        bool stream_unused;
2805
0
        CURLcode result_unused;
2806
0
        if(multi_handle_timeout(data, &now, &stream_unused, &result_unused)) {
2807
0
          infof(data, "PENDING handle timeout");
2808
0
          move_pending_to_connect(multi, data);
2809
0
        }
2810
0
      }
2811
0
    }
2812
0
  } while(t);
2813
2814
0
  if(running_handles) {
2815
0
    unsigned int running = Curl_multi_xfers_running(multi);
2816
0
    *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
2817
0
  }
2818
2819
0
  if(CURLM_OK >= returncode)
2820
0
    returncode = Curl_update_timer(multi);
2821
2822
0
  return returncode;
2823
0
}
2824
2825
CURLMcode curl_multi_cleanup(CURLM *m)
2826
0
{
2827
0
  struct Curl_multi *multi = m;
2828
0
  if(GOOD_MULTI_HANDLE(multi)) {
2829
0
    void *entry;
2830
0
    unsigned int mid;
2831
0
    if(multi->in_callback)
2832
0
      return CURLM_RECURSIVE_API_CALL;
2833
2834
    /* First remove all remaining easy handles,
2835
     * close internal ones. admin handle is special */
2836
0
    if(Curl_uint_tbl_first(&multi->xfers, &mid, &entry)) {
2837
0
      do {
2838
0
        struct Curl_easy *data = entry;
2839
0
        if(!GOOD_EASY_HANDLE(data))
2840
0
          return CURLM_BAD_HANDLE;
2841
2842
#ifdef DEBUGBUILD
2843
        if(mid != data->mid) {
2844
          CURL_TRC_M(data, "multi_cleanup: still present with mid=%u, "
2845
                     "but unexpected data->mid=%u\n", mid, data->mid);
2846
          DEBUGASSERT(0);
2847
        }
2848
#endif
2849
2850
0
        if(data == multi->admin)
2851
0
          continue;
2852
2853
0
        if(!data->state.done && data->conn)
2854
          /* if DONE was never called for this handle */
2855
0
          (void)multi_done(data, CURLE_OK, TRUE);
2856
2857
0
        data->multi = NULL; /* clear the association */
2858
0
        Curl_uint_tbl_remove(&multi->xfers, mid);
2859
0
        data->mid = UINT_MAX;
2860
2861
#ifdef USE_LIBPSL
2862
        if(data->psl == &multi->psl)
2863
          data->psl = NULL;
2864
#endif
2865
0
        if(data->state.internal)
2866
0
          Curl_close(&data);
2867
0
      }
2868
0
      while(Curl_uint_tbl_next(&multi->xfers, mid, &mid, &entry));
2869
0
    }
2870
2871
0
    Curl_cpool_destroy(&multi->cpool);
2872
0
    Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
2873
0
    if(multi->admin) {
2874
0
      CURL_TRC_M(multi->admin, "multi_cleanup, closing admin handle, done");
2875
0
      multi->admin->multi = NULL;
2876
0
      Curl_uint_tbl_remove(&multi->xfers, multi->admin->mid);
2877
0
      Curl_close(&multi->admin);
2878
0
    }
2879
2880
0
    multi->magic = 0; /* not good anymore */
2881
2882
0
    Curl_multi_ev_cleanup(multi);
2883
0
    Curl_hash_destroy(&multi->proto_hash);
2884
0
    Curl_dnscache_destroy(&multi->dnscache);
2885
0
    Curl_psl_destroy(&multi->psl);
2886
0
#ifdef USE_SSL
2887
0
    Curl_ssl_scache_destroy(multi->ssl_scache);
2888
0
#endif
2889
2890
#ifdef USE_WINSOCK
2891
    WSACloseEvent(multi->wsa_event);
2892
#else
2893
0
#ifdef ENABLE_WAKEUP
2894
0
    wakeup_close(multi->wakeup_pair[0]);
2895
#ifndef USE_EVENTFD
2896
    wakeup_close(multi->wakeup_pair[1]);
2897
#endif
2898
0
#endif
2899
0
#endif
2900
2901
0
    multi_xfer_bufs_free(multi);
2902
#ifdef DEBUGBUILD
2903
    if(Curl_uint_tbl_count(&multi->xfers)) {
2904
      multi_xfer_tbl_dump(multi);
2905
      DEBUGASSERT(0);
2906
    }
2907
#endif
2908
0
    Curl_uint_bset_destroy(&multi->process);
2909
0
    Curl_uint_bset_destroy(&multi->dirty);
2910
0
    Curl_uint_bset_destroy(&multi->pending);
2911
0
    Curl_uint_bset_destroy(&multi->msgsent);
2912
0
    Curl_uint_tbl_destroy(&multi->xfers);
2913
0
    free(multi);
2914
2915
0
    return CURLM_OK;
2916
0
  }
2917
0
  return CURLM_BAD_HANDLE;
2918
0
}
2919
2920
/*
2921
 * curl_multi_info_read()
2922
 *
2923
 * This function is the primary way for a multi/multi_socket application to
2924
 * figure out if a transfer has ended. We MUST make this function as fast as
2925
 * possible as it will be polled frequently and we MUST NOT scan any lists in
2926
 * here to figure out things. We must scale fine to thousands of handles and
2927
 * beyond. The current design is fully O(1).
2928
 */
2929
2930
CURLMsg *curl_multi_info_read(CURLM *m, int *msgs_in_queue)
2931
0
{
2932
0
  struct Curl_message *msg;
2933
0
  struct Curl_multi *multi = m;
2934
2935
0
  *msgs_in_queue = 0; /* default to none */
2936
2937
0
  if(GOOD_MULTI_HANDLE(multi) &&
2938
0
     !multi->in_callback &&
2939
0
     Curl_llist_count(&multi->msglist)) {
2940
    /* there is one or more messages in the list */
2941
0
    struct Curl_llist_node *e;
2942
2943
    /* extract the head of the list to return */
2944
0
    e = Curl_llist_head(&multi->msglist);
2945
2946
0
    msg = Curl_node_elem(e);
2947
2948
    /* remove the extracted entry */
2949
0
    Curl_node_remove(e);
2950
2951
0
    *msgs_in_queue = curlx_uztosi(Curl_llist_count(&multi->msglist));
2952
2953
0
    return &msg->extmsg;
2954
0
  }
2955
0
  return NULL;
2956
0
}
2957
2958
2959
void Curl_multi_will_close(struct Curl_easy *data, curl_socket_t s)
2960
0
{
2961
0
  if(data) {
2962
0
    struct Curl_multi *multi = data->multi;
2963
0
    if(multi) {
2964
0
      CURL_TRC_M(data, "Curl_multi_will_close fd=%" FMT_SOCKET_T, s);
2965
0
      Curl_multi_ev_socket_done(multi, data, s);
2966
0
    }
2967
0
  }
2968
0
}
2969
2970
/*
2971
 * add_next_timeout()
2972
 *
2973
 * Each Curl_easy has a list of timeouts. The add_next_timeout() is called
2974
 * when it has just been removed from the splay tree because the timeout has
2975
 * expired. This function is then to advance in the list to pick the next
2976
 * timeout to use (skip the already expired ones) and add this node back to
2977
 * the splay tree again.
2978
 *
2979
 * The splay tree only has each sessionhandle as a single node and the nearest
2980
 * timeout is used to sort it on.
2981
 */
2982
static CURLMcode add_next_timeout(struct curltime now,
2983
                                  struct Curl_multi *multi,
2984
                                  struct Curl_easy *d)
2985
0
{
2986
0
  struct curltime *tv = &d->state.expiretime;
2987
0
  struct Curl_llist *list = &d->state.timeoutlist;
2988
0
  struct Curl_llist_node *e;
2989
2990
  /* move over the timeout list for this specific handle and remove all
2991
     timeouts that are now passed tense and store the next pending
2992
     timeout in *tv */
2993
0
  for(e = Curl_llist_head(list); e;) {
2994
0
    struct Curl_llist_node *n = Curl_node_next(e);
2995
0
    struct time_node *node = Curl_node_elem(e);
2996
0
    timediff_t diff = curlx_timediff_us(node->time, now);
2997
0
    if(diff <= 0)
2998
      /* remove outdated entry */
2999
0
      Curl_node_remove(e);
3000
0
    else
3001
      /* the list is sorted so get out on the first mismatch */
3002
0
      break;
3003
0
    e = n;
3004
0
  }
3005
0
  e = Curl_llist_head(list);
3006
0
  if(!e) {
3007
    /* clear the expire times within the handles that we remove from the
3008
       splay tree */
3009
0
    tv->tv_sec = 0;
3010
0
    tv->tv_usec = 0;
3011
0
  }
3012
0
  else {
3013
0
    struct time_node *node = Curl_node_elem(e);
3014
    /* copy the first entry to 'tv' */
3015
0
    memcpy(tv, &node->time, sizeof(*tv));
3016
3017
    /* Insert this node again into the splay. Keep the timer in the list in
3018
       case we need to recompute future timers. */
3019
0
    multi->timetree = Curl_splayinsert(*tv, multi->timetree,
3020
0
                                       &d->state.timenode);
3021
0
  }
3022
0
  return CURLM_OK;
3023
0
}
3024
3025
struct multi_run_ctx {
3026
  struct Curl_multi *multi;
3027
  struct curltime now;
3028
  size_t run_xfers;
3029
  SIGPIPE_MEMBER(pipe_st);
3030
  bool run_cpool;
3031
};
3032
3033
static void multi_mark_expired_as_dirty(struct multi_run_ctx *mrc)
3034
0
{
3035
0
  struct Curl_multi *multi = mrc->multi;
3036
0
  struct Curl_easy *data = NULL;
3037
0
  struct Curl_tree *t = NULL;
3038
3039
  /*
3040
   * The loop following here will go on as long as there are expire-times left
3041
   * to process (compared to mrc->now) in the splay and 'data' will be
3042
   * re-assigned for every expired handle we deal with.
3043
   */
3044
0
  while(1) {
3045
    /* Check if there is one (more) expired timer to deal with! This function
3046
       extracts a matching node if there is one */
3047
0
    multi->timetree = Curl_splaygetbest(mrc->now, multi->timetree, &t);
3048
0
    if(!t)
3049
0
      return;
3050
3051
0
    data = Curl_splayget(t); /* assign this for next loop */
3052
0
    if(!data)
3053
0
      continue;
3054
3055
0
    (void)add_next_timeout(mrc->now, multi, data);
3056
0
    Curl_multi_mark_dirty(data);
3057
0
  }
3058
0
}
3059
3060
static CURLMcode multi_run_dirty(struct multi_run_ctx *mrc)
3061
0
{
3062
0
  struct Curl_multi *multi = mrc->multi;
3063
0
  CURLMcode result = CURLM_OK;
3064
0
  unsigned int mid;
3065
3066
0
  if(Curl_uint_bset_first(&multi->dirty, &mid)) {
3067
0
    do {
3068
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3069
0
      if(data) {
3070
0
        CURL_TRC_M(data, "multi_run_dirty");
3071
3072
0
        if(data == multi->admin) {
3073
0
          Curl_uint_bset_remove(&multi->dirty, mid);
3074
0
          mrc->run_cpool = TRUE;
3075
0
          continue;
3076
0
        }
3077
0
        else if(!Curl_uint_bset_contains(&multi->process, mid)) {
3078
          /* We are no longer processing this transfer */
3079
0
          Curl_uint_bset_remove(&multi->dirty, mid);
3080
0
          continue;
3081
0
        }
3082
3083
0
        mrc->run_xfers++;
3084
0
        sigpipe_apply(data, &mrc->pipe_st);
3085
        /* runsingle() clears the dirty mid */
3086
0
        result = multi_runsingle(multi, &mrc->now, data);
3087
3088
0
        if(CURLM_OK >= result) {
3089
          /* reassess event handling of data */
3090
0
          result = Curl_multi_ev_assess_xfer(multi, data);
3091
0
          if(result)
3092
0
            goto out;
3093
0
        }
3094
0
      }
3095
0
      else {
3096
0
        CURL_TRC_M(multi->admin, "multi_run_dirty, %u no longer found", mid);
3097
0
        Curl_uint_bset_remove(&multi->dirty, mid);
3098
0
      }
3099
0
    }
3100
0
    while(Curl_uint_bset_next(&multi->dirty, mid, &mid));
3101
0
  }
3102
3103
0
out:
3104
0
  return result;
3105
0
}
3106
3107
static CURLMcode multi_socket(struct Curl_multi *multi,
3108
                              bool checkall,
3109
                              curl_socket_t s,
3110
                              int ev_bitmask,
3111
                              int *running_handles)
3112
0
{
3113
0
  CURLMcode result = CURLM_OK;
3114
0
  struct multi_run_ctx mrc;
3115
3116
0
  (void)ev_bitmask;
3117
0
  memset(&mrc, 0, sizeof(mrc));
3118
0
  mrc.multi = multi;
3119
0
  mrc.now = curlx_now();
3120
0
  sigpipe_init(&mrc.pipe_st);
3121
3122
0
  if(checkall) {
3123
    /* *perform() deals with running_handles on its own */
3124
0
    result = curl_multi_perform(multi, running_handles);
3125
3126
0
    if(result != CURLM_BAD_HANDLE) {
3127
      /* Reassess event status of all active transfers */
3128
0
      result = Curl_multi_ev_assess_xfer_bset(multi, &multi->process);
3129
0
    }
3130
0
    mrc.run_cpool = TRUE;
3131
0
    goto out;
3132
0
  }
3133
3134
0
  if(s != CURL_SOCKET_TIMEOUT) {
3135
    /* Mark all transfers of that socket as dirty */
3136
0
    Curl_multi_ev_dirty_xfers(multi, s, &mrc.run_cpool);
3137
0
  }
3138
0
  else {
3139
    /* Asked to run due to time-out. Clear the 'last_expire_ts' variable to
3140
       force Curl_update_timer() to trigger a callback to the app again even
3141
       if the same timeout is still the one to run after this call. That
3142
       handles the case when the application asks libcurl to run the timeout
3143
       prematurely. */
3144
0
    memset(&multi->last_expire_ts, 0, sizeof(multi->last_expire_ts));
3145
0
    mrc.run_cpool = TRUE;
3146
0
  }
3147
3148
0
  multi_mark_expired_as_dirty(&mrc);
3149
0
  result = multi_run_dirty(&mrc);
3150
0
  if(result)
3151
0
    goto out;
3152
3153
0
  if(mrc.run_xfers) {
3154
    /* Running transfers takes time. With a new timestamp, we might catch
3155
     * other expires which are due now. Instead of telling the application
3156
     * to set a 0 timeout and call us again, we run them here.
3157
     * Do that only once or it might be unfair to transfers on other
3158
     * sockets. */
3159
0
    mrc.now = curlx_now();
3160
0
    multi_mark_expired_as_dirty(&mrc);
3161
0
    result = multi_run_dirty(&mrc);
3162
0
  }
3163
3164
0
out:
3165
0
  if(mrc.run_cpool) {
3166
0
    sigpipe_apply(multi->admin, &mrc.pipe_st);
3167
0
    Curl_cshutdn_perform(&multi->cshutdn, multi->admin, s);
3168
0
  }
3169
0
  sigpipe_restore(&mrc.pipe_st);
3170
3171
0
  if(multi_ischanged(multi, TRUE))
3172
0
    process_pending_handles(multi);
3173
3174
0
  if(running_handles) {
3175
0
    unsigned int running = Curl_multi_xfers_running(multi);
3176
0
    *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
3177
0
  }
3178
3179
0
  if(CURLM_OK >= result)
3180
0
    result = Curl_update_timer(multi);
3181
0
  return result;
3182
0
}
3183
3184
#undef curl_multi_setopt
3185
CURLMcode curl_multi_setopt(CURLM *m,
3186
                            CURLMoption option, ...)
3187
0
{
3188
0
  CURLMcode res = CURLM_OK;
3189
0
  va_list param;
3190
0
  unsigned long uarg;
3191
0
  struct Curl_multi *multi = m;
3192
3193
0
  if(!GOOD_MULTI_HANDLE(multi))
3194
0
    return CURLM_BAD_HANDLE;
3195
3196
0
  if(multi->in_callback)
3197
0
    return CURLM_RECURSIVE_API_CALL;
3198
3199
0
  va_start(param, option);
3200
3201
0
  switch(option) {
3202
0
  case CURLMOPT_SOCKETFUNCTION:
3203
0
    multi->socket_cb = va_arg(param, curl_socket_callback);
3204
0
    break;
3205
0
  case CURLMOPT_SOCKETDATA:
3206
0
    multi->socket_userp = va_arg(param, void *);
3207
0
    break;
3208
0
  case CURLMOPT_PUSHFUNCTION:
3209
0
    multi->push_cb = va_arg(param, curl_push_callback);
3210
0
    break;
3211
0
  case CURLMOPT_PUSHDATA:
3212
0
    multi->push_userp = va_arg(param, void *);
3213
0
    break;
3214
0
  case CURLMOPT_PIPELINING:
3215
0
    multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX ? 1 : 0;
3216
0
    break;
3217
0
  case CURLMOPT_TIMERFUNCTION:
3218
0
    multi->timer_cb = va_arg(param, curl_multi_timer_callback);
3219
0
    break;
3220
0
  case CURLMOPT_TIMERDATA:
3221
0
    multi->timer_userp = va_arg(param, void *);
3222
0
    break;
3223
0
  case CURLMOPT_MAXCONNECTS:
3224
0
    uarg = va_arg(param, unsigned long);
3225
0
    if(uarg <= UINT_MAX)
3226
0
      multi->maxconnects = (unsigned int)uarg;
3227
0
    break;
3228
0
  case CURLMOPT_MAX_HOST_CONNECTIONS:
3229
0
    multi->max_host_connections = va_arg(param, long);
3230
0
    break;
3231
0
  case CURLMOPT_MAX_TOTAL_CONNECTIONS:
3232
0
    multi->max_total_connections = va_arg(param, long);
3233
0
    break;
3234
    /* options formerly used for pipelining */
3235
0
  case CURLMOPT_MAX_PIPELINE_LENGTH:
3236
0
    break;
3237
0
  case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
3238
0
    break;
3239
0
  case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
3240
0
    break;
3241
0
  case CURLMOPT_PIPELINING_SITE_BL:
3242
0
    break;
3243
0
  case CURLMOPT_PIPELINING_SERVER_BL:
3244
0
    break;
3245
0
  case CURLMOPT_MAX_CONCURRENT_STREAMS:
3246
0
    {
3247
0
      long streams = va_arg(param, long);
3248
0
      if((streams < 1) || (streams > INT_MAX))
3249
0
        streams = 100;
3250
0
      multi->max_concurrent_streams = (unsigned int)streams;
3251
0
    }
3252
0
    break;
3253
0
  case CURLMOPT_NETWORK_CHANGED: {
3254
0
    long val = va_arg(param, long);
3255
0
    if(val & CURLMNWC_CLEAR_DNS) {
3256
0
      Curl_dnscache_clear(multi->admin);
3257
0
    }
3258
0
    if(val & CURLMNWC_CLEAR_CONNS) {
3259
0
      Curl_cpool_nw_changed(multi->admin);
3260
0
    }
3261
0
    break;
3262
0
  }
3263
0
  default:
3264
0
    res = CURLM_UNKNOWN_OPTION;
3265
0
    break;
3266
0
  }
3267
0
  va_end(param);
3268
0
  return res;
3269
0
}
3270
3271
/* we define curl_multi_socket() in the public multi.h header */
3272
#undef curl_multi_socket
3273
3274
CURLMcode curl_multi_socket(CURLM *m, curl_socket_t s, int *running_handles)
3275
0
{
3276
0
  struct Curl_multi *multi = m;
3277
0
  if(multi->in_callback)
3278
0
    return CURLM_RECURSIVE_API_CALL;
3279
0
  return multi_socket(multi, FALSE, s, 0, running_handles);
3280
0
}
3281
3282
CURLMcode curl_multi_socket_action(CURLM *m, curl_socket_t s,
3283
                                   int ev_bitmask, int *running_handles)
3284
0
{
3285
0
  struct Curl_multi *multi = m;
3286
0
  if(multi->in_callback)
3287
0
    return CURLM_RECURSIVE_API_CALL;
3288
0
  return multi_socket(multi, FALSE, s, ev_bitmask, running_handles);
3289
0
}
3290
3291
CURLMcode curl_multi_socket_all(CURLM *m, int *running_handles)
3292
0
{
3293
0
  struct Curl_multi *multi = m;
3294
0
  if(multi->in_callback)
3295
0
    return CURLM_RECURSIVE_API_CALL;
3296
0
  return multi_socket(multi, TRUE, CURL_SOCKET_BAD, 0, running_handles);
3297
0
}
3298
3299
3300
static bool multi_has_dirties(struct Curl_multi *multi)
3301
0
{
3302
0
  unsigned int mid;
3303
0
  if(Curl_uint_bset_first(&multi->dirty, &mid)) {
3304
0
    do {
3305
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3306
0
      if(data) {
3307
0
        if(Curl_uint_bset_contains(&multi->process, mid))
3308
0
          return TRUE;
3309
        /* We are no longer processing this transfer */
3310
0
        Curl_uint_bset_remove(&multi->dirty, mid);
3311
0
      }
3312
0
      else {
3313
0
        CURL_TRC_M(multi->admin, "dirty transfer %u no longer found", mid);
3314
0
        Curl_uint_bset_remove(&multi->dirty, mid);
3315
0
      }
3316
0
    }
3317
0
    while(Curl_uint_bset_next(&multi->dirty, mid, &mid));
3318
0
  }
3319
0
  return FALSE;
3320
0
}
3321
3322
static CURLMcode multi_timeout(struct Curl_multi *multi,
3323
                               struct curltime *expire_time,
3324
                               long *timeout_ms)
3325
0
{
3326
0
  static const struct curltime tv_zero = {0, 0};
3327
3328
0
  if(multi->dead) {
3329
0
    *timeout_ms = 0;
3330
0
    return CURLM_OK;
3331
0
  }
3332
3333
0
  if(multi_has_dirties(multi)) {
3334
0
    *expire_time = curlx_now();
3335
0
    *timeout_ms = 0;
3336
0
    return CURLM_OK;
3337
0
  }
3338
0
  else if(multi->timetree) {
3339
    /* we have a tree of expire times */
3340
0
    struct curltime now = curlx_now();
3341
3342
    /* splay the lowest to the bottom */
3343
0
    multi->timetree = Curl_splay(tv_zero, multi->timetree);
3344
    /* this will not return NULL from a non-empty tree, but some compilers
3345
     * are not convinced of that. Analyzers are hard. */
3346
0
    *expire_time = multi->timetree ? multi->timetree->key : tv_zero;
3347
3348
    /* 'multi->timetree' will be non-NULL here but the compilers sometimes
3349
       yell at us if we assume so */
3350
0
    if(multi->timetree &&
3351
0
       curlx_timediff_us(multi->timetree->key, now) > 0) {
3352
      /* some time left before expiration */
3353
0
      timediff_t diff = curlx_timediff_ceil(multi->timetree->key, now);
3354
      /* this should be safe even on 32-bit archs, as we do not use that
3355
         overly long timeouts */
3356
0
      *timeout_ms = (long)diff;
3357
0
    }
3358
0
    else {
3359
0
      if(multi->timetree) {
3360
0
        struct Curl_easy *data = Curl_splayget(multi->timetree);
3361
0
        CURL_TRC_M(data, "multi_timeout() says this has expired");
3362
0
      }
3363
      /* 0 means immediately */
3364
0
      *timeout_ms = 0;
3365
0
    }
3366
0
  }
3367
0
  else {
3368
0
    *expire_time = tv_zero;
3369
0
    *timeout_ms = -1;
3370
0
  }
3371
3372
0
  return CURLM_OK;
3373
0
}
3374
3375
CURLMcode curl_multi_timeout(CURLM *m,
3376
                             long *timeout_ms)
3377
0
{
3378
0
  struct curltime expire_time;
3379
0
  struct Curl_multi *multi = m;
3380
3381
  /* First, make some basic checks that the CURLM handle is a good handle */
3382
0
  if(!GOOD_MULTI_HANDLE(multi))
3383
0
    return CURLM_BAD_HANDLE;
3384
3385
0
  if(multi->in_callback)
3386
0
    return CURLM_RECURSIVE_API_CALL;
3387
3388
0
  return multi_timeout(multi, &expire_time, timeout_ms);
3389
0
}
3390
3391
#define DEBUG_UPDATE_TIMER    0
3392
3393
/*
3394
 * Tell the application it should update its timers, if it subscribes to the
3395
 * update timer callback.
3396
 */
3397
CURLMcode Curl_update_timer(struct Curl_multi *multi)
3398
0
{
3399
0
  struct curltime expire_ts;
3400
0
  long timeout_ms;
3401
0
  int rc;
3402
0
  bool set_value = FALSE;
3403
3404
0
  if(!multi->timer_cb || multi->dead)
3405
0
    return CURLM_OK;
3406
0
  if(multi_timeout(multi, &expire_ts, &timeout_ms)) {
3407
0
    return CURLM_OK;
3408
0
  }
3409
3410
0
  if(timeout_ms < 0 && multi->last_timeout_ms < 0) {
3411
#if DEBUG_UPDATE_TIMER
3412
    fprintf(stderr, "Curl_update_timer(), still no timeout, no change\n");
3413
#endif
3414
0
  }
3415
0
  else if(timeout_ms < 0) {
3416
    /* there is no timeout now but there was one previously */
3417
#if DEBUG_UPDATE_TIMER
3418
    fprintf(stderr, "Curl_update_timer(), remove timeout, "
3419
        " last_timeout=%ldms\n", multi->last_timeout_ms);
3420
#endif
3421
0
    timeout_ms = -1; /* normalize */
3422
0
    set_value = TRUE;
3423
0
  }
3424
0
  else if(multi->last_timeout_ms < 0) {
3425
#if DEBUG_UPDATE_TIMER
3426
    fprintf(stderr, "Curl_update_timer(), had no timeout, set now\n");
3427
#endif
3428
0
    set_value = TRUE;
3429
0
  }
3430
0
  else if(curlx_timediff_us(multi->last_expire_ts, expire_ts)) {
3431
    /* We had a timeout before and have one now, the absolute timestamp
3432
     * differs. The relative timeout_ms may be the same, but the starting
3433
     * point differs. Let the application restart its timer. */
3434
#if DEBUG_UPDATE_TIMER
3435
    fprintf(stderr, "Curl_update_timer(), expire timestamp changed\n");
3436
#endif
3437
0
    set_value = TRUE;
3438
0
  }
3439
0
  else {
3440
    /* We have same expire time as previously. Our relative 'timeout_ms'
3441
     * may be different now, but the application has the timer running
3442
     * and we do not to tell it to start this again. */
3443
#if DEBUG_UPDATE_TIMER
3444
    fprintf(stderr, "Curl_update_timer(), same expire timestamp, no change\n");
3445
#endif
3446
0
  }
3447
3448
0
  if(set_value) {
3449
#if DEBUG_UPDATE_TIMER
3450
    fprintf(stderr, "Curl_update_timer(), set timeout %ldms\n", timeout_ms);
3451
#endif
3452
0
    multi->last_expire_ts = expire_ts;
3453
0
    multi->last_timeout_ms = timeout_ms;
3454
0
    set_in_callback(multi, TRUE);
3455
0
    rc = multi->timer_cb(multi, timeout_ms, multi->timer_userp);
3456
0
    set_in_callback(multi, FALSE);
3457
0
    if(rc == -1) {
3458
0
      multi->dead = TRUE;
3459
0
      return CURLM_ABORTED_BY_CALLBACK;
3460
0
    }
3461
0
  }
3462
0
  return CURLM_OK;
3463
0
}
3464
3465
/*
3466
 * multi_deltimeout()
3467
 *
3468
 * Remove a given timestamp from the list of timeouts.
3469
 */
3470
static void
3471
multi_deltimeout(struct Curl_easy *data, expire_id eid)
3472
0
{
3473
0
  struct Curl_llist_node *e;
3474
0
  struct Curl_llist *timeoutlist = &data->state.timeoutlist;
3475
  /* find and remove the specific node from the list */
3476
0
  for(e = Curl_llist_head(timeoutlist); e; e = Curl_node_next(e)) {
3477
0
    struct time_node *n = Curl_node_elem(e);
3478
0
    if(n->eid == eid) {
3479
0
      Curl_node_remove(e);
3480
0
      return;
3481
0
    }
3482
0
  }
3483
0
}
3484
3485
/*
3486
 * multi_addtimeout()
3487
 *
3488
 * Add a timestamp to the list of timeouts. Keep the list sorted so that head
3489
 * of list is always the timeout nearest in time.
3490
 *
3491
 */
3492
static CURLMcode
3493
multi_addtimeout(struct Curl_easy *data,
3494
                 struct curltime *stamp,
3495
                 expire_id eid)
3496
0
{
3497
0
  struct Curl_llist_node *e;
3498
0
  struct time_node *node;
3499
0
  struct Curl_llist_node *prev = NULL;
3500
0
  size_t n;
3501
0
  struct Curl_llist *timeoutlist = &data->state.timeoutlist;
3502
3503
0
  node = &data->state.expires[eid];
3504
3505
  /* copy the timestamp and id */
3506
0
  memcpy(&node->time, stamp, sizeof(*stamp));
3507
0
  node->eid = eid; /* also marks it as in use */
3508
3509
0
  n = Curl_llist_count(timeoutlist);
3510
0
  if(n) {
3511
    /* find the correct spot in the list */
3512
0
    for(e = Curl_llist_head(timeoutlist); e; e = Curl_node_next(e)) {
3513
0
      struct time_node *check = Curl_node_elem(e);
3514
0
      timediff_t diff = curlx_timediff(check->time, node->time);
3515
0
      if(diff > 0)
3516
0
        break;
3517
0
      prev = e;
3518
0
    }
3519
3520
0
  }
3521
  /* else
3522
     this is the first timeout on the list */
3523
3524
0
  Curl_llist_insert_next(timeoutlist, prev, node, &node->list);
3525
0
  return CURLM_OK;
3526
0
}
3527
3528
void Curl_expire_ex(struct Curl_easy *data,
3529
                    const struct curltime *nowp,
3530
                    timediff_t milli, expire_id id)
3531
0
{
3532
0
  struct Curl_multi *multi = data->multi;
3533
0
  struct curltime *curr_expire = &data->state.expiretime;
3534
0
  struct curltime set;
3535
3536
  /* this is only interesting while there is still an associated multi struct
3537
     remaining! */
3538
0
  if(!multi)
3539
0
    return;
3540
3541
0
  DEBUGASSERT(id < EXPIRE_LAST);
3542
3543
0
  set = *nowp;
3544
0
  set.tv_sec += (time_t)(milli/1000); /* might be a 64 to 32 bits conversion */
3545
0
  set.tv_usec += (int)(milli%1000)*1000;
3546
3547
0
  if(set.tv_usec >= 1000000) {
3548
0
    set.tv_sec++;
3549
0
    set.tv_usec -= 1000000;
3550
0
  }
3551
3552
  /* Remove any timer with the same id just in case. */
3553
0
  multi_deltimeout(data, id);
3554
3555
  /* Add it to the timer list. It must stay in the list until it has expired
3556
     in case we need to recompute the minimum timer later. */
3557
0
  multi_addtimeout(data, &set, id);
3558
3559
0
  if(curr_expire->tv_sec || curr_expire->tv_usec) {
3560
    /* This means that the struct is added as a node in the splay tree.
3561
       Compare if the new time is earlier, and only remove-old/add-new if it
3562
       is. */
3563
0
    timediff_t diff = curlx_timediff(set, *curr_expire);
3564
0
    int rc;
3565
3566
0
    if(diff > 0) {
3567
      /* The current splay tree entry is sooner than this new expiry time.
3568
         We do not need to update our splay tree entry. */
3569
0
      return;
3570
0
    }
3571
3572
    /* Since this is an updated time, we must remove the previous entry from
3573
       the splay tree first and then re-add the new value */
3574
0
    rc = Curl_splayremove(multi->timetree, &data->state.timenode,
3575
0
                          &multi->timetree);
3576
0
    if(rc)
3577
0
      infof(data, "Internal error removing splay node = %d", rc);
3578
0
  }
3579
3580
  /* Indicate that we are in the splay tree and insert the new timer expiry
3581
     value since it is our local minimum. */
3582
0
  *curr_expire = set;
3583
0
  Curl_splayset(&data->state.timenode, data);
3584
0
  multi->timetree = Curl_splayinsert(*curr_expire, multi->timetree,
3585
0
                                     &data->state.timenode);
3586
0
  if(data->id >= 0)
3587
0
    CURL_TRC_M(data, "set expire[%d] in %" FMT_TIMEDIFF_T "ns",
3588
0
               id, curlx_timediff_us(set, *nowp));
3589
0
}
3590
3591
/*
3592
 * Curl_expire()
3593
 *
3594
 * given a number of milliseconds from now to use to set the 'act before
3595
 * this'-time for the transfer, to be extracted by curl_multi_timeout()
3596
 *
3597
 * The timeout will be added to a queue of timeouts if it defines a moment in
3598
 * time that is later than the current head of queue.
3599
 *
3600
 * Expire replaces a former timeout using the same id if already set.
3601
 */
3602
void Curl_expire(struct Curl_easy *data, timediff_t milli, expire_id id)
3603
0
{
3604
0
  struct curltime now = curlx_now();
3605
0
  Curl_expire_ex(data, &now, milli, id);
3606
0
}
3607
3608
/*
3609
 * Curl_expire_done()
3610
 *
3611
 * Removes the expire timer. Marks it as done.
3612
 *
3613
 */
3614
void Curl_expire_done(struct Curl_easy *data, expire_id id)
3615
0
{
3616
  /* remove the timer, if there */
3617
0
  multi_deltimeout(data, id);
3618
0
}
3619
3620
/*
3621
 * Curl_expire_clear()
3622
 *
3623
 * Clear ALL timeout values for this handle.
3624
 */
3625
bool Curl_expire_clear(struct Curl_easy *data)
3626
0
{
3627
0
  struct Curl_multi *multi = data->multi;
3628
0
  struct curltime *nowp = &data->state.expiretime;
3629
3630
  /* this is only interesting while there is still an associated multi struct
3631
     remaining! */
3632
0
  if(!multi)
3633
0
    return FALSE;
3634
3635
0
  if(nowp->tv_sec || nowp->tv_usec) {
3636
    /* Since this is an cleared time, we must remove the previous entry from
3637
       the splay tree */
3638
0
    struct Curl_llist *list = &data->state.timeoutlist;
3639
0
    int rc;
3640
3641
0
    rc = Curl_splayremove(multi->timetree, &data->state.timenode,
3642
0
                          &multi->timetree);
3643
0
    if(rc)
3644
0
      infof(data, "Internal error clearing splay node = %d", rc);
3645
3646
    /* clear the timeout list too */
3647
0
    Curl_llist_destroy(list, NULL);
3648
3649
0
    CURL_TRC_M(data, "Expire cleared");
3650
0
    nowp->tv_sec = 0;
3651
0
    nowp->tv_usec = 0;
3652
0
    return TRUE;
3653
0
  }
3654
0
  return FALSE;
3655
0
}
3656
3657
CURLMcode curl_multi_assign(CURLM *m, curl_socket_t s,
3658
                            void *hashp)
3659
0
{
3660
0
  struct Curl_multi *multi = m;
3661
0
  if(!GOOD_MULTI_HANDLE(multi))
3662
0
    return CURLM_BAD_HANDLE;
3663
3664
0
  return Curl_multi_ev_assign(multi, s, hashp);
3665
0
}
3666
3667
static void move_pending_to_connect(struct Curl_multi *multi,
3668
                                    struct Curl_easy *data)
3669
0
{
3670
0
  DEBUGASSERT(data->mstate == MSTATE_PENDING);
3671
3672
  /* Remove this node from the pending set, add into process set */
3673
0
  Curl_uint_bset_remove(&multi->pending, data->mid);
3674
0
  Curl_uint_bset_add(&multi->process, data->mid);
3675
3676
0
  multistate(data, MSTATE_CONNECT);
3677
0
  Curl_multi_mark_dirty(data); /* make it run */
3678
0
}
3679
3680
/* process_pending_handles() moves a handle from PENDING back into the process
3681
   list and change state to CONNECT.
3682
3683
   We do not move all transfers because that can be a significant amount.
3684
   Since this is tried every now and then doing too many too often becomes a
3685
   performance problem.
3686
3687
   When there is a change for connection limits like max host connections etc,
3688
   this likely only allows one new transfer. When there is a pipewait change,
3689
   it can potentially allow hundreds of new transfers.
3690
3691
   We could consider an improvement where we store the queue reason and allow
3692
   more pipewait rechecks than others.
3693
*/
3694
static void process_pending_handles(struct Curl_multi *multi)
3695
0
{
3696
0
  unsigned int mid;
3697
0
  if(Curl_uint_bset_first(&multi->pending, &mid)) {
3698
0
    do {
3699
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3700
0
      if(data) {
3701
0
        move_pending_to_connect(multi, data);
3702
0
        break;
3703
0
      }
3704
      /* transfer no longer known, should not happen */
3705
0
      Curl_uint_bset_remove(&multi->pending, mid);
3706
0
      DEBUGASSERT(0);
3707
0
    }
3708
0
    while(Curl_uint_bset_next(&multi->pending, mid, &mid));
3709
0
  }
3710
0
}
3711
3712
void Curl_set_in_callback(struct Curl_easy *data, bool value)
3713
0
{
3714
0
  if(data && data->multi)
3715
0
    data->multi->in_callback = value;
3716
0
}
3717
3718
bool Curl_is_in_callback(struct Curl_easy *data)
3719
0
{
3720
0
  return data && data->multi && data->multi->in_callback;
3721
0
}
3722
3723
unsigned int Curl_multi_max_concurrent_streams(struct Curl_multi *multi)
3724
0
{
3725
0
  DEBUGASSERT(multi);
3726
0
  return multi->max_concurrent_streams;
3727
0
}
3728
3729
CURL **curl_multi_get_handles(CURLM *m)
3730
0
{
3731
0
  struct Curl_multi *multi = m;
3732
0
  void *entry;
3733
0
  unsigned int count = Curl_uint_tbl_count(&multi->xfers);
3734
0
  CURL **a = malloc(sizeof(struct Curl_easy *) * (count + 1));
3735
0
  if(a) {
3736
0
    unsigned int i = 0, mid;
3737
3738
0
    if(Curl_uint_tbl_first(&multi->xfers, &mid, &entry)) {
3739
0
      do {
3740
0
        struct Curl_easy *data = entry;
3741
0
        DEBUGASSERT(i < count);
3742
0
        if(!data->state.internal)
3743
0
          a[i++] = data;
3744
0
      }
3745
0
      while(Curl_uint_tbl_next(&multi->xfers, mid, &mid, &entry));
3746
0
    }
3747
0
    a[i] = NULL; /* last entry is a NULL */
3748
0
  }
3749
0
  return a;
3750
0
}
3751
3752
CURLMcode curl_multi_get_offt(CURLM *m,
3753
                              CURLMinfo_offt info,
3754
                              curl_off_t *pvalue)
3755
0
{
3756
0
  struct Curl_multi *multi = m;
3757
3758
0
  if(!GOOD_MULTI_HANDLE(multi))
3759
0
    return CURLM_BAD_HANDLE;
3760
0
  if(!pvalue)
3761
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
3762
3763
0
  switch(info) {
3764
0
  case CURLMINFO_XFERS_CURRENT: {
3765
0
    unsigned int n = Curl_uint_tbl_count(&multi->xfers);
3766
0
    if(n && multi->admin)
3767
0
      --n;
3768
0
    *pvalue = (curl_off_t)n;
3769
0
    return CURLM_OK;
3770
0
  }
3771
0
  case CURLMINFO_XFERS_RUNNING:
3772
0
    *pvalue = (curl_off_t)Curl_uint_bset_count(&multi->process);
3773
0
    return CURLM_OK;
3774
0
  case CURLMINFO_XFERS_PENDING:
3775
0
    *pvalue = (curl_off_t)Curl_uint_bset_count(&multi->pending);
3776
0
    return CURLM_OK;
3777
0
  case CURLMINFO_XFERS_DONE:
3778
0
    *pvalue = (curl_off_t)Curl_uint_bset_count(&multi->msgsent);
3779
0
    return CURLM_OK;
3780
0
  case CURLMINFO_XFERS_ADDED:
3781
0
    *pvalue = multi->xfers_total_ever;
3782
0
    return CURLM_OK;
3783
0
  default:
3784
0
    *pvalue = -1;
3785
0
    return CURLM_UNKNOWN_OPTION;
3786
0
  }
3787
0
}
3788
3789
CURLcode Curl_multi_xfer_buf_borrow(struct Curl_easy *data,
3790
                                    char **pbuf, size_t *pbuflen)
3791
0
{
3792
0
  DEBUGASSERT(data);
3793
0
  DEBUGASSERT(data->multi);
3794
0
  *pbuf = NULL;
3795
0
  *pbuflen = 0;
3796
0
  if(!data->multi) {
3797
0
    failf(data, "transfer has no multi handle");
3798
0
    return CURLE_FAILED_INIT;
3799
0
  }
3800
0
  if(!data->set.buffer_size) {
3801
0
    failf(data, "transfer buffer size is 0");
3802
0
    return CURLE_FAILED_INIT;
3803
0
  }
3804
0
  if(data->multi->xfer_buf_borrowed) {
3805
0
    failf(data, "attempt to borrow xfer_buf when already borrowed");
3806
0
    return CURLE_AGAIN;
3807
0
  }
3808
3809
0
  if(data->multi->xfer_buf &&
3810
0
     data->set.buffer_size > data->multi->xfer_buf_len) {
3811
    /* not large enough, get a new one */
3812
0
    free(data->multi->xfer_buf);
3813
0
    data->multi->xfer_buf = NULL;
3814
0
    data->multi->xfer_buf_len = 0;
3815
0
  }
3816
3817
0
  if(!data->multi->xfer_buf) {
3818
0
    data->multi->xfer_buf = malloc((size_t)data->set.buffer_size);
3819
0
    if(!data->multi->xfer_buf) {
3820
0
      failf(data, "could not allocate xfer_buf of %zu bytes",
3821
0
            (size_t)data->set.buffer_size);
3822
0
      return CURLE_OUT_OF_MEMORY;
3823
0
    }
3824
0
    data->multi->xfer_buf_len = data->set.buffer_size;
3825
0
  }
3826
3827
0
  data->multi->xfer_buf_borrowed = TRUE;
3828
0
  *pbuf = data->multi->xfer_buf;
3829
0
  *pbuflen = data->multi->xfer_buf_len;
3830
0
  return CURLE_OK;
3831
0
}
3832
3833
void Curl_multi_xfer_buf_release(struct Curl_easy *data, char *buf)
3834
0
{
3835
0
  (void)buf;
3836
0
  DEBUGASSERT(data);
3837
0
  DEBUGASSERT(data->multi);
3838
0
  DEBUGASSERT(!buf || data->multi->xfer_buf == buf);
3839
0
  data->multi->xfer_buf_borrowed = FALSE;
3840
0
}
3841
3842
CURLcode Curl_multi_xfer_ulbuf_borrow(struct Curl_easy *data,
3843
                                      char **pbuf, size_t *pbuflen)
3844
0
{
3845
0
  DEBUGASSERT(data);
3846
0
  DEBUGASSERT(data->multi);
3847
0
  *pbuf = NULL;
3848
0
  *pbuflen = 0;
3849
0
  if(!data->multi) {
3850
0
    failf(data, "transfer has no multi handle");
3851
0
    return CURLE_FAILED_INIT;
3852
0
  }
3853
0
  if(!data->set.upload_buffer_size) {
3854
0
    failf(data, "transfer upload buffer size is 0");
3855
0
    return CURLE_FAILED_INIT;
3856
0
  }
3857
0
  if(data->multi->xfer_ulbuf_borrowed) {
3858
0
    failf(data, "attempt to borrow xfer_ulbuf when already borrowed");
3859
0
    return CURLE_AGAIN;
3860
0
  }
3861
3862
0
  if(data->multi->xfer_ulbuf &&
3863
0
     data->set.upload_buffer_size > data->multi->xfer_ulbuf_len) {
3864
    /* not large enough, get a new one */
3865
0
    free(data->multi->xfer_ulbuf);
3866
0
    data->multi->xfer_ulbuf = NULL;
3867
0
    data->multi->xfer_ulbuf_len = 0;
3868
0
  }
3869
3870
0
  if(!data->multi->xfer_ulbuf) {
3871
0
    data->multi->xfer_ulbuf = malloc((size_t)data->set.upload_buffer_size);
3872
0
    if(!data->multi->xfer_ulbuf) {
3873
0
      failf(data, "could not allocate xfer_ulbuf of %zu bytes",
3874
0
            (size_t)data->set.upload_buffer_size);
3875
0
      return CURLE_OUT_OF_MEMORY;
3876
0
    }
3877
0
    data->multi->xfer_ulbuf_len = data->set.upload_buffer_size;
3878
0
  }
3879
3880
0
  data->multi->xfer_ulbuf_borrowed = TRUE;
3881
0
  *pbuf = data->multi->xfer_ulbuf;
3882
0
  *pbuflen = data->multi->xfer_ulbuf_len;
3883
0
  return CURLE_OK;
3884
0
}
3885
3886
void Curl_multi_xfer_ulbuf_release(struct Curl_easy *data, char *buf)
3887
0
{
3888
0
  (void)buf;
3889
0
  DEBUGASSERT(data);
3890
0
  DEBUGASSERT(data->multi);
3891
0
  DEBUGASSERT(!buf || data->multi->xfer_ulbuf == buf);
3892
0
  data->multi->xfer_ulbuf_borrowed = FALSE;
3893
0
}
3894
3895
CURLcode Curl_multi_xfer_sockbuf_borrow(struct Curl_easy *data,
3896
                                        size_t blen, char **pbuf)
3897
0
{
3898
0
  DEBUGASSERT(data);
3899
0
  DEBUGASSERT(data->multi);
3900
0
  *pbuf = NULL;
3901
0
  if(!data->multi) {
3902
0
    failf(data, "transfer has no multi handle");
3903
0
    return CURLE_FAILED_INIT;
3904
0
  }
3905
0
  if(data->multi->xfer_sockbuf_borrowed) {
3906
0
    failf(data, "attempt to borrow xfer_sockbuf when already borrowed");
3907
0
    return CURLE_AGAIN;
3908
0
  }
3909
3910
0
  if(data->multi->xfer_sockbuf && blen > data->multi->xfer_sockbuf_len) {
3911
    /* not large enough, get a new one */
3912
0
    free(data->multi->xfer_sockbuf);
3913
0
    data->multi->xfer_sockbuf = NULL;
3914
0
    data->multi->xfer_sockbuf_len = 0;
3915
0
  }
3916
3917
0
  if(!data->multi->xfer_sockbuf) {
3918
0
    data->multi->xfer_sockbuf = malloc(blen);
3919
0
    if(!data->multi->xfer_sockbuf) {
3920
0
      failf(data, "could not allocate xfer_sockbuf of %zu bytes", blen);
3921
0
      return CURLE_OUT_OF_MEMORY;
3922
0
    }
3923
0
    data->multi->xfer_sockbuf_len = blen;
3924
0
  }
3925
3926
0
  data->multi->xfer_sockbuf_borrowed = TRUE;
3927
0
  *pbuf = data->multi->xfer_sockbuf;
3928
0
  return CURLE_OK;
3929
0
}
3930
3931
void Curl_multi_xfer_sockbuf_release(struct Curl_easy *data, char *buf)
3932
0
{
3933
0
  (void)buf;
3934
0
  DEBUGASSERT(data);
3935
0
  DEBUGASSERT(data->multi);
3936
0
  DEBUGASSERT(!buf || data->multi->xfer_sockbuf == buf);
3937
0
  data->multi->xfer_sockbuf_borrowed = FALSE;
3938
0
}
3939
3940
static void multi_xfer_bufs_free(struct Curl_multi *multi)
3941
0
{
3942
0
  DEBUGASSERT(multi);
3943
0
  Curl_safefree(multi->xfer_buf);
3944
0
  multi->xfer_buf_len = 0;
3945
0
  multi->xfer_buf_borrowed = FALSE;
3946
0
  Curl_safefree(multi->xfer_ulbuf);
3947
0
  multi->xfer_ulbuf_len = 0;
3948
0
  multi->xfer_ulbuf_borrowed = FALSE;
3949
0
  Curl_safefree(multi->xfer_sockbuf);
3950
0
  multi->xfer_sockbuf_len = 0;
3951
0
  multi->xfer_sockbuf_borrowed = FALSE;
3952
0
}
3953
3954
struct Curl_easy *Curl_multi_get_easy(struct Curl_multi *multi,
3955
                                      unsigned int mid)
3956
0
{
3957
0
  struct Curl_easy *data = mid ? Curl_uint_tbl_get(&multi->xfers, mid) : NULL;
3958
0
  if(data && GOOD_EASY_HANDLE(data))
3959
0
    return data;
3960
0
  CURL_TRC_M(multi->admin, "invalid easy handle in xfer table for mid=%u",
3961
0
             mid);
3962
0
  Curl_uint_tbl_remove(&multi->xfers, mid);
3963
0
  return NULL;
3964
0
}
3965
3966
unsigned int Curl_multi_xfers_running(struct Curl_multi *multi)
3967
0
{
3968
0
  return multi->xfers_alive;
3969
0
}
3970
3971
void Curl_multi_mark_dirty(struct Curl_easy *data)
3972
0
{
3973
0
  if(data->multi && data->mid != UINT_MAX)
3974
0
    Curl_uint_bset_add(&data->multi->dirty, data->mid);
3975
0
}
3976
3977
#ifdef DEBUGBUILD
3978
static void multi_xfer_dump(struct Curl_multi *multi, unsigned int mid,
3979
                            void *entry)
3980
{
3981
  struct Curl_easy *data = entry;
3982
3983
  (void)multi;
3984
  if(!data) {
3985
    fprintf(stderr, "mid=%u, entry=NULL, bug in xfer table?\n", mid);
3986
  }
3987
  else {
3988
    fprintf(stderr, "mid=%u, magic=%s, p=%p, id=%" FMT_OFF_T ", url=%s\n",
3989
            mid, (data->magic == CURLEASY_MAGIC_NUMBER) ? "GOOD" : "BAD!",
3990
            (void *)data, data->id, data->state.url);
3991
  }
3992
}
3993
3994
static void multi_xfer_tbl_dump(struct Curl_multi *multi)
3995
{
3996
  unsigned int mid;
3997
  void *entry;
3998
  fprintf(stderr, "=== multi xfer table (count=%u, capacity=%u\n",
3999
          Curl_uint_tbl_count(&multi->xfers),
4000
          Curl_uint_tbl_capacity(&multi->xfers));
4001
  if(Curl_uint_tbl_first(&multi->xfers, &mid, &entry)) {
4002
    multi_xfer_dump(multi, mid, entry);
4003
    while(Curl_uint_tbl_next(&multi->xfers, mid, &mid, &entry))
4004
      multi_xfer_dump(multi, mid, entry);
4005
  }
4006
  fprintf(stderr, "===\n");
4007
  fflush(stderr);
4008
}
4009
#endif /* DEBUGBUILD */