mod_file_cache.c revision ca53a74f4012a45cbad48e940eddf27d866981f9
/* ====================================================================
* Copyright (c) 1998-1999 The Apache Group. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
/*
* Author: mod_file_cache by Bill Stoddard <stoddard@apache.org>
* Based on mod_mmap_static by Dean Gaudet <dgaudet@arctic.org>
*
* v0.01: initial implementation
*/
/*
Documentation:
Some sites have a set of static files that are really busy, and
change infrequently (or even on a regular schedule). Save time
by caching open handles to these files. This module, unlike
mod_mmap_static, caches open file handles, not file content.
On systems (like Windows) with heavy system call overhead and
that have an efficient sendfile implementation, caching file handles
offers several advantages over caching content. First, the file system
can manage the memory, allowing infrequently hit cached files to
be paged out. Second, since caching open handles does not consume
significant resources, it will be possible to enable an AutoLoadCache
feature where static files are dynamically loaded in the cache
as the server runs. On systems that have file change notification,
this module can be enhanced to automatically garbage collect
cached files that change on disk.
This module should work on Unix systems that have sendfile. Place
cachefile directives into your configuration to direct files to
be cached.
...
These files are only cached when the server is restarted, so if you
change the list, or if the files are changed, then you'll need to
restart the server.
To reiterate that point: if the files are modified *in place*
without restarting the server you may end up serving requests that
are completely bogus. You should update files by unlinking the old
copy and putting a new copy in place.
There's no such thing as inheriting these files across vhosts or
whatever... place the directives in the main server only.
Known problems:
Don't use Alias or RewriteRule to move these files around... unless
you feel like paying for an extra stat() on each request. This is
a deficiency in the Apache API that will hopefully be solved some day.
The file will be served out of the file handle cache, but there will be
an extra stat() that's a waste.
*/
#ifdef HAVE_STDIOP_H
#include <stdio.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#define CORE_PRIVATE
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_core.h"
#include "apr_mmap.h"
#include "apr_strings.h"
typedef struct {
#if APR_HAS_SENDFILE
#endif
const char *filename;
int is_mmapped;
#if APR_HAS_MMAP
apr_mmap_t *mm;
#endif
} a_file;
typedef struct {
{
return sconf;
}
#if APR_HAS_SENDFILE
apr_pool_t *p)
{
#ifdef WIN32
/* The Windows file needs to be opened for overlapped i/o, which APR doesn't
* support.
*/
GENERIC_READ, /* access (read-write) mode */
FILE_SHARE_READ, /* share mode */
NULL, /* pointer to security attributes */
OPEN_EXISTING, /* how to create */
NULL); /* handle to file with attributes to copy */
if (hFile != INVALID_HANDLE_VALUE) {
}
else {
rv = GetLastError();
}
#else
#endif
return rv;
}
#endif /* APR_HAS_SENDFILE */
{
size_t n;
while(n) {
#if APR_HAS_MMAP
if (file->is_mmapped) {
}
else
#endif
#if APR_HAS_SENDFILE
#endif
++file;
--n;
}
return APR_SUCCESS;
}
{
#if APR_HAS_SENDFILE
/* canonicalize the file name? */
/* os_canonical... */
"mod_file_cache: unable to stat(%s), skipping", filename);
return NULL;
}
"mod_file_cache: %s isn't a regular file, skipping", filename);
return NULL;
}
/* Note: open_file should call apr_open for Unix and CreateFile for Windows.
* The Windows file needs to be opened for async I/O to allow multiple threads
* to serve it up at once.
*/
if (rc != APR_SUCCESS) {
"mod_file_cache: unable to open(%s, O_RDONLY), skipping", filename);
return NULL;
}
/* first one, register the cleanup */
}
return NULL;
#else
/* Sendfile not supported on this platform */
"mod_file_cache: unable to cache file: %s. Sendfile is not supported on this OS", filename);
return NULL;
#endif
}
{
#if APR_HAS_MMAP
const char *fspec;
"mod_file_cache: unable to stat(%s), skipping", filename);
return NULL;
}
"mod_file_cache: %s isn't a regular file, skipping", filename);
return NULL;
}
"mod_file_cache: unable to open %s, skipping",
filename);
return NULL;
}
if ((rc = apr_mmap_create(&tmp.mm, fd, 0, tmp.finfo.size, APR_MMAP_READ, cmd->pool)) != APR_SUCCESS) {
"mod_file_cache: unable to mmap %s, skipping", filename);
return NULL;
}
/* first one, register the cleanup */
}
return NULL;
#else
/* MMAP not supported on this platform*/
return NULL;
#endif
}
{
}
{
int nelts;
/* sort the elements of the main_server, by filename */
/* and make the virtualhosts share the same thing */
}
}
/* If it's one of ours, fill in r->finfo now to avoid extra stat()... this is a
* bit of a kludge, because we really want to run after core_translate runs.
*/
static int file_cache_xlat(request_rec *r)
{
int res;
/* we only operate when at least one cachefile directive was used */
return DECLINED;
res = ap_core_translate(r);
return res;
}
sizeof(a_file), file_compare);
return DECLINED;
/* pass bsearch results to handler */
/* shortcircuit the get_path_info() stat() calls and stuff */
return OK;
}
{
#if APR_HAS_MMAP
#endif
return OK;
}
{
#if APR_HAS_SENDFILE
if (rv != APR_SUCCESS) {
"seek failed");
return HTTP_INTERNAL_SERVER_ERROR;
}
if (rv != APR_SUCCESS) {
/* ap_send_fd will log the error */
return HTTP_INTERNAL_SERVER_ERROR;
}
#endif
return OK;
}
static int file_cache_handler(request_rec *r)
{
int errstatus;
return DECLINED;
}
/* we don't handle anything but GET */
/* did xlat phase find the file? */
return DECLINED;
}
/* note that we would handle GET on this resource */
/* This handler has no use for a request body (yet), but we still
* need to read and discard it if the client sent one.
*/
return errstatus;
ap_set_etag(r);
return errstatus;
}
/* Call appropriate handler */
if (!r->header_only) {
else
}
return rc;
}
static command_rec file_cache_cmds[] =
{
"A space separated list of files to add to the file handle cache at config time"),
"A space separated list of files to mmap at config time"),
{NULL}
};
static void register_hooks(void)
{
/* This trick doesn't work apparently because the translate hooks
are single shot. If the core_hook returns OK, then our hook is
not called.
ap_hook_translate_name(file_cache_xlat, aszPre, NULL, AP_HOOK_MIDDLE);
*/
}
{
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
create_server_config, /* create per-server config structure */
NULL, /* merge per-server config structures */
file_cache_cmds, /* command handlers */
register_hooks /* register hooks */
};