1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
* This module is used to load Apache modules at runtime. This means that the
19
* server functionality can be extended without recompiling and even without
20
* taking the server down at all. Only a HUP or AP_SIG_GRACEFUL signal
21
* needs to be sent to the server to reload the dynamically loaded modules.
23
* To use, you'll first need to build your module as a shared library, then
24
* update your configuration (httpd.conf) to get the Apache core to load the
27
* The easiest way to build a module as a shared library is to use the
28
* `SharedModule' command in the Configuration file, instead of `AddModule'.
29
* You should also change the file extension from `.o' to `.so'. So, for
30
* example, to build the status module as a shared library edit Configuration
32
* AddModule modules/standard/mod_status.o
34
* SharedModule modules/standard/mod_status.so
36
* Run Configure and make. Now Apache's httpd binary will _not_ include
37
* mod_status. Instead a shared object called mod_status.so will be build, in
38
* the modules/standard directory. You can build most of the modules as shared
39
* libraries like this.
41
* To use the shared module, move the .so file(s) into an appropriate
42
* directory. You might like to create a directory called "modules" under you
43
* server root for this (e.g. /usr/local/httpd/modules).
45
* Then edit your conf/httpd.conf file, and add LoadModule lines. For
47
* LoadModule status_module modules/mod_status.so
49
* The first argument is the module's structure name (look at the end of the
50
* module source to find this). The second option is the path to the module
51
* file, relative to the server root. Put these directives right at the top
52
* of your httpd.conf file.
54
* Now you can start Apache. A message will be logged at "debug" level to your
55
* error_log to confirm that the module(s) are loaded (use "LogLevel debug"
56
* directive to get these log messages).
58
* If you edit the LoadModule directives while the server is live you can get
59
* Apache to re-load the modules by sending it a HUP or AP_SIG_GRACEFUL
60
* signal as normal. You can use this to dynamically change the capability
61
* of your server without bringing it down.
63
* Because currently there is only limited builtin support in the Configure
64
* script for creating the shared library files (`.so'), please consult your
65
* vendors cc(1), ld(1) and dlopen(3) manpages to find out the appropriate
66
* compiler and linker flags and insert them manually into the Configuration
67
* file under CFLAGS_SHLIB, LDFLAGS_SHLIB and LDFLAGS_SHLIB_EXPORT.
69
* If you still have problems figuring out the flags both try the paper
70
* http://developer.netscape.com/library/documentation/enterprise
71
* /unix/svrplug.htm#1013807
72
* or install a Perl 5 interpreter on your platform and then run the command
74
* $ perl -V:usedl -V:ccdlflags -V:cccdlflags -V:lddlflags
76
* This gives you what type of dynamic loading Perl 5 uses on your platform
77
* and which compiler and linker flags Perl 5 uses to create the shared object
80
* Another location where you can find useful hints is the `ltconfig' script
81
* of the GNU libtool 1.2 package. Search for your platform name inside the
82
* various "case" constructs.
88
#include "apr_strings.h"
89
#include "apr_errno.h"
92
#include "ap_config.h"
94
#include "http_config.h"
96
#include "http_core.h"
100
module AP_MODULE_DECLARE_DATA so_module;
104
* Server configuration to keep track of actually
105
* loaded modules and the corresponding module name.
108
typedef struct so_server_conf {
109
apr_array_header_t *loaded_modules;
112
static void *so_sconf_create(apr_pool_t *p, server_rec *s)
116
soc = (so_server_conf *)apr_pcalloc(p, sizeof(so_server_conf));
117
soc->loaded_modules = apr_array_make(p, DYNAMIC_MODULE_LIMIT,
118
sizeof(ap_module_symbol_t));
126
* This is the cleanup for a loaded shared object. It unloads the module.
127
* This is called as a cleanup function from the core.
130
static apr_status_t unload_module(void *data)
132
ap_module_symbol_t *modi = (ap_module_symbol_t*)data;
134
/* only unload if module information is still existing */
135
if (modi->modp == NULL)
138
/* remove the module pointer from the core structure */
139
ap_remove_loaded_module(modi->modp);
141
/* destroy the module information */
148
* This is called for the directive LoadModule and actually loads
149
* a shared object file into the address space of the server process.
152
static const char *load_module(cmd_parms *cmd, void *dummy,
153
const char *modname, const char *filename)
155
apr_dso_handle_t *modhandle;
156
apr_dso_handle_sym_t modsym;
158
const char *szModuleFile = ap_server_root_relative(cmd->pool, filename);
159
so_server_conf *sconf;
160
ap_module_symbol_t *modi;
161
ap_module_symbol_t *modie;
165
/* we need to setup this value for dummy to make sure that we don't try
166
* to add a non-existant tree into the build when we return to
169
*(ap_directive_t **)dummy = NULL;
172
return apr_pstrcat(cmd->pool, "Invalid LoadModule path ",
177
* check for already existing module
178
* If it already exists, we have nothing to do
179
* Check both dynamically-loaded modules and statically-linked modules.
181
sconf = (so_server_conf *)ap_get_module_config(cmd->server->module_config,
183
modie = (ap_module_symbol_t *)sconf->loaded_modules->elts;
184
for (i = 0; i < sconf->loaded_modules->nelts; i++) {
186
if (modi->name != NULL && strcmp(modi->name, modname) == 0) {
187
ap_log_perror(APLOG_MARK, APLOG_WARNING, 0,
188
cmd->pool, "module %s is already loaded, skipping",
194
for (i = 0; ap_preloaded_modules[i]; i++) {
195
const char *preload_name;
196
apr_size_t preload_len;
197
apr_size_t thismod_len;
199
modp = ap_preloaded_modules[i];
201
/* make sure we're comparing apples with apples
202
* make sure name of preloaded module is mod_FOO.c
203
* make sure name of structure being loaded is FOO_module
206
if (memcmp(modp->name, "mod_", 4)) {
210
preload_name = modp->name + strlen("mod_");
211
preload_len = strlen(preload_name) - 2;
213
if (strlen(modname) <= strlen("_module")) {
216
thismod_len = strlen(modname) - strlen("_module");
217
if (strcmp(modname + thismod_len, "_module")) {
221
if (thismod_len != preload_len) {
225
if (!memcmp(modname, preload_name, preload_len)) {
226
return apr_pstrcat(cmd->pool, "module ", modname,
227
" is built-in and can't be loaded",
232
modi = apr_array_push(sconf->loaded_modules);
233
modi->name = modname;
236
* Load the file into the Apache address space
238
if (apr_dso_load(&modhandle, szModuleFile, cmd->pool) != APR_SUCCESS) {
241
return apr_pstrcat(cmd->pool, "Cannot load ", szModuleFile,
243
apr_dso_error(modhandle, my_error, sizeof(my_error)),
246
ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, cmd->pool,
247
"loaded module %s", modname);
250
* Retrieve the pointer to the module structure through the module name:
251
* First with the hidden variant (prefix `AP_') and then with the plain
254
if (apr_dso_sym(&modsym, modhandle, modname) != APR_SUCCESS) {
257
return apr_pstrcat(cmd->pool, "Can't locate API module structure `",
258
modname, "' in file ", szModuleFile, ": ",
259
apr_dso_error(modhandle, my_error, sizeof(my_error)),
262
modp = (module*) modsym;
263
modp->dynamic_load_handle = (apr_dso_handle_t *)modhandle;
267
* Make sure the found module structure is really a module structure
270
if (modp->magic != MODULE_MAGIC_COOKIE) {
271
return apr_pstrcat(cmd->pool, "API module structure `", modname,
272
"' in file ", szModuleFile, " is garbled -"
273
" perhaps this is not an Apache module DSO?", NULL);
277
* Add this module to the Apache core structures
279
error = ap_add_loaded_module(modp, cmd->pool);
285
* Register a cleanup in the config apr_pool_t (normally pconf). When
286
* we do a restart (or shutdown) this cleanup will cause the
287
* shared object to be unloaded.
289
apr_pool_cleanup_register(cmd->pool, modi, unload_module, apr_pool_cleanup_null);
292
* Finally we need to run the configuration process for the module
294
ap_single_module_configure(cmd->pool, cmd->server, modp);
300
* This implements the LoadFile directive and loads an arbitrary
301
* shared object file into the adress space of the server process.
304
static const char *load_file(cmd_parms *cmd, void *dummy, const char *filename)
306
apr_dso_handle_t *handle;
309
file = ap_server_root_relative(cmd->pool, filename);
312
return apr_pstrcat(cmd->pool, "Invalid LoadFile path ",
316
if (apr_dso_load(&handle, file, cmd->pool) != APR_SUCCESS) {
319
return apr_pstrcat(cmd->pool, "Cannot load ", filename,
321
apr_dso_error(handle, my_error, sizeof(my_error)),
325
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
326
"loaded file %s", filename);
331
static module *ap_find_loaded_module_symbol(server_rec *s, const char *modname)
333
so_server_conf *sconf;
334
ap_module_symbol_t *modi;
335
ap_module_symbol_t *modie;
338
sconf = (so_server_conf *)ap_get_module_config(s->module_config,
340
modie = (ap_module_symbol_t *)sconf->loaded_modules->elts;
342
for (i = 0; i < sconf->loaded_modules->nelts; i++) {
344
if (modi->name != NULL && strcmp(modi->name, modname) == 0) {
351
static void dump_loaded_modules(apr_pool_t *p, server_rec *s)
353
ap_module_symbol_t *modie;
354
ap_module_symbol_t *modi;
355
so_server_conf *sconf;
357
apr_file_t *out = NULL;
359
if (!ap_exists_config_define("DUMP_MODULES")) {
363
apr_file_open_stderr(&out, p);
365
apr_file_printf(out, "Loaded Modules:\n");
367
sconf = (so_server_conf *)ap_get_module_config(s->module_config,
370
modi = &ap_prelinked_module_symbols[i];
371
if (modi->name != NULL) {
372
apr_file_printf(out, " %s (static)\n", modi->name);
379
modie = (ap_module_symbol_t *)sconf->loaded_modules->elts;
380
for (i = 0; i < sconf->loaded_modules->nelts; i++) {
382
if (modi->name != NULL) {
383
apr_file_printf(out, " %s (shared)\n", modi->name);
388
#else /* not NO_DLOPEN */
390
static const char *load_file(cmd_parms *cmd, void *dummy, const char *filename)
392
ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, cmd->pool,
393
"WARNING: LoadFile not supported on this platform");
397
static const char *load_module(cmd_parms *cmd, void *dummy,
398
const char *modname, const char *filename)
400
ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, cmd->pool,
401
"WARNING: LoadModule not supported on this platform");
405
#endif /* NO_DLOPEN */
407
static void register_hooks(apr_pool_t *p)
410
APR_REGISTER_OPTIONAL_FN(ap_find_loaded_module_symbol);
411
ap_hook_test_config(dump_loaded_modules, NULL, NULL, APR_HOOK_MIDDLE);
415
static const command_rec so_cmds[] = {
416
AP_INIT_TAKE2("LoadModule", load_module, NULL, RSRC_CONF | EXEC_ON_READ,
417
"a module name and the name of a shared object file to load it from"),
418
AP_INIT_ITERATE("LoadFile", load_file, NULL, RSRC_CONF | EXEC_ON_READ,
419
"shared object file or library to load into the server at runtime"),
423
module AP_MODULE_DECLARE_DATA so_module = {
424
STANDARD20_MODULE_STUFF,
425
NULL, /* create per-dir config */
426
NULL, /* merge per-dir config */
427
so_sconf_create, /* server config */
428
NULL, /* merge server config */
429
so_cmds, /* command apr_table_t */
430
register_hooks /* register hooks */