rotatelogs.c revision 306d2e9a37bb1314a777897017954e547c6bd9ff
/* 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_thread_proc.h"
#include "apr_poll.h"
#endif
#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;
int echo;
const char *szLogRoot;
int truncate;
const char *linkfile;
const char *postrotate_prog;
int create_empty;
#endif
};
typedef struct rotate_status rotate_status_t;
/* Structure to contain relevant logfile state: fd, pool and
* filename. */
struct logfile {
apr_file_t *fd;
};
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] [-L linkname] [-p prog] [-f] [-t] [-e] [-c] <logfile> "
#else
"Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] <logfile> "
#endif
"{<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. By default, the generated name will be\n"
"<logfile>.nnnn where nnnn is the system time at which the log\n"
"nominally starts (N.B. if using a rotation time, the time will\n"
"always be a multiple of the rotation time, so you can synchronize\n"
"cron scripts with it). If <logfile> contains strftime conversion\n"
"specifications, those will be used instead. At the end of each\n"
"rotation time or when the file size is reached a new log is\n"
"started.\n"
"\n"
"Options:\n"
" -v Verbose operation. Messages are written to stderr.\n"
" -l Base rotation on local time instead of UTC.\n"
" -L path Create hard link from current log to specified path.\n"
" -p prog Run specified program after opening a new log file. See below.\n"
" -f Force opening of log on program start.\n"
" -t Truncate logfile instead of rotating, tail friendly.\n"
" -e Echo log to stdout for further processing.\n"
" -c Create log even if it is empty.\n"
#endif
"\n"
"The program is invoked as \"[prog] <curfile> [<prevfile>]\"\n"
"where <curfile> is the filename of the newly opened logfile, and\n"
"<prevfile>, if given, is the filename of the previously used logfile.\n"
"\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.
*/
{
}
}
/*
* Dump the configuration parsing result to STDERR.
*/
{
#endif
}
/*
* 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);
}
}
}
/*
* Handle post-rotate processing.
*/
{
char error[120];
const char *argv[4];
/* Handle link file, if configured. */
}
if (rv != APR_SUCCESS) {
char error[120];
exit(2);
}
}
if (!config->postrotate_prog) {
/* Nothing more to do. */
return;
}
/* Collect any zombies from a previous run, but don't wait. */
/* noop */;
"post_rotate: apr_procattr_create failed for '%s': %s\n",
return;
}
if (rv == APR_SUCCESS)
if (rv != APR_SUCCESS) {
"post_rotate: could not set up process attributes for '%s': %s\n",
return;
}
}
else {
}
if (rv != APR_SUCCESS) {
return;
}
}
/* After a error, truncate the current file and write out an error
* message, which must be contained in status->errbuf. The process is
* terminated on failure. */
{
exit(2);
}
exit(2);
}
}
/*
* 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 {
}
else {
}
}
}
if (rv == APR_SUCCESS) {
/* Handle post-rotate processing. */
/* Close out old (previously 'current') logfile, if any. */
}
/* New log file is now 'current'. */
}
else {
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);
}
/* Throw away new state; it isn't going to be used. */
/* 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",
}
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 *opt_arg;
apr_pollfd_t pollfd = { 0 };
int polltimeout;
#endif
#else
#endif
switch (c) {
case 'l':
break;
case 'L':
break;
case 'p':
break;
case 'f':
break;
case 't':
break;
case 'v':
break;
case 'e':
break;
case 'c':
break;
#endif
}
}
}
/*
* 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);
}
exit(1);
}
/*
* Write out result of config parsing if verbose is set.
*/
dumpConfig(&config);
}
}
#endif
/*
* Immediately open the logfile as we start, if we were forced
* to do so via '-f'.
*/
if (config.force_open) {
}
for (;;) {
if (polltimeout <= 0) {
}
else {
}
}
if (pollret == APR_SUCCESS) {
if (APR_STATUS_IS_EOF(rv)) {
break;
}
else if (rv != APR_SUCCESS) {
exit(3);
}
}
else if (pollret == APR_TIMEUP) {
*buf = 0;
nRead = 0;
}
else {
exit(5);
}
#else /* APR_FILES_AS_SOCKETS */
if (APR_STATUS_IS_EOF(rv)) {
break;
}
else if (rv != APR_SUCCESS) {
exit(3);
}
#endif /* APR_FILES_AS_SOCKETS */
}
char strerrbuf[120];
cur_offset = 0;
cur_offset = -1;
}
status.nMessCount++;
"%10d messages lost (%s)\n",
}
else {
status.nMessCount++;
}
exit(4);
}
}
}
return 0; /* reached only at stdin EOF. */
}