]> granicus.if.org Git - apache/blob - support/rotatelogs.c
Added log rotation based on file size to the RotateLog support utility.
[apache] / support / rotatelogs.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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.
25  *
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.
30  *
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.
34  *
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
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
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/>.
53  */
54
55 /*
56  * Simple program to rotate Apache logs without having to kill the server.
57  *
58  * Contributed by Ben Laurie <ben@algroup.co.uk>
59  *
60  * 12 Mar 1996
61  *
62  * Ported to APR by Mladen Turk <mturk@mappingsoft.com>
63  *
64  * 23 Sep 2001
65  */
66
67
68 #include "apr.h"
69 #include "apr_lib.h"
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"
75 #include "apr_time.h"
76
77 #if APR_HAVE_STDLIB_H
78 #include <stdlib.h>
79 #endif
80 #if APR_HAVE_STRING_H
81 #include <string.h>
82 #endif
83 #if APR_HAVE_STRINGS_H
84 #include <strings.h>
85 #endif
86
87 #define BUFSIZE         65536
88 #define ERRMSGSZ        82
89
90 #ifndef MAX_PATH
91 #define MAX_PATH        1024
92 #endif
93
94 int main (int argc, const char * const argv[])
95 {
96     char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
97     int tLogEnd = 0, tRotation = 0, utc_offset = 0;
98     unsigned int sRotation = 0;
99     int nMessCount = 0;
100     apr_size_t nRead, nWrite;
101     int use_strftime = 0;
102     int now;
103     const char *szLogRoot;
104     apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
105     apr_pool_t *pool;
106     char *ptr = NULL;
107
108     apr_app_initialize(&argc, &argv, NULL);
109     atexit(apr_terminate);
110
111     apr_pool_create(&pool, NULL);
112     if (argc < 3 || argc > 4) {
113         fprintf(stderr,
114                 "Usage: %s <logfile> <rotation time in seconds> "
115                 "[offset minutes from UTC] or <rotation size in megabytes>\n\n",
116                 argv[0]);
117 #ifdef OS2
118         fprintf(stderr,
119                 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
120                 argv[0]);
121 #else
122         fprintf(stderr,
123                 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
124                 argv[0]);
125         fprintf(stderr,
126                 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
127 #endif
128         fprintf(stderr,
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");
135         exit(1);
136     }
137
138     szLogRoot = argv[1];
139
140     ptr = strchr (argv[2], 'M');
141     if (ptr) {
142         if (*(ptr+1) == '\0') {
143             sRotation = atoi(argv[2]) * 1048576;
144         }
145         if (sRotation == 0) {
146             fprintf(stderr, "Invalid rotation size parameter\n");
147             exit(1);
148         }
149     }
150     else {
151         if (argc >= 4) {
152             utc_offset = atoi(argv[3]) * 60;
153         }
154         tRotation = atoi(argv[2]);
155         if (tRotation <= 0) {
156             fprintf(stderr, "Rotation time must be > 0\n");
157             exit(6);
158         }
159     }
160
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");
164         exit(1);
165     }
166
167     for (;;) {
168         nRead = sizeof(buf);
169         if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
170             exit(3);
171         if (nRead == 0)
172             exit(3);
173         if (tRotation) {
174             now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
175             if (nLogFD != NULL && (now >= tLogEnd || nRead < 0)) {
176                 nLogFDprev = nLogFD;
177                 nLogFD = NULL;
178             }
179         }
180         else if (sRotation) {
181             apr_finfo_t finfo;
182             unsigned int current_size = -1;
183
184             if ((nLogFD != NULL) && 
185                 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
186                 current_size = finfo.size;
187             }
188
189             if (current_size > sRotation || nRead < 0) {
190                 nLogFDprev = nLogFD;
191                 nLogFD = NULL;
192             }
193         }
194         else {
195             fprintf(stderr, "No rotation time or size specified\n");
196             exit(2);
197         }
198
199         if (nLogFD == NULL) {
200             int tLogStart;
201                 
202             if (tRotation)
203                 tLogStart = (now / tRotation) * tRotation;
204             else
205                 tLogStart = apr_time_now() / APR_USEC_PER_SEC;
206
207             if (use_strftime) {
208                         apr_time_t tNow = tLogStart * APR_USEC_PER_SEC;
209                 apr_time_exp_t e;
210                 apr_size_t rs;
211
212                 apr_time_exp_gmt(&e, tNow);
213                 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
214             }
215             else {
216                 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
217             }
218             tLogEnd = tLogStart + tRotation;
219             apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
220                           APR_OS_DEFAULT, pool);
221             if (nLogFD == NULL) {
222                 /* Uh-oh. Failed to open the new log file. Try to clear
223                  * the previous log file, note the lost log entries,
224                  * and keep on truckin'. */
225                 if (nLogFDprev == NULL) {
226                     fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
227                     exit(2);
228                 }
229                 else {
230                     nLogFD = nLogFDprev;
231                     sprintf(errbuf,
232                             "Resetting log file due to error opening "
233                             "new log file. %10d messages lost.\n",
234                             nMessCount);
235                     nWrite = strlen(errbuf);
236                     apr_file_trunc(nLogFD, 0);
237                     if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
238                         fprintf(stderr, "Error writing to the file %s\n", buf2);
239                         exit(2);
240                     }
241                 }
242             }
243             else if (nLogFDprev) {
244                 apr_file_close(nLogFDprev);
245             }
246             nMessCount = 0;
247         }
248         do {
249             nWrite = nRead;
250             apr_file_write(nLogFD, buf, &nWrite);
251         } while (nWrite < 0 && errno == EINTR);
252         if (nWrite != nRead) {
253             nMessCount++;
254             sprintf(errbuf,
255                     "Error writing to log file. "
256                     "%10d messages lost.\n",
257                     nMessCount);
258             nWrite = strlen(errbuf);
259             apr_file_trunc(nLogFD, 0);
260             if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
261                 fprintf(stderr, "Error writing to the file %s\n", buf2);
262                 exit(2);
263             }
264         }
265         else {
266             nMessCount++;
267         }
268     }
269     /* Of course we never, but prevent compiler warnings */
270     return 0;
271 }