Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

请教一个curl的问题

44 views
Skip to first unread message

阿水

unread,
Jun 4, 2008, 9:29:34 PM6/4/08
to
接手别人的一个程序,用了curl库。在调用curl_easy_perform( )函数后程序非正常退出,coredump了。
程序是多线程的,在每个线程里的curl_easy_perform( )函数调用之前,都有这两个调用:
curl_global_init(CURL_GLOBAL_ALL);
CURL *c = curl_easy_init();

我查了查curl的网站,说是curl_global_init是thread unsafe的,是不是不该这样调用curl_global_init,而应该在线程start之前?我试了试,放了几个地方都不行,虽然没有非正常退出,但是抛出未知异常,执行不到调用curl_easy_perform( )函数的地方了。
大牛们给点意见?谢谢!
--
孤舘燈青

野店雞號

旅枕夢殘


[m [37m※ 来源:·水木社区 http://newsmth.net·[FROM: 147.243.236.*] [m

努力----奋斗!

unread,
Jun 4, 2008, 9:34:21 PM6/4/08
to
把curl_global_init拿掉看看

【 在 UN733 (阿水) 的大作中提到: 】
: 接手别人的一个程序,用了curl库。在调用curl_easy_perform( )函数后程序非正常退出,coredump了。


: 程序是多线程的,在每个线程里的curl_easy_perform( )函数调用之前,都有这两个调用:
: curl_global_init(CURL_GLOBAL_ALL);

: ...................

--

[m [1;34m※ 来源:·水木社区 newsmth.net·[FROM: 219.143.222.*] [m

从头再来

unread,
Jun 4, 2008, 9:35:22 PM6/4/08
to
贴问题代码吧

【 在 UN733 (阿水) 的大作中提到: 】
: 接手别人的一个程序,用了curl库。在调用curl_easy_perform( )函数后程序非正常退出,coredump了。
: 程序是多线程的,在每个线程里的curl_easy_perform( )函数调用之前,都有这两个调用:
: curl_global_init(CURL_GLOBAL_ALL);
: ...................

--

[m [1;32m※ 来源:·水木社区 newsmth.net·[FROM: 60.247.104.*] [m

阿水

unread,
Jun 4, 2008, 10:19:50 PM6/4/08
to
下面是线程函数体。
去掉了一些无关的东西,大体结构是这样的。trace文件里有curlErrFlag = 0, 但是没有resultCode = 的信息,说明是curl_easy_perform( )出的错。而且负责通信的另一端的人说,命令是传送过去并执行了的。
大牛们看看?
void slccorCurl_c::PerformCurl(const char* url,string userpwd, string xmlbuffer , string protocol )
{
try
{
CPFDEF::TraceBuffer_c trace;
trace.SetPacketLevel( 5 );

struct memoryStruct response;
response.memory=NULL;
response.size = 0;

curl_global_init(CURL_GLOBAL_ALL);
CURL *c = curl_easy_init();

if( ! c ) {

CPFDEF::Error_c err(SLCCOR_CURL_INIT_EXCEPTION,
"Error during curl initialisation");
err.SetLocation(__FILE__, __LINE__);

CPFCEC::ErrorLog_c::Send( err );

}

int curlErrFlag = 0;

if (( curl_easy_setopt(c, CURLOPT_URL, url)) != CURLE_OK ) curlErrFlag = 1;
//...a lot of curl_easy_setopt calling
if (( curl_easy_setopt(c, CURLOPT_SSL_VERIFYHOST, 0))!= CURLE_OK ) curlErrFlag = 1;

trace << " curlErrFlag = " << curlErrFlag << endl << flush ;

if ( curlErrFlag == 1 )
{
result = SLCCOR_CURL_ERROR;
}
else
{

CURLcode resultCode = curl_easy_perform( c );


trace << " resultCode = " << resultCode << endl << flush ;

if ( resultCode == CURLE_COULDNT_CONNECT || resultCode == CURLE_COULDNT_RESOLVE_HOST )
{
result = SLCCOR_NO_NE_CONNECT ;
}

if(response.memory)
{
xmlResponse = response.memory;
}

free (response.memory);

curl_easy_cleanup( c );
}
}
catch (...) {
//...
}

}
【 在 shellcode (从头再来) 的大作中提到: 】
: 贴问题代码吧

努力----奋斗!

unread,
Jun 4, 2008, 10:27:21 PM6/4/08
to
curl_global_init
This function is not thread safe. You must not call it when any other
thread in the program (i.e. a thread sharing the same memory) is run-
ning. This doesn't just mean no other thread that is using libcurl.
Because curl_global_init() calls functions of other libraries that are
similarly thread unsafe, it could conflict with any other thread that
uses these other libraries.

See the description in libcurl(3) of global environment requirements
for details of how to use this function.

是不是这个函数只用在主线程里调一次?

【 在 UN733 (阿水) 的大作中提到: 】
: 下面是线程函数体。


: 去掉了一些无关的东西,大体结构是这样的。trace文件里有curlErrFlag = 0, 但是没有resultCode = 的信息,说明是curl_easy_perform( )出的错。而且负责通信的另一端的人说,命令是传送过去并执行了的。
: 大牛们看看?

阿水

unread,
Jun 4, 2008, 10:26:31 PM6/4/08
to
http://curl.haxx.se/libcurl/c/curl_easy_init.html 看到的:
If you did not already call curl_global_init(3), curl_easy_init(3) does it automatically. This may be lethal in multi-threaded cases, since curl_global_init(3) is not thread-safe, and it may result in resource problems because there is no corresponding cleanup.
估计是不行吧,因为我们的东西是多线程的。我试试看。
【 在 dcl (努力----奋斗!) 的大作中提到: 】
: 把curl_global_init拿掉看看

努力----奋斗!

unread,
Jun 4, 2008, 10:28:25 PM6/4/08
to
才看到这段,我的程序也是多线程,没有调用global_init,居然没事,看来rp不错:)

是不是应该在主线程里调用curl_global_init呢?
【 在 UN733 (阿水) 的大作中提到: 】
: 在http://curl.haxx.se/libcurl/c/curl_easy_init.html 看到的:


: If you did not already call curl_global_init(3), curl_easy_init(3) does it automatically. This may be lethal in multi-threaded cases, since curl_global_init(3) is not thread-safe, and it may result in resource problems because there is no correspondi

: 估计是不行吧,因为我们的东西是多线程的。我试试看。

阿水

unread,
Jun 5, 2008, 4:48:40 AM6/5/08
to
大家给点意见?

【 在 UN733 (阿水) 的大作中提到: 】
: 下面是线程函数体。
: 去掉了一些无关的东西,大体结构是这样的。trace文件里有curlErrFlag = 0, 但是没有resultCode = 的信息,说明是curl_easy_perform( )出的错。而且负责通信的另一端的人说,命令是传送过去并执行了的。
: 大牛们看看?
: ...................

--
孤舘燈青

野店雞號

旅枕夢殘


[m [32m※ 来源:·水木社区 http://newsmth.net·[FROM: 147.243.236.*] [m

阿水

unread,
Jun 5, 2008, 9:03:02 AM6/5/08
to
进一步的出错信息:
最后一句应该是关键点,就是curl_easy_perform( )函数关闭链接的时候出错了。但是这个“double free or corruption (fasttop): 0x084fa4a8”是什么意思呢?我把curl_global_init(CURL_GLOBAL_ALL) 移到了所有线程的初始化之前,而且改成了curl_global_init(CURL_GLOBAL_SSL),因为我们的是linux平台。
* Trying 10.8.15.58... * connected
* Connected to 10.8.15.58 (10.8.15.58) port 80
* Server auth using Basic with user 'trace1'
> POST /cgi-bin/remcliexec.tcl HTTP/1.1
Authorization: Basic dHJhY2UxOkhscjc4OVNnc24=
Host: 10.8.15.58
Pragma: no-cache
Accept: */*
Content-Length: 294
Content-Type: application/x-www-form-urlencoded

execsh=mmi&cliLines=%3c%3fxml version=%221%2e0%22%3f%3e%3cCLIREQUEST Version=%221%2e0%22%3e%3cTRANSACTION Number=%221%22%3e%3 ivateimsi n112233445588888%3ar7508%3as0%3a1%3c%2fCMDTEXT%3e%3c%2fCMDREQUEST%3e%3c%2fTRANSACTION%3e%3c%2fCLIREQUEST%3e< HTTP/1
< Date: Thu, 05 Jun 2008 10:15:16 GMT
< Server: Apache/1.3.6 (Unix) mod_auth_pam/1.1.1 mod_ssl/2.3.11 OpenSSL/0.9.7d
< Transfer-Encoding: chunked
< Content-Type: text/html
* Connection #0 to host 10.8.15.58 left intact
* Closing connection #0
*** glibc detected *** double free or corruption (fasttop): 0x084fa4a8 ***

【 在 UN733 (阿水) 的大作中提到: 】
: 接手别人的一个程序,用了curl库。在调用curl_easy_perform( )函数后程序非正常退出,coredump了。
: 程序是多线程的,在每个线程里的curl_easy_perform( )函数调用之前,都有这两个调用:
: curl_global_init(CURL_GLOBAL_ALL);

: ...................

--
孤舘燈青

野店雞號

旅枕夢殘


[m [33m※ 来源:·水木社区 http://newsmth.net·[FROM: 125.70.82.*] [m

0 new messages