我正在尝试动态链接 libcurl,但在 easy_performing 时出现 Segfault。
以下是全局范围声明:
#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 *);
然后我动态链接它:
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;
}
在这一点上它不会退出或失败。
所以这里是节目开始的地方。当像这样设置 curlopts 时,我没有收到错误:
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;
}
但是,当easy_performing我得到段错误:
if ( (res = (*curl_easy_perform)(curl)) != CURLE_OK ){
return -1;
}
我很确定这个错误很容易执行,但我决定继续生成一个 core_dump。当我 gdb 这个核心时,我实际上在 easy_init 上得到了一个错误:
程序以信号 11 终止,分段错误。#0 0x00007f3d2f9cd3f8 在 curl_easy_init () 来自 curlapp
任何人都知道这里可能出了什么问题?
提前致谢!
[编辑]
我已经删除了 curl_easy_perform 块并且程序运行正常。我不确定它失败是因为 curl_easy_perform() 还是仅仅因为在这个函数中发生了事情。换句话说,它可能意味着 'init' 和 'setopts' 仅适用于 easy_perform。(这只是一个猜测)
[编辑2]
更改 dlsym 块:
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");
在函数调用中:
if ( curl_easy_setopt(curl, CURLOPT_POST, 1) != CURLE_OK) {
if ( DEBUG_DETAILS ) vTrace("curl_easy_setopt(curl, CURLOPT_POST, 1) failed");
return -1;
}
但是我仍然在 curl_easy_init() 处遇到 Segfault。
有人能帮我吗?