Error while dynamically linking LibCURL

610 views Asked by At

I'm trying to dynamically link libcurl, but I'm getting Segfault when easy_performing.
Here are the global scope declaration:

#define LIBCURL_PATH_64      "/usr/lib64/libcurl.so"
void *hLibCurl = NULL;
CURL *curl;
CURL* (*curl_easy_init)(void);
CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...);
CURLcode (*curl_easy_perform)(CURL *);
const char* (*curl_easy_strerror)(CURLcode);
struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *);

Then I dynamically linked it:

  hLibCurl = dlopen(LIBCURL_PATH_64, RTLD_NOW);
  *(void**) (&curl_easy_init) = dlsym(hLibCurl, "curl_easy_init");
  *(void**) (&curl_slist_append) = dlsym(hLibCurl, "curl_slist_append");
  *(void**) (&curl_easy_setopt) = dlsym(hLibCurl, "curl_easy_setopt");
  *(void**) (&curl_easy_strerror) = dlsym(hLibCurl, "curl_easy_strerror");
  *(void**) (&curl_easy_perform) = dlsym(hLibCurl, "curl_easy_perform");
  if ( (curl = (*curl_easy_init)()) == NULL ){
    LogMsg("CurlInit easy_init failed");
    return -1;
  }

It doesn't exits or fails in this point.
So here's where the show starts. When setting curlopts like this, I got no error:

  if ((*curl_easy_setopt)(curl, CURLOPT_POST, 1) != CURLE_OK) {
    LogMsg("curl_easy_setopt(curl, CURLOPT_POST, 1) failed");
    return -1;
  }
  if ((*curl_easy_setopt)(curl, CURLOPT_URL, "https://ws.service.com.br/services-nac/services/SomeService?wsdl") != CURLE_OK) {
    LogMsg("curl_easy_setopt(curl, CURLOPT_URL) failed");
    return -1;
  }
  if ((*curl_easy_setopt)(curl, CURLOPT_PORT, 443) != CURLE_OK) {
    LogMsg("curl_easy_setopt(curl, CURLOPT_PORT, 443) failed");
    return -1;
  }

BUT, when easy_performing I got segfault:

   if ( (res = (*curl_easy_perform)(curl)) != CURLE_OK ){
      return -1;
   }

I'm pretty sure the error is on easy perform, but I decided to go ahead and generate a core_dump. When I gdb this core, I actually got an error on easy_init:

Program terminated with signal 11, Segmentation fault. #0 0x00007f3d2f9cd3f8 in curl_easy_init () from curlapp

Anyone has an idea of what can be wrong here?

Thanks in advance!


[EDIT]

I've removed the curl_easy_perform block and the program runs normally. I'm not sure If its failing because of curl_easy_perform() or just because is in this function that things happen. In other words, it can mean 'init' and 'setopts' are only applied on easy_perform. ( It's just a guess)

[EDIT2]

Changed dlsym block:

  curl_easy_init = 
    (CURL* (*)(void)) 
      dlsym(hLibCurl, "curl_easy_init");
  curl_slist_append = 
    (struct curl_slist*(*)(struct curl_slist *, const char *)) 
      dlsym(hLibCurl, "curl_slist_append");
  curl_easy_setopt = 
    (CURLcode (*)(CURL *, CURLoption, ...))
      dlsym(hLibCurl, "curl_easy_setopt");
  curl_easy_perform = 
    (CURLcode (*)(CURL *))
      dlsym(hLibCurl, "curl_easy_perform");

And in the function calls:

 if ( curl_easy_setopt(curl, CURLOPT_POST, 1) != CURLE_OK) {
    if ( DEBUG_DETAILS ) vTrace("curl_easy_setopt(curl, CURLOPT_POST, 1) failed");
    return -1;
  }

But I still getting Segfault at curl_easy_init().
Can someone help me?

1

There are 1 answers

0
Hans Z. On

Load and call curl_global_init first as documented in: http://curl.haxx.se/libcurl/c/curl_global_init.html