0

I am trying to write a sample Apache module to read config file whose file path is specified in httpd.conf like that:

<Location ~ /(?!.*\.(png|jpeg|jpg|gif|bmp|tif)$)>
        SetInputFilter SAMPLE_INPUT_FILTER
        SetOutputFilter SAMPLE_OUTPUT_FILTER
        ConfigFilePath "/etc/httpd/conf.d/sample/sample.config"
</Location>

At command record structure, I do:

static const command_rec config_check_cmds[] =
{
    AP_INIT_TAKE1( "ConfigFilePath", read_config_file, NULL, OR_ALL, "sample config"),
    { NULL }
};

I also set:

module AP_MODULE_DECLARE_DATA SAMPLE_module = {
    STANDARD20_MODULE_STUFF, 
    create_dir_config,          /* create per-dir   config structures */
    NULL,                   /* merge per-dir    config structures */
    NULL,                   /* create per-server config structures */
    NULL,                   /* merge per-server config structures */
    config_check_cmds,              /* table of config file commands */
    sample_register_hooks   /* register hooks */
};

I could read config file path successfully. And now I want to check that if "ConfigFilePath" is not specified in httpd.conf, It will show error at console when I use "service httpd restart"

How could I do that?

GSP
  • 574
  • 3
  • 7
  • 34

1 Answers1

1

You'd register a ap_hook_post_config hook and do the verification of required settings there. Be aware that that hook is called twice as documented in the answer here: Init modules in apache2

See an example post config hook implementation here: http://svn.apache.org/repos/asf/httpd/httpd/tags/2.3.0/modules/examples/mod_example_hooks.c

Community
  • 1
  • 1
Hans Z.
  • 50,496
  • 12
  • 102
  • 115
  • thanks for your answer, **but I still do not know the way to check existence of "ConfigFilePath"**. If this key exist, read_config_file() will be called. If I define a global flag (default is true), in read_config_file() i set to false. I will check this flag at ap_hook_post_config. `Is it right way?` – GSP Jul 08 '15 at 09:37
  • 1
    I'm assuming you store the value of `ConfigFilePath` in a variable in the server/dir struct in the `read_config_file` function and initialize that variable to NULL. So what you'd do is check that variable in the post config hook and return/log an error if it is not set. – Hans Z. Jul 08 '15 at 10:39
  • I know this is very old, I am still struggling right now. Verifying a parameter set in `srv_conf` within `ap_hook_post_config` is pretty straighforward, as the server conf is accessible via `ap_get_module_config(s->module_config, &module)`. But how about directory configs? I am trying to verify every directory configuration before accepting any requests. – tworabbits Mar 14 '18 at 10:07