home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / os / win32 / mod_dll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  7.1 KB  |  191 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /*
  59.  * mod_dll.c - DLL module loader for Windows
  60.  * by Alexei Kosut, based on mod_dld.c by rst
  61.  *
  62.  * This module loads another module into the server that has been
  63.  * compiled as a DLL. It doesn't work perfectly, but well enough.
  64.  *
  65.  * To use, compile the module into a DLL. Then add the following to the
  66.  * server's config file (before any directives belonging to the loaded module):
  67.  *
  68.  * LoadModule module_name mod_name.dll
  69.  *
  70.  * module_name should be the name of the module (e.g. includes_module),
  71.  * and mod_name.dll should be the name of the DLL, relative to the server
  72.  * root.
  73.  *
  74.  * There is also a directive that will load a non-module DLL, if you'd
  75.  * like to load additional libraries into the server:
  76.  *
  77.  * LoadFile filename.dll
  78.  *
  79.  * Compiling a module as a DLL (using Microsoft Visual C++):
  80.  *
  81.  * 1. Add the following to the module source file's module record
  82.  *    definition: MODULE_VAR_EXPORT. i.e. if you have
  83.  *    "module foo_module;", replace it with
  84.  *    "module MODULE_VAR_EXPORT foo_module;". If your module is to be
  85.  *    compiled with both Windows and Unix, you may wish to use an #ifdef
  86.  *    WIN32
  87.  *
  88.  *    Note that your module should still work just fine compiled-in
  89.  *    with this code bit there. It only activates when using the module
  90.  *    as a DLL.
  91.  *
  92.  * 2. Create a DLL file with just the module source file (and any associated
  93.  *    files). Be sure to link it against the ApacheCore.lib created when
  94.  *    compiling ApacheCore.dll. You may also have to tweak the settings to
  95.  *    find all of the Apache includes files correctly. After creating the
  96.  *    DLL, follow the above instructions to load it into Apache.
  97.  */
  98.  
  99. #include "httpd.h"
  100. #include "http_config.h"
  101.  
  102. /*
  103.  * The hard part of implementing LoadModule is deciding what to do about
  104.  * rereading the config files.  This proof-of-concept implementation takes the 
  105.  * cheap way out:  we only actually load the modules the first time through.
  106.  */
  107.  
  108. static int been_there_done_that = 0; /* Loaded the modules yet? */
  109. static int have_symbol_table = 0;
  110.  
  111. char *load_module (cmd_parms *cmd, void *dummy, char *modname, char *filename)
  112. {
  113.     HINSTANCE modhandle;
  114.     module *modp;
  115.     const char *szModuleFile=ap_server_root_relative(cmd->pool, filename);
  116.  
  117.     if (been_there_done_that) return NULL;
  118.     
  119.     if (!(modhandle = LoadLibraryEx(szModuleFile, NULL,
  120.                     LOAD_WITH_ALTERED_SEARCH_PATH)))
  121.     return ap_pstrcat (cmd->pool, "Cannot load ", szModuleFile, " into server",
  122.             NULL);
  123.  
  124.     /* If I knew what the correct cast is here, I'd be happy. But 
  125.      * I don't. So I'll use (void *). It works.
  126.      */
  127.     if (!(modp = (module *)(GetProcAddress (modhandle, modname)))) {
  128.     return ap_pstrcat (cmd->pool, "Can't find module ", modname,
  129.             " in file ", filename, NULL);
  130.     }
  131.     
  132.     ap_add_module (modp);
  133.  
  134.     /* Alethea Patch (rws,djw2) - need to run configuration functions
  135.        in new modules */
  136.  
  137.     if (modp->create_server_config)
  138.       ((void**)cmd->server->module_config)[modp->module_index]=
  139.     (*modp->create_server_config)(cmd->pool, cmd->server);
  140.  
  141.     if (modp->create_dir_config)
  142.       ((void**)cmd->server->lookup_defaults)[modp->module_index]=
  143.     (*modp->create_dir_config)(cmd->pool, NULL);
  144.  
  145.  
  146.     return NULL;
  147. }
  148.  
  149. char *load_file (cmd_parms *cmd, void *dummy, char *filename)
  150. {
  151.    if (been_there_done_that) return NULL;
  152.     
  153.     if (!LoadLibrary(ap_server_root_relative(cmd->pool, filename)))
  154.         return ap_pstrcat (cmd->pool, "Cannot load ", filename, " into server", NULL);
  155.  
  156.     return NULL;
  157. }
  158.  
  159. void check_loaded_modules (server_rec *dummy, pool *p)
  160. {
  161.     if (been_there_done_that) return;
  162.  
  163.     been_there_done_that = 1;
  164. }
  165.  
  166. command_rec dll_cmds[] = {
  167. { "LoadModule", load_module, NULL, RSRC_CONF, TAKE2,
  168.   "a module name, and the name of a file to load it from"},
  169. { "LoadFile", load_file, NULL, RSRC_CONF, ITERATE,
  170.   "files or libraries to link into the server at runtime"},
  171. { NULL }
  172. };
  173.  
  174. module dll_module = {
  175.    STANDARD_MODULE_STUFF,
  176.    check_loaded_modules,    /* initializer */
  177.    NULL,            /* create per-dir config */
  178.    NULL,            /* merge per-dir config */
  179.    NULL,            /* server config */
  180.    NULL,            /* merge server config */
  181.    dll_cmds,            /* command table */
  182.    NULL,            /* handlers */
  183.    NULL,            /* filename translation */
  184.    NULL,            /* check_user_id */
  185.    NULL,            /* check auth */
  186.    NULL,            /* check access */
  187.    NULL,            /* type_checker */
  188.    NULL,            /* logger */
  189.    NULL                /* header parser */
  190. };
  191.