htcacheclean.c revision 03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/* Copyright 2001-2004 The Apache Software Foundation
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin *
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * Licensed under the Apache License, Version 2.0 (the "License");
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * you may not use this file except in compliance with the License.
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * You may obtain a copy of the License at
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin *
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * http://www.apache.org/licenses/LICENSE-2.0
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin *
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * Unless required by applicable law or agreed to in writing, software
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * distributed under the License is distributed on an "AS IS" BASIS,
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * See the License for the specific language governing permissions and
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * limitations under the License.
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/*
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * htcacheclean.c: simple program for cleaning of
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * the disk cache of the Apache HTTP server
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin *
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * Contributed by Andreas Steinmetz <ast@domdv.de>
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * 8 Oct 2004
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_lib.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_strings.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_file_io.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_file_info.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_pools.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_hash.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_thread_proc.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_signal.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_getopt.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_ring.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include "apr_date.h"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#if APR_HAVE_UNISTD_H
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include <unistd.h>
c1e7441013e1c8f09cb36e48117497f5650b154amartin#endif
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#if APR_HAVE_STDLIB_H
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#include <stdlib.h>
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#endif
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/* mod_disk_cache.c extract start */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define DISK_FORMAT_VERSION 0
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrintypedef struct {
c1e7441013e1c8f09cb36e48117497f5650b154amartin /* Indicates the format of the header struct stored on-disk. */
c1e7441013e1c8f09cb36e48117497f5650b154amartin int format;
c1e7441013e1c8f09cb36e48117497f5650b154amartin /* The HTTP status code returned for this response. */
c1e7441013e1c8f09cb36e48117497f5650b154amartin int status;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin /* The size of the entity name that follows. */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_size_t name_len;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin /* The number of times we've cached this entity. */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_size_t entity_version;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin /* Miscellaneous time values. */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t date;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t expire;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t request_time;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t response_time;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin} disk_cache_info_t;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define CACHE_HEADER_SUFFIX ".header"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define CACHE_DATA_SUFFIX ".data"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/* mod_disk_cache.c extract end */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/* mod_disk_cache.c related definitions start */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/*
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * this is based on #define AP_TEMPFILE "/aptmpXXXXXX"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin *
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * the above definition could be reworked into the following:
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin *
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * #define AP_TEMPFILE_PREFIX "/"
c1e7441013e1c8f09cb36e48117497f5650b154amartin * #define AP_TEMPFILE_BASE "aptmp"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * #define AP_TEMPFILE_SUFFIX "XXXXXX"
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * #define AP_TEMPFILE_BASELEN strlen(AP_TEMPFILE_BASE)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * #define AP_TEMPFILE_NAMELEN strlen(AP_TEMPFILE_BASE AP_TEMPFILE_SUFFIX)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * #define AP_TEMPFILE AP_TEMPFILE_PREFIX AP_TEMPFILE_BASE AP_TEMPFILE_SUFFIX
562e9ce367eaaf4d3ea0ed4eaf3dbf0a644cf4aajorton *
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * these definitions would then match the definitions below:
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
c1e7441013e1c8f09cb36e48117497f5650b154amartin#define AP_TEMPFILE_BASE "aptmp"
c1e7441013e1c8f09cb36e48117497f5650b154amartin#define AP_TEMPFILE_SUFFIX "XXXXXX"
c1e7441013e1c8f09cb36e48117497f5650b154amartin#define AP_TEMPFILE_BASELEN strlen(AP_TEMPFILE_BASE)
c1e7441013e1c8f09cb36e48117497f5650b154amartin#define AP_TEMPFILE_NAMELEN strlen(AP_TEMPFILE_BASE AP_TEMPFILE_SUFFIX)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/* mod_disk_cache.c related definitions end */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/* define the following for debugging */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#undef DEBUG
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/*
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * Note: on Linux delays <= 2ms are busy waits without
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * scheduling, so never use a delay <= 2ms below
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define NICE_DELAY 10000 /* usecs */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define DELETE_NICE 10 /* be nice after this amount of delete ops */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define STAT_ATTEMPTS 10 /* maximum stat attempts for a file */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define STAT_DELAY 5000 /* usecs */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define HEADER 1 /* headers file */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define DATA 2 /* body file */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define TEMP 4 /* temporary file */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define HEADERDATA (HEADER|DATA)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define MAXDEVIATION 3600 /* secs */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define SECS_PER_MIN 60
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define KBYTE 1024
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define MBYTE 1048576
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define DIRINFO (APR_FINFO_MTIME|APR_FINFO_SIZE|APR_FINFO_TYPE|APR_FINFO_LINK)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrintypedef struct _direntry {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin APR_RING_ENTRY(_direntry) link;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin int type; /* type of file/fileset: TEMP, HEADER, DATA, HEADERDATA */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t htime; /* headers file modification time */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t dtime; /* body file modification time */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_off_t hsize; /* headers file size */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_off_t dsize; /* body or temporary file size */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin char *basename; /* file/fileset base name */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin} DIRENTRY;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrintypedef struct _entry {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin APR_RING_ENTRY(_entry) link;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t expire; /* cache entry exiration time */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t response_time; /* cache entry time of last response to client */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t htime; /* headers file modification time */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_time_t dtime; /* body file modification time */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_off_t hsize; /* headers file size */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_off_t dsize; /* body or temporary file size */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin char *basename; /* fileset base name */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin} ENTRY;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int delcount; /* file deletion count for nice mode */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int interrupted; /* flag: true if SIGINT or SIGTERM occurred */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int realclean; /* flag: true means user said apache is not running */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int verbose; /* flag: true means print statistics */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int benice; /* flag: true means nice mode is activated */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int dryrun; /* flag: true means dry run, don't actually delete
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin anything */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int baselen; /* string length of the path to the proxy directory */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic apr_time_t now; /* start time of this processing run */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic apr_file_t *errfile; /* stderr file handle */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic apr_off_t unsolicited; /* file size summary for deleted unsolicited
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin files */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic APR_RING_ENTRY(_entry) root; /* ENTRY ring anchor */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#ifdef DEBUG
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/*
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * fake delete for debug purposes
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#define apr_file_remove fake_file_remove
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic void fake_file_remove(char *pathname, apr_pool_t *p)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin{
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_finfo_t info;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin /* stat and printing to simulate some deletion system load and to
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin display what would actually have happened */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_stat(&info, pathname, DIRINFO, p);
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_file_printf(errfile, "would delete %s\n", pathname);
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin}
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#endif
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/*
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * called on SIGINT or SIGTERM
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic void setterm(int unused)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin{
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#ifdef DEBUG
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_file_printf(errfile, "interrupt\n");
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin#endif
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin interrupted = 1;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin}
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/*
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * called in out of memory condition
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic int oom(int unused)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin{
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin static int called = 0;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin /* be careful to call exit() only once */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (!called) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin called = 1;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin exit(1);
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin return APR_ENOMEM;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin}
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin/*
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin * print purge statistics
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin */
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrinstatic void printstats(apr_off_t total, apr_off_t sum, apr_off_t max,
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_off_t etotal, apr_off_t entries)
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin{
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin char ttype, stype, mtype, utype;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_off_t tfrag, sfrag, ufrag;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (!verbose) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin return;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin ttype = 'K';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin tfrag = ((total * 10) / KBYTE) % 10;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin total /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (total >= KBYTE) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin ttype = 'M';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin tfrag = ((total * 10) / KBYTE) % 10;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin total /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin stype = 'K';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin sfrag = ((sum * 10) / KBYTE) % 10;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin sum /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (sum >= KBYTE) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin stype = 'M';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin sfrag = ((sum * 10) / KBYTE) % 10;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin sum /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin mtype = 'K';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin max /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (max >= KBYTE) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin mtype = 'M';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin max /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_file_printf(errfile, "Statistics:\n");
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (unsolicited) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin utype = 'K';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin ufrag = ((unsolicited * 10) / KBYTE) % 10;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin unsolicited /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (unsolicited >= KBYTE) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin utype = 'M';
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin ufrag = ((unsolicited * 10) / KBYTE) % 10;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin unsolicited /= KBYTE;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin if (!unsolicited && !ufrag) {
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin ufrag = 1;
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_file_printf(errfile, "unsolicited size %d.%d%c\n",
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin (int)(unsolicited), (int)(ufrag), utype);
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin }
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_file_printf(errfile, "size limit %d.0%c\n", (int)(max), mtype);
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_file_printf(errfile, "total size was %d.%d%c, total size now "
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin "%d.%d%c\n",
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin (int)(total), (int)(tfrag), ttype, (int)(sum),
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin (int)(sfrag), stype);
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin apr_file_printf(errfile, "total entries was %d, total entries now %d\n",
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin (int)(etotal), (int)(entries));
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin}
abe0d0e38b9705f21a13ac8748bce1e3ed35e488minfrin
/*
* delete a single file
*/
static void delete_file(char *path, char *basename, apr_pool_t *pool)
{
char *nextpath;
apr_pool_t *p;
if (dryrun) {
return;
}
/* temp pool, otherwise lots of memory could be allocated */
apr_pool_create(&p, pool);
nextpath = apr_pstrcat(p, path, "/", basename, NULL);
apr_file_remove(nextpath, p);
apr_pool_destroy(p);
if (benice) {
if (++delcount >= DELETE_NICE) {
apr_sleep(NICE_DELAY);
delcount = 0;
}
}
}
/*
* delete cache file set
*/
static void delete_entry(char *path, char *basename, apr_pool_t *pool)
{
char *nextpath;
apr_pool_t *p;
if (dryrun) {
return;
}
/* temp pool, otherwise lots of memory could be allocated */
apr_pool_create(&p, pool);
nextpath = apr_pstrcat(p, path, "/", basename, CACHE_HEADER_SUFFIX, NULL);
apr_file_remove(nextpath, p);
nextpath = apr_pstrcat(p, path, "/", basename, CACHE_DATA_SUFFIX, NULL);
apr_file_remove(nextpath, p);
apr_pool_destroy(p);
if (benice) {
delcount += 2;
if (delcount >= DELETE_NICE) {
apr_sleep(NICE_DELAY);
delcount = 0;
}
}
}
/*
* walk the cache directory tree
*/
static int process_dir(char *path, apr_pool_t *pool)
{
apr_dir_t *dir;
apr_pool_t *p;
apr_hash_t *h;
apr_hash_index_t *i;
apr_file_t *fd;
apr_status_t status;
apr_finfo_t info;
apr_size_t len;
apr_time_t current, deviation;
char *nextpath, *base, *ext;
APR_RING_ENTRY(_direntry) anchor;
DIRENTRY *d, *t, *n;
ENTRY *e;
int skip, retries;
disk_cache_info_t disk_info;
APR_RING_INIT(&anchor, _direntry, link);
apr_pool_create(&p, pool);
h = apr_hash_make(p);
fd = NULL;
skip = 0;
deviation = MAXDEVIATION * APR_USEC_PER_SEC;
if (apr_dir_open(&dir, path, p) != APR_SUCCESS) {
return 1;
}
while (apr_dir_read(&info, 0, dir) == APR_SUCCESS && !interrupted) {
/* skip first two entries which will always be '.' and '..' */
if (skip < 2) {
skip++;
continue;
}
d = apr_pcalloc(p, sizeof(DIRENTRY));
d->basename = apr_pstrcat(p, path, "/", info.name, NULL);
APR_RING_INSERT_TAIL(&anchor, d, _direntry, link);
}
apr_dir_close(dir);
if (interrupted) {
return 1;
}
skip = baselen + 1;
for (d = APR_RING_FIRST(&anchor);
!interrupted && d != APR_RING_SENTINEL(&anchor, _direntry, link);
d=n) {
n = APR_RING_NEXT(d, link);
base = strrchr(d->basename, '/');
if (!base++) {
base = d->basename;
}
ext = strchr(base, '.');
/* there may be temporary files which may be gone before
* processing, always skip these if not in realclean mode
*/
if (!ext && !realclean) {
if (!strncasecmp(base, AP_TEMPFILE_BASE, AP_TEMPFILE_BASELEN)
&& strlen(base) == AP_TEMPFILE_NAMELEN) {
continue;
}
}
/* this may look strange but apr_stat() may return errno which
* is system dependent and there may be transient failures,
* so just blindly retry for a short while
*/
retries = STAT_ATTEMPTS;
status = APR_SUCCESS;
do {
if (status != APR_SUCCESS) {
apr_sleep(STAT_DELAY);
}
status = apr_stat(&info, d->basename, DIRINFO, p);
} while (status != APR_SUCCESS && !interrupted && --retries);
/* what may happen here is that apache did create a file which
* we did detect but then does delete the file before we can
* get file information, so if we don't get any file information
* we will ignore the file in this case
*/
if (status != APR_SUCCESS) {
if (!realclean && !interrupted) {
continue;
}
return 1;
}
if (info.filetype == APR_DIR) {
if (process_dir(d->basename, pool)) {
return 1;
}
continue;
}
if (info.filetype != APR_REG) {
continue;
}
if (!ext) {
if (!strncasecmp(base, AP_TEMPFILE_BASE, AP_TEMPFILE_BASELEN)
&& strlen(base) == AP_TEMPFILE_NAMELEN) {
d->basename += skip;
d->type = TEMP;
d->dsize = info.size;
apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
}
continue;
}
if (!strcasecmp(ext, CACHE_HEADER_SUFFIX)) {
*ext = '\0';
d->basename += skip;
/* if a user manually creates a '.header' file */
if (d->basename[0] == '\0') {
continue;
}
t = apr_hash_get(h, d->basename, APR_HASH_KEY_STRING);
if (t) {
d = t;
}
d->type |= HEADER;
d->htime = info.mtime;
d->hsize = info.size;
apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
continue;
}
if (!strcasecmp(ext, CACHE_DATA_SUFFIX)) {
*ext = '\0';
d->basename += skip;
/* if a user manually creates a '.data' file */
if (d->basename[0] == '\0') {
continue;
}
t = apr_hash_get(h, d->basename, APR_HASH_KEY_STRING);
if (t) {
d = t;
}
d->type |= DATA;
d->dtime = info.mtime;
d->dsize = info.size;
apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
}
}
if (interrupted) {
return 1;
}
path[baselen] = '\0';
for (i = apr_hash_first(p, h); i && !interrupted; i = apr_hash_next(i)) {
apr_hash_this(i, NULL, NULL, (void **)(&d));
switch(d->type) {
case HEADERDATA:
nextpath = apr_pstrcat(p, path, "/", d->basename,
CACHE_HEADER_SUFFIX, NULL);
if (apr_file_open(&fd, nextpath, APR_READ, APR_OS_DEFAULT,
p) == APR_SUCCESS) {
len = sizeof(disk_cache_info_t);
if (apr_file_read_full(fd, &disk_info, len,
&len) == APR_SUCCESS) {
apr_file_close(fd);
if (disk_info.format == DISK_FORMAT_VERSION) {
e = apr_palloc(pool, sizeof(ENTRY));
APR_RING_INSERT_TAIL(&root, e, _entry, link);
e->expire = disk_info.expire;
e->response_time = disk_info.response_time;
e->htime = d->htime;
e->dtime = d->dtime;
e->hsize = d->hsize;
e->dsize = d->dsize;
e->basename = apr_palloc(pool,
strlen(d->basename) + 1);
strcpy(e->basename, d->basename);
break;
}
}
else {
apr_file_close(fd);
}
}
/* we have a somehow unreadable headers file which is associated
* with a data file. this may be caused by apache currently
* rewriting the headers file. thus we may delete the file set
* either in realclean mode or if the headers file modification
* timestamp is not within a specified positive or negative offset
* to the current time.
*/
current = apr_time_now();
if (realclean || d->htime < current - deviation
|| d->htime > current + deviation) {
delete_entry(path, d->basename, p);
unsolicited += d->hsize;
unsolicited += d->dsize;
}
break;
/* single data and header files may be deleted either in realclean
* mode or if their modification timestamp is not within a
* specified positive or negative offset to the current time.
* this handling is necessary due to possible race conditions
* between apache and this process
*/
case HEADER:
current = apr_time_now();
if (realclean || d->htime < current - deviation
|| d->htime > current + deviation) {
delete_entry(path, d->basename, p);
unsolicited += d->hsize;
}
break;
case DATA:
current = apr_time_now();
if (realclean || d->dtime < current - deviation
|| d->dtime > current + deviation) {
delete_entry(path, d->basename, p);
unsolicited += d->dsize;
}
break;
/* temp files may only be deleted in realclean mode which
* is asserted above if a tempfile is in the hash array
*/
case TEMP:
delete_file(path, d->basename, p);
unsolicited += d->dsize;
break;
}
}
if (interrupted) {
return 1;
}
apr_pool_destroy(p);
if (benice) {
apr_sleep(NICE_DELAY);
}
if (interrupted) {
return 1;
}
return 0;
}
/*
* purge cache entries
*/
static void purge(char *path, apr_pool_t *pool, apr_off_t max)
{
apr_off_t sum, total, entries, etotal;
ENTRY *e, *n, *oldest;
sum = 0;
entries = 0;
for (e = APR_RING_FIRST(&root);
e != APR_RING_SENTINEL(&root, _entry, link);
e = APR_RING_NEXT(e, link)) {
sum += e->hsize;
sum += e->dsize;
entries++;
}
total = sum;
etotal = entries;
if (sum <= max) {
printstats(total, sum, max, etotal, entries);
return;
}
/* process all entries with a timestamp in the future, this may
* happen if a wrong system time is corrected
*/
for (e = APR_RING_FIRST(&root);
e != APR_RING_SENTINEL(&root, _entry, link) && !interrupted;) {
n = APR_RING_NEXT(e, link);
if (e->response_time > now || e->htime > now || e->dtime > now) {
delete_entry(path, e->basename, pool);
sum -= e->hsize;
sum -= e->dsize;
entries--;
APR_RING_REMOVE(e, link);
if (sum <= max) {
if (!interrupted) {
printstats(total, sum, max, etotal, entries);
}
return;
}
}
e = n;
}
if (interrupted) {
return;
}
/* process all entries with are expired */
for (e = APR_RING_FIRST(&root);
e != APR_RING_SENTINEL(&root, _entry, link) && !interrupted;) {
n = APR_RING_NEXT(e, link);
if (e->expire != APR_DATE_BAD && e->expire < now) {
delete_entry(path, e->basename, pool);
sum -= e->hsize;
sum -= e->dsize;
entries--;
APR_RING_REMOVE(e, link);
if (sum <= max) {
if (!interrupted) {
printstats(total, sum, max, etotal, entries);
}
return;
}
}
e = n;
}
if (interrupted) {
return;
}
/* process remaining entries oldest to newest, the check for an emtpy
* ring actually isn't necessary except when the compiler does
* corrupt 64bit arithmetics which happend to me once, so better safe
* than sorry
*/
while (sum > max && !interrupted && !APR_RING_EMPTY(&root, _entry, link)) {
oldest = APR_RING_FIRST(&root);
for (e = APR_RING_NEXT(oldest, link);
e != APR_RING_SENTINEL(&root, _entry, link);
e = APR_RING_NEXT(e, link)) {
if (e->dtime < oldest->dtime) {
oldest = e;
}
}
delete_entry(path, oldest->basename, pool);
sum -= oldest->hsize;
sum -= oldest->dsize;
entries--;
APR_RING_REMOVE(oldest, link);
}
if (!interrupted) {
printstats(total, sum, max, etotal, entries);
}
}
/*
* usage info
*/
static void usage(void)
{
apr_file_printf(errfile, "htcacheclean -- program for cleaning the "
"disk cache.\n");
apr_file_printf(errfile, "Usage: htcacheclean [-Dvrn] -pPATH -lLIMIT\n");
apr_file_printf(errfile, " htcacheclean [-Dvrn] -pPATH -LLIMIT\n");
apr_file_printf(errfile, " htcacheclean [-ni] -dINTERVAL -pPATH "
"-lLIMIT\n");
apr_file_printf(errfile, " htcacheclean [-ni] -dINTERVAL -pPATH "
"-LLIMIT\n");
apr_file_printf(errfile, "Options:\n");
apr_file_printf(errfile, " -d Daemonize and repeat cache cleaning "
"every INTERVAL minutes. This\n"
" option is mutually exclusive with "
"the -D, -v and -r options.\n");
apr_file_printf(errfile, " -D Do a dry run and don't delete anything. "
"This option is mutually\n"
" exclusive with the -d option.\n");
apr_file_printf(errfile, " -v Be verbose and print statistics. "
"This option is mutually exclusive\n"
" with the -d option.\n");
apr_file_printf(errfile, " -r Clean thoroughly. This assumes that "
"the Apache web server\n"
" is not running. This option is "
"mutually exclusive with the -d option.\n");
apr_file_printf(errfile, " -n Be nice. This causes slower processing "
"in favour of other processes.\n");
apr_file_printf(errfile, " -p Specify PATH as the root directory of "
"the disk cache.\n");
apr_file_printf(errfile, " -l Specify LIMIT as the total disk cache "
"size limit in KBytes.\n");
apr_file_printf(errfile, " -L Specify LIMIT as the total disk cache "
"size limit in MBytes.\n");
apr_file_printf(errfile, " -i Be intelligent and run only when there "
"was a modification\n"
" of the disk cache. This option is only "
"possible together with\n"
" the -d option.\n");
exit(1);
}
/*
* main
*/
int main(int argc, const char * const argv[])
{
apr_off_t max;
apr_time_t current, repeat, delay, previous;
apr_status_t status;
apr_pool_t *pool, *instance;
apr_getopt_t *o;
apr_finfo_t info;
int retries, isdaemon, limit_found, intelligent, dowork;
char opt;
const char *arg;
char *proxypath, *path;
interrupted = 0;
repeat = 0;
isdaemon = 0;
dryrun = 0;
limit_found = 0;
max = 0;
verbose = 0;
realclean = 0;
benice = 0;
intelligent = 0;
proxypath = NULL;
if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
return 1;
}
atexit(apr_terminate);
if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
return 1;
}
apr_pool_abort_set(oom, pool);
apr_file_open_stderr(&errfile, pool);
apr_signal(SIGINT, setterm);
apr_signal(SIGTERM, setterm);
apr_getopt_init(&o, pool, argc, argv);
while (1) {
status = apr_getopt(o, "iDnvrd:l:L:p:", &opt, &arg);
if (status == APR_EOF) {
break;
}
else if (status != APR_SUCCESS) {
usage();
}
else {
switch (opt) {
case 'i':
if (intelligent) {
usage();
}
intelligent = 1;
break;
case 'D':
if (dryrun) {
usage();
}
dryrun = 1;
break;
case 'n':
if (benice) {
usage();
}
benice = 1;
break;
case 'v':
if (verbose) {
usage();
}
verbose = 1;
break;
case 'r':
if (realclean) {
usage();
}
realclean = 1;
break;
case 'd':
if (isdaemon) {
usage();
}
isdaemon = 1;
repeat = apr_atoi64(arg);
repeat *= SECS_PER_MIN;
repeat *= APR_USEC_PER_SEC;
break;
case 'l':
if (limit_found) {
usage();
}
limit_found = 1;
max = apr_atoi64(arg);
max *= KBYTE;
break;
case 'L':
if (limit_found) {
usage();
}
limit_found = 1;
max = apr_atoi64(arg);
max *= MBYTE;
break;
case 'p':
if (proxypath) {
usage();
}
proxypath = apr_pstrdup(pool, arg);
if (apr_filepath_set(proxypath, pool) != APR_SUCCESS) {
usage();
}
break;
} /* switch */
} /* else */
} /* while */
if (o->ind != argc) {
usage();
}
if (isdaemon && (repeat <= 0 || verbose || realclean || dryrun)) {
usage();
}
if (!isdaemon && intelligent) {
usage();
}
if (!proxypath || max <= 0) {
usage();
}
if (apr_filepath_get(&path, 0, pool) != APR_SUCCESS) {
usage();
}
baselen = strlen(path);
#ifndef DEBUG
if (isdaemon) {
apr_file_close(errfile);
apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
}
#endif
do {
apr_pool_create(&instance, pool);
now = apr_time_now();
APR_RING_INIT(&root, _entry, link);
delcount = 0;
unsolicited = 0;
dowork = 0;
switch (intelligent) {
case 0:
dowork = 1;
break;
case 1:
retries = STAT_ATTEMPTS;
status = APR_SUCCESS;
do {
if (status != APR_SUCCESS) {
apr_sleep(STAT_DELAY);
}
status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
} while (status != APR_SUCCESS && !interrupted && --retries);
if (status == APR_SUCCESS) {
previous = info.mtime;
intelligent = 2;
}
dowork = 1;
break;
case 2:
retries = STAT_ATTEMPTS;
status = APR_SUCCESS;
do {
if (status != APR_SUCCESS) {
apr_sleep(STAT_DELAY);
}
status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
} while (status != APR_SUCCESS && !interrupted && --retries);
if (status == APR_SUCCESS) {
if (previous != info.mtime) {
dowork = 1;
}
previous = info.mtime;
break;
}
intelligent = 1;
dowork = 1;
break;
}
if (dowork && !interrupted) {
if (!process_dir(path, instance) && !interrupted) {
purge(path, instance, max);
}
else if (!isdaemon && !interrupted) {
apr_file_printf(errfile,
"An error occurred, cache cleaning aborted.\n");
return 1;
}
if (intelligent && !interrupted) {
retries = STAT_ATTEMPTS;
status = APR_SUCCESS;
do {
if (status != APR_SUCCESS) {
apr_sleep(STAT_DELAY);
}
status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
} while (status != APR_SUCCESS && !interrupted && --retries);
if (status == APR_SUCCESS) {
previous = info.mtime;
intelligent = 2;
}
else {
intelligent = 1;
}
}
}
apr_pool_destroy(instance);
current = apr_time_now();
if (current < now) {
delay = repeat;
}
else if (current - now >= repeat) {
delay = repeat;
}
else {
delay = now + repeat - current;
}
/* we can't sleep the whole delay time here apiece as this is racy
* with respect to interrupt delivery - think about what happens
* if we have tested for an interrupt, then get scheduled
* before the apr_sleep() call and while waiting for the cpu
* we do get an interrupt
*/
if (isdaemon) {
while (delay && !interrupted) {
if (delay > APR_USEC_PER_SEC) {
apr_sleep(APR_USEC_PER_SEC);
delay -= APR_USEC_PER_SEC;
}
else {
apr_sleep(delay);
delay = 0;
}
}
}
} while (isdaemon && !interrupted);
if (!isdaemon && interrupted) {
apr_file_printf(errfile,
"Cache cleaning aborted due to user request.\n");
return 1;
}
return 0;
}