httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From (Rasmus Lerdorf)
Subject Re: apache/linux modules
Date Tue, 03 Feb 1998 05:20:32 GMT
> Obviously, the rest of it is correct; you need a standard name, or some
> way of extracting the information in another way. A good way might be for
> shared modules to all use the same name for their module structure. Then
> the loading module can just dlsym() that name, and it will always be
> there. This is how ISAPI extensions work, for example.

This is also how PHP modules work.  Or close to it at least.  

> (though obviously this won't work if the module is statically linked
> instead of dynamically. But that's what #ifdefs are for.)

A PHP module has a list of functions followed by a module_entry struct
that defines a name, startup and shutdown functions plus some other

function_entry mysql_functions[] = {
    {"mysql_connect",       php3_mysql_connect,         NULL},
    {"mysql_pconnect",      php3_mysql_pconnect,        NULL},
    {"mysql_close",         php3_mysql_close,           NULL},
    {"mysql_select_db",     php3_mysql_select_db,       NULL},

php3_module_entry mysql_module_entry = {
   "MySQL", mysql_functions, php3_minit_mysql, php3_mshutdown_mysql,
    php3_rinit_mysql, NULL, php3_info_mysql, 0, 0, 0, NULL

#if defined(COMPILE_DL)
DLEXPORT php3_module_entry *get_module(void) { return &mysql_module_entry; }

Then, in the core of PHP (in the dl.c file for anyone with the source)
    /* load dynamic symbol */
    handle = dlopen(libpath, RTLD_LAZY);
    if (!handle) {
#if MSVC5
        php3_error(E_ERROR,"Unable to load dynamic library '%s'<br>\n%s",libpath,php3_win_err());
        php3_error(E_ERROR,"Unable to load dynamic library '%s' - %s",libpath,dlerror());
    get_module = (php3_module_entry *(*)(void)) dlsym(handle,"get_module");
    if (!get_module) {
        php3_error(E_CORE_WARNING,"Invalid library (maybe not a PHP3 library) '%s' ",file->value.strval);
    module_entry = get_module();

This setup, along with the COMPILE_DL #define, allows all modules to be
written the same way regardless of whether they are compiled as a static
library or as a shared one.  As far as the PHP core goes, there is no
difference.  A lot of the core of PHP are in modules as well that could
theoretically be yanked out into .so's.  This translates well into the
Win32 world.  All PHP modules compile directly to Win32 DLL's.  There are
some thread-local-storage tricks involved behind the scenes, but for
someone writing a module they don't need to know about this.

(just hoping to inject some ideas into the 2.0 API re-design)


View raw message