From a45cbef86be2508c39b74d65aa15085211c46c8e Mon Sep 17 00:00:00 2001 From: Joe Orton Date: Wed, 26 Oct 2011 13:54:42 +0000 Subject: [PATCH] * support/rotatelogs.c (usage, main): Add support for -c option. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Submitted by: Jan Kaluža git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1189220 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES | 5 ++- docs/manual/programs/rotatelogs.xml | 4 ++ support/rotatelogs.c | 68 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index c700897c8e0..afc323ef89c 100644 --- a/CHANGES +++ b/CHANGES @@ -12,6 +12,9 @@ Changes with Apache 2.3.15 PR 51714. [Stefan Fritsch, Jim Jagielski, Ruediger Pluem, Eric Covener, ] + *) rotatelogs: Add -c option to force logfile creation in every rotation + interval, even if empty. [Jan Kaluža ] + *) core: Limit ap_pregsub() to 64K, add ap_pregsub_ex() for longer strings. [Stefan Fritsch] @@ -62,7 +65,7 @@ Changes with Apache 2.3.15 name have been merged. [Stefan Fritsch] *) mod_ssl: If MaxMemFree is set, ask OpenSSL >= 1.0.0 to reduce memory - usage. PR 51618. [Cristian Rodríguez , + usage. PR 51618. [Cristian Rodríguez , Stefan Fritsch] *) mod_ssl: At startup, when checking a server certificate whether it diff --git a/docs/manual/programs/rotatelogs.xml b/docs/manual/programs/rotatelogs.xml index 4eee1b18540..bae96f84355 100644 --- a/docs/manual/programs/rotatelogs.xml +++ b/docs/manual/programs/rotatelogs.xml @@ -40,6 +40,7 @@ [ -f ] [ -v ] [ -e ] + [ -c ] logfile rotationtime|filesize(B|K|M|G) [ offset ]

@@ -98,6 +99,9 @@ close actions.
Echo logs through to stdout. Useful when logs need to be further processed in real time by a further tool in the chain.
+
-c
+
Create log file for each interval, even if empty.
+
logfile

The path plus basename of the logfile. If logfile diff --git a/support/rotatelogs.c b/support/rotatelogs.c index c026a070159..81bd950fc2e 100644 --- a/support/rotatelogs.c +++ b/support/rotatelogs.c @@ -49,6 +49,9 @@ #include "apr_time.h" #include "apr_getopt.h" #include "apr_thread_proc.h" +#if APR_FILES_AS_SOCKETS +#include "apr_poll.h" +#endif #if APR_HAVE_STDLIB_H #include @@ -93,6 +96,9 @@ struct rotate_config { int truncate; const char *linkfile; const char *postrotate_prog; +#if APR_FILES_AS_SOCKETS + int create_empty; +#endif }; typedef struct rotate_status rotate_status_t; @@ -123,7 +129,11 @@ static void usage(const char *argv0, const char *reason) fprintf(stderr, "%s\n", reason); } fprintf(stderr, +#if APR_FILES_AS_SOCKETS + "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] [-c] " +#else "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] " +#endif "{|(B|K|M|G)} " "[offset minutes from UTC]\n\n", argv0); @@ -156,6 +166,9 @@ static void usage(const char *argv0, const char *reason) " -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" +#if APR_FILES_AS_SOCKETS + " -c Create log even if it is empty.\n" +#endif "\n" "The program is invoked as \"[prog] []\"\n" "where is the filename of the newly opened logfile, and\n" @@ -208,6 +221,9 @@ static void dumpConfig (rotate_config_t *config) 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, "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); } @@ -518,6 +534,11 @@ int main (int argc, const char * const argv[]) char c; const char *opt_arg; const char *err = NULL; +#if APR_FILES_AS_SOCKETS + apr_pollfd_t pollfd = { 0 }; + apr_status_t pollret = APR_SUCCESS; + int polltimeout; +#endif apr_app_initialize(&argc, &argv, NULL); atexit(apr_terminate); @@ -528,7 +549,11 @@ int main (int argc, const char * const argv[]) 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:ftvec", &c, &opt_arg)) == APR_SUCCESS) { +#else while ((rv = apr_getopt(opt, "lL:p:ftve", &c, &opt_arg)) == APR_SUCCESS) { +#endif switch (c) { case 'l': config.use_localtime = 1; @@ -551,6 +576,11 @@ int main (int argc, const char * const argv[]) case 'e': config.echo = 1; break; +#if APR_FILES_AS_SOCKETS + case 'c': + config.create_empty = 1; + break; +#endif } } @@ -596,6 +626,15 @@ int main (int argc, const char * const argv[]) dumpConfig(&config); } +#if APR_FILES_AS_SOCKETS + if (config.create_empty && config.tRotation) { + pollfd.p = status.pool; + pollfd.desc_type = APR_POLL_FILE; + pollfd.reqevents = APR_POLLIN; + pollfd.desc.f = f_stdin; + } +#endif + /* * Immediately open the logfile as we start, if we were forced * to do so via '-f'. @@ -606,6 +645,34 @@ int main (int argc, const char * const argv[]) for (;;) { nRead = sizeof(buf); +#if APR_FILES_AS_SOCKETS + if (config.create_empty && config.tRotation) { + polltimeout = status.tLogEnd ? status.tLogEnd - get_now(&config) : config.tRotation; + if (polltimeout <= 0) { + pollret = APR_TIMEUP; + } + else { + pollret = apr_poll(&pollfd, 1, &pollret, apr_time_from_sec(polltimeout)); + } + } + if (pollret == APR_SUCCESS) { + rv = apr_file_read(f_stdin, buf, &nRead); + if (APR_STATUS_IS_EOF(rv)) { + break; + } + else if (rv != APR_SUCCESS) { + exit(3); + } + } + else if (pollret == APR_TIMEUP) { + *buf = 0; + nRead = 0; + } + else { + fprintf(stderr, "Unable to poll stdin\n"); + exit(5); + } +#else /* APR_FILES_AS_SOCKETS */ rv = apr_file_read(f_stdin, buf, &nRead); if (APR_STATUS_IS_EOF(rv)) { break; @@ -613,6 +680,7 @@ int main (int argc, const char * const argv[]) else if (rv != APR_SUCCESS) { exit(3); } +#endif /* APR_FILES_AS_SOCKETS */ checkRotate(&config, &status); if (status.rotateReason != ROTATE_NONE) { doRotate(&config, &status); -- 2.47.2