Coverage Report

Created: 2026-10-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/multi_ntfy.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#include "urldata.h"
27
#include "curl_trc.h"
28
#include "multihandle.h"
29
#include "multiif.h"
30
#include "multi_ntfy.h"
31
32
#if CURLMNOTIFY_LAST > 31
33
#error "notification types must fit below the curl_multi_ntfy high flag bit"
34
#endif
35
36
261k
#define CURL_MNTFY_TYPE_FLAG(type)  ((uint32_t)1 << (type))
37
38
struct mntfy_entry {
39
  uint32_t mid;
40
  uint32_t type;
41
};
42
43
135k
#define CURL_MNTFY_CHUNK_SIZE 128
44
45
struct mntfy_chunk {
46
  struct mntfy_chunk *next;
47
  size_t r_offset;
48
  size_t w_offset;
49
  struct mntfy_entry entries[CURL_MNTFY_CHUNK_SIZE];
50
};
51
52
static struct mntfy_chunk *mnfty_chunk_create(void)
53
16.9k
{
54
16.9k
  return curlx_calloc(1, sizeof(struct mntfy_chunk));
55
16.9k
}
56
57
static void mnfty_chunk_destroy(struct mntfy_chunk *chunk)
58
16.9k
{
59
16.9k
  curlx_free(chunk);
60
16.9k
}
61
62
static void mnfty_chunk_reset(struct mntfy_chunk *chunk)
63
31.2k
{
64
31.2k
  chunk->r_offset = chunk->w_offset = 0;
65
31.2k
}
66
67
static bool mntfy_chunk_append(struct mntfy_chunk *chunk,
68
                               struct Curl_easy *data,
69
                               uint32_t type)
70
76.1k
{
71
76.1k
  struct mntfy_entry *e;
72
73
76.1k
  if(chunk->w_offset >= CURL_MNTFY_CHUNK_SIZE)
74
0
    return FALSE;
75
76.1k
  e = &chunk->entries[chunk->w_offset++];
76
76.1k
  e->mid = data->mid;
77
76.1k
  e->type = type;
78
76.1k
  return TRUE;
79
76.1k
}
80
81
static struct mntfy_chunk *mntfy_non_full_tail(struct curl_multi_ntfy *mntfy)
82
76.1k
{
83
76.1k
  struct mntfy_chunk *chunk;
84
76.1k
  if(!mntfy->tail) {
85
16.9k
    chunk = mnfty_chunk_create();
86
16.9k
    if(!chunk)
87
54
      return NULL;
88
16.9k
    DEBUGASSERT(!mntfy->head);
89
16.9k
    mntfy->head = mntfy->tail = chunk;
90
16.9k
    return chunk;
91
16.9k
  }
92
59.2k
  else if(mntfy->tail->w_offset < CURL_MNTFY_CHUNK_SIZE)
93
59.2k
    return mntfy->tail;
94
0
  else { /* tail is full. */
95
0
    chunk = mnfty_chunk_create();
96
0
    if(!chunk)
97
0
      return NULL;
98
0
    DEBUGASSERT(mntfy->head);
99
0
    mntfy->tail->next = chunk;
100
0
    mntfy->tail = chunk;
101
0
    return chunk;
102
0
  }
103
76.1k
}
104
105
static void mntfy_chunk_dispatch_all(struct Curl_multi *multi,
106
                                     struct mntfy_chunk *chunk)
107
31.2k
{
108
31.2k
  struct mntfy_entry *e;
109
31.2k
  struct Curl_easy *data;
110
111
107k
  while(multi->ntfy.ntfy_cb &&
112
107k
        (chunk->r_offset < chunk->w_offset) && !multi->ntfy.failure) {
113
76.1k
    e = &chunk->entries[chunk->r_offset];
114
76.1k
    data = e->mid ? Curl_multi_get_easy(multi, e->mid) : multi->admin;
115
    /* only when notification has not been disabled in the meantime */
116
76.1k
    if(data && (multi->ntfy.flags & CURL_MNTFY_TYPE_FLAG(e->type))) {
117
      /* this may cause new notifications to be added! */
118
76.1k
      CURL_TRC_M(multi->admin, "[NTFY] dispatch %u to xfer %u",
119
76.1k
                 e->type, e->mid);
120
76.1k
      multi->ntfy.ntfy_cb(multi, e->type, data, multi->ntfy.ntfy_cb_data);
121
76.1k
    }
122
    /* once dispatched, safe to increment */
123
76.1k
    chunk->r_offset++;
124
76.1k
  }
125
31.2k
  mnfty_chunk_reset(chunk);
126
31.2k
}
127
128
void Curl_mntfy_init(struct Curl_multi *multi)
129
329k
{
130
329k
  memset(&multi->ntfy, 0, sizeof(multi->ntfy));
131
329k
}
132
133
void Curl_mntfy_cleanup(struct Curl_multi *multi)
134
329k
{
135
346k
  while(multi->ntfy.head) {
136
16.9k
    struct mntfy_chunk *chunk = multi->ntfy.head;
137
16.9k
    multi->ntfy.head = chunk->next;
138
16.9k
    mnfty_chunk_destroy(chunk);
139
16.9k
  }
140
329k
  multi->ntfy.tail = NULL;
141
329k
}
142
143
CURLMcode Curl_mntfy_enable(struct Curl_multi *multi, unsigned int type)
144
72.5k
{
145
72.5k
  if(type >= CURLMNOTIFY_LAST)
146
0
    return CURLM_UNKNOWN_OPTION;
147
72.5k
  multi->ntfy.flags |= CURL_MNTFY_TYPE_FLAG(type);
148
72.5k
  return CURLM_OK;
149
72.5k
}
150
151
CURLMcode Curl_mntfy_disable(struct Curl_multi *multi, unsigned int type)
152
36.2k
{
153
36.2k
  if(type >= CURLMNOTIFY_LAST)
154
0
    return CURLM_UNKNOWN_OPTION;
155
36.2k
  multi->ntfy.flags &= ~CURL_MNTFY_TYPE_FLAG(type);
156
36.2k
  return CURLM_OK;
157
36.2k
}
158
159
void Curl_mntfy_add(struct Curl_easy *data, unsigned int type)
160
76.2k
{
161
76.2k
  struct Curl_multi *multi = data ? data->multi : NULL;
162
76.2k
  if(multi && multi->ntfy.ntfy_cb && !multi->ntfy.failure &&
163
76.1k
     type < CURLMNOTIFY_LAST &&
164
76.1k
     (multi->ntfy.flags & CURL_MNTFY_TYPE_FLAG(type))) {
165
    /* append to list of outstanding notifications */
166
76.1k
    struct mntfy_chunk *tail = mntfy_non_full_tail(&multi->ntfy);
167
76.1k
    CURL_TRC_M(data, "[NTFY] add %u for xfer %u", type, data->mid);
168
76.1k
    if(tail)
169
76.1k
      mntfy_chunk_append(tail, data, (uint32_t)type);
170
54
    else
171
54
      multi->ntfy.failure = CURLM_OUT_OF_MEMORY;
172
76.1k
    multi->ntfy.flags |= CURL_MNTFY_FLAG_HAS_ENTRIES;
173
76.1k
  }
174
76.2k
}
175
176
CURLMcode Curl_mntfy_dispatch_all(struct Curl_multi *multi)
177
31.3k
{
178
31.3k
  struct Curl_mapi_guard guard;
179
180
31.3k
  if(!multi)
181
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
182
183
31.3k
  CURL_CBAPI_MULTI_START(&guard, multi, multi_ntfy_cb);
184
185
31.3k
  while(multi->ntfy.head && !multi->ntfy.failure) {
186
31.2k
    struct mntfy_chunk *chunk = multi->ntfy.head;
187
    /* this may cause new notifications to be added! */
188
31.2k
    mntfy_chunk_dispatch_all(multi, chunk);
189
31.2k
    DEBUGASSERT(chunk->r_offset == chunk->w_offset);
190
191
31.2k
    if(chunk == multi->ntfy.tail) /* last one, keep */
192
31.2k
      break;
193
0
    DEBUGASSERT(chunk->next);
194
0
    DEBUGASSERT(multi->ntfy.head != multi->ntfy.tail);
195
0
    multi->ntfy.head = chunk->next;
196
0
    mnfty_chunk_destroy(chunk);
197
0
  }
198
199
31.3k
  CURL_CBAPI_MULTI_END(&guard);
200
201
31.3k
  if(multi->ntfy.failure) {
202
54
    CURLMcode mresult = multi->ntfy.failure;
203
54
    multi->ntfy.failure = CURLM_OK; /* reset, once delivered */
204
54
    return mresult;
205
54
  }
206
31.3k
  else
207
31.3k
    multi->ntfy.flags &= ~CURL_MNTFY_FLAG_HAS_ENTRIES;
208
31.3k
  return CURLM_OK;
209
31.3k
}