How to programmatically obtain the apache2.conf file directory or path inside a module?

177 views Asked by At

I have a module that is hooking ap_hook_child_init. From that callback I would like to obtain the directory where the main apache2.conf file lives (or the full path to that file, I'll parse out the directory).

That callback takes a server_rec struct and a apr_poot_t struct. server_rec has a path member, but it is null.

2

There are 2 answers

0
covener On BEST ANSWER

You can't find this directly, and of course "apache2.conf" may not exist at all. This file is a debian-ism.

However, one option you have is to:

  • Find a directive commonly present in this config file or whatever substitutes you'd like treat the same
  • Add the directive to your module with the same definition as the core module
  • When you get control in the callback for handling the directive, look at cmd->directive->fname and save the path away in a global variable.

Multiple modules can share the same directive name and they all get called to process it.

0
widon1104 On
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <linux/limits.h>
#include <libgen.h>

void getJsonConfigPath(char *path, int pathLen)
{
    char dir[PATH_MAX] = {0};
    int n = readlink("/proc/self/exe", dir, PATH_MAX);
    char *dname = dirname(dir);
    if (strlen(dname) < pathLen) {
        strcpy(path, dname);
        int pathLen = strlen(path);
        if (path[pathLen-1] == '/') {
            strcat(path, "config.json");
        } else {
            strcat(path, "/config.json");
        }
    } else {
        printf("dname too long: %d\n", strlen(dname));
    }
}

int main() {
    char path[PATH_MAX] = {0};
    getJsonConfigPath(path, sizeof(path));
    printf("path: %s\n", path);
 
    return 0;
}

You can use the function getJsonConfigPath in you apache moduleļ¼Œthis function can return the config path you can use.