curl-util.c revision 23e096cc60eb73f24b812e4ceba7c4c2c141e2db
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering This file is part of systemd.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Copyright 2014 Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is free software; you can redistribute it and/or modify it
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering under the terms of the GNU Lesser General Public License as published by
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (at your option) any later version.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is distributed in the hope that it will be useful, but
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Lesser General Public License for more details.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering You should have received a copy of the GNU Lesser General Public License
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void curl_glue_check_finished(CurlGlue *g) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering g->on_finished(g, msg->easy_handle, msg->data.result);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering translated_fd = PTR_TO_FD(hashmap_get(g->translate_fds, FD_TO_PTR(fd)));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if ((revents & (EPOLLIN|EPOLLOUT)) == (EPOLLIN|EPOLLOUT))
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_multi_socket_action(g->curl, translated_fd, action, &k) < 0) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_debug("Failed to propagate IO event.");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int curl_glue_socket_callback(CURLM *curl, curl_socket_t s, int action, void *userdata, void *socketp) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_event_source_set_enabled(io, SD_EVENT_OFF);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering hashmap_remove(g->translate_fds, FD_TO_PTR(fd));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = hashmap_ensure_allocated(&g->ios, &trivial_hash_ops);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = hashmap_ensure_allocated(&g->translate_fds, &trivial_hash_ops);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (sd_event_source_set_io_events(io, events) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (sd_event_source_set_enabled(io, SD_EVENT_ON) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* When curl needs to remove an fd from us it closes
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering * the fd first, and only then calls into us. This is
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering * nasty, since we cannot pass the fd on to epoll()
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering * anymore. Hence, duplicate the fds here, and keep a
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering * copy for epoll which we control after use. */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (sd_event_add_io(g->event, &io, fd, events, curl_glue_on_io, g) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_event_source_set_description(io, "curl-io");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = hashmap_put(g->ios, FD_TO_PTR(s), io);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = hashmap_put(g->translate_fds, FD_TO_PTR(fd), FD_TO_PTR(s));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int curl_glue_on_timer(sd_event_source *s, uint64_t usec, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_multi_socket_action(g->curl, CURL_SOCKET_TIMEOUT, 0, &k) != CURLM_OK) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_debug("Failed to propagate timeout.");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int curl_glue_timer_callback(CURLM *curl, long timeout_ms, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (sd_event_source_set_enabled(g->timer, SD_EVENT_OFF) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering usec = now(clock_boottime_or_monotonic()) + (usec_t) timeout_ms * USEC_PER_MSEC + USEC_PER_MSEC - 1;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (sd_event_source_set_time(g->timer, usec) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (sd_event_source_set_enabled(g->timer, SD_EVENT_ONESHOT) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (sd_event_add_time(g->event, &g->timer, clock_boottime_or_monotonic(), usec, 0, curl_glue_on_timer, g) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_event_source_set_description(g->timer, "curl-timer");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering while ((io = hashmap_steal_first(g->ios))) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering hashmap_remove(g->translate_fds, FD_TO_PTR(fd));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringint curl_glue_new(CurlGlue **glue, sd_event *event) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_multi_setopt(g->curl, CURLMOPT_SOCKETDATA, g) != CURLM_OK)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_multi_setopt(g->curl, CURLMOPT_SOCKETFUNCTION, curl_glue_socket_callback) != CURLM_OK)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_multi_setopt(g->curl, CURLMOPT_TIMERDATA, g) != CURLM_OK)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_multi_setopt(g->curl, CURLMOPT_TIMERFUNCTION, curl_glue_timer_callback) != CURLM_OK)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringint curl_glue_make(CURL **ret, const char *url, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* curl_easy_setopt(c, CURLOPT_VERBOSE, 1L); */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_easy_setopt(c, CURLOPT_URL, url) != CURLE_OK) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_easy_setopt(c, CURLOPT_PRIVATE, userdata) != CURLE_OK) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering useragent = strjoina(program_invocation_short_name, "/" PACKAGE_VERSION);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_easy_setopt(c, CURLOPT_USERAGENT, useragent) != CURLE_OK) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L) != CURLE_OK) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (curl_multi_add_handle(g->curl, c) != CURLM_OK)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringvoid curl_glue_remove_and_free(CurlGlue *g, CURL *c) {
assert(g);
if (g->curl)
struct curl_slist *l;
if (!first)
return NULL;
return NULL;
struct curl_slist *n;
n = curl_slist_append(l, i);
return NULL;
const char *p = contents;
size_t l;
if (sz < l)
sz -= l;
sz--;
sz--;
return -ENOMEM;
*value = s;
time_t v;
assert(t);
return -errno;
return -EINVAL;
return -EINVAL;