rotatelogs.c revision 4a13940dc2990df0a798718d3a3f9cf1566c2217
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Simple program to rotate Apache logs without having to kill the server.
*
* Contributed by Ben Laurie <ben algroup.co.uk>
*
* 12 Mar 1996
*
* Ported to APR by Mladen Turk <mturk mappingsoft.com>
*
* 23 Sep 2001
*
* -l option added 2004-06-11
*
* -l causes the use of local time rather than GMT as the base for the
* interval. NB: Using -l in an environment which changes the GMT offset
* (such as for BST or DST) can lead to unpredictable results!
*
* the logfile as soon as it's started, not as soon as it sees
* data.
*
* -v option added Feb, 2008. Verbose output of command line parsing.
*/
#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_errno.h"
#include "apr_file_io.h"
#include "apr_file_info.h"
#include "apr_general.h"
#include "apr_time.h"
#include "apr_getopt.h"
#include <stdlib.h>
#endif
#define APR_WANT_STRFUNC
#include "apr_want.h"
#define BUFSIZE 65536
#define ERRMSGSZ 256
#ifndef MAX_PATH
#define MAX_PATH 1024
#endif
#define ROTATE_NONE 0
#define ROTATE_NEW 1
#define ROTATE_TIME 2
#define ROTATE_SIZE 3
#define ROTATE_FORCE 4
static const char *ROTATE_REASONS[] = {
"None",
"Open a new file",
"Time interval expired",
"Maximum size reached",
"Forced rotation",
};
typedef struct rotate_config rotate_config_t;
struct rotate_config {
unsigned int sRotation;
int tRotation;
int utc_offset;
int use_localtime;
int use_strftime;
int force_open;
int verbose;
const char *szLogRoot;
};
typedef struct rotate_status rotate_status_t;
struct rotate_status {
int rotateReason;
int tLogEnd;
int nMessCount;
};
static rotate_config_t config;
static rotate_status_t status;
{
if (reason) {
}
"Usage: %s [-v] [-l] [-f] <logfile> "
"{<rotation time in seconds>|<rotation size>(B|K|M|G)} "
"[offset minutes from UTC]\n\n",
argv0);
#ifdef OS2
argv0);
#else
argv0);
#endif
"to httpd.conf. The generated name will be /some/where.nnnn "
"where nnnn is the\nsystem time at which the log nominally "
"starts (N.B. if using a rotation time,\nthe time will always "
"be a multiple of the rotation time, so you can synchronize\n"
"cron scripts with it). At the end of each rotation time or "
"when the file size\nis reached a new log is started.\n");
exit(1);
}
/*
* Get the unix time with timezone corrections
* given in the config struct.
*/
{
if (config->use_localtime) {
/* Check for our UTC offset before using it, since it might
* change if there's a switch between standard and daylight
* savings time.
*/
}
}
/*
* Close a file and destroy the associated pool.
*/
{
}
}
if (pool) {
}
}
}
/*
* Dump the configuration parsing result to STDERR.
*/
{
}
/*
* Check whether we need to rotate.
* Possible reasons are:
* - No log file open (ROTATE_NEW)
* - User forces us to rotate (ROTATE_FORCE)
* - Our log file size is already bigger than the
* allowed maximum (ROTATE_SIZE)
* - The next log time interval expired (ROTATE_TIME)
*
* When size and time constraints are both given,
* it suffices that one of them is fulfilled.
*/
{
}
}
}
}
}
}
}
}
else {
exit(2);
}
}
return;
}
/*
* Open a new log file, and if successful
* also close the old one.
*
* The timestamp for the calculation of the file
* name of the new log file will be the actual millisecond
* timestamp, except when a regular rotation based on a time
* interval is configured and the previous interval
* is over. Then the timestamp is the starting time
* of the actual interval.
*/
{
int tLogStart;
int tLogEnd;
/*
* Check if rotation was forced and the last rotation
* interval is not yet over. Use the value of now instead
* of the time interval boundary for the file name then.
*/
}
}
else {
}
if (config->use_strftime) {
apr_time_exp_gmt(&e, tNow);
}
else {
}
}
if (rv != APR_SUCCESS) {
char error[120];
/* Uh-oh. Failed to open the new log file. Try to clear
* the previous log file, note the lost log entries,
* and keep on truckin'. */
exit(2);
}
else {
/* Try to keep this error message constant length
* in case it occurs several times. */
"Resetting log file due to error opening "
"new log file, %10d messages lost: %-25.25s\n",
exit(2);
}
}
}
else {
}
status->nMessCount = 0;
}
/*
* Get a size or time param from a string.
* Parameter 'last' indicates, whether the
* argument is the last commadnline argument.
* UTC offset is only allowed as a last argument
* in order to make is distinguishable from the
* rotation interval time.
*/
/* Byte multiplier */
unsigned int mult = 1;
mult = 1;
}
mult = 1024;
}
}
}
if (ptr) { /* rotation based on file size */
return "Rotation size parameter allowed only once";
}
}
return "Invalid rotation size parameter";
}
}
/* rotation based on elapsed time */
if (config->use_localtime) {
return "UTC offset parameter is not valid with -l";
}
}
else { /* rotation based on elapsed time */
return "Rotation time parameter allowed only once";
}
return "Invalid rotation time parameter";
}
}
return NULL;
}
{
char c;
const char *optarg;
config.utc_offset = 0;
config.use_localtime = 0;
config.use_strftime = 0;
config.force_open = 0;
status.nMessCount = 0;
switch (c) {
case 'l':
break;
case 'f':
break;
case 'v':
break;
}
}
}
/*
* After the initial flags we need 2 to 4 arguments,
* the file name, either the rotation interval time or size
* or both of them, and optionally the UTC offset.
*/
}
/* Read in the remaining flags, namely time, size and UTC offset. */
}
}
exit(1);
}
/*
* Write out result of config parsing if verbose is set.
*/
dumpConfig(&config);
}
/*
* Immediately open the logfile as we start, if we were forced
* to do so via '-f'.
*/
if (config.force_open) {
}
for (;;) {
if (rv != APR_SUCCESS) {
exit(3);
}
}
/* buffer partially written, which for rotatelogs means we encountered
* an error such as out of space or quota or some other limit reached;
* try to write the rest so we get the real error code
*/
}
char strerrbuf[120];
cur_offset = 0;
cur_offset = -1;
}
status.nMessCount++;
"%10d messages lost (%s)\n",
exit(2);
}
}
else {
status.nMessCount++;
}
}
/* Of course we never, but prevent compiler warnings */
return 0;
}