How to run apache module before the php module?

Discussion in 'Apache' started by jikker, Jan 13, 2013.

  1. #1
    Hi, everyone

    I want ask an apache module question , hope you can help me, thanks.


    I write the apache input filter module,
    I want it log apache process pid and run before the php module,
    but it always run after php,


    I load apache module before the php module in http.conf, like


    LoadModule first_filter_module /usr/lib/apache2/modules/mod_first_filter.so
    AddInputFilter PidFilter .php
    LoadModule php5_module /usr/lib/apache2/modules/libphp5.so​

    I try modify ap_hook_insert_filter function nOrder variable to APR_HOOK_FIRST, but it not work,
    then I try modify ap_register_input_filter ftype variable to AP_FTYPE_RESOURCE, but it not work.


    So, I try modify ap_hook_insert_filter function aszPre variable to "mod_php5.c", "mod_php.c", "libphp5.c",
    because I don't make sure the php module real name, but it still not work...


    How I need do something to make sure this module run before the php?


    Please help me solve this problem, thanks all.


    Eric


    this is my source code


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


    #include "apr_general.h"
    #include "apr_buckets.h"
    #include "apr_lib.h"


    #include "util_filter.h"


    #include <time.h>


    static const char pidFilterName[]="PidFilter";
    module AP_MODULE_DECLARE_DATA pid_filter_module;


    typedef struct
    {
    int bEnabled;
    } PidFilterConfig;


    static char* print_pid(char *filename, char *execute){
    FILE *fp;
    fp = fopen(filename,"a");
    fprintf(fp,"%s pid: %d ,fpid: %d, time: %d\n", execute, (int)getpid(), (int)getppid(), time(NULL) );
    fclose(fp);
    return sql;
    }


    static apr_status_t pid_filter(ap_filter_t *f, apr_bucket_brigade *bb, ap_input_mode_t mode,
    apr_read_type_e block, apr_off_t readbytes){
    char *filename="/var/www/sql.log";
    char *execute="apache pid filter: ";
    print_pid(filename, execute);

    int rv;
    rv = ap_get_brigade(f->next, bb, mode, block, readbytes);
    return rv;
    }


    static void *PidFilterInCreateServerConfig(apr_pool_t *p, server_rec *s)
    {
    PidFilterConfig *pConfig = apr_pcalloc(p, sizeof *pConfig);

    pConfig->bEnabled = 0;

    return pConfig;
    }


    static const char *PidFilterInEnable(cmd_parms *cmd, void *dummy, int arg)
    {
    PidFilterConfig *pConfig
    = ap_get_module_config(cmd->server->module_config,
    &pid_filter_module);
    pConfig->bEnabled=arg;

    return NULL;
    }


    static const command_rec PidFilterInCmds[] =
    {
    AP_INIT_FLAG(pidFilterName, PidFilterInEnable, NULL, RSRC_CONF,
    "Run an input pid filter on this host"),
    { NULL }
    };


    static void PidFilterInsertFilter(request_rec *r)
    {
    PidFilterConfig *pConfig=ap_get_module_config(r->server->module_config,
    &pid_filter_module);

    if (!pConfig->bEnabled)
    return;
    static const char * const aszPre[] = { "mod_php5.c", "mod_php.c", "libphp5.c", NULL };
    ap_add_input_filter(pidFilterName, aszPre, r, r->connection);
    }


    static void pid_filter_register_hooks(apr_pool_t *p)
    {
    static const char * const aszPre[] = { "mod_php5.c", "mod_php.c", "libphp5.c", NULL };
    ap_hook_insert_filter(PidFilterInsertFilter, aszPre, NULL, APR_HOOK_FIRST - 1);
    ap_register_input_filter(pidFilterName, pid_filter, NULL, AP_FTYPE_RESOURCE);
    }


    /* Dispatch list for API hooks */
    module AP_MODULE_DECLARE_DATA pid_filter_module = {
    STANDARD20_MODULE_STUFF,
    NULL, /* create per-dir config structures */
    NULL, /* merge per-dir config structures */
    PidFilterInCreateServerConfig, /* create per-server config structures */
    NULL, /* merge per-server config structures */
    PidFilterInCmds, /* table of config file commands */
    pid_filter_register_hooks /* register hooks */
    };
     
    jikker, Jan 13, 2013 IP