FCGX_Accept_r失败并返回-88返回码

时间:2016-02-23 10:20:01

标签: c sockets fastcgi

我正在用c ++编写一个fastcgi应用程序,并且正在努力退出FCGX_Accept_r()而没有阻塞,返回值为" -88"。经过大量的谷歌搜索后,我发现你必须正确初始化FCGX结构,更正uds套接字上的umask,不要忘记FCGX_Init()并检查FCGX_Init()和FCGX_InitRequest()的返回码。

我已经为我的程序做了以上所有事情,结果仍然相同。

所以我试图在fcgi网站上构建示例C app:http://www.fastcgi.com/devkit/examples/threaded.c  它会立即退出。

奇怪的是,他们没有打开任何插座。所以我添加了几行 - 打印出FCGX_Accept_()返回值并打开一个uds套接字。

以下是生成的代码:

/*
 * threaded.c -- A simple multi-threaded FastCGI application.
 */

#ifndef lint
static const char rcsid[] = "$Id: threaded.c,v 1.9 2001/11/20 03:23:21 robs Exp $";
#endif /* not lint */

#include "fcgi_config.h"

#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdio.h>
#include "fcgiapp.h"


#define THREAD_COUNT 20

static int counts[THREAD_COUNT];

static void *doit(void *a)
{
    int rc, i, thread_id = (int)a;
    pid_t pid = getpid();
    FCGX_Request request;
    char *server_name;

    FCGX_InitRequest(&request, 0, 0);

    for (;;)
    {
        static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
        static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER;

        /* Some platforms require accept() serialization, some don't.. */
        pthread_mutex_lock(&accept_mutex);
        rc = FCGX_Accept_r(&request);
        pthread_mutex_unlock(&accept_mutex);

        if (rc < 0) {
        printf("failed accept: %d\n", rc);
            break;
    }

        server_name = FCGX_GetParam("SERVER_NAME", request.envp);

        FCGX_FPrintF(request.out,
            "Content-type: text/html\r\n"
            "\r\n"
            "<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>"
            "<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>"
            "Thread %d, Process %ld<p>"
            "Request counts for %d threads running on host <i>%s</i><p><code>",
            thread_id, pid, THREAD_COUNT, server_name ? server_name : "?");

        sleep(2);

        pthread_mutex_lock(&counts_mutex);
        ++counts[thread_id];
        for (i = 0; i < THREAD_COUNT; i++)
            FCGX_FPrintF(request.out, "%5d " , counts[i]);
        pthread_mutex_unlock(&counts_mutex);

        FCGX_Finish_r(&request);
    }

    return NULL;
}

int main(void)
{
    int i;
    pthread_t id[THREAD_COUNT];

    FCGX_Init();

    umask(0);
    FCGX_OpenSocket("/tmp/fcgi.sock", 10);

    for (i = 1; i < THREAD_COUNT; i++)
        pthread_create(&id[i], NULL, doit, (void*)i);

    doit(0);

    return 0;
}

猜猜:

# gcc fcgi.c -lpthread -lfcgi         
# ./a.out
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -88
failed accept: -failed accept: -88

有人可以指出我做错了什么吗? 谢谢!

更新

有趣的是,这里的例子https://habrahabr.ru/post/154187/起作用。我并没有看到太大的不同。

1 个答案:

答案 0 :(得分:0)

问题在于他们使用的示例

FCGX_InitRequest(&request, 0, 0);

而第二个参数必须是套接字ID。很好的例子,伙计。