0

ソケットを使用して他のサーバーに接続する Apache モジュールを作成しようとしています。うまく機能し、データを取得できますが、3 つの問題に遭遇しました。

  1. サーバーとの接続を維持できません (リクエストごとに自動終了します)。
  2. error log にリクエストごとに 2 つのエラーが表示されますAH00052: child pid 7970 exit signal Segmentation fault (11)
  3. ブラウザで f5 キーを押し続けると、「データが受信されていません」というエラーが表示されます。

これは私のモジュールのコードです:

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/select.h>

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"

static int sockfd = -1;
static struct sockaddr_in saddr;
/* The sample content handler */
static int search_handler(request_rec *r)
{
    r->content_type = "text/html";

    ap_rprintf(r,"sockfd = %d<br>", sockfd);
    if(sockfd == -1){
            sockfd = socket(AF_INET, SOCK_STREAM, 0);
            struct hostent *server = gethostbyname("127.0.0.1");
            if(server == NULL ) return DECLINED;
            bzero((char *) &saddr, sizeof(saddr));
            saddr.sin_family = AF_INET;
            bcopy((char *)server->h_addr, (char *)&saddr.sin_addr.s_addr,server->h_length);
            saddr.sin_port = htons(9999);
            if(sockfd == -1) return DECLINED;
            if(connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0){
                    ap_rputs("Can't connect.\n", r);
                    return OK;
            }
    }
    send(sockfd, r->args, strlen(r->args), 0);
    fd_set read_sd;
    FD_ZERO(&read_sd);
    FD_SET(sockfd, &read_sd);
    int sel = select(sockfd + 1, &read_sd, 0, 0, 0);
    if(sel < 0) {close(sockfd);return DECLINED;}
    if( sel == 0) {ap_rprintf(r, "time out."); return OK;}
    char buf[5000];
    if(recv(sockfd, buf, 5000, 0) <= 0) return DECLINED;
    ap_rprintf(r, "%s<br>%d", buf, sockfd);

    return OK;
}

static void search_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(search_handler, NULL, NULL, APR_HOOK_LAST);
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA search_module = {
STANDARD20_MODULE_STUFF,
NULL,                  /* create per-dir    config structures */
NULL,                  /* merge  per-dir    config structures */
NULL,                  /* create per-server config structures */
NULL,                  /* merge  per-server config structures */
NULL,                  /* table of config file commands       */
search_register_hooks  /* register hooks                      */
};

どうすればこれを解決できますか?

4

1 に答える 1

1

明確な答えではありませんが、モジュール内のリソース管理にはapache プールを使用する必要があると思います。

于 2012-08-06T14:31:12.340 に答える