在Apache模块中使用libcurl进行https调用时出现分段错误

时间:2018-10-22 15:13:45

标签: apache libcurl apache-modules

我正在编写一个apache模块,该模块调用HTTPs URL。当以多进程模式启动apache服务器(使用apachectl start)时,curl调用失败,并出现分段错误。

相同的代码在以下两种情况下均有效

  • 使用httpd -X在单进程模式下启动apache
  • 拨打http通话而不是https通话

以下是该模块的代码

#include <curl/curl.h>
#include "httpd.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_log.h"

static apr_status_t test_curl_call(request_rec *r) {
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "CURL call failed: %s", curl_easy_strerror(res));
        }
        curl_easy_cleanup(curl);
    }
    return OK;
}

static void register_hooks(apr_pool_t *pool) {
    ap_hook_post_read_request(test_curl_call, NULL, NULL, APR_HOOK_FIRST);
}

AP_DECLARE_MODULE(curl_example) = {
        STANDARD20_MODULE_STUFF, NULL, NULL, NULL, NULL, NULL,
        register_hooks
};

以下是我在日志中看到的错误

[Mon Oct 22 20:32:57.306320 2018] [core:notice] [pid 1392] AH00052: child pid 2211 exit signal Segmentation fault (11)

然后是CLion所示的堆栈

_dispatch_queue_push_queue 0x00007fff8cee26d7
<unknown> 0x00007ffffffffff8
_dispatch_queue_wakeup_with_qos_slow 0x00007fff8cee0b06
_dispatch_mach_msg_send 0x00007fff8cee713f
dispatch_mach_send 0x00007fff8cee68dc
_xpc_connection_send_message_with_reply_f 0x000000010220ac8b
xpc_connection_send_message_with_reply_sync 0x000000010220ab92
__78-[CFPrefsPlistSource sendRequestNewDataMessage:toConnection:retryCount:error:]_block_invoke 0x00007fff958c2e86
CFPREFERENCES_IS_WAITING_FOR_CFPREFSD 0x00007fff9586dea6
-[CFPrefsPlistSource sendRequestNewDataMessage:toConnection:retryCount:error:] 0x00007fff958c2dfa
__50-[CFPrefsPlistSource alreadylocked_requestNewData]_block_invoke 0x00007fff958c2cfb
_CFPrefsWithDaemonConnection 0x00007fff9586e9bd
-[CFPrefsPlistSource alreadylocked_requestNewData] 0x00007fff958c2c1b
_copyValueForKey 0x00007fff958a1004
-[CFPrefsPlistSource copyValueForKey:] 0x00007fff958a0ef3
___CFPreferencesCopyValueWithContainer_block_invoke 0x00007fff958a0ea0
+[CFPrefsSource withSourceForIdentifier:user:byHost:container:perform:] 0x00007fff9586b3dd
_CFPreferencesCopyValueWithContainer 0x00007fff958a0e3c
_SSLContextReadDefault 0x000000010258dabe
__pthread_once_handler 0x00007fff8d892bf6
_os_once 0x00007fff841f2fc4
pthread_once 0x00007fff8d892b95
SSLCreateContextWithRecordFuncs 0x000000010258bfe9
SSLCreateContext 0x000000010258be74
darwinssl_connect_common 0x00007fff9577fb99
Curl_ssl_connect_nonblocking 0x00007fff9577ef9e
Curl_http_connect 0x00007fff95747e69
Curl_protocol_connect 0x00007fff957563df
multi_runsingle 0x00007fff957689d3
curl_multi_perform 0x00007fff95768656
curl_easy_perform 0x00007fff95761b10

test_curl_call mod_curl_example.c:15

ap_run_post_read_request 0x0000000102062f2c
ap_read_request 0x0000000102062afe
ap_process_http_connection 0x0000000102078cb0
ap_run_process_connection 0x0000000102056b7e
child_main 0x000000010228c81e
make_child 0x000000010228c30f
startup_children 0x000000010228c368
prefork_run 0x000000010228b5ce
ap_run_mpm 0x0000000102058ff7
main 0x000000010204d56a
start 0x00007fff9839c5ad

要使libcurl在apache模块中工作还需要做其他事情吗?

0 个答案:

没有答案