1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
56 * Simple program to rotate Apache logs without having to kill the server.
58 * Contributed by Ben Laurie <ben@algroup.co.uk>
62 * Ported to APR by Mladen Turk <mturk@mappingsoft.com>
70 #include "apr_strings.h"
71 #include "apr_errno.h"
72 #include "apr_file_io.h"
73 #include "apr_file_info.h"
74 #include "apr_general.h"
83 #if APR_HAVE_STRINGS_H
94 int main (int argc, const char * const argv[])
96 char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
97 int tLogEnd = 0, tRotation = 0, utc_offset = 0;
98 unsigned int sRotation = 0;
100 apr_size_t nRead, nWrite;
101 int use_strftime = 0;
103 const char *szLogRoot;
104 apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
108 apr_app_initialize(&argc, &argv, NULL);
109 atexit(apr_terminate);
111 apr_pool_create(&pool, NULL);
112 if (argc < 3 || argc > 4) {
114 "Usage: %s <logfile> <rotation time in seconds> "
115 "[offset minutes from UTC] or <rotation size in megabytes>\n\n",
119 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
123 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
126 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
129 "to httpd.conf. The generated name will be /some/where.nnnn "
130 "where nnnn is the\nsystem time at which the log nominally "
131 "starts (N.B. if using a rotation time,\nthe time will always "
132 "be a multiple of the rotation time, so you can synchronize\n"
133 "cron scripts with it). At the end of each rotation time or "
134 "when the file size\nis reached a new log is started.\n");
140 ptr = strchr (argv[2], 'M');
142 if (*(ptr+1) == '\0') {
143 sRotation = atoi(argv[2]) * 1048576;
145 if (sRotation == 0) {
146 fprintf(stderr, "Invalid rotation size parameter\n");
152 utc_offset = atoi(argv[3]) * 60;
154 tRotation = atoi(argv[2]);
155 if (tRotation <= 0) {
156 fprintf(stderr, "Rotation time must be > 0\n");
161 use_strftime = (strchr(szLogRoot, '%') != NULL);
162 if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
163 fprintf(stderr, "Unable to open stdin\n");
169 if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
172 now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
173 if (nLogFD != NULL && now >= tLogEnd) {
178 else if (sRotation) {
180 apr_off_t current_size = -1;
182 if ((nLogFD != NULL) &&
183 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
184 current_size = finfo.size;
187 if (current_size > sRotation) {
193 fprintf(stderr, "No rotation time or size specified\n");
197 if (nLogFD == NULL) {
201 tLogStart = (now / tRotation) * tRotation;
203 tLogStart = (int)apr_time_sec(apr_time_now());
206 apr_time_t tNow = apr_time_from_sec(tLogStart);
210 apr_time_exp_gmt(&e, tNow);
211 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
214 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
216 tLogEnd = tLogStart + tRotation;
217 apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
218 APR_OS_DEFAULT, pool);
219 if (nLogFD == NULL) {
220 /* Uh-oh. Failed to open the new log file. Try to clear
221 * the previous log file, note the lost log entries,
222 * and keep on truckin'. */
223 if (nLogFDprev == NULL) {
224 fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
230 "Resetting log file due to error opening "
231 "new log file. %10d messages lost.\n",
233 nWrite = strlen(errbuf);
234 apr_file_trunc(nLogFD, 0);
235 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
236 fprintf(stderr, "Error writing to the file %s\n", buf2);
241 else if (nLogFDprev) {
242 apr_file_close(nLogFDprev);
247 apr_file_write(nLogFD, buf, &nWrite);
248 if (nWrite != nRead) {
251 "Error writing to log file. "
252 "%10d messages lost.\n",
254 nWrite = strlen(errbuf);
255 apr_file_trunc(nLogFD, 0);
256 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
257 fprintf(stderr, "Error writing to the file %s\n", buf2);
265 /* Of course we never, but prevent compiler warnings */