#include "apr_time.h"
#include "apr_getopt.h"
#include "apr_thread_proc.h"
+#include "apr_signal.h"
#if APR_FILES_AS_SOCKETS
#include "apr_poll.h"
#endif
#include "apr_want.h"
#define BUFSIZE 65536
-#define ERRMSGSZ 256
#define ROTATE_NONE 0
#define ROTATE_NEW 1
int force_open;
int verbose;
int echo;
- const char *szLogRoot;
+ char *szLogRoot;
int truncate;
const char *linkfile;
const char *postrotate_prog;
int create_empty;
#endif
int num_files;
+ int create_path;
};
typedef struct rotate_status rotate_status_t;
+/* "adjusted_time_t" is used to store Unix time (seconds since epoch)
+ * which has been adjusted for some timezone fudge factor. It should
+ * be used for storing the return values from get_now(). A typedef is
+ * used since this type is similar to time_t, but different. */
+typedef long adjusted_time_t;
+
/* Structure to contain relevant logfile state: fd, pool and
* filename. */
struct logfile {
struct rotate_status {
struct logfile current; /* current logfile. */
apr_pool_t *pool; /* top-level pool */
- char errbuf[ERRMSGSZ];
int rotateReason;
- int tLogEnd;
+ adjusted_time_t tLogEnd;
int nMessCount;
int fileNum;
};
}
fprintf(stderr,
#if APR_FILES_AS_SOCKETS
- "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] [-c] [-n number] <logfile> "
+ "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-D] [-t] [-e] [-c] [-n number] <logfile> "
#else
- "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] [-n number] <logfile> "
+ "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-D] [-t] [-e] [-n number] <logfile> "
#endif
"{<rotation time in seconds>|<rotation size>(B|K|M|G)} "
"[offset minutes from UTC]\n\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"
+ " -D Create parent directories of log file.\n"
" -t Truncate logfile instead of rotating, tail friendly.\n"
" -e Echo log to stdout for further processing.\n"
#if APR_FILES_AS_SOCKETS
" -c Create log even if it is empty.\n"
#endif
+ " -n num Rotate file by adding suffixes '.0', '.1', ..., '.(num-1)'.\n"
"\n"
- "The program is invoked as \"[prog] <curfile> [<prevfile>]\"\n"
+ "The program for '-p' 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.
- */
-static int get_now(rotate_config_t *config)
+/* This function returns the current Unix time (time_t) adjusted for
+ * any configured or derived local time offset. The offset applied is
+ * returned via *offset. */
+static adjusted_time_t get_now(rotate_config_t *config, apr_int32_t *offset)
{
apr_time_t tNow = apr_time_now();
- int utc_offset = config->utc_offset;
+ apr_int32_t utc_offset;
+
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
apr_time_exp_lt(<, tNow);
utc_offset = lt.tm_gmtoff;
}
- return (int)apr_time_sec(tNow) + utc_offset;
+ else {
+ utc_offset = config->utc_offset;
+ }
+
+ if (offset)
+ *offset = utc_offset;
+
+ return apr_time_sec(tNow) + utc_offset;
}
/*
fprintf(stderr, "Rotation based on localtime: %12s\n", config->use_localtime ? "yes" : "no");
fprintf(stderr, "Rotation file date pattern: %12s\n", config->use_strftime ? "yes" : "no");
fprintf(stderr, "Rotation file forced open: %12s\n", config->force_open ? "yes" : "no");
+ fprintf(stderr, "Create parent directories: %12s\n", config->create_path ? "yes" : "no");
fprintf(stderr, "Rotation verbose: %12s\n", config->verbose ? "yes" : "no");
#if APR_FILES_AS_SOCKETS
fprintf(stderr, "Rotation create empty logs: %12s\n", config->create_empty ? "yes" : "no");
#endif
fprintf(stderr, "Rotation file name: %21s\n", config->szLogRoot);
- fprintf(stderr, "Post-rotation prog: %21s\n", config->postrotate_prog);
+ fprintf(stderr, "Post-rotation prog: %21s\n", config->postrotate_prog ? config->postrotate_prog : "not used");
}
/*
status->rotateReason = ROTATE_SIZE;
}
else if (config->tRotation) {
- if (get_now(config) >= status->tLogEnd) {
+ if (get_now(config, NULL) >= status->tLogEnd) {
status->rotateReason = ROTATE_TIME;
}
}
}
else if (config->tRotation) {
- if (get_now(config) >= status->tLogEnd) {
+ if (get_now(config, NULL) >= status->tLogEnd) {
status->rotateReason = ROTATE_TIME;
}
}
rotate_config_t *config, rotate_status_t *status)
{
apr_status_t rv;
- char error[120];
apr_procattr_t *pattr;
const char *argv[4];
apr_proc_t proc;
if (config->linkfile) {
apr_file_remove(config->linkfile, newlog->pool);
if (config->verbose) {
- fprintf(stderr,"Linking %s to %s\n", newlog->name, config->linkfile);
+ fprintf(stderr, "Linking %s to %s\n", newlog->name, config->linkfile);
}
rv = apr_file_link(newlog->name, config->linkfile);
if (rv != APR_SUCCESS) {
- apr_strerror(rv, error, sizeof error);
- fprintf(stderr, "Error linking file %s to %s (%s)\n",
- newlog->name, config->linkfile, error);
+ char *error = apr_psprintf(pool, "Error linking file %s to %s (%pm)\n",
+ newlog->name, config->linkfile, &rv);
+ fputs(error, stderr);
exit(2);
}
}
/* noop */;
if ((rv = apr_procattr_create(&pattr, pool)) != APR_SUCCESS) {
- fprintf(stderr,
- "post_rotate: apr_procattr_create failed for '%s': %s\n",
- config->postrotate_prog,
- apr_strerror(rv, error, sizeof(error)));
+ char *error = apr_psprintf(pool, "post_rotate: apr_procattr_create failed " \
+ "for '%s': %pm\n", config->postrotate_prog, &rv);
+ fputs(error, stderr);
return;
}
rv = apr_procattr_cmdtype_set(pattr, APR_PROGRAM_ENV);
if (rv != APR_SUCCESS) {
- fprintf(stderr,
- "post_rotate: could not set up process attributes for '%s': %s\n",
- config->postrotate_prog,
- apr_strerror(rv, error, sizeof(error)));
+ char *error = apr_psprintf(pool, "post_rotate: could not set up process " \
+ "attributes for '%s': %pm\n", config->postrotate_prog,
+ &rv);
+ fputs(error, stderr);
return;
}
rv = apr_proc_create(&proc, argv[0], argv, NULL, pattr, pool);
if (rv != APR_SUCCESS) {
- fprintf(stderr, "Could not spawn post-rotate process '%s': %s\n",
- config->postrotate_prog,
- apr_strerror(rv, error, sizeof(error)));
+ char *error = apr_psprintf(pool, "Could not spawn post-rotate process " \
+ "'%s': %pm\n", config->postrotate_prog, &rv);
+ fputs(error, stderr);
return;
}
}
/* After a error, truncate the current file and write out an error
- * message, which must be contained in status->errbuf. The process is
+ * message, which must be contained in message. The process is
* terminated on failure. */
-static void truncate_and_write_error(rotate_status_t *status)
+static void truncate_and_write_error(rotate_status_t *status, const char *message)
{
- apr_size_t buflen = strlen(status->errbuf);
+ apr_size_t buflen = strlen(message);
if (apr_file_trunc(status->current.fd, 0) != APR_SUCCESS) {
fprintf(stderr, "Error truncating the file %s\n", status->current.name);
exit(2);
}
- if (apr_file_write_full(status->current.fd, status->errbuf, buflen, NULL) != APR_SUCCESS) {
+ if (apr_file_write_full(status->current.fd, message, buflen, NULL) != APR_SUCCESS) {
fprintf(stderr, "Error writing error (%s) to the file %s\n",
- status->errbuf, status->current.name);
+ message, status->current.name);
exit(2);
}
}
*/
static void doRotate(rotate_config_t *config, rotate_status_t *status)
{
-
- int now = get_now(config);
- int tLogStart;
+ apr_int32_t offset;
+ adjusted_time_t now, tLogStart;
apr_status_t rv;
struct logfile newlog;
int thisLogNum = -1;
+ /* Retrieve local-time-adjusted-Unix-time. */
+ now = get_now(config, &offset);
+
status->rotateReason = ROTATE_NONE;
if (config->tRotation) {
- int tLogEnd;
+ adjusted_time_t tLogEnd;
+
tLogStart = (now / config->tRotation) * config->tRotation;
tLogEnd = tLogStart + config->tRotation;
/*
apr_time_exp_t e;
apr_size_t rs;
- apr_time_exp_gmt(&e, tNow);
+ /* Explode the local-time-adjusted-Unix-time into a struct tm,
+ * first *reversing* local-time-adjustment applied by
+ * get_now() if we are using localtime. */
+ if (config->use_localtime)
+ apr_time_exp_lt(&e, tNow - apr_time_from_sec(offset));
+ else
+ apr_time_exp_gmt(&e, tNow);
apr_strftime(newlog.name, &rs, sizeof(newlog.name), config->szLogRoot, &e);
}
else {
}
}
else {
- apr_snprintf(newlog.name, sizeof(newlog.name), "%s.%010d", config->szLogRoot,
+ apr_snprintf(newlog.name, sizeof(newlog.name), "%s.%010ld", config->szLogRoot,
tLogStart);
}
}
apr_pool_create(&newlog.pool, status->pool);
+ if (config->create_path) {
+ char *ptr = strrchr(newlog.name, '/');
+ if (ptr && ptr > newlog.name) {
+ char *path = apr_pstrmemdup(newlog.pool, newlog.name, ptr - newlog.name);
+ if (config->verbose) {
+ fprintf(stderr, "Creating directory tree %s\n", path);
+ }
+ rv = apr_dir_make_recursive(path, APR_FPROT_OS_DEFAULT, newlog.pool);
+ if (rv != APR_SUCCESS) {
+ char *error = apr_psprintf(newlog.pool,
+ "Could not create directory '%s' (%pm)\n",
+ path, &rv);
+ fputs(error, stderr);
+ exit(2);
+ }
+ }
+ }
if (config->verbose) {
fprintf(stderr, "Opening file %s\n", newlog.name);
}
status->current = newlog;
}
else {
- char error[120];
-
- apr_strerror(rv, error, sizeof error);
+ char *error = apr_psprintf(newlog.pool, "%pm", &rv);
+ char *message;
/* Uh-oh. Failed to open the new log file. Try to clear
* the previous log file, note the lost log entries,
exit(2);
}
- /* Throw away new state; it isn't going to be used. */
- apr_pool_destroy(newlog.pool);
-
/* Try to keep this error message constant length
* in case it occurs several times. */
- apr_snprintf(status->errbuf, sizeof status->errbuf,
- "Resetting log file due to error opening "
- "new log file, %10d messages lost: %-25.25s\n",
- status->nMessCount, error);
+ message = apr_psprintf(newlog.pool,
+ "Resetting log file due to error opening "
+ "new log file, %10d messages lost: %-25.25s\n",
+ status->nMessCount, error);
- truncate_and_write_error(status);
+ truncate_and_write_error(status, message);
+
+ /* Throw away new state; it isn't going to be used. */
+ apr_pool_destroy(newlog.pool);
}
status->nMessCount = 0;
#if APR_FILES_AS_SOCKETS
apr_pollfd_t pollfd = { 0 };
apr_status_t pollret = APR_SUCCESS;
- int polltimeout;
+ long polltimeout;
#endif
apr_app_initialize(&argc, &argv, NULL);
apr_pool_create(&status.pool, NULL);
apr_getopt_init(&opt, status.pool, argc, argv);
#if APR_FILES_AS_SOCKETS
- while ((rv = apr_getopt(opt, "lL:p:ftvecn:", &c, &opt_arg)) == APR_SUCCESS) {
+ while ((rv = apr_getopt(opt, "lL:p:fDtvecn:", &c, &opt_arg)) == APR_SUCCESS) {
#else
- while ((rv = apr_getopt(opt, "lL:p:ftven:", &c, &opt_arg)) == APR_SUCCESS) {
+ while ((rv = apr_getopt(opt, "lL:p:fDtven:", &c, &opt_arg)) == APR_SUCCESS) {
#endif
switch (c) {
case 'l':
break;
case 'p':
config.postrotate_prog = opt_arg;
+#ifdef SIGCHLD
+ /* Prevent creation of zombies (on modern Unix systems). */
+ apr_signal(SIGCHLD, SIG_IGN);
+#endif
break;
case 'f':
config.force_open = 1;
break;
+ case 'D':
+ config.create_path = 1;
+ break;
case 't':
config.truncate = 1;
break;
usage(argv[0], "Incorrect number of arguments");
}
- config.szLogRoot = argv[opt->ind++];
+ rv = apr_filepath_merge(&config.szLogRoot, "", argv[opt->ind++],
+ APR_FILEPATH_TRUENAME, status.pool);
+ if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
+ usage(argv[0], "Invalid filename given");
+ }
/* Read in the remaining flags, namely time, size and UTC offset. */
for(; opt->ind < argc; opt->ind++) {
nRead = sizeof(buf);
#if APR_FILES_AS_SOCKETS
if (config.create_empty && config.tRotation) {
- polltimeout = status.tLogEnd ? status.tLogEnd - get_now(&config) : config.tRotation;
+ polltimeout = status.tLogEnd ? status.tLogEnd - get_now(&config, NULL) : config.tRotation;
if (polltimeout <= 0) {
pollret = APR_TIMEUP;
}
rv = apr_file_write_full(status.current.fd, buf, nWrite, &nWrite);
if (nWrite != nRead) {
apr_off_t cur_offset;
+ apr_pool_t *pool;
+ char *error;
cur_offset = 0;
if (apr_file_seek(status.current.fd, APR_CUR, &cur_offset) != APR_SUCCESS) {
cur_offset = -1;
}
status.nMessCount++;
- apr_snprintf(status.errbuf, sizeof status.errbuf,
- "Error %d writing to log file at offset %" APR_OFF_T_FMT ". "
- "%10d messages lost (%pm)\n",
- rv, cur_offset, status.nMessCount, &rv);
+ apr_pool_create(&pool, status.pool);
+ error = apr_psprintf(pool, "Error %d writing to log file at offset %"
+ APR_OFF_T_FMT ". %10d messages lost (%pm)\n",
+ rv, cur_offset, status.nMessCount, &rv);
- truncate_and_write_error(&status);
+ truncate_and_write_error(&status, error);
+ apr_pool_destroy(pool);
}
else {
status.nMessCount++;
}
if (config.echo) {
- if (apr_file_write_full(f_stdout, buf, nRead, &nWrite)) {
+ if (apr_file_write_full(f_stdout, buf, nRead, NULL)) {
fprintf(stderr, "Unable to write to stdout\n");
exit(4);
}