Coverage Report

Created: 2026-12-01 06:28

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
0
#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
0
#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
0
{
54
0
  return curlx_calloc(1, sizeof(struct mntfy_chunk));
55
0
}
56
57
static void mnfty_chunk_destroy(struct mntfy_chunk *chunk)
58
0
{
59
0
  curlx_free(chunk);
60
0
}
61
62
static void mnfty_chunk_reset(struct mntfy_chunk *chunk)
63
0
{
64
0
  chunk->r_offset = chunk->w_offset = 0;
65
0
}
66
67
static bool mntfy_chunk_append(struct mntfy_chunk *chunk,
68
                               struct Curl_easy *data,
69
                               uint32_t type)
70
0
{
71
0
  struct mntfy_entry *e;
72
73
0
  if(chunk->w_offset >= CURL_MNTFY_CHUNK_SIZE)
74
0
    return FALSE;
75
0
  e = &chunk->entries[chunk->w_offset++];
76
0
  e->mid = data->mid;
77
0
  e->type = type;
78
0
  return TRUE;
79
0
}
80
81
static struct mntfy_chunk *mntfy_non_full_tail(struct curl_multi_ntfy *mntfy)
82
0
{
83
0
  struct mntfy_chunk *chunk;
84
0
  if(!mntfy->tail) {
85
0
    chunk = mnfty_chunk_create();
86
0
    if(!chunk)
87
0
      return NULL;
88
0
    DEBUGASSERT(!mntfy->head);
89
0
    mntfy->head = mntfy->tail = chunk;
90
0
    return chunk;
91
0
  }
92
0
  else if(mntfy->tail->w_offset < CURL_MNTFY_CHUNK_SIZE)
93
0
    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
0
}
104
105
static void mntfy_chunk_dispatch_all(struct Curl_multi *multi,
106
                                     struct mntfy_chunk *chunk)
107
0
{
108
0
  struct mntfy_entry *e;
109
0
  struct Curl_easy *data;
110
111
0
  while(multi->ntfy.ntfy_cb &&
112
0
        (chunk->r_offset < chunk->w_offset) && !multi->ntfy.failure) {
113
0
    e = &chunk->entries[chunk->r_offset];
114
0
    if(e->mid != UINT32_MAX) {
115
0
      data = e->mid ? Curl_multi_get_easy(multi, e->mid) : multi->admin;
116
      /* only when notification has not been disabled in the meantime */
117
0
      if(data && (multi->ntfy.flags & CURL_MNTFY_TYPE_FLAG(e->type))) {
118
        /* this may cause new notifications to be added! */
119
0
        CURL_TRC_M(multi->admin, "[NTFY] dispatch %u to xfer %u",
120
0
                   e->type, e->mid);
121
0
        multi->ntfy.ntfy_cb(multi, e->type, data, multi->ntfy.ntfy_cb_data);
122
0
      }
123
0
    }
124
    /* once dispatched, safe to increment */
125
0
    chunk->r_offset++;
126
0
  }
127
0
  mnfty_chunk_reset(chunk);
128
0
}
129
130
void Curl_mntfy_init(struct Curl_multi *multi)
131
0
{
132
0
  memset(&multi->ntfy, 0, sizeof(multi->ntfy));
133
0
}
134
135
void Curl_mntfy_cleanup(struct Curl_multi *multi)
136
0
{
137
0
  while(multi->ntfy.head) {
138
0
    struct mntfy_chunk *chunk = multi->ntfy.head;
139
0
    multi->ntfy.head = chunk->next;
140
0
    mnfty_chunk_destroy(chunk);
141
0
  }
142
0
  multi->ntfy.tail = NULL;
143
0
}
144
145
CURLMcode Curl_mntfy_enable(struct Curl_multi *multi, unsigned int type)
146
0
{
147
0
  if(type >= CURLMNOTIFY_LAST)
148
0
    return CURLM_UNKNOWN_OPTION;
149
0
  multi->ntfy.flags |= CURL_MNTFY_TYPE_FLAG(type);
150
0
  return CURLM_OK;
151
0
}
152
153
CURLMcode Curl_mntfy_disable(struct Curl_multi *multi, unsigned int type)
154
0
{
155
0
  if(type >= CURLMNOTIFY_LAST)
156
0
    return CURLM_UNKNOWN_OPTION;
157
0
  multi->ntfy.flags &= ~CURL_MNTFY_TYPE_FLAG(type);
158
0
  return CURLM_OK;
159
0
}
160
161
void Curl_mntfy_add(struct Curl_easy *data, unsigned int type)
162
0
{
163
0
  struct Curl_multi *multi = data ? data->multi : NULL;
164
0
  if(multi && multi->ntfy.ntfy_cb && !multi->ntfy.failure &&
165
0
     type < CURLMNOTIFY_LAST &&
166
0
     (multi->ntfy.flags & CURL_MNTFY_TYPE_FLAG(type))) {
167
    /* append to list of outstanding notifications */
168
0
    struct mntfy_chunk *tail = mntfy_non_full_tail(&multi->ntfy);
169
0
    CURL_TRC_M(data, "[NTFY] add %u for xfer %u", type, data->mid);
170
0
    if(tail)
171
0
      mntfy_chunk_append(tail, data, (uint32_t)type);
172
0
    else
173
0
      multi->ntfy.failure = CURLM_OUT_OF_MEMORY;
174
0
    multi->ntfy.flags |= CURL_MNTFY_FLAG_HAS_ENTRIES;
175
0
  }
176
0
}
177
178
void Curl_mntfy_remove(struct Curl_easy *data)
179
0
{
180
0
  struct Curl_multi *multi = data ? data->multi : NULL;
181
0
  uint32_t mid = data ? data->mid : UINT32_MAX;
182
0
  struct mntfy_chunk *chunk;
183
0
  size_t i;
184
185
0
  if(!multi || (mid == UINT32_MAX))
186
0
    return;
187
188
0
  for(chunk = multi->ntfy.head; chunk; chunk = chunk->next) {
189
0
    for(i = chunk->r_offset; i < chunk->w_offset; ++i) {
190
0
      if(chunk->entries[i].mid == mid)
191
0
        chunk->entries[i].mid = UINT32_MAX;
192
0
    }
193
0
  }
194
0
}
195
196
CURLMcode Curl_mntfy_dispatch_all(struct Curl_multi *multi)
197
0
{
198
0
  struct Curl_mapi_guard guard;
199
200
0
  if(!multi)
201
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
202
203
0
  CURL_CBAPI_MULTI_START(&guard, multi, multi_ntfy_cb);
204
205
0
  while(multi->ntfy.head && !multi->ntfy.failure) {
206
0
    struct mntfy_chunk *chunk = multi->ntfy.head;
207
    /* this may cause new notifications to be added! */
208
0
    mntfy_chunk_dispatch_all(multi, chunk);
209
0
    DEBUGASSERT(chunk->r_offset == chunk->w_offset);
210
211
0
    if(chunk == multi->ntfy.tail) /* last one, keep */
212
0
      break;
213
0
    DEBUGASSERT(chunk->next);
214
0
    DEBUGASSERT(multi->ntfy.head != multi->ntfy.tail);
215
0
    multi->ntfy.head = chunk->next;
216
0
    mnfty_chunk_destroy(chunk);
217
0
  }
218
219
0
  CURL_CBAPI_MULTI_END(&guard);
220
221
0
  if(multi->ntfy.failure) {
222
0
    CURLMcode mresult = multi->ntfy.failure;
223
0
    multi->ntfy.failure = CURLM_OK; /* reset, once delivered */
224
0
    return mresult;
225
0
  }
226
0
  else
227
0
    multi->ntfy.flags &= ~CURL_MNTFY_FLAG_HAS_ENTRIES;
228
0
  return CURLM_OK;
229
0
}