From 3f7be35406b5049fea794bbb8b0ff30c5955e30a Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 29 Aug 2010 13:31:18 +0200 Subject: [PATCH] Fix segfault in ccurl_thread.c curl_global_init() is not thread-safe, it must be called at the start of main() --- src/ccurl_thread.c | 47 ++++++++++++++++++++++------------------------- src/conky.c | 12 ++++++++++++ 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/ccurl_thread.c b/src/ccurl_thread.c index 0dbe88e..f52a654 100644 --- a/src/ccurl_thread.c +++ b/src/ccurl_thread.c @@ -122,35 +122,32 @@ void ccurl_fetch_data(ccurl_location_t *curloc) chunk.memory = NULL; chunk.size = 0; - if (curl_global_init(CURL_GLOBAL_ALL) == 0) { - curl = curl_easy_init(); - if (curl) { - DBGP("reading curl data from '%s'", curloc->uri); - curl_easy_setopt(curl, CURLOPT_URL, curloc->uri); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk); - curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.0"); - - res = curl_easy_perform(curl); - if (res == CURLE_OK && chunk.size) { - long http_status_code; - - if(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status_code) == CURLE_OK && http_status_code == 200) { - timed_thread_lock(curloc->p_timed_thread); - (*curloc->process_function)(curloc->result, chunk.memory); - timed_thread_unlock(curloc->p_timed_thread); - } else { - NORM_ERR("curl: no data from server"); - } - free(chunk.memory); + curl = curl_easy_init(); + if (curl) { + DBGP("reading curl data from '%s'", curloc->uri); + curl_easy_setopt(curl, CURLOPT_URL, curloc->uri); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.0"); + + res = curl_easy_perform(curl); + if (res == CURLE_OK && chunk.size) { + long http_status_code; + + if(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status_code) == CURLE_OK && http_status_code == 200) { + timed_thread_lock(curloc->p_timed_thread); + (*curloc->process_function)(curloc->result, chunk.memory); + timed_thread_unlock(curloc->p_timed_thread); } else { NORM_ERR("curl: no data from server"); } - - curl_easy_cleanup(curl); + free(chunk.memory); + } else { + NORM_ERR("curl: no data from server"); } - curl_global_cleanup(); + + curl_easy_cleanup(curl); } } diff --git a/src/conky.c b/src/conky.c index 3144172..0154056 100644 --- a/src/conky.c +++ b/src/conky.c @@ -70,6 +70,9 @@ #ifdef XOAP #include #endif /* XOAP */ +#ifdef HAVE_CURL +#include +#endif /* local headers */ #include "core.h" @@ -5894,6 +5897,11 @@ int main(int argc, char **argv) tcp_portmon_set_max_connections(0); #endif +#ifdef HAVE_CURL + if(curl_global_init(CURL_GLOBAL_ALL)) + NORM_ERR("curl_global_init() failed, you may not be able to use curl variables"); +#endif + /* handle command line parameters that don't change configs */ #ifdef X11 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s) @@ -5985,6 +5993,10 @@ int main(int argc, char **argv) main_loop(); +#ifdef HAVE_CURL + curl_global_cleanup(); +#endif + #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) kvm_close(kd); #endif -- 1.7.9.5