home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / experimental / mod_mmap_static.c < prev   
Encoding:
C/C++ Source or Header  |  1999-01-01  |  12.0 KB  |  396 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1998-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_mmap_static: mmap a config-time list of files for faster serving
  60.  *
  61.  * v0.04
  62.  * 
  63.  * Author: Dean Gaudet <dgaudet@arctic.org>
  64.  *
  65.  * v0.01: initial implementation
  66.  * v0.02: get rid of the extra stat() in the core by filling in what we know
  67.  * v0.03: get rid of the cached match from the xlat routine since there are
  68.  *        many cases where the request is modified between it and the
  69.  *        handler... so we do the binary search twice, but the second time
  70.  *        we can use st_ino and st_dev to speed it up.
  71.  * v0.04: work around mod_rewrite, which sets r->filename to the uri first
  72.  */
  73.  
  74. /*
  75.     Documentation:
  76.  
  77.     The concept is simple.  Some sites have a set of static files that are
  78.     really busy, and change infrequently (or even on a regular schedule).
  79.     Save time by mmap()ing these files into memory and avoid a lot of the
  80.     crap required to do normal file serving.  Place directives such as:
  81.  
  82.     mmapfile /path/to/file1
  83.     mmapfile /path/to/file2
  84.     ...
  85.  
  86.     into your configuration.  These files are only mmap()d when the server
  87.     is restarted, so if you change the list, or if the files are changed,
  88.     then you'll need to restart the server.
  89.  
  90.     To reiterate that point:  if the files are modified *in place*
  91.     without restarting the server you may end up serving requests that
  92.     are completely bogus.  You should update files by unlinking the old
  93.     copy and putting a new copy in place.  Most tools such as rdist and
  94.     mv do this.
  95.  
  96.     There's no such thing as inheriting these files across vhosts or
  97.     whatever... place the directives in the main server only.
  98.  
  99.     Known problems:
  100.  
  101.     Don't use Alias or RewriteRule to move these files around...  unless
  102.     you feel like paying for an extra stat() on each request.  This is
  103.     a deficiency in the Apache API that will hopefully be solved some day.
  104.     The file will be served out of the mmap cache, but there will be
  105.     an extra stat() that's a waste.
  106. */
  107.  
  108. #include <stdio.h>
  109. #include <sys/types.h>
  110. #include <sys/stat.h>
  111. #include <fcntl.h>
  112. #include <errno.h>
  113. #include <string.h>
  114. #include <sys/mman.h>
  115.  
  116. #define CORE_PRIVATE
  117.  
  118. #include "httpd.h"
  119. #include "http_config.h"
  120. #include "http_log.h"
  121. #include "http_protocol.h"
  122. #include "http_request.h"
  123. #include "http_core.h"
  124.  
  125. module MODULE_VAR_EXPORT mmap_static_module;
  126.  
  127. typedef struct {
  128.     char *filename;
  129.     void *mm;
  130.     struct stat finfo;
  131. } a_file;
  132.  
  133. typedef struct {
  134.     array_header *files;
  135.     array_header *inode_sorted;
  136. } a_server_config;
  137.  
  138.  
  139. static void *create_server_config(pool *p, server_rec *s)
  140. {
  141.     a_server_config *sconf = ap_palloc(p, sizeof(*sconf));
  142.  
  143.     sconf->files = ap_make_array(p, 20, sizeof(a_file));
  144.     sconf->inode_sorted = NULL;
  145.     return sconf;
  146. }
  147.  
  148. static void cleanup_mmap(void *sconfv)
  149. {
  150.     a_server_config *sconf = sconfv;
  151.     size_t n;
  152.     a_file *file;
  153.  
  154.     n = sconf->files->nelts;
  155.     file = (a_file *)sconf->files->elts;
  156.     while(n) {
  157.     munmap(file->mm, file->finfo.st_size);
  158.     ++file;
  159.     --n;
  160.     }
  161. }
  162.  
  163. static const char *mmapfile(cmd_parms *cmd, void *dummy, char *filename)
  164. {
  165.     a_server_config *sconf;
  166.     a_file *new_file;
  167.     a_file tmp;
  168.     int fd;
  169.     caddr_t mm;
  170.  
  171.     if (stat(filename, &tmp.finfo) == -1) {
  172.     ap_log_error(APLOG_MARK, APLOG_WARNING, cmd->server,
  173.         "mmap_static: unable to stat(%s), skipping", filename);
  174.     return NULL;
  175.     }
  176.     if ((tmp.finfo.st_mode & S_IFMT) != S_IFREG) {
  177.     ap_log_error(APLOG_MARK, APLOG_WARNING, cmd->server,
  178.         "mmap_static: %s isn't a regular file, skipping", filename);
  179.     return NULL;
  180.     }
  181.     ap_block_alarms();
  182.     fd = open(filename, O_RDONLY, 0);
  183.     if (fd == -1) {
  184.     ap_log_error(APLOG_MARK, APLOG_WARNING, cmd->server,
  185.         "mmap_static: unable to open(%s, O_RDONLY), skipping", filename);
  186.     return NULL;
  187.     }
  188.     mm = mmap(NULL, tmp.finfo.st_size, PROT_READ, MAP_SHARED, fd, 0);
  189.     if (mm == (caddr_t)-1) {
  190.     int save_errno = errno;
  191.     close(fd);
  192.     ap_unblock_alarms();
  193.     errno = save_errno;
  194.     ap_log_error(APLOG_MARK, APLOG_WARNING, cmd->server,
  195.         "mmap_static: unable to mmap %s, skipping", filename);
  196.     return NULL;
  197.     }
  198.     close(fd);
  199.     tmp.mm = mm;
  200.     tmp.filename = ap_pstrdup(cmd->pool, filename);
  201.     sconf = ap_get_module_config(cmd->server->module_config, &mmap_static_module);
  202.     new_file = ap_push_array(sconf->files);
  203.     *new_file = tmp;
  204.     if (sconf->files->nelts == 1) {
  205.     /* first one, register the cleanup */
  206.     ap_register_cleanup(cmd->pool, sconf, cleanup_mmap, ap_null_cleanup);
  207.     }
  208.     ap_unblock_alarms();
  209.     return NULL;
  210. }
  211.  
  212. static command_rec mmap_static_cmds[] =
  213. {
  214.     {
  215.     "mmapfile", mmapfile, NULL, RSRC_CONF, ITERATE,
  216.     "A space separated list of files to mmap at config time"
  217.     },
  218.     {
  219.     NULL
  220.     }
  221. };
  222.  
  223. static int file_compare(const void *av, const void *bv)
  224. {
  225.     const a_file *a = av;
  226.     const a_file *b = bv;
  227.  
  228.     return strcmp(a->filename, b->filename);
  229. }
  230.  
  231. static int inode_compare(const void *av, const void *bv)
  232. {
  233.     const a_file *a = *(a_file **)av;
  234.     const a_file *b = *(a_file **)bv;
  235.     long c;
  236.  
  237.     c = a->finfo.st_ino - b->finfo.st_ino;
  238.     if (c == 0) {
  239.     return a->finfo.st_dev - b->finfo.st_dev;
  240.     }
  241.     return c;
  242. }
  243.  
  244. static void mmap_init(server_rec *s, pool *p)
  245. {
  246.     a_server_config *sconf;
  247.     array_header *inodes;
  248.     a_file *elts;
  249.     int nelts;
  250.     int i;
  251.     
  252.     /* sort the elements of the main_server, by filename */
  253.     sconf = ap_get_module_config(s->module_config, &mmap_static_module);
  254.     elts = (a_file *)sconf->files->elts;
  255.     nelts = sconf->files->nelts;
  256.     qsort(elts, nelts, sizeof(a_file), file_compare);
  257.  
  258.     /* build an index by inode as well, speeds up the search in the handler */
  259.     inodes = ap_make_array(p, nelts, sizeof(a_file *));
  260.     sconf->inode_sorted = inodes;
  261.     for (i = 0; i < nelts; ++i) {
  262.     *(a_file **)ap_push_array(inodes) = &elts[i];
  263.     }
  264.     qsort(inodes->elts, nelts, sizeof(a_file *), inode_compare);
  265.  
  266.     /* and make the virtualhosts share the same thing */
  267.     for (s = s->next; s; s = s->next) {
  268.     ap_set_module_config(s->module_config, &mmap_static_module, sconf);
  269.     }
  270. }
  271.  
  272. /* If it's one of ours, fill in r->finfo now to avoid extra stat()... this is a
  273.  * bit of a kludge, because we really want to run after core_translate runs.
  274.  */
  275.  
  276. static int mmap_static_xlat(request_rec *r)
  277. {
  278.     a_server_config *sconf;
  279.     a_file tmp;
  280.     a_file *match;
  281.     int res;
  282.  
  283.     sconf = ap_get_module_config(r->server->module_config, &mmap_static_module);
  284.  
  285.     /* we only operate when at least one mmapfile directive was used */
  286.     if (ap_is_empty_table(sconf->files))
  287.     return DECLINED;
  288.  
  289.     /* we require other modules to first set up a filename */
  290.     res = core_module.translate_handler(r);
  291.     if (res == DECLINED || !r->filename) {
  292.     return res;
  293.     }
  294.     tmp.filename = r->filename;
  295.     match = (a_file *)bsearch(&tmp, sconf->files->elts, sconf->files->nelts,
  296.     sizeof(a_file), file_compare);
  297.     if (match == NULL) {
  298.     return DECLINED;
  299.     }
  300.  
  301.     /* shortcircuit the get_path_info() stat() calls and stuff */
  302.     r->finfo = match->finfo;
  303.     return OK;
  304. }
  305.  
  306.  
  307. static int mmap_static_handler(request_rec *r)
  308. {
  309.     a_server_config *sconf;
  310.     a_file tmp;
  311.     a_file *ptmp;
  312.     a_file **pmatch;
  313.     a_file *match;
  314.     int rangestatus, errstatus;
  315.  
  316.     /* we don't handle anything but GET */
  317.     if (r->method_number != M_GET) return DECLINED;
  318.  
  319.     /* file doesn't exist, we won't be dealing with it */
  320.     if (r->finfo.st_mode == 0) return DECLINED;
  321.  
  322.     sconf = ap_get_module_config(r->server->module_config, &mmap_static_module);
  323.     tmp.finfo.st_dev = r->finfo.st_dev;
  324.     tmp.finfo.st_ino = r->finfo.st_ino;
  325.     ptmp = &tmp;
  326.     pmatch = (a_file **)bsearch(&ptmp, sconf->inode_sorted->elts,
  327.     sconf->inode_sorted->nelts, sizeof(a_file *), inode_compare);
  328.     if (pmatch == NULL) {
  329.     return DECLINED;
  330.     }
  331.     match = *pmatch;
  332.  
  333.     /* note that we would handle GET on this resource */
  334.     r->allowed |= (1 << M_GET);
  335.  
  336.     /* This handler has no use for a request body (yet), but we still
  337.      * need to read and discard it if the client sent one.
  338.      */
  339.     if ((errstatus = ap_discard_request_body(r)) != OK)
  340.         return errstatus;
  341.  
  342.     ap_update_mtime(r, match->finfo.st_mtime);
  343.     ap_set_last_modified(r);
  344.     ap_set_etag(r);
  345.     if (((errstatus = ap_meets_conditions(r)) != OK)
  346.     || (errstatus = ap_set_content_length (r, match->finfo.st_size))) {
  347.         return errstatus;
  348.     }
  349.  
  350.     rangestatus = ap_set_byterange(r);
  351.     ap_send_http_header(r);
  352.  
  353.     if (!r->header_only) {
  354.     if (!rangestatus) {
  355.         ap_send_mmap (match->mm, r, 0, match->finfo.st_size);
  356.     }
  357.     else {
  358.         long offset, length;
  359.         while (ap_each_byterange(r, &offset, &length)) {
  360.         ap_send_mmap(match->mm, r, offset, length);
  361.         }
  362.     }
  363.     }
  364.     return OK;
  365. }
  366.  
  367.  
  368. static const handler_rec mmap_static_handlers[] =
  369. {
  370.     { "*/*", mmap_static_handler },
  371.     { NULL }
  372. };
  373.  
  374. module MODULE_VAR_EXPORT mmap_static_module =
  375. {
  376.     STANDARD_MODULE_STUFF,
  377.     mmap_init,            /* initializer */
  378.     NULL,            /* dir config creater */
  379.     NULL,            /* dir merger --- default is to override */
  380.     create_server_config,    /* server config */
  381.     NULL,            /* merge server config */
  382.     mmap_static_cmds,        /* command handlers */
  383.     mmap_static_handlers,    /* handlers */
  384.     mmap_static_xlat,        /* filename translation */
  385.     NULL,            /* check_user_id */
  386.     NULL,            /* check auth */
  387.     NULL,            /* check access */
  388.     NULL,            /* type_checker */
  389.     NULL,            /* fixups */
  390.     NULL,            /* logger */
  391.     NULL,            /* header parser */
  392.     NULL,            /* child_init */
  393.     NULL,            /* child_exit */
  394.     NULL            /* post read-request */
  395. };
  396.