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 static void usage(const char *argv0, const char *reason)
70 fprintf(stderr, "%s\n", reason);
73 "Usage: %s [-l] [-f] <logfile> "
74 "{<rotation time in seconds>|<rotation size in megabytes>} "
75 "[offset minutes from UTC]\n\n",
79 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
83 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
86 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv0);
89 "to httpd.conf. The generated name will be /some/where.nnnn "
90 "where nnnn is the\nsystem time at which the log nominally "
91 "starts (N.B. if using a rotation time,\nthe time will always "
92 "be a multiple of the rotation time, so you can synchronize\n"
93 "cron scripts with it). At the end of each rotation time or "
94 "when the file size\nis reached a new log is started.\n");
98 static int get_now(int use_localtime, int utc_offset)
100 apr_time_t tNow = apr_time_now();
102 /* Check for our UTC offset before using it, since it might
103 * change if there's a switch between standard and daylight
107 apr_time_exp_lt(<, tNow);
108 utc_offset = lt.tm_gmtoff;
110 return (int)apr_time_sec(tNow) + utc_offset;
113 int main (int argc, const char * const argv[])
115 char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
116 int tLogEnd = 0, tRotation = 0, utc_offset = 0;
117 unsigned int sRotation = 0;
119 apr_size_t nRead, nWrite;
120 int use_strftime = 0;
121 int use_localtime = 0;
124 const char *szLogRoot;
125 apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
127 apr_pool_t *pfile = NULL;
128 apr_pool_t *pfile_prev = NULL;
135 apr_app_initialize(&argc, &argv, NULL);
136 atexit(apr_terminate);
138 apr_pool_create(&pool, NULL);
139 apr_getopt_init(&opt, pool, argc, argv);
140 while ((rv = apr_getopt(opt, "lf", &c, &optarg)) == APR_SUCCESS) {
152 usage(argv[0], NULL /* specific error message already issued */ );
155 if (opt->ind + 2 != argc && opt->ind + 3 != argc) {
156 usage(argv[0], "Incorrect number of arguments");
159 szLogRoot = argv[opt->ind++];
161 ptr = strchr(argv[opt->ind], 'M');
162 if (ptr) { /* rotation based on file size */
163 if (*(ptr+1) == '\0') {
164 sRotation = atoi(argv[opt->ind]) * 1048576;
166 if (sRotation == 0) {
167 usage(argv[0], "Invalid rotation size parameter");
170 else { /* rotation based on elapsed time */
171 tRotation = atoi(argv[opt->ind]);
172 if (tRotation <= 0) {
173 usage(argv[0], "Invalid rotation time parameter");
178 if (opt->ind < argc) { /* have UTC offset */
180 usage(argv[0], "UTC offset parameter is not valid with -l");
182 utc_offset = atoi(argv[opt->ind]) * 60;
185 use_strftime = (strchr(szLogRoot, '%') != NULL);
186 if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
187 fprintf(stderr, "Unable to open stdin\n");
194 * Bypass reading stdin if we are forcing the logfile
195 * to be opened as soon as we start. Since we won't be
196 * writing anything, we just want to open the file.
197 * First time through is the only time we do this
198 * since we reset bypass_io after the 1st loop
201 if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS) {
206 now = get_now(use_localtime, utc_offset);
207 if (nLogFD != NULL && now >= tLogEnd) {
212 else if (sRotation) {
214 apr_off_t current_size = -1;
216 if ((nLogFD != NULL) &&
217 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
218 current_size = finfo.size;
221 if (current_size > sRotation) {
227 fprintf(stderr, "No rotation time or size specified\n");
231 if (nLogFD == NULL) {
236 tLogStart = (now / tRotation) * tRotation;
239 tLogStart = get_now(use_localtime, utc_offset);
243 apr_time_t tNow = apr_time_from_sec(tLogStart);
247 apr_time_exp_gmt(&e, tNow);
248 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
251 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
253 tLogEnd = tLogStart + tRotation;
255 apr_pool_create(&pfile, pool);
256 rv = apr_file_open(&nLogFD, buf2, APR_WRITE | APR_CREATE | APR_APPEND,
257 APR_OS_DEFAULT, pfile);
258 if (rv != APR_SUCCESS) {
261 apr_strerror(rv, error, sizeof error);
263 /* Uh-oh. Failed to open the new log file. Try to clear
264 * the previous log file, note the lost log entries,
265 * and keep on truckin'. */
266 if (nLogFDprev == NULL) {
267 fprintf(stderr, "Could not open log file '%s' (%s)\n", buf2, error);
272 apr_pool_destroy(pfile);
274 /* Try to keep this error message constant length
275 * in case it occurs several times. */
276 apr_snprintf(errbuf, sizeof errbuf,
277 "Resetting log file due to error opening "
278 "new log file, %10d messages lost: %-25.25s\n",
280 nWrite = strlen(errbuf);
281 apr_file_trunc(nLogFD, 0);
282 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
283 fprintf(stderr, "Error writing to the file %s\n", buf2);
288 else if (nLogFDprev) {
289 apr_file_close(nLogFDprev);
291 apr_pool_destroy(pfile_prev);
297 * If we just bypassed reading stdin, due to bypass_io,
298 * then we have nothing to write, so skip this.
302 rv = apr_file_write(nLogFD, buf, &nWrite);
303 if (rv == APR_SUCCESS && nWrite != nRead) {
304 /* buffer partially written, which for rotatelogs means we encountered
305 * an error such as out of space or quota or some other limit reached;
306 * try to write the rest so we get the real error code
308 apr_size_t nWritten = nWrite;
310 nRead = nRead - nWritten;
312 rv = apr_file_write(nLogFD, buf + nWritten, &nWrite);
314 if (nWrite != nRead) {
316 apr_off_t cur_offset;
319 if (apr_file_seek(nLogFD, APR_CUR, &cur_offset) != APR_SUCCESS) {
322 apr_strerror(rv, strerrbuf, sizeof strerrbuf);
324 apr_snprintf(errbuf, sizeof errbuf,
325 "Error %d writing to log file at offset %" APR_OFF_T_FMT ". "
326 "%10d messages lost (%s)\n",
327 rv, cur_offset, nMessCount, strerrbuf);
328 nWrite = strlen(errbuf);
329 apr_file_trunc(nLogFD, 0);
330 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
331 fprintf(stderr, "Error writing to the file %s\n", buf2);
340 /* now worry about reading 'n writing all the time */
344 /* Of course we never, but prevent compiler warnings */