6

I know when I install a Linux app from source ,I execute ./configure --sysconfdir=/etc, then this app's conf file(such as httpd.conf) will goto /etc.

But from the view of source code, how do source code know the conf file is under /etc when parse it. I mean the code like fopen("/../../app.conf", "r"); is determined before we install it, will the configure file change source code or some other mechanism exist ?

2 Answers2

5

The configure script will generate the necessary Makefile that will use the C compiler's -DMACRO=content functionality to essentially inject C preprocessor #define MACRO content statements into the compilation units. So, sysconfdir could be used via Make rules:

foo.o: foo.c
        $(CC) -DCONFDIR=$(sysconfdir) -o $@ $<

(That says to build the foo.o object file when foo.c is updated; to build it, use the $(CC) variable to run the C compiler, define CONFDIR with the contents of $(sysconfdir) (supplied via the ./configure script), put output into the target file ($@) and give the source file ($<) as the lone input to the compiler.))

Then the C code in foo.c could use it like this:

 FILE *conf;
 if (conf = fopen(CONFDIR "/foo", "r")) {
     /* read config file */
 } else {
     /* unable to open config, either error and die or supply defaults */
 }

Note the C string concatenation is performed before compiling the program -- super convenient for exactly this kind of use.

More details here: http://www.gnu.org/software/hello/manual/autoconf/Installation-Directory-Variables.html#Installation-Directory-Variables

Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • For Apache, look in the `include/ap_config_layout.h.in` file for some `#define` statements that are used elsewhere in the program. Rsync configures `RSYNCD_SYSCONF` in `configure.in`, which is used in `clientserver.c` function `load_config()`. – sarnold Feb 20 '11 at 08:44
1

When you execute ./configure, it typically generates a makefile that includes the command options for the C compiler. These options will include -D... options that (in effect) "#define" various CPP symbols. One of these will have the "/etc" value that you supplied when you ran ./configure --sysconfdir=/etc.

From there, the "/etc" string gets compiled into the code anywhere that the source code uses the #defined symbol.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks very much for your quick answer, so what source code would be look like in that way? I've looked into rsync and httpd's source code but I didn't find THE code parsing conf file. It would be greatly appreciated if you can offer an example. – Linux-C-newbie Feb 20 '11 at 08:23
  • In the case of httpd, look for the SERVER_CONFIG_FILE symbol. How the configuration process goes from --sysconfigdir to that is (ahem) complicated, and involves some apache specific M4 macros. Look at "configure.in" and "acinclude.m4". And "main.c" contains places where SERVER_CONFIG_FILE is used. – Stephen C Feb 20 '11 at 14:00
  • Oh yeah, seems I have a lot work to do now, Thank you very much – Linux-C-newbie Feb 21 '11 at 03:33