Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux fork()之后的libCurl SSL错误_Linux_Curl_Fork_Libcurl_Fuse - Fatal编程技术网

Linux fork()之后的libCurl SSL错误

Linux fork()之后的libCurl SSL错误,linux,curl,fork,libcurl,fuse,Linux,Curl,Fork,Libcurl,Fuse,我正在开发一个FUSE驱动程序,当我将其作为守护进程运行时(不带-f或-d标志),通过libcurl发出的所有https请求都会失败。我可以通过发出https请求、分叉并返回父进程,然后从新进程发出第二个请求来重现错误。如果删除fork调用或发出http请求,则不会出现错误 我现在正在做一个官方的bug报告,但是有人知道我如何才能让它工作吗 以下是我的代码和(日志文件)输出: 注意:如果运行我的程序,请将管道连接到/dev/null,因为默认情况下libcurl会将收到的缓冲区发送到stdout

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

我现在正在做一个官方的bug报告,但是有人知道我如何才能让它工作吗

以下是我的代码和(日志文件)输出:

注意:如果运行我的程序,请将管道连接到/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;
}

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

要实现这一点,您需要在
fork
之前调用
curl\u global\u cleanup
并在
fork
之后再次调用
curl\u global\u init
。libcurl邮件列表中有人提到,在初始化需要初始化的库之后调用
fork
时,这是一个常见问题。

对于其他语言中存在相同问题的库(在我的例子中是perl),请注意,似乎只有在nss而不是openssl中链接curl时才会出现问题。因此,您不妨重新编译您的curl以仅使用openssl。此解决方案可以,但会使curl在fork之后不可用于父级。是否有一种解决方案可以让父对象在fork之后继续工作和使用curl?(例如,一个需要使用curl记录一些数据的进程,但它将使用culr记录数据的childs分叉)这节省了我的时间。谢谢
$ 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.