#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
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 logfile current; /* current logfile. */
apr_pool_t *pool; /* top-level pool */
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] [-d] [-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] [-d] [-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"
+ " -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);
}
-/* This function returns the current Unix time (time_t) plus any
- * configured or derived local time offset. The offset applied is
+/* 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 int get_now(rotate_config_t *config, apr_int32_t *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;
+ apr_int32_t utc_offset;
if (config->use_localtime) {
/* Check for our UTC offset before using it, since it might
if (offset)
*offset = utc_offset;
- return (int)apr_time_sec(tNow) + utc_offset;
+ return apr_time_sec(tNow) + utc_offset;
}
/*
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");
}
/*
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) {
static void doRotate(rotate_config_t *config, rotate_status_t *status)
{
apr_int32_t offset;
- int now;
- int tLogStart;
+ adjusted_time_t now, tLogStart;
apr_status_t rv;
struct logfile newlog;
int thisLogNum = -1;
status->rotateReason = ROTATE_NONE;
if (config->tRotation) {
- int tLogEnd;
+ adjusted_time_t tLogEnd;
+
tLogStart = (now / config->tRotation) * config->tRotation;
tLogEnd = tLogStart + config->tRotation;
/*
}
}
else {
- apr_snprintf(newlog.name, sizeof(newlog.name), "%s.%010d", config->szLogRoot,
+ apr_snprintf(newlog.name, sizeof(newlog.name), "%s.%010ld", config->szLogRoot,
tLogStart);
}
}
#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:fdtvecn:", &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:fdtven:", &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':
+ case 'D':
config.create_path = 1;
break;
case 't':
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);
}