Debian Codegraph @

src/shared/curl-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "curl-util.h"
4 #include "log.h"
5
6 #if HAVE_LIBCURL
7
8 #include "sd-dlopen.h"
9 #include "sd-event.h"
10
11 #include "alloc-util.h"
12 #include "dlfcn-util.h"
13 #include "fd-util.h"
14 #include "hashmap.h"
15 #include "set.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "time-util.h"
19 #include "version.h"
20
21 DLSYM_PROTOTYPE(curl_easy_cleanup) = NULL;
22 DLSYM_PROTOTYPE(curl_easy_getinfo) = NULL;
23 DLSYM_PROTOTYPE(curl_easy_init) = NULL;
24 DLSYM_PROTOTYPE(curl_easy_perform) = NULL;
25 DLSYM_PROTOTYPE(curl_easy_setopt) = NULL;
26 DLSYM_PROTOTYPE(curl_easy_strerror) = NULL;
27 #if LIBCURL_VERSION_NUM >= 0x075300
28 DLSYM_PROTOTYPE(curl_easy_header) = NULL;
29 #endif
30 DLSYM_PROTOTYPE(curl_getdate) = NULL;
31 static DLSYM_PROTOTYPE(curl_multi_add_handle) = NULL;
32 static DLSYM_PROTOTYPE(curl_multi_assign) = NULL;
33 static DLSYM_PROTOTYPE(curl_multi_cleanup) = NULL;
34 static DLSYM_PROTOTYPE(curl_multi_info_read) = NULL;
35 static DLSYM_PROTOTYPE(curl_multi_init) = NULL;
36 static DLSYM_PROTOTYPE(curl_multi_remove_handle) = NULL;
37 static DLSYM_PROTOTYPE(curl_multi_setopt) = NULL;
38 static DLSYM_PROTOTYPE(curl_multi_socket_action) = NULL;
39 DLSYM_PROTOTYPE(curl_slist_append) = NULL;
40 DLSYM_PROTOTYPE(curl_slist_free_all) = NULL;
41
42 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(CURLM*, sym_curl_multi_cleanup, curl_multi_cleanupp, NULL);
43
44 struct CurlGlue {
45 unsigned n_ref;
46 sd_event *event;
47 CURLM *curl;
48 sd_event_source *timer;
49 Hashmap *ios;
50 sd_event_source *defer;
51 Set *slots; /* CurlSlot* — back-pointer set; floating slots are kept alive here */
52 };
53
54 struct CurlSlot {
55 unsigned n_ref;
56 CurlGlue *glue; /* NULL once disconnected (callback fired, cancelled, or glue died) */
57 CURL *easy; /* owned; cleared once the easy handle has been freed */
58 bool floating;
59 curl_finished_t callback;
60 void *userdata;
61 };
62
63 static void curl_slot_disconnect(CurlSlot *slot, bool unref) {
64 assert(slot);
65
66 /* Tear down the slot's connection to the glue: pull the easy handle out of the multi,
67 * curl_easy_cleanup() it, and remove the slot from the glue's lookup set. Floating
68 * slots are owned by that set, so on disconnect we drop the implicit ref (when
69 * unref=true; the recursive call from curl_slot_free passes false to avoid infinite
70 * recursion). Non-floating slots release the back-ref they held on the glue.
71 *
72 * Idempotent: once slot->glue is NULL, subsequent calls are no-ops. */
73
74 if (!slot->glue)
75 return;
76
77 CurlGlue *glue = slot->glue;
78
79 if (slot->easy) {
80 if (glue->curl)
81 (void) sym_curl_multi_remove_handle(glue->curl, slot->easy);
82 sym_curl_easy_cleanup(slot->easy);
83 slot->easy = NULL;
84 }
85
86 set_remove(glue->slots, slot);
87 slot->glue = NULL;
88
89 if (!slot->floating)
90 curl_glue_unref(glue);
91 else if (unref)
92 curl_slot_unref(slot);
93 }
94
95 static CurlSlot* curl_slot_free(CurlSlot *slot) {
96 if (!slot)
97 return NULL;
98
99 curl_slot_disconnect(slot, /* unref= */ false);
100 return mfree(slot);
101 }
102
103 DEFINE_TRIVIAL_REF_UNREF_FUNC(CurlSlot, curl_slot, curl_slot_free);
104
105 CURL* curl_slot_get_easy(CurlSlot *slot) {
106 assert(slot);
107 return slot->easy;
108 }
109
110 CurlGlue* curl_slot_get_glue(CurlSlot *slot) {
111 assert(slot);
112 return slot->glue;
113 }
114
115 static void curl_glue_check_finished(CurlGlue *g) {
116 int r;
117
118 assert(g);
119
120 /* sd_event_get_exit_code() returns -ENODATA if no exit was scheduled yet */
121 r = sd_event_get_exit_code(g->event, /* ret= */ NULL);
122 if (r >= 0)
123 return; /* exit scheduled? Then don't process this anymore */
124 if (r != -ENODATA)
125 log_debug_errno(r, "Unexpected error while checking for event loop exit code, ignoring: %m");
126
127 CURLMsg *msg;
128 int k = 0;
129 msg = sym_curl_multi_info_read(g->curl, &k);
130 if (!msg)
131 return;
132
133 if (msg->msg == CURLMSG_DONE) {
134 CURL *easy = msg->easy_handle;
135 CURLcode code = msg->data.result;
136 CurlSlot *slot = NULL;
137
138 if (sym_curl_easy_getinfo(easy, CURLINFO_PRIVATE, (char**) &slot) == CURLE_OK && slot) {
139 /* Pin the slot across the callback: a floating slot's only
140 * reference is the one held via the glue's slots set, and
141 * disconnect drops it. */
142 curl_slot_ref(slot);
143
144 if (slot->callback) {
145 r = slot->callback(slot, easy, code, slot->userdata);
146 if (r < 0)
147 log_debug_errno(r, "Curl finished callback returned error, ignoring: %m");
148 }
149
150 curl_slot_disconnect(slot, /* unref= */ true);
151 curl_slot_unref(slot);
152 }
153 }
154
155 /* This is a queue, process another item soon, but do so in a later event loop iteration. */
156 (void) sd_event_source_set_enabled(g->defer, SD_EVENT_ONESHOT);
157 }
158
159 static int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
160 CurlGlue *g = ASSERT_PTR(userdata);
161 int action, k = 0;
162
163 assert(s);
164
165 if (FLAGS_SET(revents, EPOLLIN | EPOLLOUT))
166 action = CURL_POLL_INOUT;
167 else if (revents & EPOLLIN)
168 action = CURL_POLL_IN;
169 else if (revents & EPOLLOUT)
170 action = CURL_POLL_OUT;
171 else
172 action = 0;
173
174 if (sym_curl_multi_socket_action(g->curl, fd, action, &k) != CURLM_OK)
175 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
176 "Failed to propagate IO event.");
177
178 curl_glue_check_finished(g);
179 return 0;
180 }
181
182 static int curl_glue_socket_callback(CURL *curl, curl_socket_t s, int action, void *userdata, void *socketp) {
183 sd_event_source *io = socketp;
184 CurlGlue *g = ASSERT_PTR(userdata);
185 uint32_t events = 0;
186 int r;
187
188 assert(curl);
189
190 if (action == CURL_POLL_REMOVE) {
191 if (io) {
192 sd_event_source_disable_unref(io);
193
194 hashmap_remove(g->ios, FD_TO_PTR(s));
195 }
196
197 return 0;
198 }
199
200 /* Don't configure io event source anymore when the event loop is dead already. */
201 if (g->event && sd_event_get_state(g->event) == SD_EVENT_FINISHED)
202 return 0;
203
204 r = hashmap_ensure_allocated(&g->ios, &trivial_hash_ops);
205 if (r < 0) {
206 log_oom();
207 return -1;
208 }
209
210 if (action == CURL_POLL_IN)
211 events = EPOLLIN;
212 else if (action == CURL_POLL_OUT)
213 events = EPOLLOUT;
214 else if (action == CURL_POLL_INOUT)
215 events = EPOLLIN|EPOLLOUT;
216
217 if (io) {
218 if (sd_event_source_set_io_events(io, events) < 0)
219 return -1;
220
221 if (sd_event_source_set_enabled(io, SD_EVENT_ON) < 0)
222 return -1;
223 } else {
224 if (sd_event_add_io(g->event, &io, s, events, curl_glue_on_io, g) < 0)
225 return -1;
226
227 if (sym_curl_multi_assign(g->curl, s, io) != CURLM_OK)
228 return -1;
229
230 (void) sd_event_source_set_description(io, "curl-io");
231
232 r = hashmap_put(g->ios, FD_TO_PTR(s), io);
233 if (r < 0) {
234 log_oom();
235 sd_event_source_unref(io);
236 return -1;
237 }
238 }
239
240 return 0;
241 }
242
243 static int curl_glue_on_timer(sd_event_source *s, uint64_t usec, void *userdata) {
244 CurlGlue *g = ASSERT_PTR(userdata);
245 int k = 0;
246
247 assert(s);
248
249 if (sym_curl_multi_socket_action(g->curl, CURL_SOCKET_TIMEOUT, 0, &k) != CURLM_OK)
250 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
251 "Failed to propagate timeout.");
252
253 curl_glue_check_finished(g);
254 return 0;
255 }
256
257 static int curl_glue_timer_callback(CURLM *curl, long timeout_ms, void *userdata) {
258 CurlGlue *g = ASSERT_PTR(userdata);
259 usec_t usec;
260
261 assert(curl);
262
263 /* Don't configure timer anymore when the event loop is dead already. */
264 if (g->timer) {
265 sd_event *event_loop = sd_event_source_get_event(g->timer);
266 if (event_loop && sd_event_get_state(event_loop) == SD_EVENT_FINISHED)
267 return 0;
268 }
269
270 if (timeout_ms < 0) {
271 if (sd_event_source_set_enabled(g->timer, SD_EVENT_OFF) < 0)
272 return -1;
273
274 return 0;
275 }
276
277 usec = (usec_t) timeout_ms * USEC_PER_MSEC + USEC_PER_MSEC - 1;
278
279 if (g->timer) {
280 if (sd_event_source_set_time_relative(g->timer, usec) < 0)
281 return -1;
282
283 if (sd_event_source_set_enabled(g->timer, SD_EVENT_ONESHOT) < 0)
284 return -1;
285 } else {
286 if (sd_event_add_time_relative(g->event, &g->timer, CLOCK_BOOTTIME, usec, 0, curl_glue_on_timer, g) < 0)
287 return -1;
288
289 (void) sd_event_source_set_description(g->timer, "curl-timer");
290 }
291
292 return 0;
293 }
294
295 static int curl_glue_on_defer(sd_event_source *s, void *userdata) {
296 CurlGlue *g = ASSERT_PTR(userdata);
297
298 assert(s);
299
300 curl_glue_check_finished(g);
301 return 0;
302 }
303
304 static CurlGlue* curl_glue_free(CurlGlue *g) {
305 sd_event_source *io;
306 CurlSlot *slot;
307
308 if (!g)
309 return NULL;
310
311 /* Drain any slots still hanging off us. By construction only floating slots can
312 * be here: connected non-floating slots hold a glue back-ref, so glue's last ref
313 * couldn't have dropped while one was attached. disconnect(unref=true) does the
314 * floating slot's free as part of its work. set_steal_first() pops up front so
315 * forward progress doesn't depend on disconnect's internal set_remove(). */
316 while ((slot = set_steal_first(g->slots)))
317 curl_slot_disconnect(slot, /* unref= */ true);
318 g->slots = set_free(g->slots);
319
320 if (g->curl)
321 sym_curl_multi_cleanup(g->curl);
322
323 while ((io = hashmap_steal_first(g->ios)))
324 sd_event_source_unref(io);
325
326 hashmap_free(g->ios);
327
328 sd_event_source_disable_unref(g->timer);
329 sd_event_source_disable_unref(g->defer);
330 sd_event_unref(g->event);
331 return mfree(g);
332 }
333
334 DEFINE_TRIVIAL_REF_UNREF_FUNC(CurlGlue, curl_glue, curl_glue_free);
335
336 int curl_glue_new(CurlGlue **glue, sd_event *event) {
337 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
338 _cleanup_(curl_multi_cleanupp) CURLM *c = NULL;
339 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
340 int r;
341
342 assert(glue);
343
344 r = dlopen_curl(LOG_DEBUG);
345 if (r < 0)
346 return r;
347
348 if (event)
349 e = sd_event_ref(event);
350 else {
351 r = sd_event_default(&e);
352 if (r < 0)
353 return r;
354 }
355
356 c = sym_curl_multi_init();
357 if (!c)
358 return -ENOMEM;
359
360 g = new(CurlGlue, 1);
361 if (!g)
362 return -ENOMEM;
363
364 *g = (CurlGlue) {
365 .n_ref = 1,
366 .event = TAKE_PTR(e),
367 .curl = TAKE_PTR(c),
368 };
369
370 if (sym_curl_multi_setopt(g->curl, CURLMOPT_SOCKETDATA, g) != CURLM_OK)
371 return -EINVAL;
372
373 if (sym_curl_multi_setopt(g->curl, CURLMOPT_SOCKETFUNCTION, curl_glue_socket_callback) != CURLM_OK)
374 return -EINVAL;
375
376 if (sym_curl_multi_setopt(g->curl, CURLMOPT_TIMERDATA, g) != CURLM_OK)
377 return -EINVAL;
378
379 if (sym_curl_multi_setopt(g->curl, CURLMOPT_TIMERFUNCTION, curl_glue_timer_callback) != CURLM_OK)
380 return -EINVAL;
381
382 r = sd_event_add_defer(g->event, &g->defer, curl_glue_on_defer, g);
383 if (r < 0)
384 return r;
385
386 (void) sd_event_source_set_description(g->defer, "curl-defer");
387
388 *glue = TAKE_PTR(g);
389
390 return 0;
391 }
392
393 int curl_glue_make(CURL **ret, const char *url) {
394 _cleanup_(curl_easy_cleanupp) CURL *c = NULL;
395 const char *useragent;
396 int r;
397
398 assert(ret);
399 assert(url);
400
401 r = dlopen_curl(LOG_DEBUG);
402 if (r < 0)
403 return r;
404
405 c = sym_curl_easy_init();
406 if (!c)
407 return -ENOMEM;
408
409 if (DEBUG_LOGGING)
410 (void) sym_curl_easy_setopt(c, CURLOPT_VERBOSE, 1L);
411
412 if (sym_curl_easy_setopt(c, CURLOPT_URL, url) != CURLE_OK)
413 return -EIO;
414
415 useragent = strjoina(program_invocation_short_name, "/" GIT_VERSION);
416 if (sym_curl_easy_setopt(c, CURLOPT_USERAGENT, useragent) != CURLE_OK)
417 return -EIO;
418
419 if (sym_curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK)
420 return -EIO;
421
422 if (sym_curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1L) != CURLE_OK)
423 return -EIO;
424
425 if (sym_curl_easy_setopt(c, CURLOPT_LOW_SPEED_TIME, 60L) != CURLE_OK)
426 return -EIO;
427
428 if (sym_curl_easy_setopt(c, CURLOPT_LOW_SPEED_LIMIT, 30L) != CURLE_OK)
429 return -EIO;
430
431 #if LIBCURL_VERSION_NUM >= 0x075500 /* libcurl 7.85.0 */
432 if (sym_curl_easy_setopt(c, CURLOPT_PROTOCOLS_STR, "HTTP,HTTPS,FILE") != CURLE_OK)
433 #else
434 if (sym_curl_easy_setopt(c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS|CURLPROTO_FILE) != CURLE_OK)
435 return -EIO;
436 if (sym_curl_easy_setopt(c, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS) != CURLE_OK)
437 #endif
438 return -EIO;
439
440 *ret = TAKE_PTR(c);
441 return 0;
442 }
443
444 int curl_glue_perform_async(
445 CurlGlue *g,
446 CURL *easy,
447 curl_finished_t cb,
448 void *userdata,
449 CurlSlot **ret_slot) {
450
451 int r;
452
453 assert(g);
454 assert(easy);
455
456 _cleanup_(curl_slot_unrefp) CurlSlot *slot = new(CurlSlot, 1);
457 if (!slot)
458 return -ENOMEM;
459
460 *slot = (CurlSlot) {
461 .n_ref = 1,
462 .glue = NULL, /* wired up below, after we've committed to the multi */
463 .easy = easy,
464 .floating = !ret_slot,
465 .callback = cb,
466 .userdata = userdata,
467 };
468
469 r = set_ensure_put(&g->slots, &trivial_hash_ops, slot);
470 if (r < 0)
471 return r;
472 assert(r > 0);
473
474 if (sym_curl_multi_add_handle(g->curl, easy) != CURLM_OK) {
475 set_remove(g->slots, slot);
476 return -EIO;
477 }
478
479 /* Stash the slot pointer on the easy handle so curl_glue_check_finished() can recover
480 * it on completion. Set this only after we've fully committed to the multi, so that
481 * error paths above don't leave a dangling pointer on the easy handle. */
482 if (sym_curl_easy_setopt(easy, CURLOPT_PRIVATE, slot) != CURLE_OK) {
483 sym_curl_multi_remove_handle(g->curl, easy);
484 set_remove(g->slots, slot);
485 return -EIO;
486 }
487
488 slot->glue = g;
489 if (!slot->floating)
490 curl_glue_ref(g);
491
492 /* Transfer the slot's single reference: to the caller for non-floating slots, or to
493 * the glue's slot set (implicitly, until disconnect drops it) for floating ones. */
494 if (ret_slot)
495 *ret_slot = slot;
496
497 TAKE_PTR(slot);
498 return 0;
499 }
500
501 struct curl_slist *curl_slist_new(const char *first, ...) {
502 struct curl_slist *l;
503 va_list ap;
504
505 if (!first)
506 return NULL;
507
508 l = sym_curl_slist_append(NULL, first);
509 if (!l)
510 return NULL;
511
512 va_start(ap, first);
513
514 for (;;) {
515 struct curl_slist *n;
516 const char *i;
517
518 i = va_arg(ap, const char*);
519 if (!i)
520 break;
521
522 n = sym_curl_slist_append(l, i);
523 if (!n) {
524 va_end(ap);
525 sym_curl_slist_free_all(l);
526 return NULL;
527 }
528
529 l = n;
530 }
531
532 va_end(ap);
533 return l;
534 }
535
536 int curl_header_strdup(const void *contents, size_t sz, const char *field, char **value) {
537 const char *p;
538 char *s;
539
540 assert(value);
541
542 p = memory_startswith_no_case(contents, sz, field);
543 if (!p)
544 return 0;
545
546 sz -= p - (const char*) contents;
547
548 if (memchr(p, 0, sz))
549 return 0;
550
551 /* Skip over preceding whitespace */
552 while (sz > 0 && strchr(WHITESPACE, p[0])) {
553 p++;
554 sz--;
555 }
556
557 /* Truncate trailing whitespace */
558 while (sz > 0 && strchr(WHITESPACE, p[sz-1]))
559 sz--;
560
561 s = strndup(p, sz);
562 if (!s)
563 return -ENOMEM;
564
565 *value = s;
566 return 1;
567 }
568
569 int curl_parse_http_time(const char *t, usec_t *ret) {
570 assert(t);
571 assert(ret);
572
573 time_t v = sym_curl_getdate(t, NULL);
574 if (v == (time_t) -1)
575 return -EINVAL;
576
577 if ((usec_t) v >= USEC_INFINITY / USEC_PER_SEC) /* check overflow */
578 return -ERANGE;
579
580 *ret = (usec_t) v * USEC_PER_SEC;
581
582 return 0;
583 }
584
585 int curl_append_to_header(struct curl_slist **list, char **headers) {
586 /* This function leaves 'list' modified on partial failure.
587 * Input/output param list may point to NULL. */
588
589 assert(list);
590
591 STRV_FOREACH(h, headers) {
592 struct curl_slist *l = sym_curl_slist_append(*list, *h);
593 if (!l)
594 return -ENOMEM;
595 *list = l;
596 }
597
598 return 0;
599 }
600
601 #endif
602
603 int dlopen_curl(int log_level) {
604 #if HAVE_LIBCURL
605 static void *curl_dl = NULL;
606
607 CURL_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED);
608
609 return dlopen_many_sym_or_warn(
610 &curl_dl,
611 "libcurl.so.4",
612 log_level,
613 DLSYM_ARG(curl_easy_cleanup),
614 DLSYM_ARG(curl_easy_getinfo),
615 DLSYM_ARG(curl_easy_init),
616 DLSYM_ARG(curl_easy_perform),
617 DLSYM_ARG(curl_easy_setopt),
618 DLSYM_ARG(curl_easy_strerror),
619 #if LIBCURL_VERSION_NUM >= 0x075300
620 DLSYM_ARG(curl_easy_header),
621 #endif
622 DLSYM_ARG(curl_getdate),
623 DLSYM_ARG(curl_multi_add_handle),
624 DLSYM_ARG(curl_multi_assign),
625 DLSYM_ARG(curl_multi_cleanup),
626 DLSYM_ARG(curl_multi_info_read),
627 DLSYM_ARG(curl_multi_init),
628 DLSYM_ARG(curl_multi_remove_handle),
629 DLSYM_ARG(curl_multi_setopt),
630 DLSYM_ARG(curl_multi_socket_action),
631 DLSYM_ARG(curl_slist_append),
632 DLSYM_ARG(curl_slist_free_all));
633 #else
634 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
635 "libcurl support is not compiled in.");
636 #endif
637 }