1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 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, char *argv[])
96 char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
97 int tLogEnd = 0, tRotation, utc_offset = 0;
99 apr_size_t nRead, nWrite;
100 int use_strftime = 0;
103 apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
107 atexit(apr_terminate);
109 apr_pool_create(&pool, NULL);
110 if (argc < 3 || argc > 4) {
112 "Usage: %s <logfile> <rotation time in seconds> "
113 "[offset minutes from UTC]\n\n",
117 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
121 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
125 "to httpd.conf. The generated name will be /some/where.nnnn "
126 "where nnnn is the\nsystem time at which the log nominally "
127 "starts (N.B. this time will always be a\nmultiple of the "
128 "rotation time, so you can synchronize cron scripts with it).\n"
129 "At the end of each rotation time a new log is started.\n");
135 utc_offset = atoi(argv[3]) * 60;
137 tRotation = atoi(argv[2]);
138 if (tRotation <= 0) {
139 fprintf(stderr, "Rotation time must be > 0\n");
143 use_strftime = (strchr(szLogRoot, '%') != NULL);
144 if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
145 fprintf(stderr, "Unable to open stdin\n");
151 if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
153 now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
156 if (nLogFD != NULL && (now >= tLogEnd || nRead < 0)) {
160 if (nLogFD == NULL) {
161 int tLogStart = (now / tRotation) * tRotation;
163 apr_time_t tNow = tLogStart * APR_USEC_PER_SEC;
164 apr_exploded_time_t e;
167 apr_explode_gmt(&e, tNow);
168 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
171 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
173 tLogEnd = tLogStart + tRotation;
174 apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
175 APR_OS_DEFAULT, pool);
176 if (nLogFD == NULL) {
177 /* Uh-oh. Failed to open the new log file. Try to clear
178 * the previous log file, note the lost log entries,
179 * and keep on truckin'. */
180 if (nLogFDprev == NULL) {
181 fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
187 "Resetting log file due to error opening "
188 "new log file. %10d messages lost.\n",
190 nWrite = strlen(errbuf);
191 apr_file_trunc(nLogFD, 0);
192 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
193 fprintf(stderr, "Error witing to the file %s\n", buf2);
198 else if (nLogFDprev) {
199 apr_file_close(nLogFDprev);
205 apr_file_write(nLogFD, buf, &nWrite);
206 } while (nWrite < 0 && errno == EINTR);
207 if (nWrite != nRead) {
210 "Error writing to log file. "
211 "%10d messages lost.\n",
213 nWrite = strlen(errbuf);
214 apr_file_trunc(nLogFD, 0);
215 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
216 fprintf(stderr, "Error witing to the file %s\n", buf2);
224 /* Of course we never, but prevent compiler warnings */