fork后的libCurl SSL错误() [英] libCurl SSL error after fork()

查看:1091
本文介绍了fork后的libCurl SSL错误()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a FUSE driver and when I run it as a daemon (without the -f or -d flags) all https request made through libcurl fail. I was able to reproduce the error by making an https request, forking and returning the parent process, and then making a second request from the new process. If I remove the fork call or make an http request there are no errors.

我正在做一个官方的bug报告,但是有谁知道我怎么能使它工作?

I'm making an official bug report right now, but does anyone know how I can make it work?

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

Here's my code and the (logfile) output:

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

Note: if you run my program, pipe to /dev/null because libcurl sends the received buffer to stdout by default.

#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;
}

...和输出:

$ 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 version 7.29.0,(最新的)openssl version 1.0 .1e,我使用内核版本3.7.4运行Fedora 18.

I'm using (the latest) libcurl version 7.29.0, (the latest) openssl version 1.0.1e, and I'm running Fedora 18 with kernel version 3.7.4.

推荐答案

<$ c之后再次之前调用 curl_global_cleanup 之前和 curl_global_init $ c> fork 。 libcurl邮件列表中的某个人提到在初始化需要初始化的库后调用 fork 时这是一个常见的问题。

To make this work, you need to call curl_global_cleanup before fork and curl_global_init again after fork. Someone in the libcurl mailing list mentioned that this is a common problem when calling fork after initializing libraries that need to be initialized.

这篇关于fork后的libCurl SSL错误()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆