|
|
Any existing Apache module can be used as a template for a new module.
Mod_example.c is distributed with Apache in /APACHE/PUB/libexec
and makes a useful template for a simple module. When compiled and linked as
the shared library (NMXL) mod_example.so, this module is a fully working
DSO. The module libexec/mod_example.so has already been pre-built.
For a more functional module, try a different module as your template. For
instance, to create a new module that does authentication, starting with one of
Apache's authentication modules may be more appropriate. If you want to create
a module that has its own configuration directives, start with another module
that already does this.
To create the module file "mod_hw.c" from file "mod_example.c",
log on as MGR.APACHE so that the file is created with the right
ownership:
:HELLO MGR.APACHE
:XEQ SH.HPBIN.SYS -L
shell/iX> mkdir hw
shell/iX> cd hw
shell/iX> cp /APACHE/PUB/libexec/mod_example.c mod_hw.c
Change all references inside mod_hw.c from mod_example,
example_module, example_handler, etc. to mod_hw,
hw_module, hw_handler, etc., and modify/add any other code, as
needed. Creating a separate directory for the module, such as hw,
separates it from other modules under development.
To compile a module, certain compile options must be specified and the Apache C
header files must also be included. Below, gcc creates two object files,
hw.o and mod_hw.o, using the necessary options and include files.
Use the -c option for compilation:
shell/iX> gcc -DMPE -D_POSIX_SOURCE -D_SOCKET_SOURCE
-DNO_DBM_REWRITEMAP -DUSE_HSREGEX -DEAPI -DSHARED_MODULE
-I/APACHE/PUB/include -c mod_hw.c hw.c
Next, link the module. The link steps will be different when calling external
functions that reside in archive or shared libraries. Other examples of linking
are shown later.
To link, the MPE/iX LinkEditor can be called from the CI or the POSIX
shell:
:linkedit
or
shell/iX> callci linkeditLinkEd> buildxl xl=./mod_hw.so;limit=5
LinkEd> addxl from=./mod_hw.o,./hw.o;to=./mod_hw.so;
rl=/lib/libm.a,/lib/libc.a;merge;share
1 OBJECT FILE HAS BEEN ADDED.
The "rl=" option is used to specify which archive libraries are used to
resolve external function calls. The math library (/lib/libm.a) is
specified here to resolve pow() in mod_hw.o. /lib/libc.a
is not actually needed by the sample code. But it is a good practice to always
specify libc since most modules and other libraries are likely to need
functions from this library. If libc is not specified explicitly as
shown here, the MPE/iX C library will be used by default
(LIBC.LIB.SYS). Since Apache is built with libc, we recommend
explicitly specifying /lib/libc instead of defaulting to
LIBC.LIB.SYS. /lib/libc and LIBC.LIB.SYS are not
identical. The order of the libraries listed by "rl=" is important and
libc should always be specified last. The merge directive is
necessary when functions are called across object boundaries such as
mod_hw.o calling helloworld() in hw.o. The share
option is needed when global data is shared between multiple object files. The
share option is not actually needed by the sample code.
The compile and link steps can be put in a Makefile to facilitate
multiple builds of a module. As an example, refer to the section
“Modified APXS Makefile
(mod_hw)”.
Mod_hw.so is now ready to be configured into Apache. To do this, refer
to the section "Creating Apache Modules".
|