Ejemplo n.º 1
0
static int
request_close_(CONTAINER_REQUEST *me)
{
	FCGX_FClose(me->in);
	FCGX_FClose(me->err);
	FCGX_FClose(me->out);
	return 0;
}
Ejemplo n.º 2
0
/*
 *----------------------------------------------------------------------
 *
 * FCGX_Finish_r --
 *
 *      Finishes the current request from the HTTP server.
 *
 * Side effects:
 *
 *      Finishes the request accepted by (and frees any
 *      storage allocated by) the previous call to FCGX_Accept.
 *
 *      DO NOT retain pointers to the envp array or any strings
 *      contained in it (e.g. to the result of calling FCGX_GetParam),
 *      since these will be freed by the next call to FCGX_Finish
 *      or FCGX_Accept.
 *
 *----------------------------------------------------------------------
 */
void FCGX_Finish_r(FCGX_Request * reqDataPtr)
{
	int close;

	if (reqDataPtr == NULL) {
		return;
	}

	close = !reqDataPtr->keepConnection;

	/* This should probably use a 'status' member instead of 'in' */
	if (reqDataPtr->in) {
		close |= FCGX_FClose(reqDataPtr->err);
		close |= FCGX_FClose(reqDataPtr->out);

		close |= FCGX_GetError(reqDataPtr->in);
	}

	FCGX_Free(reqDataPtr, close);
}
Ejemplo n.º 3
0
int FCGI_fclose(FCGI_FILE *fp)
{
    int n = EOF;
    if(fp->stdio_stream) {
        n = fclose(fp->stdio_stream);
        fp->stdio_stream = NULL;
    } else if(fp->fcgx_stream) {
        n = FCGX_FClose(fp->fcgx_stream);
        fp->fcgx_stream = NULL;
    }
    if((fp != FCGI_stdin) && (fp != FCGI_stdout) && (fp != FCGI_stderr)) {
        free(fp);
    }
    return n;
}
Ejemplo n.º 4
0
FCGI_FILE *FCGI_freopen(const char *path, const char *mode,
                        FCGI_FILE *fp)
{
    if(fp->stdio_stream) {
        if(freopen(path, mode, fp->stdio_stream) == NULL)
            return NULL;
        else
            return fp;
    } else if(fp->fcgx_stream) {
        (void) FCGX_FClose(fp->fcgx_stream);
        fp->stdio_stream = fopen(path, mode);
        if(fp->stdio_stream == NULL)
            return NULL;
        else {
	    fp->fcgx_stream = NULL;
            return fp;
	}
    }
    return NULL;
}
Ejemplo n.º 5
0
void FastCgiDevice::close()
{
	// Warning!! Not recommended by FCGI documentation
	FCGX_FClose( m_stream );
}
int main (int argc, char **argv) {
    int fcgiSock = -1;
    FCGX_Stream *paramsStream;
    char *pool = NULL;
    char *content, *data;
    int type, count;
    struct json_object  *obj, *slaveobj;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Connect to fcgi server */
    fcgiSock = mp_fcgi_connect(fcgisocket);

    /* Prepare Begin Request */
    FCGI_BeginRequestBody body;
    body.roleB1 = 0x00;
    body.roleB0 = FCGI_RESPONDER;
    body.flags  = 0x000;
    memset(body.reserved, 0, sizeof(body.reserved));
    mp_fcgi_write(fcgiSock, 42, FCGI_BEGIN_REQUEST, (char*)&body, sizeof(body));

    /* Set FCGI Params */
    paramsStream = FCGX_CreateWriter(fcgiSock, 1, 8192, FCGI_PARAMS);
    mp_fcgi_putkv(paramsStream, "REQUEST_METHOD", "GET");
    mp_fcgi_putkv(paramsStream, "SCRIPT_NAME", query);
    mp_fcgi_putkv(paramsStream, "SCRIPT_FILENAME", query);
    mp_fcgi_putkv(paramsStream, "QUERY_STRING", "json&");
    FCGX_FClose(paramsStream);
    FCGX_FreeStream(&paramsStream);

    /* Start request processing by stdin closing */
    mp_fcgi_write(fcgiSock, 42, FCGI_STDIN, NULL, 0);

    /* Wait for answer */
    data = NULL;
    do {
        content = NULL;
        type = mp_fcgi_read(fcgiSock, &content, &count);
        if (type == FCGI_STDOUT)
            data = content;
        else if (content)
            free(content);
    } while (type != FCGI_END_REQUEST);

    /* Skip http headers */
    content = data;
    do {
        (void)strsep(&data, "\n");
    } while (data && data[0] != '\r');

    /* Parse JSON */
    obj = mp_json_tokener_parse(data);

    /* Read pool name */
    mp_json_object_object_get(obj, "pool", &slaveobj);
    pool = mp_strdup(json_object_get_string(slaveobj));

    /* Read accepted connections */
    mp_json_object_object_get(obj, "accepted conn", &slaveobj);
    mp_perfdata_int("accepted_conn", json_object_get_int(slaveobj), "c", NULL);

    /* Read listen queue */
    mp_json_object_object_get(obj, "listen queue", &slaveobj);
    mp_perfdata_int("listen_queue", json_object_get_int(slaveobj), "", NULL);

    /* Read idle processes */
    mp_json_object_object_get(obj, "idle processes", &slaveobj);
    mp_perfdata_int("idle_processes", json_object_get_int(slaveobj), "", NULL);

    /* Read active processes */
    mp_json_object_object_get(obj, "active processes", &slaveobj);
    mp_perfdata_int("active_processes", json_object_get_int(slaveobj), "", NULL);

    free(content);
    json_object_put(obj);

    ok("PHP-FPM: %s", pool);
}
Ejemplo n.º 7
0
int main(int argc, char **argv, char **envp)
{
    int count;
    FCGX_Stream *paramsStream;
    fd_set readFdSet, writeFdSet;
    int numFDs, selectStatus;
    unsigned char headerBuff[8];
    int headerLen, valueLen;
    char *equalPtr;
    FCGI_BeginRequestRecord beginRecord;
    int	doBind, doStart, nServers;
    char appPath[MAXPATHLEN], bindPath[MAXPATHLEN];

    if(ParseArgs(argc, argv, &doBind, &doStart,
		   (char *) &bindPath, (char *) &appPath, &nServers)) {
	fprintf(stderr,
"Usage:\n"
"    cgi-fcgi -f <cmdPath> , or\n"
"    cgi-fcgi -connect <connName> <appPath> [<nServers>] , or\n"
"    cgi-fcgi -start -connect <connName> <appPath> [<nServers>] , or\n"
"    cgi-fcgi -bind -connect <connName> ,\n"
"where <connName> is either the pathname of a UNIX domain socket\n"
"or (if -bind is given) a hostName:portNumber specification\n"
"or (if -start is given) a :portNumber specification (uses local host).\n");
	exit(1);
    }
    if(doBind) {
        appServerSock = OS_FcgiConnect(bindPath);
    }
    if(doStart && (!doBind || appServerSock < 0)) {
        FCGI_Start(bindPath, appPath, nServers);
        if(!doBind) {
            exit(0);
        } else {
            appServerSock = OS_FcgiConnect(bindPath);
	}
    }
    if(appServerSock < 0) {
        fprintf(stderr, "Could not connect to %s\n", bindPath);
        exit(errno);
    }
    /*
     * Set an arbitrary non-null FCGI RequestId
     */
    requestId = 1;
    /*
     * XXX: Send FCGI_GET_VALUES
     */

    /*
     * XXX: Receive FCGI_GET_VALUES_RESULT
     */

    /*
     * Send FCGI_BEGIN_REQUEST (XXX: hack, separate write)
     */
    beginRecord.header = MakeHeader(FCGI_BEGIN_REQUEST, requestId,
            sizeof(beginRecord.body), 0);
    beginRecord.body = MakeBeginRequestBody(FCGI_RESPONDER, TRUE);
    count = write(appServerSock, &beginRecord, sizeof(beginRecord));
    if(count != sizeof(beginRecord)) {
        exit(errno);
    }
    /*
     * Send environment to the FCGI application server
     */
    paramsStream = CreateWriter(appServerSock, requestId, 8192, FCGI_PARAMS);
    for( ; *envp != NULL; envp++) {
        equalPtr = strchr(*envp, '=');
        if(equalPtr  == NULL) {
            exit(1000);
        }
        valueLen = strlen(equalPtr + 1);
        FCGIUtil_BuildNameValueHeader(
                equalPtr - *envp,
                valueLen,
                &headerBuff[0],
                &headerLen);
        if(FCGX_PutStr((char *) &headerBuff[0], headerLen, paramsStream) < 0
                || FCGX_PutStr(*envp, equalPtr - *envp, paramsStream) < 0
                || FCGX_PutStr(equalPtr + 1, valueLen, paramsStream) < 0) {
            exit(FCGX_GetError(paramsStream));
        }
    }
    FCGX_FClose(paramsStream);
    FreeStream(&paramsStream);
    /*
     * Perform the event loop until AppServerReadHander sees FCGI_END_REQUEST
     */
    fromWS.stop = fromWS.next = &fromWS.buff[0];
    webServerReadHandlerEOF = FALSE;
    FD_ZERO(&readFdSet);
    FD_ZERO(&writeFdSet);
    numFDs = max(appServerSock, STDIN_FILENO) + 1;
    SetFlags(appServerSock, O_NONBLOCK);
    for(;;) {
        if((fromWS.stop == fromWS.next) && !webServerReadHandlerEOF) {
            FD_SET(STDIN_FILENO, &readFdSet);
        } else {
            FD_CLR(STDIN_FILENO, &readFdSet);
        }
        if(fromWS.stop != fromWS.next) {
            FD_SET(appServerSock, &writeFdSet);
        } else {
            FD_CLR(appServerSock, &writeFdSet);
        }
        FD_SET(appServerSock, &readFdSet);
        selectStatus = select(numFDs, &readFdSet, &writeFdSet, NULL, NULL);
        if(selectStatus < 0) {
            exit(errno);
        }
        if(selectStatus == 0) {
            /*
             * Should not happen, no select timeout.
             */
            continue;
        }
        if(FD_ISSET(STDIN_FILENO, &readFdSet)) {
            WebServerReadHandler();
        }
        if(FD_ISSET(appServerSock, &writeFdSet)) {
            AppServerWriteHandler();
        }
        if(FD_ISSET(appServerSock, &readFdSet)) {
            AppServerReadHandler();
	}
        if(exitStatusSet) {
            exit(exitStatus);
	}
    } 
}