fork()后的libCurl SSL错误

use*_*954 5 linux curl fork fuse libcurl

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

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

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

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

#include <curl/curl.h>
#include <string>
#include <unistd.h>
#include <iostream>

using namespace std;

void log(string str)
{   //TODO: 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, CURLOPT_URL, url.c_str());
    curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, 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;
}
Run Code Online (Sandbox Code Playgroud)

......和输出:

$ 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.
Run Code Online (Sandbox Code Playgroud)

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