rotatelogs.c revision 9e152751ed380f87c5ecae4fb0221c956e5fbd24
/* 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 "apr_signal.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
#ifdef SIGHUP
#ifdef SIGINT
#define HAVE_SIGNALS 1
#endif
#endif
#define CHECK_LOG 0
#define CHECK_SIG_CHECK 1
#define CHECK_SIG_FORCE 2
#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 checkReason;
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 in megabytes>} "
"[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);
}
{
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.
*/
}
}
{
}
}
if (pool) {
}
}
}
{
}
{
}
}
}
}
}
}
}
else {
exit(2);
}
}
/*
* Let's close the file before immediately
* if we got here via a signal.
*/
}
return;
}
{
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;
/*
* Reset marker for signal triggered rotation
*/
}
#ifdef HAVE_SIGNALS
/*
* called on SIG_CHECK and SIG_FORCE
*/
static void external_rotate(int signal)
{
/*
* Set marker for signal triggered rotation
*/
}
else {
}
/*
* Close old file conditionally
*/
/*
* Open new file if force flag was set
*/
}
}
#endif
{
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;
}
}
}
}
if (ptr) { /* rotation based on file size */
}
}
}
else { /* rotation based on elapsed time */
}
}
if (config.use_localtime) {
}
}
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) {
}
#ifdef HAVE_SIGNALS
#endif
for (;;) {
#ifdef HAVE_SIGNALS
#endif
#ifdef HAVE_SIGNALS
#endif
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;
}