linux – fork()后的libCurl SSL错误

我正在开发一个FUSE驱动程序,当我将它作为守护进程运行时(没有-f或-d标志),通过libcurl发出的所有https请求都会失败.我能够通过发出https请求,分叉和返回父进程,然后从新进程发出第二个请求来重现错误.如果我删除fork调用或发出http请求,则没有错误.

我现在正在制作官方错误报告,但是有谁知道我怎么能让它发挥作用?

这是我的代码和(logfile)输出

注意:如果您运行我的程序,请管道到/ dev / null,因为libcurl认将接收到的缓冲区发送到stdout.

#include odo: remove
    static const char logfile[] = "/home/austin/megalog";
    FILE *f = fopen(logfile,"a");
    fwrite(str.data(),str.size(),1,f);
    fclose(f);
    cout << str;
}

int main(int argc,char *argv[])
{
    string url = "https://www.google.com/";
    char errBuf[1024];
    CURLcode err;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    CURL *handle = curl_easy_init();
    curl_easy_setopt(handle,CURLOPT_URL,url.c_str());
    curl_easy_setopt(handle,CURLOPT_ERRORBUFFER,errBuf);

    if ((err = curl_easy_perform(handle)))
    {
        log("first request Failed\n");
        return 1;
    }
    curl_easy_cleanup(handle);

    if(fork())
        return 0;

    handle = curl_easy_init();
    curl_easy_setopt(handle,errBuf);

    if ((err = curl_easy_perform(handle)))
    {
        log(string("curl error while sending: (") + to_string(err) + ") " + curl_easy_strerror(err) + "\n");
        log(errBuf);
    }
    else
        log("no error\n");

    return 0;
}

……和输出

$g++ -std=c++11 main.cpp -lcurl
$rm -f log
$./a.out > /dev/null
$cat log
curl error while sending: (35) SSL connect error
A PKCS #11 module returned CKR_DEVICE_ERROR,indicating that a problem has occurred with the token or slot.

我正在使用(最新的)libcurl版本7.29.0,(最新的)openssl版本1.0.1e,我正在使用内核版本3.7.4运行Fedora 18.

最佳答案
要使其工作,您需要在fork之前调用curl_global_cleanup,在fork之后再次调用curl_global_init. libcurl邮件列表中的某个人提到在初始化需要初始化的库之后调用fork时这是一个常见问题.

相关文章

在Linux上编写运行C语言程序,经常会遇到程序崩溃、卡死等异...
git使用小结很多人可能和我一样,起初对git是一无所知的。我...
1. 操作系统环境、安装包准备 宿主机:Max OSX 10.10.5 虚拟...
因为业务系统需求,需要对web服务作nginx代理,在不断的尝试...
Linux模块机制浅析 Linux允许用户通过插入模块,实现干预内核...
一、Hadoop HA的Web页面访问 Hadoop开启HA后,会同时存在两个...