1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Simple program to rotate Apache logs without having to kill the server.
20 * Contributed by Ben Laurie <ben algroup.co.uk>
24 * Ported to APR by Mladen Turk <mturk mappingsoft.com>
28 * -l option added 2004-06-11
30 * -l causes the use of local time rather than GMT as the base for the
31 * interval. NB: Using -l in an environment which changes the GMT offset
32 * (such as for BST or DST) can lead to unpredictable results!
34 * -f option added Feb, 2008. This causes rotatelog to open/create
35 * the logfile as soon as it's started, not as soon as it sees
42 #include "apr_strings.h"
43 #include "apr_errno.h"
44 #include "apr_file_io.h"
45 #include "apr_file_info.h"
46 #include "apr_general.h"
48 #include "apr_getopt.h"
56 #if APR_HAVE_STRINGS_H
67 typedef struct rotate_config rotate_config_t;
69 struct rotate_config {
70 unsigned int sRotation;
76 const char *szLogRoot;
79 typedef struct rotate_status rotate_status_t;
81 struct rotate_status {
84 apr_pool_t *pfile_prev;
86 apr_file_t *nLogFDprev;
87 char filename[MAX_PATH];
88 char errbuf[ERRMSGSZ];
95 static rotate_config_t config;
96 static rotate_status_t status;
98 static void usage(const char *argv0, const char *reason)
101 fprintf(stderr, "%s\n", reason);
104 "Usage: %s [-l] [-f] <logfile> "
105 "{<rotation time in seconds>|<rotation size in megabytes>} "
106 "[offset minutes from UTC]\n\n",
110 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
114 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
117 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv0);
120 "to httpd.conf. The generated name will be /some/where.nnnn "
121 "where nnnn is the\nsystem time at which the log nominally "
122 "starts (N.B. if using a rotation time,\nthe time will always "
123 "be a multiple of the rotation time, so you can synchronize\n"
124 "cron scripts with it). At the end of each rotation time or "
125 "when the file size\nis reached a new log is started.\n");
129 static int get_now(int use_localtime, int utc_offset)
131 apr_time_t tNow = apr_time_now();
133 /* Check for our UTC offset before using it, since it might
134 * change if there's a switch between standard and daylight
138 apr_time_exp_lt(<, tNow);
139 utc_offset = lt.tm_gmtoff;
141 return (int)apr_time_sec(tNow) + utc_offset;
144 void checkRotate(rotate_config_t *config, rotate_status_t *status) {
146 if (status->nLogFD == NULL) {
147 status->needsRotate = 1;
149 else if (config->tRotation) {
150 status->now = get_now(config->use_localtime, config->utc_offset);
151 if (status->now >= status->tLogEnd) {
152 status->needsRotate = 1;
155 else if (config->sRotation) {
157 apr_off_t current_size = -1;
159 if (apr_file_info_get(&finfo, APR_FINFO_SIZE, status->nLogFD) == APR_SUCCESS) {
160 current_size = finfo.size;
163 if (current_size > config->sRotation) {
164 status->needsRotate = 1;
168 fprintf(stderr, "No rotation time or size specified\n");
175 void doRotate(rotate_config_t *config, rotate_status_t *status) {
180 status->needsRotate = 0;
181 status->nLogFDprev = status->nLogFD;
182 status->nLogFD = NULL;
183 status->pfile_prev = status->pfile;
185 status->now = get_now(config->use_localtime, config->utc_offset);
186 if (config->tRotation) {
187 tLogStart = (status->now / config->tRotation) * config->tRotation;
188 status->tLogEnd = tLogStart + config->tRotation;
191 tLogStart = status->now;
194 if (config->use_strftime) {
195 apr_time_t tNow = apr_time_from_sec(tLogStart);
199 apr_time_exp_gmt(&e, tNow);
200 apr_strftime(status->filename, &rs, sizeof(status->filename), config->szLogRoot, &e);
203 sprintf(status->filename, "%s.%010d", config->szLogRoot, tLogStart);
205 apr_pool_create(&status->pfile, status->pool);
206 rv = apr_file_open(&status->nLogFD, status->filename, APR_WRITE | APR_CREATE | APR_APPEND,
207 APR_OS_DEFAULT, status->pfile);
208 if (rv != APR_SUCCESS) {
211 apr_strerror(rv, error, sizeof error);
213 /* Uh-oh. Failed to open the new log file. Try to clear
214 * the previous log file, note the lost log entries,
215 * and keep on truckin'. */
216 if (status->nLogFDprev == NULL) {
217 fprintf(stderr, "Could not open log file '%s' (%s)\n", status->filename, error);
222 status->nLogFD = status->nLogFDprev;
223 apr_pool_destroy(status->pfile);
224 status->pfile = status->pfile_prev;
225 /* Try to keep this error message constant length
226 * in case it occurs several times. */
227 apr_snprintf(status->errbuf, sizeof status->errbuf,
228 "Resetting log file due to error opening "
229 "new log file, %10d messages lost: %-25.25s\n",
230 status->nMessCount, error);
231 nWrite = strlen(status->errbuf);
232 apr_file_trunc(status->nLogFD, 0);
233 if (apr_file_write(status->nLogFD, status->errbuf, &nWrite) != APR_SUCCESS) {
234 fprintf(stderr, "Error writing to the file %s\n", status->filename);
239 else if (status->nLogFDprev) {
240 apr_file_close(status->nLogFDprev);
241 if (status->pfile_prev) {
242 apr_pool_destroy(status->pfile_prev);
243 status->pfile_prev = NULL;
246 status->nMessCount = 0;
249 int main (int argc, const char * const argv[])
252 apr_size_t nRead, nWrite;
260 apr_app_initialize(&argc, &argv, NULL);
261 atexit(apr_terminate);
263 config.sRotation = 0;
264 config.tRotation = 0;
265 config.utc_offset = 0;
266 config.use_localtime = 0;
267 config.use_strftime = 0;
268 config.force_open = 0;
271 status.pfile_prev = NULL;
272 status.nLogFD = NULL;
273 status.nLogFDprev = NULL;
275 status.needsRotate = 0;
277 status.nMessCount = 0;
279 apr_pool_create(&status.pool, NULL);
280 apr_getopt_init(&opt, status.pool, argc, argv);
281 while ((rv = apr_getopt(opt, "lf", &c, &optarg)) == APR_SUCCESS) {
284 config.use_localtime = 1;
287 config.force_open = 1;
293 usage(argv[0], NULL /* specific error message already issued */ );
296 if (opt->ind + 2 != argc && opt->ind + 3 != argc) {
297 usage(argv[0], "Incorrect number of arguments");
300 config.szLogRoot = argv[opt->ind++];
302 ptr = strchr(argv[opt->ind], 'M');
303 if (ptr) { /* rotation based on file size */
304 if (*(ptr+1) == '\0') {
305 config.sRotation = atoi(argv[opt->ind]) * 1048576;
307 if (config.sRotation == 0) {
308 usage(argv[0], "Invalid rotation size parameter");
311 else { /* rotation based on elapsed time */
312 config.tRotation = atoi(argv[opt->ind]);
313 if (config.tRotation <= 0) {
314 usage(argv[0], "Invalid rotation time parameter");
319 if (opt->ind < argc) { /* have UTC offset */
320 if (config.use_localtime) {
321 usage(argv[0], "UTC offset parameter is not valid with -l");
323 config.utc_offset = atoi(argv[opt->ind]) * 60;
326 config.use_strftime = (strchr(config.szLogRoot, '%') != NULL);
327 if (apr_file_open_stdin(&f_stdin, status.pool) != APR_SUCCESS) {
328 fprintf(stderr, "Unable to open stdin\n");
333 * Immediately open the logfile as we start, if we were forced
336 if (config.force_open) {
337 doRotate(&config, &status);
342 if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS) {
345 checkRotate(&config, &status);
346 if (status.needsRotate) {
347 doRotate(&config, &status);
351 rv = apr_file_write(status.nLogFD, buf, &nWrite);
352 if (rv == APR_SUCCESS && nWrite != nRead) {
353 /* buffer partially written, which for rotatelogs means we encountered
354 * an error such as out of space or quota or some other limit reached;
355 * try to write the rest so we get the real error code
357 apr_size_t nWritten = nWrite;
359 nRead = nRead - nWritten;
361 rv = apr_file_write(status.nLogFD, buf + nWritten, &nWrite);
363 if (nWrite != nRead) {
365 apr_off_t cur_offset;
368 if (apr_file_seek(status.nLogFD, APR_CUR, &cur_offset) != APR_SUCCESS) {
371 apr_strerror(rv, strerrbuf, sizeof strerrbuf);
373 apr_snprintf(status.errbuf, sizeof status.errbuf,
374 "Error %d writing to log file at offset %" APR_OFF_T_FMT ". "
375 "%10d messages lost (%s)\n",
376 rv, cur_offset, status.nMessCount, strerrbuf);
377 nWrite = strlen(status.errbuf);
378 apr_file_trunc(status.nLogFD, 0);
379 if (apr_file_write(status.nLogFD, status.errbuf, &nWrite) != APR_SUCCESS) {
380 fprintf(stderr, "Error writing to the file %s\n", status.filename);
388 /* Of course we never, but prevent compiler warnings */